﻿function ValidateChildrenDOBDate( source, arguments )
{
    var lastPossibleDateStr = null;
    if(GetBookingProcessKind() == "FA")
    {
        var textBoxDepartureDate = GetControl("textBoxDepartureDate", 0, "input" );
        var departureDate = GetDateFromString(textBoxDepartureDate.value);
        if(departureDate == null)
        {
            arguments.IsValid = false;
            return;
        }
        var flexibility = parseInt(GetControl("dropDownListFlexibility", 0, "select").value);
        var durationOfStay = GetControl("dropDownListDurationOfStay", 0, "select").value;
        var re = /^(\d{1,2})(\-)(\d{1,2})$/i;
        if(durationOfStay.match(re) != null)
        {
            var durationOfStayMax = parseInt(RegExp.$3.valueOf());
            var lastPossbleDate = new Date( departureDate.getTime() + ( flexibility + durationOfStayMax )*24*60*60*1000 );
            lastPossibleDateStr = lastPossbleDate.getDate().toString() + "/" + (lastPossbleDate.getMonth()+1).toString() + "/" + lastPossbleDate.getFullYear().toString();
        }
    }
    else
    {
        var textBoxCheckOutDate = GetControl("textBoxCheckOutDate", 0, "input" );
        lastPossibleDateStr = textBoxCheckOutDate.value;
    }
	var daysDifference = maxChildAge*365 + maxChildAge/4;
	arguments.IsValid = ValidateDate( arguments.Value, lastPossibleDateStr, daysDifference, true );
}

function ValidateAdultsPlusChildrenMoreThanZero( source, arguments )
{
	var roomNumber = source.id.substring(source.id.length-1,source.id.length);
	var dropDownListAdults = null,dropDownListChildren = null,AdultsChildrenNumber = 0;	

	dropDownListAdults = GetControl("dropDownListAdultsR"+roomNumber, 0, "select" );
	dropDownListChildren = GetControl("dropDownListChildrenR"+roomNumber, 0, "select" );
	
	if ( dropDownListAdults != null && dropDownListChildren != null )
		AdultsChildrenNumber = parseInt(dropDownListAdults.value) + parseInt(dropDownListChildren.value);
		
			
	var tempTable = document.getElementById("tableRoom"+roomNumber);
	var cellAdultsR = document.getElementById("CellAdultsR"+roomNumber);
	var cellChildrenR = document.getElementById("CellChildrenR"+roomNumber);
	
	if ( AdultsChildrenNumber > 0 )
	{		
		arguments.IsValid = true;
		
		ChangeValidatorsVisibility(cellAdultsR,false);
		ChangeValidatorsVisibility(cellChildrenR,false);
	}
	else
	{
		arguments.IsValid = false;
		
		ChangeValidatorsVisibility(cellAdultsR,true);
		ChangeValidatorsVisibility(cellChildrenR,true);	
	}
}

//function AdjustSmartStartupMaskTabsStyle()
//{
//	var CYHTab = GetControl( 'CYHTab',0,"td" );
//	var RMPTab = GetControl( 'RMPTab',0,"td" );
//	
//	CYHTab.style.cursor = (UsedBrowser == "Gecko")?"pointer":"hand";
//	RMPTab.style.cursor = (UsedBrowser == "Gecko")?"pointer":"hand";
//	
//}

