// JavaScript Document

function doOver(_link,_name) {
	var img = _link.getElementsByTagName("img");
	img[0].src = SITE_PATH + "/public/images/mainnav/" + _name + "_o.gif";
}



function doOut(_link,_name) {
	var img = _link.getElementsByTagName("img");
	img[0].src = SITE_PATH + "/public/images/mainnav/" + _name + ".gif";
}


/* range navigation */
function doOverRange(_link,_name) {
	var img = _link.getElementsByTagName("img");
	img[0].src = SITE_PATH + "/public/images/rangenav/" + _name + "_o.gif";
}



function doOutRange(_link,_name) {
	var img = _link.getElementsByTagName("img");
	img[0].src = SITE_PATH + "/public/images/rangenav/" + _name + ".gif";
}

function doCheck(id) {
	if ( document.getElementById(id) ) {
		var o = document.getElementById(id);
		o.checked = ( o.checked ) ? 0 : 1;
	} else { alert('Element does not exist'); }
}

//hand adding/removing using text box
function addRemText(item_id, shape_id)
{
	var range_id = ( arguments[2] ) ? arguments[2] : '';
	timer = setTimeout('validate("'+item_id+'", "'+shape_id+'", "'+range_id+'")', 1000);
}
function validate(item_id, shape_id, range_id)
{
	var qty_box= document.getElementById("qty"+item_id);
	if(qty_box.value>''){
		if(isNumeric(qty_box.value))
		{
			addRemProduct(item_id, shape_id, 'text', range_id);
		}
		else
		{
			alert("Please enter numbers only.");
			qty_box.value=0;
		}
	}
	else
	{
		qty_box.value=0;
	}
	clearTimeout(timer);
	return true;
}
// handle adding/removing to cart system
function addRemProduct(item_id, shape_id, trigger, range_id) {
	//alert(1);
	if ( document.getElementById("range_id") ) {
		//alert(2);
		var colour_id = '';
		//alert('colour_select'+shape_id);
		var colour_select_el = '';
		
		if(document.getElementById('colour_select'+shape_id))
		{
			colour_select_el = document.getElementById('colour_select'+shape_id);
			colour_id = colour_select_el[colour_select_el.options.selectedIndex].value;
		}
		var range_id = document.getElementById("range_id").value;
		var chkbox = document.getElementById("item"+item_id);
		var is_checked = chkbox.checked;

		var qty_box= document.getElementById("qty"+item_id)
		
		var quantity = qty_box.value;
		if(quantity=='')
		{
			//alert(4);
			quantity=0;
		}
		
		//alert( "range: " + range_id + " shape id: " + shape_id + " is checked: " + is_checked + " quantity: " + quantity + " color: " + colour_id );
		
		/* if event is triggered by a check box */
		//cart._delete(range_id,shape_id);
		switch(trigger)
		{
			case 'checkbox':
				/* if quantity is zero set it to 1 */
				if(!is_checked)
				{
					//alert("delete: checkbox");
					cart._delete(range_id,shape_id);
					qty_box.value = 0;
				}
				else if ( is_checked && quantity == 0 ) {
					//alert("add: checkbox");
					
					cart.add(range_id,shape_id,1, colour_id);
					qty_box.value = 1;
				}
				else 
				{
					qty_box.value=0;
					
				}
					
				break;
			
			case 'text':
				if(quantity==0)
				{
					chkbox.checked = false;
					cart._delete(range_id,shape_id);
				}
				else
				{
					chkbox.checked = true;
					cart.add(range_id,shape_id,quantity, colour_id);
				}
				break;
				
			case 'anchor':
				chkbox.checked = true;
				
				//alert("add: anchor");
				quantity++;
				cart.add(range_id,shape_id,quantity, colour_id);
				qty_box.value = quantity;
				
				break;
		}
		
	
		
	} else {
		var qty_box = document.getElementById("qty"+item_id);
		var quantity = qty_box.value;
		
		var colour_id = '';
		var colour_select = '';
		if(document.getElementById('colour_select'+shape_id))
		{
			colour_select = document.getElementById('colour_select'+shape_id);
			colour_id = colour_select[colour_select.options.selectedIndex].value;
		}
		
		//alert( "range: " + range_id + " shape id: " + shape_id + " is checked: " + is_checked + " quantity: " + quantity + " color: " + colour_id );
		
		if ( quantity != '' ) {
			//alert("add");
			cart.add(range_id,shape_id,quantity, colour_id);
		} else {
			//alert("delete");
			cart._delete(range_id,shape_id,1);
		}
		
	}
}
function isNumeric(expression) {
	var nums = "0123456789";
	if (expression.length==0)return(false);
	for (var n=0; n < expression.length; n++){
		if(nums.indexOf(expression.charAt(n))==-1)
		return false;
	}
	return true;
}

