// 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 bookmark(url,title){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(url,title);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(title,url,"");
  } else {
    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
  }
}
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);
}

function SaveOptInEmail() {
	var email = document.getElementById("txtoptinemail");	
	var fullname = "";	
	if(document.getElementById("txtfullname")) {
		fullname = document.getElementById("txtfullname").value;	
	}
	if(validateEmail(email.value)) {
		try{
			
			xmlHttp.open("POST", "includes/SQL/optin-email.php", true);
			var stuff = "email="+email.value+"&fullname="+fullname;
	 		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) {
			alert(e);
			//location = "includes/SQL/getChatCust.php";
		}
		
	} else {
		alert('Invalid Email Address');	
		return;
	}
}

function RemoveOptInEmail() {
	var email = document.getElementById("txtoptinemail");	
	var fullname = "";	
	if(document.getElementById("txtfullname")) {
		fullname = document.getElementById("txtfullname").value;	
	}
	if(validateEmail(email.value)) {
		try{
			
			xmlHttp.open("POST", "includes/SQL/removeoptin-email.php", true);
			var stuff = "email="+email.value+"&fullname="+fullname;
	 		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) {
			alert(e);
			//location = "includes/SQL/getChatCust.php";
		}
		
	} else {
		alert('Invalid Email Address');	
		return;
	}
}

