if(!Array.indexOf){
  Array.prototype.indexOf = function(obj){
   for(var i=0; i<this.length; i++){
    if(this[i]==obj){
     return i;
    }
   }
   return -1;
  }
}


var phone_field_length=0;

//Auto-Tabs to next inputbox
function TabNext(obj,event,len,next_field) {
	if (event == "down") {
		phone_field_length=obj.value.length;
		}
	else if (event == "up") {
		if (obj.value.length != phone_field_length) {
			phone_field_length=obj.value.length;
			if (phone_field_length == len) {
				next_field.focus();
			}
		}
	}
}

function setfocus(thisbox) {
	thisbox.focus();
}

//Validates Email
function checkEmail(entered,thisid) {
	with (entered) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		lastpos=value.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
			entered.select();
			entered.focus();
			return false; 
		}
		else {
			return true;
		}
	}
}

//Handles multiple form types and fields for microsites
function validate_combo_breaker(validate_obj){
var validate_obj_size = validate_obj.length - 1;
	//standardize values for future use
	for(current=0;current<=validate_obj_size; current++){
		switch(validate_obj[current].name){
			case 'country':
			countryobj = validate_obj[current];
			break;
		}
	}
	
	for(current=0;current<=validate_obj_size; current++){
		switch(validate_obj[current].name){
			case 'lead_referrer': 
			if(checkName(validate_obj[current])==false){
				alert("Please enter a referrer name");
				return false;
			}
			break;
			
			case 'firstname':
			if (checkName(validate_obj[current]) == false) {
				alert("Please enter a first name.");
				return false;
			}
			break;
			
			case 'lastname':
			if (checkName(validate_obj[current]) == false) {
				alert("Please enter a last name.");
				return false;
			}
			break;
			
			case 'email':
			case 'txtEmailAddress':  //LeadWatchLive
			if (checkEmail(validate_obj[current]) == false) {
				alert("Please enter a valid email.");
				return false;
			}
			break;
			
			case 'address1':
			if (checkAddr(validate_obj[current]) == false) {
				alert("Please enter a valid street address.");
				return false;
			}
			break;
			
			case 'address2':
			if (validate_obj[current].value == "Apt.") {
				validate_obj[current].value = "";
			}
			break;
			
			case 'city':
			if (checkAddr(validate_obj[current]) == false) {
				alert("Please enter a valid city.");
				return false;
			}
			break;
			
			case 'state':
			if (checkCountry(countryobj) == false && checkState(validate_obj[current]) == false) {
				alert("Please enter a valid state.");
				return false;
			}
			break;
			
			case 'zip':
			case 'txtZipCode': //LeadWatchLive
			if (checkCountry(countryobj) == false && checkZipsubmit(validate_obj[current]) == false) {
				return false;
			}
			break;
			
			case 'homephone':
			case 'txtPhone1': //LeadWatchLive
			if(checkCountry(countryobj) == false && ValidatePhone(validate_obj[current]) == false) {
				alert("Please enter a valid phone number.");
				return false;
			}
			else if(checkCountry(countryobj) == true &&  ValidateInternationalPhone(validate_obj[current]) == false) {
				alert("Please enter an internationally valid phone number.");
				return false;
			}
			break;
			
			case 'cellphone':
			if(ValidatePhone(validate_obj[current]) == false) {
				return false;
			}
			break;
			
			case 'program':
			if (checkMajor(validate_obj[current]) == false) {
				alert("Please enter a valid program of interest.");
				return false;
			} 
			break;
			
			case 'highestlevel':
			case 'EducationLevel': //LeadWatchLive
				if (checkHighest(validate_obj[current]) == false) {
					alert("Please select your highest level of education.");
					return false;
				}
			break;
			
			case 'company':
			if($("input[name=referrer_type]:checked").val()==="organization"){
				if(($("[name=company]").val() === "") || ($("[name=company]").val() === "Organization Name")){
				alert("Please enter the name of the organization or company.");
				return false;
				}
			}
			break;
			
			//NOT EMPTY
			case 'ClientDegreeId': //LeadWatchLive
			var obj_title = validate_obj[current].title;
			if(validate_obj[current].value.length <= 0){
				alert("please supply a " + obj_title);
				return false;
			}
			break;
			
			case 'user_def1':
			if(user_def_rules(validate_obj[current]) == false){
				return false;
			}
			break;
			
			case 'user_def2':
			if(user_def_rules(validate_obj[current]) == false){
				return false;
			}
			break;
			
			case 'user_def3':
			if(user_def_rules(validate_obj[current]) == false){
				return false;
			}
			break;
			
			case 'user_def4':
			if(user_def_rules(validate_obj[current]) == false){
				return false;
			}
			break;
			
			case 'user_def5':
			if(user_def_rules(validate_obj[current]) == false){
				return false;
			}
			break;
			
			default:
			if(current = validate_obj_size){
				return true;
			}
			break;
		}
	}
	return true;
}

