﻿function getObj(name)
{
  if (document.getElementById)
  {
    return document.getElementById(name);	    
  }
  else if (document.all)
  {
    return document.all[name];	    
  }
  else if (document.layers)
  {
    return document.layers[name];   	    
  }
}
    
function setLyr(obj,lyr)
{
    var newX = findPosX(obj);
    var newY = findPosY(obj);	 	    
    lyr.style.top = (newY + findHeight(obj)) + 'px';
    lyr.style.left = newX + 'px';
}

function AddToFavorites(propID,object)
{
   
        if(addToFavoritesAjax)
        {
            s.prop22 = propID;
            s.eVar21 = propID;
            s.t();
            addToFavoritesAjax(propID);
            object.setAttribute("onclick","");
            object.style.color = "";
            object.style.cursor = "default";
            object.innerHTML = "Saved";
        }
   
}

function DoProximityChange(theControl)
{
    s.prop25 = theControl.value;
    s.t();
}

function DoSleepsChange(theControl)
{
     s.prop27 = theControl.value;
     s.t();
}

function DoPriceChange(theControl)
{
    s.prop26 = theControl.value;
    s.t();
}

function DoLogin()
{
    s.events = "event4";
    s.t();
}

function DoSwitchToMap()
{
    s.events = "event7";
    s.t();
}

function DoAmenityClick(amenityDescr,theBox)
{
    if(theBox.checked)
    {    
        s.prop24 = amenityDescr;
        s.eVar23 = s.prop24;
        s.t();
    }
}

function DoEmailToFriend(propId)
{
    s.prop23 = propId;
    s.eVar22 = s.prop23;
    s.t();
}

function findHeight(obj)
{
    var curheight = 0;
    if (obj.offsetheight)
    {
        curheight = obj.offsetheight;
    }
    else if (obj.height)
    {
        curheight = obj.height;
    }
    return curheight;
}

function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

/* Performs a currency format on a specified number.
 *
 * num : Number to format.
 *
 * Returns : US Currency formatted string.
 */
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
        cents = "0" + cents;

    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));

    return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function formatMinutes(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "00";
    
    if(num < 10)
        num = "0" + num;
        
    return num;
}

/* Sets a cookie on the client.
 * 
 * name : Name of the cookie.
 * value : Value of the cookie.
 * [days] : The number of days until the cookie expires. (defaults to end of current session).
 */
function setCookie(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    
    document.cookie = name + "=" + value + expires;
}

/* Appends a value to an existing cookie. The cookie will be created if it does
 * not already exist.
 *
 * name : Name of the cookie.
 * value : Value to append to the cookie.
 * [separator] : Used to separate the specified value from the existing value.
 * [days] : The number of days until the cookie expires. (defaults to end of current session).
 */
function appendCookie(name, value, separator, days) {
    var currentValue = getCookie(name);
    
    if(currentValue) {
        currentValue += (separator ? separator : "") + value;
    } else {
        currentValue = value;
    }
    
    setCookie(name, currentValue, days);
}

/* Gets a cookie from the client with the specified name.
 *
 * name : Name of the desired cookie.
 *
 * Returns : String containing value of specified cookie or null if cookie does not exist.
 */
function getCookie(name) {
    var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	
	for(var i = 0;i < ca.length;++i) {
	    var c = ca[i];
	    while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
	}
	return null;
}

/* Delete the specified cookie.
 *
 * name : Name of the cookie.
 * [path] : Path of the cookie (must be same as path used to create cookie).
 * [domain] : Domain of the cookie (must be same as domain used to create cookie).
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/* Verifies that the client has support for cookies.
 * 
 * Returns : true if the client does support cookies; otherwise, false.
 */
function verifyCookie() {
    var name = "cookie-support";
    var value = "testing-your-cookie-support";
    
    setCookie(name, value);
    
    return getCookie(name) == value;
}

// Returns the source element of an event.
function getEventSource(e) {
    var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	
	return targ;
}

function persistOmnitureRef()
{
    if(getCookie('ZonderOmn'))
    {
        var temp = getCookie('ZonderOmn')
        if(temp)
        {
            if(temp.indexOf('&domain') != -1)
            {
                s.eVar24 = temp.substring(0,temp.indexOf('&domain'));
                s.eVar25 = temp.substring((temp.indexOf('&domain')+8),temp.length);
            }
            else
            {
            }
        }
        //s.eVar24 = s.referrer;
    }
    else
    {
        if(s.referrer)
        {        
            appendCookie('ZonderOmn',s.referrer+"&domain=www.vast.com");
            s.eVar24 = s.referrer;
            var regexp = '^(?:[^/]+://)?([^/:]+)';
            s.eVar25 = s.referrer.match(regexp)[1];
        }
        else
        {
            appendCookie('ZonderOmn',"Typed/Bookmarked&domain=Typed/Bookmarked");
        }
    }
    //getCookie('Zonder');
    
}

