﻿//<reference path="scripts/jquery-1.3.2.js" />
function TTRemoteSearch(options){
	//this.regionsList = new Array();

	//Initializes form controls' values.
    this.initialize = function(options) {
        this.mergeOptions(options);
        this.populateRegions();
        jQuery("#Target").val(this.regionsPageName);
        jQuery("#frmIBEMainForm").attr('action', this.websiteURL);
        
    };

    this.mergeOptions = function(options) {
        this.departureDateOffset = options.departureDateOffset ? options.departureDateOffset : 0;
        this.returnDateOffset = options.returnDateOffset ? options.returnDateOffset : 30;
        this.regionsPageName = options.regionsPageName ? options.regionsPageName : "Regions";
        this.hotelsPageName = options.hotelsPageName ? options.hotelsPageName : "Hotels";
        this.websiteURL = options.websiteURL;
        this.regionsList = options.regionsList;
    };

	//Populates regions select using values from this.regionsList array.
    this.populateRegions = function(destinationID) {
        var regionsSelect = document.getElementById("Region");
        if (regionsSelect == null) {
            return;
        }
        regionsSelect.length = 0;
        regionsSelect.options[regionsSelect.length] = new Option("Any", "");
        for (var i = 0; i < this.regionsList.length; i++) {
            if (destinationID == null || destinationID == '' || destinationID == this.regionsList[i].destinationID)
                regionsSelect.options[regionsSelect.length] = new Option(this.regionsList[i].regionName, this.regionsList[i].regionID);
        }
    };

	//Selects destination based on regionID. Called when region select value changes.
	this.setSelectedDestination = function(regionID){
		if(regionID=='')
			return;
		var destinationSelect = document.getElementById("Group");
		if(destinationSelect==null)
			return;
		if(regionID==null||regionID==''){
			destinationSelect.selectedIndex = 0;
		}
		var destinationID = -1;
		for (var i = 0; i < this.regionsList.length; i++) {
			if(this.regionsList[i].regionID == regionID){
				destinationID = this.regionsList[i].destinationID;
				break;
			}
		}
		if(destinationID==-1)
			return;
		for(var i=0;i<destinationSelect.length;i++){
			if(destinationSelect.options[i].value==destinationID){
				destinationSelect.selectedIndex = i;
				break;
			}
		}
		jQuery("#Target").val(this.hotelsPageName);
    };
    
    //Adds 'offsetDays' to the 'dateToOffset' date or today's date, if 'dateToOffset' is null
	this.getOffsetDate = function(dateToOffset, offsetDays) {	    
	    if (dateToOffset != "") {
	        var d = dateToOffset;
	    } else {
	        var d = new Date();
	    }
	   	    	    	    
	    d.setDate(d.getDate() + offsetDays);    
	    return d;
	};
};

function GetElementById(id) {
    var element = null;

    if (!document.getElementById) {
        if (document.all)
            element = document.all[id];
        else if (document.layers)
            element = document[arguments[0]];
    }
    else
        element = document.getElementById(id);
    return element;
}


function UpdateDate() {
    var fromDate = jQuery("#FromDate");
    var toDate = jQuery("#ToDate");
    if (fromDate.val() && toDate.val()) {
        var fromDateDP = fromDate.datepicker("getDate");
        var toDateDP = toDate.datepicker("getDate");
    }
    var Dur = jQuery("#Duration");
    var durationDays = GetElementById("DurationDays");
    //alert(Dur.val());
    if (Dur.val()) {
        if (Dur.val().split(';').length > 1 && Dur.val().split(';')[1] != undefined)
            if (Dur.val().split(';')[1] != null) {
            // Set the duration days depending on the duration selected
            durationDays.value = Dur.val().split(';')[1];
            //Update the topdate depending on the duration selected
            toDate.datepicker("setDate", ttRemoteSearch.getOffsetDate(fromDateDP, parseInt(durationDays.value)));
        }
    }
}

// This function is only required when the exact number of days option is available in the duration selection option. 
function CheckDuration(dur) {
    var days = GetElementById("DurationDays");
    if ((days != null) && (days.type == "text")) {
        days.style.height = dur.style.height;
        days.style.margin = "0px";
        if (dur.value == "6" && dur.style.visibility != "hidden" && dur.style.display != "none") {
            dur.style.width = (parseInt(dur.style.width.replace("px", ""), 10) - 27) + "px";
            days.style.width = "20px";
            if (!IsIE())
                days.style.left = parseInt(dur.style.left.replace("px", ""), 10) + parseInt(dur.style.width.replace("px", ""), 10) + "px";
            days.style.visibility = "visible";
            days.style.position = "absolute";
            days.style.display = "inline";
        }
        else {
            if (days.style.width != "0px") {
                days.style.width = "0px";
                days.style.position = "absolute";
                days.style.display = "none";
                days.style.visibility = "hidden";
                dur.style.width = (parseInt(dur.style.width.replace("px", ""), 10) + 27) + "px";
            }
        }
    }
}

// Set todate as selected. 
function UpdateToDate() {
    var fromDate = jQuery("#FromDate");
    var toDate = jQuery("#ToDate");
    if (fromDate.val() && toDate.val()) {
        var fromDateDP = fromDate.datepicker("getDate");
        var toDateDP = toDate.datepicker("getDate");
    }
    toDate.datepicker('setDate', getOffsetDate("", 1));
}


//function UpdateDate() {
//    var fromDate = GetElementById("FromDate");
//    var toDate = GetElementById("ToDate");
//    var tempToDate = null;
//    var duration = GetElementById("Duration");
//    var DurationDays = GetElementById("DurationDays");
//    var DurationNum = 0;
//    if (fromDate != null) {
//        var test = 1;
//        var tmpDate = new Date(); 
//        tempToDate = new Date();
//        tempToDate.setFullYear((parseInt(toDate.value.split('/')[2], 10)), parseInt(toDate.value.split('/')[1], 10) - 1, toDate.value.split('/')[0]);
//        tmpDate.setFullYear((parseInt(fromDate.value.split('/')[2], 10)), parseInt(fromDate.value.split('/')[1], 10) - 1, fromDate.value.split('/')[0]);
//        
//            var offset = window.datesOffset != null ? datesOffset : 30;
//            
//            if ((duration != null) && (duration.options != null)) {
//                if (duration.options[duration.selectedIndex].value.split(';')[1] != undefined) {
//                    if ((DurationDays != null) && (DurationDays != undefined)) {
//                        DurationDays.value = duration.options[duration.selectedIndex].value.split(';')[1];
//                        offset = parseInt(DurationDays.value);
//                    }
//                }
//               
//            }

//            
//            
//            tempToDate.setTime(tmpDate.getTime() + (60 * 60 * 24 * offset * 1000));
//            toDate.value = tempToDate.getDate() + "/" + GetMonth(tempToDate.getMonth()+1) + "/" + tempToDate.getFullYear();
//            //toDate.datepicker('setDate', ttRemoteSearch.getOffsetDate(fromDate.datepicker("getDate"), offset));
//    }
//}

//function GetMonth(mon) {
//    if (mon != null) {
//        if (mon < 10) { return "0" + mon  }
//        else { return mon}
//    }
//}

