//JavaScript Functions to validate all the system forms

function validateLanguage(){
	
	var languageInfo = document.getElementById("opts3").value;
	if(languageInfo.indexOf("|1-") == -1){
		alert("You must enter language information (English is a required language)");
		return false;
	}
	return true;
	
}

function checkLogIn(theForm){

	  if (!theForm.user.value)
	  { 
	   	 alert ("You must type in your username");
	     theForm.user.focus()
	      return false;
	  }
      
	 
	  if (!theForm.pass.value)
	  {
	  	 alert('You must type in your password');
	     theForm.pass.focus();
		 return false;
	  } 
	 
	return true;   

}

function checkRegister(theForm){	  
	 
	  if (!theForm.name.value){
	     alert('You must type in your name');
	     theForm.name.focus();
		 return false;
	  }  
	 
	  if(!theForm.lastname.value){
	  	alert('You must type in your LastName');
		theForm.lastname.focus();
		return false;
	  	
	  }  
		
		
		
	  if(!theForm.lastname2.value){
	  	alert('You must Complete Your LastName');
		theForm.lastname2.focus();
		return false;
	  	
	  } 
	  
	  if(!theForm.mail.value){
	  	alert('You must type in your E-mail');
		theForm.mail.focus();
		return false;
	  	
	  }  
	  
	  apos=theForm.mail.value.indexOf("@")
	  dotpos=theForm.mail.value.lastIndexOf(".")
	  if (apos<1||dotpos-apos<2){ 
		 alert("You must type a valid E-mail address");
		 theForm.mail.focus();
		 return false;
			
	  }
	 
	  
	  if(!theForm.pass.value){
	  	alert('You must type in your Password');
		theForm.pass.focus();
		return false;
	  	
	  } 
	   
	  if(!theForm.pass2.value){
	  	alert('You must confirm your Password');
		theForm.pass2.focus();
		return false;
	  	
	  } 
	  
	  if(!(theForm.pass.value == theForm.pass2.value)){
	  	alert('The Pasword fields doesn\'t match');
		theForm.pass.focus();
	  	return false;
	  
	  }
	        
    return true;	  
  }
  
  //Personal Data Validation
  function pData(theForm){		 
	  
		if(!(theForm.postNumber.value)||(!(theForm.area.value))){
	  	alert('You need to type in your Post Number and Area');
			theForm.postNumber.focus();
	  	return false; 
	  }
		
		
		
	   if(!(theForm.name.value)){
	  	alert('You need to type in your Name');
		theForm.name.focus();
	  	return false; 
	  }
	   
	   if(!(theForm.lastname.value)){
	  	alert('You need to type in your Last name');
		theForm.lastname.focus();
	  	return false; 
	  }   
		 
		if(!(theForm.lastname2.value)){
	  	alert('You need to Complete Your LastName');
		theForm.lastname2.focus();
	  	return false; 
	  }    
	  
	   if(!(theForm.address.value)){
	  	alert('You need to type in your address');
		theForm.address.focus();
	  	return false; 
	  }    
	    
	   if(!(theForm.city.value)){
	  	alert('You need to type in your city');
		theForm.city.focus();
	  	return false; 
	  }   
	    if(!theForm.mail.value){
	  	alert('You need to type in your E-mail');
		theForm.mail.focus();
		return false;
	  	
	  }  
	  if(!theForm.state.value){
	  	alert('You need to select a State');
		theForm.state.focus();
		return false;
	  	
	  }  
	   
	 if(!theForm.rfc.value){
	  	alert('You must type your RFC');
		theForm.lastname.focus();
		return false;
	  	
	  } 
		
		
	  apos=theForm.mail.value.indexOf("@")
	  dotpos=theForm.mail.value.lastIndexOf(".")
	  if (apos<1||dotpos-apos<2){ 
		 alert("You must type a valid E-mail address");
		 theForm.mail.focus();
		 return false;
			
	  }
	   	
    return true;	  
  }
  
  //Aacademic Data Validation
  function acaData(theForm){	  
  		if(!theForm.college.value){
	  	alert('You need to select an Institution or \"None\" if does\'nt apply');
		theForm.college.focus();
		return false;
	  	
	  }  
	  if(!theForm.degree.value){
	  	alert('You need to select a PostGraduate Degree or \"None\" if does\'nt apply');
		theForm.degree.focus();
		return false;
	  	
	  }  
	  
	  return true;
  }
  
  //Validation of TextArea <=300 Characters Input
  function update() {
   var old = document.pdata.count1.value;
   var old1 = document.pdata.count2.value;
   var old2 = document.pdata.count3.value;
   
   
   document.pdata.count1.value = document.pdata.desc1.value.length;
   document.pdata.count2.value = document.pdata.desc2.value.length;
   document.pdata.count3.value = document.pdata.desc3.value.length;
  
  
   if(document.pdata.count1.value > 300 && old <= 300) {
     alert('Too much data in Job Description ! MAX LENGTH = 300');
     if(document.styleSheets) {
      document.pdata.count1.style.fontWeight = 'bold';
      document.pdata.count1.style.color = '#ff0000'; 
	 } 
   }
   else if(document.pdata.count1.value <= 300 && old > 300 && document.styleSheets ) {
       document.pdata.count1.style.fontWeight = 'normal';
       document.pdata.count1.style.color = '#000000'; 
	   } 
	   
	if(document.pdata.count2.value > 300 && old1 <= 300) {
     alert('Too much data in Job Description ! MAX LENGTH = 300');
     if(document.styleSheets) {
      document.pdata.count2.style.fontWeight = 'bold';
      document.pdata.count2.style.color = '#ff0000'; 
	 } 
   }
   else if(document.pdata.count2.value <= 300 && old1 > 300 && document.styleSheets ) {
       document.pdata.count2.style.fontWeight = 'normal';
       document.pdata.count2.style.color = '#000000'; 
	   }    
   
   if(document.pdata.count3.value > 300 && old2 <= 300) {
     alert('Too much data in Job Description ! MAX LENGTH = 300');
     if(document.styleSheets) {
      document.pdata.count3.style.fontWeight = 'bold';
      document.pdata.count3.style.color = '#ff0000'; 
	 } 
   }
   else if(document.pdata.count3.value <= 300 && old2 > 300 && document.styleSheets ) {
       document.pdata.count3.style.fontWeight = 'normal';
       document.pdata.count3.style.color = '#000000'; 
	   } 
	  
 }
   function update2(){
      var old3 = document.relinfo.count4.value;
      document.relinfo.count4.value = document.relinfo.rinfo.value.length;
   
   	  if(document.relinfo.count4.value > 200 && old3 <= 200) {
         alert('Too much data in Additional Info ! MAX LENGTH = 200');
         if(document.styleSheets) {
         document.relinfo.count4.style.fontWeight = 'bold';
         document.relinfo.count4.style.color = '#ff0000'; 
	     } 
      }
      else if(document.relinfo.count4.value <= 200 && old3 > 200 && document.styleSheets ) {
       document.relinfo.count4.style.fontWeight = 'normal';
       document.relinfo.count4.style.color = '#000000'; 
	  } 
   }
  
   function update3(counter,obj2){
      var obj = document.getElementById(counter);
	  var old = obj.value;
	 
	  obj.value = obj2.value.length;
	  
	  if(obj.value > 200 && old <= 200){
	     alert('Too much data in Comment Info ! MAX LENGTH = 200');
	      if(document.styleSheets) {
        	 obj.style.fontWeight = 'bold';
        	 obj.style.color = '#ff0000'; 
	     } 
	  }
       else if(obj.value <= 200 && old > 200 && document.styleSheets ) {
      		obj.style.fontWeight = 'normal';
            obj.style.color = '#000000'; 
	  } 
   }
   
   //Professional Data Validation
   function  profData(theForm){
   	  if(!theForm.jobexp.value){
	  	alert('You need to Enter your Job Experience, if less than a Year put "0"');
		theForm.jobexp.focus();
		return false;
	  }  
      if(theForm.desc1.value.length > 300){
	  	alert('Job description 1 needs to be less than 300 characters');
		theForm.desc1.focus();
		return false;
	  }  
	  if(theForm.desc2.value.length > 300){
	  	alert('Job description 2 needs to be less than 300 characters');
		theForm.desc2.focus();
		return false;
	  }  
	  if(theForm.desc3.value.length > 300){
	  	alert('Job description 3 needs to be less than 300 characters');
		theForm.desc3.focus();
		return false;
	  }  
	  
	   if(Number(theForm.year2.value) < Number(theForm.year1.value )){
	  	alert('Finish year must be greater then begin year');
		theForm.year2.focus();
		return false;
	  }  
	  
	   if(Number(theForm.year2.value) == Number(theForm.year1.value) ){
	  	if(Number(theForm.month2.value) < Number(theForm.month1.value)){
			alert('Finish month must be after begin month');
			theForm.month2.focus();
			return false;
	    }
	  }  
	  
	  
	    if(Number(theForm.year4.value) < Number(theForm.year3.value )){
	  	alert('Finish year Previous Job 1 must be greater then begin year');
		theForm.year4.focus();
		return false;
	  }  
	  
	   if(Number(theForm.year4.value) == Number(theForm.year3.value) ){
	  	if(Number(theForm.month4.value) < Number(theForm.month3.value)){
			alert('Finish month Previous Job 1 must be after begin month');
			theForm.month4.focus();
			return false;
	    }
	  }  
	  
	  
   	  return true;
   }
   
   
   function relevantData(theForm){
       
   		if(theForm.rinfo.value.length > 200){
	  	alert('Additional Info needs to be less than 200 characters');
		theForm.rinfo.focus();
		return false;
	   }  
      return true;
   }
   
   //Toggle Function to display or not Previous Jobs

	function toggle(id){
		obj=document.getElementById(id);
		if(obj.style.display == "none")		
		   	obj.style.display="";
		else
			obj.style.display="none";
			
	}

