
// Set path to PHP script
//var phpscript = 'http://www.camsnetworkcash.com/scripts/checklogin.php';
var phpscript = 'scripts/checklogin.php';

function createRequestObject() 
{
	var req;

	if(window.XMLHttpRequest)
	{
		// Firefox, Safari, Opera...
		req = new XMLHttpRequest();
	} 
	else if(window.ActiveXObject) 
	{
		// Internet Explorer 5+
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	else 
	{
		// There is an error creating the object,
		// just as an old browser is being used.
		alert('There was a problem creating the XMLHttpRequest object');
	}

	return req;
}

// Make the XMLHttpRequest object
var http = createRequestObject();

function sendRequestPost() 
{		
	var user = document.getElementById('username').value;
	var pass = document.getElementById('password').value;

	// Open PHP script for requests
	http.open('post', phpscript);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponsePost;
	http.send('un=' + user + '&pw=' + pass + '&action=login');
}


function handleResponsePost() 
{
	if(http.readyState == 1)
	{ 
		document.getElementById("response").innerHTML = "<font color='#ffffff'>Validating... Please wait... </font><img src='/images/themes/ncv2/frontend/loadinfo.net-1.gif' border='0'> ";
	} 

	if(http.readyState == 4 && http.status == 200)
	{
		// Text returned from PHP script
		var response = http.responseText;

		if(parseInt(response) == 1)
		{
			// Update ajaxTest2 content
			document.getElementById("response").innerHTML = "<font color='green'>Authenticated!</font>";
			location.href='/members/';
		}
		else
		{
			document.getElementById("response").innerHTML = "<font color='red'>Authentication Fail!</font>";
		}
	}
}

function handleResponsePost_chkpass(new_pass, c_pass) 
{
	var ajax_reponse = document.getElementById("ajax_reponse");
	var val = document.getElementById("val");
	var val_err = document.getElementById("val_err");
	
	if(http.readyState == 1)
	{ 
		val.innerHTML = "<font color='green'>Validating... Please wait... </font>";
		ajax_reponse.value = '0';
	}
	
	if(http.readyState == 4 && http.status == 200)
	{
		var response = parseInt(http.responseText);
		
		if(response == 1)
		{
			ajax_reponse.value = '1';
			val.innerHTML = "<font color='green'>Validated ...</font>";
						
			if(new_pass.value != c_pass.value)
			{
				str = '<font color=\'red\'>Your new password and your confirm password do not match.  Try again.</font>';
				val_err.innerHTML = str;

				new_pass.value = '';
				c_pass.value = '';
				new_pass.focus();
				
				return false;
			}

			if(validatePassword(new_pass), true)
			{
				str = "<font color='green'>To make the change take affect, please check your email and follow the instructions.</font>";
				val_err.innerHTML = str;
				val.innerHTML = '';
				
				document.updatePasswordForm.submit();
			}

		}
		else
		{
			ajax_reponse.value = '0';
			val.innerHTML = "<font color='red'>Incorrect Current Password!</font>";
			new_pass.focus();
		}
	}
}

function sendRequestPost_chkpass(act, pass, new_pass, c_pass) 
{
	var phpscript = '/scripts/checklogin.php';

	http.open('post', phpscript, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = function() { handleResponsePost_chkpass(new_pass, c_pass); };
	http.send('a=' + act + '&p=' + pass.value + '&action=checkpass');

	return false;	
}



