﻿// JScript File

function OpenCal(pathtoroot, ControlId)
{
    var Element = document.getElementById(ControlId);
    var OriginalElement = document.getElementById(ControlId);
    var coordinates = GetPosition(Element);
    xPos = posLeft() + coordinates[0];
    yPos = posTop() + coordinates[1];
    if (document.all)
    {
        xPos = window.screenLeft + xPos;
        yPos = window.screenTop + yPos;
    }
    else if (window.screenX != null)
    {
        xPos = window.screenX + xPos;
        yPos = window.screenY + yPos;
    }
    xPos += OriginalElement.clientWidth + 40;
    
	var StartDate = Element.value;
	calwin=window.open(pathtoroot+'UserControls/Calendar.aspx?ControlId=' + ControlId + '&date=' + StartDate,'calwin','left=' + xPos + ',top=' + yPos + ',height=240,width=240,scrollbars=no,resizable=yes');
	calwin.focus();
}

function setFieldOnOpenerForm(ControlId, val)
{
    window.opener.document.getElementById(ControlId).value = val;
}

function ShowHide(curobj, cid, cid2, PathToRoot)
{
    ShowHideWithImage(curobj, cid, cid2,  PathToRoot,'ArrowUp.gif', 'ArrowDown.gif');
}

function ShowHideWithImage(curobj, cid, cid2, PathToRoot, UpImage, DownImage)
{
    ElementToToggle = document.getElementById(cid);
    HiddenElement = document.getElementById(cid2);
    if(ElementToToggle != null)
    {
        if(ElementToToggle.style.display=="none")
        {
            ElementToToggle.style.display="block";
            curobj.src= PathToRoot + "PublicResources/Images/" + DownImage;
        }
        else
        {
            ElementToToggle.style.display="none";
            curobj.src= PathToRoot + "PublicResources/Images/" + UpImage;
        } 
    }
    if(HiddenElement != null)
    {
        if(HiddenElement.style.display=="none")
        {
            HiddenElement.style.display="block";
        }
        else
        {
            HiddenElement.style.display="none";
        } 
    }
}

function GetPosition(Element)
{
    var coordinates=new Array(2)
    var xPos = 0;
    var yPos = 0;
    while( Element != null )
    {
        yPos += Element.offsetTop;
        xPos += Element.offsetLeft;
        Element = Element.offsetParent;
    }
    coordinates[0] = xPos;
    coordinates[1] = yPos;
    
    return coordinates;
}


function RepositionElement(Element, IdOfElementToBeMoved, MoveLeft, MoveTop)
{
    var browser = navigator.appName;
    var xPos = 0;
    var yPos = 0;
    var coordinates = GetPosition(Element);
    xPos = coordinates[0] + MoveLeft;
    yPos = coordinates[1] + MoveTop;
    ElementToBeMoved = document.getElementById(IdOfElementToBeMoved);
    if (browser=="Netscape")
    {
        ElementToBeMoved.style.left = xPos + 'px';
        ElementToBeMoved.style.top = yPos + 'px';
    }
    else if (browser=="Microsoft Internet Explorer")
    {
        ElementToBeMoved.style.posLeft =  xPos;
        ElementToBeMoved.style.posTop = yPos;
    }
    window.scroll(xPos,yPos)
    //alert(xPos + " | " + yPos);
}

function AddTitles(DropDown, TitleList, OffSet)
{
    iLoop = OffSet;
    for (x in TitleList)
    {
        var Title = TitleList[x];
        DropDown.options[iLoop].title = Title;
        iLoop++;
    }
    return true;
}

function ShowOverLib(Element, IdOfElementToBeMoved, Text)
{
    RepositionElement(Element, IdOfElementToBeMoved, 30, 0);
    ElementToBeMoved = document.getElementById(IdOfElementToBeMoved)
    ElementToBeMoved.innerHTML = Text;
    ElementToBeMoved.style.visibility ="visible";
}

function HideOverLib(IdOfElementToBeMoved)
{
    ElementToBeMoved = document.getElementById(IdOfElementToBeMoved)
    ElementToBeMoved.style.visibility ="hidden";
}

function ChangeTabs(HiddenTabId, DivPreFix, CellPreFix, SelectedTab, TabCount)
{
    for (TabLoop = 1; TabLoop <= TabCount; TabLoop++)
    {
        var TabDiv = document.getElementById(DivPreFix + TabLoop);
        var TabCell = document.getElementById(CellPreFix + TabLoop);
        
        TabCell.className = "";
        if(TabLoop == SelectedTab)
        {
            TabCell.className += " currentTab";
            TabDiv.style.display="block";
            
            document.getElementById(HiddenTabId).Value = TabLoop;
        }
        else
        {
            TabDiv.style.display="none";
        }
        if(TabLoop == TabCount)
        {
            TabCell.className += " last";
        }        
    }
}

