// JavaScript Document  
   //Candidate section Client Site Validation Submit New Resume
var user_valid =/^[a-zA-Z0-9_@.]+$/;
var pass_valid =/^[a-zA-Z0-9]+$/;
var name_exp=/^([A-Za-z]+[A-Za-z. ]+)$/;
var pattern = /[^a-zA-Z0-9'\s&,.\-\(\)]/;
var re_text = /\.doc|\.docx/i;
function trim_ws(str)
{
        temp=str.replace(/^\s*/, "");
        temp=temp.replace(/\s*$/,"");
        return temp;
}
//Description: UserName Validates Function.
function rb_checkUser(user)
{       if (user.search(/^[a-zA-Z0-9_@.]+$/) == -1)
                return false;
        else
                return true;
	}
//Description: Password Validates Function.	
function rb_checkPassword(pass)
{       if (pass.search(/^[a-zA-Z0-9]+$/) == -1)
                return false;
        else
                return true;
}

//Description: Name Validates Function.
function rb_checkName(str)
{       if(!name_exp.test(str))
                return false;
        else
                return true;
}
//Description: Email Validates Function.
function rb_checkEmail(email)
{       if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
                return false;
        else
                return true;
}
//Description: Phone Validates Function.
function rb_checkPhone(phone)
{       if (phone.search(/^[0-9]+[0-9,\ -]*[0-9]$/) == -1)
                return false;
        else
                return true;
}

//Description: Resume Upload Function.
function checkResume(fileext)
{
        if (fileext.search(re_text) == -1)
                return false;
        else
                return true;
}

//Description: Validates disable for Experience fields
function enable()
{	if(document.getElementById('fresh').checked)
	{  //alert("Fresh is enabled");
		document.getElementById('Yearexp').disabled=true;
		document.getElementById('Yearexp').style.background="";
		document.getElementById('Monthexp').disabled=true;
		document.getElementById('Monthexp').style.background="";
		document.getElementById('preffred_indus').style.display='none';
		indusmsg ='Preferred Industry Type<font color=green>*</font>';
		document.getElementById("prefindus").innerHTML=indusmsg;
		funclmsg ='Preferred Functional Area<font color=green>*</font>';
		document.getElementById("prefunarea").innerHTML=funclmsg;
		rolemsg ='Preferred Role<font color=green>*</font>';
		document.getElementById("prefrole").innerHTML=rolemsg;
		document.getElementById('Lac').disabled=true;
		document.getElementById('Lac').style.background="";
		document.getElementById('Thousand').disabled=true;
		document.getElementById('Thousand').style.background="";
	}
	else
	{	//alert("Fresh is disabled");
		document.getElementById('Yearexp').disabled=false;
		document.getElementById('Monthexp').disabled=false;
		document.getElementById('preffred_indus').style.display='';
		indusmsg ='Current Industry Type<font color=green>*</font>';
		document.getElementById("prefindus").innerHTML=indusmsg;
		funclmsg ='Current Functional Area<font color=green>*</font>';
		document.getElementById("prefunarea").innerHTML=funclmsg;
		rolemsg ='Current Role<font color=green>*</font>';
		document.getElementById("prefrole").innerHTML=rolemsg;
		document.getElementById('Lac').disabled=false;
		document.getElementById('Thousand').disabled=false;
		  return true;
	}
}
//Description: End Validates disable Experience fields

function Validatenewresume()
{    	var msg='';
		var user = trim_ws(document.getElementById('username').value);
		var error=0;
		if(user == '')
		{       msg+='Please specify  UserName.<br>';
				document.getElementById('username').style.background="#FFB6B1";
			    error=error+1;
		}
		else if(!rb_checkUser(user))
        {       msg+="'"+user+"' is not a valid UserName.Please specify  valid UserName.<br>";
				document.getElementById('username').style.background="#FFB6B1";
			    error=error+1;
        }
	    else if( (user.length<8) || (user.length>50) )
        {       msg+='Username  length should be between 8 - 50 characters.<br>';
				document.getElementById('username').style.background="#FFB6B1";
			    error=error+1;
        }
		else
		{		document.getElementById('username').style.background =""; 
		}
		//Description: Validates the Password field.	
		var pass=trim_ws(document.getElementById('userpass').value);
		var confirmpass=trim_ws(document.getElementById('compass').value);
        if(pass == '')
        {       msg+='Please mention a password.<br>';
				document.getElementById('userpass').style.background="#FFB6B1";
			    error=error+1;
        }
        else if(!rb_checkPassword(pass))
        {       msg+="'"+pass+"' is not  valid password. Please specify a valid password.<br>";
				document.getElementById('userpass').style.background="#FFB6B1";				
				error=error+1;
        }
	    else if( ( pass.length < 8) || (pass.length > 24) ) 
        {       msg+='Password length should be between 8 - 24 characters.<br>';
				document.getElementById('userpass').style.background="#FFB6B1";
				error=error+1;
        }
		else
		{	document.getElementById('userpass').style.background="";
		}
		//Description: Validates the Confirm Password field.	
		if(confirmpass == '')
        {       msg+='Please mention a  confirm password.<br>';
				document.getElementById('compass').style.background="#FFB6B1";
			    error=error+1;
        }
        else if(!rb_checkPassword(confirmpass))
        {       msg+="'"+confirmpass+"' is not  a valid Password. Please mention a   valid Password.<br>";
				document.getElementById('compass').style.background="#FFB6B1";
				error=error+1;
        }
		else if( (confirmpass.length < 8) || (confirmpass.length > 24) )
        {       msg+='Password length should be between 8 - 24 characters.<br>';
				document.getElementById('compass').style.background="#FFB6B1";
				error=error+1;
        }
		else if(pass != confirmpass)
		{	    msg+='Passwords are not matching.<br>';
				document.getElementById('userpass').style.background="#FFB6B1";
				document.getElementById('compass').style.background="#FFB6B1";
				error=error+1;
		}
		else
		{		document.getElementById('userpass').style.background="";
				document.getElementById('compass').style.background="";
		}
		//Description: Validates the Email ID field.	
		var email_id=trim_ws(document.getElementById('email').value);
		if(email_id == '')
        {       msg+='Please mention an email Id<br>';
				document.getElementById('email').style.background="#FFB6B1";
 				error=error+1;
        }
        else if(!rb_checkEmail(email_id))
        {       msg+="'"+email_id+"' is not a valid email Id. Please mention a  valid email Id<br>";
  			    document.getElementById('email').style.background="#FFB6B1";
                error=error+1;
        }
		else
		{	document.getElementById('email').style.background="";
		}
		//Description: Validates the Date of Birth field.	
		var day=document.newresume.dateofbirthDay.value;
	    var month=document.newresume.dateofbirthMonth.value;
		var year=document.newresume.dateofbirthYear.value;
		if(day == '' && month =='' && year == '')
        {       msg+='Please select  date of birth.<br>';
				document.newresume.dateofbirthDay.style.background="#FFB6B1";
				document.newresume.dateofbirthMonth.style.background="#FFB6B1";
				document.newresume.dateofbirthYear.style.background="#FFB6B1";								
				error=error+1;
        }
		else if(day == '')
		 {	msg+='Please select a Day of Birth.<br>';
			document.newresume.dateofbirthDay.style.background="#FFB6B1";
			error=error+1;
		 }
		else if(month == '')
		 {	msg+='Please select  a Month of Birth.<br>';
			document.newresume.dateofbirthMonth.style.background="#FFB6B1";
			error=error+1;
		 }
		else if(year == '')
		 {	msg+='Please select  a Year of Birth.<br>';
			document.newresume.dateofbirthYear.style.background="#FFB6B1";
			error=error+1;
		 }
		 else
		 {	document.newresume.dateofbirthDay.style.background="";
			document.newresume.dateofbirthMonth.style.background="";
			document.newresume.dateofbirthYear.style.background="";			
		  }
		if(day != '' && month !='' && year != '')
		{	var dateOfBirth = new Date(year,month - 1,day);
			var today       = new Date();
			//Set 1 day in milliseconds
			var one_day     = 1000*60*60*24;
			var diff_days   = Math.ceil((dateOfBirth.getTime()-today.getTime())/(one_day));
			if  ( (day > 0) || ( month > 0) || (year > 0) ) 
			{   jsDay   = dateOfBirth.getDate();
				jsMonth = dateOfBirth.getMonth() + 1;
				jsYear  = dateOfBirth.getFullYear();
				if ( ! (( jsDay == day) && (jsMonth == month) && (jsYear == year)) )
				{ 	msg+='Please choose  valid Date  of Birth.<br>';
					document.newresume.dateofbirthDay.style.background="#FFB6B1";
					document.newresume.dateofbirthMonth.style.background="#FFB6B1";
					document.newresume.dateofbirthYear.style.background="#FFB6B1";	
					error=error+1;
				}
			}
				if ( diff_days > 0 )
				{   msg+='Please specify valid birthdate.<br>'; 
					document.newresume.dateofbirthDay.style.background="#FFB6B1";
					document.newresume.dateofbirthMonth.style.background="#FFB6B1";
					document.newresume.dateofbirthYear.style.background="#FFB6B1";	
					error=error+1;
				}
		}
		//Description: Validates the First Name field.	
		var first_name=trim_ws(document.getElementById('firstname').value);
        if(first_name == '')
        {       msg+='Please fill your first name<br>';
				document.getElementById('firstname').style.background="#FFB6B1";
				error=error+1;
        }
        else if(!rb_checkName(first_name)) 
        {       msg+='Please fill a valid first name<br>';
				document.getElementById('firstname').style.background="#FFB6B1";
	            error=error+1;
        }
		else
		{		document.getElementById('firstname').style.background="";
		}
		//Description: Validates the Last Name field.	
		var last_name=trim_ws(document.getElementById('lastname').value);
        if(last_name == '')
        {       msg+='Please fill your  last name.<br>';
				document.getElementById('lastname').style.background="#FFB6B1";
				error=error+1;
        }
		else if(!rb_checkName(last_name)) 
        {       msg+='Please fill a  valid last name.<br>';
			    document.getElementById('lastname').style.background="#FFB6B1";
                error=error+1;
        }
		else
		{		document.getElementById('lastname').style.background="";
		}
		var gender=document.getElementById('gen').value;
		if(gender == '')
        {	    msg+='Please select your Gender.<br>';
				document.getElementById('gen').style.background="#FFB6B1";
 				error=error+1;
        }
		else
		{		document.getElementById('gen').style.background="";			
		}
		var mtst=document.getElementById('status').value;
		if(mtst == '')
        {	    msg+='Please select your Marital staus.<br>';
				document.getElementById('status').style.background="#FFB6B1";
 				error=error+1;
        }
		else
		{		document.getElementById('status').style.background="";			
		}
		
		//Description: Validates the Current Location field.	
		var curr_loc=document.getElementById('currloc').value;
	    if(curr_loc == '')
        {       msg+='Please select your Current Location.<br>';
				document.getElementById('currloc').style.background="#FFB6B1";
                error=error+1;
        }
		else
		{		document.getElementById('currloc').style.background="";			
		}
		//Description: Validates the Home City field.	
		var homecity=document.getElementById('home_city').value;
	    if(homecity == 'none')
        {       msg+='Please select your  Home City.<br>';
				document.getElementById('home_city').style.background="#FFB6B1";
                error=error+1;
        }
		else
		{		document.getElementById('home_city').style.background="";			
		}
		 //Description: Validates the Contact No and Phone No field.	
	 	var contactno=trim_ws(document.getElementById('contact_no').value);
		var mobileno=trim_ws(document.getElementById('mobile_no').value);
	 	var cotcode=trim_ws(document.getElementById('countrycode').value);
		var stcode=trim_ws(document.getElementById('stdcode').value);
		if(contactno == '' && mobileno == '')
        {       msg+='Please give atleast one, Phone No or Mobile No.<br>';
				document.getElementById('stdcode').style.background="#FFB6B1";
				document.getElementById('contact_no').style.background="#FFB6B1";
				document.getElementById('mobile_no').style.background="#FFB6B1";
                error=error+1;
        }
		else
		{		document.getElementById('stdcode').style.background="";
				document.getElementById('contact_no').style.background="";			
				document.getElementById('mobile_no').style.background="";			
		}
	  if(contactno != '')
        {	if(cotcode == '')
			{		msg+='Please specify a Country Code.<br>';
					document.getElementById('countrycode').style.background="#FFB6B1";
					error=error+1;
			}
			else if(cotcode.length < 2)
			{		msg+='Country code should be atleast 2 digits long.<br>';
					document.getElementById('countrycode').style.background="#FFB6B1";
					error=error+1;
			}
			else
			{		document.getElementById('countrycode').style.background="";
			}
		}
		if(contactno != '')
        {  if(!rb_checkPhone(contactno))
			{		msg+='Please specify a valid Contact No.<br>';
					document.getElementById('contact_no').style.background="#FFB6B1";
					error=error+1;
			}
			else if(contactno.length < 6)
			{		msg+='Contact no  should be atleast 6 digits long.<br>';
					document.getElementById('contact_no').style.background="#FFB6B1";
					error=error+1;
			}
			else if(stcode == '')	
        	{    msg+='Please specify  a STD Code.<br>';
  			     document.getElementById('stdcode').style.background="#FFB6B1";
                 error=error+1;
        	}
			else if(stcode.length < 3)
			{		msg+='STD Code should be  atleast 3 digits long.<br>';
					document.getElementById('stdcode').style.background="#FFB6B1";
					error=error+1;
			}
		    else if(isNaN(stcode))
			{		msg+='Please specify a valid STD code.<br>';
					document.getElementById('stdcode').style.background="#FFB6B1";
					error=error+1;
			}
			else
			{	  	document.getElementById('contact_no').style.background="";
					document.getElementById('stdcode').style.background="";
			}
		}
	  if(mobileno != '')
        { if(isNaN(mobileno))
			{		msg+='Please specify a valid Mobile no.<br>';
					document.getElementById('mobile_no').style.background="#FFB6B1";
					error=error+1;
			}
			else if(mobileno.length < 10)
			{		msg+='Mobile no should be atleast 10 digits long.<br>';
					document.getElementById('mobile_no').style.background="#FFB6B1";
					error=error+1;
			}
			else
			{		document.getElementById('mobile_no').style.background="";
			}
		}
		//Description: Validates the Qualifaction  field.	
		var qualifid=document.getElementById('qualif_id').value;
	    if(qualifid == 'none')
        {	    msg+='Please select  your qualification.<br>';
				document.getElementById('qualif_id').style.background="#FFB6B1";
                error=error+1;
        }
		else
		{	    document.getElementById('qualif_id').style.background="";			
		}
		//Description: Validates the Specialization  field.	
		var spec_id=document.getElementById('specliz_id').value;
	    if(spec_id == '')
        {		msg+='Please select  your specialization.<br>';
				document.getElementById('specliz_id').style.background="#FFB6B1";
                error=error+1;
        }
		else
		{		document.getElementById('specliz_id').style.background="";			
		}
		//Description: Validates the Institute City field.	
		var instcity=document.getElementById('InstCity').value;
	    if(instcity == 'none')
        {       msg+='Please select a City for your institute.<br>';
				document.getElementById('InstCity').style.background="#FFB6B1";
                error=error+1;
        }
		else
		{		document.getElementById('InstCity').style.background="";			
		}
	   //Description: Validates the Institute  field.	
		var inst_id=document.getElementById('Institute').value;
	    if(inst_id == '')
        {       msg+='Please select an Institute.<br>';
				document.getElementById('Institute').style.background="#FFB6B1";
                error=error+1;
        }
		else
		{		document.getElementById('Institute').style.background="";			
		}
		//Description: Validates the Other Institute  field.	
		if(inst_id == '0')
        {		var otherinst_id=trim_ws(document.getElementById('otherInstitute').value);
				if(otherinst_id == '')
				{	msg+='Specify another Institute Name if it is not listed there.<br>';
					document.getElementById('otherInstitute').style.background="#FFB6B1";
					error=error+1;
				}
				else if(pattern.test(otherinst_id))
				{	msg+='Please enter a valid Institute Name.<br>';
					document.getElementById('otherInstitute').style.background="#FFB6B1";
					error=error+1;
				}
				else
				{	document.getElementById('otherInstitute').style.background="";			
				}
		}
	    var yearofpass=trim_ws(document.getElementById('yearofpassing').value);
		var dtyear = parseInt (year) + parseInt (15);
		if(yearofpass=='')
		{		msg+='Please select a year of Passing.<br>';
				document.getElementById('yearofpassing').style.background="#FFB6B1";
				error=error+1;
		}
		else if(yearofpass <= dtyear)
        {		msg+='Please select  a year of Passing. (Should be 15 years after DOB)<br>';
				document.getElementById('yearofpassing').style.background="#FFB6B1";
				error=error+1;
        }
		else
		{		document.getElementById('yearofpassing').style.background="";			
		}
		 var mon=document.getElementById('Monthexp').value;		
		 var yer=document.getElementById('Yearexp').value;
		 var indus_id=document.getElementById('industry').value;
		 var maxi = document.newresume.pref_industry.length;
		 var funcl_id=document.getElementById('functionalarea').value;
		 var role_id=document.getElementById('Role').value;
		 var lac_id=document.getElementById('Lac').value;
		 var thous_id=document.getElementById('Thousand').value;
		if(!document.getElementById('fresh').checked)
			{
			  // alert("Unchecked");
			  if(yer == '' && mon == '')
				{		msg+='Please mention your total experience.<br>';
						document.getElementById('Yearexp').style.background="#FFB6B1";
						document.getElementById('Monthexp').style.background="#FFB6B1";
						error=error+1;
				}
				else
				{		document.getElementById('Yearexp').style.background="";
						document.getElementById('Monthexp').style.background="";
				}
			 //Description: Validates the Current Industry Type field.
			  if(indus_id == '')
				{		msg+='Please select your  current industry type.<br>';
						document.getElementById('industry').style.background="#FFB6B1";
						error=error+1;
				}
				else
				{		document.getElementById('industry').style.background="";
				}
		  		//Description: Validates the preferred Industry field.
				var maxi = document.newresume.pref_industry.length; 
				var total = 0;
				var mx =5;
				for (var idx = 0; idx < maxi; idx++) 
				{	if(document.newresume.pref_industry[idx].checked==true)
					{
					total += 1;
					}
				if(total>=mx+1)
					document.newresume.pref_industry[idx].checked=false;
				}
				if(!(total > 0 && total <= 5))
				{		msg+='Please choose minimum 1 and max 5 preferred industry types.<br>';
						error=error+1;
				}
			   //Description: Validates the Current functionalarea field.
				if(funcl_id == 'none')
				{		msg+='Please select  your current functional area.<br>';
						document.getElementById('functionalarea').style.background="#FFB6B1";
						error=error+1;
				}
				else
				{
						document.getElementById('functionalarea').style.background="";
				}
				//Description: Validates the Current Role field.
				if(role_id == '')
				{		msg+='Please select  your current Role.<br>';
						document.getElementById('Role').style.background="#FFB6B1";
						error=error+1;
				}
				else
				{		document.getElementById('Role').style.background="";
				}
				//Description: Validates the Experience field.
				if(lac_id == '' && thous_id == '')
				{		msg+='Please select Cost to Company (Annual Salary Package).<br>';
						document.getElementById('Lac').style.background="#FFB6B1";
						document.getElementById('Thousand').style.background="#FFB6B1";
						error=error+1;
				}
				else
				{		document.getElementById('Lac').style.background="";
						document.getElementById('Thousand').style.background="";			
				}
				
			}
		 else
			  {
			  //Description: Validates the Current Industry Type field.
			    var maxi = document.newresume.pref_industry.length; 
				var total = 0;
				var mx =5;
				for (var idx = 0; idx < maxi; idx++) 
				{	if(document.newresume.pref_industry[idx].checked==true)
					{
					total += 1;
					}
				if(total>=mx+1)
					document.newresume.pref_industry[idx].checked=false;
				}
				if(!(total > 0 && total <= 5))
				{		msg+='Please choose minimum 1 and max 5 preferred industry types.<br>';
						error=error+1;
				}
		   	    //Description: Validates the Current functionalarea field.
				   var funcl_id=document.getElementById('functionalarea').value;
					if(funcl_id == 'none')
					{		msg+='Please select preferred functional area.<br>';
							document.getElementById('functionalarea').style.background="#FFB6B1";
							error=error+1;
					}
					else
					{		document.getElementById('functionalarea').style.background="";
					}
					//Description: Validates the Current Role field.
					var role_id=document.getElementById('Role').value;
					if(role_id == '')
					{		msg+='Please select preferred role.<br>';
							document.getElementById('Role').style.background="#FFB6B1";
							error=error+1;
					}
					else
					{		document.getElementById('Role').style.background="";
					}
					  
			  }
	  
	    //Description: Validates the Keyskill field.	
		var skill=trim_ws(document.getElementById('keyskill').value);
		if(skill == '')
        {       msg+='Please specify  your key skills.<br>';
				document.getElementById('keyskill').style.background="#FFB6B1";
                error=error+1;
        }
		else
		{		document.getElementById('keyskill').style.background="";			
		}
		//Description: Validates the Prefjobloc field.
		var prefjob_loc=document.getElementById('prefjobloc').value;
		if(prefjob_loc == 'none' || prefjob_loc == '' )
		{			msg+='Please select preferred job locations.<br>';
					document.getElementById('prefjobloc').style.background="#FFB6B1";
					error=error+1;
		}
		else
		{			document.getElementById('prefjobloc').style.background="";			
		}
		//Description: Validates the resumeheadline field.	
        var resume_id=trim_ws(document.getElementById('resumeheadline').value);
        if(resume_id == '')
        {       msg+='Please mention your  Resume Headline.<br>';
				document.getElementById('resumeheadline').style.background="#FFB6B1";
				error=error+1;
		}
       else if(pattern.test(resume_id))
	 	{		msg+='Please enter a valid Resume Headline. (without Special characters)<br>';
				document.getElementById('resumeheadline').style.background="#FFB6B1";
				error=error+1;
	 	}
		else
		{		document.getElementById('resumeheadline').style.background="";			
		}
		//Description: Validates the BrowseResume field.	
         var uploadresume=trim_ws(document.getElementById('candresume').value);
		 var text_resume=trim_ws(document.getElementById('textresume').value);
		 if(uploadresume == '' && text_resume == '')
			{		msg+='Please Select atleast One Option (Upload / Copy-paste Resume).<br>';
					document.getElementById('candresume').style.background="#FFB6B1";
					document.getElementById('textresume').style.background="#FFB6B1";
   				    error=error+1;
			}
			else
			{		document.getElementById('candresume').style.background="";			
					document.getElementById('textresume').style.background="";			
			}
			if(uploadresume != '')
			{  if (!checkResume(uploadresume))
				{	msg+='Please Attach Microsoft document only.<br>';
					document.getElementById('candresume').style.background="#FFB6B1";
					error=error+1;
				}
				else
				{	document.getElementById('candresume').style.background="";			
				}
		 	}
			if(text_resume != '')
			{	if( (text_resume.length < 80) || (text_resume.length > 100000) )
				{		msg+='Text resume (Copy / Paste) should be  between 80 - 1 Lac  characters.<br>';
						document.getElementById('textresume').style.background="#FFB6B1";
						error=error+1;
				}
				else
				{		document.getElementById('textresume').style.background="";			
				}
		 	}
				 if(document.getElementById('agreement').checked)
				 {}
				 else
				 { msg+='Please Select Agreement/ Terms and Conditions Option.<br>';	
				   error=error+1;
				 }
		
		if(error>0)
		{
			popup.alert(msg);
			return false;
		}
		else
        return true;
}