function user_def_rules(validate_obj){
	switch(validate_obj[current].title){
	
	case "First Name":
		if (checkName(validate_obj) == false) {
			alert("Please enter a first name.");
			return false;
		}
	break;
	
	case "Last Name":
		if (checkName(validate_obj) == false) {
			alert("Please enter a last name.");
			return false;
		}
	break;
	
	default: 
		alert("no rule defined for field " + validate_obj[current].title);
	break;
	}
}

function validate_spread(fnameobj, lnameobj, emailobj, refererobj) {
	if (checkName(refererobj) == false) {
		alert("Please enter your name.");
		return false;
	}
	else if (checkName(fnameobj) == false) {
		alert("Please enter your friend's first name.");
		return false;
	}
	else if (checkName(lnameobj) == false) {
		alert("Please enter your friend's last name.");
		return false;
	}
	else if (checkEmail(emailobj) == false) {
		alert("Please enter your friend's email.");
		return false;
	}
	else {
		return true;
	}
}

function submitForm(fm) {
	if(fm.firstname.value == "First Name"){
		alert("Please Type a Valid First Name");
		return false;
	}
	else if(fm.lastname.value == "Last Name"){
		alert("Please Type a Valid Last Name");
		return false;
	}
	else if(fm.email.value == "Type Your Email"){
		alert("Please Type a Valid email");
		return false;
	}
	else
		return true;
}

function checkCountry(objName) {
    var nametfield = objName;
    if (chkCountry(objName) == false) {
		return false;
    }
    else {
        return true;
    }
}

function chkCountry(objName) {
	var thisCountry = objName.value;
	if(thisCountry == "US") {
		return false;
	}
	else {
		return true;
	}
}

function checkHighest(objName) {
	var objName = objName.value;
	if(objName == "") {
		return false;
	}
    else {
        return true;
    }
}

function checkMajor(objName) {
	var objName = objName.value;
	if(objName == "") {
		return false;
	}
    else {
        return true;
    }
}


//validation for the firstname and lastname fields (mainly for on submit use)
function checkName(objName) {
    var namefield = objName;
    if (chkname(objName) == false) {
        namefield.select();
        objName.focus();
        return false;
    }
    else {
        return true;
    }
}

//helper function for checkName()
/*function chkname(objName){
	var thisName = objName.value;
	var disavar = "((item1|item2))
	if(/((first(|-| )name)|(last(| )name))/i.test(thisName) || /last(| )name/i.test(thisName) || /nombre/i.test(thisName) || /apellido/i.test(thisName) || /full name/i.test(thisName)) {
		return false;
	}
	else {
		return true;	
	}
}*/

function chkname(objName)
{
	var defaultValues = ['first name', 'last name', 'nombre', 'apellido', 'full name'];
	// unclear validation below.
	return (-1 === defaultValues.indexOf(objName.value.toLowerCase()));
}

function checkAddr(objName) {
    var namefield = objName;
    if (chkaddr(objName) == false) {
        namefield.select();
        objName.focus();
        return false;
    }
    else {
        return true;
    }
}

function chkaddr(objName){
	var thisAddress = objName.value;
	if(thisAddress == "Street Address") {
		return false;
	}
	else if(thisAddress == "Direcci\363n"){
		return false;
	}
	else if(thisAddress == ""){
		return false;
	}
	else if(thisAddress == "City") {
		return false;
	}
	else if(thisAddress == "Ciudad") {
		return false;
	}
	else {
		return true;
	}
}

function checkState(objName) {
    var namefield = objName;
    if (chkState(objName) == false) {
		return false;
    }
    else {
        return true;
    }
}
function chkState(objName) {
	var thisState = objName.value;
	if(thisState == "") {
		return false;
	}
	else {
		return true;
	}
}
function checkZipsubmit(objName) {
    var namefield = objName;
    if (chkzipsubmit(objName) == false) {
        namefield.select();
		return false;
        objName.focus();
    }
    else {
        return true;
    }
}

