// JavaScript Document
var xmlHttp;
function GetXmlHttpObject(handler)
{ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

function updateCountry(countryName) {

	if (countryName == "United States" || countryName == "United Kingdom") {
		document.getElementById("div_zip").style.display = "block";
		document.getElementById("div_state").style.display = "none";
		document.getElementById("div_county").style.display = "none";
		document.getElementById("div_city").style.display = "none";
	} else {
		document.getElementById("div_zip").style.display = "block";
		document.getElementById("div_county").style.display = "block";
		document.getElementById("div_city").style.display = "block";
	}
}

// get all zip details
function updateZip(zipCode) {
	countryName = document.formRegistration.country[document.formRegistration.country.selectedIndex].value;

	if (zipCode.length < 5) {
		alert ("Error: Please enter a valid zip code.");
		return;
	}
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Error: Your browser does not support HTTP Request.");
		// if no ajax validation, then display all fields
		document.getElementById("div_zip").style.display = "block";
		document.getElementById("div_county").style.display = "block";
		document.getElementById("div_city").style.display = "block";
		return;
	}
	
	if (countryName == "United States") {
		
		var url="ajax/getUsZipDetails.php?zipCode="+zipCode;
		xmlHttp.onreadystatechange=displayUsResults;

	} else if (countryName == "United Kingdom") {

		var url="ajax/getUkZipDetails.php?zipCode="+zipCode;
		xmlHttp.onreadystatechange=displayUkResults;
	} else {

		document.getElementById("div_state").style.display = "none";
		document.getElementById("div_county").style.display = "block";
		document.getElementById("div_city").style.display = "block";
		return;
	}
	
	url=url+"&sid="+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function displayUsResults() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		
		if (false) { // strpos | is false
		 document.getElementById("resultsZip").innerHTML = "<span style='margin-left:150px;'>Wrong US zip code.</span>";
		}
		
		// split the results
		var results = xmlHttp.responseText.split("|");
		alert(results);
		document.getElementById("div_state").style.display = "block";
		document.getElementById("div_county").style.display = "block";
		document.getElementById("div_city").style.display = "block";
		// display pre filled address/state/phone details
		document.formRegistration.state.value = results[0];
		document.formRegistration.county.value = results[1];
		document.formRegistration.city.value = results[2];
		document.formRegistration.phone.value = results[3];
//		document.getElementById("results").innerHTML = xmlHttp.responseText;
	} 
} 

function displayUkResults() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		
		var results = xmlHttp.responseText;
		
		if (results == "ErrorWrongZipCode")  
		{ //strpos | is false
			
			document.getElementById("resultsZip").innerHTML = "<span style='margin-left:150px;'>Wrong UK zip code. Please try again.</span>";
			
		} 
		else 
		{		
			document.getElementById("resultsZip").innerHTML = "";
			document.getElementById("div_state").style.display = "none";
			document.getElementById("div_county").style.display = "block";
			document.getElementById("div_city").style.display = "block";
			// display pre filled address/state/phone details
			document.formRegistration.city.value = results;
			document.formRegistration.state.value = "";
			document.formRegistration.county.value = "";
			document.formRegistration.phone.value = "";
		}
	} 
} 


function validateForm() {
	var message = "";
	if (document.formRegistration.city.value == "") {
		message = "Please fill in a valid city name";
	}
		
	if (message != "") { alert(message); return false; }
	else return true;
}


/* USED IN members/account-upgrade to select packs */
function activatePack1 () {
//	document.getElementById("pack1box").checked = true;
	document.getElementById("pack2box").checked = false;
	document.getElementById("pack3box").checked = false;
	document.getElementById("bodyPan_full").className = "LIpack1Active"; 
	document.FormPaypal.item_number.value = "PACK1";
	document.FormPaypal.item_name.value = "Popular-Biz.com / Default PACK1";
	document.FormPaypal.amount.value = "0.00";

	return false;
}
function activatePack2 () {
//	document.getElementById("pack1box").checked = false;
	document.getElementById("pack2box").checked = true;
	document.getElementById("pack3box").checked = false;
	document.getElementById("bodyPan_full").className = "LIpack2Active";
	
	document.FormPaypal.item_number.value = "PACK2";
	document.FormPaypal.item_name.value = "Popular-Biz.com / Upgrade to PACK2";
	document.FormPaypal.amount.value = "19.00";
	
	return false;
}
function activatePack3() {
//	document.getElementById("pack1box").checked = false;
	document.getElementById("pack2box").checked = false;
	document.getElementById("pack3box").checked = true;
	document.getElementById("bodyPan_full").className = "LIpack3Active";
	
	document.FormPaypal.item_number.value = "PACK3";
	document.FormPaypal.item_name.value = "Popular-Biz.com / Upgrade to PACK3";
	document.FormPaypal.amount.value = "99.00";
	
	return false;
}

function submitFormUpgrade(myForm)
{
	if (myForm.item_number.value == "PACK1")
	{
		alert("No upgrade required for PACK1");
		return false;
	}
}