﻿function ShowHideControls(targetctl)
{
   $("#"+targetctl+"").toggle();
}

//Trim function.
function trim(str)
{
    if(!str || typeof str != 'string')
        return "";

    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}

// Date Validation Javascript
// copyright 30th October 2004, by Stephen Chapman
// http://javascript.about.com

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.
function validateDateFormat(fld,fmt,rng) 
{
var mth = new Array(' ','january','february','march','april','may','june','july','august','september','october','november','december');
var day = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

    var dd=0, mm=0, yy=0;var today = new Date;var t = new Date;fld = stripBlanks(fld);
    if (fld == '') return false;var d1 = fld.split('\/');
    if (d1.length != 3) d1 = fld.split(' ');
    if (d1.length != 3){alert('Invalid date format'); return false};

    if(stripBlanks(d1[1])=='' || stripBlanks(d1[0])=='' || stripBlanks(d1[2])=='')
   { 
        alert('Invalid Date format. Date Format should be mm/dd/yyyy');
        return false;
    }
    
    if(stripBlanks(d1[0]).length >2 || stripBlanks(d1[1]).length >2  || stripBlanks(d1[2]).length >4)
   { 
         alert('Invalid Date format. Date Format should be mm/dd/yyyy');
        return false;
   }
        
    if (fmt == 'u' || fmt == 'U') {
      dd = d1[1]; mm = d1[0]; yy = d1[2];}
    else if (fmt == 'j' || fmt == 'J') {
      dd = d1[2]; mm = d1[1]; yy = d1[0];}
    else if (fmt == 'w' || fmt == 'W'){
      dd = d1[0]; mm = d1[1]; yy = d1[2];}
    else return false;
    
    var n = dd.lastIndexOf('st');
    if (n > -1) dd = dd.substr(0,n);
    n = dd.lastIndexOf('nd');
    if (n > -1) dd = dd.substr(0,n);
    n = dd.lastIndexOf('rd');
    if (n > -1) dd = dd.substr(0,n);
    n = dd.lastIndexOf('th');
    if (n > -1) dd = dd.substr(0,n);
    n = dd.lastIndexOf(',');
    if (n > -1) dd = dd.substr(0,n);
    n = mm.lastIndexOf(',');
    if (n > -1) mm = mm.substr(0,n);
    if (!isNum(dd)) return false;
    if (!isNum(yy)) return false;
    
    if (!isNum(mm)) 
    {
          var nn = mm.toLowerCase();
          for (var i=1; i < 13; i++) 
          {
                if (nn == mth[i] || nn == mth[i].substr(0,3)) 
                {
                     mm = i; i = 13;
                }
          }
    }
    
    if (!isNum(mm)) 
        return false;

    dd = parseFloat(dd); mm = parseFloat(mm); yy = parseFloat(yy);
    
    if (yy < 100) yy += 2000;
    if (yy < 1582 || yy > 4881) 
   {
       alert('Invalid Year. Year should be  [1582 - 4881]');
        return false;
   }
    
    if (mm == 2 && (yy%400 == 0 || (yy%4 == 0 && yy%100 != 0))) day[mm-1]++;
    if (mm < 1 || mm > 12) 
   {
   alert('Invalid Month. Month should be [1 - 12] ') 
   return false;
   }
   if (dd < 1 || dd > day[mm-1]) 
   {
        alert('Invalid Day. Date should be [1 - '+ day[mm-1] +']');
        return false;
   }
    
    t.setDate(dd); 
    t.setMonth(mm-1); 
    t.setFullYear(yy);
    
    if (rng == 'p' || rng == 'P') 
    {
        if (t > today) return false;
    }
    else if (rng == 'f' || rng == 'F') 
    {
        if (t < today) 
        {
            alert('Invalid Date. Date should be greater than current Date');
            return false;
        }
    }
    else if (rng != 'a' && rng != 'A') return false;
    
    return true;
}

function isNum(parm) 
{
    return isValid(parm,numb);
}

var numb = '0123456789';

function isValid(parm,val) 
{
    if (parm == "")
    return true;
    for (i=0; i<parm.length; i++) 
    {
         if (val.indexOf(parm.charAt(i),0) == -1)
         return false;
    }
    return true;
 }
 
 function stripBlanks(fld) 
{
    var result = "";
   var c=0; 
    for (i=0; i<fld.length; i++)
    {
        if (fld.charAt(i) != " " || c > 0)
        {
            result += fld.charAt(i);
            
            if (fld.charAt(i) != " ") 
                c = result.length;
        }
    }
    
    return result.substr(0,c);
}
 