// Displays a loading spinner until the real content loads from a callback.
function Spinner(context, text) {
    if(text == null) text = "Loading...";
      
    context.innerHTML = "<span class=\"spinner\"><img src=\"/WebSite/Images/spinner.gif\" height=\"16\" width=\"16\" align=\"middle\" /> " + text + "</span>";
}
    
function replaceString(sString, sReplaceThis, sWithThis) { 
    if (sReplaceThis != "" && sReplaceThis != sWithThis) {
        var counter = 0;
        var start = 0;
        var before = "";
        var after = "";
        while (counter<sString.length) {
            start = sString.indexOf(sReplaceThis, counter);
            if (start == -1){
                break;
            } else {
                before = sString.substr(0, start);
                after = sString.substr(start + sReplaceThis.length, sString.length);
                sString = before + sWithThis + after;
                counter = before.length + sWithThis.length;
            }
        }
    }
    return sString;
}

function trim(str) {
    // Strip leading and trailing white-space
    return str.replace(/^\s*|\s*$/g, "");
}

// Parses all text out of a number.
function ParseNumber(num) {
    if (num == null) { num = 0; }
    
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = 0;
        
    return parseFloat(num);
}

/*
 * Handles multiple class assignments on an element.
 *
 * action : Defines the action to perform. A list of actions is below.
 * elem : The element to perform the action on.
 * [c1] : The name of the first class.
 * [c2] : The name of the second class.
 *
 * ACTIONS:
 * swap   : Replaces class c1 with class c2 on the element.
 * add    : Add class c1 to the element.
 * remove : Removes class c1 from the element.
 * check  : Checks to see if class c1 is already applied to the element and returns true or false.
 */
function jscss(action, elem, c1, c2){ 
    switch (action){
        case 'swap':
            elem.className = !jscss('check', elem, c1) ? elem.className.replace(c2, c1) : elem.className.replace(c1, c2);
            break;
        case 'add':
            if(!jscss('check', elem, c1)) {elem.className += elem.className ? ' ' + c1 : c1;}
            break;
        case 'remove':
            var rep = elem.className.match(' ' + c1) ? ' ' + c1 : c1;
            elem.className = elem.className.replace(rep, '');
            break;
        case 'check':
            return new RegExp('\\b'+c1+'\\b').test(elem.className)
            break;
    }
}

// Runs every time a page is loaded.
function common_init() {
    if (typeof(ValidationControlHighlight) != "undefined") {
        ValidationControlHighlight();
    }
}

function daysInMonth(month,year) {
    var dd = new Date(year, month, 0);
    return dd.getDate();
}

function overlay(id) {
	el = document.getElementById(id);
	el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
	
	return false;
}

function positionToParent(elemId, parentId, width)
{
    var elem = document.getElementById(elemId);
    var parent = document.getElementById(parentId);

    var x = parent.offsetLeft;
    var y = parent.offsetTop;
    
    while (parent.offsetParent)
    {
        parent = parent.offsetParent;
        x += parent.offsetLeft;
        y += parent.offsetTop;
    }
    elem.style.top = y + "px";
    elem.style.left = (x - width) + "px";
    
    return false;
}

//function js_waterMark_Focus(objname, waterMarkText, waterMarkStyle, normalStyle, isPassword)
//{
//    obj = document.getElementById(objname);
//    
//    if(obj.value == waterMarkText)
//    {
//        obj.value = '';
//    }
//    
//    obj.className = normalStyle;
//    
//    if(isPassword)
//    {
//        var newObj = changeInputType(obj, 'password');
//        setTimeout("document.getElementById('" + newObj.id + "').focus()", 10);
//    }
//}
//function js_waterMark_Blur(objname, waterMarkText, waterMarkStyle, normalStyle, isPassword)
//{
//    obj = document.getElementById(objname);
//    
//    if(obj.value == '')
//    {
//        obj.value = waterMarkText;
//        obj.className = waterMarkStyle;
//        if(isPassword)
//        {
//            changeInputType(obj, 'text');
//        }
//        
//        //Booking process page 1 for changing rates
//        if(obj.id == "ctl00_phContent_StartDatePickerTextBox2Booking")
//        {
//            setTimeout('__doPostBack(\'ctl00$phContent$StartDatePickerTextBox2Booking\',\'\')', 0);
//        }
//        else if(obj.id == "ctl00_phContent_EndDatePickerTextBox2Booking")
//        {
//            setTimeout('__doPostBack(\'ctl00$phContent$EndDatePickerTextBox2Booking\',\'\')', 0);
//        }
//    }
//    else
//    {
//        obj.className = normalStyle;
//    }

