var re1 = new RegExp("\"", "gi")
var re2 = new RegExp("'", "gi")
var g_strCityIdentifierName = "";

var sOptionSeparator = ";"
var sTextValueSeparator = "[:]"

function ReplaceEscapedXML(sValueInput){
	var re1 = new RegExp("&amp;","gi")
	var re2 = new RegExp("&quot","gi")
	var re3 = new RegExp("&lt;","gi")
	var re4 = new RegExp("&gt;","gi")
	
	var temp =	sValueInput.replace(re1,"&")
	temp = temp.replace(re2,"\"")
	temp = temp.replace(re3,"<")
	temp = temp.replace(re4,">")
	
	return temp 
}

function ReplaceUnEscapedXML(sValueInput){
	var re1 = new RegExp("&","gi")
	var re2 = new RegExp("\"","gi")
	var re3 = new RegExp("<","gi")
	var re4 = new RegExp(">","gi")
	
	var temp =	sValueInput.replace(re1,"&gt;")
	temp = temp.replace(re2,"&quot;")
	temp = temp.replace(re3,"&lt;")
	temp = temp.replace(re4,"&gt;")
	
	return temp 
}


function GetSelectedIndexes(oSelectionBoxInput){ 

        if(oSelectionBoxInput.type != "select-multiple" && oSelectionBoxInput.type != "select-one") 
              return 
    
        var aSelectedIndexes   = new Array() 
        
        if(oSelectionBoxInput.options.selectedIndex == -1){ 
              // No item is seleted in the selection box 
              aSelectedIndexes[0] = -1 
              return aSelectedIndexes 
        } 
        
        for(var i=0; i<oSelectionBoxInput.options.length; i++){ 
              if(oSelectionBoxInput.options[i].selected == true){
                  aSelectedIndexes[aSelectedIndexes.length] = i
              } 
        } 
        return aSelectedIndexes        
} 

function SelectAllOption(oSelBoxInput){
    if(oSelBoxInput.type != "select-multiple" && oDependentSelBoxInput.type != "select-one") 
              return 
	
	for(var i=0; i<oSelBoxInput.length; i++){
		oSelBoxInput.options[i].selected = true 
	}
	return 
}


function ChangeDependentSelectionBox(oDependentSelBoxInput, sContentCSVInput, sOptionSepeartorInput, sTextValueSeparatorInput,sSelectedIndexCSVInput){
        
    if(oDependentSelBoxInput.type != "select-multiple" && oDependentSelBoxInput.type != "select-one") 
          return 
    if(sContentCSVInput == "") { 
          //It means that the oDependentSelBoxInput doesn't have any contents (options), 
          //i.e, clear any option box in the oDependentSelBoxInput 
          for(var i=oDependentSelBoxInput.options.length-1; i>=0; i--){
              oDependentSelBoxInput.options[i] = null ;
          } 
          return 
    } 
        
	sOptionSepeartorInput = (sOptionSepeartorInput==null)? sOptionSepeartor : sOptionSepeartorInput
	sTextValueSeparatorInput = (sTextValueSeparatorInput==null)? sTextValueSeparator : sTextValueSeparatorInput
		
	//tcdbg
    //var aOption = sContentCSVInput.split(sOptionSepeartorInput) 
    var aOption = ReplaceEscapedXML(sContentCSVInput).split(sOptionSepeartorInput) 
        
    var sText 
    var sValue 
    var oOption 
        
	var iDisplayType = 1;
	if (document.forms[0].JobLocation) {
		if (document.forms[0].JobLocation.displaytype) {
			iDisplayType = document.forms[0].JobLocation.displaytype;
		}
	}

    for(var i=0 ; i<aOption.length; i++){ 
		//Populate the oDependentSelBoxInput based on the sContentCSVInput
		sValue = aOption[i].split(sTextValueSeparatorInput)[1]
		sText = aOption[i].split(sTextValueSeparatorInput)[0]

		if (iDisplayType == 2) {
			//fix long location name
			if (!IsAnyCityOfCountry(sValue)) {
				sText = sText.substr(sText.indexOf("-")+2)
			}
		}
			
		oOption = new Option(sText,sValue) 
		oDependentSelBoxInput.options[i] = oOption          
    } 
        
    for(var i=oDependentSelBoxInput.options.length-1; i>= aOption.length; i--){
          //remove the additioal options from the previous selection box 
          oDependentSelBoxInput.options[i] = null 
    } 
        
    if(sSelectedIndexCSVInput.length > 0){
		var aSelectedIndexes = sSelectedIndexCSVInput.split(",") 
		for(var i=0; i<aSelectedIndexes.length; i++){ 
		      if(aSelectedIndexes[i]  < oDependentSelBoxInput.options.length){
		          //safeguard the array index out of bound
		          oDependentSelBoxInput.options[aSelectedIndexes[i]].selected = true 
		      } 
		} 
	}
} 


