//Page load... 
function pageLoad() { 

    //Get page path.
    var path = window.location.pathname;
    
    //Only when loading the Login page.
                           
    //Only when loading the Calendars page.
    if (path == '/nCalendars.aspx'){
    
    //Set panels position relative to the footer...
    hangPanels();
    
    
    //Keeps current panel position fixed after postback when using a DragPanel Extender.  
    //Retrieves the panel position from a hidden field when the page is posted back. 
               
    /*  Listen for drag event: Every time the panel is drag save its position using
        the BehaviorID="dragPanelLeft" of the DragPanel Extender.
        Both departure (Left) and return (Right) Panel are listening.    
    */
    
    //Departure (Left) Panel. 
    PanelLeftListening();
    
    //Return (Right) Panel.
    PanelRightListening(); 
  }
}

//Close Browser with no warning.
function closeNoWarning() {
    window.open('close.html','_self');
}

///////////////////////// Calendars.aspx /////////////////////////////

//Set panels position relative to the footer.
function hangPanels()
{
    //Get footer object. 
    var objFooter = document.getElementById('footer');
               
    //Get position of the footer to be used as a reference to position the panels.
    var X = findPosX(objFooter);
    var Y = findPosY(objFooter);
    
    //Get width of the footer.
    var widthOfFooter = objFooter.getAttribute('width');
    
    //Width of the Panels...(340:hardcoded width + 20:borders and padding)
    var widthOfPanels = 360;
    
    //Fine-tuning parameters.
    var tuningX = 22;
    var tuningY = 120;
                  
    //Offsets...
    var offsetXLeft = X + tuningX;
    var offsetYLeft = Y - tuningY;
    var offsetXRight = X + (widthOfFooter - widthOfPanels) - tuningX - 3;
    var offsetYRight = offsetYLeft;
    
    //Reposition both panels relative to the footer.
    $find('dragPanelLeft').set_location(new Sys.UI.Point(offsetXLeft, offsetYLeft));
    $find('dragPanelRight').set_location(new Sys.UI.Point(offsetXRight, offsetYRight));
} 

//Listening for dragging departure (Left) Panel. 
function PanelLeftListening() {

    //Get position from hidden field.
    var positionLeft = document.getElementById('positionPanelLeft');  
   
     //Listening...
     $find('dragPanelLeft').add_move(getPanelPositionLeft);
     
     //Panel moved...         
     if (positionLeft.value != "0") {
        var partsLeft = positionLeft.value.split(',');
        var posXLeft = parseInt(partsLeft[0]);
        var posYLeft = parseInt(partsLeft[1]);
                               
        // Restore panel position using the BehaviorID="dragPanelLeft" of the DragPanel Extender.
        $find('dragPanelLeft').set_location(new Sys.UI.Point(posXLeft, posYLeft)); 
      }
      
      // Pivate function: Gets the location of the Panel. (Every time the panel is dragged)
      function getPanelPositionLeft() {   
      // Using the DragPanel Extender.
      var dragPanelExtender = $find('dragPanelLeft').get_element();
        
      var pos = $common.getLocation(dragPanelExtender);
      
      // Saves the position of the panel in the hidden field.
      document.getElementById('positionPanelLeft').value = pos.x + ',' + pos.y;    
    }
}      

//Listening for dragging return (Right) Panel.
function PanelRightListening() {

    //Get position from hidden field.
    var positionRight = document.getElementById('positionPanelRight');  
    
     //Listening...
    $find('dragPanelRight').add_move(getPanelPositionRight);
    
    //Panel moved...     
    if (positionRight.value != "0")
    {       
        var partsRight = positionRight.value.split(',');
        var posXRight = parseInt(partsRight[0]);
        var posYRight = parseInt(partsRight[1]);
                
        // Restore panel position using the BehaviorID="dragPanelRight" of the DragPanel Extender.
        $find('dragPanelRight').set_location(new Sys.UI.Point(posXRight, posYRight)); 
     }
     
     // Pivate function: Gets the location of the Panel. (Every time the panel is dragged)
      function getPanelPositionRight() {   
      // Using the DragPanel Extender.
      var dragPanelExtender = $find('dragPanelRight').get_element();
        
      var pos = $common.getLocation(dragPanelExtender);
      
      // Saves the position of the panel in the hidden field.
      document.getElementById('positionPanelRight').value = pos.x + ',' + pos.y;    
    }
  }           

//Fix invalid departure route NY-CMW.
function fixDepartureNY_CMW(){
    //Invalid Departure...
    //Invalid Route Origen:NY - Destination:CMW
    if ((document.getElementById('ddlDeparFrom').selectedIndex == '1') && (document.getElementById('ddlDeparTo').selectedIndex == '1')) {
        //When flight origen is New York we must select Havana as our destination.
        alert('When flight origen is New York you must select Havana as your destination. The System will do it for you now.');
        document.getElementById('ddlDeparTo').selectedIndex = '0';
    }
}

