
<!-- Copyright 2005 Bontrager Connection, LLC
//
// Two places need to be customized.
//
//
// Place 1:
// Between the quotation marks, specify the name of 
//    your form.

//var FormName = 'webtolead';


// Place 2:
// Between the quotation marks, specify the field names 
//    that are required. List the field name separated 
//    with a comma.

var RequiredFields = 'First Name,Last Name,Phone,E-mail,How did you hear about us?';
var RequiredElements = '1,2,3,4,5';


//
// No other customization of this JavaScript is required.
//
/////////////////////////////////////////////////////////

function ValidateRequiredFields(thisform)
{
	var FieldList = RequiredFields.split(",");
	var ElementList = RequiredElements.split(",");
	var BadList = new Array();

	with(thisform) {
		for(var i = 0; i < FieldList.length; i++) {
			try{
				var s = eval('elements['+ElementList[i]+']' + '.value');
			} catch(e) {
				alert(e + ' | ' + FieldList[i]);
			}
			
			s = StripSpacesFromEnds(s);
			if(s.length < 1) { 
				BadList.push(FieldList[i]); 
			}
			else if(eval('elements['+ElementList[i]+']' + '.name') == 'hearabout') {
				if(eval('elements['+ElementList[i]+']' + '.value') == 'Other') {
					var SpecifyOther = parseInt(ElementList[i]) + 1;
					var n = eval('elements['+SpecifyOther+']' + '.value');
					if(n.length < 1) {
						BadList.push('How did you hear about us? (You chose \'Other\', please specify!)');
					}
				}
			}
			else if(eval('elements['+ElementList[i]+']' + '.name') == 'phone') {
				var n = eval('elements['+ElementList[i]+']' + '.value');
				if(checkInternationalPhone(n) == false) {
					BadList.push('Phone (Please enter a valid phone number!)');
				}
			}
			else {
				var n = eval('elements['+ElementList[i]+']' + '.name');
				if(n == 'email') {
					n = eval('elements['+ElementList[i]+']');
					if(ValidateEmail(n) == false) {
						BadList.push('E-mail (Please enter a vaild e-mail address!)');
					}
				}
			}
		}
	}
	
	if(BadList.length < 1) { 
		return true; 
	}
	
	var ess = new String();
	if(BadList.length > 1) {
		ess = 's';
	}

	var message = new String('The following field' + ess + ' are required:\n');
	for(var i = 0; i < BadList.length; i++) { 
		message += '\n' + '- ' + BadList[i]; 
	}
	
	alert(message);
	return false;
}

function StripSpacesFromEnds(s)
{
	while((s.indexOf(' ',0) == 0) && (s.length > 1)) {
		s = s.substring(1,s.length);
	}
	while((s.lastIndexOf(' ') == (s.length - 1)) && (s.length > 1)) {
		s = s.substring(0,(s.length - 1));
	}
	if((s.indexOf(' ',0) == 0) && (s.length == 1)) {
		s = ''; 
	}

	return s;
}

function ValidateEmail(field)
{
with (field)
  {
  apos=value.indexOf("@");
  dotpos=value.lastIndexOf(".");
  if (apos<1||dotpos-apos<2)
    {return false;}
  else {return true;}
  }
}

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// 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 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 >= minDigitsInIPhoneNumber);
}

function ElementToNameID()
{
	var msg = new String();
	for(var j = 0; j < document.webtolead.elements.length; j++) {
		msg += '('+j+') '+ document.webtolead.elements[j].name +'\n';
	}
	alert(msg);
}

// -->
