
<!--
var from_control;    // global
var validated=true;  // global

function popup_win( loc, wd, hg ) {
	var remote = null;
	remote = window.open('','','width=' + wd + ',height=' + hg + ',resizable=0,scrollbars=1,top=0,left=0');
	if (remote != null) {
		if (remote.opener == null) {
			remote.opener = self;
		}
		remote.location.href = loc;
	} 
	else { self.close(); }
}

// This scans all the onchanged routines
function onFrmSubmit(frm) {
	// force validation of all fields			  		
	var i;
	validated = true;	
	
	for (i=0;i<frm.elements.length && validated;i++) {
		if (frm.elements[i].onchange!=null){
			frm.elements[i].onchange();  // force fire onchange event
		}
	}
	
	if (validated==true) {	  
	  frm.submit();
	}
}

function validLoginID(vl) {
	rv=valid(vl,"Please enter a valid login ID",
			"[\\w-_][\\w-_][\\w-_][\\w-_]+");
	//if (rv)
	//	fld.value="(" + RegExp.$1 + ") " + RegExp.$2 + "-" + RegExp.$3;
	return rv;
}

function validPassword(vl) {
	rv=valid(vl,"Please enter a valid password",
			"[\\w-_][\\w-_][\\w-_][\\w-_][\\w-_][\\w-_]+");
	return rv;
}

function ValidatePassword(frm, ctl) {
  if (frm.password.value.length < 6) {
    alert('Your password should contain at least 6 characters for security reasons.');
    ctl.focus();
    validated = false;
    return false;
  }
  
  //check that password only contains alphanumeric values and underscore character
  var re = "^\\w+\\w+$"
  if (!frm.password.value.match(re)) {
	alert('Your password should only contain alphabets, numbers, or underscore characters.');
	ctl.focus();
	validated = false;
	return false;
  }
  
  if (frm.password.value == frm.password_check.value) {
    validated = true;
    return true;
  }
  else {
    alert('Your password confirmation does not match, plese re-enter your password');
    ctl.focus();
    validated = false;
    return false;
  }
}

function ValidateOneEmail(ctl) {
    var vl =ctl.value;
	rv=valid(vl,"Invalid email. Please enter a valid email.",
			"[\\w-_]+(\\.[\\w-_]+)*\\@+[\\w-_]+(\\.[\\w-_]+)+");
    if (!rv) {
      ctl.focus();
    }
    
    if (vl.indexOf('@jobstreet.com') > 0) {
	   alert ("Warning: Your email address should not contain 'jobstreet.com'. Please change it.");	   
//	   return false;
	}
	else {
	   return rv;
	}	    
}

function ValidateOneEmailRefer(ctl) {
    var vl =ctl.value;
	rv=valid(vl,"Invalid email. Please enter a valid email.",
			"[\\w-_]+(\\.[\\w-_]+)*\\@+[\\w-_]+(\\.[\\w-_]+)+");
    if (!rv) {
      ctl.focus();
	  return false;
    } else {
	  return true;  // ok
    }
}

function ValidateCtl(ctl, msg) {
  if (GetValueFromCtl(ctl) == "") {
	alert(msg);
	ctl.focus();
	validated = false;
	return false;
  }
  else {
    validated = true;
    return true;
  }
}

function validateRadio(frm){
    var radioselected=false;
    for(t=0;t<frm.foo.length;t++){
        if (frm.foo[t].checked) radioselected=true;
    }

    if (radioselected){
        alert("A radio button has been selected");
    }else{
        alert("A radio button is not selected");
    }
}


function ValidateCtlx(ctl, msg, compulsory) {
  if (GetValueFromCtl(ctl) == "" && compulsory=="1") {
	alert(msg);
	ctl.focus();
	validated = false;
	return false;
  }
  else {
    validated = true;
    return true;
  }
}

function ValidateText(ctl,msg) {
	var temp_str = Trim(ctl.value);

	if (temp_str == "") {
      ctl.value = "";
	  alert(msg);
	  ctl.focus();
	  validated = false;
	  return false;
	}
	else {
	  ctl.value = temp_str;
	  validated = true;
	  return true;
	}
}