//Fix invalid departure route CMW-NY. ***To make it more readable this option is treated as a separate function.***
function fixDepartureCMW_NY(){
    //Invalid Departure...
    //Invalid Route Destination:CMW - Origen:NY 
    if ((document.getElementById('ddlDeparTo').selectedIndex == '1') && (document.getElementById('ddlDeparFrom').selectedIndex == '1')) {
        //When flight destination is Camaguey we must select Miami as our origin.
        alert('When flight origen is New York you must select Havana as your destination. The System will do it for you now.');
        document.getElementById('ddlDeparTo').selectedIndex = '0';
    }
}

//Fix invalid return route CMW-NY.
function fixReturnCMW_NY(){
    //Invalid Return...
    //Invalid Route Origen:CMW - Destination:NY
    if ((document.getElementById('ddlRetFrom').selectedIndex == '1') && (document.getElementById('ddlRetTo').selectedIndex == '1')) {
        //When flight origen is Camaguey we must select Miami as our destination.
        alert('When flight origen is Camaguey you must select Miami as your destination. The System will do it for you now.');
        document.getElementById('ddlRetTo').selectedIndex = '0';
    }
}

//Fix invalid return route NY-CMW. ***To make it more readable this option is treated as a separate function.***
function fixReturnNY_CMW(){
    //Invalid Return...
    //Invalid Route Destination:NY - Origen:CMW 
    if ((document.getElementById('ddlRetTo').selectedIndex == '1') && (document.getElementById('ddlRetFrom').selectedIndex == '1')) {
        //When flight destination is New York we must select Havana as our origen.
        alert('When flight origen is Camaguey you must select Miami as your destination. The System will do it for you now.');
        document.getElementById('ddlRetTo').selectedIndex = '0';
    }
}

//Client-side Validations for Cities.apsx.
function validations_Cities(){

    //Default to no error.
    validationError = false;

    //Validate Paxs...
    validationError = validatePax();

    //Validate Routes...

    //ddlDeparFrom 0:Miami 1:New York

    //ddlDeparTo 0:Havana 1:Camaguey

    //ddlRetFrom 0:Havana 1:Camaguey

    //ddlRetTo 0:Miami 1:New York

    //Departure. 
    //NY-CMW
    if ((document.getElementById('ddlDeparFrom').selectedIndex == '1') && (document.getElementById('ddlDeparTo').selectedIndex == '1')) {
        //When flight origen is New York we must select Havana as our destination.
        //Display error message.
        alert('Invalid Departure: When flight origen is New York you must select Havana as your destination.');
        //Invalid Route Origen:NY - Destination:CMW
        validationError = true;
    }
    //CMW-NY
    else if ((document.getElementById('ddlDeparTo').selectedIndex == '1') && (document.getElementById('ddlDeparFrom').selectedIndex == '1')) {
        //When flight destination is Camaguey we must select Miami as our origin.
        //Display error message.
        alert('Invalid Departure: When flight destination is Camaguey you must select Miami as your origin.');
        //Invalid Route Destination:CMW - Origen:NY 
        validationError = true;
    }

    //Return.
    //Only if RT selected. 
    if (document.getElementById('rbtnRT').checked) {
        //CMW-NY
        if ((document.getElementById('ddlRetFrom').selectedIndex == '1') && (document.getElementById('ddlRetTo').selectedIndex == '1')) {
            //Display error message.
            //When flight origen is Camaguey we must select Miami as our destination.
            alert('Invalid Return: When flight origen is Camaguey you must select Miami as your destination.');
            //Invalid Route Origen:CMW - Destination:NY
            validationError = true;
        }
        //NY-CMW
        else if ((document.getElementById('ddlRetTo').selectedIndex == '1') && (document.getElementById('ddlRetFrom').selectedIndex == '1')) {
            //Display error message.
            //When flight destination is New York we must select Havana as our origen.
            alert('Invalid Return: When flight destination is New York you must select Havana as your origen.');
            //Invalid Route Destination:NY - Origen:CMW 
            validationError = true
        }
    }
    
    //If validation is error free...
    if (validationError == false) { submitPage(); }

}

/*Find absolute position X and Y of an Object.  
Credit to: Peter-Paul Koch & Alex Tingle.
*/
function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

/*Current date script  
Credit to: JavaScript Kit (www.javascriptkit.com)
*/
//Get Date.
function getDate() {
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
return dayarray[day]+", "+montharray[month]+" "+daym+", "+year
}

//Get year.
function getYear() {
var mydate=new Date();
var year=mydate.getYear();
if (year < 1000)
year+=1900
return year;
}
