

var nbsp = 160;    // non-breaking space char
var node_text = 3; // DOM text node-type
var emptyString = /^\s*$/
var glb_vfld;      // retain vfld for timer thread

var val1=false;
var val2=false;
var val3=false;
var teleVal=false;
var expe=false;
var passVal=false;
var passVal2=false;
var varEmail=false;
var expectedSalary=false;
var subIndustry=false;
var industryFlag=false;
var jobTitle=false;
var val_name=false;

var exitFlag = false;

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

function setFocusDelayed()
{
  glb_vfld.focus();
}

function setfocus(vfld)
{
  glb_vfld = vfld;
  setFocusDelayed();
  setTimeout( 'setFocusDelayed()', 100 );
}

function msg(fld, msgtype,  message) 
{
  var dispmessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  
  
  if(msgtype == "error")
  {
	  elem.style.color = "red";
	}  
};

var proceed = 2;  

function commonCheck    (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(ifld);
  if (!elem.firstChild)
    return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text)
    return true;  // ifld is wrong type of node  

  if (emptyString.test(vfld.value)) {
    if (reqd) {
      msg (ifld, "error", "ERROR: required");  
      setfocus(vfld);
      return false;
    }
    else {
      msg (ifld, "warn", "");   // OK
      return true;  
    }
  }
  return proceed;
}


function commonCheck2    (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(ifld);
  if (!elem.firstChild)
    return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text)
    return true;  // ifld is wrong type of node  

  if (emptyString.test(vfld.value)) {
    if (reqd) {
      msg (ifld, "error", "ERROR: required");  
      setfocus(vfld);
      return false;
    }
    else {
      msg (ifld, "warn", "");   // OK
      return true;  
    }
  }
  return proceed;
}

function validatePresent(vfld, ifld) 
{
 
 var digits="0123456789";
  var stat = commonCheck2 (vfld, ifld, false);
  if (stat != proceed) return stat;

  for (var i=0;i<vfld.value.length;i++)
  {
		temp=vfld.value.substring(i,i+1)
		if (digits.indexOf(temp)>=0)
		{
			msg (ifld, "error", "ERROR: Invalid name");
			val_name=false;
			return false;
      	}
  }
  
  msg (ifld, "warn", "");  
  val_name=true;
  return true;
 
};



function validatePresent2(vfld, ifld) 
{
 
 var digits="0123456789";
  var stat = commonCheck2 (vfld, ifld, false);
  if (stat != proceed) return stat;

  for (var i=0;i<vfld.value.length;i++)
  {
		temp=vfld.value.substring(i,i+1)
		if (digits.indexOf(temp)>=0)
		{
			msg (ifld, "error", "ERROR: Invalid Username");
			val1=false;
			return false;
      	}
		if(temp == " ")
		{
			msg (ifld, "error", "ERROR: Invalid Username");
			val1=false;
			return false;
		}
  }
  
  msg (ifld, "warn", "");  
  val1=true;
  return true;
 
};


function validatePass(vfld, ifld,ifld2) 
{
  var stat = commonCheck2 (vfld, ifld, false);
  if (stat != proceed) return stat;
  
  msg (ifld, "warn", ""); 
  msg (ifld2, "warn", ""); 
  passVal=true;
  return true;
 
};

function validatePass2(vfld,vfld2, ifld,ifld2) 
{
	

  if(vfld.value != vfld2)
  {
	   msg (ifld2, "error", "Password doesn't match");
	   msg (ifld, "error", "Password doesn't match");
	    passVal2=false;
		return false;
  }
  
  msg (ifld, "warn", "");
  msg (ifld2, "warn", "");
  passVal2=true;
  return true;
 
};

function validateTeleNumber(vfld, ifld, reqd)
{
if(vfld.value.length<13)
  {
	   msg (ifld, "error", "ERROR: invalid Tele. Number");
	   teleVal=false;
       return false;
  }

 if(!Number(vfld.value))
 {
	 msg (ifld, "error", "ERROR: Field must be numeric");
   teleVal=false;
       return false;
 }
	
	
   msg (ifld, "", "");
  teleVal=true;
  return true
  
}

