﻿    var ROWS = [];
    var result = [];
    var ROWS_src = [];
    var INFO = [];
    var INFO_src = [];
    var MaxOnPage = 4;
    var CitizenCountry = 'IL';
    var CountryName = '';
    var CityName = '';
    var Currency = '';
    var DETALES =[];
    var DETALES_src =[];
    var Kinder = [];
    var STARS = [];
    var LOCATIONS = [];
    var PRICES = [];
    var Pack = 0;
    var Session = {OrderBy:26,
            Page:0,
            Desc:false,
            OrderByID:2};
    var trActived = '', trActivedLoadAnyway = true;
    var CityArray = [];
    
    var default_opts = 
	{   City: ''/*taken from default.js.aspx*/,
	    Currency: 'Euro',
	    Country: 'בחר מדינה',
	    Location: 'בחר עיר',
	    CheckInDateD: setDay(7),
	    CheckInDateM: setMonth(7),
	    CheckInDateY: setYear(7),
	    RoomsCount: '1',
	    Nights: '3',
	    RoomAdultCount_1: 2,
	    RoomAdultCount_2: 1,
	    RoomAdultCount_3: 1,
	    
	    RoomInfantCount_1: 0,
	    RoomInfantCount_2: 0,
	    RoomInfantCount_3: 0,
	    
	    RoomInfantAge1_1: 0,
	    RoomInfantAge1_2: 0,
	    RoomInfantAge1_3: 0,
	    
	    RoomInfantAge2_1: 0,
	    RoomInfantAge2_2: 0,
	    RoomInfantAge2_3: 0
	};
    
    function _onchange(id)
    {   if(id.indexOf('Country')!=-1)
        {   LocationsLoad();
        }
        else if(id.indexOf('RoomInfantCount')!=-1)
        {   N = id.substring(id.length-1,id.length);
            var inf = !isNaN(parseInt($(id).value)) ? parseInt($(id).value) : 0;
            $(id).value = inf;
            var max = 4 - inf;
            SimpleSelects.Load($('RoomAdultCount_'+N), 'RoomAdultCount_'+N, 1, max);
            v = $('RoomAdultCount_' + N).value;
            v = !isNaN(parseInt(v)) ? parseInt(v) : 0;
            if(v > max) 
            {   $('RoomAdultCount_' + N).value = max;
                $('RoomAdultCount_' + N + '_input').value = max;
            }
            $('RoomInfantAge_' + N +'_text').style.display = inf>0 ? '' : 'none';
            if(inf>0) $('RoomInfantAge_' + N + '_text').innerHTML = inf==1 ? 'גיל ילד:' : 'גיל ילדים:';
            for(var i=1;i<=2;i++)
            {   $('RoomInfantAge' + i + '_' + N + '_cont').style.display = i<=inf ? '' : 'none';
                if(i>inf)
                {   $('RoomInfantAge' + i + '_' + N).value = '0';
                    $('RoomInfantAge' + i + '_' + N + '_input').value = '0';
                }
            }
        }
        else if(id == 'RoomsCount')
        {   var rooms = !isNaN(parseInt($(id).value)) ? parseInt($(id).value) : 1;
            $(id).value = rooms;
            $('ROOM1_text').innerHTML = rooms==1 ? 'חדר:' : 'חדר 1:';
            
            for(var i=2;i<=3;i++)
            {   $('ROOM' + i).style.display = i<=rooms ? '' : 'none';
                if(i>rooms)
                {   $('RoomAdultCount_' + i).value = '2';
                    $('RoomAdultCount_' + i + '_input').value = '2';
                    $('RoomInfantCount_' + i).value = '0';
                    $('RoomInfantCount_' + i + '_input').value = '0';
                }
                _onchange('RoomInfantCount_' + i);
            }
        }
        else if(id == 'Nights')
        {   CheckOutWrite();
        }
        else if(id.match(/checkindate/i))
        {   CheckOutWrite();
        }
        else if(id == 'Currency')
        {   Currency = $(id).value;
            LoadFilters(true);Filter(true);
            //try{FiltersToDefauilt()}catch(ex){}
        }
        else if(id == 'HotelNames')
        {   FilterWriteCash(true);
            SetFilters()
            Filter();
        }
    };
    Date.prototype.addDays = function(days)
    {   this.setDate(this.getDate()+days);
    } 
    function CheckOutWrite()
    {   var D,d,m,y,n;
        d = parseInt($('CheckInDateD').value.replace(/^0/,''));
        m = parseInt($('CheckInDateM').value.replace(/^0/,''))-1;
        y = parseInt($('CheckInDateY').value.replace(/^0/,'20'));
        n = parseInt($('Nights').value.replace(/^0/,''));
        
        D = new Date();
        D.setFullYear(y);
        D.setMonth(m);
        D.setDate(d);
        D.addDays(n);
        
        d = D.getDate();
        m = DateTime.monthNames[D.getMonth()];
        y = D.getFullYear();
        $('CheckOut').innerHTML = d+' ל'+m+', '+y;
    }
    function getSysRoomCode(i)
	{   var SysRoomCode = "S"; var Age = 0;
	    var ad = !isNaN(parseInt($('RoomAdultCount_'+i).value)) ? parseInt($('RoomAdultCount_'+i).value) : 0;
	    var inf = !isNaN(parseInt($('RoomInfantCount_'+i).value)) ? parseInt($('RoomInfantCount_'+i).value) : 0;
	    var age1 = !isNaN(parseInt($('RoomInfantAge1_'+i).value)) ? parseInt($('RoomInfantAge1_'+i).value) : 0;
	    var age2 = !isNaN(parseInt($('RoomInfantAge2_'+i).value)) ? parseInt($('RoomInfantAge2_'+i).value) : 0;
	    
	    if(ad==1 && inf==0) SysRoomCode = "S";
	    else if(ad==1 && inf==1) SysRoomCode = "T0";
	    else if(ad==2 && inf==1) SysRoomCode = "D1";
	    else if(ad==2 && inf==0) SysRoomCode = "D0";
	    else if(ad==3 && inf==0) SysRoomCode = "T3";
	    else
	    {   if(ad==1 && inf==2) SysRoomCode = "T1";
	        else if(ad+inf == 4) SysRoomCode = "Q4";
	    }
	    if(inf>0)
        {   var a1 = age1;
            if(inf == 2)
            {   /*ad++;
                inf=1;
                Age = Math.min(age1,age2);
                if(Age<=0) Age = 1;*/
                var a2 = Math.max(age1,age2);
                if(a2<=0)a2=1;
                a1 = Math.min(age1,age2);
                $('RoomInfantAge2_'+i).value = a2;
            }
            else $('RoomInfantAge2_'+i).value = 0;
            if(a1<=0) a1=1;
            $('RoomInfantAge1_'+i).value = a1;
        }
	    return SysRoomCode;
	};
	var ClearSession = false;
	function Search()
	{   if($('City').value == default_opts.City)$('City').value = '';
	    if($('search_form').style.display!='none')
	    {   var error = Params.params(Fields.Search, 'Get');
	        
	        if(!error)
	        {   var R = [];
	            var RC = !isNaN(parseInt($('RoomsCount').value)) ? parseInt($('RoomsCount').value) : 1;
	            if(RC<=0) RC = 1;
	            for(var i=0;i<3;i++)
	            {   if(i==0 || i<RC) R[i] = $('RoomAdultCount_'+(i+1)).value +'-'+$('RoomInfantCount_'+(i+1)).value+'-'+$('RoomInfantAge1_'+(i+1)).value+'-'+$('RoomInfantAge2_'+(i+1)).value;
	                else R[i] = '';
	            }
	            ClearSession = true;
	            location.href = 'Search.aspx?P='+encodeURIComponent($('City').value)+
	                                '&D='+$('CheckInDate').value.split('T')[0]+
	                                '&N='+$('Nights').value+
	                                '&R='+RC+
	                                '&R1='+R[0]+
	                                (R[1]!=''?'&R2='+R[1]:'')+
	                                (R[2]!=''?'&R3='+R[2]:'');
	        }
	        else alert(error)
	    }
	    else {try{FiltersToDefauilt()}catch(ex){}}
	}
	
	
	var Display = 
    {   SearchEngLoaded:function()
        {   $('SearchEngLoader').style.display = 'none';
            if($('res_table'))$('res_table').style.display = '';
        },
        ResultsLoading:function()
        {  if(Elems && Elems.pageNext)
            {   Elems.pageNext.style.display = 'none';
                Elems.pagePrev.style.display = 'none';
            }
            $('main_res').style.display = 'none';
            Elems.ResTable.style.display = 'none';
            Elems.ResTableError.style.display = 'none';
	        if($('goToMap')) $('goToMap').style.display = 'none';
            if($('res_info')) $('res_info').style.visibility = '';
            if($('res_table')) $('res_table').style.display = '';
            if(Elems.SelectedItemContainer)Elems.SelectedItemContainer.innerHTML = '';
            
            $('SearchEngLoader').style.display = '';
        },
        ResultsLoaded:function(NoResults)
        {   $('SearchEngLoader').style.display = 'none';
            Elems.ResTableLoader.style.display = 'none';
            Elems.ResTableLoader.innerHTML = '';
            $('main_res').style.display = '';
            
            if(NoResults)
            {   Elems.ResTable.style.display = 'none';
                $('search_buttons_2').style.display = 'none';
                $('search_buttons_1').style.display = '';
                $('advansed_search_form').style.display = 'none';
                $('search_form').style.display = '';
                if($('res_box'))$('res_box').style.display = 'none';
                if($('res_box_loader'))$('res_box_loader').style.display = '';
            }
            else
            {   Elems.ResTable.style.display = '';
                $('search_buttons_1').style.display = 'none';
                $('search_buttons_2').style.display = '';
                $('search_form').style.display = 'none';
                $('advansed_search_form').style.display = '';
                if($('mail_res'))$('mail_res').style.display = '';
                if($('res_box_loader'))$('res_box_loader').style.display = 'none';
                if($('res_box'))$('res_box').style.display = '';
	            if($('goToMap'))$('goToMap').style.display = '';
            }
        },
        ResultsError:function()
        {   Elems.ResTable.style.display = 'none';
            Elems.ResTableLoader.style.display = 'none';
            Elems.ResTableError.style.display = '';
            $('SearchEngLoader').style.display = 'none';
        }
    };
    
	function encodeArray(array)
    {   var str = '', and = '';
	    if(!array || (!array.join && (array.replace || !isNaN(array))))
	    {   if(!array && array!=0)array='';
	        if(!isNaN(array)) str += array+'';
	        else str+='\''+array.replace(/\'/g,'\\\'')+'\'';
	    }
        else 
        {   str += '[';
            for(var i=0;i<array.length;i++)
            {   str += and + encodeArray(array[i]);
                and = ',';
            }
            str += ']';
        }
        return str;
    }
    var start = 0;
    var Images=[];
	function replaceImg(dir)
	{   start +=dir;
	    var length = Images.length;
	    if(start==-1) start = length-1;
	    var imgArr = $('_about_img_cont').getElementsByTagName('img');
        for(var i=0; i<5; i++)
        {   var num = (start+i)%length;
	        imgArr[i].setAttribute('src',Images[num].image.src);
	        imgArr[i].setAttribute('onClick','popup_img('+num+', this)');
	    }
	}
	function putImages()
	{   var myDiv = $('_about_img_cont');
	    myDiv.innerHTML = '';
	    var count = Images.length;
	    for(var i=0;i<count;i++)
	    {   Images[i].image = new Image;
	        Images[i].image.src = 'img.aspx?href='+Images[i].img+'&h=70';
	    }
		$('r_link').style.display = count<=5 ? 'none' : '';
		$('l_link').style.display = count<=5 ? 'none' : '';
	    count = count>5 ? 5 : count;
		for(var i=0; i<count; i++)
		{   var myImg = document.createElement('img');
		    myImg.setAttribute('src',Images[i].image.src);
		    myImg.setAttribute('onClick', 'popup_img('+i+', this)')
		    myImg.style.cursor = 'pointer';
		    myDiv.appendChild(myImg);
		}
	}
	function popup_img(i, el)
	{   $('img_popup').style.visibility = 'hidden';
	    $('img_popup').style.display = '';
	    $('img_popup_img').innerHTML = '<img src="img.aspx?href='+Choice.Images[i].img+'&h=200&w=200" />';
	    //$('img_popup_desc').innerHTML = Choice.Images[i].desc;
	    var position = new Object();
	    position = getPosition(el);
	    var popupH = $('img_popup').offsetHeight;
	    var popupW = $('img_popup').offsetWidth;
	    var theHeight = 0;
        var Scroll = 0;
        if (window.innerHeight) theHeight=window.innerHeight;
        else if (document.documentElement && document.documentElement.clientHeight) theHeight=document.documentElement.clientHeight;
        else if (document.body) theHeight=document.body.clientHeight;
        if (document.documentElement && document.documentElement.scrollTop)Scroll = document.documentElement.scrollTop;
        else if (document.body && document.body.scrollTop)Scroll = document.body.scrollTop;
	    var Correction = (popupH+position.y + 20)-(theHeight+Scroll);
	    if(Correction<=0) Correction = 20;
	    position.y = position.y - Correction;
	    position.x = position.x - (popupW - el.width)/2;
	    $('img_popup').style.left = position.x + 'px';
	    $('img_popup').style.top = position.y + 'px';
	    $('img_popup').style.visibility = '';
	}
	
	
	
	
	var Shems =
    {   Errors:
        {   search: function(N) {return '<h5></h5><div style="text-align:center;font-size:16px;font-weight:bold;padding:0 30px 0 30px;color:#650F6D">'+N+'</div>';},
            popup: function(N) {return '<div style="text-align:center;font-size:16px;font-weight:bold;padding:0 30px 0 30px;color:#650F6D">'+N+'</div>';},
            DETALES: function(N) {return Errors.DETALES+'<br /><a href="#" onclick="Back()">BACK</a>';}
        },
        Loaders:
        {   /*search: function(N) { var html= '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100" height="70" id="ajax/SearchLoader.swf" align="middle">'+
                                            '<param name="allowScriptAccess" value="sameDomain" />'+
                                            '<param name="allowFullScreen" value="false" />'+
                                            '<param name="movie" value="ajax/SearchLoader.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#e6dde1" />	<embed src="ajax/SearchLoader.swf" quality="high" bgcolor="#e6dde1" width="100" height="70" name="ajax/SearchLoader.swf" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />'+
                                            '</object>'; 
            var n=document.createElement('div');n.innerHTML='<table><tbody><tr><td>'+html+'</td></tr></tbody></table>';return n.childNodes.item(0).childNodes.item(0)},
            */
            popup: '<div class="XL b" style="text-align:center;">LOADING <img src="ajax/loading.gif" /></div>',
            section: function(N) { var html='<div class="XL b" style="text-align:center;">LOADING <img src="ajax/loading.gif" /></div>'; var n=document.createElement('div');n.innerHTML='<table><tbody><tr><td>'+html+'</td></tr></tbody></table>';return n.childNodes.item(0).childNodes.item(0)},
            SelectedItem: '<img src="img/banner.gif" />',
            DETALES: '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100" height="70" id="ajax/SearchLoader.swf" align="middle">'+
                    '<param name="allowScriptAccess" value="sameDomain" />'+
                    '<param name="allowFullScreen" value="false" />'+
                    '<param name="movie" value="ajax/SearchLoader.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#e6dde1" />	<embed src="ajax/SearchLoader.swf" quality="high" bgcolor="#e6dde1" width="100" height="70" name="ajax/SearchLoader.swf" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />'+
                    '</object>'
        }
    }
    var Elems = 
    {   PopupContainer:$('popup'),
        PopupLayOut:$('popup').innerHTML.replace(/%7B/gi,'{').replace(/%7D/gi,'}')
    };
    
