// JavaScript Document

function myAlert() {
	alert("made it");
}

function isValidForm()
{
	var validEmail = Email_Check(document.inquiry.email.value.toLowerCase());
	//var validPhone = validatePhone(document.inquiry.phone.value);
	var validPhone = checkInternationalPhone(document.inquiry.phone.value);
		
	if ((validEmail == false) || (document.inquiry.email.value.length == 0))
	{
		alert("Your email address is not a valid format.\n\n-  Enter your email address in the format of user@host.domain.\n- Your email must contain a valid domain.");
		document.inquiry.email.focus();
	}
	else if (validPhone == false)
	{
		alert("Please enter a valid Phone Number!");
		document.inquiry.phone.focus();
	}
	else
	{
		document.inquiry.submit();
	}	
}


//Validate phone number for 10 digit US numbers.
//phoneField - The HTML input field containing the phone number to validate.
//format - Integer value that defines how to format the text field.
function validatePhone(phoneField) {

	//alert(phoneField);
	
}

function Valid_Numbers(field_value) 
{
  var valid_num = "1234567890";
	
  //Verifies that each character is numeric.	
  for (var position=0; position < field_value.length; position++) 
  {
	var temp = "" + field_value.substring(position, position+1);
	if (valid_num.indexOf(temp) == "-1") return false;
  }//End for loop.			

  return true;  //Returns true if field_value contains valid characters.
}//End Valid_Num() function.


function Email_Check(emailStr) 
{
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (emailStr.indexOf(invalidChars.charAt(i),0) > -1) {
	      return false;
	   }
	}
	for (i=0; i<emailStr.length; i++) {
	   if (emailStr.charCodeAt(i)>127) {
	      return false;
	   }
	}

	// Variable Declarations
	
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\[\\]\\."   ;//Excludes the following special characters from e-mail addresses: ( ) < > @ , ; : \ " . [ ]
	var validChars="\[^\\s" + specialChars + "\]" ; //States which special characters aren't allowed in a user/domain name.
	var atom=validChars + '+' ;//Pattern for non-special characters.
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");  //Pattern for a normal symbolic domain

	var emailPat=/^(.+)@(.+)$/  ;//Verifies the user@domain format for e-mail.
	var quotedUser="(\"[^\"]*\")"  ;//Pattern for "user" a in quoted string.
	var word="(" + atom + "|" + quotedUser + ")";  //Pattern for 2 word user names (as separated by characters such as .
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");  // The following pattern describes the structure of the user
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/ ;  //Pattern for IP (rather than character) e-mail addresses.  Squared brackets required.
	var matchArray=emailStr.match(emailPat);  //Breaks up the user@domain format to individually analyze.

	if (matchArray==null) 
		{  //Checks for too many @, ., etc.
			return false;
		}

	//Validate user name
	var user=matchArray[1];
	var domain=matchArray[2];
	if (user.match(userPat)==null) 
		{    // user is not valid
	    		return false;
		}

	// Validate IP e-mail addresses
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) 
		{ // this is an IP address
		for (var i=1;i<=4;i++) 
			{
		    if (IPArray[i]>255) 
		    {
			return false;
		    }
		}
		return true;
		}
	
	var suffix = emailStr.substring(emailStr.lastIndexOf('.')+1);
	// .aero, .arpa, .biz, .cat, .com, .coop, .edu, .info, .int, .jobs, .mobi, .museum, .name, .net, .org, .pro, and .travel
	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' && suffix != 'cat' && suffix != 'jobs' && suffix != 'mobi' && suffix != 'travel') {
	   return false;
	}
	
	return true;
}

	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 trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}

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)
{
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && (s.length == 0 || s.length >= minDigitsInIPhoneNumber));
}

//End Email_Check() function