function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
		return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
		return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

function isnumber(arg){	
	var oldstring = arg; 
	var newstring = parseFloat(arg).toString();
		
	if(oldstring.length == newstring.length && newstring != "NaN")
	{
		return 1;
	}
	else
	{
		return 0;
	}	
}

//Sets the current address to the permanent address
function set(){
	if(document.app.padd.checked==true)
	{
		document.app.cstreet.value=document.app.pstreet.value;      				
		document.app.capt.value=document.app.papt.value;    				
		document.app.ccity.value=document.app.pcity.value;					
		document.app.cstate.value=document.app.pstate.value;					
		document.app.czip.value=document.app.pzip.value;    				
		document.app.cctry.value=document.app.pctry.value;					
		document.app.cphone.value=document.app.pphone.value;					
	}
	else
	{
		document.app.cstreet.value="";      				
		document.app.capt.value="";    				
		document.app.ccity.value="";					
		document.app.cstate.value="";					
		document.app.czip.value="";    				
		document.app.cctry.value="";					
		document.app.cphone.value="";	
	}
}
		
function enable_citizen(){			
	if(document.app.citizen.value=="No")
	{
		document.app.per_resident.disabled=false;
		//enable_per_resident(); //Removing the Alien Registration Number
	}
	else
	{
		document.app.per_resident.disabled=true;
		//document.app.alien_no.disabled=true; //Removing the Alien Registration Number
	}
}
/*function enable_per_resident(){
	if(document.app.per_resident.value=="Yes")
		document.app.alien_no.disabled=false;
	if(document.app.per_resident.value=="No")
		document.app.alien_no.disabled=true;
}*/   //Removing the Alien Registration Number

// Changes the page to the selected school's UGR application
function change_page(arg){
	switch(arg){
		case 0:
		   	window.location="?school=TAMU";
		   	break;
		case 1:
		   	window.location="?school=TAMUCC";
		  	break;
		case 2:
		   	window.location="?school=PVAMU";
		  	break;
	}
}	
		
function tamu_validation(){
		
	var check = 0; //Used for other required fields
	var check2 = 0; //Used for UIN checks
	
	if(
		trim(document.app.uin.value)=='' || trim(document.app.fname.value)=='' || trim(document.app.lname.value)=='' || 
		trim(document.app.bmonth.value)=='' || trim(document.app.bday.value)=='' || trim(document.app.byear.value)=='' || trim(document.app.gender.value)=='' || trim(document.app.race.value)=='' || trim(document.app.pstreet.value)=='' || trim(document.app.pcity.value)=='' || trim(document.app.pstate.value)=='' || trim(document.app.pzip.value)=='' || trim(document.app.pctry.value)=='' || trim(document.app.cstreet.value)=='' || trim(document.app.ccity.value)=='' || trim(document.app.cstate.value)=='' || trim(document.app.czip.value)=='' || trim(document.app.cctry.value)=='' || 
		trim(document.app.cell.value)=='' || trim(document.app.email.value)=='' || trim(document.app.pers_email.value)=='' || 
		trim(document.app.gpa.value)=='' || trim(document.app.major.value)=='' || trim(document.app.mentor.value)=='' || trim(document.app.pers_stat.value)=='' || trim(document.app.agree.value)==''
	)
	{
		check = check + 1;
	}		
	/*if(document.app.per_resident.value == "Yes" && document.app.citizen.value == "No"){
		if(trim(document.app.alien_no.value) == ''){
			check = check + 1;
		}
	}*/ //Removing the Alien Registration Number
	if(isnumber(document.app.uin.value) == 0){				
		check2 = check2 + 1;
	}
	
	if(check2 != 0){
		alert("The UIN that you entered is not valid.");
	}
	else if(check != 0){
		alert("You must fill out all required fields before proceeding.");
	}			
	else{
		check_conf();						
	}
	
}		
		
function tamucc_validation(){

	var check = 0;
	
	if(
		trim(document.app.uin.value)=='' || trim(document.app.fname.value)=='' || trim(document.app.lname.value)=='' || 
		trim(document.app.bmonth.value)=='' || trim(document.app.bday.value)=='' || trim(document.app.byear.value)=='' || trim(document.app.gender.value)=='' || trim(document.app.race.value)=='' || trim(document.app.citizen.value)=='' || 
		trim(document.app.pstreet.value)=='' || trim(document.app.pcity.value)=='' || trim(document.app.pstate.value)=='' || trim(document.app.pzip.value)=='' || trim(document.app.pctry.value)=='' || 
		trim(document.app.cstreet.value)=='' || trim(document.app.ccity.value)=='' || trim(document.app.cstate.value)=='' || trim(document.app.czip.value)=='' || trim(document.app.cctry.value)=='' || 
		trim(document.app.cell.value)=='' || trim(document.app.email.value)=='' || 
		trim(document.app.gpa.value)=='' || trim(document.app.major.value)=='' || trim(document.app.mentor.value)=='' || trim(document.app.agree.value)==''
	)
	{
		check = check + 1;
	}	
		
	if(check != 0){
		alert("You must fill out all required fields before proceeding.");
	}			
	else{
		check_conf();						
	}
	
}


function pvamu_validation(){

	var check = 0;
	//var check2 = 0;
	
	if(
		trim(document.app.uin.value)=='' || trim(document.app.fname.value)=='' || trim(document.app.lname.value)=='' || 
		trim(document.app.bmonth.value)=='' || trim(document.app.bday.value)=='' || trim(document.app.byear.value)=='' || trim(document.app.gender.value)=='' || trim(document.app.race.value)=='' || trim(document.app.citizen.value)=='' || 
		trim(document.app.pstreet.value)=='' || trim(document.app.pcity.value)=='' || trim(document.app.pstate.value)=='' || trim(document.app.pzip.value)=='' || trim(document.app.pctry.value)=='' || 
		trim(document.app.cstreet.value)=='' || trim(document.app.ccity.value)=='' || trim(document.app.cstate.value)=='' || trim(document.app.czip.value)=='' || trim(document.app.cctry.value)=='' || 
		trim(document.app.cell.value)=='' || trim(document.app.email.value)=='' || 
		trim(document.app.gpa.value)=='' || trim(document.app.major.value)=='' || trim(document.app.mentor.value)=='' || trim(document.app.agree.value)==''
	)
	{
		check = check + 1;
	}	
	
	//if(isnumber(document.app.uin.value) == 0){				
		//check2 = check2 + 1;
	//}
	
	//if(check2 != 0){
		//alert("The UIN that you entered is not valid.");
	//}else 
	if(check != 0){
		alert("You must fill out all required fields before proceeding.");
	}			
	else{
		check_conf();	
	}
	
}

// Checks if applicant has agreed to the terms and conditions
 function check_conf(){
	var conf = confirm("Proceed with submission?");
	 if(conf == true){				
	//	if(document.app.agree.checked==true){ 
			document.app.submit();
	//	}
	//	else{
	//		alert("You must agree to the terms and conditions before submitting your application.");
		}							
	//}						
} 
