// JavaScript Document
function validate_required(field,alerttxt) {
	with (field) {
		if (value == null || value == "") { // if value is = to null OR value = to "nothing"
			alert(alerttxt); // display alert textbox
			return false;
		} else {
			return true;
		} // end if
	} // end with
} // end function

function validate_email(field,alerttxt) {
	with (field) {
		apos   = value.indexOf("@")
		dotpos = value.lastIndexOf(".")
		if (apos < 1 || dotpos-apos < 2) {
			alert(alerttxt);
			return false;
		} else {
			return true;
		} // end if
	} // end with
} // end function

// Declaring required variables for phone number
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);
}
//end phone validation functions

function validate_form(thisform) {
	with (thisform) {

	//name
	if (validate_required(name,"Please tell us your name.")==false) { 
		name.focus();
		return false;
	}
	//home phone
	var Phone=document.contactForm.phone
	
	if ((Phone.value==null)||(phone.value=="")) {
		alert("Please provide us with a phone number where we can reach you.")
		phone.focus();
		return false;
	}
	if (checkInternationalPhone(phone.value)==false) {
		alert("Please insert a valid phone number.\nExample: 239-555-5555 or 239555555")
		phone.value=""
		phone.focus();
		return false;
	}
	//email
	if (validate_email(email,"Please insert your e-mail address.\nExample: joe@aol.com.")==false) {
		email.focus();
		return false;
	}
	//message
	if (validate_required(message,"Please tell me how we may assist you.")==false) { 
		message.focus();
		return false;
	}

	} // end with
} // end function