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;
		}
	}
}

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;
	}
}

//Calls various validation functions for call on form submit
function validate(highestobj, majorobj, fnameobj, lnameobj, homephoneobj, emailobj, addrobj, cityobj, stateobj, zipobj, cntryobj, add2obj) {

	if (checkHighest(highestobj) == false) {
		alert("Please select your highest level of education.");
		return false;
	}
	else if (checkMajor(majorobj) == false) {
		alert("Please enter a valid major.");
		return false;
	} 
	else if (checkName(fnameobj) == false) {
		alert("Please enter your first name.");
		return false;
	}
	else if (checkName(lnameobj) == false) {
		alert("Please enter your last name.");
		return false;
	}
	else if(ValidatePhone(homephoneobj) == false) {
		return false;
	}
	else if (checkEmail(emailobj) == false) {
		alert("Please enter a valid email.");
		return false;
	}
	else if (checkAddr(addrobj) == false) {
		alert("Please enter a valid street address.");
		return false;
	}
	else if (checkAddr(cityobj) == false) {
		alert("Please enter a valid city.");
		return false;
	}
	else if (checkState(stateobj) == false && checkCountry(cntryobj) == false) {
		alert("Please enter a valid state.");
		return false;
	}
	else if (checkCountry(cntryobj) == true && stateobj.value == "Other") {
		stateobj.value = "";
		return true;
	}
	else if (checkZipsubmit(zipobj) == false && checkCountry(cntryobj) == false) {
		return false;
	} 
	else if (checkCountry(cntryobj) == true && zipobj.value == "Zip") {
		zipobj.value = "";
		return true;
	}
	else if (add2obj.value == "Apt.") {
		add2obj.value = "";
		return true;
    }
	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 namefield = objName;
    if (chkCountry(objName) == false) {
		return false;
    }
    else {
        return true;
    }
}

function chkCountry(objName) {
	thisName = objName.value;
	if(thisName == "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){
	thisName = objName.value;
	if(thisName == "First Name" || thisName == "Last Name" || thisName == "Your Full Name") {
		return false;
	}
	else {
		return true;	
	}
}

function checkAddr(objName) {
    var namefield = objName;
    if (chkaddr(objName) == false) {
        namefield.select();
        objName.focus();
        return false;
    }
    else {
        return true;
    }
}

function chkaddr(objName){
	thisName = objName.value;
	if(thisName == "Street Address") {
		return false;
	}
	else if(thisName == "City") {
		return false;
	}
	else {
		return true;
	}
}
function checkState(objName) {
    var namefield = objName;
    if (chkState(objName) == false) {
		return false;
    }
    else {
        return true;
    }
}
function chkState(objName) {
	thisName = objName.value;
	if(thisName == "") {
		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 chkzipsubmit(field) {
	var valid = "0123456789-";
	var hyphencount = 0;
	var field = field.value;

	if (field.length!=5 && field.length!=10) {
	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;
}

////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 ValidatePhone(Phone){
	if (checkInternationalPhone(Phone.value)==false && Phone.value != ""){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }
 
 
