﻿//Browser checks
var	cal_bPageLoaded=false;
var	ie=document.all;
var	dom=document.getElementById;
var	ns4=document.layers;

var	cal_today =	new	Date();
var	cal_dateNow	 = cal_today.getDate();
var	cal_monthNow = cal_today.getMonth();
var	cal_yearNow	 = cal_today.getFullYear();
var flagCheckOutChange=false;

function BuildCheckInDayDropDownList() {
    var checkInDayDropDown = document.getElementById('ddlCheckInDay');
    var checkInMonthDropDown = document.getElementById('ddlCheckInMonthYear');
    BuildDayDropDownList(checkInDayDropDown, checkInMonthDropDown)
    changeCheckInDay();
}
	
function BuildCheckOutDayDropDownList() {
    var checkOutDayDropDown = document.getElementById('ddlCheckOutDay');
    var checkOutMonthDropDown = document.getElementById('ddlCheckOutMonthYear');
    BuildDayDropDownList(checkOutDayDropDown, checkOutMonthDropDown);
    flagCheckOutChange=true;
}

function BuildDayDropDownList(dayDropDown, monthDropDown) {
    var preSelectedDay=cal_dateNow;
    var hasdate=true;
    if (dayDropDown.length>0)
        preSelectedDay=dayDropDown.options[dayDropDown.selectedIndex].value;
       
    var chosenMonth = monthDropDown.options[monthDropDown.selectedIndex].value;
    var numberOfDays = numberOfDaysPerMonthHashTable[chosenMonth];
   
    if (numberOfDays==undefined) {
        numberOfDays=31;
        hasdate=false;
        preSelectedDay=1;
    }

    // Clear the drop down list
	dayDropDown.options.length = 0;
	
	var newOption = new Option(txtDay, "");
	dayDropDown.options[0]=newOption;

	for (i=1; i <= numberOfDays; i++ )	{
		var newOption = new Option(fmt00(i), fmt00(i));
		dayDropDown.options[i]=newOption;
	}
	
	// hack the javascript bug!
	if (parseInt(preSelectedDay)==0) {
        preSelectedDay = preSelectedDay.substring(1);
	}

	if (numberOfDays >= parseInt(preSelectedDay) && hasdate) {
	    dayDropDown.selectedIndex=parseInt(preSelectedDay);		    
	} else {
	    if (preSelectedDay>1) {
	        if (preSelectedDay>numberOfDays) 
	            preSelectedDay=numberOfDays;
	            
	        preSelectedDay=numberOfDays;
	        dayDropDown.selectedIndex=preSelectedDay;
	    } else
	        dayDropDown.selectedIndex=0;
	}
}

function changeCheckInDay() {
    var checkInDayDropDown = document.getElementById('ddlCheckInDay');
    var checkOutDayDropDown = document.getElementById('ddlCheckOutDay');
    var checkInMonthDropDown = document.getElementById('ddlCheckInMonthYear');
    var checkOutMonthDropDown = document.getElementById('ddlCheckOutMonthYear');
    
    if (checkInMonthDropDown.selectedIndex==0)
         return;

    var chosenMonth = checkInMonthDropDown.options[checkInMonthDropDown.selectedIndex].value;
    var numberOfDays = numberOfDaysPerMonthHashTable[chosenMonth];
    
    var chosenCheckInDay = parseInt(checkInDayDropDown.options[checkInDayDropDown.selectedIndex].value,10);
    
    if (!chosenCheckInDay) {
        checkInDayDropDown.selectedIndex+=1;
        chosenCheckInDay = 1;
    }
    
    if ((chosenCheckInDay + 3) <= numberOfDays) {
        if (flagCheckOutChange==false || checkInMonthDropDown.selectedIndex>checkOutMonthDropDown.selectedIndex || (checkInMonthDropDown.selectedIndex==checkOutMonthDropDown.selectedIndex && checkInDayDropDown.selectedIndex>checkOutDayDropDown.selectedIndex)) {
            checkOutMonthDropDown.selectedIndex = checkInMonthDropDown.selectedIndex;
            BuildDayDropDownList(checkOutDayDropDown, checkOutMonthDropDown);
            checkOutDayDropDown.selectedIndex = chosenCheckInDay + 3;
        }
    } else {
        if (checkInMonthDropDown.selectedIndex >= 12) {
	        alert("If you wish to book more than 1 year, please contact a customer service agent.");
	        return false;
        }
        else {
            if (flagCheckOutChange==false || checkInMonthDropDown.selectedIndex>checkOutMonthDropDown.selectedIndex || (checkInMonthDropDown.selectedIndex==checkOutMonthDropDown.selectedIndex && checkInDayDropDown.selectedIndex>checkOutDayDropDown.selectedIndex)) {
                checkOutMonthDropDown.selectedIndex = checkInMonthDropDown.selectedIndex + 1;
                BuildDayDropDownList(checkOutDayDropDown, checkOutMonthDropDown);
                var remainingDays = 3 - (numberOfDays - chosenCheckInDay);	        
                checkOutDayDropDown.selectedIndex = remainingDays;
            }
        }
    }
}

