// JavaScript Document to check the the online quote request form for required and valid fields

<!-- begin form checking javascript section 
// Declaring required variables
// digits is used to validate number-only fields (used in phone and zip code fields)
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;
// string to carry the error messages from checking functions back to FormCheck function
var errortext = "";
// string to identify whether US or Intl phone number has been entered - only 1 is required
var locale = "";

function FormCheck(fn) { //parameter fn is form number which is passed in from the calling form.  
						//this indexing into the forms array allows multiple forms on the same page.
var thisForm = document.forms[fn].name; 
//alert("start of FormCheck function. \nThe name of the calling form is " + thisForm + ".\nIn this form there are " + document.forms[fn].length + " elements. \nIn this page there are " + document.forms.length + " forms.");
//alert("sendIt2 - step1: assigned email value to userEmail variable: " + document.forms[fn].email.value );

// check that first name and last name boxes are not empty
	if(WithoutContent(document.forms[fn].fname.value)){
		alert("Please enter your first name and re-submit.");
		document.forms[fn].fname.focus();
		document.forms[fn].fname.select();
		return false;
		}
	if(WithoutContent(document.forms[fn].lname.value)){
		alert("Please enter your last name and re-submit.");
		document.forms[fn].lname.focus();
		document.forms[fn].lname.select();
		return false;
		}
/**/	
// check for valid email
//alert("step2: made it to InvalidEmail function call");
	if (InvalidEmail(document.forms[fn].email.value))
		{
		alert("A valid email address is required.  The email address you entered contains the following errors:\n" + errortext + "\n\nPlease enter a valid email address and re-submit.");
		document.forms[fn].email.focus();
		document.forms[fn].email.select();
		return false;
		}
// check that job title and company name are not empty
	if(WithoutContent(document.forms[fn].title.value)){
		alert("Please enter your job title and re-submit.");
		document.forms[fn].title.focus();
		document.forms[fn].title.select();
		return false;
		}
	if(WithoutContent(document.forms[fn].cname.value)){
		alert("Please enter your company's name and re-submit.");
		document.forms[fn].cname.focus();
		document.forms[fn].cname.select();
		return false;
		}
// if zip code is entered (it's not a repuired field), then check for valid zip code (first 5 are numbers and length is either 5 or 10 (including the dash))
	if(InvalidZip(document.forms[fn].czip.value)){
		alert(errortext);
		document.forms[fn].czip.focus();
		document.forms[fn].czip.select();
		return false;
		}
// check for valid company website url
	if(InvalidURL(document.forms[fn].cwebsite.value)){
		alert("Please enter a valid web site address in the format \"www.yourwebsite.com\" for your company and re-submit.");
		document.forms[fn].cwebsite.focus();
		document.forms[fn].cwebsite.select();
		return false;
		}
//check for a phone number being entered in either the US or Int'l boxes
	if(NoneWithContent(document.forms[fn].cphone)){ 
		alert("Please enter a phone number into the appropriate box and re-submit.");
		document.forms[fn].cphone[0].focus();
		document.forms[fn].cphone[0].select();
		return false;
		}
	else {
		locale = "";
		if(document.forms[fn].cphone[0].value.length > 0)
			{
			locale = "US";
			}
		else {
			locale = "Intl";
//alert("the phone number length for the US phone field is: " + document.forms[fn].cphone[0].value.length);
			}
		}

// check for valid U.S. phone number
//alert("got to USphone number check function and variable locale is: " + locale); 
	if(locale == "US" && InvalidUSPhone(document.forms[fn].cphone[0])){
		alert("Please enter a valid phone number (with area code) and re-submit.");
		document.forms[fn].cphone[0].focus();
		document.forms[fn].cphone[0].select();
		return false;
		}
// check for valid international phone number
//alert("got to Intl phone number check function and variable locale is: " + locale); 
	if(locale == "Intl" && InvalidInternationalPhone(document.forms[fn].cphone[1])){
		alert(errortext);
		document.forms[fn].cphone[1].focus();
		document.forms[fn].cphone[1].select();
		return false;
		}
/*// check for valid target website url
	if(InvalidURL(document.forms[fn].targeturl.value)){
		alert("Please enter a valid web site address for the free search rank analysis in the format \"www.yourwebsite.com\" and re-submit.");
		document.forms[fn].targeturl.focus();
		document.forms[fn].targeturl.select();
		return false;
		}

// check that there is at least one keyword phrase entered
	if(NoneWithContent(document.forms[fn].keyword)){
		alert("Please enter at least one keyword phrase and re-submit.");
		document.forms[fn].keyword[0].focus();
		document.forms[fn].keyword[0].select();
		return false;
		}
// check to make sure offer terms are accepted
var accepted = document.forms[fn].acceptofferterms[0].checked;
//alert("got to offer terms check, and accepted is: " + accepted);
	if(document.forms[fn].acceptofferterms[0].checked == false) {
		alert("In order to process your request, you must indicate your acceptance of the terms of this free offer.  \nPlease indicate your acceptance by clicking on the \"yes\" button and re-submit.");
		document.forms[fn].acceptofferterms[0].focus();
		document.forms[fn].acceptofferterms[0].select();
		return false;
		}
*/
// check that at least one type of service is selected
	if(NoneWithCheck(document.forms[fn].services)){ 
		alert("Please indicate the StratiMind services that interest you by checking at least one check box and then re-submit.");
		document.forms[fn].services[0].focus();
		document.forms[fn].services[0].select();
		return false;
		}
// check that "current situation", "project goals" and "how we can help" are not empty
	if(WithoutContent(document.forms[fn].current_situation.value)){
		alert("Please provide us with some information about your current situation (such as why you are looking for help) and re-submit.");
		document.forms[fn].current_situation.focus();
		document.forms[fn].current_situation.select();
		return false;
		}
	if(WithoutContent(document.forms[fn].project_goals.value)){
		alert("Please provide us with some information about your project goals (what you are trying to accomplish) and re-submit.");
		document.forms[fn].project_goals.focus();
		document.forms[fn].project_goals.select();
		return false;
		}
	if(WithoutContent(document.forms[fn].how_help.value)){
		alert("Please provide us with some information about the kind of help you would like from StratiMind and re-submit.");
		document.forms[fn].how_help.focus();
		document.forms[fn].how_help.select();
		return false;
		}

// check to make sure a selection is made for "decision stage", "decision timeframe" and "project start time"
	if(WithoutSelectionValue(document.forms[fn].decision_stage)){ 
		alert("Please choose a stage of the consulting hiring process from the drop-down menu and re-submit.");
		document.forms[fn].decision_stage.options[0].focus();
		return false;
		}
	if(WithoutSelectionValue(document.forms[fn].decision_timeframe)){ 
		alert("Please select a decision timeframe from the drop-down menu and re-submit.");
		document.forms[fn].decision_timeframe.options[0].focus();
		return false;
		}
	if(WithoutSelectionValue(document.forms[fn].project_start)){ 
		alert("Please choose a project start time from the drop-down menu and re-submit.");
		document.forms[fn].project_start.options[0].focus();
		return false;
		}
/**/
// check to make sure a radio button is selected for "currently budgeted" and "signing authority"
	if(NoneWithCheck(document.forms[fn].budgeted)){ 
		alert("Please indicate the budget status of this project by clicking one radio button and re-submit.");
		document.forms[fn].budgeted[0].focus();
		document.forms[fn].budgeted[0].select();
		return false;
		}
	if(NoneWithCheck(document.forms[fn].signature_authority)){ 
		alert("Please indicate signature authority for this project by clicking one radio button and re-submit.");
		document.forms[fn].signature_authority[0].focus();
		document.forms[fn].signature_authority[0].select();
		return false;
		}
/*	if(WithoutCheck(document.forms[fn].radioLoner)){ 
		alert("Please click the single radio button and re-submit.");
		document.forms[fn].radioLoner.focus();
		document.forms[fn].radioLoner.select();
		return false;
		}
	if(WithoutCheck(document.forms[fn].checkLoner)){ 
		alert("Please check the checkbox and re-submit.");
		document.forms[fn].checkLoner.focus();
		document.forms[fn].checkLoner.select();
		return false;
		}
	if(NoneWithContent(document.forms[fn].oneOrTheOther)){ 
		alert("Please enter some text into either or both of the text boxes and re-submit.");
		document.forms[fn].oneOrTheOther[0].focus();
		document.forms[fn].oneOrTheOther[0].select();
		return false;
		}
	if(WithoutContent(document.forms[fn].areaName.value)){ 
		alert("Please enter some text into the large textarea box and re-submit.");
		document.forms[fn].areaName.focus();
		document.forms[fn].areaName.select();
		return false;
		}
	if(WithoutContent(document.forms[fn].FileGet.value)){ 
		alert("Please select a filename for uploading and re-submit.");
		document.forms[fn].FileGet.focus();
		document.forms[fn].FileGet.select();
		return false;
		}
*/
// Put field checks above this point and comment-out those that aren't used in this form.
//	alert("made it to return true at end of sendIt2 script. Should submit form to server and go to thankyou page next.");
//alert("made it to final 2 lines of FormCheck function.  Form should now email contents and forward to thankyou page.");
	document.forms[fn].submit();
	return true;
} // end of FieldCheck function.

