/* script for hotel booking form... must be loaded after form */
function tickCheckBox(el)
{
    if (document.getElementById)
    {
        if (document.getElementById(el))
        {
            document.getElementById(el).checked = true;
        }
    }
    return true;
}


var filaMonth;

function showCalendar (img, cal, dt, frm,m,y,d) {
    if (document.getElementById) {
        var c = document.getElementById(cal);
        var i = document.getElementById(img);
        var f = document.getElementById(frm);
        calendar.calfrm = frm;
        calendar.cal = c;
        calendar.caldt = dt;
        calendar.calf = f;
        var my = f[dt + '_month'].value.split("-");
        y=my[0];m=my[1];d=f[ dt + '_day'].value;
        buildCal(y,m,d);

        document.getElementById("b_calendarInner").style.display="block";
        var imageOffset = jQuery(i).offset();
        var containerOffset = jQuery('div.calendarform').offset();
        jQuery(c).css({
            position:'absolute',
            display: 'block',
            left: imageOffset.left - containerOffset.left + 'px',
            top: imageOffset.top - containerOffset.top + 'px'
        });
    }
}

function closeCal() {
    calendar.cal.style.display='none';
}

function buildCal(y,m,d){
    var daysInMonth=[31,0,31,30,31,30,31,31,30,31,30,31];
    td=new Date();
    if (!y) y = td.getFullYear();
    if (!m) m = td.getMonth()+1;
    if (!d) d = td.getDate;
    var frm = calendar.calfrm;
    var dt = calendar.caldt;

    var mDate = new Date(y, m-1, 1);
    var firstMonthDay = mDate.getDay();
    daysInMonth[1]=(((mDate.getFullYear()%100!=0) && (mDate.getFullYear()%4==0)) || (mDate.getFullYear()%400==0))?29:28;

    var today = (y==td.getFullYear() && m==td.getMonth()+1)? td.getDate() : 0;
    var t='<table class="b_caltable" cellspacing="0"><tr class="b_calHeader">';
    var flm=td.getMonth()+1;var flyr=td.getFullYear();
    for(p=0;p<=11;p++){
        if (flm == m) {
            filaMonth = p;
        }
        flm++; if (flm>12) { flm=1;flyr++ }
    }
    t+='<td colspan="7">';
    if (filaMonth == 0) {
        t+='&nbsp;&lt;&nbsp;';
    }
    else {
        t+='<a href="javascript:prevMonth('+y+','+m+');" title="'+tr.prevMonth+'">&nbsp;&lt;&nbsp;</a>';
    }
    t+='&nbsp;<select name="ym" onchange="goMonth(this.options[this.selectedIndex].value)">';
    var mn=td.getMonth()+1;var yr=td.getFullYear();
    for(n=0;n<=11;n++){
        t+='<option value="' + mn + '"';
        if (mn == m) {
            t+=' selected="selected"';
        }
        t+='>' + months[mn-1] + ' ' + yr +'</option>';
        mn++; if (mn>12) { mn=1;yr++ }
    }
    t+= ' </select>&nbsp;';
    if (filaMonth == 11) {
        t+='&nbsp;&gt;&nbsp;';
    }
    else {
        t+='<a href="javascript:nextMonth('+y+','+m+');" title="' + tr.nextMonth +'">&nbsp;&gt;&nbsp;</a>';
    }
    t+='</td></tr>';
    t+='<tr class="b_calDayNames">';
    for(dn=0;dn<7;dn++){
        var cl = '';
        if ((dn%7==5) || (dn%7 == 6)) cl += ' b_calWeekend';
        t+='<th class="'+cl+'">'+days[dn]+'</th>';
    }
    t+='</tr><tr class="b_calDays">';
    for(i=1;i<=42;i++){
        var x = i - (firstMonthDay+6)%7;
        if (x > daysInMonth[m-1] || x <1) x = '&nbsp;';
        var cl = '';
        var href = 0;
        if ((i%7==0) || (i%7 == 6)) cl += ' b_calWeekend';
        if (x>0){
            var xDay = new Date(y, m-1, x);
            if ((xDay.getFullYear() == y) && (xDay.getMonth()+1 == m)
                    && (xDay.getDate() == d))
            { cl += ' b_calSelected' ; href=1}
            if ((xDay.getFullYear() == td.getFullYear())
                    && (xDay.getMonth() == td.getMonth())
                    && (xDay.getDate() == td.getDate()))
            { cl += ' b_calToday'; href=1;}
            else {
                if (xDay > td){ cl += ' b_calFuture'; href=1; }
                else {
                    if (xDay < td) { cl += ' b_calPast'}
                }
            }
        };
        t+='<td class="'+cl+'">';
        if (href){
            t+='<a href="javascript:pickDate('+y+','+m+','+x+',\''+dt+'\',\''+frm+'\');">'+x+'</a>';
        } else {
            t+=x;
        }
        t+='</td>';
        if(((i)%7==0)&&(i<36)) {
            t+='</tr><tr class="b_calDays">';
        }
    }
    t+='</tr><tr class="b_calClose"><td colspan="7"><a href="javascript:closeCal();">' + tr.closeCalendar + '</a></td></tr></table>';
    document.getElementById("b_calendarInner").innerHTML= t;
}

