
//------------------------------------------------//

	String.prototype.trim = function() {
	
	 // skip leading and trailing whitespace
	 // and return everything in between
	  var x=this;
	  x=x.replace(/^\s*(.*)/, "$1");
	  x=x.replace(/(.*?)\s*$/, "$1");
	  return x;
	}

//------------------------------------------------//

function Right(str, n)
	/***
		IN: str - the string we are RIGHTing
			n - the number of characters we want to return
	
		RETVAL: n characters from the right side of the string
	***/
	{
		if (n <= 0)     // Invalid bound, return blank string
			return "";
		else if (n > String(str).length)   // Invalid bound, return
			return str;                     // entire string
		else { // Valid bound, return appropriate substring
			var iLen = String(str).length;
			return String(str).substring(iLen, iLen - n);
		}
	}

//------------------------------------------------//
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;
}
//------------------------------------------------//

/*var chkDot = true;
var usEmail = true;

function validEmail(eAddr, whom) 
{ 
      var lenSuffix = (usEmail) ? 4 : 3;
      var result = false;
      var ndxAt = ndxDot =  0;
     
	  if(eAddr == "") 
	  {
	  	alert(whom + " Email is required");
	  	return false;
	  }
	      
      ndxAt  = eAddr.indexOf("@");
      ndxDot = eAddr.indexOf(".") ;
      ndxDot2 = eAddr.lastIndexOf(".") ;
          
      if ((ndxDot < 0) || (ndxAt < 0))
         alert(whom + " email address lacks '.' or '@'.\n\nThe format is 'you@dom.suf'"); 
      else if (chkDot && (ndxDot < ndxAt) )
         chkDot = !( confirm("You entered a 'dot' before the '@'\nAre you sure that is right?") );
      else if ( (ndxDot2 - 3) <= ndxAt)
         alert("You may be missing your domain name.\n\nThe format is 'you@dom.suf'");
      else if (eAddr.length < ndxDot2 + lenSuffix) 
         usEmail = !(confirm("You have fewer than 3 characters as a domain suffix.\nAre you sure that is right?") );
      else 
           result=true; 
          
      return result; 
} */
 
// ---------------------------------------- //

function ChkEmail( obj )
{	
	var value = obj.value.trim();
	if (!/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test( value ) )	{
		alert("Please enter a valid Email address");
		obj.focus();
		return false;
	} else
		return true;	
}

/*function ChkEmail( obj, message )
{	
	var value = obj.value.trim();
	if( value == "" || value == message )	{
		alert( message );
		obj.focus();
		return false;
	}
	if (!/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test( value ) )	{
		alert("Please enter a valid Email address");
		obj.focus();
		return false;
	} else
		return true;	
} */

/* ---------------------------------------- */

function PhoneNumberVal( obj ) { 
	var phoneNo = obj.value.trim();

	if (/^\(?\d{3}\)?([ -\/\.])\d{3}([ -\/\.])\d{4}$/.test( phoneNo ) ) {   
		return true; 
	} else {
		alert( "Please enter a phone number in the correct format. Example: 000-000-0000" );
		obj.focus();
		return false; 
	} 
}

/*function PhoneNumberVal( obj, message ) { 
	var phoneNo = obj.value.trim();
	if ( phoneNo == "" || phoneNo == message ) {
		alert( message );
		obj.focus();
		return false;
	}

	if (/^\(?\d{3}\)?([ -\/\.])\d{3}([ -\/\.])\d{4}$/.test( phoneNo ) ) {   
		return true; 
	} else {
		alert( "This is an INVALID phone number! Example: 000-000-0000" );
		obj.focus();
		return false; 
	} 
} */
 

//----------------------------------------------//

function ZipValidate( obj )
{	
	var field = obj.value;
	var valid = "0123456789";
	if (field.length!=5) 
	{
		alert("Please enter your 5 digit zip code.");
		obj.focus();
		return false;
	}
	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
		alert("Invalid characters in your zip code.  Please try again.");
		obj.focus();
		return false;
		}
	}
	return true;
}

/*function ZipValidate( obj, message )
{	
	var field = obj.value;
	var valid = "0123456789-";
	var hyphencount = 0;
	if( field == "" || field == message ) {
		alert("Please enter your 5 digit or 5 digit+4 zip code.");
		obj.focus();
		return false;
	}
	if (field.length!=5 && field.length!=10) 
	{
		alert("Please enter your 5 digit or 5 digit+4 zip code.");
		obj.focus();
		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.");
		obj.focus();
		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.");
			obj.focus();
			return false;
   		}
	}
	return true;
} */

//----------------------------------------//

function CheckCheckBox( obj )
{	
  	if ( obj.checked ) return true;		
	return false;
}
//----------------------------------------//

