var miRow=3;
var moCal,mdMin,mdMax,mdSel,mdCur,mbFrom;

var masMonths=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var masDays=new Array("Mon","Tue","Wed","Thu","Fri","Sat","Sun");

var mdFrom,mdTo;
var miNoNights;
var msH="apartment name contains...";

function init(){
        if(navigator.appName.indexOf("Microsoft")>-1){
                init_searchForm();
        }
}

function init_searchForm(){
        var st=controls("st"),sfc;
        var sD="",sM="",iM=0,iY=0;

        mdMin=date();
        if(mdMin.getMonth()-1 == -1){
         maxmonth = 11;
         maxyear = mdMin.getFullYear();
        }else{
         maxmonth = mdMin.getMonth()-1;
         maxyear = mdMin.getFullYear()+1;
        }
        mdMax=new Date(daysInMonth(maxmonth,maxyear)+" "+masMonths[maxmonth]+" "+maxyear);
        mdFrom=new Date(cookieGet("dateFrom",date()));
        mdTo=date();mdTo.setTime(mdFrom.getTime()+(1000*60*60*24*miNoNights));
        miNoNights=cookieGet("noNights",1);

        //create options for drop downs         
        for(var i=1;i<13;i++){
                iM=mdMin.getMonth()+i;iY=mdMin.getFullYear();if(iM>12){iY++;iM=iM-12};
                sM+="<option value='"+iY+"-"+iM+"'>"+masMonths[iM-1]+" "+iY+"</option>";
        }
        for(var i=1;i<=daysInMonth(iM,iY);i++){sD+="<option>"+i+"</option>";}
        
        if(st!=null){
                //full search form
                sfc=st.rows[1].cells[1]
                with(st.insertRow(2)){
                        with(insertCell(0)){className="label";innerHTML="Check-out:";}
                        with(insertCell(1)){align="left";innerHTML="<select id='do' onchange='dateTo_changed()'>"+sD+"</select> <select id='myo' onchange='dateTo_changed()'>"+sM+"</select> <a onclick='javaScript:calendar_popup(mdTo,this,false);'><img src='./images/calender.gif' alt='select your date with the calender' /></a>";}
                }
//              st.insertRow(6).insertCell(0);
//              remRoom(3);remRoom(2);
        }else{sfc=controls("sfc")}
        
        if(sfc!=null){
                //calendar
                moCal=document.createElement("div");
                moCal.className="calendar";
                moCal.style.position="absolute";
                moCal.style.display="none";
                //window.document.body.appendChild(moCal);

                //search date from box
                sfc.innerHTML="<select id='d' name='d' onchange='dateFrom_changed()'>"+sD+"</select> ";
                sfc.innerHTML+="<select id='my' name='my' onchange='dateFrom_changed()'>"+sM+"</select> ";
                sfc.innerHTML+="<a onclick='javaScript:calendar_popup(mdFrom,document.getElementById(\"i\"),true);'><img id='i' class='calendarIcon' src='./images/calender.gif' alt='select your date with the calender' /></a> "
                sfc.innerHTML+="<label for='n'>nights</label><input id='n' name='nn' maxlength='2' size='1' onchange='noNights_changed()' />";
        }

        if(controls("nn")!=null){
                selectSetSelected("d",mdFrom.getDate());
                selectSetSelected("my",mdFrom.getFullYear()+'-'+(mdFrom.getMonth()+1));
                controls("nn").value=checkNights(miNoNights);
                
                dateFrom_changed();
                noNights_changed();
                
                //setControl("h",msH);
        }
}
function init_calendar(){
        if(moCal!=null){window.document.body.appendChild(moCal)}
}

