function TrimString(str) {
	str = this != window? this : str;
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function isEmpty(TmpString) {
	if (!TmpString) {
		return true;
	}
	TmpString = TrimString(TmpString);
	if (TmpString.length <= 0) {
		return true;
	}
	return false;
}

function ShowError(Errors) {
	Errors = Errors.toLowerCase();
	alert("The following error(s) occurred:\n" + Errors.substring(Errors,Errors.length-1) + "\n\nSorry can not Process the form");
	return false;
}

function checkSearch() {
	varSearchString = document.getElementById("searchForm").searchString;
	if ((isEmpty(varSearchString)) || (varSearchString == "Search Products")) {
		return false;	
	}
}

function formControls() {
	if (document.getElementById("searchForm")) {
		if (document.getElementById("searchForm").searchString) {
			searchString = document.getElementById("searchForm").searchString;
			searchString.onfocus = new Function("focusValues(searchString, 'Search Products')");
			searchString.onblur = new Function("blurValues(searchString, 'Search Products')");
		}
	}
	if (document.getElementById("HeaderForm")) {
		headerUserName = document.getElementById("HeaderForm").UserName;
		headerPassword = document.getElementById("HeaderForm").Password;
		headerUserName.onfocus = new Function("focusValues(headerUserName, 'Email Address')");
		headerUserName.onblur = new Function("blurValues(headerUserName, 'Email Address')");
		headerPassword.onfocus = new Function("focusValues(headerPassword, 'Password')");
		headerPassword.onblur = new Function("blurValues(headerPassword, 'Password')");
	}
	if (document.getElementById("UserDeliveryForm")) {
		deliveryUserName = document.getElementById("UserDeliveryForm").UserName;
		deliveryPassword = document.getElementById("UserDeliveryForm").Password;
		deliveryUserName.onfocus = new Function("focusValues(deliveryUserName, 'Email Address')");
		deliveryUserName.onblur = new Function("blurValues(deliveryUserName, 'Email Address')");
		deliveryPassword.onfocus = new Function("focusValues(deliveryPassword, 'Password')");
		deliveryPassword.onblur = new Function("blurValues(deliveryPassword, 'Password')");
	}
	if (document.getElementById("registerForm")) {
		email = document.getElementById("registerForm").EmailAddress;
		password = document.getElementById("registerForm").Password;
		confirmPassword = document.getElementById("registerForm").ConfirmPassword;
		email.onkeyup = new Function("checkEmailAddress()");
		password.onkeyup = new Function("checkPassword()");
		confirmPassword.onkeyup = new Function("checkConfirmPassword()");
	}
}

function focusValues(formElement, val) {
	if (formElement.value == val) {
		formElement.value = "";
	}
}

function blurValues(formElement, val) {
	if (formElement.value == "") {
		formElement.value = val;
	}
}

function checkEmailAddress() {
	var outcome = false;
	var string = document.getElementById("registerForm").EmailAddress.value;
	stringLength = string.length;
	if (stringLength >= 1) {
		whiteSpace = stringContains(string, " ");
		atSign = stringContains(string, "@");
		bullet = stringContains(string, ".");
		if ((stringLength >= 3) && (!whiteSpace) && (atSign) && (bullet)) {
			outcome = true;	
			requestURL = "/ajax/checkUserAvailable.php";
			requestVar = "emailAddress=" + string;
			myRequest = new ajaxObject(requestURL);
			myRequest.callback = function(responseText) { showFormResponse("Email",responseText);}
			myRequest.update(requestVar,"POST");
		}
		showFormResponse("Email", outcome);
	} else {
		showFormResponse("Email", "");
	}
}

function checkPassword() {
	var outcome = false;
	var string = document.getElementById("registerForm").Password.value;
	stringLength = string.length;
	if (stringLength >= 1) {
		whiteSpace = stringContains(string, " ");
		if ((stringLength >= 6) && (!whiteSpace)) {
			outcome = true;	
		}
		showFormResponse("Password", outcome);
	} else {
		showFormResponse("Password", "");
	}
}

function checkConfirmPassword() {
	var outcome = false;
	var firstString = document.getElementById("registerForm").Password.value;
	var secondString = document.getElementById("registerForm").ConfirmPassword.value;
	stringLength = secondString.length;
	if (stringLength >= 1) {
		if (secondString == firstString) {
			outcome = true;	
		}
		showFormResponse("ConfirmPassword", outcome);
	} else {
		showFormResponse("ConfirmPassword", "");
	}
}


function showFormResponse(errorElement,response) {
	responseBox = errorElement + "Result";
	responseText = "";
	validBox = "valid" + errorElement;
	document.getElementById(validBox).value = makeBool(response);
	if ((response === false) || (response === true)) {
		responseText = "<img src=\"/images/icons/" + response + ".gif\" width=\"15\" height=\"15\" alt=\"" + response + "\" title=\"" + response + "\" />"
	}
	document.getElementById(responseBox).innerHTML = responseText;
}

function submitForm(FormName) {
	document.getElementById(FormName).submit();
}

function removeProduct(CartItem) {
	QuanBox = "Quantity" + CartItem;
	cartRow = "cartRow" + CartItem;
	document.getElementById(cartRow).className = "Hidden";
	document.getElementById(QuanBox).value = "0";
	document.getElementById("ProcessType").value = "update";
	submitForm("Basket");
}

function updateCart() {
	document.getElementById("ProcessType").value = "update";
	submitForm("Basket");
}