// 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 "company_details":
		if (!NotEmpty ("Name") || !NotEmpty ("Full_Name") || !NotEmpty ("Address") || !NotEmpty ("Phone1")  || !ValidEmail("Email1")|| !NotEmpty("Web1"))
		return false
		if (NotEmpty("Email2"))
		{
			if (!ValidEmail("Email2"))
				return false
		}
		var elm = document.getElementById("Contact_Email")
		elm.style.backgroundColor = "#fff"
		break

		case "site_details":
		if (!NotEmpty ("Name") || !NotEmpty ("Slogan") || !NotEmpty ("NumInList") || !NumberOnly ("NumInList"))
		return false
		break

		case "Add_Town":
		if (!StringOnly ("Name") || !NotEmpty ("Name"))
		return false
		break

		case "Add_Page":
		if (!NotEmpty ("Page_Title") || !NotEmpty ("Page_Text"))
		return false
		break

		case "Add_Area":
		if (!StringOnly ("Name") || !NotEmpty ("Name"))
		return false
		break

		case "Send_Mail":
		if (!ValidEmail ("To"))
		return false
		break

		case "Mail_List":
		if (!NotEmpty("Mail_Name") || !ValidEmail("Mail_Email"))
		return false
		break

		case "Contact_Form":
		if (!NotEmpty("CName") || !ValidEmail("Email") || !NotEmpty("Phone") || !NotEmpty("Message"))
		return false
		break

		case "Tell_Friend":
		if (!NotEmpty("YName") || !ValidEmail("YEmail") || !NotEmpty("TName") || !ValidEmail("TEmail") || !NotEmpty("TFMessage"))
		return false
		break

		case "Property":
		if (!NotEmpty("Area") || !NotEmpty("Town") || !NotSelected("Area") || !NotSelected("Town") || !NotEmpty("Price") || !NumberOnly("Price"))
		return false
		if (NotEmpty("Contact_Email"))
		{
			if (!ValidEmail("Contact_Email"))
				return false
		}
		var elm = document.getElementById("Contact_Email")
		elm.style.backgroundColor = "#fff"
		break

		case "Currency":
		if (!NumberOnly ("USD") || !NumberOnly ("GBP") || !NumberOnly ("EUR"))
		return false
		if (!NotEmpty ("USD") || !NotEmpty ("GBP") || !NotEmpty ("EUR"))
		return false
		break

	} //end switch

	return true

} // end function
