﻿function Do(arg){
    var tbl = document.getElementById(container);
    var couter = parseInt(document.getElementById("Count").value);

    if(arg.Option != 0){
        ClearPricesClass();
        try{tbl.innerHTML = "";}
        catch (e) {tbl.innerText = "";}
    }
    
    HotelsCount = arg.Count;
    for(var i=0; i<arg.Count; i++){
        couter++;

        var row = document.createElement("TR");
        row.id = "book_tr_"+i;
        tbl.appendChild(row);

        var tdPicture  = document.createElement("TD");
        var tdName     = document.createElement("TD");
        var tdClass    = document.createElement("TD");
        var tdProperty = document.createElement("TD");
        var tdPrice    = document.createElement("TD");
        var tdBook     = document.createElement("TD");
        
        row.appendChild(tdPicture);
        row.appendChild(tdName);
        row.appendChild(tdClass);
        row.appendChild(tdProperty);
        row.appendChild(tdPrice);
        row.appendChild(tdBook);
        
        tdPicture.innerHTML     = arg.Items[i].Picture;
//alert("Name:\n" + arg.Items[i].Name);         
        tdName.innerHTML        = arg.Items[i].Name;
        tdClass.innerHTML       = arg.Items[i].Class;
//alert("Property:\n" + arg.Items[i].Property);        
        tdProperty.innerHTML    = arg.Items[i].Property;
        tdPrice.innerHTML       = arg.Items[i].Price;
        tdBook.innerHTML        = arg.Items[i].Book;

        tdPrice.align = "right";
        tdBook.align = "right";
//        tdBook.className = "book";
        tdName.align = "left";
        tdClass.align = "left";
        
    }
    SetProgress(arg.Online, arg.Requested, arg.Total, arg.SearchStatus == "NOAUTO");
    document.getElementById("Count").value = couter;
    document.getElementById("Iteration").value = parseInt(document.getElementById("Iteration").value, 10) + 1;
    document.getElementById("MarqueeHidden").value = arg.Behavior;
    
    SetRange(arg.MinPrice, arg.MaxPrice);
    SetActive(0, arg.Option);
    if(arg.Total == arg.Requested || arg.SearchStatus == "NOAUTO"){
        isALL = true;
        clearTimeout(TimeOutID);
        Clr_Progress();
        //if(arg.SearchStatus == "NOAUTO")
            Show_SearchEmpty(arg.Online, arg.Requested, arg.Total, true);
    }
}

function Show_SearchEmpty(Online, Requested, Total, flag){
    var Empty1 = "";
    var Empty2 = "";
    
    var online = parseInt(document.getElementById("OnlineCount").innerHTML);
    online = online + Online;
        
    if(Total<250){
        if(online>50){Empty1 = SearchEmpty1; Empty2 = SearchEmpty2}
        else if(flag && online<10 && online < 0.5 * Requested) 
        {Empty1 = SearchEmpty5; Empty2 = SearchEmpty6}
    }
    else if(online>40){Empty1 = SearchEmpty3; Empty2 = SearchEmpty4}
    else if(flag && online<10 && online < 0.5 * Requested) 
        {Empty1 = SearchEmpty5; Empty2 = SearchEmpty6}
    
    if(Empty1 != ""){
        $("SearchEmpty").innerHTML =  
            '<h1 style="margin:0px; font-size:18px; font-weight:normal; color:#8c8d75; line-height:24px;">'+
                    Empty1+
            '</h1>'+
            '<p style="margin:0px 0px 10px 0px; font-size:12px; line-height:16px;">'+
                Empty2+
            '</p>';
        $("SearchEmpty").style.display = "block";
    }            
    else $("SearchEmpty").style.display = "none";
}

var isALL = false;

function SetRange(MinPrice, MaxPrice){
    var d = $("MinPrice");
    var v = d.value;
    min = parseFloat(v);
    if(min<0) min = 999999;
    d = $("MaxPrice");
    v = d.value;
    max = parseFloat(v);
    if(max<0) max = 0;
    if(MinPrice < min) $("MinPrice").value = MinPrice;
    else MinPrice = min;
        
    if(MaxPrice > max) $("MaxPrice").value = MaxPrice;
    else MaxPrice = max;
    if(MinPrice>-1 && MaxPrice>-1)
        $("MinMaxPrice").innerHTML = MinPrice+"€ — "+MaxPrice+"€";    
}

function SortTurnOver(el){
    var res;
    if(el.value == "true"){res = "false";el.value = "false";}
    else{res = "true";el.value = "true";}
    return res;
}