// begin individual checking functions (called from FieldCheck function)
function InvalidEmail(email) {
			var invalidChars = " /:,;";
//alert("made it to InvalidEmail function.  \nemail variable defined as: " + email + "\ninvalidChars variable defined as: " + invalidChars);
			errortext = "";
				if (email == "") 
					{
					errortext += "\nnothing entered in email box";
					return true;
					}
			for (i=0; i<invalidChars.length; i++) 
				{
				badChar = invalidChars.charAt(i);
				if (email.indexOf(badChar,0) > -1) 
					{
					errortext += "\nemail contains one or more of the following invalid characters: <space>,/,:,;,\"";
					return true;
					}
				}
			atPos = email.indexOf("@",1);
			if (atPos == -1) //true when at symbol is missing 
				{
				errortext += "\nemail does not contain an \"@\" symbol.";
				return true;
				}
			if (email.indexOf("@",atPos+1) > -1) 
				{
				errortext += "\nemail contains too many \"@\" symbols.";
				return true;
				}
			periodPos = email.indexOf(".",atPos);
			if (periodPos == -1) 
				{
				errortext += "\nemail does not contain a \".\" ";
				return true;
				}
			if (periodPos+3 > email.length)	{
				errortext += "\nemail contains more than 3 characters after the second period.";
				return true;
			}
			return false;
		}