function ShowReason(DropDownAction, ControlId, Ids)
{
    Control = document.getElementById(ControlId)
    for (x in Ids)
    {
        var selectedValue = DropDownAction.options[DropDownAction.selectedIndex].value;
        if(Ids[x] == selectedValue)
        {
            Control.style.display="block";
            break;
        }
        else
        {
            Control.style.display="none";
        }
    }
}

function clearDefault(control, defaultValue) {
  if (control.value==defaultValue) control.value = ""
}

function SelectAllCheckboxes(CheckBoxSelectAll, CheckBoxList, Total)
{
    for (var i = 0; i < Total; i++)
    {
        var CheckBox = document.getElementById(CheckBoxList + '_' + i);
        
        if(CheckBox.type=="checkbox")
        {
            CheckBox.checked = CheckBoxSelectAll.checked;
        }
    }
}

function SetDropDownIndex(value, control)
{
    for (f = 0; f < document.forms.length; f++)
    {
        var elements = document.forms[f].elements;
        for (e = 0; e < elements.length; e++)
        {
            if (elements[e].id.indexOf(control) > 0)
            {
                var options = elements[e].options;
                for(o = 0; o < options.length; o++)
                {
                    if(options[o].value == value)
                    {
                        options[o].selected = true;
                    }
                }
            }
        }
    }
}

function mask(e, str,textbox)
{
    var WhichKey = KeyPress(e);
    if (WhichKey != '8' && WhichKey != '46' && WhichKey != '13')
    {
        var Length = str.length;
        
        if(Length == 2)
        {
            str += "/";
    	    textbox.value = str
        }
        else if(Length == 5)
        {
            str += "/20";
    	    textbox.value = str
        }
	}
}

function KeyPress(e)
{
    var keychar
    if(e.keyCode != null) // IE
    {
        keychar = e.keyCode
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keychar = e.which
    }
    return (keychar);
}

function DisableItems(DropDown)
{
    var options = DropDown.options;
    for(o = 0; o < options.length; o++)
    {
        if(options[o].value == "")
        {
            options[o].disabled = true;
        }
    }   
}