function ValidateTextLimit(ctl,min_val,max_val,msg) {
	var temp_str = Trim(ctl.value);
	
	if (temp_str.length < min_val || temp_str.length > max_val) {
	  alert(msg);
	  ctl.focus();
	  validated = false;
	  return false;
	}
	else {
	  ctl.value = temp_str;
	  validated = true;
	  return true;
	}
}

function ValidateNumber(ctl, min_val, max_val, msg, compulsory) {
    var temp_num = Trim(ctl.value);
    if (compulsory == "0" && temp_num.length == 0) {
          ctl.value = temp_num;
          validated = true;
          return true;
    }
    else {
      if (isReal(temp_num) && temp_num.length > 0) {
         if ((temp_num < min_val) || (temp_num > max_val)) {
  		    alert(msg + " within the range of " + min_val + " to " + max_val + ".");
            ctl.focus();
	        validated = false;
	        return false;
         }
         else {
		    ctl.value = temp_num;
	        validated = true;
            return true;
         }
       }
       else {
	  	    alert(msg + ". Your entry was invalid.");
	        ctl.focus();
	        validated = false;
	        return false;
       }
     }
}

function ValidateDate(txtDate,msg,compulsory) {
	var tempDate = Trim(txtDate.value);

	datepart = txtDate.value.split("/");
	if(datepart.length == 3) {
    	day = datepart[0];
	    month = datepart[1];
    	year = datepart[2];

		if (year != '' && year < 1900  || year > 2100) 
			validated = false;
		else 
		{
			var test = new Date(year,month-1,day);
    		if ((test.getFullYear() == year) && (month - 1 == test.getMonth()) && (day == test.getDate()))
        		validated = true;
    		else
				validated = false;
	    }    
	} else {
		if (compulsory == '0' && tempDate == '')		
			validated = true;
		else
			validated = false;
	}

	if (validated == false) {
   		alert(msg);
	    txtDate.focus();
	}
	return validated;
}

//Jyoti
function ValidateNewDate(txtDate,msg,compulsory) 
{
	var tempDate = Trim(txtDate.value);	
	datepart = txtDate.value.split("/");
	if(datepart.length == 3) {
    	month = datepart[0];
	    day = datepart[1];
    	year = datepart[2];
    	yearLead = year.substr(0, 1);

		if (year != '' && year < 1900  || year > 2100 || yearLead == 0) 
			validated = false;
		else 
		{
			var test = new Date(year,month-1,day);
    		if ((test.getFullYear() == year) && (month - 1 == test.getMonth()) && (day == test.getDate()))    			
        		validated = true;
    		else
				validated = false;
	    }    
	} else {
		if (compulsory == '0' && tempDate == '')		
			validated = true;
		else
			validated = false;
	}

	if (validated == false) {
   		alert(msg);
	    txtDate.focus();
	}
	return validated;
}


function ValidateState(country_ctl, state_ctl, msg) {
  var state_country_val = GetValueFromCtl(state_ctl);
  var country = GetValueFromCtl(country_ctl);
  var state_country = new Array(2);
  state_country = state_country_val.split(",");
  if (country == "MY" || country == "SG" || country == "IN" || country == "PH" || country == "AU" || country == "ID" || country == "HK" || country == "TH") {
    if (country == state_country[1]) {
      validated = true;
      return true;
    }
    else {
      alert("Please choose a state that matches your Country.  Choose Others if your state is not listed.");
      state_ctl.focus();
      validated = false;
      return false;
    }
  }
  else {
    if (state_country[1] == "00") {
      validated = true;
      return true;
    }
    else {
      alert("Please choose Others and specify your state on the right.");
      state_ctl.focus();
      validated = false;
      return false;
    }
  } 
}

function ValidateOthState(state_ctl,oth_ctl,msg) {
  var state = GetValueFromCtl(state_ctl);
  if (state == "00,00") {
    ValidateText(oth_ctl,msg);
  }
}

