function loadXMLDoc(url,cfunc) {
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=cfunc;
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
}

function showModalBox(flag) {
	if(flag) {
		scroll(0,0);
		document.getElementById("html").style.overflow = "hidden";
		document.getElementById("disabler").style.display = "block";
		document.getElementById("modal-box").style.display = "block";
		document.getElementById("modal-box").innerHTML = '<p class="pcenter"><img src="images/ajax-loader.gif" alt="Loading" /><br/>Loading</p>';
		centerModalBox();
	} else {
		scroll(0,0);
		document.getElementById("html").style.overflow = "auto";
		document.getElementById("disabler").style.display = "none";
		document.getElementById("modal-box").style.display = "none";
		document.getElementById("modal-box").innerHTML = '';
	}
}

function centerModalBox() {
	var width;
	var height;
	var div = document.getElementById('modal-box');
	var left;
	var top;
	//Most browsers
	if(window.clientHeight && window.clientWidth) {
		width = window.clientWidth;
		height = window.clientHeight;
	}
	//IE
	if(document.documentElement.clientHeight && document.documentElement.clientWidth) {
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
	}
	
	left = (width - div.offsetWidth)/2;
	top = (height - div.offsetHeight)/2;
	
	div.style.left = left+"px";
	div.style.top = top+"px";
}

function getRetailer(state) {
	document.getElementById("retailers-list").innerHTML = '<p class="pcenter"><img src="images/ajax-loader.gif" alt="Loading" /><br/>Loading</p>';
	loadXMLDoc("php/retailers.php?state="+state,function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("retailers-list").innerHTML = xmlhttp.responseText;
		}
	});
}

function showSignupForm() {
	showModalBox(true);
	loadXMLDoc("php/signup-form.php",function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("modal-box").innerHTML = xmlhttp.responseText;
			centerModalBox();
		}
	});
}

function submitSignupForm() {
	var parameters;
	parameters = 'name='+document.getElementById("signup-name").value;
	parameters += '&age='+document.getElementById("signup-age").value;
	parameters += '&state='+document.getElementById("signup-state").value;
	parameters += '&email='+document.getElementById("signup-email").value;
	
	showModalBox(true);
	
	loadXMLDoc("php/signup-process.php?"+parameters,function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("modal-box").innerHTML = xmlhttp.responseText;
			centerModalBox();
		}
	});
}

function submitTestimonialForm() {
	var parameters;
	parameters = 'name='+document.getElementById("testimonials-name").value;
	parameters += '&email='+document.getElementById("testimonials-email").value;
	parameters += '&occupation='+document.getElementById("testimonials-occupation").value;
	parameters += '&age='+document.getElementById("testimonials-age").value;
	parameters += '&testimony='+document.getElementById("testimonials-testimony").value;
	
	showModalBox(true);
	
	loadXMLDoc("php/testimonials-process.php?"+parameters,function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("modal-box").innerHTML = xmlhttp.responseText;
			centerModalBox();
			document.getElementById("testimonials-name").value = "";
			document.getElementById("testimonials-email").value = "";
			document.getElementById("testimonials-occupation").value = "";
			document.getElementById("testimonials-age").value = "";
			document.getElementById("testimonials-testimony").value = "";
		}
	});
}

function submitRetailerForm() {
	var parameters;
	parameters = 'name='+document.getElementById("retailers-name").value;
	parameters += '&company='+document.getElementById("retailers-company").value;
	parameters += '&email='+document.getElementById("retailers-email").value;
	parameters += '&message='+document.getElementById("retailers-message").value;
	
	showModalBox(true);
	
	loadXMLDoc("php/retailers-process.php?"+parameters,function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("modal-box").innerHTML = xmlhttp.responseText;
			centerModalBox();
			document.getElementById("retailers-name").value = "";
			document.getElementById("retailers-company").value = "";
			document.getElementById("retailers-email").value = "";
			document.getElementById("retailers-message").value = "";
		}
	});
}