//Functions to ADD / REMOVE elements of Knowledge & Skills  AND Specific Experience Areas List
	
	var selList;
	var avaList;
	var selLang;
	var avaLang;
	var levelLang;
	var levelList;
	var selList2;
	var avaList2;
	var levelList2;
	var experience;
	var childCount;
	var childCount2;
	var childCount3;
	var selected;
	
	function createListObj(){
		//For Skills List
		avaList = document.getElementById("tools");
		selList = document.getElementById("selectedOptions");
		levelList = document.getElementById("level");
	    experience = document.getElementById("exp");
		
		
		//For Languages List 
		 avaLang = document.getElementById("languages");
	     selLang = document.getElementById("sellanguages");
		 levelLang = document.getElementById("langlevel");
	   
		childCount = selList.length;
		childCount3 = selLang.length;
	}
 
 	function createListObj2(){
		//For specific experience Areas
		avaList2 = document.getElementById("areas");
		selList2 = document.getElementById("selectedAreas");
		levelList2 = document.getElementById("level");
		childCount2 = selList2.length;
	}
	
	function selectNone(list1,list2){
			list1.selectedIndex = -1;
			list2.selectedIndex = -1;
			
			addIndex = -1;
			addIndex2 = -1;
			addIndex3 = -1;
			addIndex4 = -1;
			addIndex5 = -1;
			selIndex = -1;
			selIndex2 = -1;
			selIndex3 = -1;
			
	}
	function setSize(list2){
		list2.size = getSize(list2);	
	}
	function getSize(list){
		var len = list.childNodes.length;
		
		var nsLen =0;
		
		for(i=0;i<len;i++){
			if(list.childNodes.item(i).nodeType==1)
				nsLen++;
		}
		if(nsLen < 2)
		 	return 2;
		else
			return nsLen;
	}
	
	function showSelected(){
		var optionList = document.getElementById("selectedOptions").options;
		var data = '';
		var len = optionList.length;
		for(i=0; i< len; i++){
			if(i>0)
				data += ',';
			data += optionList.item(i).value;
		}
		alert(data);
	}
	
	function delObj(){
		var selIndex = selList.selectedIndex;
	
		if(selIndex < 0)
		    return 
	 	var values = selList.options.item(selIndex).innerHTML.split('/');
		selList.options.item(selIndex).innerHTML = values[0];
		avaList.appendChild(selList.options.item(selIndex));
		selectNone(selList,avaList);
		setSize(selList);
		childCount--;
		
		
	}
	function delObj2(){
		var selIndex2 = selList2.selectedIndex;
	
		if(selIndex2 < 0)
		    return 
	 	var values2 = selList2.options.item(selIndex2).innerHTML.split('/');
		selList2.options.item(selIndex2).innerHTML = values2[0];
		avaList2.appendChild(selList2.options.item(selIndex2));
		selectNone(selList2,avaList2);
		setSize(selList2);
		childCount2--;
		
		
	}
	function delObj3(){
		var selIndex3 = selLang.selectedIndex;
	
		if(selIndex3 < 0)
		    return 
	 	var values3 = selLang.options.item(selIndex3).innerHTML.split('/');
		selLang.options.item(selIndex3).innerHTML = values3[0];
		avaLang.appendChild(selLang.options.item(selIndex3));
		selectNone(selLang,avaLang);
		setSize(selLang);
		childCount3--;
		
		
	}
	
	function addObj(){
		var addIndex = avaList.selectedIndex;
		var addIndex2 = levelList.selectedIndex;
		
		if(addIndex < 0)
			 return 
		selList.appendChild(avaList.options.item(addIndex));
		selList.options.item(childCount).innerHTML += " / " + levelList.options.item(addIndex2).innerHTML + " / " + experience.value;
		selList.options[childCount].value += "-" + levelList.options[addIndex2].value + "-" + experience.value;
		
		childCount++;
		selectNone(selList,avaList);
		setSize(selList);
		
	   
	}
	
	function addObj2(){
		var addIndex2 = avaList2.selectedIndex;
		var addIndex3 = levelList2.selectedIndex;
		
		if(addIndex2 < 0)
			 return 
		selList2.appendChild(avaList2.options.item(addIndex2));
		selList2.options.item(childCount2).innerHTML += " / " + levelList2.options.item(addIndex3).innerHTML;
		selList2.options[childCount2].value += "-" + levelList2.options[addIndex3].value;
		
		childCount2++;
		selectNone(selList2,avaList2);
		setSize(selList2);
	}
	
	function addObj3(){
		var addIndex4 = avaLang.selectedIndex;
		var addIndex5 = levelLang.selectedIndex;
		
		if(addIndex4 < 0)
			 return 
		selLang.appendChild(avaLang.options.item(addIndex4));
		selLang.options.item(childCount3).innerHTML += " / " + levelLang.options.item(addIndex5).innerHTML;
		selLang.options[childCount3].value += "-" + levelLang.options[addIndex5].value;
		
		childCount3++;
		selectNone(selLang,avaLang);
		setSize(selLang);
		
	   
	}
	
	//Function that adds to a hidden field all the selected tools for insert into DB
	function getList(){
		var sel_opts = document.getElementById("opts");
		sel_opts.value = "|";
		for(i=0;i<selList.length;i++){	
			    sel_opts.value += selList.options[i].value + "|";
		}  
		
		return true;
	}
	
	function getList2(){
		var sel_opts = document.getElementById("opts2");
		sel_opts.value += "|";
		for(i=0;i<selList2.length;i++){	
			    sel_opts.value += selList2.options[i].value + "|";
		}  
		
		return true;
	}
	
	function getList3(){
		var sel_opts = document.getElementById("opts3");
		sel_opts.value = "|";
		for(i=0;i<selLang.length;i++){	
			    sel_opts.value += selLang.options[i].value + "|";
		}  
		
		return true;
	}
	
	
