﻿//opens a popup window in the center of the screen
function PopupWindow(url, popW, popH)
{
	var w, h;
	
	if (document.all) {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	else if (document.layers)
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}

	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

	window.open(url,'popup','width=' + popW + ',height='+popH+',top='+topPos+',left='+leftPos);
}

//used when adding items to the cart
function AddItem(item, stain)
{
	document.addform.stain.value = stain
	document.addform.itemid.value = item;
	document.addform.additem.value = true;
	document.addform.submit();
}

//check for a valid postcode
function ValidatePostcode(code)
{
	if(code.length!=4)
	{
		return -1;
	}
	
	var allowedChars = "0123456789";
	
	for(i = 0; i < code.length; i++)
	{
		var char = code.charAt(i);
		
		if(allowedChars.indexOf(char)==-1)
		{
			return 0;
		}
	}
	
	return 1;
}

//check for a valid phone number
function ValidatePhoneNumber(phone)
{
	if(phone.length==0)
	{
		return -1;
	}
	
	var allowedChars = "() 0123456789";
	
	for(i = 0; i < phone.length; i++)
	{
		var char = phone.charAt(i);
		
		if(allowedChars.indexOf(char)==-1)
		{
			return 0;
		}
	}
	
	return 1;
}

//check for a valid email address
function ValidateEmail(email)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(!filter.test(email))
	{
		return false;
	}
	
	return true;
}

//validate the checkout info form
function ValidateInfo()
{
	var message = "";
	
	var theform = document.checkoutinfo;
	
	if(theform.firstname.value.length==0)
	{
		message += "You must enter your first name.\n";
	}
	
	if(theform.surname.value.length==0)
	{
		message += "You must enter your surname.\n";
	}
	
	if(theform.address.value.length==0)
	{
		message += "You must enter your address.\n";
	}
	
	if(theform.suburb.value.length==0)
	{
		message += "You must enter your suburb.\n";
	}
	
	if(ValidatePostcode(theform.postcode.value)==-1)
	{
		message += "Your post code must be 4 numbers in length.\n";
	}
	else if(ValidatePostcode(theform.postcode.value)==0)
	{
		message += "Your post code must contain numbers only.\n";
	}
	
	if(theform.state.value.length==0)
	{
		message += "You must select your state.\n";
	}
	
	if(ValidatePhoneNumber(theform.phone.value)==-1)
	{
		message += "You must enter your phone number.\n";
	}
	else if(ValidatePhoneNumber(theform.phone.value)==0)
	{
		message += "Your phone number must only contain numbers, spaces, ( or ).\n";
	}
	
	if(!ValidateEmail(theform.email.value))
	{
		message += "Your email address is invalid.\n";
	}
	
	if(theform.paymentmethod.value.length==0)
	{
		message += "You must select a payment method.\n";
	}
	
	if(message=="")
	{
		return true;
	}
	else
	{
		alert("ERROR:\n\n" + message);
		
		return false;
	}
}

//get the stain select box
function returnObjById(id)
{
    if(document.getElementById)
    {
	    var returnVar = document.getElementById(id);
    }
    else if(document.all)
    {
	    var returnVar = document.all[id];
    }
    else if(document.layers)
    {
	    var returnVar = document.layers[id];
    }
    
    return returnVar;
}