function submitContactForm() {
	var parameters;
	parameters = 'name='+document.getElementById("contact-name").value;
	parameters += '&address='+document.getElementById("contact-address").value;
	parameters += '&email='+document.getElementById("contact-email").value;
	parameters += '&message='+document.getElementById("contact-message").value;
	
	showModalBox(true);
	
	loadXMLDoc("php/contact-process.php?"+parameters,function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("modal-box").innerHTML = xmlhttp.responseText;
			centerModalBox();
			document.getElementById("contact-name").value = "";
			document.getElementById("contact-address").value = "";
			document.getElementById("contact-email").value = "";
			document.getElementById("contact-message").value = "";
		}
	});
}

function getElementValue(name){
	var i;
	var retval;
	var myarray = document.getElementsByName(name);
	for(i=0; i<myarray.length; i++){
		if(myarray[i].checked){
			retval = myarray[i].value;
			return retval;
		}
	}
}

function reviewPurchase() {
	var parameters;
	parameters = 'quantity='+getElementValue('quantity');
	parameters += '&gwrap='+getElementValue('gwrap');
	parameters += '&postage='+getElementValue('postage');
	parameters += '&happybirthday1='+getElementValue('happybirthday1');
	parameters += '&happybirthday2='+getElementValue('happybirthday2');
	
	var address1='', address2='', suburb='', state='', pcode='';
	if(document.getElementById("del-address1")) address1 = document.getElementById("del-address1").value;
	if(document.getElementById("del-address2")) address2 = document.getElementById("del-address2").value;
	if(document.getElementById("del-suburb")) suburb = document.getElementById("del-suburb").value;
	if(document.getElementById("del-state")) state = document.getElementById("del-state").value;
	if(document.getElementById("del-pcode")) pcode = document.getElementById("del-pcode").value;
	
	showModalBox(true);
	
	loadXMLDoc("php/review-purchase.php?"+parameters,function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("modal-box").innerHTML = xmlhttp.responseText;
			centerModalBox();
			document.getElementById("del-address1").value = address1;
			document.getElementById("del-address2").value = address2;
			document.getElementById("del-suburb").value = suburb;
			document.getElementById("del-state").value = state;
			document.getElementById("del-pcode").value = pcode;
		}
	});
}

function getDeliveryAddress() {
	var address1='', address2='', suburb='', state='', pcode='';
	if(document.getElementById("del-address1")) address1 = document.getElementById("del-address1").value;
	if(document.getElementById("del-address2")) address2 = document.getElementById("del-address2").value;
	if(document.getElementById("del-suburb")) suburb = document.getElementById("del-suburb").value;
	if(document.getElementById("del-state")) state = document.getElementById("del-state").value;
	if(document.getElementById("del-pcode")) pcode = document.getElementById("del-pcode").value;
	showModalBox(true);
	loadXMLDoc("php/delivery-form.php",function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("modal-box").innerHTML = xmlhttp.responseText;
			centerModalBox();
			document.getElementById("del-address1").value = address1;
			document.getElementById("del-address2").value = address2;
			document.getElementById("del-suburb").value = suburb;
			document.getElementById("del-state").value = state;
			document.getElementById("del-pcode").value = pcode;
		}
	});
}

function proceedToPaymentOptions() {	
	var parameters;

	parameters = 'address1='+document.getElementById("del-address1").value;
	parameters += '&address2='+document.getElementById("del-address2").value;
	parameters += '&suburb='+document.getElementById("del-suburb").value;
	parameters += '&state='+document.getElementById("del-state").value;
	parameters += '&pcode='+document.getElementById("del-pcode").value;
	
	parameters += '&quantity='+getElementValue('quantity');
	parameters += '&gwrap='+getElementValue('gwrap');
	parameters += '&postage='+getElementValue('postage');
	parameters += '&happybirthday1='+getElementValue('happybirthday1');
	parameters += '&happybirthday2='+getElementValue('happybirthday2');
	parameters += '&message='+document.getElementById('gift-message').value;
	
	showModalBox(true);
	loadXMLDoc("php/payment-options.php?"+parameters,function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("modal-box").innerHTML = xmlhttp.responseText;
			centerModalBox();
		}
	});
}