var letters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var numbers='1234567890';
var signs='!?,.:;@-';
var mathsigns='+-=()*/^';
var custom='<>#$%&amp;?¿';
var splchars='- *,.';
var emailchars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.-_@';
var regZipCanada = /[a-zA-Z0-9]/
var regZipUs = /^\d{5}$/
function AllowKeys(e,allow) 
{ 
        //var k;
        // this gets the key that was pressed on the keyboard!
        //k=document.all?parseInt(e.keyCode): parseInt(e.which);
        if(allow == "ZipCode")
        {
//                        if(window.document.getElementById("csPassenger_Shipping_Country").value == 'Canada')

//                                        allow = numbers + letters;

//                        else
                            allow = numbers;
        }                                                

        var chCode = (e.which)?e.which:e.keyCode;

        if (typeof document.getElementById!="undefined" && typeof document.all=="undefined")
        {
                        if ((34 < e.charCode && e.charCode < 41) || e.charCode == 46) return false;
                        if ((34 < chCode && chCode < 41) || chCode == 46) return true;
        }

        if (!(allow.indexOf(String.fromCharCode(parseInt(chCode))) != -1 || parseInt(chCode) == 8 || parseInt(chCode) == 13 || parseInt(chCode) <= 31)) return false;
        //if((chCode<48 || 57<chCode) && chCode>31) return false;
        return true;
}

function ShowCalendar(CtlId,imgCtlId)
{
    var cal = new Calendar.setup({
	
	        inputField     :    CtlId,   // id of the input field
	        button         :    imgCtlId,  // What will trigger the popup of the calendar
	        ifFormat       :    "%m/%d/%Y",       // format of the input field
	        timeFormat     :    "24",
	        showsTime      :     false,          //no time
	        electric       :     false
	        });
}

//added by Liju for the MultiCalendar

function ShowMultiCalendar(CtlId) {

    if (CtlId == "uscIsland1") {
        CtlChkInDate = "uscIsland1_txtCheckinDate";
        CtlChkOutDate = "uscIsland1_txtCheckoutDate";
        CtlChkInBtn = "uscIsland1_imgHotelCheckinDate";
        CtlChkOutBtn = "uscIsland1_imgHotelCheckoutDate";

        startDate1 = new Date(document.getElementById(CtlChkInDate).value);
        endDate1 = new Date(document.getElementById(CtlChkOutDate).value);
        callbacks = 0;

        InitializeCalendar();
    }
    else {

        CtlChkInDate = "uscIsland2_txtCheckinDate";
        CtlChkOutDate = "uscIsland2_txtCheckoutDate";
        CtlChkInBtn = "uscIsland2_imgHotelCheckinDate";
        CtlChkOutBtn = "uscIsland2_imgHotelCheckoutDate";

        startDate2 = new Date(document.getElementById(CtlChkInDate).value);
        endDate2 = new Date(document.getElementById(CtlChkOutDate).value);
        callbacks = 0;

        InitializeMultiCalendar();
    }

    
}

function InitializeCalendar() {
    var cal = new Calendar.setup({

        inputField: CtlChkInDate,   // id of the input field
        button: CtlChkInBtn,  // What will trigger the popup of the calendar
        ifFormat: "%m/%d/%Y",       // format of the input field
        timeFormat: "24",
        showsTime: false,          //no time
        electric: false,
        dateStatusFunc: dateInRange1, //the function to call
        onUpdate: filterDates1

    });

    Calendar.setup({
        inputField: CtlChkOutDate,
        button: CtlChkOutBtn,  // What will trigger the popup of the calendar
        ifFormat: "%m/%d/%Y",
        timeFormat: "24",
        showsTime: false,          //no time
        electric: false,
        dateStatusFunc: dateInRange2, //the function to call
        onUpdate: filterDates2
    });
}
function InitializeMultiCalendar() {
    var cal = new Calendar.setup({

        inputField: CtlChkInDate,   // id of the input field
        button: CtlChkInBtn,  // What will trigger the popup of the calendar
        ifFormat: "%m/%d/%Y",       // format of the input field
        timeFormat: "24",
        showsTime: false,          //no time
        electric: false,
        dateStatusFunc: multidateInRange1, //the function to call
        onUpdate: multifilterDates1

    });

    Calendar.setup({
        inputField: CtlChkOutDate,
        button: CtlChkOutBtn,  // What will trigger the popup of the calendar
        ifFormat: "%m/%d/%Y",
        timeFormat: "24",
        showsTime: false,          //no time
        electric: false,
        dateStatusFunc: multidateInRange2, //the function to call
        onUpdate: multifilterDates2
    });
}
function multidateInRange1(date) {

    if (endDate2 != null) {

        // Disable dates after end date
        var compareEnd = compareDatesOnly(date, endDate2);
        if (compareEnd < 0) {
            return (false);
        }

        // Hilight end date with "edges" style
        if (compareEnd == 0) {
            { return "edges"; }
        }


        // Hilight inner dates with "between" style
        if (startDate2 != null) {
            var compareStart = compareDatesOnly(date, startDate2);
            if (compareStart < 0) {
                return "between";
            }
        }
    }

    //disable days prior to today
    var today = new Date();
    var compareToday = compareDatesOnly(date, today);
    if (compareToday > 0) {
        return (true);
    }


    //all other days are enabled
    return false;
    //alert(ret + " " + today + ":" + date + ":" + compareToday + ":" + days1 + ":" + days2);
    return (ret);
}