// fmt00: pad with a leading zero for numbers between 0 and 9. only for positive integer.
function fmt00(number) {
    var intValue = parseInt(number)
    if (intValue >= 0 && intValue < 10) {
        return "0" + intValue;       
    }
    return intValue
}

// 29 Nov 2007 added cal_offsetyear to support Thai dates
function frmtDate(frmt,d,m,y) {
	var sTmp = frmt;
	sTmp = sTmp.replace("dd",padZero(d));
	sTmp = sTmp.replace("MMMM",cal_monthNames[m]);
	sTmp = sTmp.replace("mmmm",cal_monthNames[m]);
	sTmp = sTmp.replace("MMM",cal_monthOutput[m]);
	sTmp = sTmp.replace("mmm",cal_monthOutput[m]);
	sTmp = sTmp.replace("mm",padZero(m+1));
	sTmp = sTmp.replace("MM",m);
	return sTmp.replace("yyyy",y+cal_offsetyear);
}

function chkEnableDay(chkday,chkmonth,chkyear) {
    //correction when roll over into next year
    if (chkmonth>=12) {
        chkmonth-=12;
        chkyear+=1;
    }
    var retval=true;
    var date = new Date();
    var	dateNow	 = date.getDate();
    var	monthNow = date.getMonth();
    var	yearNow	 = date.getFullYear();
	if (yearNow<1900) { 
	    yearNow+=1900;	
	}
	// Check past
	if ((chkyear-yearNow) <= 0 && (chkmonth<monthNow || (chkmonth==monthNow && chkday<dateNow))) {
	    retval=false;
	}
	// Check more then 1 year
	if (chkyear>yearNow && ((chkmonth>=monthNow && chkday>dateNow) || chkmonth>monthNow)) {
	    retval=false;
	}
	if (chkyear<yearNow || (chkyear-yearNow>1))
	    retval=false;

    return retval;
}

function agoda_doGenDDL(objSource){
    var strTargetAdult='ddlAdult';
    var strTargetChild='ddlChildren';
    var iValue = (objSource.value*1);
    if 	(objSource.value=="99"){return;}

    agoda_doGen(strTargetAdult,iValue,(iValue*4));
    agoda_doGen(strTargetChild,0,(iValue*3));
}

function agoda_doGen(strTargetName, iStart, iEnd){		
    document.getElementById(strTargetName).length=0;
    var oTarget = document.getElementById(strTargetName);
    for(i=iStart;i<=iEnd;i++){		
	    agoda_doCreateOption(i,i,oTarget);
    }
}

function agoda_doCreateOption(strText, strValue, oTarget){
    var newOpt  = new Option(strText,strValue);
    var selLength = oTarget.length;
    oTarget.options[selLength] = newOpt;
}