function prevMonth(y,m) {
    if (new Date(y,m-1,1) < td) return;
    if (m > 1) {m--} else {m = 12; y--};
    buildCal(y,m);
}

function nextMonth(y,m) {
    //    if ((new Date(y,m-1,1) - td) > 1000*3600*24*300) return;
    if (m<12){m++;} else {m=1;y++;}
    if (y > td.getFullYear() && m >= td.getMonth() ) return;
    buildCal(y,m);
}

function goMonth(m){
    var y = td.getFullYear();
    if (m < td.getMonth()+1) y++;
    buildCal(y,m);
}

function pickDate(y,m,d,dt,frm){
    // set form values
    var f = calendar.calf;
    var dt = calendar.caldt;
    f[dt + '_month'].value = y + "-"  + m;
    f[dt + '_day'].value = d;
    tickCheckBox('b_availcheck');
    if (dt == "b_checkin"){
        checkDateOrder(calendar.calfrm, 'b_checkin_day', 'b_checkin_month', 'b_checkout_day', 'b_checkout_month');
    }
    closeCal();
}


function addzero( value )
{
    while( value.length<2 ) value = String("0") + value;
    return value;
}


function checkDateOrder(frm, ci_day, ci_month_year, co_day, co_month_year) {
    if (document.getElementById) {
        var frm = document.getElementById(frm);
        // create date object from checkin values
        // set date to 12:00 to avoid problems with one
        // date being wintertime and the other summertime
        var my = frm[ci_month_year].value.split("-");
        var ci = new Date (my[0], my[1]-1, frm[ci_day].value, 12, 0, 0, 0);

        // create date object from checkout values
        my = frm[co_month_year].value.split("-");
        var co = new Date (my[0], my[1]-1, frm[co_day].value, 12, 0, 0, 0);

        // if checkin date is at or after checkout date,
        // add a day full of milliseconds, and set the
        // selectbox values for checkout date to new value
        if (ci >= co){
            co.setTime(ci.getTime() + 1000 * 60 * 60 * 24);
            frm[co_day].value =  co.getDate();
            var com = co.getMonth()+1;
            frm[co_month_year].value = co.getFullYear() + "-" + com;
        }
    }
}


// <![CDATA[
calendar = new Object();
tr = new Object();
tr.nextMonth = "Next month";
tr.prevMonth = "Previous month";
tr.closeCalendar = "Close calendar";
tr.pressCtlD = "Press control-d or choose bookmarks/add or favorites/add in your browser";
tr.pressCtlP = "Press control-p or choose file/print in your browser";
tr.icons = "/static/affiliate_base/img/";
var months=["January","February","March","April","May","June","July","August","September","October","November","December"];
var days=['Mo','Tu','We','Th','Fr','Sa','Su'];
// ]]>




<!--
    if (document.getElementById) {
        document.write("<style type='text/css'>");
        document.write(".b_blocktoggle {display: none}");
        document.write("</style>");
        var shown = new Array();
    }

function blocktoggle(i) {
    if (document.getElementById) {
        shown[i] = (shown[i]) ? false : true;
        currentdisplay = (shown[i]) ? 'block' : 'none';
        document.getElementById('b_blocktoggle'+i).style.display = currentdisplay;
    }
}

