// JavaScript Document
// Checkout Javascript
// NX 11/10/2005
//

function NX_initFormChk(){
	document.forms["checkout"].elements["costate"].disabled=true;
	document.forms["checkout"].elements["dostate"].disabled=true;
	focusElement(document.forms["checkout"].elements["firstname"]);
}

function NX_validateCheckout(theform){
	
	theform.elements["firstname"].value=trimBlanks(theform.elements["firstname"].value);
	if(theform.elements["firstname"].value==""){
		focusElement(theform.elements["firstname"]);
		alert("Error: You must enter a firstname.");
		return false;
	}
	theform.elements["lastname"].value=trimBlanks(theform.elements["lastname"].value);
	if(theform.elements["lastname"].value==""){
		focusElement(theform.elements["lastname"]);
		alert("Error: You must enter a lastname.");
		return false;
	}
	theform.elements["phonenumber"].value=trimBlanks(theform.elements["phonenumber"].value);
	if(theform.elements["phonenumber"].value==""){
		focusElement(theform.elements["phonenumber"]);
		alert("Error: You must enter a daytime contact phonenumber.");
		return false;
	}
	theform.elements["email"].value=trimBlanks(theform.elements["email"].value);
	if(theform.elements["email"].value==""){
		focusElement(theform.elements["email"]);
		alert("Error: You must enter an email address.");
		return false;
	} else if(!echeck(theform.elements["email"].value)){
		focusElement(theform.elements["email"]);
		return false;
	}
	theform.elements["orgname"].value=trimBlanks(theform.elements["orgname"].value);
	theform.elements["position"].value=trimBlanks(theform.elements["position"].value);
	
	if(theform.elements["ccountry"].value.toUpperCase()=="AUSTRALIA"){
		
		if(!NX_validateAddress(theform.elements["caddress1"], 
								theform.elements["caddress2"],
								theform.elements["ccity"],
								theform.elements["cstate"],
								theform.elements["cpostcode"],
								theform.elements["ccountry"])){
			return false;
		} 
	} else {
		if(!NX_validateAddress(theform.elements["caddress1"], 
								theform.elements["caddress2"],
								theform.elements["ccity"],
								theform.elements["costate"],
								theform.elements["cpostcode"],
								theform.elements["ccountry"])){
			return false;
		} 
	}
	if(theform.elements["dcountry"].value.toUpperCase()=="AUSTRALIA"){
		if(!NX_validateAddress(theform.elements["daddress1"], 
								theform.elements["daddress2"],
								theform.elements["dcity"],
								theform.elements["dstate"],
								theform.elements["dpostcode"],
								theform.elements["dcountry"])){
			return false;
		}
	} else {
		if(!NX_validateAddress(theform.elements["daddress1"], 
								theform.elements["daddress2"],
								theform.elements["dcity"],
								theform.elements["dostate"],
								theform.elements["dpostcode"],
								theform.elements["dcountry"])){
			return false;
		}
	}
	
	// temporarily disable checking
	// payment method
	// if(!theform.elements["paymentmethod"].checked){
	//  	alert("You must select a payment method.");
	//	return false;
	// }
	//alert(theform.elements["paymentmethod"].value);
	

	return true;
		
}