function ValidateWorkExpDate(txtCompName,txtDate,msg) {
 var tempDate = Trim(txtDate.value);
 
 if (Trim(txtCompName.value) != '') { 
	datepart = txtDate.value.split("/");
	if(datepart.length == 2) {
		day = '1';
		month = datepart[0];
		year = datepart[1];
			 
		if (year != '' && year < 1900 || year > 2100) 
			validated = false;
		else 
		{
			var test = new Date(year,month-1,day);
			if ((test.getFullYear() == year) && (month - 1 == test.getMonth()))
				validated = true;
			else
				validated = false;
			}    
	}  
	else {
		 validated = false;		
	}
 }
 else {
	validated = true;
 }
 
 if (validated == false) {
    alert(msg + " (e.g. 12/2000)");
    txtDate.focus();
 }
 return validated;
}


function ValidateExperienceDate(frm,txtDate,msg,compulsory) {
 var tempDate = Trim(txtDate.value);
 
 if (frm.freshgrad_check.checked == false) { 
  datepart = txtDate.value.split("/");
  if(datepart.length == 2) {
   day = '1';
   month = datepart[0];
   year = datepart[1];
 
   if (year != '' && year < 1900 || year > 2100) 
    validated = false;
   else 
   {
    var test = new Date(year,month-1,day);
       if ((test.getFullYear() == year) && (month - 1 == test.getMonth()))
			validated = true;
       else
			validated = false;
   }    
  }  
  else {
   if (compulsory == '0' && tempDate == '')  
    validated = true;
   else
    validated = false;
  }
 }
 else {
  validated = true;
 }
 
 if (validated == false) {
     alert(msg + " (e.g. 12/2000)");
     txtDate.focus();
 }
 return validated;
}

function ValidateList(ctl,msg,ctlfocus) {
	var temp_str = Trim(ctl.value);
	
	if (temp_str == "") {
	  alert(msg);
	  ctlfocus.focus();
	  validated = false;
	  return false;
	}
	else {
	  ctl.value = temp_str;
	  validated = true;
	  return true;
	}
}

function ValidateConsistencySC(currency, salary) {
	var res = /^\s+/ig;
	var ree = /\s+$/ig;

	if (ValidateNumber(salary, '0', '999999999', 'Please enter numeric value without comma or other characters for Salary',0)) {
		if ((GetValueFromCtl(currency) == "" || GetValueFromCtl(currency) == 0 || GetValueFromCtl(currency) == "0") && salary.value.replace(res,"").replace(ree,"") != ""){
			alert("Please fill in your Salary Currency");
			currency.focus();
			validated = false;
    		return false;
		}		
		validated = true;
		return true;
	} else {
		validated = false;
		return false;
	}
}

function isInt(string) {
    for (var i=0;i < string.length;i++)
        if ((string.substring(i,i+1) < '0') || (string.substring(i,i+1) > '9') )
            return false;
	return true;
}

function isReal(string) {
    var decimal_found = false;
    var ws;

    for (var i=0;i < string.length;i++)
      if ((string.substring(i,i+1) < '0' || string.substring(i,i+1) > '9') && string.substring(i,i+1) != '.') {
            return false;
      }
      else {
        if (string.substring(i,i+1) == '.') {
          if (decimal_found == true) {
            return false;
          }
          else {
            decimal_found = true;
          }
        }   
      }
    return true;      
}

function isDate (day,month,year) {
	 var test = new Date(year,month-1,day);

	 if ((test.getFullYear() == year) && (month - 1 == test.getMonth()) && (day == test.getDate()))
         return true;
     else
         return false;
 }

function countTxt(frm) {
	txtCount = frm.char_count;
	txtCount.value = frm.cv.value.length;
	frm.char_count.value = txtCount.value; 
}

function AddRecord(frm, i) {
	from_control = i;

	if (from_control == 1) {
		var ctlTo = frm.skill_list;
		var ctlRecordsString = frm.txtSelectedString_skill;

		if (frm.skill_name.value == "") {
			alert("Please fill in the skill name and click on [Add].");	
			return false;
		}
	}
	else {
		var ctlTo = frm.language_list;
		var ctlRecordsString = frm.txtSelectedString_lang;

		if (GetValueFromCtl(frm.language_id) == "") {
			alert("Please select a language from the language selection list and click on [Add].");	
			return false;
		}
	}
		
	if (ctlRecordsString.value == "") {
		BumpUp(ctlTo);				
	}
		
	var no = new Option;
	no.value = BuildRecordString();
	no.text = BuildSelectedString();
	   
	if (SetToRecordsString (ctlRecordsString, no.value, frm)) {
		mblnIsDirty = true;
		ctlTo.options[ctlTo.options.length] = no;
	}
	return false;
}