function sameaddress() {
	document.getElementById("txtshipaddress1").value = document.getElementById("txtbilladdress1").value;	
	document.getElementById("txtshipaddress2").value = document.getElementById("txtbilladdress2").value;	
	document.getElementById("txtshipcity").value = document.getElementById("txtbillcity").value;	
	document.getElementById("txtshipstate").value = document.getElementById("txtbillstate").value;	
	document.getElementById("cbxshipcountry").value = document.getElementById("cbxbillcountry").value;	
	document.getElementById("txtshippostalcode").value = document.getElementById("txtbillpostalcode").value;	
}
function ValidateRegPage() {
	var obj = document.registrationform;
	var chk_updatepass = document.getElementById("chk_updatepass");
	var msg = "";
	//alert(TrimAll(document.getElementById("txtpasswordx").value) +' *** '+ TrimAll(document.getElementById("txtconfirmpassword").value));
	if(TrimAll(document.getElementById("txtfirstname").value) == '') msg = msg + " First Name is required \n ";
	if(TrimAll(document.getElementById("txtlastname").value) == '')	msg = msg + " Last Name is required \n ";
	if(TrimAll(document.getElementById("txtbilladdress1").value) == '')	msg = msg + " Billing Address Line 1 is required \n ";
	if(TrimAll(document.getElementById("txtbillcity").value) == '')	msg = msg + " Billing City is required \n ";
	if(TrimAll(document.getElementById("txtbillstate").value) == '')	msg = msg + " Billing State is required \n ";
	if(TrimAll(document.getElementById("txtbillpostalcode").value ) == "")	msg = msg + " Billing Postal Code is required \n ";
	if(TrimAll(document.getElementById("cbxbillcountry").value) == '')	msg = msg + " Billing Country is required \n ";
	if(TrimAll(document.getElementById("txtworkac").value) == '' )	msg = msg + " Phone No. is required \n ";
	if(TrimAll(document.getElementById("txtemail").value) == '')	msg = msg + " Email is required \n ";
	//if(TrimAll(document.getElementById("txtusername").value) == '')	msg = msg + " Username is required \n ";
	if(!chk_updatepass) {
		if(TrimAll(document.getElementById("txtpasswordx").value) == '')	msg = msg + " Password is required \n ";
		if(TrimAll(document.getElementById("txtconfirmpassword").value) == '')	msg = msg + " Password Confirmation is required \n ";
		if(TrimAll(document.getElementById("txtpasswordx").value) != TrimAll(document.getElementById("txtconfirmpassword").value)) msg = " Password and Confirm password must be the same";
	} else {
		if(chk_updatepass.checked) {
			if(TrimAll(document.getElementById("txtpasswordx").value) == '')	msg = msg + " Password is required \n ";
			if(TrimAll(document.getElementById("txtconfirmpassword").value) == '')	msg = msg + " Password Confirmation is required \n ";
			if(TrimAll(document.getElementById("txtpasswordx").value) != TrimAll(document.getElementById("txtconfirmpassword").value)) msg = " Password and Confirm password must be the same";
		}
	}
	if(TrimAll(document.getElementById("txtemail").value) != TrimAll(document.getElementById("txtconfirmemail").value)) msg =  msg + "Email confirmation must be the same\n";
	//if(TrimAll(document.getElementById("dbpasswordquestion").value) == '')	msg = msg + " Password recovery question is required \n ";
	//if(TrimAll(document.getElementById("txtpasswordanswer").value) == '')	msg = msg + " Password recovery answer is required \n ";
	if(document.getElementById("chksameaddress").checked) {
		document.getElementById("txtshipaddress1").value = document.getElementById("txtbilladdress1").value;	
		document.getElementById("txtshipaddress2").value = document.getElementById("txtbilladdress2").value;	
		document.getElementById("txtshipcity").value = document.getElementById("txtbillcity").value;	
		document.getElementById("txtshipstate").value = document.getElementById("txtbillstate").value;	
		document.getElementById("cbxshipcountry").value = document.getElementById("cbxbillcountry").value;	
		document.getElementById("txtshippostalcode").value = document.getElementById("txtbillpostalcode").value;	
	} else {
		if(TrimAll(document.getElementById("txtshipaddress1").value) == "") msg = msg + " Shipping Address Line 1 is required \n ";
		//if(TrimAll(document.getElementById("txtshipaddress2").value) == "") msg = msg + " Shipping Address Line 2 is required \n ";
		if(TrimAll(document.getElementById("txtshipcity").value ) == "")	msg = msg + " Shipping City is required \n ";
		if(TrimAll(document.getElementById("txtshipstate").value ) == "")	msg = msg + " Shipping State is required \n ";
		if(TrimAll(document.getElementById("cbxshipcountry").value ) == "")	msg = msg + " Shipping Country is required \n ";
		if(TrimAll(document.getElementById("txtshippostalcode").value ) == "")	msg = msg + " Shipping Postal Code is required \n ";
		
	}
	if(msg != "") {
		alert(msg);
		//document.getElementById('submitholder').innerHTML = msg;
		return;
		//obj.submit_flg.value = "submit";
		//obj.submit();
	} else {
		if(!validateEmail(document.getElementById("txtemail").value)) {
			alert('Invalid Email Address')
			return;
		} else {
			//check if mail is already registered
				
			if(!chk_updatepass) {
				var email = document.getElementById("txtemail");	
				try{
					
					xmlHttp.open("POST", "../includes/SQL/valregmail.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) {
							if (TrimAll(xmlHttp.responseText) == "") {
			
								document.getElementById("submit_flg").value = "save";
								obj.submit();
							} else {
								alert(xmlHttp.responseText);
								return;
							}
							//window.location  									= '/optin-success.php';
						}
					};
				} catch(e) {
					//alert(e);
					//location = "includes/SQL/getChatCust.php";
				}
			} else {
				document.getElementById("submit_flg").value = "update";
				obj.submit();
			}
			
		}
	}
}

function ForgotPass(txtemail) {
	var email = document.getElementById(txtemail);	
	if(validateEmail(email.value)) {
		try{
			
			xmlHttp.open("POST", "../includes/SQL/forgotpass.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) {
					alert(xmlHttp.responseText);
					document.getElementById(txtemail).value = "";
				}
			};
		} catch(e) {
			//location = "includes/SQL/getChatCust.php";
		}
		
	} else {
		alert('Invalid Email Address');	
		return;
	}
}


//--------------------Function for string Trim
function LTrim(str){
	if(str==null){
		return null;
	}
	for(var i=0;str.charAt(i)==" ";i++);
	return str.substring(i,str.length);
}
function RTrim(str){
	if(str==null){
		return null;
	}
	for(var i=str.length-1;str.charAt(i)==" ";i--);
	return str.substring(0,i+1);
}
function Trim(str){
	return LTrim(RTrim(str));
	}
function LTrimAll(str){
	if(str==null){
		return str;
	}
	for(var i=0;str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\t";i++);
	return str.substring(i,str.length);
}
function RTrimAll(str){
	if(str==null){
		return str;
	}
	for(var i=str.length-1;str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\t";i--);
	return str.substring(0,i+1);
}
function TrimAll(str){
	return LTrimAll(RTrimAll(str));
}	