function NX_validateAddress(addr1Elem, addr2Elem, cityElem, stateElem, postcodeElem, countryElem){
	
	addr1Elem.value=trimBlanks(addr1Elem.value);
	if(addr1Elem.value==""){
		alert("Error: You must enter a street address.");
		focusElement(addr1Elem);
		return false;
	}
	addr2Elem.value=trimBlanks(addr2Elem.value);
	cityElem.value=trimBlanks(cityElem.value);
	if(cityElem.value==""){
		alert("Error: You must enter a city or suburb name.");
		focusElement(cityElem);
		return false;
	}
	countryElem.value=trimBlanks(countryElem.value);
	if(countryElem==""){
		alert("Error: You must enter a country name.");
		countryElem.focus();
		return false;
	}
	stateElem.value=trimBlanks(stateElem.value);
	if(stateElem.value=="" || stateElem.value=="###"){
		alert("Error: You must enter a state name.");
		stateElem.focus();
		return false;
	}
	
	postcodeElem.value=trimBlanks(postcodeElem.value);
	if(postcodeElem.value==""){
		alert("Error: You must enter a postcode.");
		focusElement(postcodeElem);
		return false;
	}
	if(countryElem.value.toUpperCase()=="AUSTRALIA"){
		//if(!checkState(countryElem.value.toUpperCase, stateElem.value.toUpperCase)){
		//	focusElement(stateElem);
		//	alert("Error: You have not entered an Australian state or territory.");
		//	return false;
		//}
		var pcodeStr=postcodeElem.value;
		var stateStr=stateElem.value;
		if(!validAusPostcodes(pcodeStr, stateStr)){
			//alert("Error: The postcode does not match the state. Please enter the correct postcode.");
			focusElement(postcodeElem);
			return false;
		}
		
	}
	
	return true;
}

function NX_sameAddress(thewidget, formname){
	
	if(thewidget.checked){
		document.forms["checkout"].elements["daddress1"].value=document.forms["checkout"].elements["caddress1"].value;
		document.forms["checkout"].elements["daddress2"].value=document.forms["checkout"].elements["caddress2"].value;
		document.forms["checkout"].elements["dcity"].value=document.forms["checkout"].elements["ccity"].value;
		document.forms["checkout"].elements["dostate"].value=document.forms["checkout"].elements["costate"].value;
		document.forms["checkout"].elements["dpostcode"].value=document.forms["checkout"].elements["cpostcode"].value;
		document.forms["checkout"].elements["dstate"].selectedIndex=document.forms["checkout"].elements["cstate"].selectedIndex;
	} else {
		document.forms["checkout"].elements["daddress1"].value="";
		document.forms["checkout"].elements["daddress2"].value="";
		document.forms["checkout"].elements["dcity"].value="";
		document.forms["checkout"].elements["dostate"].value="";
		document.forms["checkout"].elements["dpostcode"].value="";
		document.forms["checkout"].elements["dstate"].selectedIndex=0;
	}
	document.forms["checkout"].elements["dostate"].disabled=document.forms["checkout"].elements["costate"].disabled;
	document.forms["checkout"].elements["dstate"].disabled=document.forms["checkout"].elements["cstate"].disabled;
	document.forms["checkout"].elements["dcountry"].selectedIndex=document.forms["checkout"].elements["ccountry"].selectedIndex;
}

function NX_countryChanged(thewidget, cprefix){
	var country=thewidget.value;
	var countryidx=thewidget.selectedIndex;
	
	if (country.toUpperCase() == "AUSTRALIA"){
		if (cprefix.toUpperCase()=="C"){
			document.forms["checkout"].elements["costate"].value="";
			document.forms["checkout"].elements["costate"].disabled=true;
			document.forms["checkout"].elements["cstate"].disabled=false;
			document.forms["checkout"].elements["cstate"].selectedIndex=0;
		} else if (cprefix.toUpperCase() =="D"){
			document.forms["checkout"].elements["dostate"].value="";
			document.forms["checkout"].elements["dostate"].disabled=true;
			document.forms["checkout"].elements["dstate"].disabled=false;
			document.forms["checkout"].elements["dstate"].selectedIndex=0;
		}

	} else {
		if (cprefix.toUpperCase() == "C"){
			document.forms["checkout"].elements["costate"].disabled=false;
			document.forms["checkout"].elements["cstate"].selectedIndex=0;
			document.forms["checkout"].elements["cstate"].disabled=true;
		} else if (cprefix.toUpperCase() == "D"){
			document.forms["checkout"].elements["dostate"].disabled=false;
			document.forms["checkout"].elements["dstate"].selectedIndex=0;
			document.forms["checkout"].elements["dstate"].disabled=true;
		}
	}
	
}

function NX_selectedAction(thewidget){
}