function uncheckAll() {
	for ( i = 1 ; i < 300; i++ ) {
		if ( document.getElementById('item'+i) ) {
			var o = document.getElementById('item'+i);
			o.checked = false;
		}
	}
}

/* download free recipe form */
function validRecipe(recipeForm) {
	

	if (recipeForm.name.value == 'Name...') {
		alert("Please enter your your Name");
		recipeForm.name.focus();
		return false;
		}

	 	if (recipeForm.name.value == '') {
		alert("Please enter your your Name");
		recipeForm.name.focus();
		return false;
		}

	if (recipeForm.email.value == 'Email...') {
		alert("Please enter your email address");
		recipeForm.email.focus();
		return false;
		}

	if (recipeForm.email.value == '') {
		alert("Please enter your email address");
		recipeForm.email.focus();
		return false;
		}

	if (!validEmail(recipeForm.email.value)) {
		alert("Please enter a valid email");
		recipeForm.email.focus();
		return false;
		}

	return true;

	

}


/* -- Contact Us Form --- */
function validContact(contactForm) {
	
	if ( contactForm.fullname.value == '' ) {
		alert("Please enter your  Full Name");
		contactForm.fullname.focus();
		return false;
		}
	
		if ( contactForm.email.value == '' ) {
		alert("Please enter your email");
		contactForm.email.focus();
		return false;
		}
	
		if (!validEmail(contactForm.email.value) ) {
		alert("Please enter a valid email");
		contactForm.email.focus();
		return false;
		}
	
		if ( contactForm.tel.value == '' ) {
		alert("Please enter your Contact NUmber");
		contactForm.tel.focus();
		return false;
		}
	
		if (!validNum(contactForm.tel.value) ) {
		alert("Please enter a valid Contact Number");
		contactForm.tel.focus();
		return false;
		}
	
		if ( contactForm.country.value == '' ) {
		alert("Please select a country");
		contactForm.country.focus();
		return false;
		}
	
		return true;
	
}


/* cook book order page validation */

function validCookbook(cookbookForm) {
	
		if ( cookbookForm.firstname.value == '' ) {
		alert("Please enter your First Name");
		cookbookForm.firstname.focus();
		return false;
		}
		
		if ( cookbookForm.lastname.value == '' ) {
		alert("Please enter your Last Name");
		cookbookForm.lastname.focus();
		return false;
		}
		
		if ( cookbookForm.tel.value == '' ) {
		alert("Please enter your Telephone number");
		cookbookForm.tel.focus();
		return false;
		}
		
		if (!validNum(cookbookForm.tel.value) ) {
		alert("Please enter a valid Contact Number");
		cookbookForm.tel.focus();
		return false;
		}
		
		if ( cookbookForm.mobile.value == '' ) {
		alert("Please enter your Mobile Number");
		cookbookForm.mobile.focus();
		return false;
		}
		
		if (!validNum(cookbookForm.mobile.value) ) {
		alert("Please enter a valid Mobile Number");
		cookbookForm.mobile.focus();
		return false;
		}
	
		if ( cookbookForm.email.value == '' ) {
		alert("Please enter your email");
		cookbookForm.email.focus();
		return false;
		}
	
		if (!validEmail(cookbookForm.email.value) ) {
		alert("Please enter a valid email");
		cookbookForm.email.focus();
		return false;
		}
	
	
		if ( cookbookForm.address1.value == '' ) {
		alert("Please enter your Address");
		cookbookForm.address1.focus();
		return false;
		}
		
		if ( cookbookForm.suburb.value == '' ) {
		alert("Please select a suburb");
		cookbookForm.suburb.focus();
		return false;
		}
		
		if ( cookbookForm.province.value == '' ) {
		alert("Please select a province / state");
		cookbookForm.province.focus();
		return false;
		}
		
		if ( cookbookForm.country.value == '' ) {
		alert("Please select a country");
		cookbookForm.country.focus();
		return false;
		}
		
		if ( cookbookForm.quantity.value == '' ) {
		alert("Please select a quantity");
		cookbookForm.quantity.focus();
		return false;
		}
	
		if ( cookbookForm.country.value == '' ) {
		alert("Please select a country");
		cookbookForm.country.focus();
		return false;
		}
	
	return true;
	
}