function blockdisplay(i) {
    if (document.getElementById){
        if (document.getElementById("b_blockdisplay"+i)) {
            for ( j = 1; j < 1000; j++ ) {
                if (document.getElementById('b_blockdisplay' + j)) {document.getElementById('b_blockdisplay' + j).style.display = 'none';}
                else {j = 1000;}
            }
            if (i) {document.getElementById("b_blockdisplay"+i).style.display = 'block';}
        }
    }
}   //--> 

function CheckRangeDate()
{
    //check in day, month, year
    var checkin_month_arr = (document.getElementById("b_checkin_month").value).split("-");
    checkin_day = document.getElementById("b_checkin_day").value;
    checkin_month = checkin_month_arr[1];
    checkin_year = checkin_month_arr[0]; 

    //check out day, month, year
    var checkout_month_arr = (document.getElementById("b_checkout_month").value).split("-");
    checkout_day = document.getElementById("b_checkout_day").value;
    checkout_month = checkout_month_arr[1];
    checkout_year = checkout_month_arr[0]; 

    var full_checkin_date = new Date(checkin_year, checkin_month-1, checkin_day);
    var full_checkout_date = new Date(checkout_year, checkout_month-1, checkout_day);

    difference_in_seconds = full_checkout_date - full_checkin_date;

    difference_day = parseInt( difference_in_seconds/86400000 ); //second in a day -> 86400000

    if ((difference_day > 30) || (difference_day <= 0))
    {
        alert("Sorry, please re-check your dates. You have either tried to book for more than 30 days, have specified a check-out date that falls before check-in, or have specified the same date for both check-in and check-out.");
        return false;
    }

}

var currentDate = new Date(); var currentYear = 1900 + currentDate.getYear();
var dailyMS = 24*60*60*1000;
var arrivalDate = new Date(currentDate.getTime());
var departureDate = new Date(currentDate.getTime() + 1*dailyMS);
var arrivalYear = 1900+arrivalDate.getYear(); 
var arrivalMonth = 1+arrivalDate.getMonth(); var arrivalDay = arrivalDate.getDate();
var departureYear = 1900+departureDate.getYear(); 
var departureMonth = 1+departureDate.getMonth(); var departureDay = departureDate.getDate();
var a=document.b_frm;
if ((a.checkin_monthday.selectedIndex == 0) && (a.checkout_monthday.selectedIndex == 0)){
    a.checkin_monthday.options[arrivalDay-1].selected = true;
    a.checkout_monthday.options[departureDay-1].selected = true;
}

var frm = document.getElementById('b_frm');
// create date object from checkin values
// set date to 12:00 to avoid problems with one
// date being wintertime and the other summertime
var my = frm['b_checkin_month'].value.split("-");
var ci = new Date (my[0], my[1]-1, frm['b_checkin_day'].value, 12, 0, 0, 0);

// create date object from checkout values
my = frm['b_checkout_month'].value.split("-");
var co = new Date (my[0], my[1]-1, frm['b_checkout_day'].value, 12, 0, 0, 0);
if (ci >= co){
    co.setTime(ci.getTime() + 1000 * 60 * 60 * 24);
    frm['b_checkout_day'].value =  co.getDate();
    var com = co.getMonth()+1;
    frm['b_checkout_month'].value = co.getFullYear() + "-" + com;
}

blockdisplay(1);
jQuery(function () {
    function add_months() {
    var monthArray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    var today = new Date();
    var month= today.getMonth();
    var year = today.getFullYear()%100;
    var fullYear=today.getFullYear();
    var months = [];
    year=new String(year);
    year=addzero(year);
    var countMonth=month;
    for(var i=0;i<12;i++,countMonth++)
    {
    if(countMonth%12==0 && i>0)
    {
    countMonth=0;
    year++;
    year=new String(year);
    year=addzero(year);
    fullYear++;
    }
    //months.push("<option value=\""+fullYear+"-"+(countMonth+1)+"\">"+monthArray[countMonth]+" '"+year);
    months.push("<option value=\""+fullYear+"-"+(countMonth+1)+"\">"+monthArray[countMonth]+" "+fullYear);
    }
return months.join('');
    }

    jQuery('#b_checkin_month,#b_checkout_month')
        .append(add_months())
        .each(function(){ /* force IE to repaint */
            this.className = this.className;
        });

});