function validateJobTitle(vfld, ifld) 
{
 if (vfld.value.length==0)
	{
		msg (ifld, "error", "ERROR: Job title");
		jobTitle=false;
		return false;
	}
	
 var digits="0123456789";
  var stat = commonCheck2 (vfld, ifld, false);
  if (stat != proceed) return stat;

	
  for (var i=0;i<vfld.value.length;i++)
  {
		temp=vfld.value.substring(i,i+1)
		if (digits.indexOf(temp)>=0)
		{
			msg (ifld, "error", "ERROR: Job title");
			jobTitle=false;
			return false;
      	}
  }
  
  msg (ifld, "warn", "");  
  jobTitle=true;
  return true;
 
};

function ValidateCarrerLevel(vfld, ifld)
{
	if(vfld.value!="Select your Career Level")
	{
	msg(ifld,"warn","");
	subIndustry = true;
	return true;}
	else
	{msg(ifld,"Error","Please Select Your Career Level");
	subIndustry = false;
	return false;
	}
}

function ValidateIndustry(vfld, ifld)
{
	if(vfld.value!="Hold Ctrl key to select more than one")
	{
	msg(ifld,"warn","");
	industryFlag = true;
	return true;}
	else
	{msg(ifld,"Error","Please Select Your Industry");
	industryFlag = false;
	return false;
	}
}

function ValidateExper(vfld, ifld, reqd)
{
	if(vfld.value.length>2)
  {
	   msg (ifld, "error", "ERROR: invalid Experience. Years");
     expe=false;
       return false;
  } 
 if(!Number(vfld.value))
 {
	 msg (ifld, "error", "ERROR: Field must be numeric");
       return false;
 }
 
   msg (ifld, "", "");
  expe=true;
  return true;
}

function ValidateDegree(vfld, ifld, reqd)
{

 var digits="0123456789";
  var stat = commonCheck2 (vfld, ifld, false);
  if (stat != proceed) return stat;

  for (var i=0;i<vfld.value.length;i++)
  {
		temp=vfld.value.substring(i,i+1)
		if (digits.indexOf(temp)>=0)
		{
			msg (ifld, "error", "ERROR: Invalid Degree");
			  val1=false;
			return false;
      	}
  }
  
  msg (ifld, "warn", "");  
  val1=true;
  return true;
}


function ValidateExpectedSalary(vfld, ifld, reqd)
{
	if(vfld.value!="Select one »")
	{
	msg(ifld,"warn","");
	expectedSalary = true;
	return true;}
	else
	{msg(ifld,"Error","Please Select Expected Salary");
	expectedSalary = false;
	return false;
	}
}

function validateEmail(vfld, ifld, reqd)
{			
val2=false;
		var str = vfld.value;
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.lengtd;
		var ldot=str.indexOf(dot);
		
		if (str.indexOf(".")==(str.lengtd-1)){
		   msg (ifld, "error", "ERROR: Invalid E-mail"); 
			vfld.focus();
			return false;
		}
		if ((vfld.value==null)||(vfld.value==""))
		{
			msg (ifld, "error", "ERROR: Invalid E-mail"); 
			vfld.focus();
			return false;
		}
		if (str.indexOf(at)==-1){
		   msg (ifld, "error", "ERROR: Invalid E-mail"); 
			vfld.focus();
			return false;
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   msg (ifld, "error", "ERROR: Invalid E-mail"); 
			vfld.focus();
			return false;
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    msg (ifld, "error", "ERROR: Invalid E-mail"); 
			vfld.focus();
			return false;
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		   msg (ifld, "error", "ERROR: Invalid E-mail"); 
			vfld.focus();
			return false;
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   msg (ifld, "error", "ERROR: Invalid E-mail"); 
			vfld.focus();
			return false;
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    msg (ifld, "error", "ERROR: Invalid E-mail"); 
			vfld.focus();
			return false;
		 }
		 if (str.indexOf(" ")!=-1){
		    msg (ifld, "error", "ERROR: Invalid E-mail"); 
			vfld.focus();
			return false;
		 }
		  msg (ifld, "warn", ""); 
		  val2=true;
 		 return true;
};