function Repl(str){return str.replace(/'/g, "`");}

function BookForm(action,id,run,num){
//alert("SubmitForm(action,id): " + action+ " | " + id +  " | " + num); //$(id).value);
    if(!onClickBook(num)) return;
    document.getElementById("HiddensForSubmit").innerHTML = "";
    if(bookCountryName == "") FillParameters();
       
    if( typeof pageTracker != 'undefined' && pageTracker._trackPageview) 
	pageTracker._trackPageview('/booking/searchpage.htm');
    var ListGuid = $(id).value;
    //Append("ControlForm:_ListGuid",ListGuid);
    Append("ListGuid",ListGuid);
    var StrForSubmit = action;
    if(run=="yes")
        Append("ControlForm:_Run",run);
    //else if(run=="book") StrForSubmit += "?ListGuid="+ListGuid;   
    var VisID = "";
    VisID = $("VisitorID").value;
    
//    if(VisID != "" || pPrice != ""){
//	    StrForSubmit += "?";
//    	if(VisID != "") StrForSubmit += "VisitorID=" + VisID;
//    	if(pPrice != "") StrForSubmit += "&Price=" + pPrice;
//    }	

    document.form1.action = StrForSubmit;
    //
    vs = $("MarqueeHidden").value;
    $("MarqueeHidden").value = "";
    $("__VIEWSTATE").id = "tttt";
    $("tttt").name = "tttt";
    
    //
    document.form1.target = "_blank";
    document.form1.submit();
    document.form1.target = "_self";
    //
    $("MarqueeHidden").value = vs;
    $("tttt").id = "__VIEWSTATE";
    $("__VIEWSTATE").name = "__VIEWSTATE";
}

function SubmitForm(action, pBrand, pPid, pHotelName, pHotelClass, pIndicator, pDeposit, pRatePlanCode, pRoomTypeCode, pAdvanced, pPrice, pRun){
    document.getElementById("HiddensForSubmit").innerHTML = "";
    if(bookCountryName == "") FillParameters();
       
    if( typeof pageTracker != 'undefined' && pageTracker._trackPageview) 
	pageTracker._trackPageview('/booking/searchpage.htm');
   
       
    Append("ControlForm:book_Brand", pBrand);
    Append("ControlForm:book_Pid", pPid);
    Append("ControlForm:book_HotelName", pHotelName);
    Append("ControlForm:book_HotelClass", pHotelClass);
    Append("ControlForm:book_Indicator", pIndicator);
    Append("ControlForm:book_Deposit", pDeposit);
    Append("ControlForm:book_RatePlanCode", pRatePlanCode);
    Append("ControlForm:book_RoomTypeCode", pRoomTypeCode);
    Append("ControlForm:book_Advanced", pAdvanced);
    Append("ControlForm:_CountryName", bookCountryName);
    Append("ControlForm:_CountryCode", bookCountryCode);
    Append("ControlForm:_City", bookCity);
    Append("ControlForm:_ArrivalDate", bookArrivalDate);
    Append("ControlForm:_DepartureDate", bookDepartureDate);
    Append("ControlForm:_RoomType", bookRoomType);
    Append("ControlForm:_AmountOfRooms", bookAmountOfRooms);
    Append("Language", _CurLang);
    Append("ControlForm:_CurrencyCode", "EUR");
    Append("ControlForm:_Price",pPrice);
    Append("ControlForm:_Run",pRun);
    var VisID = "";
    VisID = document.getElementById("VisitorID").value;
    
    var StrForSubmit = action;
    if(VisID != "" || pPrice != ""){
	    StrForSubmit += "?";
    	if(VisID != "") StrForSubmit += "VisitorID=" + VisID;
    	if(pPrice != "") StrForSubmit += "&Price=" + pPrice;
    }	

    document.form1.action = StrForSubmit;
    //
    vs = document.getElementById("MarqueeHidden").value;
    document.getElementById("MarqueeHidden").value = "";
    document.getElementById("__VIEWSTATE").id = "tttt";
    document.getElementById("tttt").name = "tttt";
    
    //
    document.form1.target = "_blank";
    document.form1.submit();
    document.form1.target = "_self";
    //
    document.getElementById("MarqueeHidden").value = vs;
    document.getElementById("tttt").id = "__VIEWSTATE";
    document.getElementById("__VIEWSTATE").name = "__VIEWSTATE";
}

function MoreRates(action, pBrand, pPid, pHotelName, pHotelClass, pIndicator, pDeposit, pRatePlanCode, pRoomTypeCode, pAdvanced, pPrice, pRun)
{
    document.getElementById("HiddensForSubmit").innerHTML = "";
    if(bookCountryName == "")
        FillParameters();
        
    Append("ControlForm:book_Brand", pBrand);
    Append("ControlForm:book_Pid", pPid);
    Append("ControlForm:book_HotelName", pHotelName);
    Append("ControlForm:book_HotelClass", pHotelClass);
    Append("ControlForm:book_Indicator", pIndicator);
    Append("ControlForm:book_Deposit", pDeposit);
    Append("ControlForm:book_RatePlanCode", pRatePlanCode);
    Append("ControlForm:book_RoomTypeCode", pRoomTypeCode);
    Append("ControlForm:book_Advanced", pAdvanced);
    Append("ControlForm:_CountryName", bookCountryName);
    Append("ControlForm:_CountryCode", bookCountryCode);
    Append("ControlForm:_City", bookCity);
    Append("ControlForm:_ArrivalDate", bookArrivalDate);
    Append("ControlForm:_DepartureDate", bookDepartureDate);
    Append("ControlForm:_RoomType", bookRoomType);
    Append("ControlForm:_AmountOfRooms", bookAmountOfRooms);
    Append("Language", _CurLang);
    Append("ControlForm:_CurrencyCode", "EUR");
    Append("ControlForm:_Price",pPrice);
    Append("ControlForm:_Run",pRun);
    var VisID = "";
    VisID = document.getElementById("VisitorID").value;
    
    var StrForSubmit = action;
    if(VisID != "")
        StrForSubmit += "?VisitorID=" + VisID;

    //document.form1.action = StrForSubmit;
    //
    vs = document.getElementById("MarqueeHidden").value;
    document.getElementById("MarqueeHidden").value = "";
    document.getElementById("__VIEWSTATE").id = "tttt";
    document.getElementById("tttt").name = "tttt";
    
var newF = document.createElement("form"); 
newF.action = StrForSubmit;
newF.method = 'GET'; 
newF.target = "_blank";
document.getElementsByTagName('body')[0].appendChild(newF); 
alert("SubmitForm: " + pRun + " " + newF.action);    

newF.submit(); 
}


function Append(HIname, HIvalue){
    var el = document.createElement("input");
    el.type = "hidden";
    el.name = HIname;
    el.value = HIvalue;
    document.getElementById("HiddensForSubmit").appendChild(el);
}



function SetProgress(online, requested, total, flag){
    $("InfoResultLabel").style.display = '';
    $("InfoResult").style.display =  '';
    var aOnline = parseInt(document.getElementById("OnlineCount").innerHTML);
    
    if(isALLHOTELS == 0 && aOnline)
        aOnline = aOnline + online;
    else
        aOnline = online;
    
    $("OnlineCount").innerHTML = String(aOnline);
    $("RequestedCount").innerHTML = String(requested);
    $("TotalCount").innerHTML = String(total);
    
    var OnlineMin = 70;
    var RequestedMin = 110;
    
    var w = $("TotalIMG").offsetWidth; // style.width;   
    var wOnline = total!=0?Math.round(aOnline * w / total):1; // 220
    var wRequested = total!=0?Math.round(requested * w / total):1; // 220

    $("OnlineIMG").style.width = wOnline + "px";//"100%";
    $("RequestedIMG").style.width = wRequested + "px";
    
    Show_SearchEmpty(online, requested, total, flag);
}

function Blink(){}

function SetActive(p, Option){
    BlockForm(true);
    if(p == 0){
        TimeOutID = setTimeout('More(0, false)', TimeOutSec); //*HotelsCount);
        LinksDisabled(false);
    }
    else if(p == 1){
        clearTimeout(TimeOutID);
        switch(Option)
        {
            case 0:
            break;
            case 4:
                document.getElementById("Count").value = "0";
            break;
            default:
                document.getElementById("Count").value = "0";
            break;
        }
        LinksDisabled(true);
    }
}

function LinksDisabled(flag){
    document.getElementById("Can").value = String(flag).toLowerCase();
    return;
    if(flag)
    {
        document.getElementById("OrderPrice").style.color = "#808080";
        document.getElementById("OrderName").style.color = "#808080";
        document.getElementById("OrderClass").style.color = "#808080";
        document.getElementById("MoreBtn").disabled = true;
    }
    else
    {
        document.getElementById("OrderPrice").style.color = "#4f7d9a";
        document.getElementById("OrderName").style.color = "#4f7d9a";
        document.getElementById("OrderClass").style.color = "#4f7d9a";
        document.getElementById("MoreBtn").disabled = false;
    }
}

function Clr()
{
    clearTimeout(TimeOutID);
    ClearPricesClass();
}

function ResetAdvancedOption(){
/*
    $("LikeHotelName").value = "";
    $("HotelClass5").checked = true;
    $("HotelClass4").checked = true;
    $("HotelClass3").checked = true;
    $("HotelClass2").checked = true;
    $("HotelClass1").checked = true;
    $("HotelClass0").checked = true;
    $("FromPrice").value = "0"
    $("ToPrice").value = "any";
*/    
}

function ClearPricesClass()
{
//    document.getElementById("MinPrice").className = "MPrice";
//    document.getElementById("CurrencyMin").className = "MPrice";
//    document.getElementById("MaxPrice").className = "MPrice";
//    document.getElementById("CurrencyMax").className = "MPrice";
}

function IsProcess()
{
    var res = false;
    if(document.getElementById("Can").value == "true")
        res = true;
    else
        res = false;
    return res;
}

function OnTimeOut(arg)
{
    SetActive(0, 0);
    //alert("TimeOut");
}
function OnError(arg)
{
    SetActive(0, 0);
    //alert("Error");
}

function FillParameters()
{
    bookCountryName = document.getElementById("CountryCode").options[document.getElementById("CountryCode").selectedIndex].innerHTML;
    bookCountryCode = document.getElementById("CountryCode").value;
    bookCity = document.getElementById("City").value;
    
    
    $("CountryCity").innerHTML = bookCity + ", " + bookCountryName;
    $("asdCountryCity").innerHTML = bookCity + ", " + bookCountryName;
    
    bookArrivalDate = document.getElementById("ArrivalDate").value;
    bookDepartureDate = document.getElementById("DepartureDate").value;
    
    bookArrivalDate = $("AY").options[$("AY").selectedIndex].value + "-" + 
			  $("AM").options[$("AM").selectedIndex].value + "-" + 
			  $("AD").options[$("AD").selectedIndex].value;

	bookDepartureDate = $("DY").options[$("DY").selectedIndex].value + "-" + 
			  $("DM").options[$("DM").selectedIndex].value + "-" + 
			  $("DD").options[$("DD").selectedIndex].value;	

    $("asdBookDate").innerHTML = 
                $("AD").options[$("AD").selectedIndex].value+ "/" +
                $("AM").options[$("AM").selectedIndex].value + "/" + 
                $("AY").options[$("AY").selectedIndex].value +" - " +
                $("DD").options[$("DD").selectedIndex].value+ "/" +
                $("DM").options[$("DM").selectedIndex].value + "/" + 
                $("DY").options[$("DY").selectedIndex].value;
    
    $("asdHotelName").innerHTML = $("LikeHotelName").value;
    
    bookRoomType = document.getElementById("RoomType").options[document.getElementById("RoomType").selectedIndex].value;
    bookAmountOfRooms = document.getElementById("AmountOfRooms").options[document.getElementById("AmountOfRooms").selectedIndex].value;
    $("asdRoomType").innerHTML = bookRoomType;
    $("asdAmountOfRooms").innerHTML = bookAmountOfRooms;
    
    var s = "";
    if($("HotelClass5").checked) s += "x5"; // "<img src='images/star.gif'/>5";
    if($("HotelClass4").checked) s += (s!=""?", ":"")+"x4"; // "<img src='images/star.gif'/>4";
    if($("HotelClass3").checked) s += (s!=""?", ":"")+"x3"; // "<img src='images/star.gif'/>3";
    if($("HotelClass2").checked) s += (s!=""?", ":"")+"x2"; // "<img src='images/star.gif'/>2";
    if($("HotelClass1").checked) s += (s!=""?", ":"")+"x1"; // "<img src='images/star.gif'/>1";
    // if($("HotelClass0").checked) s += "<img src="images/star.gif"/>5";
    
    $("asdRatingHotel").innerHTML = s;
}

function SetAdvanvced(flag)
{
    /*var db;
    var tdclass;
    //var tdheight;
    var displayvar;
    if(gClientIsIE)displayvar="block";else displayvar="table-row";
    
    if(document.getElementById("TRPrices").style.display==displayvar)
    {
        db = "none";
        tdclass = "BoldAdvanced";
        //tdheight = "210px";
    }
    else
    {
        db = displayvar;
        tdclass = "Bold";
        //tdheight = "420px";
    }
    if(flag == true)
    {
        db = "none";
        tdclass = "BoldAdvanced";
    }
    */
    //document.getElementById("TDStatus").style.height = tdheight;
    //if(document.getElementById("TRAddToPrice"))
        //document.getElementById("TRAddToPrice").style.display = db;
    //document.getElementById("TRPrices").style.display = db;
    
    //document.getElementById("TRHotelName").style.display = db;
    //document.getElementById("TRHotelClasses").style.display = db;
    //document.getElementById("TDAdvanced").className = tdclass;
//    document.getElementById('showeadvs').style.display="none";
}

var bookCountryName = "";
var bookCountryCode = "";
var bookCity = "";
var bookArrivalDate = "";
var bookDepartureDate = "";
var bookRoomType = "";
var bookAmountOfRooms = "";