//Validation for New Users Adding
	function checkAddUser(theForm){	
	
		if(!theForm.name.value){
	  	alert('You must type in your Name');
		theForm.name.focus();
		return false;
	  	
	    } 	   
	   
	    if(!theForm.userName.value){
	  	alert('You must type in your UserName');
		theForm.userName.focus();
		return false;
	  	
	    } 
		
		if(!theForm.userPassword.value){
	  	alert('You must type in your Password');
		theForm.userPassword.focus();
		return false;
	  	
	    } 
	   
	   if(!theForm.passconf.value){
	  	alert('You must confirm your Password');
		theForm.passconf.focus();
		return false;
	  	
	   } 
	  
	   if(!(theForm.userPassword.value == theForm.passconf.value)){
	  	alert('The Pasword fields doesn\'t match');
		theForm.userPassword.focus();
	  	return false;
	  
	   }
	   
	  return true;
	}
	
//Function to display or not the EditUsers Password
	function togglepass(id){
		obj=document.getElementById(id);
		if(obj.style.display == "none")		
		   	obj.style.display="";
		else
			obj.style.display="none";
			
	}
	
//Function to check Edit User Values
   
    function checkeditUser(theForm){   
		
		if(!theForm.name.value){
	  	alert('You must type in your Name');
		theForm.name.focus();
		return false;
	  	
	    } 	   
	   
	    if(!theForm.userName.value){
	  	alert('You must type in your UserName');
		theForm.userName.focus();
		return false;
	  	
	    } 
		
		if(!theForm.userPassword.value && !theForm.passconf.value){
	  	
	  		return true;
	    } 
		
	   
	   if(theForm.userPassword.value && !theForm.passconf.value){
	  	alert('You must confirm the Password');
		theForm.passconf.focus();
		
		return false;
	  	
	   } 
	  
	   if(!(theForm.userPassword.value == theForm.passconf.value)){
	  	alert('The Pasword fields doesn\'t match');
		theForm.userPassword.focus();
		
	  	return false;
	  
	   }
	   
	  return true;
	}
	
	//Function to disable or not Post Gracuate Degree Area Description
	
	function checkDegree(){
	  var desc = document.getElementById('degreeDesc');
      var combo =document.getElementById('degree');
	  var value=combo.options[combo.selectedIndex].value;
	  if(value == '4' || value == '' ){
	  	desc.disabled="True";
		desc.value="";
		//alert(combo.options[combo.selectedIndex].value);
		
	  }
	  else{
		//alert(combo.options[combo.selectedIndex].value);	
		desc.disabled="";	
		
	  }
	}
	
	//function to Display or not the Add CV to Folder Option
	
	function toggleFolders(id){
	   obj=document.getElementById(id);
		if(obj.style.display == "none")		
		   	obj.style.display="";
		else
			obj.style.display="none";
	
	}
	
	//function to validate language level in Cv search
	
	function checkLangLevel(theForm){
	   if(theForm.language.value != "" && theForm.langlevel.value == "" ){
	      alert("You Need to Select Language Level");
		  theForm.langlevel.focus()
		  return false;
	   }
	   else
	   	  return true;
	}
	
	function checkPasswordChange(theform){
	
		if(!theform.userPassword.value){
	  		alert("You must type in your Current Password");
			theform.userPassword.focus();
			return false;
	    } 	   
	   
		if(!theform.newPassword.value){
	  		alert("You must type in your New Password");
			theform.newPassword.focus();
	  		return false;
	    }
			    
	    if(theform.newPassword.value != theform.passwordConfirmation.value){
		  	alert('The Pasword fields doesn\'t match');
			theform.newPassword.focus();
		 	return false;
	   	}
	
		return true;
	}
	/*////////////////////////////////////////////////////////////////////////////
	// Confirm Closure
	// This function ask the user if he/she is sure to close this post
	////////////////////////////////////////////////////////////////////////////*/
	
  function confirmClose(theForm){
			 
			if(theForm.previous.value=="open" && theForm.update.value=="Update"){
					
      		if(theForm.status.value=="closed" && theForm.hired.value=="0"){
        	  if(confirm('Are you sure you want to close this post? Currently there is no hired candidate. This action will send a closure mail to all applicants.')){
        				return true;
						}
						else{
								return false;
						}
    	  	}
    			if(theForm.status.value=="closed" && theForm.hired.value=="1"){
        	  if(confirm('Are you sure you want to close this post? This action will send a closure mail to not selected applicants.')){
        				return true;
						}
						else{
								return false;
						}
    	  	}
					
					if(theForm.status.value=="open"){
        	  return true;
    	  	}
			}
			else{
					 return true;
			}  
	  return false;
  }
	
	/*////////////////////////////////////////////////////////////////////////////
	// Check Post
	// This function checks that all required fields to register a new Post are
	// fullfilled
	////////////////////////////////////////////////////////////////////////////*/
	
