function vAddHttp(str)
{
	if (str.length != 0)
		if (str.substring(0, 7) != "http://")
			str = "http://" + str;
	return str;
}

function vMakeNumeric(src)
{
	var str = "";
	var ii;
	for (ii=0; ii<src.length; ii++)	{
		var c = src.charAt(ii);
		if (c >= '0' && c <= '9')
			str += c;
		if (c == '.')
			break;
	}
	return str;
}

function IsNumeric(src)
{
	var i, valid, ptcount;
	valid=true;
	ptcount=0;
	for (i=0; i < src.length;i++)
	{
		var c = src.charAt(i);
		if (c == '.')
			ptcount++;
		if ((ptcount > 1) || ((c<'0' || c>'9') && (c != '.')))
		{
			valid=false;
			break;
		}
	}
	if (src.length == 0)
		valid = false;
	if (src.length == 1 && ptcount == 1)
		valid = false;
	return valid;
}

function validatePhotoFile()
{
	document.AFORM.Photofile.value = vAddHttp(document.AFORM.Photofile.value);
	return true;
}

function validateHTMLFile()
{
	document.AFORM.HTMLFile.value = vAddHttp(document.AFORM.HTMLFile.value);
	return true;
}

function validatePrice(src)
{
	valid = true
	if (!IsNumeric(src.value))
	{
		alert("The price is invalid.\n"
		+ "Allowed characters are the digits 0-9 and a single decimal point.\n"
		+ "Do not include a £ sign or any commas.\n"
		+ "The field must also contain at least one digit.");
		valid = false;
	}
	else
	{
		if (src.value < 1000)
		{
			alert("The price must be greater than £1000");
			valid = false;
		}
	}
	return valid;
}


function validateNumber(src)
{
	valid = true
	if (!IsNumeric(src.value))
	{
		alert("The number entered is invalid.\n"
		+ "Allowed characters are the digits 0-9 and a single decimal point.\n"
		+ "Do not include a £ sign or any commas.\n"
		+ "The field must also contain at least one numeric digit.");
		valid = false;
	}
	return valid;
}


function validateBedrooms(src)
{
	valid = true
	if (!IsNumeric(src.value))
	{
		alert("The number of bedrooms is invalid.\n"
		+ "Allowed characters are the digits 0-9.\n"
		+ "Do not include any commas.\n"
		+ "The field must also contain at least one digit.");
		valid = false;
	}
	else
	{
		if (src.value < 0)
		{
			alert("The number of bedrooms must be 1 or more");
			valid = false;
		}
	}
	return valid;
}

function validateDescription(src)
{
	var valid;
	valid = true;
	if (src.value.length > 750)
	{
		alert("Description is limited to 750 characters in length!");
		valid = false;
	}
	return valid;
}

function checkMortCriteria()
{
	var valid;
	valid = true;
	var dIntRate;
	if (!IsNumeric(document.forms[0].amount.value))
	{
		alert("You must enter a numeric value for the amount.");
		valid = false;
	}
	if (!IsNumeric(document.forms[0].term.value))
	{
		alert("You must enter a numeric value for the term.");
		valid = false;
	} 
	dIntRate = document.forms[0].IntRate.value;
	if (IsNumeric(dIntRate))
	{
		dIntRate = dIntRate + 0.0;
		if (dIntRate<=0)
		{
			alert("You must enter a interest rate greater than zero.");
			valid = false;
		}
	}
	else
	{
		alert("You must enter a numeric value for the interest rate.");
		valid = false;
	} 
	return valid;
}

function checkApplicationForm()
{
	var valid;
	valid = true;
	if (document.forms[0].email.value.length == 0)
	{
		alert("You must enter a contact email address.");
		valid = false;
	} 
	if (document.forms[0].name1.value.length == 0 && document.forms[0].name2.value.length == 0)
	{
		alert("You must enter a contact name.");
		valid = false;
	} 
	return valid;
}

function NumberOfChar(str,char)
{
	var ii, charCount;
	charCount = 0;
	for (ii=0; ii < str.length;ii++)
	{
		var cc = str.charAt(ii);
		if (cc == char)
		{
			charCount = charCount + 1;
		}
	}
	return charCount;
}

function isValidEmail(str)
{
	var valid;
	valid = true;
	if (str.length < 6)
	{
		valid = false;
	}
	if ((NumberOfChar(str,'@')==0)||(NumberOfChar(str,'@')>1))
	{
		valid = false;
	}
	if (NumberOfChar(str,'.')==0)
	{
		valid = false;
	}
	if (NumberOfChar(str,' ')!=0)
	{
		valid = false;
	}
	return valid;
}

function checkLoginForm()
{
	var valid;
	valid = true;
	if (document.forms["login"].email.value == "")
	{
		alert("You must enter a valid email address.");
		valid = false;
	} 
	return valid;
}

function isValidPassword(str)
{
	var valid;
	valid = true;
	if (str.length <= 5)
	{
		valid = false;	
	}
	if (NumberOfChar(str," ") > 0)
	{
		valid = false;
	}
	return valid;
}

function checkBuyerDetails()
{
	var valid;
	valid = true;
	if (!isValidEmail(document.forms["details"].email.value))
	{
		alert("You must enter a valid email address.");
		valid = false;
	} 
	if ((valid)&&!isValidPassword(document.forms["details"].password.value))
	{
		alert("You must enter a password with at least 6 characters and no spaces.");
		valid = false;
	} 
	if ((valid)&&!IsNumeric(document.forms["details"].lprice.value))
	{
		alert("The 'from' price is invalid.\n"
		+ "Allowed characters are the digits 0-9 and a single decimal point.\n"
		+ "Do not include a £ sign or any commas.\n"
		+ "The field must also contain at least one digit.");
		valid = false;
	}
	if ((valid)&&!IsNumeric(document.forms["details"].uprice.value))
	{
		alert("The 'to' price is invalid.\n"
		+ "Allowed characters are the digits 0-9 and a single decimal point.\n"
		+ "Do not include a £ sign or any commas.\n"
		+ "The field must also contain at least one digit.");
		valid = false;
	}
	if (valid)
	{
		var dLower;
		var dHigher;
		dLower = document.forms["details"].lprice.value;
		dLower = dLower - 0;
		dHigher = document.forms["details"].uprice.value;
		dHigher = dHigher - 0;
		if (dLower>dHigher)
		{
			alert("The 'from' price must be less than the 'to' price.");
			valid = false;
		} 
	}	
	return valid;
}