// JavaScript Document
function projectowner_validate()
{
	if(document.projectowner.CatId.value == ""){
		alert("Select a category");
		document.projectowner.CatId.focus();
		return false;
	}
	if(document.projectowner.ServiceId.value == ""){
		alert("Select a service");
		document.projectowner.ServiceId.focus();
		return false;
	}
		if(document.projectowner.fname.value == ""){
		alert("First Name must be required");
		document.projectowner.fname.focus();
		return false;
	}
	if(document.projectowner.lname.value == ""){
		alert("Last Name must be required");
		document.projectowner.lname.focus();
		return false;
	}
	if(document.projectowner.address.value == ""){
		alert("Address must be required");
		document.projectowner.address.focus();
		return false;
	}
	if(document.projectowner.city.value == ""){
		alert("City must be required");
		document.projectowner.city.focus();
		return false;
	}
	if(document.projectowner.state.value == ""){
		alert("State must be required");
		document.projectowner.state.focus();
		return false;
	}
	if(document.projectowner.zip.value == ""){
		alert("Zipcode must be required");
		document.projectowner.zip.focus();
		return false;
	}
	
	zipvalue = document.projectowner.zip.value;
	
	if(isNaN(zipvalue) || zipvalue == 0 || zipvalue.indexOf('.') >= 0 || zipvalue.indexOf('-') >= 0 ){
		alert("Zipcode must be a number");
		document.projectowner.zip.focus();
		return false;
	}
	
	//zipvaluelength = document.projectowner.zip.value;
	
	/*if(zipvaluelength < 5){
		alert("Zipcode must be a number of 5 digits");
		document.projectowner.zip.focus();
		return false;	
	}*/

	var phone = document.projectowner.dayphone.value;
		 if (phone == "" )
		 { 
			 alert ("Phone Number must be required");
			 document.projectowner.dayphone.focus();
			 return false;
		 }	
		 
		/*returnMsg =  checkPhoneNumberEx(phone);
		
			if ( returnMsg == "wrong number"){
				alert("Please Enter a Valid Phone Number")
				document.projectowner.dayphone.value="";
				document.projectowner.dayphone.focus();
				return false;
			}*/
			
			if(document.projectowner.evephone.value != ""){
				
				returnMsg =  checkPhoneNumberEx(document.projectowner.evephone.value);
		
				if ( returnMsg == "wrong number"){
					alert("Please Enter a Valid Phone Number in the Format of 111-111-1111")
					document.projectowner.evephone.focus();
					return false;
				}
				
			}

/*
	if(document.projectowner.dayphone.value == ""){
		alert("Select a Day Phone");
		document.projectowner.dayphone.focus();
		return false;
	}
	if(isNaN(document.projectowner.dayphone.value))
	{
		alert("Select a Day Phone");
		document.projectowner.dayphone.value ="";
		document.projectowner.dayphone.focus();
		return false;
	}*/
	if(document.projectowner.email.value == ""){
		alert("Email Address must be required");
		document.projectowner.email.focus();
		return false;
	}
	if (!validateEmail(document.projectowner.email.value,1,1)) 
	 	{
		 document.projectowner.email.focus();
		 return false;
		 }
	if(document.projectowner.chkbox.checked == false)
	{
		alert("Terms and Conditions must be required");
		document.projectowner.chkbox.focus();
		return false;
	}


}

function checkPhoneNumberEx(value) {

	if ('string' != typeof(value)) return '';

	if ( '' == value ) return '';
	
	var separators = new RegExp('[^0-9]','ig');
	
	value = value.replace(separators,'');
	
	var l = value.length;
	
	phone = 'wrong number';
	
		
	if (l == 10) {
		phone =  value.substr(l-10,3) + "-" + value.substr(l-7,3) + "-" + value.substr(l-4,4);
	}
	
	if (l>10 && l<14) {
		phone =  value.substr(l-10,3) + "-" + value.substr(l-7,3) + "-" + value.substr(l-4,4);
	}
	
	if(phone == 'wrong number' ){
		alert("Wrong Number");	
		phone = "";
	}

	return phone;


}


function validateEmail(addr,man,db) {
	if (addr == '' && man) {
	   if (db) alert('Email address is mandatory');
	   return false;
	}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) alert('Email address contains invalid characters');
		  return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  if (db) alert("Email address contains non ascii characters.");
		  return false;
	   }
	}
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db) alert('Email address must contain an @');
	   return false;
	}
	if (atPos == 0) {
	   if (db) alert('Email address must not start with @');
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) alert('Email address must contain only one @');
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) alert('Email address must contain a period in the domain name');
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) alert('period must not immediately follow @ in email address');
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) alert('period must not immediately precede @ in email address');
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) alert('two periods must not be adjacent in email address');
	   return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   if (db) alert('invalid primary domain in email address');
	   return false;
	}
return true;
}


 //Function phone validation Script
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()-. ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

