var minLen = 5;
var maxLen = 8;

function checkAlertReg(f) {
	// email
	if(trimAll(f.alert_email.value) == null || trimAll(f.alert_email.value) == "") {
		alert("Please enter a valid email address");
		f.alert_email.focus();
		return false;
	}
	else if(echeck(trimAll(f.alert_email.value)) == false) {
		f.alert_email.value = "";
		f.alert_email.focus();
		return false;
	}
	else if(trimAll(f.alert_email.value) !== trimAll(f.confirm_alert_email.value)) {
		alert("Your Confirm email field doesn't match\nthe value in the Email address field.\nPlease check and re-enter.");
		f.confirm_alert_email.value = "";
		f.alert_email.focus();
		return false;
	}
	// password
	else if(trimAll(f.password.value) == null || trimAll(f.password.value) == "" || trimAll(f.password.value).length < minLen || trimAll(f.password.value).length > maxLen) {
		alert("Please enter your preferred password.\nIt should be between 5 and 8 characters in length");
		f.password.focus();
		return false;
	}
	else if(trimAll(f.password.value) !== trimAll(f.confirm_password.value)) {
		alert("Your Confirm password field doesn't match\nthe value in the Password field.\nPlease check and re-enter.");
		f.confirm_password.value = "";
		f.password.focus();
		return false;
	}
	else if(trimAll(f.alert_name.value) == null || trimAll(f.alert_name.value) == "") {
		alert("Please enter a name/nickname to be known by");
		f.alert_name.value = "";
		f.alert_name.focus();
		return false;
	}
	else {
		return true;
	}
}

function checkAlertLogin(f) {
	// email
	if(trimAll(f.alert_email.value) == null || trimAll(f.alert_email.value) == "") {
		alert("Please enter your email address");
		f.alert_email.focus();
		return false;
	}
	else if(echeck(trimAll(f.alert_email.value)) == false) {
		f.alert_email.value = "";
		f.alert_email.focus();
		return false;
	}
	// password
	else if(trimAll(f.password.value) == null || trimAll(f.password.value) == "") {
		alert("Please enter your Price Alerts password.");
		f.password.focus();
		return false;
	}
	else {
		return true;
	}
}

function checkAlertEngine(f) {
	
	if(trimAll(f.trigger_price.value) == null || trimAll(f.trigger_price.value) == "") {
		alert("Please enter a trigger price in the form '123'\nEnter a value in whole pounds without any currency symbols.");
		f.trigger_price.focus();
		return false;
	}
	else if(checkForNonInteger(f.trigger_price.value)) {
		alert("Please enter a trigger price in the form '123'\nEnter a value in whole pounds without any currency symbols.");
		f.trigger_price.focus();
		return false;
	}
	else if(trimAll(f.trigger_price.value).length > 5) {
		alert("Please enter a trigger price in the form '123'\nThe value should be no greater than 9999.");
		f.trigger_price.focus();
		return false;
	}
	else {
		return true;
	}
	
}

function checkForNonInteger(s) {
	var i;
	for (i = 0; i < s.length; i++) {   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return true;
	}
	// All characters are numbers.
	return false;
}

function trimAll(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var msg = "Your email address appears to be invalid\nPlease check and re-enter";
	
	if (str.indexOf(at)==-1) {
		alert(msg);
		return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
		alert(msg);
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
		alert(msg);
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1) {
		alert(msg);
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
		alert(msg);
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1) {
		alert(msg);
		return false;
	}

	if (str.indexOf(" ")!=-1) {
		alert(msg);
		return false;
	}

	return true;
}