function DeleteRecord(frm, i) {
	var strRemovedRecord = "";
	var n = 0;
	var res = /^\s+/ig;
	var ree = /\s+$/ig;

	from_control = i;

	if (from_control == 1 ? listing = frm.skill_list : listing = frm.language_list)

	for(var i=0; i<listing.options.length; i++) {
		if (listing.options[i].selected) {
			n++;
		}
	}

	if (n >= 5) {
		alert("Please do not select more than 5 records at once");
		return false;
	}

		// exit if there's nothing to remove
		if (listing.options.length == 0) {
			return false;
		}
	

		if (from_control == 1 ? txtSelectedString = frm.txtSelectedString_skill : txtSelectedString = frm.txtSelectedString_lang)

		// don't remove the no records string
		if (listing.options[0].value != "-1")
			strRemovedRecord = RemoveItem(listing, txtSelectedString);
	
		if (strRemovedRecord.length > 0) {
			mblnIsDirty = true;
		} 


	if (from_control == 1 ? msg = "[You have no skill in your skill list.]" : msg = "[You have no language in your language list.]")
	txtSelectedString.value = txtSelectedString.value.replace(res,"").replace(ree,""); 

	if (txtSelectedString.value == "") {
		op = new Option();
		op.value=""
		op.text = msg
		listing.options[listing.options.length] = op;
	}
	return false;
}

function BuildRecordString () {
	var frm = document.frmApplication;
	var strRecord;

	if (from_control == 1 ) {
		var res = /^\s+/ig;
		var ree = /\s+$/ig;
		var temp = /;/g;
		var temp1 = /~/g;

		skill_name_str = document.frmApplication.skill_name.value.replace(res,"").replace(ree,""); 
		skill_name_str = skill_name_str.replace(temp,",")
		skill_name_str = skill_name_str.replace(temp1,"-")
		if (skill_name_str == "") {
			return skill_name_str;
		}

		strRecord = skill_name_str;
		strRecord = strRecord + ";" + GetValueFromCtl(frm.year_of_exp);
		strRecord = strRecord + ";" + GetValueFromCtl(frm.proficiency);
	}
	else{
		strRecord = GetValueFromCtl(frm.language_id);
		strRecord = strRecord + ";" + GetValueFromCtl(frm.spoken);
		strRecord = strRecord + ";" + GetValueFromCtl(frm.written);
		return strRecord;
	}

	return strRecord;
}

function BuildSelectedString () {
	var frm = document.frmApplication;
	var str;

	if (from_control == 1 ){
		skill_name_str = Trim(document.frmApplication.skill_name.value); 

		if (skill_name_str == "") {
			return skill_name_str;
		}
	
		str = skill_name_str;
		str = str + ", " + GetTextFromCtl(frm.year_of_exp);
		str = str + ", " + GetTextFromCtl(frm.proficiency) + " ...";

		if (GetValueFromCtl(document.frmApplication.skill_list) == "") {
			BumpUp(document.frmApplication.skill_list)
		}
	}
	else{

		if (GetValueFromCtl(document.frmApplication.language_list) == "") {
			BumpUp(document.frmApplication.language_list)
		}

		str = GetTextFromCtl(frm.language_id)
		str = str + ", spoken: " + GetTextFromCtl(frm.spoken);
		str = str + ", written: " + GetTextFromCtl(frm.written) + " ...";
	}
	return str;
}

function GetValueFromCtl (ctl) {
	for (var i=0; i < ctl.options.length; i++) {
		if(ctl.options[i].selected ) {
			if (ctl.options[i].value == "-" || ctl.options[i].value == "00" || ctl.options[i].value == "000" || ctl.options[i].value == null || ctl.options[i].text == "[Select one]") {
				return "";
			} else
				return ctl.options[i].value;
		}
	}
	return "";
}

function GetTextFromCtl (ctl) {
	for (var i=0; i < ctl.options.length; i++) {
		if(ctl.options[i].selected ) {
			return ctl.options[i].text;
		}
	}
	return "";
}

