﻿
function CalcDeparture(oCalendar, newValue, oEvent){
//debugger;
	var dtDeparture = new Date();
	var oDeparture = GetControlByID("wdcDeparture", true);
	if (!oDeparture) return false;

	var oNights = GetControlByID("wcNights", false);
	if (!oNights) return false;
	var iNights = oNights.value;

	var msPerDay = 24 * 60 * 60 * 1000;
	dtDeparture.setTime(newValue.getTime() + (iNights * msPerDay));
	oDeparture.setValue(dtDeparture);
	
	document.getElementById('<%=lblSearchCriteria.ClientID%>').innerText = 'Search for availability on these dates.';
	}

function SetDeparture(webComboId){
	var dtArrival = new Date();
	var oArrival = GetControlByID("wdcArrival", true);
	if (!oArrival) return false;

	var dtDeparture = new Date();
	var oDeparture = GetControlByID("wdcDeparture", true);
	if (!oDeparture) return false;

	var oNights = GetControlByID("wcNights", false);
	if (!oNights) return false;
	var iNights = oNights.value;

	var msPerDay = 24 * 60 * 60 * 1000;
	dtArrival.setTime(oArrival.getValue());
	if (isNaN(dtArrival)) return false;
	dtDeparture.setTime(dtArrival.getTime() + (iNights * msPerDay));
	oDeparture.setValue(dtDeparture);
}
function SetNights(oCalendar, newValue, oEvent){
	var dtArrival = new Date();
	var oArrival = GetControlByID("wdcArrival", true);
	if (!oArrival) return false;

	var dtOldDeparture = new Date();
	var dtDeparture = new Date();
	var oDeparture = GetControlByID("wdcDeparture", true);
	if (!oDeparture) return false;

	var msPerDay = 24 * 60 * 60 * 1000;
	dtArrival.setTime(oArrival.getValue());
	dtDeparture.setTime(newValue.getTime());
	dtOldDeparture.setTime(oDeparture.getValue());

	if (dtDeparture < dtArrival) {
		alert ("Departure date cannot be prior to Arrival date");
		oEvent.cancel = true;
	} else {
		var iNights = (dtDeparture.getTime() - dtArrival.getTime()) / msPerDay;
		var oNights = GetControlByID("wcNights", false);
		if (!oNights) return false;
		oNights.value = Math.round(iNights);
	};
}


function GetControlByID(sName, bCombo){
	var ctl
	if (bCombo){
		ctl = igdrp_getComboById(sName);
	} else {
		ctl = document.getElementById(sName);
	}; 
	if (ctl) return ctl;

	//did not find the control by typical name
	var allElms = document.body.getElementsByTagName("*");
	for (var c=0; c< allElms.length; c++){
		var itm = allElms[c];
		if (itm.id){
			if (itm.id.length > sName.length){
				if (itm.id.substring(itm.id.length-sName.length).toLowerCase() == sName.toLowerCase()){
					if (bCombo){
						ctl = igdrp_getComboById(itm.id);
						if (ctl) return ctl;
				  } else {
 						return itm;
					};
				};
			};
		};
	};
	
	return null;
}


//cancels postback if hide room type choices & hide room number choices are configured
function wcLocations_AfterSelectChange(webComboID)
{
return true;
}

//when an image is selected, "click" the popup anchor tag 
function WebImageViewer1_ImageClick(imageViewer, evntArgs) {
    var item = evntArgs.getImageItem();
    var key = item.get_key();
    if (key) {
        document.getElementById("popupImage").onclick();
    }
}

//Set the href and onclick attributes on the popup anchor tag  
function WebImageViewer1_ImageMouseOver(imageViewer, evntArgs) {
    var item = evntArgs.getImageItem();
    var key = item.get_key();
    if (key) {
        // Resolve the javascript array that we stored on the server, 
        // and used that to populate fields from the client.  large image url, width, height
        key = Sys.Serialization.JavaScriptSerializer.deserialize(key);
        document.getElementById("popupImage").href = key[0];
        document.getElementById("popupImage").onclick = function() { return show_hide_centered_box(this, key[1], key[2], '2px solid'); };
    }



}


//Functions for RoomsCalendar.aspx page:
var arrival, departure, selectedroom;
function cellSelectChanged(sender, args) {
    // debugger;
    var cells, item, colkey, rowidx, room;

    cells = args.getSelectedCells();
    if (cells == null || cells.get_length() == 0) {
        return;
    }

    //single cell selection:
    item = cells.getItem(0);
    colkey = item.get_column(0).get_key();

    //don't change background color of non-date cells
    if (colkey == "RoomLink") {
        item.get_element().className = "infoLeft";
        return;
    }
    else if (isNaN(colkey.substring(0, 1))) {
        item.get_element().className = "infoCenter";
        return;
    }

    if (item.get_value() == "x") {
        //not available: what to do - alert keeps selecting more cells
        alert("Room is not available on selected date.");
        selectedroom = null;
        arrival = null;
        departure = null;
        return;
    }

    //which room are we working with:
    room = item.get_row().get_cellByColumnKey("Number").get_value();
    if (selectedroom == null) {
        selectedroom = room;
    }
    else {
        //has selected room changed:
        if (selectedroom != room) {
            selectedroom = room;
            arrival = null;
            departure = null;
        }
    }

    if (arrival == null && departure == null) {
        //nothing selected yet:
        arrival = colkey;
    }
    else {
        if (arrival != null && departure == null) {
            //only arrival has been selected
            departure = colkey;
            // call server method:
            //not working yet, if put back in need to enable page methods on scriptmanager
            //PageMethods.SetSelections(room, arrival, departure, onSuccess, onFail);
        }
        else {
            //both have been selected
        }
    }

    //            var item, colkey, rowidx;
    //            var idx = 0;
    //            
    //            for (idx = 0; idx < cells.get_length() - 1; idx++) {
    //                item = cells.getItem(idx);
    //                colkey = item.get_column().get_key();

    //                if (colkey == "RoomLabel") {
    //                    continue;
    //                }
    //                
    //               
    //                if (selectedrow == null) {
    //                   selectedrow = rowidx;
    //                }
    //                
    //             
    //                if (item.get_value() == "x") {
    //                    //what to do - alert keeps selecting more cells
    //                    return;
    //                }
    //                else {
    //                   var room = item.get_row().get_cellByColumnKey("Number").get_value();
    //                   if (room != null) {
    //                         // call server method:
    //                         PageMethods.SetSelections(room, colkey, onSuccess, onFail);
    //                   }
    //                           
    //               }
    //            } //end for

} // end cellSelectChanged

function onSuccess(result, userContext, methodName) {
}

function onFail(error, userContext, methodName) {
    alert("an error occurred");
}

