
function isEmailAddr(email) {
	var result = false;
	var theStr = new String(email);
	var index = theStr.indexOf("@");
	if (index > 0) {
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1)) {
		result = true;
		}
	}
	return result;
}
function isAlpha(obj) {
	var result = false;
	var inThere = obj.match(/\D/);
	if (inThere) {
		result = true;
	}
	return result;
}
function isAlphaNum(obj) {
	var result = false;
	var inThere = obj.match(/\w/);
	if (inThere) {
		result = true;
	}
	return result;
}
function isNotAlphaNum(obj) {
	var result = false;
	var inThere = obj.match(/[^\w\s]/);
	if (inThere) {
		result = true;
	}
	return result;
}

function isNum(obj) {
	var result = false;
	var inThere = obj.match(/\d/);
	if (inThere) {
		result = true;
	}
	return result;
}

function hasWhiteSpace(obj) {
	var result = false;
	var inThere = obj.match(/\s/);
	if (inThere) {
		result = true;
	}
	return result;
}

function isChecked(obj) {
	for (var i = 0; i < obj.length; i++) {
		if (obj[i].checked) {
			return "yes";
		}
	}
	return "no";
}

// Functions that validates the forms

function fvalid(form) {
	
	
	// State
	if (form.Name.value != "") 
		{
		if (isNum(form.Name.value)) {
			alert("Numbers are not allowed in the \"Name\" field.");
			form.Name.focus();
			return false;
		} 
		else if (isNotAlphaNum(form.Name.value)) 
		{
			alert("Special characters are not allowed in the \"Name\" field.");
			form.Name.focus();
			return false;
		}
		} 
		else 
		{
		alert("Please enter your name.");
		form.Name.focus();
		return false;
		}
	
	
	// EmailAddress
	if (form.Email.value != "") {
		if (!isEmailAddr(form.Email.value)) {
			alert("Please enter a valid Email address \(ex: yourname@yourdomain.com\) ");
			form.Email.focus();
			return false;
		} else if (hasWhiteSpace(form.Email.value)) {
			alert("Invalid email address \(contains blank spaces\).");
			form.Email.focus();
			return false;
		}
	} else {
		alert("Please enter a valid email address.");
		form.Email.focus();
		return false;
	}
	
	// Phone
	if (form.Phone_Number.value != "") {
		/*if (isAlpha(form.Phone_Number.value)) {
			alert("Invalid Phone Number alpha.");
			form.Phone_Number.focus();
			return false;
		}*/
		if (checkInternationalPhone(form.Phone_Number.value)==false){
			alert("Invalid Phone Number. Make sure it's at least 10-digits.")
			form.Phone_Number.focus();
			return false
		}
	} else {
		alert("Please enter a valid phone number.");
		form.Phone_Number.focus();
		return false;
	}	

	
	//Check comments
	if (form.Comments.value != "") {
			if (form.Comments.value.indexOf("http") != -1)
		 	{
			alert("Please do not enter http in this field");
			form.Comments.focus();
			return false;
			} 
	} else {
		alert("Please enter your comments or inquiries.");
		form.Comments.focus();
		return false;
	}
}


// 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);
}