function checkPost(theForm){
		
	//	alert (theForm.cancel.value);
		/*
		if(theForm.cancel.value == "Cancel"){
		 theForm.postNumber.value = "";
		 theForm.title.value = "";
		 theForm.description.value = "";
		 theForm.area.value = "";
		 theForm.status.value = "";
		 theForm.link.value = "";
    
		 return false;
    }
		*/
		
		if(theForm.register){
    		if(theForm.register.value == "Register"){
    			 
    		   if (!theForm.postNumber.value){
    	       alert('You must type in a post number');
    	       theForm.postNumber.focus();
    		     return false;
    	     }  
      		 if (!theForm.title.value){
      	       alert('You must type in a position title');
      	       theForm.title.focus();
      		    return false;
      	     }  
      		 if (!theForm.description.value){
      	       alert('You must type in a post description');
      	       theForm.description.focus();
      		    return false;
      	     }
      		 if (!theForm.area.value){
      	       alert('You must select an area');
      	       theForm.area.focus();
      		    return false;
      	     }
      		 if (!theForm.status.value){
      	       alert('You must select an status');
      	       theForm.status.focus();
      		    return false;
      	     } 
      		 if (!theForm.link.value){
      	       alert('You must select a file to upload');
      	       theForm.link.focus();
      		    return false;
    	     }
    			 
    			 return true;
    		}
		}
		
		if(theForm.update){
    		if(theForm.update.value == "Update"){
    			     
  					 if (!theForm.postNumber.value){
      	       alert('You must type in a post number');
      	       theForm.postNumber.focus();
      		     return false;
      	     }  
        		 if (!theForm.title.value){
        	       alert('You must type in a position title');
        	       theForm.title.focus();
        		    return false;
        	     }  
        		 if (!theForm.description.value){
        	       alert('You must type in a post description');
        	       theForm.description.focus();
        		    return false;
        	     }
        		 if (!theForm.area.value){
        	       alert('You must select an area');
        	       theForm.area.focus();
        		    return false;
        	     }
        		 if (!theForm.status.value){
        	       alert('You must select an status');
        	       theForm.status.focus();
        		    return false;
      	     }
  				 return true;		 						
    			}
			}
			
			
			return false;
			  
		  
}