// JavaScript Document

function setPaymentInfo(isChecked)
{
	with (window.document.frm) {
		if (isChecked) {
			txtPaymentFirstName.value  = txtShippingFirstName.value;
			txtPaymentLastName.value   = txtShippingLastName.value;
			txtPaymentAddress1.value   = txtShippingAddress1.value;
			txtPaymentAddress2.value   = txtShippingAddress2.value;
			txtPaymentPhone.value      = txtShippingPhone.value;
			txtPaymentCity.value       = txtShippingCity.value;
			txtPaymentPostalCode.value = txtShippingPostalCode.value;
			/*txtPaymentState.value      = txtShippingState.value;
			txtPaymentCountry.value    = txtShippingCountry.value;*/
			
			txtPaymentFirstName.readOnly  = true;
			txtPaymentLastName.readOnly   = true;
			txtPaymentAddress1.readOnly   = true;
			txtPaymentAddress2.readOnly   = true;
			txtPaymentPhone.readOnly      = true;
			txtPaymentCity.readOnly       = true;
			/*txtPaymentState.readOnly      = true;
			txtPaymentCountry.readOnly    = true;*/
			txtPaymentPostalCode.readOnly = true;			
		} else {
			txtPaymentFirstName.readOnly  = false;
			txtPaymentLastName.readOnly   = false;
			txtPaymentAddress1.readOnly   = false;
			txtPaymentAddress2.readOnly   = false;
			txtPaymentPhone.readOnly      = false;
			txtPaymentCity.readOnly       = false;
			/*txtPaymentState.readOnly      = false;
			txtPaymentCountry.readOnly    = false;*/
			txtPaymentPostalCode.readOnly = false;			
		}
	}
}

function validateWholeForm() {
    var why = "";
	with (window.document.frm) {
	if (why += checkEmail(txtUserEmail.value)) {
		txtUserEmail.focus();
	} else if (why += checkEmail(txtUserConEmail.value)) {
		txtUserConEmail.focus();	
	} else if (why += checkemail(txtUserEmail.value, txtUserConEmail.value)) {
		txtUserConEmail.focus();
	} else if (why += checkPassword(txtUserPassword.value)) {
		txtUserPassword.focus();
	} else if (why += checkPassword(txtUserConPassword.value)) {
		txtUserConPassword.focus();
	} else if (why += checkpassword(txtUserPassword.value, txtUserConPassword.value)) {
		txtUserConPassword.focus();
	/*} else if (why += isEmpty(txtShippingFirstName)) {
		txtShippingFirstName.focus();
	} else if (isEmpty(txtShippingLastName)) {
		txtShippingLastName.focus();
	} else if (why += isEmpty(txtShippingAddress1)) {
		txtShippingAddress1.focus();
	} else if(why += checkPhone(txtShippingPhone.value)){
		txtShippingPhone.focus();	
	} else if(why += checkDropdown(txtShippingCountry.selectedIndex)){
		return false;
	} else if(why += checkDropdown(txtShippingState.selectedIndex)){
		return false;	
	} else if (why += isEmpty(txtShippingCity)) {
		txtShippingCity.focus();
	} else if (why += isEmpty(txtShippingPostalCode)) {
		txtShippingPostalCode.focus();*/
	} else if (why += isEmpty(txtPaymentFirstName)) {
		txtPaymentFirstName.focus();
	} else if (why += isEmpty(txtPaymentLastName)) {
		txtPaymentLastName.focus();
	} else if (why += isEmpty(txtPaymentAddress1)) {
		txtPaymentAddress1.focus();
	} else if (why += checkPhone(txtPaymentPhone.value)) {
		txtPaymentPhone.focus();
	} else if(why += checkDropdown(txtPaymentCountry.selectedIndex)){
		return false;
	} else if(why += checkDropdown(txtPaymentState.selectedIndex)){
		return false;
	} else if (why += isEmpty(txtPaymentPostalCode)) {
		txtPaymentPostalCode.focus();
	} if (why != "") {alert(why); return false; }
	  else{ return true; }
 }
}

function checkEmail(strng) {
	var error="";
	if (strng == "") { error = "You didn't enter an email address.\n"; }
		var emailFilter=/^.+@.+\..{2,3}$/;
		if (!(emailFilter.test(strng))) { error = "Please enter a valid email address.\n"; }
		else { 
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (strng.match(illegalChars)) { error = "The email address contains illegal characters.\n";  }
		}
	return error;    
}

// two passwords checking
function checkpassword (strng, strng1) {
	var error = "";
	if (strng != strng1) { error = "Passwords are not same\n"; }
	return error;    
}    

//check the email and confirm mail
function checkemail (strng, strng1) {
	var error = "";
	if (strng != strng1) { error = "Mail Id's are not same\n"; }
	return error;    
}  
  