function InvalidURL(url)
  {
	var urlcheck = url.match(/\b(^(\S+\.\S+\.\S{2,3}))\b/gi);
	// url is invalid if it doesn't match the regular expression patttern: any number of non-whitespace characters followed by a period "." followed by any number of non-whitespace characters followed by a second "." followed by 2 or 3 non-whitespace characters)
	if (urlcheck == null) 		
		{
		return true;
		}
	return false;
  }
 
function InvalidZip(zip)
  {
   var len=zip.length;
   errortext = "";
   if(len > 0 && len != 5 && len != 10)
    	{
		errortext += "\nThe zip/postal code you entered is not the correct length.  Please re-enter your zip code in \"xxxxx\" or \"xxxxx-xxxx\" format and press the SEND button again.";
//alert("made it to zip check for length.  variable len is: " + len + ", variable errortext is: " + errortext);
     	return true;
		}
   for(i=0; i<5; i++)
   		{
		if (len > 0 && digits.indexOf(zip.charAt(i))<0) // true if something has been entered and any of the first five characters are not numbers
			{
			errortext += "\nThe zip/postal code you entered contains letters. The first five digits must be numbers.  Please enter a valid zip/postal code and re-submit";
//alert("made it to zip check for invalid characters.  loop check value is: " + digits.indexOf(zip.charAt(i)) + ", variable errortext is: " + errortext);
     		return true;
			}
      	}
  return false;
  }
 