function validateEmail2  (vfld,vfld2, ifld,ifld2)
{
	if(!validateEmail(vfld, ifld2, true))
	{
		msg (ifld, "error", "ERROR: Invalid E-mail");
		varEmail=false;
		return false;
	}
	
  if(vfld.value != vfld2.value)
  {
	   msg (ifld2, "error", "Email doesn't match");
	   msg (ifld, "error", "Email doesn't match");
	   varEmail=false;
		return false;
  }
  
  msg (ifld, "warn", "");
  msg (ifld2, "warn", "");
  varEmail=true;
  return true;
 
};

var empltyFlag = 0;
function ValidateReferForm()
{
	var errMsg="";
	var flag = true;
	
	if(document.mForm.name1.value != "")
	{
		if(validatePresent(document.mForm.name1, "inf_name1") != true)
		{
			flag=false;
			errMsg+="Please Correct your Name #1 Value\n";
		}
		if(validateEmail(document.mForm.email1, "inf_email1", true) != true)
		{
			flag=false;
			errMsg+="Please Correct your Email #1 Value\n";
		}
		empltyFlag++;
	}
	
	if(document.mForm.name2.value != "")
	{
		if(validatePresent(document.mForm.name2, "inf_name2") != true)
		{
			flag=false;
			errMsg+="Please Correct your Name #2 Value\n";
		}
		if(validateEmail(document.mForm.email2, "inf_email2", true) != true)
		{
			flag=false;
			errMsg+="Please Correct your Email #2 Value\n";
		}
		empltyFlag++;
	}
	if(document.mForm.name3.value != "")
	{
		if(validatePresent(document.mForm.name3, "inf_name3") != true)
		{
			flag=false;
			errMsg+="Please Correct your Name #3 Value\n";
		}
		if(validateEmail(document.mForm.email3, "inf_email3", true) != true)
		{
			flag=false;
			errMsg+="Please Correct your Email #3 Value\n";
		}
		empltyFlag++;
	}
	
	if(document.mForm.name4.value != "")
	{
		if(validatePresent(document.mForm.name4, "inf_name4") != true)
		{
			flag=false;
			errMsg+="Please Correct your Name #4 Value\n";
		}
		if(validateEmail(document.mForm.email4, "inf_email4", true) != true)
		{
			flag=false;
			errMsg+="Please Correct your Email #4 Value\n";
		}
		empltyFlag++;
	}
	
	if(document.mForm.name5.value != "")
	{
		if(validatePresent(document.mForm.name5, "inf_name5") != true)
		{
			flag=false;
			errMsg+="Please Correct your Name #5 Value\n";
		}
		if(validateEmail(document.mForm.email5, "inf_email5", true) != true)
		{
			flag=false;
			errMsg+="Please Correct your Email #5 Value\n";
		}
		empltyFlag++;
	}
	
	if(empltyFlag < 2)
	{
		errMsg+="Please invite at least two of your trusted friends to this service to activate your account";
		flag=false;
	}
	if(flag == false)
	{
		alert(errMsg);
		return false;
	}
	return true;
	
}

function TestExitingReferPage()
{
	if(empltyFlag < 2)
	{
		alert("Sorry, your account was not activated, you need at least to invite two of your friends to join Thorayya.com");
		return false;
	}
	else
	{
		return false;
	}
	
}