function MoveOptionBetweenSelectionBox(oFromSelBoxInput, oToSelBoxInput){ 
        
	if(oFromSelBoxInput.type != "select-multiple" && oFromSelBoxInput.type != "select-one")
              return false 
        if(oToSelBoxInput.type != "select-multiple" && oToSelBoxInput.type != "select-one")
              return false 
     
        if(oFromSelBoxInput.options.selectedIndex == -1){ 
              //no item selected. 
              return false ; 
        } 
     
        var aSelectedIndexes = GetSelectedIndexes(oFromSelBoxInput) 
        var iSelectedIndex 
        var oOption 
		var bOptionAlreadyExist = false


        for(var i=0; i<aSelectedIndexes.length;i++){ 
			iSelectedIndex = aSelectedIndexes[i] 
//			oOption = new Option(oFromSelBoxInput.options[iSelectedIndex].text, oFromSelBoxInput.options[iSelectedIndex].value)
			oOption = new Option(GetTextByValue(oFromSelBoxInput.options[iSelectedIndex].value,eval(g_strCityIdentifierName),sOptionSeparator,sTextValueSeparator), oFromSelBoxInput.options[iSelectedIndex].value)
			  
			bOptionAlreadyExist = false
			for(var k=0; k<oToSelBoxInput.length; k++){
				if(	oToSelBoxInput.options[k].text == oOption.text &&
					oToSelBoxInput.options[k].value == oOption.value ){
				
					bOptionAlreadyExist = true 
				}
			}
		
			if(!bOptionAlreadyExist){
				oToSelBoxInput.options[oToSelBoxInput.options.length] = oOption 
			}
        } 
        
} 



function SelectCityToTargetCity(oFromSelBoxInput, oToSelBoxInput){ 

        if(oFromSelBoxInput.type != "select-multiple" && oFromSelBoxInput.type != "select-one")
              return false 
        if(oToSelBoxInput.type != "select-multiple" && oToSelBoxInput.type != "select-one")
              return false 
        
        var aCountryCode = new Array()
        var sAnyCityValue = ""

		for (var i=0; i<oToSelBoxInput.length;i++){
			if (oToSelBoxInput.options[i].selected) {
				oToSelBoxInput.options[i].selected = false;
			}
		}

		MoveOptionBetweenSelectionBox(oFromSelBoxInput,oToSelBoxInput) 		
		
		for(var i=0; i<oToSelBoxInput.length;i++){
			if(IsAnyCityOfCountry(oToSelBoxInput.options[i].value)){
				aCountryCode[aCountryCode.length] = oToSelBoxInput.options[i].value.split("|")[0]
			}
		}
		
		for(var i=0; i<oToSelBoxInput.length;i++){
			for(var j=0; j<aCountryCode.length; j++){
				if(oToSelBoxInput.options[i].value.split("|")[0] == aCountryCode[j] && oToSelBoxInput.options[i].value.split("|")[1] != 0){
					oToSelBoxInput.options[i].selected = true
					j = aCountryCode.length
				}else{
					oToSelBoxInput.options[i].selected = false 
				}
			
			}
		}
		

		//remove the option from the selection box if choose "Any"
		for(var k = oToSelBoxInput.length-1; k>=0;k--){
			if(oToSelBoxInput.options[k].selected == true ){
				oToSelBoxInput.options[k] = null ;
			}
		}

		//MoveOptionBetweenSelectionBox(oToSelBoxInput, oFromSelBoxInput)

}		

