﻿/****************************************************************************************************************************
*    File            :   Utils.js
*    Created Date    :   01/May/2008
*    Created By      :   Nikhil Agarwal
*    Functions       :   1.ValidatePhoneNumber [Input:Phone Number][Output:Valid Phone number or not][Value:True Or False]
*                        
****************************************************************************************************************************/   
//var appUrl='http://localhost:2648/SunnyTunisia/';
var newWindow='';
function doSearch(checkInDate,checkoutdate)
{
    var queryString='';
    if(validatefrm(checkInDate,checkoutdate))
    {
        queryString +='&CheckInDate=' + checkInDate;
        queryString +='&CheckOutDate=' + checkoutdate;
        queryString +='&IsHotelSearch=Y';
        //window.location.href = appUrl + "Hotels/ProcessRequest.aspx?" + queryString;
        window.parent.location.href = appUrl + "Hotels/ProcessRequest.aspx?" + queryString;
    }
    else
    {
        return false;
    }
}
function doNameSearch(checkInDate,checkoutdate,hotelName,city)
{
    var queryString='';
    if(validatefrm(checkInDate,checkoutdate))
    {
        queryString +='&CheckInDate=' + checkInDate;
        queryString +='&CheckOutDate=' + checkoutdate;
        if(hotelName!='')
        {
            queryString +='&hotelName='+hotelName;
        }
        if(city!='')
        {
            queryString +='&city='+city;
        }
        queryString +='&IsHotelSearch=Y';
        window.location =appUrl + "Hotels/ProcessRequest.aspx?" + queryString;   
    }
    else
    {
        return false;
    }
}
function doSearchByHotelID(checkInDate,checkoutdate,hotelName,city,hotelid)
{
    var queryString='';
    if(validatefrm(checkInDate,checkoutdate))
    {
        queryString +='&CheckInDate=' + checkInDate;
        queryString +='&CheckOutDate=' + checkoutdate;
        if(hotelName!='')
        {
            queryString +='&hotelName='+hotelName;
        }
        if(city!='')
        {
            queryString +='&city='+city;
        }
        if(hotelid != '')
        {
            queryString +='&hotelId='+hotelid;
        }
        queryString +='&IsHotelIDSearch=Y';
        window.location =appUrl + "Hotels/ProcessRequest.aspx?" + queryString;   
    }
    else
    {
        return false;
    }
}
function GetDateDiff(inDate,outDate)
{
    //Calculate difference btw the two dates, and convert to days
    var one_day=1000*60*60*24
    var TotalNight
    var x = inDate.split("-");
    var y = outDate.split("-");
    var _InMonth=GetMonthInDigit(x[1]);
    var _OutMonth=GetMonthInDigit(y[1]);
    
    var InDate=new Date(x[2],_InMonth,x[0]);
    var OutDate=new Date(y[2],_OutMonth,y[0]);
    
    TotalNight =Math.ceil((OutDate.getTime()-InDate.getTime())/one_day);
    return TotalNight;
}
function GetTodayDiff(outDate)
{
    //Calculate difference btw the two dates, and convert to days
    var one_day=1000*60*60*24
    var TotalNight
    var y = outDate.split("-");
    var _OutMonth=GetMonthInDigit(y[1]);
    var OutDate=new Date(y[2],_OutMonth,y[0]);
    var now= new Date();
    TotalNight =Math.ceil((OutDate.getTime()-now.getTime())/one_day);
    return TotalNight;
}
function GetMonthInDigit(Month)
{
    for(var MonPos=0;MonPos<MonthNames.length;MonPos++)
    {
        if (MonthNames[MonPos].substr(0,3).toUpperCase() == Month.toUpperCase()) break;
    }
    return MonPos;
}
function validatefrm(checkindate,checkoutdate)
{
    var noOfNight=GetDateDiff(checkindate,checkoutdate);
    if(noOfNight<=0)
    {
        alert('Check-in date should be less than Check-out date.');
        return false;
    }        
    noOfNight=GetTodayDiff(checkindate);
    if(noOfNight<0)
    {
        alert('Check-in date should be greater than current date.');
        return false;
    }
    return true;
}