//}

function changeInputType(oldObject, oType) 
{
    var newObject = document.createElement('input');
    newObject.type = oType;
    if(oldObject.size) newObject.size = oldObject.size;
    if(oldObject.value) newObject.value = oldObject.value;
    if(oldObject.name) newObject.name = oldObject.name;
    if(oldObject.id) newObject.id = oldObject.id;
    if(oldObject.className) newObject.className = oldObject.className;
    if(oldObject.onfocus) newObject.onfocus = oldObject.onfocus;
    if(oldObject.onblur) newObject.onblur = oldObject.onblur;
    
    newObject.style.color = '#cccccc';
    newObject.style.cssFloat = 'left';
    newObject.style.paddingBottom = '5px';
    newObject.style.paddingTop = '6px';
    newObject.style.width = '172px';
    newObject.style.marginRight = '5px';
    
    oldObject.parentNode.replaceChild(newObject, oldObject);
    return newObject;
}

var lastKeyPressed;
function clickButtonOnEnter(e, buttonid) {
      var evt = e ? e : window.event;
      var bt = document.getElementById(buttonid);
      if (bt){
          if (evt.keyCode == 13 && lastKeyPressed != 38 && lastKeyPressed != 40){
                bt.click();
                return false;
          }
          lastKeyPressed = evt.keyCode;
      }
}



// Called on a search.
function SetupSearch()
{
     if ($get("isPageLinkSearch").value == 'true')
    {
        $get("isPageLinkSearch").value = 'false';
        $get("pageNumber").value = "1";
    }
}

// Show the what's this depending on the value given
function showWhatsThisPopupPanel(parent, width)
{
    var elem = document.getElementById("ctl00_phContent_Clipboard1_WhatsThisPanel");
    
    var x = parent.offsetLeft;
    var y = parent.offsetTop;
    
    while (parent.offsetParent)
    {
        parent = parent.offsetParent;
        x += parent.offsetLeft;
        y += parent.offsetTop;
    }
    elem.style.top = y + "px";
    elem.style.left = (x - width) + "px";
    
    elem.style.visibility = "visible";
    elem.style.display = "block";
    
    return false;
}

function hideWhatsThisPopupPanel()
{
    var elem = document.getElementById("ctl00_phContent_Clipboard1_WhatsThisPanel");
    
    elem.style.visibility = "hidden";
    elem.style.display = "none";
    
    return false;
}

function ValidateSearchDates(val, args)
{
    if (checkoutTextBox.value && checkoutTextBox.value != 'Check-out Date' && 
        checkinTextBox.value && checkinTextBox.value != 'Check-in Date')
    {
        var checkinDate = getDateFromShortDateString(checkinTextBox.value);
        var checkoutDate = getDateFromShortDateString(checkoutTextBox.value);
        var today = new Date();
        today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
        if (checkoutDate <= checkinDate)
        {
            datesCustomValidator.innerHTML = "*Check-out must be greater than Check-in.<br/>";
            args.IsValid = false;
        }
        else if (checkinDate < today)
        {
            datesCustomValidator.innerHTML = "*Check-in cannot be in the past.<br/>";
            args.IsValid = false;
        }
    }
    else if ((checkoutTextBox.value && checkoutTextBox.value != 'Check-out Date' && (!checkinTextBox.value || checkinTextBox.value == 'Check-in Date')) ||
        (checkinTextBox.value && checkinTextBox.value != 'Check-in Date' && (!checkoutTextBox.value || checkoutTextBox.value == 'Check-out Date')))
    {
        datesCustomValidator.innerHTML = "*Either provide both or no dates.<br/>";
        args.IsValid = false;
    }
}

function getDateFromShortDateString(shortDate)
{
    var dateParts = shortDate.split('/');
    var month = dateParts[0] - 1;
    var day = dateParts[1];
    var year = dateParts[2];
    return new Date(year, month, day);
}