if(!location.href.match(/choice.aspx/gi))
{   onloadCollection[onloadCollection.length] = function()
    {   //Selects.loadSelects.load($('City'),'City',true,null,true,false,true,true,true,false);
        
        SimpleSelects.Load($('CheckInDateD'), 'Days');
        SimpleSelects.Load($('CheckInDateM'), 'Months');
        SimpleSelects.Load($('CheckInDateY'), 'Years');
        SimpleSelects.Load($('Nights'), 'Nights');
        SimpleSelects.Load($('RoomsCount'), 'Rooms');
        
        SimpleSelects.Load($('RoomAdultCount_1'), 'RoomAdultCount_1');
        SimpleSelects.Load($('RoomAdultCount_2'), 'RoomAdultCount_2');
        SimpleSelects.Load($('RoomAdultCount_3'), 'RoomAdultCount_3');
        
        SimpleSelects.Load($('RoomInfantCount_1'), 'RoomInfantCount');
        SimpleSelects.Load($('RoomInfantCount_2'), 'RoomInfantCount');
        SimpleSelects.Load($('RoomInfantCount_3'), 'RoomInfantCount');
        
        SimpleSelects.Load($('RoomInfantAge1_1'), 'InfantAge');
        SimpleSelects.Load($('RoomInfantAge1_2'), 'InfantAge');
        SimpleSelects.Load($('RoomInfantAge1_3'), 'InfantAge');
        SimpleSelects.Load($('RoomInfantAge2_1'), 'InfantAge');
        SimpleSelects.Load($('RoomInfantAge2_2'), 'InfantAge');
        SimpleSelects.Load($('RoomInfantAge2_3'), 'InfantAge');
        
        Params.params(Fields.Search, 'Set');
        _onchange('RoomsCount');
        _onchange('RoomInfantCount_1');
        _onchange('RoomInfantCount_2');
        _onchange('RoomInfantCount_3');
        Params.params(Fields.Search, 'Set');
        $('SearchEngLoader').style.display = 'none';
         Selects.loadSelects.on_done_load({table:currency},{elem: $('Currency'), arrayName: 'Currency', postAct: null, addToSelect: true, defaultOption: false, deleteOptions: true, manualement: true,load_anyway:false})
        if($('Currency') && $('Currency').value==''){$('Currency_input').value =  default_opts.Currency;$('Currency').value = Selects.getOptionValue(currency, default_opts.Currency);}
        Currency = Selects.getOptionValue(currency, default_opts.Currency);
        
        Selects.Arrays[Selects.ArraysNames.length]=Countries;
        Selects.ArraysNames[Selects.ArraysNames.length]='Country';
        Selects.loadSelects.on_done_load({table:Countries},{elem: $('Country'), arrayName: 'Country', postAct: null, addToSelect: true, no_defaultOption: true, deleteOptions: true, manualement: true,load_anyway:false})
    
        
        CheckOutWrite();
        var SelectsArr = document.getElementsByTagName('input');
        var c = SelectsArr.length;
        for(var i=0;i<c;i++)
        {   SelectsArr[i].setAttribute('autocomplete','off');
        }
    }
}
onunloadCollection[onunloadCollection.length] = function()
{   var SelectsArr = document.getElementsByTagName('input');
    var c = SelectsArr.length;
    for(var i=0;i<c;i++)
    {   SelectsArr[i].setAttribute('autocomplete','on');
    }
}
onloadCollection[onloadCollection.length] = function()
{   var el = $('City');
    el.onclick = function(){el.style.color = '#000';$('City_text').style.color = '#fff';CityPopUp()};
    if(el.value.match(/[א-ת]/)) el.style.direction = 'rtl';
    else el.style.direction = 'ltr';
    if(el.value==default_opts.City) el.style.color = '#666';
    
}
function CityPopUp(close)
{   if(!close)
    {   $('CityPopup').style.display = '';
        //$('Country').value='';
        //$('Location').value='';
        //$('Country_input').value=default_opts.Country;
        //$('Location_input').value='';
        if($('Country').value=='') 
        {   $('Country_input').value=default_opts.Country;
            $('Location_input').value='';
            $('Location').value=''
        }
        $('CityPopup').focus();
    }
    else
    {   if($('Country').value!="" && $('Location').value!="")
        {   var C = $('Country_input').value;
            var c = $('Location_input').value;
            $('City').value = c + ', (' + C + ')';
        }
        if($('Location').value=='') $('Location_input').value=default_opts.Location;
        if($('Country').value=='') $('Location_input').value='';
        $('CityPopup').style.display = 'none';
        //$('Country').value='';
        //$('Location').value='';
        //$('Country_input').value=default_opts.Country;
        //$('Location_input').value='';
    }
}
function CityPopUpSelect(elem)
{   var v = elem.innerHTML.replace(/^\s+/,'').replace(/\s+$/,'');
    $('City').value = v;
    if($('Location').value=='') $('Location_input').value=default_opts.Location;
    if($('Country').value=='') $('Location_input').value='';
    $('CityPopup').style.display = 'none';
}
function LocationsLoad()
{   var v = $('Country').value;
    ajax('ajax/Selects.aspx?T='+(new Date().getTime()),'v='+v,LoadedLocations,LoadedLocationsError)
}
function LoadedLocations(response)
{   Selects.loadSelects.on_done_load(response,{elem: $('Location'), arrayName: 'Locations', postAct: null, addToSelect: true, no_defaultOption: true, deleteOptions: true, manualement: true,load_anyway:true})  
    $('Location_input').value = default_opts.Location;
    $('Location').value = '';
}
function LoadedLocationsError(response)
{   alert(response);
}