function pageWidth()
{
	return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

function pageHeight()
{
    return window.innerHeight != null? window.innerHeight : document.documentElement
        && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
}

function posLeft()
{
    return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement
        && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

function posTop()
{
    return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement
        && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}
function posRight()
{
    return posLeft() + pageWidth();
}

function posBottom()
{
    return posTop() + pageHeight();
}

/* availability tooltip code */

var http_request = false;
YAHOO.namespace("swift.container");

function AJAXRequest(url) {
    http_request = false;
    if (window.XMLHttpRequest)
    {
        // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType)
        {
            http_request.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject)
    {
        // IE
        try
        {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {}
        }
    }

    if (!http_request)
    {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
}


function ShowCostPanel(url, Control)
{
    ControlId = Control.id;
    url += '&rnd=' + Math.random();
    
    AJAXRequest(url);
    
    http_request.onreadystatechange = 
    
    function DoIt()
    {
        if (http_request.readyState == 4)
        {
            if (http_request.status == 200)
            {
                var xmldoc = http_request.responseXML;                
             
                var root = xmldoc.getElementsByTagName('root')[0];
                var data = root.getElementsByTagName('data');
                
                for (var i = 0 ; i < data.length ; i++) 
                {
                    var item = data[i];
                    
                    var dateLine;
                    try
                    {
                        dateLine = item.getAttribute("DateLine");
                    }
                    catch(e)
                    {
                        dateLine = null;
                    }
                    
                    var linen;
                    try
                    {
                        linen = item.getAttribute("Linentowels");
                    }
                    catch (e)
                    {
                        linen = null;
                    }
                    
                    var bookingFee;
                    try
                    {
                        bookingFee = item.getAttribute("Bookingfee");
                    }
                    catch (e)
                    {
                        bookingFee = null;
                    }
                    
                    var heating;
                    try
                    {
                        heating = item.getAttribute("Heating");
                    }
                    catch (e)
                    {
                        heating = null;
                    }
                    
                    var pets;
                    try
                    {
                        pets = item.getAttribute("Pets");
                    }
                    catch (e)
                    {
                        pets = null;
                    }

                    var availabilityDescription;
                    try
                    {
                        availabilityDescription = item.getAttribute("AvailabilityDescription");
                    }
                    catch (e)
                    {
                        availabilityDescription = null;
                    }
                    
                    var availabilityCode;
                    try
                    {
                        availabilityCode = item.getAttribute("AvailabilityCode");
                    }
                    catch (e)
                    {
                        availabilityCode = null;
                    }
                    
                    var rentBand;
                    try
                    {
                        rentBand = item.getAttribute("RentBand");
                    }
                    catch (e)
                    {
                        rentBand = null;
                    }
                    
                    var sleepsNotation;
                    try
                    {
                        sleepsNotation = item.getAttribute("SleepsNotation");
                    }
                    catch (e)
                    {
                        sleepsNotation = null;
                    }
                    
                    var reservationURL;
                    try
                    {
						reservationURL = item.getAttribute("ReservationURL");
					}
					catch (e)
                    {
                        reservationURL = null;
                    }
                    
                    var NodeValue = item.firstChild.nodeValue;
                    
			        // Instantiate a Panel from script
			        if(YAHOO.swift.container.panel2 != null)
			        {
    			        YAHOO.swift.container.panel2.destroy();
			        }
			        
			        var heightVar;
			        var widthVar;
			        if(availabilityCode == 'A')
			        {
   			            if(linen != null && linen != "&pound;0.00")
			            {
	    		            heightVar = "126px";
    		            }
    		            else
    		            {
	    		            heightVar = "92px";
    		            }
			            widthVar = "220px";
                    }
                    else
                    {
                        heightVar = "22px";
			            widthVar = "140px"
                    }
                    
                    var anchorVar = "tl";
                    var contextVar = "br";
                    var coordinates = GetPosition(Control);
                    
                    var screenRight = posRight();
                    var screenBottom = posBottom();
                    
                    if(screenRight < coordinates[0] + 220)
                    {
                        anchorVar = "tr";
                    }
                    if(screenBottom < coordinates[1] + 240)
                    {
                        contextVar = "tl";
                        if(anchorVar == "tr")
                        {
                            anchorVar = "br";
                        }
                        else
                        {
                            anchorVar = "bl";
                        }
                    }

        			YAHOO.swift.container.panel2 = new YAHOO.widget.Panel("panel2", { context:[Control,anchorVar,contextVar], height:heightVar,width:widthVar, constraintoviewport:false, draggable:"true"} ); 
                    YAHOO.swift.container.panel2.setHeader(availabilityDescription);// + "&nbsp;(" + availabilityCode + ")");
                    
   			        if(availabilityCode == 'A')
   			        {
       			        var bodyText = "<table style=\"width:100%;\" cellpadding=2 cellspacing=0>";
       			        if(rentBand != null)
       			        {
                            bodyText += "<tr><td colspan='2'>" + "Rent band&nbsp;" + rentBand + "</td></tr>";
                        }
                            
                        if(dateLine != null)
                        {
                            bodyText += "<tr><td colspan='2'>" + dateLine + "</td></tr>";
                        }

                        bodyText += "<tr><td style='width:15%'>Costs:</td><td class='bold'>" + NodeValue + "</td></tr>";
                        
   			            if(linen != null && linen != "&pound;0.00")
   			            {
   			                bodyText = bodyText + "<tr><td colspan='2' style='text-decoration:underline;'>Included optional costs</td></tr><tr><td>Linen:</td><td>" + linen + " per person per week</td></tr>";
   			            }
                        
                        if(reservationURL != null)
                        {
							bodyText = bodyText + "<tr><td colspan='2'><a style='font-weight: bold; color: red;' href='" + reservationURL + "' target='reservation'>Click here to book...</a></td></tr>";
                        }
                        
                        bodyText += "</table>";
                        YAHOO.swift.container.panel2.setBody(bodyText);
                    }
                    else
                    {
                        YAHOO.swift.container.panel2.setBody("");
                    }
                    			        
			        YAHOO.swift.container.panel2.render("container");	
			        
                    // YAHOO.util.Event.addListener(Control, "mouseout", CloseMyPanel  );
                }
            }
            else
            {
                alert('There was a problem with the request.' + http_request.status);
            }
        }
    }
    ;
    http_request.open('GET', url, true);
    http_request.send(null);
}

function ShowInformationPanel(header, main, Control)
{
    var heightVar;
    var widthVar;
            
    heightVar = "94px";
    widthVar = "220px";
    
    var anchorVar = "tl";
    var contextVar = "br";
    var coordinates = GetPosition(Control);
    
    var screenRight = posRight();
    var screenBottom = posBottom();
    
    if(screenRight < coordinates[0] + 220)
    {
        anchorVar = "tr";
    }
    if(screenBottom < coordinates[1] + 240)
    {
        contextVar = "tl";
        if(anchorVar == "tr")
        {
            anchorVar = "br";
        }
        else
        {
            anchorVar = "bl";
        }
    }

	YAHOO.swift.container.panel2 = new YAHOO.widget.Panel("panel2", { context:[Control,anchorVar,contextVar], height:heightVar,width:widthVar, constraintoviewport:false, draggable:"true"} ); 
    YAHOO.swift.container.panel2.setHeader(header);
    YAHOO.swift.container.panel2.setBody(main);

    YAHOO.swift.container.panel2.render("container");	
    YAHOO.util.Event.addListener(Control, "mouseout", CloseMyPanel  );
}

function CloseMyPanel(e)
{
    if(YAHOO.swift.container.panel2 != null)
    {
        YAHOO.swift.container.panel2.destroy();
        YAHOO.swift.container.panel2 = null;
    }
}
