// JavaScript Document
var xmlHttp = false;
var xmlBusy = false;
var loadCount = 0;
try {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
    try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) {
        xmlHttp = false;
    }
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
}

function SaveOptInEmail() {
	var email = document.getElementById("txtoptinemail");	
	if(validateEmail(email.value)) {
		try{
			xmlHttp.open("POST", "/includes/SQL/optin-email.php", true);
			var stuff = "email="+email.value;
	 
			xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-Length",stuff.length);
			xmlHttp.send(stuff);
		   
			xmlHttp.onreadystatechange = function(){
				if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
					document.getElementById('divoptinemail').innerHTML 	= xmlHttp.responseText;
					document.getElementById('txtoptinemail').value 			= "";
					window.location  									= '/optin-success.php';
				}
			};
		} catch(e) {
			//location = "includes/SQL/getChatCust.php";
		}
		
	} else {
		alert('Invalid Email Address');	
		return;
	}
}

function loginuser() {
	var txtusername = document.getElementById("txtusername").value;
	var txtpassword = document.getElementById("txtpassword").value;
	var chkremember = document.getElementById("chkremember");
	var rememberme	= 'false';
	if(chkremember) {
		if(chkremember.checked)
			rememberme	= 'true';
		else
			rememberme   = 'false';
	}
	try{
		xmlHttp.open("POST", "/includes/SQL/verify-login.php", true);
		var stuff = "email="+txtusername+"&password="+txtpassword+"&rememberme="+rememberme;
 
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-Length",stuff.length);
		xmlHttp.send(stuff);
		   
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
				if(xmlHttp.responseText == "") {
					window.location = "/profile.php";
				} else {
					document.getElementById('divlogin').innerHTML 	= xmlHttp.responseText;
				}
			}
		};
	} catch(e) {
		//location = "includes/SQL/getChatCust.php";
	}
	
}

function forgotpassword() {
	var txtemail = document.getElementById("txtemail").value;
	
	try{
		xmlHttp.open("POST", "/includes/SQL/forgotpassword.php", true);
		var stuff = "email="+txtemail;
 
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-Length",stuff.length);
		xmlHttp.send(stuff);
		   
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
				if(xmlHttp.responseText == "") {
					document.getElementById('divforgotpass').innerHTML 	= xmlHttp.responseText;
				} else {
					document.getElementById('divforgotpass').innerHTML 	= xmlHttp.responseText;
				}
			}
		};
	} catch(e) {
		//location = "includes/SQL/getChatCust.php";
	}
	
}

function _UpdatePassword(uid, password, container) {
	try{
		xmlHttp.open("POST", "/includes/SQL/updatepassword.php", true);
		var stuff = "uid="+uid+"&password="+password;
 
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-Length",stuff.length);
		xmlHttp.send(stuff);
		   
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
				if(xmlHttp.responseText == "") {
					document.getElementById(container).innerHTML 	= xmlHttp.responseText;
				} else {
					document.getElementById(container).innerHTML 	= xmlHttp.responseText;
				}
			}
		};
	} catch(e) {
		//location = "includes/SQL/getChatCust.php";
	}
	
}

function checkemailexist(formname, destination) {
	var email 		= document.getElementById("txtemail").value
	var myreturn 	= true;
	var obj			= document.forms[formname];
	obj.action 		= destination;

	xmlHttp.open("POST", "/includes/SQL/checkemailexist.php", true);
	var stuff = "email="+email;
 
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-Length",stuff.length);
	xmlHttp.send(stuff);
		   
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			//alert(xmlHttp.responseText);
			if(xmlHttp.responseText == "") {
				//return true;
				document.getElementById("divcheckemail").innerHTML 	= xmlHttp.responseText;
				obj.save_flg.value 	= "save";
				obj.submit();
			} else {
				document.getElementById("divcheckemail").innerHTML 	= xmlHttp.responseText;
				myreturn = false;
				return false;
			}
		}
	};
}

var vb_mail		= false;
function ValRegform(formname, destination) {
	var firstname	= document.getElementById("txtfirstname");
	var lastname	= document.getElementById("txtlastname");
	var email		= document.getElementById("txtemail");
	var password	= document.getElementById("txtpassword");
	var cpassword	= document.getElementById("txtconfirmpassword");
	var msg 		= "";
	
	if ( firstname.value == "" ) 	msg += "Firstname is a Required Field\n";
	if ( lastname.value == "" ) 	msg += "Lastname is a Required Field\n";
	if ( email.value == "" ) 		msg += "email is a Required Field\n";
	if ( password.value == "" ) 	msg += "Password is a Required Field\n";
	if ( cpassword.value == "" )	msg += "Confirm Password is a Required Field\n";
	
	if ( msg != "" ) {
		alert(msg);
		return false;
	}
	
	
	if (password.value != cpassword.value) {
		alert('Password and Confirm Password must be the same');
		return false;
	}

	if ( email.value != "" ) {
		if (!validateEmail(email.value)) {
			alert('Invalid Email Address');
			return false;
		} else {
			//checkemailexist(formname, destination);
			document.regform.action			= destination;
			document.regform.save_flg.value	= 'save';
			document.regform.submit();
		}

	}

	//document.regform.action	= destination;
	//document.regform.submit();
	
}

function ValCorpMemRegform(formname, destination) {
	var firstname	= document.getElementById("txtfirstname");
	var lastname	= document.getElementById("txtlastname");
	var email		= document.getElementById("txtemail");
	var password	= document.getElementById("txtpassword");
	var cpassword	= document.getElementById("txtconfirmpassword");
	var msg 		= "";
	
	if ( firstname.value == "" ) 	msg += "Firstname is a Required Field\n";
	if ( lastname.value == "" ) 	msg += "Lastname is a Required Field\n";
	if ( password.value == "" ) 	msg += "Password is a Required Field\n";
	if ( cpassword.value == "" )	msg += "Confirm Password is a Required Field\n";
	
	if ( msg != "" ) {
		alert(msg);
		return false;
	}
	
	if ( email.value != "" ) {
		if (!validateEmail(email.value)) {
			alert('Invalid Email Address');
			return false;
		} else {
			checkemailexist();
		}

	}

	if (password.value != cpassword.value) {
		alert('Password and Confirm Password must be the same');
		return false;
	}
	
	
}
function validateEmail(strValue) {
	emailRe = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	//emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
	phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/
	return emailRe.test(strValue);
}