function SetToRecordsString (ctl, strValue, frm) {
	var strRecords = ctl.value;
	var strRecordToAdd = "~" + strValue;	
	var alertmsg;
	var strRecordsLCase = (ctl.value).toLowerCase();
	var strRecordToAddLCase = ("~" + strValue).toLowerCase();

	if (from_control == 1) {				
		alertmsg = "You already have this skill in your skill list.  Please delete first if you wish to add the same skill."
	}
	else{				
		alertmsg = "You already have this language in your language list.  Please delete first if you wish to add the same language."
	}
		
	if (strRecordsLCase.indexOf(strRecordToAddLCase) == -1) {	
		strRecords = strRecords + strRecordToAdd;
		ctl.value = strRecords;

		if (from_control == 1 ? frm.txtSelectedString_skill.value = ctl.value : frm.txtSelectedString_lang.value = ctl.value)

		return true;
	} else
		alert (alertmsg);
		return false;
}

function SetValueToCtl (ctl, value) {
	for (var i=0; i < ctl.options.length; i++) {
		if(ctl.options[i].value == value ) {
			ctl.options[i].selected = true;
			return;
		}
	}
}


function RemoveItem(ctlFrom, ctlRecordsString) {
	var strRet = "";
	for(var i=0; i<ctlFrom.options.length; i++) {
		if(ctlFrom.options[i].selected && ctlFrom.options[i].value != "") {
			strRet = ctlFrom.options[i].value;
		RemoveFromRecordsString(ctlRecordsString, ctlFrom.options[i].value);
			ctlFrom.options[i].value = "";
			ctlFrom.options[i].text = "";
		}
	}
	BumpUp(ctlFrom);
	return strRet;
}

function StringReplace(pstrIn, pstrToReplace, pstrReplace) {
	var strRet = pstrIn;
	intPos = strRet.indexOf(pstrToReplace);
	while (intPos >= 0) {
		strRet = strRet.substring(0, intPos) + pstrReplace + strRet.substring (intPos + pstrToReplace.length, strRet.length);
		intPos = strRet.indexOf(pstrToReplace);
	}
	return strRet;
}

function RemoveFromRecordsString (ctl, value) {
	var strRecords = ctl.value;
	var strRE = value;

	strRecords = StringReplace(strRecords, "~" + strRE, "")
	ctl.value = strRecords;
}

function ValidateButton(btn,txt,msg) {	
	if (btn.checked && txt.value==""){
		alert(msg);
		return false; }
	else {
		return true; }
}

function valid(vl,errm) { // varying number of arguments
	var i;
	validated=true;
	// scan regular expressions
	for (i=2;i<valid.arguments.length;i++) 
	{
		var rx;
		//alert(valid.arguments[i]);
		rx=new RegExp(valid.arguments[i]);
		if ((a=rx.exec(vl))!=null && a[0].length==vl.length) {
			//alert("output:" + a[0]);
			return true;  // ok
		}
	}
	// no matches...
	alert(errm);
	validated=false;  // tell onsubmit if necessary
	return false;
}

function Trim(str) {
	var res = /^\s+/ig;
	var ree = /\s+$/ig;
	var out = str.replace(res,"").replace(ree,"");
    return out;
}	

function BumpUp(box)  {
	var ln = box.options.length;
	
	for(var i=0; i<box.options.length; i++) {
		if(box.options[i].value == "")  {
			for(var j=i; j<box.options.length-1; j++)  {
				box.options[j].value = box.options[j+1].value;
				box.options[j].text = box.options[j+1].text;
			}
			ln = i;
			break;
		}
	}

	if(ln < box.options.length)  {
		box.options.length -= 1;
		BumpUp(box);
	}
}

// This scans all the onchanged routines
// This had to be done as to implement India City Implementation. 

function onFrmSubmitSharedImpact(frm) {
	// force validation of all fields			  		
	var i;
	validated = true;	
	
	for (i=0;i<frm.elements.length && validated;i++) {
		if (frm.elements[i].onchange!=null){
			if (i != 22)	//Current Country dropdown
			{
				if (i != 23) //Current state dropdown
				{
					frm.elements[i].onchange();  // force fire onchange event
				}
			}		
		}
	}
	
	if (validated==true) {	  
	  frm.submit();
	}
}
	



