/* Gold List Browse By Destination */
function setCsp() {
    var formObj = document.destination_search;
    var region = formObj.csp_region.options[formObj.csp_region.selectedIndex].value;
    return populateRegionSelect("destination_search", "csp_select", "city_select", "gdest_select", region);
}

function browserConf() {  
    if (this.loaded) {} else {  
        var str = navigator.appVersion;  
        this.macIE = str.match(/msie(.*)mac/i) != null;  
        this.loaded = true;  
    }  
    return this;  
}

function setText(option, text) {
	if (browserConf().macIE) {
		option.text = option.value = text;
	} else {
		option.innerHTML = text;
	}
}

function _escapeCharCode(text) {
    for (var idx = text.indexOf('&#'); idx != -1; ) {
        var endIdx = text.indexOf(';', idx);
        var str = String.fromCharCode(parseInt(text.substring(idx+2)));
        text = text.substring(0, idx) + str + text.substring(endIdx+1);
        idx=text.indexOf('&#');
    }
    return text;

    var result = text.replace(/&#\d+;/g, 
                function (str0, p1, p2, offset, s) {
                    var str = str0.substring(2, str0.length-1);
                    str = String.fromCharCode(str);
                    return str;
                }
             );
    return result;
}
    
// cspSelectName: select tag name for Country/StateProvince
function populateRegionSelect(formName, cspSelectName, citySelectName, destSelectName, regionIndex) {
    document.getElementById("thirdStep").style.display="none";
    document.getElementById("lastStep").style.display="none"; 
    
    if (regionIndex == '') {
        document.getElementById("secondStep").style.display="none";
        return;
    }
        

	var regions = window.regions;
    
	//	error check
	if (regions) {} else {
		return;
	}

    var f = document.forms[formName];
    var s = f.elements[cspSelectName];
    f.cspSelectName = cspSelectName;
    f.citySelectName = citySelectName;
    f.destSelectName = destSelectName;
    f.regionSelectName = "csp_region";
    document.cities=[];
    
	//	clear current options
	s.options.length = 0;

	var o = new Option("Select one", '');
	s.options[s.options.length] = o;
    var region = regions[regionIndex];
    o.value = region.value;
    o.style.fontSize = "12px";
            
    for (var j=0; j < region.csps.length; j++) {
        var csp = region.csps[j];
        var val = region.value + "/" + csp.value;
        document.cities[s.options.length] = csp.cities;
        o = new Option(_escapeCharCode(csp.display), val);
    	s.options[s.options.length] = o;
        o.cities = csp.cities;
       	s.onchange = selectCsp;
     }	

    document.getElementById("secondStep").style.display="block";  
    
}

function _endsWith(test) {
    return this.lastIndexOf(test) == (this.length - 1);
}
String.prototype.endsWith = _endsWith;

function selectCsp() {
    document.getElementById("thirdStep").style.display="none";
    document.getElementById("lastStep").style.display="none";    
    removeMessage(document.getElementById("messageBox"));

	var sCsp = document.destination_search.csp_select;
	var sCity = sCsp.form.elements[sCsp.form.citySelectName];

	var cspOption = sCsp.options[sCsp.selectedIndex];
	var cspValue = cspOption.value;
    document.gdests=[];

    if (!cspValue.endsWith("/") ) {
        cspValue += "/";
    }

    cspOption.cities = (cspOption.cities) ? cspOption.cities : document.cities[sCsp.selectedIndex];//	fallback for mac ie

	//	error check for state value, not necessary as city is disabled if state == ""
	if (cspValue == "" || !cspOption.cities) {
		return;
	} 
	
    sCsp.form.notComplete = false;
	//	error check
	if (sCity) {} else {
		return;
	}
    
	//	reset select
	sCity.options.length = 0;
	var o = new Option("Show All", cspValue);
	sCity.options[0] = o;
	for (var i=0; i < cspOption.cities.length; i++) {
        var city = cspOption.cities[i];
        document.gdests[sCity.options.length] = city.gdests;
	    o = new Option(_escapeCharCode(city.display), cspValue + city.value);
	    sCity.options[sCity.options.length] = o;
        o.gdests = city.gdests;

       	sCity.onchange = selectGDest;
	}
    
    document.getElementById("thirdStep").style.display="block";
}

