// 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") || !ValidEmail("Email") || !NotEmpty("Phone") || !NotEmpty("Message") || !NotEmpty("Captura"))
		return false
		break

		case "registration":
		message = '';
		problem = false;
		if (!NotEmpty ("UName"))
		{		
			message += 'Please Enter your Name\n';
			problem = true;
		}
		
		if (!NotEmpty ("Email") || !ValidEmail("Email"))
		{		
			message += 'Please Enter a valid Email Address\n';
			problem = true;
		}
		
		if (!ValidPassword ("Password"))
		{		
			message += 'Please Enter a Password containing between 6 and 10 charactors\n';
			problem = true;
		}
		
		if (!ValidPassword("RPassword"))
		{		
			message += 'Please Enter a Repeat Password containing between 6 and 10 charactors\n';
			problem = true;
		}
		
		if (document.getElementById('Password').value != document.getElementById('RPassword').value)
		{		
			message += 'You Password entries do not match, please re-enter\n';
			problem = true;
		}

		if (problem)
		{
			alert(message);
			return false
		}
		else
			return true
		break;
	} //end switch

	return true

} // end function