/* 
* Can't choose days before the
* start date if it is choosen, hilights start and end dates with one style and dates between them with another
*/

function multidateInRange2(date) {
    if (startDate2 != null) {
        // Disable dates before start date
        var compareDays = compareDatesOnly(startDate2, date);
        if (compareDays < 0) {
            return (true);
        }

        // Hilight end date with "edges" style
        if (compareDays == 0) {
            { return "edges"; }
        }

        // Hilight inner dates with "between" style
        if ((endDate1 != null) && (date > startDate2) && (date < endDate2)) {
            return "between";
        }
    }

    var now = new Date();
    if (compareDatesOnly(now, date) < 0) {
        return (true);
    }

    //all other days are enabled
    return false;
}
function multifilterDates1(cal) {
    startDate2 = cal.date;
    /* If they haven't chosen an 
    end date before we'll set it to the same date as the start date This
    way if the user scrolls in the start date 5 months forward, they don't
    need to do it again for the end date.
    */
    SetMultiInterval2();
    if (endDate2 == null) {
        Calendar.setup({
            inputField: CtlChkOutDate,
            //button       :    imgHCheckOutDate,  // What will trigger the popup of the calendar
            ifFormat: "%m/%d/%Y", //"%Y-%m-%d ",
            timeFormat: "24",
            date: startDate2,
            electric: false,
            showsTime: false,          //no time
            disableFunc: multidateInRange2, //the function to call
            onUpdate: multifilterDates2
        });
    }
}

function multifilterDates2(cal) {
    endDate2 = cal.date;
}


function dateInRange1(date) {

    if (endDate1 != null) {

        // Disable dates after end date
        var compareEnd = compareDatesOnly(date, endDate1);
        if (compareEnd < 0) {
            return (false);
        }

        // Hilight end date with "edges" style
        if (compareEnd == 0) {
            { return "edges"; }
        }


        // Hilight inner dates with "between" style
        if (startDate1 != null) {
            var compareStart = compareDatesOnly(date, startDate1);
            if (compareStart < 0) {
                return "between";
            }
        }
    }

    //disable days prior to today
    var today = new Date();
    var compareToday = compareDatesOnly(date, today);
    if (compareToday > 0) {
        return (true);
    }


    //all other days are enabled
    return false;
    //alert(ret + " " + today + ":" + date + ":" + compareToday + ":" + days1 + ":" + days2);
    return (ret);
}

/* 
* Can't choose days before the
* start date if it is choosen, hilights start and end dates with one style and dates between them with another
*/

function dateInRange2(date) {
    if (startDate1 != null) {
        // Disable dates before start date
        var compareDays = compareDatesOnly(startDate1, date);
        if (compareDays < 0) {
            return (true);
        }

        // Hilight end date with "edges" style
        if (compareDays == 0) {
            { return "edges"; }
        }

        // Hilight inner dates with "between" style
        if ((endDate1 != null) && (date > startDate1) && (date < endDate1)) {
            return "between";
        }
    }

    var now = new Date();
    if (compareDatesOnly(now, date) < 0) {
        return (true);
    }

    //all other days are enabled
    return false;
}
function filterDates1(cal) {
    startDate1 = cal.date;
    /* If they haven't chosen an 
    end date before we'll set it to the same date as the start date This
    way if the user scrolls in the start date 5 months forward, they don't
    need to do it again for the end date.
    */
    SetMultiInterval1();
    if (endDate1 == null) {
        Calendar.setup({
            inputField: CtlChkOutDate,
            //button       :    imgHCheckOutDate,  // What will trigger the popup of the calendar
            ifFormat: "%m/%d/%Y", //"%Y-%m-%d ",
            timeFormat: "24",
            date: startDate1,
            electric: false,
            showsTime: false,          //no time
            disableFunc: dateInRange2, //the function to call
            onUpdate: filterDates2
        });
    }
}

function filterDates2(cal) {
    endDate1 = cal.date;
    startDate2 = endDate1;
    SetMultiInterval2();
    
}