function updDateCombos(obj,selday,selmonth,selyear) {
    var DayDropDown;
    var MonthDropDown;
    var monthvalue=frmtDate(cal_cmbMonthFormat,selday,selmonth,selyear);
    var dayvalue=frmtDate("dd",selday,selmonth,selyear);
 
    if (obj.id=="arrivaldate") {
        DayDropDown = document.getElementById('ddlCheckInDay');
        MonthDropDown = document.getElementById('ddlCheckInMonthYear');
        MonthDropDown.value=monthvalue;
        BuildCheckInDayDropDownList();
        // first need to set and build month before we can set the day
        DayDropDown.value=dayvalue;
        changeCheckInDay();
    } else {
        DayDropDown = document.getElementById('ddlCheckOutDay');
        MonthDropDown = document.getElementById('ddlCheckOutMonthYear');
        MonthDropDown.value=monthvalue;
        BuildCheckOutDayDropDownList();
        // first need to set and build month before we can set the day
        DayDropDown.value=dayvalue;
        flagCheckOutChange=true;
   }
   SetDateValues();
}

// Set Hidden Values based on combo values
function SetDateValues() {
    var checkInDayDropDown = document.getElementById("ddlCheckInDay");
    var checkOutDayDropDown = document.getElementById("ddlCheckOutDay");
    var checkInMonthDropDown = document.getElementById("ddlCheckInMonthYear");
    var checkOutMonthDropDown = document.getElementById("ddlCheckOutMonthYear");

    if (checkInMonthDropDown.selectedIndex>0) {   
        var MonthYearIn = getDateFromDropDown(checkInMonthDropDown.selectedIndex);
        var checkInDateString = MonthYearIn.replace("dd",checkInDayDropDown.value);
        document.getElementById("arrivaldate").value=checkInDateString;
    }
   
    if (checkOutMonthDropDown.selectedIndex>0) {   
        var MonthYearOut = getDateFromDropDown(checkOutMonthDropDown.selectedIndex);
        var checkOutDateString = MonthYearOut.replace("dd",checkOutDayDropDown.value);
        document.getElementById("departdate").value=checkOutDateString;
    } else if (checkOutMonthDropDown.length==0) {
        BuildDayDropDownList(checkOutDayDropDown, checkOutMonthDropDown);
    }
}

function getDateFromDropDown(index){
    var arr = new Array();
    var now = new Date();
    for(i=0; i<13; i++){
        now.setDate(1);
        // Correction on 20-Feb-2007 for Opera and other browsers that return for getYear the number of years since 1900 
        // instead of the year itself!
        var m = now.getMonth();
        var y = now.getYear();
        if (y<1900)
            y+=1900;

        var str = (m+1)+"/dd/"+y+"\n";
        arr.push(str);
        
        now.setMonth(++m);
        if(m > 11){
            m = 0+1;
            now.setYear(++y);
            now.setMonth(0);
        }
    }

    if(arr.length>0){
        if(index <= arr.length){
            return arr[index-1];
        }else{
            return "";
        }
    }else{
        return "";
    }
} 

