// --------------------------------------------------------------------------------
// sd_Application.js
// Simon Andererson 2008-02-04
// Contains Javascript relevant to the Service Directory application.
// --------------------------------------------------------------------------------

var fieldPrefix = "tmpl_hlAdvancedSearch_ctl01_";

// province/city matrix
var hl_invProvCities = null;

// --- Attach JS to document elements and execute simple statements. ---
ow_f_AppendLoadEvent(
function() {

    /*
    if (document.getElementById(fieldPrefix + "sd_btnSearch") != null) hl_f_AddEvent(document.getElementById(fieldPrefix + "sd_btnSearch"), "click", sd_formSubmitClicked, false);
    if (document.getElementById(fieldPrefix + "sd_btnSearchImage") != null) hl_f_AddEvent(document.getElementById(fieldPrefix + "sd_btnSearchImage"), "click", sd_formSubmitClicked, false);

	if (document.getElementById("sd_form") != null) {
    var inp = document.getElementById("sd_form").getElementsByTagName("input");
    for (var i = 0; i < inp.length; i++) {
    if (inp[i].type == "text") 
    hl_f_AddEvent(inp[i], "keypress", sd_formTextSubmit, false);
    }
    }
    */
    // set up the province/city matrix for the drop downs
    if (document.getElementById(fieldPrefix + "hl_lstProvince") != null) {

        if (document.getElementById(fieldPrefix + "hl_xProvinceCities") != null) {

            ow_f_AddEvent(document.getElementById(fieldPrefix + "hl_lstProvince"), "change", hl_selectCity, false);

            var provCities = document.getElementById(fieldPrefix + "hl_xProvinceCities").value.split(";");
            var provinceOptions = document.getElementById(fieldPrefix + "hl_lstProvince").options;
            var cityOptions = document.getElementById(fieldPrefix + "hl_lstCity").options;

            hl_invProvCities = new Array(provinceOptions.length);
            for (i = 0; i < provCities.length; i++) {
                if (hl_invProvCities[parseInt(provCities[i])] == null)
                    hl_invProvCities[parseInt(provCities[i])] = new Array();
                if (cityOptions[i] != null)
                    hl_invProvCities[parseInt(provCities[i])].push(cityOptions[i]);

            }

            hl_selectCity(null);
        }
    }
            
    hl_hideLogos(null);
}
);

// --------------------------------------------------------------------------------
// hl_selectCity()
// Changes the options in the select lists depending on the current province of the city.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- none
// --------------------------------------------------------------------------------
function hl_selectCity(e) {
    var hl_lstProvince = document.getElementById(fieldPrefix + "hl_lstProvince");
    if (hl_lstProvince != null) {
        var prov = hl_lstProvince.selectedIndex;

        if (hl_invProvCities != null) {
            var hl_lstCity = document.getElementById(fieldPrefix + "hl_lstCity");

            var currCity = "";
            if (hl_lstCity.selectedIndex > -1)
                currCity = hl_lstCity.options[hl_lstCity.selectedIndex].value;

            hl_lstCity.options.length = 0;

            if (prov != 0)
                hl_lstCity.options.add(hl_invProvCities[0][0]);

            if (hl_invProvCities[prov] != null)
                for (i = 0; i < hl_invProvCities[prov].length; i++)
                if (hl_invProvCities[prov][i] != null) {
                var option1 = hl_invProvCities[prov][i];
                if (option1.value == currCity)
                    option1.selected = true;
                hl_lstCity.options.add(option1);

            }
        }
    }
}
// --------------------------------------------------------------------------------
// hl_hideLogos()
// Hides all the hotel logos on a page. Fires on laod and on mouseout of a hotel name hyperlink.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function hl_hideLogos() {

    var logos = ow_f_GetElementsByClassName("hl_SRLogo", "div");
    for (var i = 0; i < logos.length; i++)
        logos[i].style.display = "none";

}

// --------------------------------------------------------------------------------
// hl_showLogo()
// Shows a specific hotel logo at logoIndex
// --------------------------------------------------------------------------------
// Arguments:
//	- logoIndex; the index of the hotel logo to display in the repeater
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function hl_showLogo(logoIndex) {

    var logos = ow_f_GetElementsByClassName("hl_SRLogo", "div");
    for (var i = 0; i < logos.length; i++) {
        if (i == logoIndex)
            logos[i].style.display = "block";
        else logos[i].style.display = "none";
    }


}

// --------------------------------------------------------------------------------
// sd_formTextSubmit()
// Fires when a textfield had focus and ENTER was pressed.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function sd_formTextSubmit(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	if (code == 13) {
		if (document.getElementById(fieldPrefix + "sd_btnSearch") != null)
			document.getElementById(fieldPrefix + "sd_btnSearch").click();
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	}
}

// --------------------------------------------------------------------------------
// sd_formSubmitClicked()
// Fires when the submit button was clicked.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function sd_formSubmitClicked(e) {
	if (!sd_checkData()) {
		if (!e) var e = window.event;
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	} else {
		return;
	}
}


// --------------------------------------------------------------------------------
// sd_checkData()
// Validates the data entered on the page.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- the results of the validation [boolean]
// --------------------------------------------------------------------------------
function sd_checkData()
{
	return true;	
}
