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();
}


function country_code() {
	if(document.forms.requestinfo.country[document.forms.requestinfo.country.selectedIndex].value != 'US') {
		document.getElementById("countrycode").style.display = "block";
	}
	else
		document.getElementById("countrycode").style.display = "none";
}

function validateform() {

	thisform = document.forms.requestinfo;
	thisform = document.requestinfo;

	apos=thisform.email.value.indexOf("@");
	dotpos=thisform.email.value.lastIndexOf(".");
	lastpos=thisform.email.value.length-1;
		
	
	if(thisform.address2.title == thisform.address2.value) thisform.address2.value = "";
	if(thisform.zip.title == thisform.zip.value) thisform.zip.value = "";
	if(thisform.homephone.title == thisform.homephone.value) thisform.homephone.value = "";
	if(thisform.cellphone.title == thisform.cellphone.value) thisform.cellphone.value = "";


	
	if(thisform.firstname.title == thisform.firstname.value) {
		alert("Please type your First Name.");
		thisform.firstname.focus();
		return false;
	}
	else if(thisform.lastname.title == thisform.lastname.value) {
		alert("Please type your Last Name.");
		thisform.lastname.focus();
		return false;
	}
	else if(thisform.term[thisform.term.selectedIndex].value == ""){
		alert("Please select a term");
		thisform.term.focus();
		return false;
	}
	else if(thisform.email.title == thisform.email.value) {
		alert("Email is required. Please type your email.");
		thisform.email.focus();
		return false;
	}
	else if(!checkEmail(thisform.email)) {
		alert("Email entered is invalid. Please try again");
		thisform.email.focus();
		return false;
	}
	else if(thisform.confirmemail.title == thisform.confirmemail.value) {
		alert("Confirm Email is required. Please re-type your email.");
		thisform.confirmemail.focus();
		return false;
	}
	else if(thisform.email.value != thisform.confirmemail.value) {
		alert("Your emails do not match. Please type the same email.");
		thisform.confirmemail.focus();
		return false;
	}
	else if(thisform.address1.title == thisform.address1.value) {
		alert("Please type your address.");
		thisform.address1.focus();
		return false;
	}
	else if(thisform.city.title == thisform.city.value) {
		alert("Please type your city.");
		thisform.city.focus();
		return false;
	}	
	
	else if(thisform.state[thisform.state.selectedIndex].value == ""){
		alert("Please select a state");
		thisform.state.focus();
		return false;
	}
	else if(thisform.homephone.value != "" && ValidatePhone(thisform.homephone) == false) {
		return false;
	}
	else if(thisform.cellphone.value != "" && ValidatePhone(thisform.cellphone) == false) {
		return false;
	}

	else
		return true;
}
function fieldname(fld)
{ // get the field label text or name
	if(fld.id && document.getElementsByTagName)
	{
		for(var i= 0, lbl= document.getElementsByTagName('LABEL'); i < lbl.length; i++)
			if(lbl[i].htmlFor==fld.id) return lbl[i].nodeValue||lbl[i].textContent||lbl[i].innerText;
		for(var i= 0, lbl= document.getElementsByTagName('label'); i < lbl.length; i++)
			if(lbl[i].htmlFor==fld.id) return lbl[i].nodeValue||lbl[i].textContent||lbl[i].innerText;
	}
	return fld.name||fld.type;
}

function fixInt(fld,sep)
{ // integer check/complainer 
	if(!fld.value.length||fld.disabled) return true; // blank fields are the domain of requireValue 
	var val = fld.value;
	if(typeof(sep)!='undefined') val= val.replace(new RegExp(sep,'g'),'');
	val = parseInt(val);
	if(isNaN(val))
	{ // parse error 
		fld.value = '';
		return false;
	}
	fld.value= val;
	return true;
}


function checkEmail(fld)
{ // simple email check
	if(!fld.value.length||fld.disabled) return true; // blank fields are the domain of requireValue 
	var phony= /@(\w+\.)*example\.(com|net|org)$/i;
	if(phony.test(fld.value))
	{ status= 'Please enter your email address in the '+fieldname(fld)+' field.'; return false; }
	var emailfmt= /^\w+([.-]\w+)*@\w+([.-]\w+)*\.\w{2,8}$/;
	if(!emailfmt.test(fld.value))
	{ status= 'The '+fieldname(fld)+' field must contain a valid email address.'; return false; }
	return true;
}



//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="";
    }
    else {
        if(pDocument.value.length==0&&pEvent.type=="blur") {
            pDocument.value=pDocument.getAttribute("title");
        }
    }
}
//-->


// Edit by Jon and Will
// Function takes field value and deletes all whitespaces (space, tab, etc.)
// Returns the field value with no whitespaces
function Trim(TRIM_VALUE)
{
	if(TRIM_VALUE.length < 1)
	{
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE=="")
	{
		return "";
	}
	else
	{
		return TRIM_VALUE;
	}
} //End Function

// Trims spaces off from right
function RTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0)
	{
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1)
	{
		if(VALUE.charAt(iTemp) == w_space)
		{
		}
		else
		{
		strTemp = VALUE.substring(0,iTemp +1);
		break;
		}
		iTemp = iTemp-1;

	} //End While
	return strTemp;

} //End Function

// Trims spaces off from left
function LTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	if(v_length < 1)
	{
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length)
	{
		if(VALUE.charAt(iTemp) == w_space)
		{
		}
	else
	{
		strTemp = VALUE.substring(iTemp,v_length);
		break;
	}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function

//FUNCTION TO VALIDATE THE CONFIRMATION DEPOSIT FORM
function validate_form(thisform) {
	 
	if(thisform.studentid.value == "") {
		alert("Please enter your Liberty Student ID");
		thisform.student_id.focus();
		return false;
	}	
	else if(thisform.firstname.title == thisform.firstname.value) {
		alert("Please type the Student's First Name.");
		thisform.firstname.focus();
		return false;
	}
	else if(thisform.lastname.title == thisform.lastname.value) {
		alert("Please type the Student's Last Name.");
		thisform.lastname.focus();
		return false;
	}
	else if((Trim(thisform.year.options[thisform.year.selectedIndex].value) == "")) {
		alert("Please select your Year of Entrance");
		thisform.year.focus();
		return false;
	}
	else if((Trim(thisform.term.options[thisform.term.selectedIndex].value) == "")) {
		alert("Please select your Term of Entrance.");
		thisform.term.focus();
		return false;
	}

	else if (!thisform.policy_check.checked) {
		 alert("You must read and agree with the Refund Policy.");
		 return false;
	 }
	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("(")
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidatePhone(Phone){
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }
