﻿function getFrontDropdown(controlId,id,submitValue) {
    
    var url = pathPrefix + '/ajaxmethods/methods.ashx';
    var pars = 'method=getFrontDropdown&controlId=' + controlId + '&id=' + id + '&submitValue='+submitValue;

    var myAjax = new Ajax.Request(
		    url,
		    {
			    method: 'get',
			    parameters: pars,
			    onComplete: setDropDownHTML
		    });
}

function getFrontDropdown2(controlId,id,submitValue1,submitValue2) {
    
    var url = pathPrefix + '/ajaxmethods/methods.ashx';
    var pars = 'method=getFrontDropdown2&controlId=' + controlId + '&id=' + id + '&submitValue1='+submitValue1 + '&submitValue2='+submitValue2;

    var myAjax = new Ajax.Request(
		    url,
		    {
			    method: 'get',
			    parameters: pars,
			    onComplete: setDropDownHTML2
		    });
}

function setDropDownHTML(originalRequest)
{
    if(originalRequest.responseText != ''){
        // new counters and new default value
        var itemsAndDropDown = originalRequest.responseText.split('<split>');
        var itemsAndDropDownSplit = itemsAndDropDown[1].split('|');
        
        // reset current values
        var resetValues = itemsAndDropDownSplit[0] == 'ddlCountry' ? 'ddlMaxpersons' : 'ddlCountry';
        var dropTable = document.getElementById('dropItems'+resetValues);
        var nRows = dropTable.rows.length;
        for (var x = 1; x < nRows; x++)
        {
            dropTable.rows[x].cells[0].className = 'dropdownrowdisabled';
            dropTable.rows[x].cells[0].getElementsByTagName('span')[0].innerHTML = '0';
        }
        
        // loop through new counters
        var changeItems = itemsAndDropDown[0].split(',');
        for (var x = 0; x < changeItems.length; x++)
        {
            // get id of counter and count
            var item = changeItems[x].split('|');
            if(document.getElementById('dropCounter'+item[0]) != null){
                document.getElementById('dropCounter'+item[0]).innerHTML = item[1];
                document.getElementById('droprow'+item[0]).className = 'dropdownrow';
            }
        }
        if(itemsAndDropDown[1] != ''){
            var curCounter = itemsAndDropDownSplit[2];
            
            if(itemsAndDropDownSplit[0].indexOf('ddlCountry') != -1){
                document.getElementById('dropCounterddlCountry').innerHTML = document.getElementById('dropCounter'+itemsAndDropDownSplit[0]+itemsAndDropDownSplit[1]).innerHTML;
                document.getElementById('dropCounterddlMaxpersons-1').innerHTML = curCounter;
                document.getElementById('dropCounterddlMaxpersons').innerHTML = document.getElementById('dropCounterddlCountry').innerHTML;
            }
            else {
                document.getElementById('dropCounterddlMaxpersons').innerHTML = document.getElementById('dropCounter'+itemsAndDropDownSplit[0]+itemsAndDropDownSplit[1]).innerHTML;
                document.getElementById('dropCounterddlCountry-1').innerHTML = curCounter;
                document.getElementById('dropCounterddlCountry').innerHTML = document.getElementById('dropCounterddlMaxpersons').innerHTML;
            }
        }
    }
}

function setDropDownHTML2(originalRequest)
{
    if(originalRequest.responseText != ''){
        // new counters and new default value
        var itemsAndDropDown = originalRequest.responseText.split('<split>');
        var itemsAndDropDownSplit = itemsAndDropDown[1].split('|');
        
        if(itemsAndDropDownSplit[4] != '')
        {
            window.location.href = itemsAndDropDownSplit[4];
        }
        
        // reset current values
        var resetValues1 = itemsAndDropDownSplit[0] != 'ddlCountry' ? 'ddlCountry' : 'ddlPark';
        var dropTable1 = document.getElementById('dropItems'+resetValues1);
        var nRows1 = dropTable1.rows.length;
        var y1 = 1;
        if(resetValues1 == 'ddlPark')
        {
            y1 = 0;
        }
        for (var x = y1; x < nRows1; x++)
        {
            var z = dropTable1.rows[x].cells[0];
            z.className = 'dropdownrowdisabled';
            z.parentNode.style.display = 'none';
            z.getElementsByTagName('span')[0].innerHTML = '-1';
        }
        
        var resetValues2 = itemsAndDropDownSplit[0] != 'ddlMaxpersons' ? 'ddlMaxpersons' : 'ddlPark';
        var dropTable2 = document.getElementById('dropItems'+resetValues2);
        var nRows2 = dropTable2.rows.length;
        var y2 = 1;
        if(resetValues2 == 'ddlPark')
        {
            y2 = 0;
        }
        for (var x = y2; x < nRows2; x++)
        {
            var z = dropTable2.rows[x].cells[0];
            z.className = 'dropdownrowdisabled';
            z.parentNode.style.display = 'none';
            z.getElementsByTagName('span')[0].innerHTML = '-1';
        }
        
        // loop through new counters
        var changeItems = itemsAndDropDown[0].split(',');
        var n = changeItems.length;
        for (var x = 0; x < n; x++)
        {
            // get id of counter and count
            var item = changeItems[x].split('|');
            var dc = document.getElementById('dropCounter'+item[0]);
            var dr = document.getElementById('droprow'+item[0]);
            if(dc != null){
                dc.innerHTML = (item[1] == '0' ? '&nbsp;' : item[1]);
                dr.className = 'dropdownrow';
                dr.parentNode.style.display = '';
            }
        }
        if(itemsAndDropDown[1] != ''){
            var curCounter1 = itemsAndDropDownSplit[2];
            var curCounter2 = itemsAndDropDownSplit[3];
            var dcdc = document.getElementById('dropCounterddlCountry');
            var dcdc1 = document.getElementById('dropCounterddlCountry-1');
            var dcdp = document.getElementById('dropCounterddlPark');
            //var dcdp1 = document.getElementById('dropCounterddlPark-1');
            var dcdm = document.getElementById('dropCounterddlMaxpersons');
            var dcdm1 = document.getElementById('dropCounterddlMaxpersons-1');
            var dcodp = document.getElementById('dropcontainerddlPark');
            
            if(itemsAndDropDownSplit[0].indexOf('ddlCountry') != -1){
                var a = document.getElementById('dropCounter'+itemsAndDropDownSplit[0]+itemsAndDropDownSplit[1]);
                dcdc.innerHTML = a.innerHTML;
                if(curCounter1 <= 7)
                {
                    dcodp.style.height = 'auto';
                    dcodp.style.width = '284px';
                    dcodp.style.overflowY = 'hidden';
                }
                else
                {
                    dcodp.style.height = '200px';
                    dcodp.style.width = '300px';
                    dcodp.style.overflowY = 'scroll';
                }
                dcdp.innerHTML = dcdc.innerHTML;
                dcdm1.innerHTML = curCounter2;
                dcdm.innerHTML = dcdc.innerHTML;
            }
            else if(itemsAndDropDownSplit[0].indexOf('ddlPark') != -1){
                var a = document.getElementById('dropCounter'+itemsAndDropDownSplit[0]+itemsAndDropDownSplit[1]);
                dcdp.innerHTML = a.innerHTML;
                dcdc1.innerHTML = curCounter1;
                dcdc.innerHTML = dcdp.innerHTML;
                dcdm1.innerHTML = curCounter2;
                dcdm.innerHTML = dcdp.innerHTML;
            }
            else {
                var a = document.getElementById('dropCounter'+itemsAndDropDownSplit[0]+itemsAndDropDownSplit[1]);
                dcdm.innerHTML = a.innerHTML;
                dcdc1.innerHTML = curCounter1;
                dcdc.innerHTML = dcdm.innerHTML;
                if(curCounter2 <= 7)
                {
                    dcodp.style.height = 'auto';
                    dcodp.style.width = '284px';
                    dcodp.style.overflowY = 'hidden';
                }
                else
                {
                    dcodp.style.height = '200px';
                    dcodp.style.width = '300px';
                    dcodp.style.overflowY = 'scroll';
                }
                dcdp.innerHTML = dcdm.innerHTML;
            }
        }
    }
}

// Method that builds the searchurl and redirects
function submitFrontSearch(){

    var formelements = document.forms['aspnetForm'].elements;
    var params = '';
    if($('dropdownsTab').style.display==''){
        var countryid = formelements['submitddlCountry'].value.replace(/ddlCountry/, "");
        if(countryid != '-1'){
            params = '//hogenboom/'+cultureCode+'/country='+countryid; 
        }
        var maxpersonid = formelements['submitddlMaxpersons'].value.replace(/ddlMaxpersons/, "");
        if(maxpersonid != '-1'){
            params = params == '' ? '//hogenboom/'+cultureCode+'/maximumpersons_bracket>{'+maxpersonid+'}' : params+'/maximumpersons_bracket>{'+maxpersonid+'}';
        }
        if(siteId == 1)
        {
            var parkid = formelements['submitddlPark'].value.replace(/ddlPark/, "");
            if(parkid != '-1'){
                params = params == '' ? '//hogenboom/'+cultureCode+'/_park_id='+parkid : params+'/_park_id='+parkid;
            }
        }
        if(params != '')
            document.location.href = AddWhitelabelQueryStringJS(pathPrefix + '/zoeken.aspx?fh_location='+URLEncode(params));
        else
            document.location.href = AddWhitelabelQueryStringJS(pathPrefix + '/zoeken.aspx');
    } else {
        var searchValue = formelements['keywordInput'].value.replace("%", "").replace("#", "").replace("&", "").replace(";", "").replace("<", "").replace(">", "");
        
        if(searchValue == null || searchValue == '')
            document.location.href = AddWhitelabelQueryStringJS(pathPrefix + '/zoeken.aspx');
        else
            document.location.href = AddWhitelabelQueryStringJS(pathPrefix + '/zoeken.aspx?fh_location='+URLEncode('//hogenboom/' + cultureCode + '/$s='+searchValue));
    }
}
function showTab(id){
    if(id == 'dropdownsTab'){
        $('textsearchTab').style.display='none';
        $('li1').className='Other';
        $('dropdownsTab').style.display='';
        $('li0').className='Sel';
        if(siteId == 2)
        {
            $('mapsearchTab').style.display='none';
        }
        $('li2').className='Other';
    }
    if(id == 'textsearchTab'){
        $('dropdownsTab').style.display='none';
        $('li0').className='Other';
        $('textsearchTab').style.display='';
        $('li1').className='Sel';
        if(siteId == 2)
        {
            $('mapsearchTab').style.display='none';
        }
        $('li2').className='Other';
    }
    if(id == 'mapsearchTab'){
        $('dropdownsTab').style.display='none';
        $('li0').className='Other';
        $('textsearchTab').style.display='none';
        $('li1').className='Other';
        $('mapsearchTab').style.display='';
        $('li2').className='Sel';
    }
}

function textSearchKeyDown(evt){
    var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
      if (keyCode == 13)   //13 = the code for pressing ENTER 
      {
         submitFrontSearch();
         return false;
      }
      return true;
}
addEvent(document, "onkeydown", textSearchKeyDown);


