﻿/* Clientside validation for the booking panel date input. Is called from the .NET custom validator control in hotel search */

function valCusDay_ClientValidate( sender , args )
{
    // get the dateInput container. At the moment this will only validate the one control
    var dateInput = $("[id*='ddlArrivalDay']").parents(".dateInputs");
    var date = getDateFromInput( dateInput );
    var error = validDate( date , true , dateInput.find("label").text() ) 
    if( error )
    {
        sender.innerHTML = "*" + error;
        args.IsValid = false;
    }
    
}

function ClearTextbox(textbox, initalValue)
{
    if (textbox.value == initalValue)
    {
        textbox.value = "";
    }
}

function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

// stretches the last element in a column to reach the bottom of the parent container (or image in the case of the twoColContent)
// for the grey boxes mainly. Needs to be a bit cleverer than the stand match height function
function setElementStretch()
{
    if( !document.lastChild || !document.getElementById || !document.firstChild ) return;
    
    var left = document.getElementById("leftColumn");
    var right = document.getElementById("rightColumn");
    
    if( !left || !right ) return;

    var leftChildPos = left.className.match( "twoCol" ) ? "first" : "last";
    

    if( document.nodeName && document.removeChild )
    {    
        while( left[ leftChildPos + "Child"].nodeName == "#text" )
            left.removeChild( left[ leftChildPos + "Child"] );
    
        while( right.lastChild.nodeName == "#text" )
            right.removeChild( right.lastChild );
    }

    var leftChild = left[ leftChildPos + "Child"];
    var rightChild = right.lastChild;
    
    if( leftChildPos == "last" )
    {   
        leftChild.className = leftChild.className.replace( /\s*greyBottomCorner/g , "" );
        rightChild.className = rightChild.className.replace( /\s*greyBottomCorner/g , "" );
    }
    
    var otLeft = getOffsetTop( leftChild );
    var obLeft = otLeft + leftChild.offsetHeight;

    var otRight = getOffsetTop( rightChild );
    var obRight = otRight + rightChild.offsetHeight;
    
    
    var sLower = obRight < obLeft ? [ "Left" , "Right" ] : [ "Right" , "Left" ];
    var elToChange = eval( sLower[1].toLowerCase() + "Child" );

//    if( obRight > obLeft && leftChildPos == "first" ) return;
    if( !elToChange.className.match( "box_" ) ) return; // only stretch if it is one of the 'box' components

    var diff = eval( "ot" + sLower[1] ) - eval( "ob" + sLower[0] );

    elToChange.style.paddingBottom = 0;

    var testHeight = elToChange.offsetHeight;
    elToChange.style.minHeight = Math.abs(diff) + 'px';

    /* test for < ie6 - which doesn't have minHeight */
    if( testHeight == elToChange.offsetHeight )
        elToChange.style.height = Math.abs(diff) + 'px';
}

function getOffsetTop( el )
{
    var ot = 0;
    while( !el.className.match("page_content") && el.tagName != "BODY" )
    {
        ot += el.offsetTop;
        el = el.offsetParent;
    }
    
    return ot;
}

function OpenPrivacyPolicy( path ){
    
    if( !path ) path = "../Popups/PrivacyPolicy.aspx"
    var resourceWindow = open( path , "PrivacyPolicy","resizable=1,width=570,height=590,status=1,scrollbars=1,location=0");
	if (resourceWindow.opener == null)
	{
	    resourceWindow.opener = self;
	}
	return false;
}

function OpenWindow(url, width, height, name)
{
    var arguments = "resizable=1,width=" + width + ",height=" + height + ",status=1,scrollbars=1,location=0"
    var win = window.open(url,name,arguments);
    return !win;
}

function validDate( d , bNotPast , sLabel)
{
    var error = "";
    
    var value = d.split("/");
    if(value[2].length<3){value[2] = "20" + value[2];}
    value = dateToString( value[0],value[1],value[2] , "/" );
    
    // check if valid date
    var date = dateToString(stringToDate( value ));

    if( date != value )
    {
        error = (sLabel || "Date") + " not valid (" +  value + ")";
    }

    // Check to see if the date can be in the past, can include today
    if( !error && bNotPast )
    {
        var today = dateToString(new Date());
        if( stringToDate( value ).valueOf() - stringToDate( today ).valueOf() < 0 )
        {
            error = (sLabel || "Date") + " can't be in the past (" +  value + ")";
        }
    }

    return error;
}

function dateToString()
{
    var d,m,y;
    var delimiter = "/";
    if( arguments.length <= 2 )
    {
        var date = new Date( arguments[0] );
        d = date.getDate();
        m = date.getMonth() + 1;
        y = date.getFullYear();
        
        if( arguments[1] ) delimiter = arguments[1];
    }
    else
    {
        d = arguments[0];
        m = arguments[1];
        y = arguments[2];
        if( arguments[3] ) delimiter = arguments[3];
    }
    
    return ("0" + d).slice(-2) + delimiter + ("0" + m).slice(-2) + delimiter + y;
}

function stringToDate( sDate , delimiter )
{
    var a = sDate.split( delimiter || "/" );
    // check year in correct format
    if(a[2].length>2){a[2] = a[2].substr(2,2)}
    a[2] = "20" + a[2];
    
    return new Date( a[2],a[1]-1,a[0] );
}