function checkZipsubmitSpanish(objName) {
    var namefield = objName;
    if (chkzipsubmitSpanish(objName) == false) {
        namefield.select();
		return false;
        objName.focus();
    }
    else {
        return true;
    }
}
	
function chkzipsubmit(field) {
	var valid = "0123456789-";
	var hyphencount = 0;
	var field = field.value;
	var chkzip_regex = /^\d{5}(|-\d{4})$/;
	if ((field.length!=5 && field.length!=10) || !(chkzip_regex.test(field))) {
		alert("Please enter your 5 digit or 5 digit+4 zip code.");
		return false;
	}
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") {
			alert("Invalid characters in your zip code.  Please try again.");
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
			return false;
		}
	}
	return true;
}

function chkzipsubmitSpanish(field) {
	var valid = "0123456789-";
	var hyphencount = 0;
	var field = field.value;

	if (field.length!=5 && field.length!=10) {
	alert("Por favor ingrese su c\363digo postal.");
	return false;
	}
	for (var i=0; i < field.length; i++) {
	temp = "" + field.substring(i, i+1);
	if (temp == "-") hyphencount++;
	if (valid.indexOf(temp) == "-1") {
	alert("Por favor ingrese su c\363digo postal .");
	return false;
	}
	if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
	alert("Por favor ingrese su c\363digo postal .");
	return false;
	   }
	}
	return true;
}
////validation for any field that does not contain a default input (ex. askQuestion textbox)
function checkNull(objName) {
    var nullfield = objName;
    if (chkNull(objName) == false) {
        nullfield.select();
        objName.focus();
        return false;
    }
    else {
        return true;
    }

}

//helper function for checkNull()
function chkNull(objName){
	thisNull = objName.value;
	if(thisNull == "") {
		return false;
	}
	else {
		return true;
	}
}



//*** INPUT NAMES ***//
//RETURNS INPUT INDEX
function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
		else i++;
	return index;
}

//LOOPS THROUGH ARRAYS CHECKING FOR THE STRING
function inArray(thisString,thisArray) {
    var len=thisArray.length;
    for(var i=0;i<len;i++){
        if(thisArray[i]==thisString)
            return i;
    }
    return -1;
}

//CHECKS IF CLASS ALREADY EXISTS
function isClassPresent(thisDoc,thisClass){
    var rgClasses=thisDoc.className.split(" ");
    return inArray(thisClass,rgClasses)>=0;
}

//CREATES CLASSES FOR EACH 'document.'
function addClass(Pq){
    var classes=Pq.className.split(" ");
    var len=arguments.length;
    for(var i=1;i<len;i++){
        var op=arguments[i].slice(0,1);
        var cls=arguments[i].slice(1);
        var idx=inArray(cls,classes);
        if(op=="-") {
            if(idx!=-1)
                classes.splice(idx,1);
        }
        else {
            if(idx==-1)
                classes.push(cls);
        }
    }
    var newClass=classes.join(" ");
    if(Pq.className!=newClass)
        Pq.className=newClass;
}

//KEEPS WHAT'S INSIDE INPUTBOXES
function keepInputText(pEvent,pDocument) {
    var bIsClean=isClassPresent(pDocument,"clean");
    if(bIsClean&&(pEvent.type=="focus"||pEvent.type=="drop")) {
        pDocument.value="";
        addClass(pDocument,"-clean");
    }
    else {
        if(pDocument.value.length==0&&pEvent.type=="blur") {
            pDocument.value=pDocument.getAttribute("title");
            addClass(pDocument,"+clean");
        }
    }
}
//-->


/**
 * 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("(")
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidateInternationalPhone(Phone){
	Phone.value = Phone.value.replace(/[^0-9]/g,"");
	if ((Phone.value.length >= 10) && (Phone.value.length <= 15)){
		return true;
	}
	else{
	Phone.value=""
	Phone.focus()
	return false
	}
 }
 
 function ValidatePhone(Phone){
	if (checkInternationalPhone(Phone.value)==false && Phone.value != ""){
		Phone.focus()
		return false
	}
	return true
 }
 
 function ValidatePhoneSpanish(Phone){
	if (checkInternationalPhone(Phone.value)==false && Phone.value != ""){
		alert("Por favor ingrese su n\372mero de tel\351fono.")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }
 
 