function agoda_doSearch() {
    var checkInDayDropDown = document.getElementById("ddlCheckInDay");
    var checkOutDayDropDown = document.getElementById("ddlCheckOutDay");
    var checkInMonthDropDown = document.getElementById("ddlCheckInMonthYear");
    var checkOutMonthDropDown = document.getElementById("ddlCheckOutMonthYear");
    // Check the Country input
    if(document.getElementById("ddlSelectCountry").value == 0) {
	    alert(txtErrorCountry);
	    return false;
    }
    if(document.getElementById("ddlSelectCity").value==0 && (checkInMonthDropDown.selectedIndex!=0 || checkOutMonthDropDown.selectedIndex!=0)) {
	    alert(txtErrorCity);
	    return false;
    }
    if ((checkInMonthDropDown.selectedIndex!=0 && checkInDayDropDown.selectedIndex==0) || (checkOutMonthDropDown.selectedIndex!=0 && checkOutDayDropDown.selectedIndex==0)) {
	    alert(txtErrorDate);
	    return false;
    }
    
    if (checkInMonthDropDown.selectedIndex==0 && checkOutMonthDropDown.selectedIndex==0) {
        document.getElementById("arrivaldate").value=""; 
        document.getElementById("departdate").value="";
        alert(txtErrorDate);
        return false;
    }  else if (checkInMonthDropDown.selectedIndex==0) {
        document.getElementById("arrivaldate").value=""; 
        alert(txtErrorNoArrival);
        return false;
    } else if (checkOutMonthDropDown.selectedIndex==0) {
        document.getElementById("departdate").value="";
        alert(txtErrorNoDepart);
        return false;
    }
    
    var checkInDateString=document.getElementById("arrivaldate").value;
    var checkOutDateString=document.getElementById("departdate").value;
    
    var checkInDate = new Date();
    checkInDate.setTime(Date.parse(checkInDateString));
    var checkOutDate = new Date();
    checkOutDate.setTime(Date.parse(checkOutDateString));

    var now = new Date();
    if (daysElapsed(checkInDate, now) < 0) {
	    alert(txtErrorPast);
	    return false;
    }
    
    if (checkInDate >= checkOutDate) {
	    alert(txtErrorLater);
	    return false;
    }
    
    if (daysElapsed(checkOutDate, checkInDate) > 120){
	    alert(txtErrorDays);
	    return false;
    }
    if (!validateCriteria()) 
	    return false;    

   	var refurl=escape(document.URL);
    var objCID=document.getElementById("agoda_CID").value
    var countryID=document.getElementById("ddlSelectCountry").value;
    var cityID=document.getElementById("ddlSelectCity").value;
    var hotelID=document.getElementById("ddlSelectHotel").value;
	var room = document.getElementById("ddlRoom").value;
	var adults = document.getElementById("ddlAdult").value;
	var children = document.getElementById("ddlChildren").value;
	var searchtype = document.getElementById("agoda_searchtype").value;
	var backlink = escape(document.getElementById("agoda_backlink").value);
	var headerlink = escape(document.getElementById("agoda_headerlink").value);
	var footerlink = escape(document.getElementById("agoda_footerlink").value);
    var sidebarlink = escape(document.getElementById("agoda_sidebarlink").value);
    if(hotelID=="0") {			        
	    window.location = agoda_posturl+"?waitpage=hotellist&fromInternal=1&cid="+objCID+"&countryID="+countryID+"&cityID="+cityID+"&CheckIn="+checkInDateString+"&CheckOut="+checkOutDateString+"&Rooms="+room+"&Adults="+adults+"&Children="+children+"&searchtype="+searchtype+"&backlink="+backlink+"&refurl="+refurl+"&header="+headerlink+"&footer="+footerlink+"&sidebar="+sidebarlink;
    } else {			        
	    window.location = agoda_posturl+"?waitpage=hotel&fromInternal=1&cid="+objCID+"&countryID="+countryID+"&cityID="+cityID+"&HotelID="+hotelID+"&CheckIn="+checkInDateString+"&CheckOut="+checkOutDateString+"&Rooms="+room+"&Adults="+adults+"&Children="+children+"&searchtype="+searchtype+"&backlink="+backlink+"&refurl="+refurl+"&header="+headerlink+"&footer="+footerlink+"&sidebar="+sidebarlink;
    }
    return true;
}

function validateCriteria() {
	var room = document.getElementById("ddlRoom").value;
	var adults = document.getElementById("ddlAdult").value;
	var children = document.getElementById("ddlChildren").value;
	
	// IF ROOM, ADULTS OR CHILDREN INCLUDE STRING "+"
	if(room == "99") {
		alert(txtMore3Rooms);
		return false;
	} else if ((adults*1) + (children*1) > (room*4)) {
		alert(txtAdultChildMax);
		return false;
    } else if ( (((adults*1) + (children*1)) / (room*1)) == 4) {
		if (confirm(txtRoomsMoreGuest)) {
			return true;	
		}else{
			return false;
		}
	} else {
		return true;
	}
}

function daysElapsed(date1, date2) {
    var difference = Date.UTC(date1.getFullYear(),date1.getMonth(),date1.getDate(),0,0,0)-Date.UTC(date2.getFullYear(),date2.getMonth(),date2.getDate(),0,0,0);
    // difference is in milliseconds, neeed to get number of days instead.
    return difference/1000/60/60/24;
}