function RestoreCityToCountry(oCountrySelBoxInput, oCitySelBoxInput, oTargetCitySelBoxInput){ 

        if(oCountrySelBoxInput.type != "select-multiple" && oCountrySelBoxInput.type != "select-one")
              return false 
        if(oCitySelBoxInput.type != "select-multiple" && oCitySelBoxInput.type != "select-one")
              return false 
        if(oTargetCitySelBoxInput.type !="select-multiple" && oTargetCitySelBoxInput.type != "select-one")
              return false                        

        var aSelelectedIndexs = GetSelectedIndexes(oTargetCitySelBoxInput) 
        var iSelectedIndex 
        var iSeparatorIndex 
//        var sCityIdentifierName 
        
        if(aSelelectedIndexs[0] != -1){ 
             
              var oOption 
              for(var i=aSelelectedIndexs.length-1; i>=0; i--){
                  iSelectedIndex = aSelelectedIndexs[i]
                    oTargetCitySelBoxInput.options[iSelectedIndex] = null 
              } 
        } 
} 


function OnChangeCountry(oCountrySelBoxInput, oCitySelBoxInput, oTargetCitySelBoxInput){ 

        if(oCountrySelBoxInput.type !="select-multiple" && oCountrySelBoxInput.type != "select-one")
              return false 
        if(oCitySelBoxInput.type != "select-multiple" && oCitySelBoxInput.type != "select-one")
              return false 
        if(oTargetCitySelBoxInput.type !="select-multiple" && oTargetCitySelBoxInput.type != "select-one")
              return false 
        
        if(oCountrySelBoxInput.options[oCountrySelBoxInput.selectedIndex].value != "dummyIndicator"){
			var iSelectedIndex = oCountrySelBoxInput.options.selectedIndex 
			//Dynamic built variables        
			g_strCityIdentifierName = "s" + oCountrySelBoxInput.options[iSelectedIndex].value + "City"
//			var sTargetIdentifierName = "s" + oCountrySelBoxInput.options[iSelectedIndex].value + "TargetCity"

			ChangeDependentSelectionBox(oCitySelBoxInput, eval(g_strCityIdentifierName),sOptionSeparator,sTextValueSeparator,"")
		}else{
			//g_strCityIdentifierName = "";
			ChangeDependentSelectionBox(oCitySelBoxInput, "",sOptionSeparator,sTextValueSeparator,"")
		}
		        
        //Reset the TargetCountry Selection Box 
        return true   
} 



function IsAnyCityOfCountry(sValueInput){
	var sCityCode
	
	sCityCode = sValueInput.split("|")[1]
	if(sCityCode == "0"){
		return true 
	}else{
		return false 
	}
}

function GetTextByValue(strValue,sContentCSVInput,sOptionSepeartorInput,sTextValueSeparatorInput) {

	//intType: 1=short, 2=long
    if(sContentCSVInput == "") { 
          //It means that the oDependentSelBoxInput doesn't have any contents (options), 
          //i.e, clear any option box in the oDependentSelBoxInput 
          for(var i=oDependentSelBoxInput.options.length-1; i>=0; i--){
              oDependentSelBoxInput.options[i] = null ;
          } 
          return 
    } 
        
	sOptionSepeartorInput = (sOptionSepeartorInput==null)? sOptionSepeartor : sOptionSepeartorInput
	sTextValueSeparatorInput = (sTextValueSeparatorInput==null)? sTextValueSeparator : sTextValueSeparatorInput
		
    var aOption = ReplaceEscapedXML(sContentCSVInput).split(sOptionSepeartorInput) 
        
    var sText 
    var sValue 
    var oOption 
        
	var iDisplayType = 1;
	if (document.forms[0].JobLocation) {
		if (document.forms[0].JobLocation.displaytype) {
			iDisplayType = document.forms[0].JobLocation.displaytype;
		}
	}

    for(var i=0 ; i<aOption.length; i++){ 
		//Populate the oDependentSelBoxInput based on the sContentCSVInput
		sValue = aOption[i].split(sTextValueSeparatorInput)[1]
		sText = aOption[i].split(sTextValueSeparatorInput)[0]

		if (iDisplayType == 2) {
			//fix long location name
			if (!IsAnyCityOfCountry(sValue)) {
				sText = sText.substr(sText.indexOf("-")+2)
			}
		}

		if (sValue == strValue) {
			return sText;
		}
    } 
}



//=============================[SG CODE UPDATE BEGIN]==========================================
//CODEID: 1
//DATE: 13/03/2006
//FUNCTION:
//DESCRIPTION:	Javascript function populating the City list, based on the Country drop down selection.
//				For Main Page Job Location display Type = '3'
//DEVELOPER: s h a y m a x
//---------------------------------------------------------------------------------------------

