﻿    function padLeftZero(x){
        var id = "";
        if(x < 10){
            id = "0"+x;
        }
        else{
            id = x;
        }
        return id;
    }
    
    function AmenityHeader(x){
        var strElement = "ctl00_c1_aG_ctl" + padLeftZero(x) + "_divHeader";
        return document.getElementById(strElement);
    }
    
    function AmenityGrid(x){
        var strElement = "ctl00_c1_aG_ctl" + padLeftZero(x) + "_divAmenityGrid";
        return document.getElementById(strElement);
    }
    function MovingBox(x){
        var strElement = "ctl00_c1_aG_ctl" + padLeftZero(x) + "_movingBox";
        return document.getElementById(strElement);
    }
    
    function PlusMinusImage(x){
        var strElement = "ctl00_c1_aG_ctl" + padLeftZero(x) + "_imagePlusMinus";
        return document.getElementById(strElement);
    }

    function showDiv(divAmenityGrid,divAmenityHeader,imagePlusMinus,movingBox){
        if (divAmenityHeader!= null && divAmenityHeader != null){
            divAmenityHeader.style.height = "125px";
            divAmenityGrid.style.display = "block";
            imagePlusMinus.src = "/images/minus.gif";
            if(movingBox != null){
                movingBox.style.top = "70px";
            }
        }
    }
    
    function hideDiv(divAmenityGrid,divAmenityHeader,imagePlusMinus,movingBox){
        if (divAmenityHeader!= null && divAmenityHeader != null){
            divAmenityHeader.style.height = "56px";
            divAmenityGrid.style.display = "none";
            imagePlusMinus.src = "/images/plus.gif";
            if(movingBox != null){
                movingBox.style.top = "0px";
            }
        }
    }
    
    
    function toggleDivs(thisID){

        var divAmenityGrid = AmenityGrid(thisID);
        var divAmenityHeader = AmenityHeader(thisID);
        var imagePlusMinus = PlusMinusImage(thisID);
        var movingBox = MovingBox(thisID);

        //this toggles the div that was clicked on
        if(divAmenityGrid != null){
            if(divAmenityGrid.style.display == "none"){
                showDiv(divAmenityGrid,divAmenityHeader,imagePlusMinus,movingBox);
                setCookie("expandedDiv",thisID,365);
            }
            else{
                hideDiv(divAmenityGrid,divAmenityHeader,imagePlusMinus,movingBox);
            }
        }
        
        //if the quanitity of amenity types is increased, the control variable needs to be increased
        var numberOfAmenityTypes = 12;
        //this closes all the other divs
        for (i = 0; i < numberOfAmenityTypes; i+=1){
            if(i != thisID){
                divAmenityGrid = AmenityGrid(i);
                divAmenityHeader = AmenityHeader(i);
                imagePlusMinus = PlusMinusImage(i);
                var movingBox = MovingBox(i);
                hideDiv(divAmenityGrid,divAmenityHeader,imagePlusMinus,movingBox);
            }
        }
    }
    
    function setCookie(c_name,value,expiredays){
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }
    function getCookie(c_name){
        if (document.cookie.length>0){
            c_start=document.cookie.indexOf(c_name + "=");
            if (c_start!=-1){
                c_start=c_start + c_name.length+1;
                c_end=document.cookie.indexOf(";",c_start);
                if (c_end==-1){
                    c_end=document.cookie.length;
                }
                return unescape(document.cookie.substring(c_start,c_end));
            }
        }
        return "";
    }
    
	function omnitureAddFromCheckBox(checkBoxID,productName,productPrice,currencyCode){
	    var itemCheckBox = document.getElementById(checkBoxID);
	    if (itemCheckBox.checked){
	        addToCart(productName,productPrice,currencyCode);
	    }
	    return true;
	}

    function checkTheBox(theBox){
        var t = document.getElementById(theBox);
	    if (t == null) {
	        return false;
	    }
	    else{
	        //we only want to add this if the box is not checked
	        //since if it is checked, it has already been added
	        if(t.checked!=true){
	            return true;
	        }
	    }
	    return false;
	}

	function getSumOfPeople(adults, children) {
	    var itemAdult = document.getElementById(adults);
	    var itemChild = document.getElementById(children);
	    var quantity = 0;
	    if (itemAdult != null) {
	        quantity += itemAdult.value * 1;
	    }
	    if (itemChild != null) {
	        quantity += itemChild.value * 1;
	    }
	    return quantity;
	}

	function omnitureAddFromFormSubmit(requestCheckBoxID,selectedCheckBoxID,adultField,childField,productName,productPrice,currencyCode){	    
	    if (checkTheBox(requestCheckBoxID) || checkTheBox(selectedCheckBoxID)){
	        var quantity = getSumOfPeople(adultField, childField)
	        if(quantity == 0){
	            quantity = 1;
	        }
	        addToCart(productName,productPrice*quantity,currencyCode);
	    }
	    return true;
	}

    function redirect(URL) { window.location = URL; }

    function checkPeople(adults,children,type,errorDiv){
        if (type == "S") {
            var quantity = getSumOfPeople(adults, children);
            if (quantity == 0 && (document.getElementById(adults) != null || document.getElementById(children) != null)) {
                ShowContent(errorDiv);
                return false;                
            }
            HideContent(errorDiv);
            return true;
        }
        HideContent(errorDiv);
        return true;
    }
