﻿var title = {DETALES:'',PHOTO:'photos',MAP:'map'};
var Errors = {DETALES:'Loading Error'}

var Choice =
    {   getRowIDByRID:function(ID)
        {   var html = '';
            for(var i=ROWS.length-1;i>=0;i--)
            {   html += '\n'+i+':'+ROWS[i][Choice.filter_Id_func('ID',ROWS_src)]+'!='+ID;
                if(ROWS[i][Choice.filter_Id_func('ID',ROWS_src)]==ID+'')
                {   return i;
                }
            }
            return -1;
        },
        getSRCRowIDByRID:function(ID)
        {   var html = '';
            for(var i=Choice.SRC_ROWS.length-1;i>=0;i--)
            {   html += '\n'+i+':'+Choice.SRC_ROWS[i][Choice.filter_Id_func('ID',ROWS_src)]+'!='+ID;
                if(Choice.SRC_ROWS[i][Choice.filter_Id_func('ID',ROWS_src)]==ID+'')
                {   return i;
                }
            }
            return -1;
        },
        getRowIDByTempID:function(TempID)
        {   //var html = '';
            var t = Choice.filter_Id_func('TempID',ROWS_src);
            for(var i=ROWS.length-1;i>=0;i--)
            {   //html += '\n'+i+':'+ROWS[i][Choice.filter_Id_func('ID',ROWS_src)]+'!='+ID;
                if(ROWS[i][t]==TempID+'') return i;
            }
            return -1;
        },
        getSRCRowIDByTempID:function(TempID)
        {   //var html = '';
            var t = Choice.filter_Id_func('TempID',ROWS_src);
            for(var i=Choice.SRC_ROWS.length-1;i>=0;i--)
            {   //html += '\n'+i+':'+Choice.SRC_ROWS[i][t]+'!='+TempID;
                if(Choice.SRC_ROWS[i][t]==TempID+'') return i;
            }
            return -1;
        },
        loading: function(url)
        {   Display.ResultsLoading();
            if(Elems && Elems.InfoContainer)Elems.InfoContainer.innerHTML = '';
            if(CheckDate())
                ajax_cached(url, 
                        'namesQuerry='+encodeURIComponent(Params.namesQuerry)+
                        '&paramsQuerry='+encodeURIComponent(Params.paramsQuerry), 
                        this.on_done, 
                        this.on_error,null,true,new Date());
            else
            {   Choice.writeError(ErrorDate);
                Display.ResultsLoaded(true);
            }
        },
        ROWS_Sorted:[],
        SRC_ROWS:[],
        SRC_ROWS_FULL:[],
        Chosen: function(row)
        {   var TempID = Choice.filter_Id_func('TempID');
            var Ch = Choice.filter_Id_func('Choice');
            if(Session.Choiced!='')
            {   for(var i=Session.Choiced.split(',').length-1;i>=0;i--)
                {   if(Session.Choiced.split(',')[i]==row[TempID])
                    {   row[Ch] = 1;
                        return row;
                    }
                }
            }
            row[Ch] = 0;
            return row;
        },
        on_done: function(response, pass_data)
        {   var r = eval(response);
            ROWS_src = r.names;
            ROWS = r.table;
            Choice.ROWS_Sorted = [];
            Choice.SRC_ROWS = [];
            var count = r.table.length;
            for(var i = 0; i < count; i++)
            {   Choice.ROWS_Sorted[i] = Choice.Chosen(r.table[i]);
                Choice.SRC_ROWS[i] = Choice.Chosen(r.table[i]);
                Choice.SRC_ROWS_FULL[i] = Choice.Chosen(r.table[i]);
            }
            if(ROWS_src.length>0&&Choice.ROWS_Sorted[0]!='error') 
            {   INFO = r.info;
                INFO_src = r.info_src;
                LOCATIONS = r.LOCATIONS;
                STARS = r.STARS;
                AvailOnly(Session.isAvailOnly);
                LoadFilters(true);
                Filter(true);
            }
            else 
            {   Choice.writeError(ROWS[2]);
                Display.ResultsLoaded(true);
            }
        },
        GetHtmlOfRowDetales: function(N)
        {   var html = Elems.SelectedItemLayOut;
            if(Currency=='')Currency = 'EUR';
            var Nights = INFO[Choice.filter_Id_func('Nights',INFO_src)];
            var PaxCount = INFO[Choice.filter_Id_func('PaxCount',INFO_src)];
            var RoomsCount = ROWS[N][Choice.filter_Id_func('RoomDescs')][0].length;
            var Price = ROWS[N][Choice.filter_Id_func('Price'+Currency)];
            var PriceDesc = '';
            PriceDesc += ' סה"כ ';
            PriceDesc += ' ל'+(Nights>1?'-' + Nights + ' לילות':'לילה 1');
            PriceDesc += ' ל'+(RoomsCount>1?'-' + RoomsCount + ' חדרים':'חדר 1');
            PriceDesc += ' ל'+(TotalPaxCount>1?'-' + TotalPaxCount + ' אורחים':'אורח 1');
            if(Price)
            {   var count = Price.length; var i=0;var ii = 0;
                for(i=0; i<3;i++)
                {   var display = '';
                    if(ii>=count) display = 'style="display:none"';
                    else
                    {   
                        var basis = ROWS[N][Choice.filter_Id_func('PackBasis')][ii].toLowerCase();
                        basis = basis!='' && basis!='לא קיים' ? ' '+basis:' לינה בלבד';
                        var classDesc = ROWS[N][Choice.filter_Id_func('RoomClassesDesc')][ii].toLowerCase();
                        var Status = ROWS[N][Choice.filter_Id_func('Status')][ii].toLowerCase().toLowerCase();
                        Status = Status=='ok' ? 'באישור מיידי' : 'בהתמנה';
                        if(!Session.isAvailOnly || Status!='')
                        {   html = html.replace('{Price_'+i+'}',Choice.price(Price[ii],Currency, true));
                            html = html.replace('{RoomClassesDesc_'+i+'}',classDesc);
                            html = html.replace('{PackBasis_'+i+'}',basis);
                            html = html.replace('{Status_'+i+'}',Status);
                            html = html.replace('{PriceDesc_'+i+'}',PriceDesc);
                            html = html.replace('{RoomClassesCode_'+i+'}',ROWS[N][Choice.filter_Id_func('RoomClassesCode')][ii])
                            var c = ROWS[N][Choice.filter_Id_func('RoomDescs')][ii].length;
                            var and = '';var rooms = '';
                            for(var j=0;j<c;j++)
                            {   rooms += and + ROWS[N][Choice.filter_Id_func('RoomDescs')][ii][j];
                                and = '<br />';
                            }
                            if(rooms!='') rooms = '<b>פרוט חדרים:</b><br />' + rooms;
                            html = html.replace(new RegExp('{RoomDescs_'+i+'}','g'),rooms.toLowerCase());
                        }
                        else i--;
                        //else if(ii==count-1) display = 'style="display:none"';
                        ii++;
                    }
                    if(display !='')
                    {
                        var reg = new RegExp('(\\<\\!--Class'+i+')([\\W\\w]+)('+i+'--\\>)','i');
                        html = html.replace(reg, '');
                    }
                }
            }
            if(ROWS[N][this.filter_Id_func('Latitude')]!='' && ROWS[N][this.filter_Id_func('Longitude')]!='')
            {   html = html.replace(/\{DETALES_MAP\}/,'OpenMap($(\'res_box\'),\''+ROWS[N][this.filter_Id_func('Latitude')]+'\' ,\''+ROWS[N][this.filter_Id_func('Longitude')]+'\' ,\''+ROWS[N][this.filter_Id_func('Name')].replace("'","\\'")+'\');');
            }
            else
            {   html = html.replace(/\<a.+?\{DETALES_MAP\}.+?\<\/a\>/i,'<a href="http://maps.google.com/maps?hl=he&q='+ROWS[N][this.filter_Id_func('AddressLine')]+', '+CountryName+'" target="_blank" class="toSearchOnMap_btn"></a>');
                
            } //html = html.replace(/({DETALES_MAP}.+?"\s*)/,'$1 style="display:none"')
            /*if(ROWS[N][this.filter_Id_func('Latitude')]=='' || ROWS[N][this.filter_Id_func('Longitude')]=='') 
            {   ROWS[N][this.filter_Id_func('Latitude')] = 37.4419;
                ROWS[N][this.filter_Id_func('Longitude')] = -122.1419;
            }*/
            html = html.replace(/\{DETALES\}/g,"Choice.load_detailes('{ID}','ajax/HotelInfo.aspx','DETALES'," + ($('res_box')?"$('res_box'))":"$('mapInfo'))"));
            //html = html.replace(/{Description}/g,'<span id="infoDescr"></span>');
            
            html = this.get_row(N,html);
            html = html.replace(/\<tr style="display:none"[\W\w\s\S]+?\<\/tr>/ig,'');
            return html;
        },
        ShowRowDetales:function(N)
        {   
            Elems.SelectedItemContainer.innerHTML = Choice.GetHtmlOfRowDetales(N);//this.get_row(N,html);
            //$('InfoPic').onmouseover=function(){GetHotelToolTip(trActived,event);};
            //$('InfoPic').onmouseout=function(){closeToolTip(trActived,event);}
            //$('InfoPic').onmousemove=function(){MoveToolTip(event);}
            this.showN = N;
            /*
            var d = ROWS[N][Choice.filter_Id_func('Description',ROWS_src)];
            if(d=='')
            {   ajax('ajax/HotelInfo.aspx',   'namesQuerry=Name, SupplierCode, ID, SupplierCityCode'+
                '&paramsQuerry=' + 
                encodeURIComponent(ROWS[N][Choice.filter_Id_func('Name',ROWS_src)]+
                ','+ROWS[N][Choice.filter_Id_func('SupplierCode',ROWS_src)]+
                ','+ROWS[N][Choice.filter_Id_func('ID',ROWS_src)]+
                ','+ROWS[N][Choice.filter_Id_func('SupplierCityCode',ROWS_src)]), this.DescrCorrect, this.DescrCorrect,{N:N});
            }
            else if($('infoDescr'))$('infoDescr').innerHTML = d;*/
        },
        showN:0,
        DescrCorrect: function(resp,Obj)
        {   var r = eval(resp);
            if(r.table && r.table.length>0)
            {   if(r.table[0]!='error')
                {   DETALES = r.table;
                    DETALES_src = r.names;
                    var d = DETALES[Choice.filter_Id_func('HotelShortDescription',DETALES_src)];
                    if(d=='') d = DETALES[Choice.filter_Id_func('HotelMainDescription',DETALES_src)];
                    ROWS[Obj.N][Choice.filter_Id_func('Description',ROWS_src)] = d;
                    if(Choice.showN==Obj.N)if($('infoDescr'))$('infoDescr').innerHTML = d;
                }
            }
        },
        WriteInfo:function(preoad)
        {   var html = Elems.InfoLayOut;
            html = html.replace('{CityName}',CityName);
            if(INFO_src.length>0)
            {   Elems.InfoContainer.innerHTML='';
                var name,param;
                for(var i=INFO_src.length-1;i>=0;i--)
                {   name = INFO_src[i];
                    param = INFO[i];
                    if(INFO_src[i].match(/Date/gi)) 
                    {   html = html.replace('{'+INFO_src[i]+'Date}',DateTime.writeDateOnly(param));
                        html = html.replace('{'+INFO_src[i]+'Time}',DateTime.writeTimeOnly(param));
                        if(INFO_src[i].match(/Time/gi))param = DateTime.writeDate(param);
                        else param = DateTime.writeDateOnly(param);
                    }
                    html = html.replace('{'+name+'}', param)
                }
                Elems.InfoContainer.innerHTML = html;
            }
        },
        on_error: function(err_msg, pass_data)
        {   Choice.writeError(err_msg);
        },
        writeError:function(err_msg)
        {   Display.ResultsError();
            Elems.ResTableError.innerHTML = Shems.Errors.search(err_msg);
        },
        showDirection:function()
        {   var dir = Session.Desc ? 'img/strelka_down.gif' : 'img/strelka_up.gif';//V^
            for(var i=0;i<=4;i++)
            {  /* $('ResTable_dir_'+i).style.background = '';
                //$('ResTable_dir_'+i).style.paddingLeft = '0';*/
                var temp = $('ResTable_dir_'+i).innerHTML;
                temp = temp.replace(/<img.+>\s*<i>/gi,'<i>');
                $('ResTable_dir_'+i).innerHTML = temp;
            }
            $('ResTable_dir_'+Session.OrderByID).innerHTML = '<img src="'+dir+'" \/>'+ $('ResTable_dir_'+Session.OrderByID).innerHTML;
            /*
            $('ResTable_dir_'+Session.OrderByID).style.background = 'url('+dir+') no-repeat left center';
			$('ResTable_dir_'+Session.OrderByID).style.paddingLeft = '11px';*/
			
        },
        writeResults:function(order_by, page, dir_elem_id, isLoad)
        {   if(order_by) ROWS = this.Sort(order_by);
            this.Page(page);
            if(dir_elem_id>=0) Session.OrderByID = dir_elem_id;
            this.showDirection();
            if(isIE6())setHoverOnTr();
            var N = this.getRowIDByTempID(trActived);
            if(N<0 || N>=ROWS.length) trActived = ROWS[0][this.filter_Id_func('TempID')];
            trActive(trActived);
        },
        LoadPics:function(url)
        {   var img = new Image;
            img.src = url;
        },
        PicsArr:[],
        LoadPic:function(url)
        {   var img = new Image;
            img.src = url;
            Choice.PicsArr[Choice.PicsArr.length] = img;
        },
        Page:function(page,dir_elem_id,dir)
        {   var tbody = document.createElement('tbody');
            for(var i=Elems.ResTable.childNodes.length-1;i>=0;i--)
            {   Elems.ResTable.removeChild(Elems.ResTable.childNodes.item(i));
            } 
            Elems.ResTable.appendChild(tbody);
            tbody.appendChild(Elems.ResTableTbody_0);
            tbody.appendChild(Elems.ResTableTbody_1);
            
            if(!page)page=0;
            if(page=='+')page = Session.Page+1;
            if(page=='-')page = Session.Page-1;
            if(page>ROWS.length/MaxOnPage) page = Math.floor(ROWS.length/MaxOnPage);
            var start = page*MaxOnPage;
            
            for(var i=start;i<ROWS.length && i<MaxOnPage+start;i++)
            {   var cont = document.createElement('div');
                cont.innerHTML = '<table>'+this.get_row(i, Elems.ResLayOut)+'</table>';
                var r = cont.childNodes.item(0).childNodes.item(0).childNodes.item(0);
                Elems.ResTable.getElementsByTagName('tbody').item(0).appendChild(r);
                Choice.LoadPic('img.aspx?href='+ROWS[i][this.filter_Id_func('Picture')]+'&h=80');
                
                var AccTR = document.createElement('tr');
                var AccTD = document.createElement('td');
                var AccDiv = document.createElement('div');
                AccDiv.innerHTML = '';
                AccDiv.id = 'Accordion_'+r.id;
                AccTD.appendChild(AccDiv);
                AccTD.setAttribute('colSpan','5');
                AccTR.style.display = 'none';
                AccDiv.style.direction = 'ltr';
                AccDiv.style.overflow = 'hidden';
                AccTR.appendChild(AccTD);
                AccTR.id='Accordion_' + r.id + 'Content';
                AccTR.setAttribute('class','AccordionContent');
                AccTD.setAttribute('class','res_box');
                AccTR.setAttribute('className','AccordionContent');
                AccTD.setAttribute('className','res_box');
                Elems.ResTable.getElementsByTagName('tbody').item(0).appendChild(AccTR);
                AccTD.id = 'Accordion_' + r.id + 'Content_td';
            }
            if(page==0)Elems.pagePrev.style.display='none';
            else Elems.pagePrev.style.display='block';
            if(ROWS.length<=(MaxOnPage+start))Elems.pageNext.style.display='none';
            else Elems.pageNext.style.display='block';
            Session.Page = page;
        },
        old_sort_item: null,
        old_sort_desc: false,
        Sort: function(order_by, array, array_src)
        {   var isROWS = false;
            var subArr = false;
            if(!array || array==ROWS) 
            {   var count = Choice.ROWS_Sorted.length;
                array = new Array(count);
                for(var i = 0; i < count; i++) array[i] = Choice.ROWS_Sorted[i];
                array_src = ROWS_src;
                isROWS = true;
            }
            if(!order_by && isROWS) order_by = 'PriceEUR';
            if(order_by == 'PriceEUR')subArr = true; 
            order_by = order_by ? this.filter_Id_func(order_by, array_src) : 0;
            if(Session.OrderBy!=order_by || !isROWS)
            {   if(isROWS)
                {   this.old_sort_item = Session.OrderBy;
                    Session.OrderBy = order_by;
                }
                this.old_sort_desc = Session.Desc;
                Session.Desc = false;
            }
            else Session.Desc = !Session.Desc;/*
            if(isROWS && (this.old_sort_item || this.old_sort_item==0))
            {   var temp = Session.OrderBy;
                var temp2 = Session.Desc;
                Session.OrderBy = this.old_sort_item;
                Session.Desc = this.old_sort_desc;
                array = this.SortFunct(this.old_sort_item, array, array_src);
                Session.OrderBy = temp;
                Session.Desc = temp2;
            }*/
            array = this.SortFunct(order_by, array, array_src, subArr);
            return array;
        },
        Yakhas:function(i,j, subArr)
        {   if(subArr){ i = i[0]; j = j[0];}
            if(!isNaN(parseFloat(i)) && !isNaN(parseFloat(j))) 
            {   i = parseFloat(i);
                j = parseFloat(j);
            }
            if(Session.Desc) {if(i < j) return true;}
            else {if(i > j) return true;}
            return false;
        },
        SortFunct: function(order_by, array, array_src, subArr)
        {   for(var i=0; i<array.length-1; i++)
            {   if(this.Yakhas(array[i][order_by],array[i+1][order_by],subArr))
                {   var b = array[i];
                    array[i] = array[i+1];
                    array[i+1] = b;
                    for(var j=i;j>0;j--)
                    {   if(this.Yakhas(array[j-1][order_by],array[j][order_by],subArr))
                        {   b = array[j];
                            array[j] = array[j-1];
                            array[j-1] = b;
                        }
                        else break;
                    }
                }
            }
            return array;
        },
        currency: function(curr)
        {   var html = '';
            if(curr)
            {   for(var i=curr_sign.length-1; i>=0; i--)
                {   if(currency[i].value==curr)
                    {   html = curr_sign[i].text;
                    }
                }
                if(html == '')
                {   for(var i=currency.length-1; i>=0; i--)
                    {   if(currency[i].value==curr)
                        {   html = currency[i].text;
                        }
                    }
                }
            }
            return html;
        },
        price: function(v, curr, float)
        {   var html = this.currency(curr);
            v = (v!=null?(float?Math.round(parseFloat(v)):Math.round(parseFloat(v)*100)/100):'0')+'';
            if(!float)
            {   var cents = '';
                if(v.split('.').length>1)
                {   cents = v.split('.')[1];
                    if(cents.length<2) cents = v.split('.')[1]+'0';
                }
                else cents = '00';
                v = v.split('.')[0]+'.'+cents;
            }
            html += v;
            return '<span style="direction:ltr">'+html+'</span>';
        },
        CaseCorrect: function(v)
        {   if(v!='' && !v.match(/[a-z]/))
            {   var temp = v.toLowerCase().split(' ');var count = temp.length;v = '';
                for (var j=0;j<count;j++)
                {   v += temp[j].substring(0,1).toUpperCase() + (temp[j].length>1?temp[j].substring(1,temp[j].length):'')+' ';
                }
            }
            v = v.replace(/^(\s*)(.+)(\s*)$/,'$2');
            return v;
        },
        CharsLimiter: function(v,max)
        {   var maxCount = max-2;
            if(v.length>max)
            {   var arr = v.split(' ');
                var c = arr.length, temp = '', and = '';
                for(var i=0;i<c;i++)
                {   temp += and + arr[i].replace(/^(\s*)(.+)(\s*)$/,'$2');
                    and = ' ';
                    if(temp.length<maxCount) v = temp;
                    else
                    {   v += ' ...';
                        break;
                    }
                }
            }
            return v;
        },
        StarRate: function(v)
        {   v = parseFloat(v);
            if(!isNaN(v)) v = v*2;else v = '';
            return v;
        },
        Best: function(v)
        {   return v==0?'<center><div class="iePNG" style="background-image: url(img/best_offer.png); height: 16px; width:17px; margin-right:6px"></div></center>':'';
        },
        AddressLine:function(v, isShort)
        {   var reg = RegExp('(\s*,*\s*'+CountryName+')|(\s*,*\s*'+CityName+')', 'gi');
            v = v.replace(reg, '');
            v = this.CaseCorrect(v);
            if(v.match(/([^a-z\d])$/))v = v.substring(0,v.length-2);
            return v!='' ? (isShort?this.CharsLimiter(v,30).replace(/\s+\.+$/,''):v) + ', ' : '';
        },
        filter_Id_func: function(N, src)
        {   if(!src)src = ROWS_src;
            for(var i=src.length-1;i>=0;i--)
            {   if(src[i]==N) return i;
            }
            return 0;
        },
        get_row: function(N, layout)
        {   var html = layout, v='';
            
            html = html.replace(/{N}/g,N);
            html = html.replace(/{CountryName}/g, CountryName);
            html = html.replace(/{CityName}/g, CityName);
            for(var i=0;i<ROWS_src.length;i++)
            {   if(ROWS[N]!=null && ROWS[N][i]!=null) 
                {   var holder = '{' + ROWS_src[i] + '}';
                    var holderName = ROWS_src[i];
                    
                    if(holderName=='Fichers') 
                    {   array = Array();array = Selects.getArrayByName('Equips');
                        src = Array();src = ROWS[N][Choice.filter_Id_func('Fichers')];
                        var t = '';var and = '';
                        if(src.length>0)
                        {   for(var j=src.length-1;j>=0;j--)
                            {   var n = Selects.getOptionText(array, src[j]);
                                if(n) t += and + n;and = '<br />';
                            }
                        }
                        html = html.replace(holder, t);
                    }
                    else if(holderName=='Picture')
                    {   html = html.replace(/({Picture)(.*?)(})/, '<img src="img.aspx?href='+ROWS[N][i]+'$2" />');
                    }
                    else if(holderName=='AddressLine')
                    {   html = html.replace(/{AddressLineShort}/g,this.AddressLine(ROWS[N][i],true));
                        html = html.replace(RegExp(holder,'g'),this.AddressLine(ROWS[N][i]));
                    }
                    else if(holderName=='Name')
                    {   html = html.replace(RegExp(holder,'g'),this.CaseCorrect(this.CharsLimiter(ROWS[N][i],32)));
                    }
                    else if(holderName=='StarRate')
                    {   html = html.replace(/{StarRate}/g, this.StarRate(ROWS[N][i]));
                    }
                    else if(holderName.indexOf('Price')!=-1&&holderName=='Price'+Currency)
                    {   var  v = parseFloat(ROWS[N][i][0]);
                        html = html.replace(/{Price}/g, this.price(v,Currency, true));
                    }
                    else if(holderName=='Choice')
                    {   var v = ROWS[N][i];
                        html = html.replace(RegExp(holder,'g'), v==1 ? 'checked' : '');
                    }
                    else if(holderName=='Best')
                    {   var v = ROWS[N][i];
                        html = html.replace(RegExp(holder,'g'), this.Best(v));
                    }
                    else html = html.replace(RegExp(holder,'g'),(ROWS[N][i]!='' ? ROWS[N][i] : ''));
                    
                }
            }
            return html;
        },
        on_done_detailes: function(response, Obj)
        {   var r = eval(response);
            var N = Choice.getRowIDByRID(Obj.ID);
            if(N<0) return null;
            var div = Obj.div;
            var popup = Obj.popup;
            var temaName = Obj.temaName;
            var LayOut = Obj.LayOut;
            var ID = Obj.ID;
            var url = Obj.url;
            var postFunct = Obj.postFunct;
            var action = '';
            {   if(r.table && r.table.length>0)
                {   if(r.table[0]=='error')
                    {   DETALES = r.table;
                        if(r.table[1]==0)
                        {   Choice.call_ajax(Obj);
                            return false;
                        }
                        action = 'error';
                    }
                    else 
                    {   DETALES = r.table;
                        DETALES_src = r.names;
                        if(Choice.NowDetales.ID==ID && Choice.NowDetales.temaName==temaName) 
                        action = 'loaded';
                        else action = 'error';
                    }
                }
                else if (Choice.NowDetales.ID==ID && Choice.NowDetales.temaName==temaName) 
                {   action = 'error';
                    if(DETALES=='') DETALES = ['error',0,'Error'];
                    if(r.table[1]==0) 
                    {   Choice.call_ajax(Obj);
                        return false;
                    }
                }
                if(action!='' && Choice.NowDetales.ID==ID && Choice.NowDetales.temaName==temaName)
                {   Choice.write_detales(ID, Choice.write_inner_text(Obj), action, div, LayOut, popup, postFunct);
                }
            }
        },
        write_detales: function(ID, LoadContent, action, div, LayOut,popup,postFunct)
        {   var N = Choice.getRowIDByRID(ID);
            if(N<0) return null;
            if(popup)
            {   var html = '<div style="background-color:white;padding:10px 10px 10px 10px;">' + 
                            '<div id="'+div.id+'_inner">';
                html +=  LoadContent + '</div>'+
                        '</div>';
                LayOut = LayOut.replace('{CONTENT}', html);
                div.innerHTML = Choice.get_row(N,LayOut);
            }
            else
            {   if(LoadContent!=Errors.DETALES)div.innerHTML =  Choice.get_row(N,LayOut);
                if(postFunct)postFunct();
                /*
                var reg = new RegExp('([\\S\\s]*)(<p\\s+id="'+div.id+'_p_0".+?\\/p>)([\\S\\s]*)');
                var html =  LoadContent;
                html = html.replace(reg,'$2');
                html = html.replace(/<p id=.*"\s*>/,'').replace(/<\/p\s*>/,'');
                html = html.replace(/\<br\s*\/?\>/gi,'\n');*/
                //div.innerHTML = html.replace(/\s*\n*\s*$/gi,'').replace(/^\s*\n*\s*/gi,'').replace(/\n+/gi,' <span class="dot">•</span>&nbsp;');
                /*if($(div.id+'_loader'))$(div.id+'_loader').style.display = 'none';
                div.style.display = '';
                */
            }
             
        },
        write_inner_text: function(Obj)
        {   var N = Choice.getRowIDByRID(Obj.ID);
            if(N<0) return null;
            var popup = Obj.popup;
            var div = Obj.div;
            var temaName = Obj.temaName;
            var html = '';
            if(temaName=='DETALES')
            {   var Head = '';
                var Locations = '';
                var Description = '';
                var ShortDescription = '';
                var HotelFacilities = '';
                var RoomFacilities = '';
                Choice.Images = []; //ImageDescs
                var count;
                if(DETALES!='')
                {   if(DETALES[0]!='error')
                    {   Choice.NowDetales.DETALES = [];
                        for(var i=DETALES_src.length-1; i>=0; i--)
                        {   var temp = ''; count = DETALES[i].length;
                            if(DETALES[i]!='' && count>0)
                            {   if(DETALES_src[i]=='Locations')
                                {   for(var j=0;j<count;j++) Locations += '<p'+ (DETALES[i][j].match && !DETALES[i][j].match(/[א-ת]/)?' style="direction:ltr"':' style="direction:rtl"') +'>'+DETALES[i][j]+'</p>';
                                    temp = Locations;
                                }
                                else if(DETALES_src[i]=='HotelMainDescription') {Description = '<p'+ (!DETALES[i].match(/[א-ת]/)?' style="direction:ltr"':' style="direction:rtl"') +'>'+DETALES[i]+'</p>'; temp = Description;}
                                else if(DETALES_src[i]=='HotelShortDescription') {ShortDescription = '<p'+ (!DETALES[i].match(/[א-ת]/)?' style="direction:ltr"':' style="direction:rtl"') +'>'+DETALES[i]+'</p>'; temp = ShortDescription;}
                                else if(DETALES_src[i]=='HotelFacilities')
                                {   for(var j=0;j<count;j++) HotelFacilities += '<p'+ (!DETALES[i][j].match(/[א-ת]/)?' style="direction:ltr"':' style="direction:rtl"') +'>'+DETALES[i][j]+"</p>";
                                    temp = HotelFacilities;
                                }
                                else if(DETALES_src[i]=='RoomFacilities')
                                {   count = DETALES[i].length;
                                    for(var j=0;j<count;j++) RoomFacilities += '<p'+ (!DETALES[i][j].match(/[א-ת]/)?' style="direction:ltr"':' style="direction:rtl"') +'>'+DETALES[i][j]+'</p>';
                                    temp = RoomFacilities;
                                }
                                else if(DETALES_src[i]=='Images')
                                {   for(var j=0;j<count;j++) 
                                    {   if(!Choice.Images[j])Choice.Images[j] = new Object();
                                        Choice.Images[j].img = DETALES[i][j];
                                    }
                                }
                                else if(DETALES_src[i]=='ImageDescs')
                                {   for(var j=0;j<count;j++) 
                                    {   if(!Choice.Images[j])Choice.Images[j] = new Object();
                                        Choice.Images[j].desc = DETALES[i][j];
                                    }
                                }
                            }
                            Choice.NowDetales.DETALES[i] = temp;
                        }
                        if(this.NowDetales.temaName=='DETALES' && popup)
                        {   Head =  '<p style="font-weight:bold;font-size:13px;text-align:right;" class="stars{StarRate}">{Name}</p>'+
                                    '<p style="font-weight:bold;font-size:12px;text-align:right;padding-bottom:8px;">{AddressLine}{CityName}, {CountryName}</p>';
                            Head +=  '<table cellspacing="0" cellpadding="0" style="float:right;margin-left:10px;margin-bottom:5px;border:7px solid #BFD5ED;background-color:#fff;">';
                            MainPhoto = ROWS[N][Choice.filter_Id_func('Picture')];
                            var count = Choice.Images.length;
                            if(count==0) 
                            {   Choice.Images[0] = new Object(); 
                                Choice.Images[0].img = MainPhoto;
                                Choice.Images[0].desc = '';
                                count ++;
                            }
                            count = count>13 ? 13 : count;
                            if(count>1)
                            {   Head += '<tr><td style="vertical-align:middle;border-bottom:7px solid #BFD5ED;background-color:#BFD5ED;"><table cellspacing="0" cellpadding="0"><tr><td>photos:</td>'
                                this.popupImgGalery_temp = 0;
                                for(var i=0;i<count;i++)
                                {   Head += '<td style="padding-left:4px;"><a style="display:'+(i==0?'none':'inline')+'" id="popupImgGalery_link_'+i+'" href="javascript:{Choice.popupImgGalery('+i+');void(0)}">'+(i+1)+'</a>'+
                                            '<span style="display:'+(i==0?'inline':'none')+'" id="popupImgGalery_'+i+'">'+(i+1)+'</span></td>';
                                }
                                Head += '</tr></table></td></tr>'
                            }
                            Head +=         '<tr>'+
                                                '<td height="200" width="220" id="popupPict" align="center" style="padding-top:10px;vertical-align:top;text-align:center;">';
                            if(count>1) Head +=     '<img src="img.aspx?href='+Choice.Images[0].img+'&h=200&w=200" />';
                            else Head +=            '{Picture&h=200&w=200}';
                            Head +=             '</td>'+
                                            '</tr>'+
                                            '<tr>'+
                                                '<td height="30" id="popupPictDesc" style="vertical-align:middle;text-align:center;">';
                            if(count>1) Head +=     Choice.Images[0].desc;
                            Head +=             '</td>'+
                                            '</tr>'+
                                        '</table>';
                            if(Locations!="") Locations = (Locations.match(/[א-ת]/)?'<h3 style="direction:rtl">מיקום:</h3>':'<h3 style="direction:ltr">Location:</h3>') + Locations;
                            if(Description!="") Description = (Description.match(/[א-ת]/)?'<h3 style="direction:rtl">תאור:</h3>':'<h3 style="direction:ltr">About:</h3>') + Description;
                            if(HotelFacilities!="") HotelFacilities = (HotelFacilities.match(/[א-ת]/)?'<h3 style="direction:rtl">במלון</h3>':'<h3 style="direction:ltr">In Hotel:</h3>') + HotelFacilities;
                            if(RoomFacilities!="") RoomFacilities = (RoomFacilities.match(/[א-ת]/)?'<h3 style="direction:rtl">בחדרים:</h3>':'<h3 style="direction:ltr">In Rooms:</h3>') + RoomFacilities;
                            html = Locations + Description + HotelFacilities + RoomFacilities;
                            if(html.match(/[א-ת]/)) html = '<div style="direction:rtl">' + html + '</div>';
                            html = Head + html;
                            
                        }
                    }
                    else if(popup) html = Shems.Errors.popup(Errors.DETALES);
                    else html = Errors.DETALES;//DETALES[2];
                }
                else if(popup) html = Shems.Errors.popup(Errors.DETALES);
                else html = Errors.DETALES;
            }
            return html;
        },
        Images:[],
        popupImgGalery_temp:0,
        popupImgGalery: function(i)
        {   $('popupPict').innerHTML='<img src="img.aspx?href='+Choice.Images[i].img+'&h=200&w=200" />';
            $('popupPictDesc').innerHTML = Choice.Images[i].desc;
            $('popupImgGalery_link_'+i+'').style.display='none';
            $('popupImgGalery_'+i+'').style.display='';
            $('popupImgGalery_'+this.popupImgGalery_temp+'').style.display='none';
            $('popupImgGalery_link_'+this.popupImgGalery_temp+'').style.display='';
            this.popupImgGalery_temp=i;
        },
        load_popup: function(target, div, HideScroll)
        {   div.style.visibility = 'hidden';
            div.style.display = 'block';
            var wCont = $(div.id+'_cont').offsetHeight;
            if(!HideScroll)
            {   if(wCont<100) wCont = 100;
                else if(wCont>300) 
                {   wCont = 300;
                    $(div.id+'_cont').style.overflowY = 'scroll';
                }
                $(div.id+'_cont').style.height = wCont + 'px';
            }
            var w = div.offsetWidth;
            var h = div.offsetHeight;
            var docPos = getPosition(target);
            var y = docPos.y;
            var x = docPos.x;
            var Client = getClientParams();
            var cor = (Client.height+Client.scrollTop) - h - 30;
            if(cor>0 && (cor - y)<0) y = cor;
            if(y<Client.scrollTop) y = Client.scrollTop+5;
            if(Client.width/2<x) x = x - w;
            div.style.left = x+'px';
            div.style.top = y +'px';
            div.style.visibility = '';
            if(dragObjectOld)dragObjectOld.style.zIndex = '';
            div.style.zIndex = '999999';
            dragObjectOld = div;
        },
        on_error_detailes: function(err_msg, Obj)
        {   Choice.write_detales(Obj.ID, Shems.Errors.popup(err_msg), 'error', Obj.div, Obj.LayOut);
        },
        load_detailes: function(ID,url,temaName,target,div,LayOut, postFunct)
        {   var N = Choice.getRowIDByRID(ID);
            if(N<0) return null;
            var popup = false;
            if(!div) 
            {   popup = true;
                div = Elems.PopupContainer;
                LayOut = Elems.PopupLayOut;
                LayOut = LayOut.replace('{TITLE}',eval('title.'+temaName)+' <span style="font-size:12px">'+this.CaseCorrect(this.CharsLimiter(ROWS[N][Choice.filter_Id_func('Name')],50)))+'</span>';
                Choice.load_popup(target, div);
                div.innerHTML = LayOut.replace('{CONTENT}',Shems.Loaders.popup);
            }
            else if(!LayOut)
            {   LayOut = div.innerHTML;
            }
            var Obj = {N:N, ID: ID,div: div,url:url, temaName: temaName,LayOut: LayOut,popup:popup, postFunct:postFunct};
            if(this.NowDetales && this.NowDetales.ID!=ID) this.call_ajax(Obj);
            else this.write_detales(ID, this.write_inner_text(Obj), 'loaded', div, LayOut, popup, postFunct);
        },
        call_ajax: function(Obj)
        {   var N = this.getRowIDByRID(Obj.ID);
            if(N<0) return null;
            this.NowDetales = Obj;
            ajax(Obj.url,   'namesQuerry=Name, SupplierCode, ID, SupplierCityCode, P'+
                        '&paramsQuerry=' + 
                        encodeURIComponent(ROWS[N][Choice.filter_Id_func('Name',ROWS_src)].replace(/,/,'{%2E}')+
                        ','+ROWS[N][Choice.filter_Id_func('SupplierCode',ROWS_src)].replace(/,/,'{%2E}')+
                        ','+ROWS[N][Choice.filter_Id_func('ID',ROWS_src)].replace(/,/,'{%2E}')+
                        ','+ROWS[N][Choice.filter_Id_func('SupplierCityCode',ROWS_src)].replace(/,/,'{%2E}')+
                        ','+P.replace(/,/,'{%2E}')), 
                        this.on_done_detailes, 
                        this.on_error_detailes,Obj);
        },
        NowDetales: Object()
    };
