// Form Validation
function NumberOnly(id) {

	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	var match = /^(\d|\s|[.])+$/.test(val)
	if (val=="")
		match=1
	if (!match) {
		elm.style.backgroundColor = "#ff0000"
		elm.focus()
		return false
	}
	elm.style.backgroundColor = "#fff"
	return true
}

function ValidEmail(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
//	var match = /^\S+\@\w+(\.\w+$)|(\.\w+\.\w+$)/.test(val)
	var match=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i.test(val)
	if (!match)  {
		elm.style.backgroundColor = "#ff0000"
		elm.focus()
		return false	}
		elm.style.backgroundColor = "#fff"
		return true
}

function ValidEmails(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	re = /([0-9a-zA-Z\@\.\-_]*,*)/
	emaillist = val.split(re)
	alert(emaillist.length)
	for(i=0;i<emaillist.length;i++)
	{
		alert (i + '' + emaillist[i]);
		var match=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i.test(emaillist[i])
		if (!match)  {
			elm.style.backgroundColor = "#ff0000"
			alert (emaillist[i]);
			elm.focus()
			return false	}
	}
	elm.style.backgroundColor = "#fff"
	return true
}

function NotEmpty(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	if (val=="") {
		elm.style.backgroundColor = "#ff0000"
		elm.focus()
		return false	}
		elm.style.backgroundColor = "#fff"
		return true
}

function NotSelected(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	if (val=="0") {
		elm.style.backgroundColor = "#ff0000"
		elm.focus()
		return false	}
		elm.style.backgroundColor = "#fff"
		return true
}

function StringOnly(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	var match = /^\w+$/.test(val)
	if (val=="")
	match=1
	if (!match) {
		elm.style.backgroundColor = "#ff0000"
		elm.focus()
		return false		}
		elm.style.backgroundColor = "#fff"
		return true
}

function ValidUsername(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	var match = /^\w{6,10}/.test(val)
	if (!match) {
		elm.style.backgroundColor = "#ff0000"
		elm.focus()
		return false		}
		elm.style.backgroundColor = "#fff"
		return true
}

function ValidPassword(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	var match = /^\w{6,10}/.test(val)
	if (!match) {
		elm.style.backgroundColor = "#ff0000"
		elm.focus()
		return false		}
		elm.style.backgroundColor = "#fff"
		return true
}

function validiate(form_id) {

	var form_id
	switch (form_id)
	{
		case "rentals":
		if (!NotEmpty("YName"))
		{
			alert("Please enter your name!");
			return false
		}
		if (!NotEmpty("Email"))
		{
			alert("Please enter a valid email address!");
			return false
		}
		if (!ValidEmail("Email"))
		{
			alert("Please enter a valid email address!");
			return false
		}
		
		if (!NotEmpty("Phone"))
		{
			alert("Please enter a phone number!");
			return false
		}		

		if (!NotEmpty("keystring"))
		{
			alert("Please enter the security code!");
			return false
		}
		break

		case "contact":
		if (!NotEmpty("YName"))
		{
			alert("Please enter your name!");
			return false
		}
		if (!NotEmpty("Email"))
		{
			alert("Please enter a valid email address!");
			return false
		}
		if (!ValidEmail("Email"))
		{
			alert("Please enter a valid email address!");
			return false
		}
		
		if (!NotEmpty("Phone"))
		{
			alert("Please enter a phone number!");
			return false
		}		

		if (!NotEmpty("keystring"))
		{
			alert("Please enter the security code!");
			return false
		}
		break
		
	} //end switch

	return true

} // end function