function calendar_show(){
        //show the calendar
        var s="";
        var iDate=0,dDate,iMonth=mdCur.getMonth(),iYear=mdCur.getFullYear();
        var iDay,iDayFirst=new Date("1 "+masMonths[iMonth]+" "+iYear).getDay(),iDays=daysInMonth(iMonth,iYear);
        //headers
        s="<table cellspacing='0'><tr><th>";
        if(firstOfTheMonth(mdCur)>=mdMin){s+="<input type='button' value='&lt;' class='action' onclick='calendar_monthPrev();' />";}
        s+="</th><th class='month' colspan='4'>"+masMonths[mdCur.getMonth()]+" "+iYear+"</th><th>";
        if(mdCur<mdMax){s+="<input type='button' value='&gt;' class='action' onclick='calendar_monthNext();' />";}
        s+="</th><th><input type='button' value='&times;' class='close' title='close this calender' onclick='calendar_close();' /></th></tr>";
        s+="<tr>"
        for(i=0;i<masDays.length;i++){s+="<th class='day'>"+masDays[i]+"</th>"}
        s+="</tr>"
        
        //days
        if(iDayFirst==0){iDayFirst=7}
        for(iWeek=1;iDate<=iDays;iWeek++){
                s+="<tr>";
                for(iDay=1;iDay<=7;iDay++){
                        if(iDate==0){iDate=iDayFirst>iDay*iWeek?0:1}
                        s+="<td>";
                        if(iDate>0&&iDate<=iDays){
                                dDate=new Date(iDate+" "+masMonths[iMonth]+" "+iYear);
                                if(dDate>=mdMin&&dDate<=mdMax){
                                        s+="<input type='button' value='"+iDate+"' onclick='calendar_select("+iDate+");calendar_close();'";                     
                                        if(dDate.getTime()==mdSel.getTime()){s+=" class='selected'";}
                                        s+=" />";
                                }
                                else{s+=iDate}
                                iDate++;
                        }
                        else{s+="&nbsp;";}
                        s+="</td>";
                }
                s+="</tr>";
        }
        s+="</table>";
        moCal.innerHTML=s;
}
function calendar_close(){moCal.style.display="none";mbFrom=null}
function calendar_monthPrev(){mdCur.setMonth(mdCur.getMonth()-1);calendar_show();}
function calendar_monthNext(){mdCur.setMonth(mdCur.getMonth()+1);calendar_show();}
function calendar_popup(selected,e,from){
        var x=elementX(e)+110,y=elementY(e);
        mdSel=new Date(selected);
        mdCur=new Date(firstOfTheMonth(selected));
        mbFrom=from;
        calendar_show();

        //Check for left column
        if(controls("left")!=null){x-=eval(document.body.clientWidth*.2);}
        //position
        with(moCal.style){
                position="absolute";
                left=x;top=y;
                zindex=1000;
                display="inline";
        }               
}
function calendar_select(iDate){
        mdSel=new Date(iDate+" "+masMonths[mdCur.getMonth()]+" "+mdCur.getFullYear());
        if(mbFrom){setFrom(mdSel);setCheckout();}
        else{setTo(mdSel);setNights();}
}

function form_validate(){
  //check for illegal and past dates
  var e="";
        if(window.document.frmSearch.booking_checkin.value == ""){e="Please select the check-in date.\n";}  
        if(window.document.frmSearch.booking_checkout.value == ""){e+="Please select the check-out date.";}
//        console.log(window.document.frmSearch);
        if((e.length==0)||(window.document.frmSearch.apt.value > 0)){
                //Set cookies to remember date from and number of nights
//                cookieSet("dateFrom",mdFrom);
//                cookieSet("noNights",miNoNights);
                return true;
        }
        else{alert(e);return false;}
}

function dateFrom_changed(){
        setFrom(getFrom());
        setCheckout();
}

function dateTo_changed(){
        setTo(getTo());
        setNights();
}

function noNights_changed(){
        getNights();
        setCheckout();
}

function showDate(d,sD,sMY){
        //shows the date passed in the form
        var cD=controls(sD);
        if(cD!=null){
                var d1=firstOfTheMonth(d);iD=d.getDate();
                var iMax=daysInMonth(d1.getMonth(),d1.getFullYear())
                if(iD>iMax){iD=iMax}
                cD.options.length=0;
                for(var i=1;i<=iMax;i++){selectOptionAdd(cD,i)}
                selectSetSelected(sD,iD);
                selectSetSelected(sMY,d.getFullYear()+'-'+(d.getMonth()+1));
        }
}