function CheckRadioButton( obj )
{	
  	for ( counter = 0; counter < obj.length; counter++ )
	{	
		if ( obj[counter].checked ) return true;		
	}	
	return false;
}

//----------------------------------------//

/*function ChkReplyType()
{
  	var radio_choice = false;
	
	
  	for ( counter = 0; counter < document.forms[0].sessionDate.length; counter++ )
	{		alert( document.forms[0].sessionDate[counter].checked );
		if ( document.forms[0].sessionDate[counter].checked ) {
			
			radio_choice = true; 
		}
	}	
	
	return radio_choice;
} */

//----------------------------------------//

function checkInput( obj, msg )
{
	if( obj.value.trim() == "" ) return false;
	if( obj.value.trim() == msg ) return false;
	return true;
}

//----------------------------------------//

function checkValidSSN( obj )
{
	if( !isInteger( obj.value ) ) return false;
	if( obj.value.length > 4 ) return false;
	return true;
}

//----------------------------------------//

function checkValidUniversityID( obj )
{
	if( !isInteger( obj.value ) ) return false;
	if( obj.value.length != 10 ) return false;
	return true;
}

//----------------------------------------//

function checkRadio( obj )
{
  	var radio_choice = false;
	
  	for( c = 0; c < obj.length; c++ )
	{
		if( obj[c].checked )
		{
			radio_choice = true;
		}
	}
	return radio_choice;
}

//----------------------------------------//

function Form( myForm )
{
	
	/*if( myForm.flag.value == "false")
	{
		alert("Please enter your correct University ID number and Birth date.");
		myForm.universityID.focus();
		return false;
	}*/
	
	if( !checkInput( myForm.last4SSN, "Enter the last 4 digits of your SSN" ) )
	{
		alert("Please enter the last 4 digits of your Social Security Number.");
		myForm.last4SSN.focus();
		return false;
	}
	
	if( !checkValidSSN( myForm.last4SSN ) )
	{
		alert("Please enter the last 4 digits of your Social Security Number in the correct format.");
		myForm.last4SSN.focus();
		return false;
	}
		
	if( !checkValidDate( myForm.DOB.value ) )		
	{
		alert("Please enter your Date of Birth. (MM/DD/YYYY)");
		myForm.DOB.focus();	
		return false;
	}
	
	var temp = Right(myForm.DOB.value, 4);
	var cutoffyear = (new Date()).getFullYear() - 16;
	if( parseInt(temp) > cutoffyear )
	{
		alert("Your birth year must be " + cutoffyear + " or before in order to use this form, please update your birth date year or contact Financial Aid for assistance.");
		myForm.DOB.focus();
		return false;
	}
	
	if( checkInput( myForm.universityID, "Enter your University ID" ) && !checkValidUniversityID( myForm.universityID ) )
	{
		alert("Please enter your 10-digit University ID Number.");
		myForm.universityID.focus();
		return false;
	}
	
	if( checkInput( myForm.zip, "Enter your Zip Code" ) && !ZipValidate( myForm.zip ) )
	{
		return false;
	}
	
	if( checkInput( myForm.email, "Enter your Email" ) && !ChkEmail( myForm.email ) )
	{			
		return false;
	}
	
	if( checkInput( myForm.phone1, "Enter your Phone Number" ) && !PhoneNumberVal( myForm.phone1 ) )
	{				
		return false;
	}
	
	if( checkInput( myForm.hsYearGrad, "Enter your Graduation Year" ) && !isInteger( myForm.hsYearGrad.value ) )
	{
		alert("Please enter your High School Graduation Year in the correct format.");
		myForm.hsYearGrad.focus();
		return false;
	}
	
	if( checkInput( myForm.hsZip, "Enter Zip Code" ) && !ZipValidate( myForm.hsZip ) )
	{
		return false;
	}
	
	if( checkInput( myForm.hsPhone, "Enter Phone Number" ) && !PhoneNumberVal( myForm.hsPhone ) )
	{
		return false;
	}
	
	/*if( myForm.essay.value.length > 5000 )
	{
		alert("Your essay is " + myForm.essay.value.length + " characters long.  Please limit it to 5000 characters.");
		myForm.essay.focus();
		return false;
	}*/
	
	// No more validation necessary if the user is only saving the info.
	if( myForm.saveForm.value == "yes" )
	{
		return true;
	}
	
	if( !checkRadio( myForm.studentType ) )
	{
		alert("Please select a Student Admittance Type.");
		myForm.studentType[0].focus();
		return false;
	}
	
	if( !checkInput(myForm.FName,"Enter your First Name") )
	{
		alert("Please enter your first name.");
		myForm.FName.focus();
		return false;
	}	
	
	if( !checkInput(myForm.MName,"Enter your Middle Initial") )
	{
		alert("Please enter your middle initial.");
		myForm.Mname.focus();
		return false;
	}		
	
	if(!checkInput(myForm.LName,"Enter your Last Name" ) )	
	{
		alert("Please enter your last name.");
		myForm.LName.focus();
		return false;
	}
	
	if(!CheckSelect(myForm.gender))
	{
		alert("Please enter your Gender.");
		myForm.gender.focus();
		return false;		
	}
	
	if( !checkInput(myForm.address1,"Enter your Address" ) )		
	{
		alert("Please enter your Address.");
		myForm.address1.focus();
		return false;
	}
	
	if( !checkInput( myForm.city, "Enter your City" ) )
	{
		alert("Please enter your City");
		myForm.city.focus();
		return false;
	}
	
	if( !checkInput( myForm.zip ) )
	{
		alert("Please enter your 5 digit zip code.");
		myForm.zip.focus();
		return false;
	}
	
	if( !checkInput( myForm.county,"Enter your County" ) )		
	{
		alert("Please enter your County.");
		myForm.county.focus();
		return false;
	}
	
	if( !checkInput( myForm.email, "Enter your Email" ) )
	{
		alert( "Please enter your Email." );
		myForm.email.focus();
		return false;
	}
	
	if ( !checkInput( myForm.phone1, "Enter your Phone Number" ) ) {
		alert( "Please enter your Phone Number." );
		myForm.phone1.focus();
		return false;
	}
	
	/*if(!checkInput(myForm.hsName,"Enter name of High School" ) )	
	{
		alert("Please enter the name of the High School attained last.");
		myForm.hsName.focus();
		return false;
	}	
	if(!checkinteger(myForm.hsYearGrad,"Enter Graduation year" ) )	
	{
		alert("Please enter the year you graduationed.");
		myForm.hsYearGrad.focus();
		return false;
	}	
	if(!checkInput(myForm.hsAddress1,"Enter Address" ) )	
	{
		alert("Please enter the address of the High School attained last.");
		myForm.hsAddress1.focus();
		return false;
	}	
		
	if(!checkInput(myForm.hsCity,"Enter City" ) )	
	{
		alert("Please enter the city where your High School is located.");
		myForm.hsCity.focus();
		return false;
	}	
	
	if(!checkInput(myForm.hsZip,"Enter Zip Code" ) )	
	{
		alert("Please enter the zip code of the High School attained last.");
		myForm.hsZip.focus();
		return false;
	}*/
	
	//myForm.js.value = 'YES'; 	

	return true;
}
	

