errorColour = "#FFFFDD";
defaultColour = "#FFFFFF";

function setMinValue (initValue, minValue){
	if(initValue >= minValue){
		return initValue;
	}else{
		return minValue;
	}
}

function checkWholesaler(tgtForm){
	var allCorrect = true;
	
	// check title
	if(!checkText(trimWhiteSpace(tgtForm.ttl.value))){
		tgtForm.ttl.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.ttl.style.backgroundColor=defaultColour;
	}

	// check first name
	if(!checkText(trimWhiteSpace(tgtForm.fName.value))){
		tgtForm.fName.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.fName.style.backgroundColor=defaultColour;
	}

	// check last name
	if(!checkText(trimWhiteSpace(tgtForm.lName.value))){
		tgtForm.lName.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.lName.style.backgroundColor=defaultColour;
	}

	// check street postal
	if(!checkText(trimWhiteSpace(tgtForm.stPost.value))){
		tgtForm.stPost.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.stPost.style.backgroundColor=defaultColour;
	}

	// check suburb
	if(!checkText(trimWhiteSpace(tgtForm.subPost.value))){
		tgtForm.subPost.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.subPost.style.backgroundColor=defaultColour;
	}

	// check city
	if(!checkText(trimWhiteSpace(tgtForm.cityPost.value))){
		tgtForm.cityPost.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.cityPost.style.backgroundColor=defaultColour;
	}

	// check post code
	if(!checkText(trimWhiteSpace(tgtForm.pcodePost.value))){
		tgtForm.pcodePost.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.pcodePost.style.backgroundColor=defaultColour;
	}

	// check post code
	if(!checkText(trimWhiteSpace(tgtForm.zonePost.value))){
		tgtForm.zonePost.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.zonePost.style.backgroundColor=defaultColour;
	}

	// check email
	if(!checkText(trimWhiteSpace(tgtForm.eAddress.value))){
		tgtForm.eAddress.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.eAddress.style.backgroundColor=defaultColour;
	}
	
	if(!isEmail(tgtForm.eAddress.value)){
		tgtForm.eAddress.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.eAddress.style.backgroundColor=defaultColour;
	}

	// check password
	if(!checkText(trimWhiteSpace(tgtForm.pw.value))){
		tgtForm.pw.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.pw.style.backgroundColor=defaultColour;
	}

	if(!allCorrect){
		alert("several fields are incorect");
	}
	return allCorrect;
}

function checkOrder(tgtForm){
	var allCorrect = true;

	// check user name 
	if(!checkText(trimWhiteSpace(tgtForm.NAME.value))){
		tgtForm.NAME.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.NAME.style.backgroundColor=defaultColour;
	}
	
	// check phone number
	if(!checkText(trimWhiteSpace(tgtForm.PHONE.value))){
		tgtForm.PHONE.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.PHONE.style.backgroundColor=defaultColour;
	}
	
	// check email
	if(!checkText(trimWhiteSpace(tgtForm.ADDRESS.value))){
		tgtForm.ADDRESS.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.ADDRESS.style.backgroundColor=defaultColour;
	}
	
	if(!isEmail(tgtForm.EMAIL.value)){
		tgtForm.EMAIL.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.EMAIL.style.backgroundColor=defaultColour;
	}

	if(!allCorrect){
		alert("several fields are incorect");
	}
	if(allCorrect){
		createCookie("uName", tgtForm.uName.value, 2);
		createCookie("uNumber", tgtForm.uNumber.value, 2);
		createCookie("uEmail", tgtForm.uEmail.value, 2);
		createCookie("delAddress", tgtForm.delAddress.value, 2);
		createCookie("uMessage", tgtForm.uMessage.value, 2);
		createCookie("paymentMethod", tgtForm.paymentMethod.value, 2);
	}
	//return false;
	return allCorrect;
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function checkCartDetails (tgtForm){
	if(!checkText(trimWhiteSpace(tgtForm.orderQuantity.value))){
		alert("Please enter an order quantity");
		return false;
	}
	return true;
}

function openItem (tgtItem, tgtWarning, startSearch){
	if((tgtWarning != "" && confirm(tgtWarning)) || (tgtWarning == "")){
		window.location = "?tgtPage=catalogue&prodRef=" + tgtItem + "&startSearch=" + startSearch;
	}
}

function openCat (tgtItem, tgtWarning){
	if((tgtWarning != "" && confirm(tgtWarning)) || (tgtWarning == "")){
		window.location = "?tgtPage=catalogue&menRef=" + tgtItem;
	}
}

function checkSubmit (tgtForm){
	var allCorrect = true;

	if(!checkText(trimWhiteSpace(tgtForm.fName.value))){
		tgtForm.fName.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.fName.style.backgroundColor=defaultColour;
	}

	if(!checkText(trimWhiteSpace(tgtForm.fEmail.value))){
		tgtForm.fEmail.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.fEmail.style.backgroundColor=defaultColour;
	}
	
	if(!isEmail(tgtForm.fEmail.value)){
		tgtForm.fEmail.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.fEmail.style.backgroundColor=defaultColour;
	}

	if(!checkText(trimWhiteSpace(tgtForm.rName.value))){
		tgtForm.rName.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.rName.style.backgroundColor=defaultColour;
	}

	if(!checkText(trimWhiteSpace(tgtForm.rEmail.value))){
		tgtForm.rEmail.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.rEmail.style.backgroundColor=defaultColour;
	}

	if(!isEmail(tgtForm.fEmail.value)){
		tgtForm.rEmail.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.rEmail.style.backgroundColor=defaultColour;
	}

	if(!allCorrect){
		alert("several fields are incorect");
	}
	return allCorrect;
}

function trimWhiteSpace(trimString){
	var newString = "";
	for(i=0; i<trimString.length; i++){
		if(trimString.substr(i, 1)!=" "){
			newString = trimString.substring(i, trimString.length);
		}
	}
	return newString;
}

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	  return (!r1.test(str) && r2.test(str));
}