/* cook book order page validation */

function validStep2(step2Form) {
	
		if ( step2Form.firstname.value == '' ) {
		alert("Please enter your First Name");
		step2Form.firstname.focus();
		return false;
		}
		
		if ( step2Form.lastname.value == '' ) {
		alert("Please enter your Last Name");
		step2Form.lastname.focus();
		return false;
		}
		
		if ( step2Form.tel.value == '' ) {
		alert("Please enter your Telephone number");
		step2Form.tel.focus();
		return false;
		}
		
		if (!validNum(step2Form.tel.value) ) {
		alert("Please enter a valid Contact Number");
		step2Form.tel.focus();
		return false;
		}
		
		if ( step2Form.mobile.value == '' ) {
		alert("Please enter your Mobile Number");
		step2Form.mobile.focus();
		return false;
		}
		
		if (!validNum(step2Form.mobile.value) ) {
		alert("Please enter a valid Mobile Number");
		step2Form.mobile.focus();
		return false;
		}
	
		if ( step2Form.email.value == '' ) {
		alert("Please enter your email");
		step2Form.email.focus();
		return false;
		}
	
		if (!validEmail(step2Form.email.value) ) {
		alert("Please enter a valid email");
		step2Form.email.focus();
		return false;
		}
	
	
		if ( step2Form.address1.value == '' ) {
		alert("Please select a Address");
		step2Form.address1.focus();
		return false;
		}
		
		if ( step2Form.suburb.value == '' ) {
		alert("Please select a suburb");
		step2Form.suburb.focus();
		return false;
		}
		
		if ( step2Form.province.value == '' ) {
		alert("Please select a province / state");
		step2Form.province.focus();
		return false;
		}
		
		if ( step2Form.townstate.value == '' ) {
		alert("Please enter your Town / State");
		step2Form.townstate.focus();
		return false;
		}
		
		if ( step2Form.country.value == '' ) {
		alert("Please select a country");
		step2Form.country.focus();
		return false;
		}
		
		if ( step2Form.postal.value == '' ) {
		alert("Please enter your Postal Code");
		step2Form.postal.focus();
		return false;
		}
		
		if ( !step2Form.terms.checked ) {
			alert("Please agree to our Terms and Conditions");
			step2Form.terms.focus();
			return false;
		}
	
	return true;
	
}
/* ************** VALIDATION FUNCTIONS ***************** */

function validNum(telno) {
	var pattern = "0123456789+-)( ";
	var i = 0;
	
	do {
		var pos = 0;
		for (var j=0;j<pattern.length;j++)
			if (telno.charAt(i)==pattern.charAt(j))
				pos = 1;
		i++;
	}
	while (pos==1 && i<telno.length)
	
	if (pos==0) return false;
	return true;
}


function validEmail(addr){
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=addr.match(emailPat)
	
	if (matchArray==null) {
		//alert("Please enter a valid email address (check @ and .'s)")
		return false
	}
	
	var user=matchArray[1]
	var domain=matchArray[2]
	
	if (user.match(userPat)==null) {
		//alert("The username doesn't seem to be valid.")
		return false
	}
	
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				//alert("Destination IP address is invalid!")
				return false
			}
		}
		return true
	}
	
	var domainArray=domain.match(domainPat)
	
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
		return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
		//alert("The address must end in a three-letter domain, or two letter country.")
		return false
	}
	if (len<2) {
		//alert("This address is missing a hostname!")
		return false
	}
	return true;
}

