/* To display the description of the selected job */
var selectedOpenings = 0;
var selectedPosts = new Array('0');
var boolSelectedOpenings = false;

function display(divId,linkId){
	if(document.getElementById(divId).style.display=='none'){
		document.getElementById(divId).style.display='block';
		document.getElementById(linkId).innerHTML='-';
	}
	else{
		document.getElementById(divId).style.display='none';
		document.getElementById(linkId).innerHTML='+';
	}
}

function setSelectedOpenings(){
	if(boolSelectedOpenings==false){
		selectedOpenings = document.frmConfirmation.noOfConfirmedJobs.value;
		boolSelectedOpenings = true;
	}
}

function validateConfirmedJobs(objName){
	//alert(selectedOpenings);
	if(validate(objName) == true && selectedOpenings>0){ return true; }
	else{ return false; }
	
}
function validateSelectedOpenings(chkName){
	if(chkName.checked == true){
		selectedOpenings++;
		selectedPosts[chkName.value] = chkName.value;
	}
	else{
		selectedOpenings--;
		selectedPosts[chkName.value] = 0;
	}
}

function validate(objName){
	if(selectedOpenings <= 0){
		alert('Please select any jobs in the given list.');
		return false;
	}
	else{
		/*if(!confirm("Please confirm that you have choosen all the post that you want to apply ?")) return false;
		else{ objName.arrSelectedPosts.value = selectedPosts; return true; }//else(inner)*/
		objName.arrSelectedPosts.value = selectedPosts;
		return true;
	}//else(outer)
}


function validateFormOnSubmit(theForm) {
var reason = "";
var domains_selected = document.getElementById("domains");
var non_IT_selected = document.getElementById("specific_non_it_skill_areas");
  
  //other validations
  reason += validateHowHeardAboutUs(theForm.how_did_you_know);
  reason += validateName(theForm.first_name,"Please enter first name");
  reason += validateName(theForm.last_name,"Please enter last name");
  
  //date of birth
  reason += validateBirthdate(theForm.birth_date);
  reason += validateBirthmonth(theForm.birth_month);
  reason += validateBirthyear(theForm.birth_year);
  
  reason += validatePincode(theForm.pincode);
 // reason += validatePhone(theForm.mobile);
  reason += validateEmail(theForm.email_address);  
  
  
  //check empty
  //reason += validateEmpty(theForm.city_of_birth,"Please enter city of birth");
  //reason += validateEmpty(theForm.nationality,"Please enter nationality");
  reason += validateEmpty(theForm.address,"Please enter address");
  reason += validateEmpty(theForm.pincode,"Please enter pincode");
  reason += validateEmpty(theForm.city,"Please enter city");
  reason += validateEmpty(theForm.state,"Please enter state");
  
  if  (domains_selected.options[domains_selected.selectedIndex].value == 0 && non_IT_selected.options[non_IT_selected.selectedIndex].value==0)
	{
	 reason += "Please Enter IT/Non-IT Skills \n";
	}

if  (domains_selected.options[domains_selected.selectedIndex].value > 1 || non_IT_selected.options[non_IT_selected.selectedIndex].value > 1) {
  reason += validateEmpty(theForm.area_of_expertise,"Please enter Areas of Expertise");
  reason += validateEmpty(theForm.current_employer,"Please enter Current Employer");
  reason += validateEmpty(theForm.current_experience,"Please enter Current Experience");
 }
else
	{
	theForm.area_of_expertise.style.background = 'White';
    theForm.current_employer.style.background = 'White';
	 theForm.current_experience.style.background = 'White';
	}
  
  
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}


//empty field validation
function validateEmpty(fld,fld_desp) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#ccc'; 
        error = fld_desp + "\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}


//name validation
function validateName(fld,fld_desp) {
	//alert("hi...............")
    var error = "";
   // var illegalChars = /\W/; // allow letters, numbers, and underscores
 	var illegalChars = /^[a-zA-Z]+$/; // allow only letters
    if (fld.value == "") {
        fld.style.background = '#ccc'; 
        error = fld_desp+"\n";
    } 
	/*else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length.\n";
    } */
	else if(!fld.value.match(illegalChars)){
		fld.style.background = '#ccc'; 
        error = "The name field contains illegal characters.\n";
	}
    else {
        fld.style.background = 'White';
    }
    return error;
}

//field: how did you hear about us?
function validateHowHeardAboutUs(fld){
	 var error = "";
	if (fld.selectedIndex == 0 || fld.selectedIndex == "") {
        fld.style.background = '#ccc';
        error = "Select a valid option for How did you hear about us?.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

//date of birth validation
function validateBirthdate(fld){
	 var error = "";
	if (fld.selectedIndex == 0) {
        fld.style.background = '#ccc';
        error = "Select Date of Birth.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateBirthmonth(fld){
	 var error = "";
	if (fld.selectedIndex == 0) {
        fld.style.background = '#ccc';
        error = "Select Month of Birth.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateBirthyear(fld){
	 var error = "";
	if (fld.selectedIndex == 0) {
        fld.style.background = '#ccc';
        error = "Select Year of Birth.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}



//email validation
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = '#ccc';
        error = "Please enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#ccc';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#ccc';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}



//pincode validation
function validatePincode(fld) {
   var error = "";
   var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Please enter a pincode.\n";
        fld.style.background = '#ccc';
    } else if (isNaN(parseInt(stripped))) {
        error = "The pincode contains illegal characters.\n";
        fld.style.background = '#ccc';
    } else if (!(stripped.length == 6)) {
        error = "The phone number is the wrong length.\n";
        fld.style.background = '#ccc';
    }
    return error;
}



//phone validation
function validatePhone(fld) {
   var error = "";
   var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Please enter a phone number.\n";
        fld.style.background = '#ccc';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#ccc';
    } else if ((stripped.length < 10)) {
        error = "The phone number is the wrong length.\n";
        fld.style.background = '#ccc';
    }
    return error;
}