// phone number - strip out delimiters and check for 10 digits
function checkPhone (strng) {
	var error = "";
	if (strng == "") { error = "You didn't enter a phone number.\n"; }
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
		if (isNaN(parseInt(stripped))) { error = "The phone number contains illegal characters.";}
		if (!(stripped.length == 10)) { error = "The phone number is the wrong length. Make sure you included an area code.\n"; } 
	return error;
}

//function for shipping contact number
function shipcheckPhone (strng) {
	var error = "";
	if (strng == "") {error = "You didn't enter a shipping contact number.\n"; }
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
		if (isNaN(parseInt(stripped))) { error = "The shipping contact number contains illegal characters."; }
		if (!(stripped.length == 10)) { error = "The shipping contact number is the wrong length. Make sure you included an area code.\n"; } 
	return error;
}

//function for zip code
function checkzip (strng) {
	var error = "";
	if (strng == "") { error = "You didn't enter a zip number.\n"; }	
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
		if (isNaN(parseInt(stripped))) { error = "The phone number contains illegal characters."; }
		if (!(stripped.length == 6)) { error = "The zip code is wrong length. Make sure you included an code.\n"; } 
	return error;
}

//shipping zip code
function shipcheckzip (strng) {
	var error = "";
	if (strng == "") { error = "You didn't enter a shipping zip code.\n"; }	
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
		if (isNaN(parseInt(stripped))) { error = "The phone number contains illegal characters."; }
		if (!(stripped.length == 6)) { error = "The shipping zip code is wrong length. Make sure you included an code.\n"; } 
	return error;
}

// password - between 6-8 chars, uppercase, lowercase, and numeral
function checkPassword (strng) {
	var error = "";
	if (strng == "") { error = "You didn't enter a password.\n"; }	
	else{
		var illegalChars = /[\W_]/; // allow only letters and numbers		
		if ((strng.length < 6) || (strng.length > 15)) { error = "The password is the wrong length.\n"; }
		else if (illegalChars.test(strng)) { error = "The password contains illegal characters.\n"; } 
		else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
		   error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
		}}  
	return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.
function checkUsername (strng) {
	var error = "";
	if (strng == "") { error = "You didn't enter a username.\n"; }
		var illegalChars = /\W/; // allow letters, numbers, and underscores
		if ((strng.length < 4) || (strng.length > 20)) { error = "The username is the wrong length.\n"; }
		else if (illegalChars.test(strng)) { error = "The username contains illegal characters.\n"; } 
	return error;
}       

//check city
function checkCity (strng) {
	var error = "";
	if (strng == "") { error = "You didn't enter a city.\n"; }
		var illegalChars = /\W/; // allow letters, numbers, and underscores
		if (illegalChars.test(strng)) { error = "The city contains illegal characters.\n"; } 
	return error;
}

//check state
function checkState (strng) {
	var error = "";
	if (strng == "") { error = "You didn't enter a state.\n"; }
		var illegalChars = /\W/; // allow letters, numbers, and underscores
		if (illegalChars.test(strng)) { error = "The state contains illegal characters.\n";  } 
	return error;
}       
   
// non-empty textbox
function isEmpty(strng) {
	var error = "";
	  if (strng.length == 0) { error = "The mandatory text area has not been filled in.\n" }
	return error;	  
}

// was textbox altered
function isDifferent(strng) {
	var error = ""; 
	if (strng != "Can\'t touch this!") { error = "You altered the inviolate text area.\n"; }
	return error;
}

// exactly one radio button is chosen
function checkRadio(checkvalue) {
	var error = "";
	if (!(checkvalue)) { error = "Please check a radio button.\n"; }
	return error;
}

// valid selector from dropdown list
function checkDropdown(choice) {
	//alert(choice)
	var error = "";
    if (choice == 0) { error = "You didn't choose an option from the drop-down list.\n"; }    
	return error;
} 


//function country drop down
function getXMLHTTP() { //fuction to return the xml http object
	var xmlhttp=false;	
	try{
		xmlhttp=new XMLHttpRequest();
	}
	catch(e)	{		
		try{			
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1){
				xmlhttp=false;
			}
		}
	}		 	
	return xmlhttp;
}
	
function stateChanged(){
	if (xmlhttp.readyState==4) {
		document.getElementById("paystatediv").innerHTML=xmlhttp.responseText;
	}
}

function shipstateChanged(){
	if (xmlhttp.readyState==4) {
		document.getElementById("shipstatediv").innerHTML=xmlhttp.responseText;
	}
}
	
function GetXmlHttpObject(){
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}


function showPaymentCountry(countryId){	
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="getcountry.php";		
	url=url+"?countryId="+countryId;
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function showShipcountry(shipcountryId){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="getshipcountry.php";		
	url=url+"?shipcountryId="+shipcountryId;
	xmlhttp.onreadystatechange=shipstateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}