function setCheckout(){
        //set the checkout date according to the number of nights
        if(!isNaN(miNoNights)){
                setTo(new Date(mdFrom.getTime()+(1000*60*60*24*miNoNights)));
        }else{
                selectSetSelected("do","");
                selectSetSelected("myo","");
        }
}
function setFrom(d){
        mdFrom=new Date(d);
        showDate(mdFrom,"d","my");
}
function getFrom(){
        return(new Date(selectGet("d").text+" "+selectGet("my").text));
}

function setTo(d){
        if(d>mdMax){d=mdMax}
        if(d<=mdFrom){d=new Date(mdFrom.getTime()+(100*60*60*24))}
        mdTo=new Date(d);
        showDate(mdTo,"do","myo");
}
function getTo(){
        if(controls("do")==null){return(mdTo)}
        else{return(new Date(selectGet("do").text+" "+selectGet("myo").text))}
}

function setNights(){
        //set the nights according to the date difference in the controls
        var iDMY=getFrom().getTime();
        var iDMYO=getTo().getTime();
        miNoNights=checkNights(parseInt((iDMYO-iDMY)/(1000*60*60*24)));
        controls("nn").value=miNoNights;
        mdTo=new Date(iDMYO);
}
function getNights(){
        //get the number of nights from the control
        miNoNights=checkNights(parseInt(controls("nn").value));
        controls("nn").value=miNoNights;
}

function checkNights(nn){
        if(nn<1){nn=1;setCheckout()}
        return nn;
}

// Date Handling
function firstOfTheMonth(d){return new Date(1+" "+masMonths[d.getMonth()]+" "+d.getFullYear())}
function daysInMonth(m,y){
        switch(m){
        case 1: return ((y % 4==0 && y % 100 != 0)||y % 400 == 0 ? 29 : 28);
        case 3: case 5: case 8: case 10: return 30;
        default: return 31;
        }
}
function date(){
        var d=new Date();
        return new Date(d.getDate()+" "+masMonths[d.getMonth()]+" "+d.getFullYear());
}

// Room Type Add Remove Functions
function addRoom(){
var st=controls("st");
        miRow++;
        with(st.insertRow(miRow+2)){
                with(insertCell(0)){className="label";}
                insertCell(1).innerHTML=st.rows[3].cells[1].innerHTML.replace(/r[0-9]+/g,"r"+miRow);
        }
        controls("r"+miRow+"t").selectedIndex=0
        setRooms();
}
function remRoom(r){
        if(miRow>1){
                controls("st").deleteRow(r+2);
                miRow--;
                setRooms();
        }
}
function setRooms(){
var st=controls("st");
        for(var r=4;r<miRow+3;r++){
                with(st.rows[r].cells[0]){
                        innerHTML="<input type='checkbox' onclick='javaScript:remRoom("+(r-2)+");' checked title='uncheck to remove this room type from the search' />room type "+(r-2);
                }
        }
        with(st.rows[miRow+3].cells[0]){
                if(miRow<controls("r1t").length){
                        innerHTML="<input type='checkbox' onclick='javaScript:addRoom();' title='check to select an additional room type on this search' />room type "+(miRow+1);
                        className="label";
                }else{
                        innerHTML="";
                }
        }
}
function roomsCheckQuantity(){
        var i=1,c;
        while(i<=50){
                c=controls("rq"+i);
                if(c==null){return(false)}
                else if(parseInt(selectGetSelectedText("rq"+i))>0){return(true)}
                i++;
        }
}
function roomsCheckSelection(){
        var sSel="",sSelAll="";
        for(var i=1;i<=miRow;i++){
                if(parseInt(selectGetSelectedText("r"+i+"q"))>0){
                        sSel=selectGetSelected("r"+i+"t").value+";"
                        if(sSelAll.indexOf(sSel)>=0){return(false)}
                        sSelAll+=sSel
                }
        }
        return(true);
}