function ValidateCanReferForm()
{
	var errMsg="";
	var flag = true;
	var empltyFlag = true;
	if(document.mForm.name1.value != "")
	{
		if(validatePresent(document.mForm.name1, "inf_name1") != true)
		{
			flag=false;
			errMsg+="Please Correct your Name #1 Value\n";
		}
		if(validateEmail(document.mForm.email1, "inf_email1", true) != true)
		{
			flag=false;
			errMsg+="Please Correct your Email #1 Value\n";
		}
		empltyFlag = false;
	}
	
	if(document.mForm.name2.value != "")
	{
		if(validatePresent(document.mForm.name2, "inf_name2") != true)
		{
			flag=false;
			errMsg+="Please Correct your Name #2 Value\n";
		}
		if(validateEmail(document.mForm.email2, "inf_email2", true) != true)
		{
			flag=false;
			errMsg+="Please Correct your Email #2 Value\n";
		}
		empltyFlag = false;
	}
	if(document.mForm.name3.value != "")
	{
		if(validatePresent(document.mForm.name3, "inf_name3") != true)
		{
			flag=false;
			errMsg+="Please Correct your Name #3 Value\n";
		}
		if(validateEmail(document.mForm.email3, "inf_email3", true) != true)
		{
			flag=false;
			errMsg+="Please Correct your Email #3 Value\n";
		}
		empltyFlag = false;
	}
	
	if(document.mForm.name4.value != "")
	{
		if(validatePresent(document.mForm.name4, "inf_name4") != true)
		{
			flag=false;
			errMsg+="Please Correct your Name #4 Value\n";
		}
		if(validateEmail(document.mForm.email4, "inf_email4", true) != true)
		{
			flag=false;
			errMsg+="Please Correct your Email #4 Value\n";
		}
		empltyFlag = false;
	}
	
	if(document.mForm.name5.value != "")
	{
		if(validatePresent(document.mForm.name5, "inf_name5") != true)
		{
			flag=false;
			errMsg+="Please Correct your Name #5 Value\n";
		}
		if(validateEmail(document.mForm.email5, "inf_email5", true) != true)
		{
			flag=false;
			errMsg+="Please Correct your Email #5 Value\n";
		}
		empltyFlag = false;
	}
	
	if(flag == false)
	{
		alert(errMsg);
		return false;
	}
	return true;
	
}

function InitValidation()
{	
	if(document.mForm.userName.value!="Select one »"){
	ValidateExpectedSalary(document.mForm.expectSalary, 'inf_exp', true);}
	
	if(document.mForm.userName.value.length!=0){
	validatePresent2(document.mForm.userName, 'inf_name');}
	if(document.mForm.firstName.value.length!=0)
	validatePresent(document.mForm.firstName, 'inf_FirstName');
	if(document.mForm.secondName.value.length!=0)
	validatePresent(document.mForm.secondName, 'inf_LastName');
	if(document.mForm.teleNum.value.length!=0)	{
	validateTeleNumber(document.mForm.teleNum, 'inf_teleNo', true);	}
	if(document.mForm.degree.value.length!=0)
	ValidateDegree(document.mForm.degree, 'inf_Degree', true);
	if(document.mForm.experience.value.length!=0)
	ValidateExpectedSalary(document.mForm.expectSalary, 'inf_exp', true);
	if(document.mForm.jobTitle.value.length!=0)
	validateJobTitle(document.mForm.jobTitle, 'inf_JobTitle', true);
	
	if(document.mForm.experience.value.length!=0)
	ValidateExper(document.mForm.experience, 'inf_expSalary', true);
	ValidateCarrerLevel(document.mForm.subIndustry, 'inf_CalreerLevel');
	if(document.mForm.Email.value.length!=0){
		validateEmail(document.mForm.Email, 'inf_email', true);
		validateEmail2(document.mForm.Email,document.mForm.Email2,'inf_email','inf_email2');}
		
	if(document.mForm.pass1.value != document.mForm.pass2.value)
	{msg ("inf_pass", "error", "Password doesn't match");msg ("inf_pass2", "error", "Password doesn't match");}
	else{msg ("inf_pass", "warn", "");msg ("inf_pass2", "warn", "");	}
}