function GetSelectedMaskData( bookingProcessType )
{
    var result = bookingProcessType;
    var control = null;
    
    //-----------
    //Shared Data
    var to = GetSelectedControlText( "select","dropDownListDestinations");
//    var accommodationType = GetSelectedControlText( "select","dropDownListAccommodationType");
//    var stars = GetSelectedControlText( "select","dropDownListAccommodationCategory");
//    var accommodationName = GetSelectedControlText( "select","dropDownListAccommodationNames");
    var numberOfRooms = GetSelectedControlText( "select","dropDownListAccommodationRoomCount");
    var roomPersons = GetRoomsPersons(GetSelectedControlText( "select","dropDownListAccommodationRoomCount"));
    //-------------------------
    //Packages 
    
    switch(bookingProcessType)
    {
        case "FA":
            result += String.format(",{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}",
                        //From
                        GetSelectedControlText( "select","dropDownListDepartureLocations"),
                        //To
                        to,
                        //Date
                        GetSelectedControlText( "input","textBoxDepartureDate"),
                        //Flexibility
                        GetSelectedControlText( "select","dropDownListFlexibility"),
                        //Duration
                        GetSelectedControlText( "select","dropDownListDurationOfStay"),
                        //AccommodationType
                        "",
                        //Stars
                        "",
                        //AccommodationName
                        "",
                        //Number of rooms
                        numberOfRooms,
                        //RoomsPersons
                        roomPersons);
            break;
            case "AO":
                result += String.format(",{0},{1},{2},{3},{4},{5},{6},{7},{8}",
                        //Country of Residence
                        GetSelectedControlText( "select","dropDownListLivingInLocations"),
                        //To
                        to,
                        //Arrival Date
                        GetSelectedControlText( "input","textBoxCheckInDate"),
                        //Departure Date
                        GetSelectedControlText( "input","textBoxCheckOutDate"),
                        //AccommodationType
                        accommodationType,
                        //Stars
                        stars,
                        //AccommodationName
                        accommodationName,
                        //Number of rooms
                        numberOfRooms,
                        //RoomsPersons
                        roomPersons);
              break;
        
    }
    
    return result;    
}
function GetRoomsPersons(numberOfRooms)
{
    var result = "";
    numberOfRooms = parseInt(numberOfRooms);
    
    if(!isNaN(numberOfRooms))
    {
        for(var i=1;i<=numberOfRooms;i++)
        {            
            result += String.format("{0}:{1}:{2}{3}",GetSelectedControlText("select","dropDownListAdultsR" + i.toString()),GetSelectedControlText("select","dropDownListChildrenR" + i.toString()),GetSelectedChildrenDates(i,parseInt(GetSelectedControlText("select","dropDownListChildrenR" + i.toString()))),numberOfRooms == i?"":"-");                        
        }    
    }
    
    return result;
}
function GetSelectedChildrenDates(roomNumber, numberOfChildren)
{
    var result = "";
    for(var i=1;i<=numberOfChildren;i++)
    {
       result += String.format("{0}{1}", GetSelectedControlText("input",String.format("textBoxR{0}Child{1}",roomNumber,i.toString())), numberOfChildren != i?":":"");
    }    
}
function GetSelectedControlText(type,controlName)
{
    var result = null;
    
    switch(type)
    {
      case "select":
        result = GetControl( controlName,0,"select" );
        
        if(result != null && result.selectedIndex != -1)
		    result = result.options[result.selectedIndex].text;
		
	  break;
	  case "input":
	    result = GetControl( controlName,0,"input" );
	    result = result == null?"":result.value;
	  break; 
    }
    
    return result;
}

function showLayer(whichLayer) {

if (document.getElementById) {
    // this is the way the standards work
    document.getElementById(whichLayer).style.DISPLAY = "inline";
    document.getElementById(whichLayer).style.visibility = "visible";
    }
    else if (document.all) {
    // this is the way old msie versions work
    document.all[whichlayer].style.visibility = "visible";
    document.all[whichlayer].style.DISPLAY = "inline";
    }
    else if (document.layers) {
    // this is the way nn4 works
    document.layers[whichLayer].visibility = "visible";
    document.layers[whichLayer].DISPLAY = "inline";
    }
}

function hideLayer(whichLayer) {
    if (document.getElementById) {
    // this is the way the standards work
    document.getElementById(whichLayer).style.DISPLAY = "none";
    document.getElementById(whichLayer).style.visibility = "hidden";
    }
    else if (document.all) {
    // this is the way old msie versions work
    document.all[whichlayer].style.visibility = "hidden";
    document.all[whichlayer].style.DISPLAY = "none";
    }
    else if (document.layers) {
    // this is the way nn4 works
    document.layers[whichLayer].visibility = "hidden";
    document.layers[whichLayer].DISPLAY = "none";
    }
}