//Adding Intervals when Changing Checkin Date for island 1
function SetMultiInterval1() {

    var DaysAdvance = document.getElementById('hdnHotelDayInterval').value;
    var ChkinDate = new Date(document.getElementById('uscIsland1_txtCheckinDate').value);
    var newCheckOutDate = new Date(ChkinDate.getTime() + parseInt(DaysAdvance) * 24 * 60 * 60 * 1000);
    var day, month;
    day = newCheckOutDate.getDate();
    month = newCheckOutDate.getMonth() + 1;

    if (day.toString().length < 2)
        day = '0' + day.toString();
    if (month.toString().length < 2)
        month = '0' + month.toString();
    document.getElementById('uscIsland1_txtCheckoutDate').value = month + '/' + day + '/' + newCheckOutDate.getFullYear();
    document.getElementById('uscIsland2_txtCheckinDate').value = month + '/' + day + '/' + newCheckOutDate.getFullYear();
    startDate1 = new Date(document.getElementById('uscIsland1_txtCheckinDate').value);
    endDate1 = new Date(document.getElementById('uscIsland1_txtCheckoutDate').value);
    SetMultiInterval2();
    //startDate2 = new Date(document.getElementById('uscIsland2_txtCheckinDate').value);
    //frmSearch.UscSearch$hdnCheckoutDate.value = document.getElementById(CtlChkOutDate).value;
    callbacks = 0;
}

//Adding Intervals when Changing Checkin Date for island 2
function SetMultiInterval2() {

    var DaysAdvance = document.getElementById('hdnHotelDayInterval').value;
    var ChkinDate = new Date(document.getElementById('uscIsland2_txtCheckinDate').value);
    var newCheckOutDate = new Date(ChkinDate.getTime() + parseInt(DaysAdvance) * 24 * 60 * 60 * 1000);
    var day, month;
    day = newCheckOutDate.getDate();
    month = newCheckOutDate.getMonth() + 1;

    if (day.toString().length < 2)
        day = '0' + day.toString();
    if (month.toString().length < 2)
        month = '0' + month.toString();
    document.getElementById('uscIsland2_txtCheckoutDate').value = month + '/' + day + '/' + newCheckOutDate.getFullYear();
    startDate2 = new Date(document.getElementById('uscIsland2_txtCheckinDate').value);
    endDate2 = new Date(document.getElementById('uscIsland2_txtCheckoutDate').value);
    //frmSearch.UscSearch$hdnCheckoutDate.value = document.getElementById(CtlChkOutDate).value;
    callbacks = 0;
}
/*
* Given two dates (in seconds) find out if date1 is bigger, date2 is bigger or
* they're the same, taking only the dates, not the time into account.
* In other words, different times on the same date returns equal.
* returns -1 for date1 bigger, 1 for date2 is bigger 0 for equal
*/

function compareDatesOnly(date1, date2) {
    var year1 = date1.getYear();
    var year2 = date2.getYear();
    var month1 = date1.getMonth();
    var month2 = date2.getMonth();
    var day1 = date1.getDate();
    var day2 = date2.getDate();

    if (year1 > year2) {
        return -1;
    }
    if (year2 > year1) {
        return 1;
    }

    //years are equal
    if (month1 > month2) {
        return -1;
    }
    if (month2 > month1) {
        return 1;
    }

    //years and months are equal
    if (day1 > day2) {
        return -1;
    }
    if (day2 > day1) {
        return 1;
    }

    //days are equal
    return 0;


    /* Can't do this because of timezone issues
    var days1 = Math.floor(date1.getTime()/Date.DAY);
    var days2 = Math.floor(date2.getTime()/Date.DAY);
    return (days1 - days2);
    */
}
//end  by Liju for MultiCalendar




function SetUniqueRadioButton(nameregex, current)
{

   re = new RegExp(nameregex);
   
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i]
      
      if (elm.type == 'radio')
      {
         if (re.test(elm.id))
         {
            elm.checked = false;
         }
      }
   }
   current.checked = true;
}

function CompareDate(Date1,Date2)
{
    var date1 = new Date(Date1);
    var date2 = new Date(Date2);
    
     var date1Comp = date1.getTime(); // milliseconds
    var date2Comp = date2.getTime();

    if (date1Comp > date2Comp)
    {
        return -1
    }
    else if(date1Comp == date2Comp)
        return 0
    else 
        return 1
}

function DisplayProcessingImage(parentCtlid)
{
    var a = document.createElement('div');
    var parent=document.getElementById(parentCtlid);

	a.cellSpacing='1px';
	a.cellPadding='2px';
	a.style.border='0px solid #DDDDDD';
	a.style.position='absolute';
	var top=0;
	var left=0;
	while(parent)
	{
	    top += parent.offsetTop;
	    left += parent.offsetLeft;
	    parent = parent.offsetParent;
    }
    
    var parent=document.getElementById(parentCtlid);
    
    top= top + (parent.offsetHeight/2);
    left = left + (parent.offsetWidth/2);
	a.style.top = top + "px";
	a.style.left = left+ "px";
	a.id = 'imgdiv';
	a.innerHTML ="<img src='images/processing.gif'/>"
	document.body.appendChild(a);
}