/* ---------------------------------------- */

function Form2( myForm )
{
	
	if( !checkInput( myForm.last4SSN, "Enter the last 4 digits of your SSN" ) )
	{
		alert("Please enter the last 4 digits of your Social Security Number.");
		myForm.last4SSN.focus();
		return false;
	}
	
	if( !checkValidSSN( myForm.last4SSN ) )
	{
		alert("Please enter the last 4 digits of your Social Security Number in the correct format.");
		myForm.last4SSN.focus();
		return false;
	}
		
	if( !checkValidDate( myForm.DOB.value ) )		
	{
		alert("Please enter your Date of Birth. (MM/DD/YYYY)");
		myForm.DOB.focus();	
		return false;
	}
	
	var temp = Right(myForm.DOB.value, 4);
	if( parseInt(temp) > 1992 )
	{
		alert("Your birth year must be 1993 or before in order to use this form, please update your birth date year or contact Financial Aid for assistance.");
		myForm.DOB.focus();
		return false;
	}
	
	/*if( checkInput( myForm.REF_EMAIL_1, "" ) && !ChkEmail( myForm.REF_EMAIL_1 ) )
	{			
		return false;
	}
	
	if( checkInput( myForm.REF_EMAIL_2, "" ) && !ChkEmail( myForm.REF_EMAIL_2 ) )
	{			
		return false;
	}
	
	if( checkInput( myForm.REF_EMAIL_3, "" ) && !ChkEmail( myForm.REF_EMAIL_3 ) )
	{			
		return false;
	}*/
	
	if( myForm.essay.value.length > 5000 )
	{
		alert("Your essay is " + myForm.essay.value.length + " characters long.  Please limit it to 5000 characters.");
		myForm.essay.focus();
		return false;
	}
	
	if( !confirm("Are you sure you want to submit this form?") )
		return false;
	
	return true;
}

/* ---------------------------------------- */

function checkinteger( obj )
{   
	var bool = true  
     var regX = /[^0-9\.]/;    
     if(obj.value.match(regX))
     {
          //alert('Sorry, this field accepts numbers only.');
          obj.value="";
          obj.focus();
		  bool = false;
     }
	 return bool;
}