function OnChangeCountry_Layout3(oCountrySelBoxInput, oCitySelBoxInput, oTargetCitySelBoxInput){ 

	
	if(oCountrySelBoxInput.options[oCountrySelBoxInput.selectedIndex].value != "dummyIndicator"){
			var iSelectedIndex = oCountrySelBoxInput.options.selectedIndex 
			//Dynamic built variables        
			g_strCityIdentifierName = "s" + oCountrySelBoxInput.options[iSelectedIndex].value + "City"
//			var sTargetIdentifierName = "s" + oCountrySelBoxInput.options[iSelectedIndex].value + "TargetCity"
			
			ChangeDependentSelectionBox_Layout3(oCitySelBoxInput, eval(g_strCityIdentifierName),sOptionSeparator,sTextValueSeparator,"")
	}else{
			//g_strCityIdentifierName = "";
			ChangeDependentSelectionBox_Layout3(oCitySelBoxInput, "",sOptionSeparator,sTextValueSeparator,"")
	}
		        
     //Reset the TargetCountry Selection Box 
     return true 

}
//---------------------------------------------------------------------------------------------
//CODEID: 2
//DATE: 13/03/2006
//FUNCTION:
//DESCRIPTION: Javascript function populating the City list, based on the Country drop down selection.
//				For Main Page Job Location display Type = '3'
//DEVELOPER: s h a y m a x
//---------------------------------------------------------------------------------------------

function ChangeDependentSelectionBox_Layout3(oDependentSelBoxInput, sContentCSVInput, sOptionSepeartorInput, sTextValueSeparatorInput,sSelectedIndexCSVInput){
        
    if(oDependentSelBoxInput.type != "select-multiple" && oDependentSelBoxInput.type != "select-one") 
          return 
    if(sContentCSVInput == "") { 
          //It means that the oDependentSelBoxInput doesn't have any contents (options), 
          //i.e, clear any option box in the oDependentSelBoxInput 
          for(var i=oDependentSelBoxInput.options.length-1; i>=0; i--){
              oDependentSelBoxInput.options[i] = null ;
          } 
          return 
    } 
        
	sOptionSepeartorInput = (sOptionSepeartorInput==null)? sOptionSepeartor : sOptionSepeartorInput
	sTextValueSeparatorInput = (sTextValueSeparatorInput==null)? sTextValueSeparator : sTextValueSeparatorInput
		
	//tcdbg
    //var aOption = sContentCSVInput.split(sOptionSepeartorInput) 
    var aOption = ReplaceEscapedXML(sContentCSVInput).split(sOptionSepeartorInput) 
        
    var sText 
    var sValue 
    var oOption 
        
	var iDisplayType = 1;
	if (document.forms[0].JobLocation) {
		if (document.forms[0].JobLocation.displaytype) {
			iDisplayType = document.forms[0].JobLocation.displaytype;
		}
	}

    for(var i=0 ; i<aOption.length; i++){ 
		//Populate the oDependentSelBoxInput based on the sContentCSVInput
		sValue = aOption[i].split(sTextValueSeparatorInput)[1]
		sText = aOption[i].split(sTextValueSeparatorInput)[0]

		if (iDisplayType == 2) {
			//fix long location name
			if (!IsAnyCityOfCountry(sValue)) {
				sText = sText.substr(sText.indexOf("-")+2)
			}
		}
			
		oOption = new Option(sText,sValue) 
		oDependentSelBoxInput.options[i] = oOption          
    } 
        
    for(var i=oDependentSelBoxInput.options.length-1; i>= aOption.length; i--){
          //remove the additioal options from the previous selection box 
          oDependentSelBoxInput.options[i] = null 
    } 
        
    if(sSelectedIndexCSVInput.length > 0){
		var aSelectedIndexes = sSelectedIndexCSVInput.split(",") 
		for(var i=0; i<aSelectedIndexes.length; i++){ 
		      if(aSelectedIndexes[i]  < oDependentSelBoxInput.options.length){
		          //safeguard the array index out of bound
		          oDependentSelBoxInput.options[aSelectedIndexes[i]].selected = true 
		      } 
		} 
	}
} 

//=============================[/SG CODE UPDATE END]==========================================