// 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 "Contact_Form":
		if (!NotEmpty("CName"))
		{
			alert("Please enter your name!");
			return false
		}
		if (NotEmpty("Email"))
		{
			if (!ValidEmail("Email"))
			{
				alert("Please enter a valid email address!");
				return false
			}
			else
			{
				var elm = document.getElementById("Email")
				elm.style.backgroundColor = "#fff"
			}
		}
		else
		{
			var elm = document.getElementById("Email")
			elm.style.backgroundColor = "#fff"
			if (!NotEmpty("Phone"))
			{
				alert("Please enter a valid email address or phone number!");
				return false
			}		
		}
		if (!NotEmpty("Message"))
		{
			alert("Please enter a message!");
			return false
		}
		break

		case "comment_form":
		if (!NotEmpty("CName"))
		{
			alert("Please enter your name!");
			return false
		}
		if (NotEmpty("Email"))
		{
			if (!ValidEmail("Email"))
			{
				alert("Please enter a valid Email address!");
				return false
			}
		}
		else
		{
			var elm = document.getElementById("Email")
			elm.style.backgroundColor = "#fff"
		}
		
		if (!NotEmpty("Comment"))
		{
			alert("Please enter a Comment!");
			return false
		}
		break

		case "Res_Form":
		if (!NotEmpty("name"))
		{
			alert("Please enter your name!");
			return false
		}
		if (NotEmpty("email"))
		{
			if (!ValidEmail("email"))
			{
				alert("Please enter a valid email address!");
				return false
			}
		}
		else
		{
			var elm = document.getElementById("email")
			elm.style.backgroundColor = "#fff"
			if (!NotEmpty("phone"))
			{
				alert("Please enter a valid email address or phone number!");
				return false
			}		
		}
		
		if (!NotEmpty("comment"))
		{
			alert("Please enter a Comment!");
			return false
		}
		break

	} //end switch

	return true

} // end function
