var CookiePrefix = "AutismDietOrder";

function updatepage() {
	var navtopage = "ad_order_new.php?"+
	"item_quantity="+document.forms.order.item_quantity.value+
	"&first_name="+document.forms.order.first_name.value+
	"&last_name="+document.forms.order.last_name.value+
	"&address1="+document.forms.order.address1.value+
	"&address2="+document.forms.order.address2.value+
	"&city="+document.forms.order.city.value+
	"&state="+document.forms.order.state.value+
	"&zip="+document.forms.order.zip.value+
	"&delivery_country="+document.forms.order.delivery_country.value+
	"&delivery_pobox="+document.forms.order.delivery_pobox.checked+
	"&delivery_preference="+document.forms.order.delivery_preference.value+
	"&email="+document.forms.order.email.value+
	"&night_phone_b="+document.forms.order.night_phone_b.value;
	window.location.href = navtopage;
	//newwindow=window.open(navtopage,'name','scrollbars=yes,toolbar=yes,height=600,width=800');
	//if (window.focus) {newwindow.focus()}
}

function showfaxform() {
	var navtopage = "ad_fax_order.php?"+
	"item_quantity="+document.forms.order.item_quantity.value+
	"&first_name="+document.forms.order.first_name.value+
	"&last_name="+document.forms.order.last_name.value+
	"&address1="+document.forms.order.address1.value+
	"&address2="+document.forms.order.address2.value+
	"&city="+document.forms.order.city.value+
	"&state="+document.forms.order.state.value+
	"&zip="+document.forms.order.zip.value+
	"&delivery_country="+document.forms.order.delivery_country.value+
	"&delivery_pobox="+document.forms.order.delivery_pobox.checked+
	"&delivery_preference="+document.forms.order.delivery_preference.value+
	"&email="+document.forms.order.email.value+
	"&shipping="+document.forms.order.shipping.value+
	"&post_service="+document.forms.order.post_service.value+
	"&amount="+document.forms.order.amount.value+
	"&night_phone_b="+document.forms.order.night_phone_b.value;
	if (validateorder()) {
		newwindow=window.open(navtopage,'name','scrollbars=yes,toolbar=yes,height=600,width=800');
		if (window.focus) {newwindow.focus()}
	}
}

function validateorder() {
	var quantity = document.forms.order.item_quantity.value;
	var first_name = document.forms.order.first_name.value;
	var last_name = document.forms.order.last_name.value;
	var address1 = document.forms.order.address1.value;
	var address2 = document.forms.order.address2.value;
	var city = document.forms.order.city.value;
	var state = document.forms.order.state.value;
	var zip = document.forms.order.zip.value;
	var delivery_country = document.forms.order.delivery_country.value;
	var delivery_pobox = document.forms.order.delivery_pobox.checked;
	var post_service = document.forms.order.post_service.value;
	var email = document.forms.order.email.value;
	var night_phone_b = document.forms.order.night_phone_b.value;
	var shipping = document.forms.order.shipping.value;

	var validation = "";
	if (delivery_country == "AU") {
		if (state == "Outside Australia") {
			validation = validation + "\n* Selected an Australian State from the list";
		}
		if ((zip < "0000" || zip > "9999") || (zip.length != 4)) {
			validation = validation + "\n* Supplied a four-digit Australian Postcode";
		}
	} else {
		if (zip == "") {
			validation = validation + "\n* Supplied your post/zip/delivery code";
		}		
	}

	if (email == "" || !echeck(email)) {
		validation = validation + "\n* Entered your correct email address";
	}

	if (first_name == "" || last_name == "") {
		validation = validation + "\n* Told us your full name (first name and surname)";
	}
	if (address1 == "" && address2 == "") {
		validation = validation + "\n* Specified your delivery address details";
	}
	if (city == "") {
		validation = validation + "\n* Specified your delivery city, suburb or town";
	}
	if (night_phone_b == "") {
		validation = validation + "\n* Provided us with a contact telephone number";
	}
	if (shipping == 0 && validation == "") {
		alert("We cannot find a way of delivering our publication to your address.\n\nPlease email us with your order details and we will assist you with\nyour purchase.\n");
		return false;
	} else {
		if (validation != "") {
			alert("Please check that you have:\n"+validation);
			return false;
		} else {
			return true;
		}
	}
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	   return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
	   return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	   return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
	   return false
	}
	if (str.indexOf(" ")!=-1){
	   return false
	}
	return true					
}