function ValidateAll()
{	
	var errMsg="";
	var flag;
	if(expectedSalary == false)
	{
		flag=false;
		errMsg+="Please Correct your Expected Salary value\n";
	}
	if(val_name == false)
	{
		flag=false;
		errMsg+="Please Correct your Name\n";
	}
	if(val1 == false)
	{
		flag=false;
		errMsg+="Please Correct your User Name Value\n";
	}	
	if(subIndustry == false)
	{
		flag=false;
		errMsg+="Please choose your Career level\n";
	}
	if(industryFlag == false)
	{
		flag=false;
		errMsg+="Please choose your industry\n";
	}
	if(val2 == false)
	{
		flag=false;
		errMsg+="Please Correct your Email Address\n";
	}
	/*if(teleVal == false)
	{
		flag=false;
		errMsg+="Please Correct your Telephone Number\n";
	}*/
	if(document.mForm.pass1.value != document.mForm.pass2.value)
	{
		flag = false;
		errMsg+="Password doesn't match\n";
	}
	if(document.mForm.Email.value != document.mForm.Email2.value)
	{
		flag = false;
		errMsg+="Email doesn't match\n";
	}
	if(document.mForm.userfile.value == "")
	{
		flag = false;
		errMsg+="You should select your CV\n";
	}
	if(expe == false)
	{
		flag = false;
		errMsg+="Invalid experience value\n";
	}
	if(document.mForm.checkUser.checked!=true)
	{
		flag = false;
		errMsg+="is your submitted data are valid\n";
	}
	if(document.mForm.condTerm.checked!=true)
	{
		flag = false;
		errMsg+="Please check terms & conditions\n";
	}
	if(document.mForm.pass1.value.length==0)
	{
		flag = false;
		errMsg+="Please check your password\n";
	}
	if(validatePass(document.mForm.pass1, 'inf_pass','inf_pass2')==false)
	{
		flag = false;
		errMsg+="Please check your password\n";
	}
	
	if(flag == false)
	{
		alert(errMsg);
		return false;
	}
	return true;
}

function ValidateAll2()
{	
	var errMsg="";
	var flag;
	if(expectedSalary == false)
	{
		flag=false;
		errMsg+="Please Correct your Expected Salary value\n";
	}
	if(val_name == false)
	{
		flag=false;
		errMsg+="Please Correct your Name\n";
	}	
	if(subIndustry == false)
	{
		flag=false;
		errMsg+="Please choose your Career level\n";
	}
	if(industryFlag == false)
	{
		flag=false;
		errMsg+="Please choose your industry\n";
	}
	if(val2 == false)
	{
		flag=false;
		errMsg+="Please Correct your Email Address\n";
	}
	if(teleVal == false)
	{
		flag=false;
		errMsg+="Please Correct your Telephone Number\n";
	}
	if(document.mForm.pass1.value != document.mForm.pass2.value)
	{
		flag = false;
		errMsg+="Password doesn't match\n";
	}
	if(document.mForm.Email.value != document.mForm.Email2.value)
	{
		flag = false;
		errMsg+="Email doesn't match\n";
	}
	if(expe == false)
	{
		flag = false;
		errMsg+="Invalid experience value\n";
	}
	if(document.mForm.pass1.value.length==0)
	{
		flag = false;
		errMsg+="Please check your password\n";
	}
	if(validatePass(document.mForm.pass1, 'inf_pass','inf_pass2')==false)
	{
		flag = false;
		errMsg+="Please check your password\n";
	}
	
	if(flag == false)
	{
		alert(errMsg);
		return false;
	}
	return true;
}

onerror=handleErr;
var txt="";

function handleErr(msg,url,l)
{
	txt="There was an error on this page.\n\n";
	txt+="Error: " + msg + "\n";
	txt+="URL: " + url + "\n";
	txt+="Line: " + l + "\n\n";
	txt+="Click OK to continue.\n\n";
	alert(txt);
	return true;
}

function validateAgencyPresent(vfld, ifld) 
{
 
 var digits="0123456789";
  var stat = commonCheck2 (vfld, ifld, false);
  if (stat != proceed) return stat;

  for (var i=0;i<vfld.value.length;i++)
  {
		temp=vfld.value.substring(i,i+1)
		if (digits.indexOf(temp)>=0)
		{
			msg (ifld, "error", "ERROR: Invalid agency name");
			return false;
      	}
  }
  
  msg (ifld, "warn", "");  
  agencyVal=true;
  return true;
 
}