//------------------------------------------------//
function checkValidDate(dateStr) {
    // dateStr must be of format month day year with slashes
    // separating the parts. Some minor changes would have
    // to be made to use day month year or another format.
    // This function returns True if the date is valid.
    var slash1 = dateStr.indexOf("/");
    if (slash1 == -1) { return false; }
    var dateMonth = dateStr.substring(0, slash1)
    var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
    var slash2 = dateMonthAndYear.indexOf("/");
    if (slash2 == -1) { return false; }
    var dateDay = dateMonthAndYear.substring(0, slash2);
    var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
    if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
    // if any non-digits in the month, invalid date
    for (var x=0; x < dateMonth.length; x++) {
        var digit = dateMonth.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text month to a number
    var numMonth = 0;
    for (var x=0; x < dateMonth.length; x++) {
        digit = dateMonth.substring(x, x+1);
        numMonth *= 10;
        numMonth += parseInt(digit);
    }
    if ((numMonth <= 0) || (numMonth > 12)) { return false; }
    // if any non-digits in the day, invalid date
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text day to a number
    var numDay = 0;
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        numDay *= 10;
        numDay += parseInt(digit);
    }
    if ((numDay <= 0) || (numDay > 31)) { return false; }
    // February can't be greater than 29 (leap year calculation comes later)
    if ((numMonth == 2) && (numDay > 29)) { return false; }
    // check for months with only 30 days
    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) { 
        if (numDay > 30) { return false; } 
    }
    // if any non-digits in the year, invalid date
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text year to a number
    var numYear = 0;
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        numYear *= 10;
        numYear += parseInt(digit);
    }
    // Year must be a 2-digit year or a 4-digit year
    if ( dateYear.length != 4 ) { return false; }
    
    if ((numYear <= 0) || (numYear > 9999)) { return false; }
    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
        var div4 = numYear % 4;
        var div100 = numYear % 100;
        var div400 = numYear % 400;
        // if not divisible by 4, then not a leap year so Feb 29 is invalid
        if (div4 != 0) { return false; }
        // at this point, year is divisible by 4. So if year is divisible by
        // 100 and not 400, then it's not a leap year so Feb 29 is invalid
        if ((div100 == 0) && (div400 != 0)) { return false; }
    }
    // date is valid
    return true;
}

/*function checkValidDate(dateStr) {
    // dateStr must be of format month day year with either slashes
    // or dashes separating the parts. Some minor changes would have
    // to be made to use day month year or another format.
    // This function returns True if the date is valid.
    var slash1 = dateStr.indexOf("/");
    if (slash1 == -1) { slash1 = dateStr.indexOf("-"); }
    // if no slashes or dashes, invalid date
    if (slash1 == -1) { return false; }
    var dateMonth = dateStr.substring(0, slash1)
    var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
    var slash2 = dateMonthAndYear.indexOf("/");
    if (slash2 == -1) { slash2 = dateMonthAndYear.indexOf("-"); }
    // if not a second slash or dash, invalid date
    if (slash2 == -1) { return false; }
    var dateDay = dateMonthAndYear.substring(0, slash2);
    var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
    if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
    // if any non-digits in the month, invalid date
    for (var x=0; x < dateMonth.length; x++) {
        var digit = dateMonth.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text month to a number
    var numMonth = 0;
    for (var x=0; x < dateMonth.length; x++) {
        digit = dateMonth.substring(x, x+1);
        numMonth *= 10;
        numMonth += parseInt(digit);
    }
    if ((numMonth <= 0) || (numMonth > 12)) { return false; }
    // if any non-digits in the day, invalid date
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text day to a number
    var numDay = 0;
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        numDay *= 10;
        numDay += parseInt(digit);
    }
    if ((numDay <= 0) || (numDay > 31)) { return false; }
    // February can't be greater than 29 (leap year calculation comes later)
    if ((numMonth == 2) && (numDay > 29)) { return false; }
    // check for months with only 30 days
    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) { 
        if (numDay > 30) { return false; } 
    }
    // if any non-digits in the year, invalid date
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text year to a number
    var numYear = 0;
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        numYear *= 10;
        numYear += parseInt(digit);
    }
    // Year must be a 2-digit year or a 4-digit year
    if ( dateYear.length != 4 ) { return false; }
    
    if ((numYear <= 0) || (numYear > 9999)) { return false; }
    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
        var div4 = numYear % 4;
        var div100 = numYear % 100;
        var div400 = numYear % 400;
        // if not divisible by 4, then not a leap year so Feb 29 is invalid
        if (div4 != 0) { return false; }
        // at this point, year is divisible by 4. So if year is divisible by
        // 100 and not 400, then it's not a leap year so Feb 29 is invalid
        if ((div100 == 0) && (div400 != 0)) { return false; }
    }
    // date is valid
    return true;
} */
// ----------------------------------------//

function CheckSelect( obj )
{
    for (i = 0; i < obj.length; i++)
   	{
		if ((obj.options[i].selected) && (obj.options[i].value.trim() != ""))
		return true;
	}
     	return false;
}