function checkText (tgtText){
	if(tgtText == ""){
		return false;
	}else{
		return true;
	}
}

function checkContact(tgtForm){
	var allCorrect = true;

	if(!checkText(trimWhiteSpace(tgtForm.uName.value))){
		tgtForm.uName.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.uName.style.backgroundColor=defaultColour;
	}
	
	if(!checkText(trimWhiteSpace(tgtForm.uEmail.value))){
		tgtForm.uEmail.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.uEmail.style.backgroundColor=defaultColour;
	}
	
	if(!isEmail(tgtForm.uEmail.value)){
		tgtForm.uEmail.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.uEmail.style.backgroundColor=defaultColour;
	}

	if(!checkText(trimWhiteSpace(tgtForm.uMessage.value))){
		tgtForm.uMessage.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.uMessage.style.backgroundColor=defaultColour;
	}
	
	if(!allCorrect){
		alert("several fields are incorect");
	}
	return allCorrect;
}


//-----------------------------------------------------------------------------------------

function checkCatgeory (tgtForm){
	var allCorrect = true;

	if(!checkText(trimWhiteSpace(tgtForm.categoryName.value))){
		tgtForm.categoryName.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.categoryName.style.backgroundColor=defaultColour;
	}
	
	if(!allCorrect){
		alert("several fields are incorect");
	}
	return allCorrect;
}




function checkEditForm (tgtForm){
	var allCorrect = true;

	if(!checkText(trimWhiteSpace(tgtForm.productName.value))){
		tgtForm.productName.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.productName.style.backgroundColor=defaultColour;
	}

	if(checkText(trimWhiteSpace(tgtForm.imagePic.value))){
		checkString = tgtForm.imagePic.value;
		if(checkString.substr(checkString.length-4, 4).toLowerCase() != ".jpg"){
			tgtForm.imagePic.style.backgroundColor=errorColour;
			allCorrect = false;
		} else {
			tgtForm.imagePic.style.backgroundColor=defaultColour;
		}
	}

	if(!checkText(trimWhiteSpace(tgtForm.productPrice.value))){
		tgtForm.productPrice.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.productPrice.style.backgroundColor=defaultColour;
	}

	if(!Number(tgtForm.productPrice.value)){
		tgtForm.productPrice.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.productPrice.style.backgroundColor=defaultColour;
	}

	if(!allCorrect){
		alert("several fields are incorect");
	}
	return allCorrect;
}

function checkForm(tgtForm){
	var allCorrect = true;

	if(!checkText(trimWhiteSpace(tgtForm.formName.value))){
		tgtForm.formName.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.formName.style.backgroundColor=defaultColour;
	}

	if(!checkText(trimWhiteSpace(tgtForm.formFile.value))){
		tgtForm.formFile.style.backgroundColor=errorColour;
		allCorrect = false;
	} else {
		tgtForm.formFile.style.backgroundColor=defaultColour;
	}

	if(!allCorrect){
		alert("several fields are incorect");
	}
	return allCorrect;
}