function selectGDest() {


    document.getElementById("lastStep").style.display="block";
    removeMessage(document.getElementById("messageBox"));

	var sCsp = document.destination_search.city_select;
	var sDest = sCsp.form.elements[sCsp.form.destSelectName];

	var cspOption = sCsp.options[sCsp.selectedIndex];
	var cspValue = cspOption.value;
    if (!cspValue.endsWith("/") ) {
        cspValue += "/";
    }
    
    if (sCsp.selectedIndex == 0) {
        document.getElementById("lastStep").style.display="none";
    }
    
    cspOption.gdests = (cspOption.gdests) ? cspOption.gdests : document.gdests[sCsp.selectedIndex];//	fallback for mac ie

    
	//	error check for state value, not necessary as city is disabled if state == ""
	if (cspValue == "" || !cspOption.gdests) {
		return;
	}

	//	error check
	if (sDest) {} else {
		return;
	}
    
	//	reset select
	sDest.options.length = 0;
	var o = new Option("Show All", cspValue);
	sDest.options[0] = o;
	
	for (var i=0; i < cspOption.gdests.length; i++) {
            var city = cspOption.gdests[i];
	    o = new Option(_escapeCharCode(city.display), cspValue + city.value);
	    sDest.options[sDest.options.length] = o;
	}
    if (sDest.options.length == 1) {
        document.getElementById("lastStep").style.display="none";
    } else {
        document.getElementById("lastStep").style.display="block";
    }
}


function submitSearch(f,goldListyear) { 
    
     if (f.elements["csp_select"].value == "")
        f.notComplete = true;
     
     if (f.notComplete) { 
          var msg = "Please make a selection."; 
          showMessage(msg, document.getElementById("messageBox")); 
          return false; 
     } 

    var l = document.destination_search.csp_region.selectedIndex;        
    var m = document.destination_search.csp_select.selectedIndex;   
    var n = document.destination_search.city_select.selectedIndex;     
    var o = document.destination_search.gdest_select.selectedIndex; 
    var p = document.destination_search.gdest_select[o].value;
    if (m == 0) {
        showMessage("Please select one",document.getElementById("messageBox"));
        return false;
    }    
    if (n == 0) {
        p = document.destination_search.city_select[n].value; 
    } else {
        p = document.destination_search.gdest_select[o].value; 
    }
    var url = "/tools/travelawards/goldlist/"+goldListyear +"/regions/" + p;
    location = url; 
    return false; 
} 

 
function showMessage(text, container) { 
    if (container) { 
		container.style.display = 'inline';
        container.innerHTML = text; 
    } else { 
        alert(text); 
        return; 
    } 
} 

function removeMessage(container) {
    if (container) {
        //container.innerHTML = '';
		container.style.display = 'none';
    }
}

function  getRegionIndex(urlName)
{
     for (var i=0; i < regions.length; i++) {
            var region = regions[i];
            if(region.value == urlName)
              return  i;
     }
     return -1;

}


function  getCountryStateProvIndex(region,urlName)
{
     if (region == null ) return -1;
     var csps=region.csps;
     var numCsps=region.csps.length;
     var cntStaProv="";
     for (var i=0; i < numCsps; i++) {
            cntStaProv = csps[i];
            if(cntStaProv.value == urlName)
              return  i;
     }
     return -1;
}


function setSelectedIndexOfRegionSelectBox(selectObj, urlName){
              selectObj.selectedIndex = getRegionIndex(urlName)+1;
}

function setSelectedIndexOfCSP(selectObj,regionUrlName,cspUrlName)
{
            var regionIndex=getRegionIndex(regionUrlName);
            if ( regionIndex == -1) return;
            var region = regions[regionIndex];
            selectObj.selectedIndex = getCountryStateProvIndex(region,cspUrlName)+1;
}

function setSelectedIndexOfCity(selectObj,regionUrlName,cspUrlName,urlName)
{
            var regionIndex=getRegionIndex(regionUrlName);
            if ( regionIndex == -1) return;
            var region = regions[regionIndex];

            var cityList="";
            var cntStateProvIndex=getCountryStateProvIndex(region,cspUrlName);
            if ( cntStateProvIndex == -1) return;
            cntStateProv=region.csps[cntStateProvIndex];
            cityList = cntStateProv.cities;

            var numCities=cityList.length;
            var cityObj=null;
            for (var i=0; i < numCities; i++) {
            cityObj  = cityList[i];
            if(cityObj.value == urlName)
             {
               selectObj.selectedIndex = i+1;
             }
           }
}

function setGDest(destinationUrl){
    var sName = document.destination_search.gdest_select;
    for (var i=0; i < sName.options.length; i++) {
        if (sName[i].value == destinationUrl) {
            sName.selectedIndex = i;

        }
    }
}