function InvalidUSPhone(phonenumber) 
{ 
//////////////////////////////////////
// Input: a single string parameter - the US/Canada phone number with area code 
// Output: boolean true(1) or false(0) 
// 
// The InValidUSPhone function will return true for any alphanumeric string that does not match the following 
// sequence of characters: any number of spaces [optional], a single 
// open parentheses [optional], any number of spaces [optional], 3 digits (area 
// code), any number of spaces [optional], a single close parentheses [optional], a single 
// dash [optional], any number of spaces [optional], 3 digits, any number of spaces [optional], 
// a single dash [optional], any number of spaces [optional], 4 digits, any 
// number of spaces [optional]. 
//////////////////////////////////////// 

//alert("got to US Phone Number check function");
if((phonenumber.value.match(/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null)) //true if there is no match to the phone number pattern (match array is empty or null)
	{
	return true; 
	}
return false; 
} 

function isInteger(s)  // used in intl phone number check function
	{   
//	alert("made it to isInteger function.");
	//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 digits.
//alert("all characters in stripped Int Phone number are digits and length of stripped string is: " + s.length);
    return true;
}

function stripCharsInBag(s,bag) // used in intl phone number check function
	{  
//	alert("made it to stripCharsInBag function");
	//alert("variable values are:\n s = " + s + ", \n bag = " + bag + ",\n s.length = " + s.length); 
	//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);
//		alert(i + " of " s.length + "\nc is: " + c + ", \nbag.indexOf(c) is: " + eval(bag.indexOf(c)) + ",\nreturnstring is: " + returnString);
        if (bag.indexOf(c) == -1) returnString += c;
    }
//	alert("after stripcharsinbag function, returnString variable is: " + returnString);
    return returnString;
}

function checkInternationalPhone(strPhone)
	{ 
//	alert("made it to new iphone checkInternationalPhone function and strPhone is: " + strPhone + "and validWorldPhoneChars is: " + validWorldPhoneChars);
	s=stripCharsInBag(strPhone,validWorldPhoneChars); // s is the phone number string after stripping all the acceptable non-digit characters
//	alert("back in checkInternationalPhone function after call to stripCharsInBag and string s is now: " + s);
	if(isInteger(s)==false)
		{
		errortext += "\nthe international phone number contains invalid characters.";
		return false;
		}
	if(s.length < minDigitsInIPhoneNumber)
		{ 
		errortext += "\nthe international phone number has fewer than the required " + minDigitsInIPhoneNumber + " digits.";
		return false;
		}
//	alert("checkInternationalPhone determined that iphone is all integers and long enough");
	return true;// returns true if the stripped phone number is all integers and at least the required length
}

function InvalidInternationalPhone(iPhone){
//alert("made it to InvalidInternationalPhone function");
errortext = "";
	if ((iPhone.value==null)||(iPhone.value=="")) // true if nothing is entered in the form
		{
//		alert("iphone field is empty text or null");
		errortext += "Please Enter your Phone Number and re-submit.";
		return true;
		}
	if (checkInternationalPhone(iPhone.value)==false) // true if phone number is either not all integers or is too short
		{
//		alert("checkinternationalphone function has returned false, meaning stripped phone string is not all integers or is not long enough.");
		errortext += "\nPlease Enter a Valid International Phone Number, including country code and re-submit.";
		return true;
		}
	return false;
 }
/* end international phone number check */

function WithoutContent(ss) {
//alert("sendIt2 - step4: got to WithoutContent function and the length of text in the text box is " +  ss.length);
if(ss.length > 0) { return false; }
return true;
}

function NoneWithContent(ss) {
//alert("sendIt2 - step4: got to NoneWithContent function and the length of text in the 1st text box is " +  ss[0].value.length);
for(var i = 0; i < ss.length; i++) {
	if(ss[i].value.length > 0) { return false; }
	}
return true;
}

function NoneWithCheck(ss) {
//alert("sendIt2 - step4: got to NoneWithCheck function and the value of the 1st check box is " + ss[0].checked + "and the length of the ss array is " + ss.length);
for(var i = 0; i < ss.length; i++) {
	if(ss[i].checked) { return false; }
	}
return true;
} 

function WithoutCheck(ss) {
//alert("sendIt2 - step4: got to WithoutCheck function and the value of the check box is " + ss.checked );
	if(ss.checked) { return false; }
	return true;
	}
function WithoutSelectionValue(ss) {
//alert("sendIt2 - step4: got to WithoutSelectionValue function and the length of the selection list is " + ss.length + ", and the value of the 1st selection box is " + ss[0].selected + " and the ss[0].value.length is " + ss[0].value.length);
for(var i = 0; i < ss.length; i++) {
	if(ss[i].selected) {
		if(ss[i].value.length) { return false; }
		}
	}
return true;
}

// end form checking javascript section -->

