//$$Revision: 3 $
//$$Workfile: common.js $

function ULRollOver(which,image)
{
    document.getElementById('navbg1').style.background = image;
}

function DisableSelect()
{
    $$('select').invoke('hide');
}

function EnableSelect()
{
    $$('select').invoke('show');
}

function Submit_frmNavigation(Action,StateYN)
{
    if (StateYN == 'N')
        $('AtopWebStateId').value = '';
    $('frmNavigation_AtopWebAction').value = Action;
    $('frmNavigation').submit();
}

function FOFunc()
{
    //document.getElementById('SelectAccTitle').innerHTML = "<label>Select Preferred Accommodation</label>";
    //document.searchform.accomm.disabled = true;
    //document.searchform.airport.disabled = false;
}

function BHFunc()
{
  if(document.getElementById('bhTab') && document.getElementById('crTab') && document.getElementById('csTab'))
  {
    document.getElementById('bhTab').className = 'first selected';
    document.getElementById('crTab').className = '';
    document.getElementById('csTab').className = '';
  }
  
  var thedurationSelect = document.getElementById('theduration');
  thedurationSelect.innerHTML = "";
  var nights = ["7", "10", "11", "14"];
  for (i = 0; i < nights.length; i++)
  {
    if (nights[i] == getCookie("theduration"))
    {
      //thedurationSelect.innerHTML = thedurationSelect.innerHTML + '<option value="'+nights[i]+'" selected="selected">'+nights[i]+' nights</option>';
      option = document.createElement('option');
      option.value = nights[i];
      option.text = nights[i] + ' nights';
      option.selected = true;
      try 
      {
        thedurationSelect.add(option,null); // Standards compliant.
      } catch (e) {
        thedurationSelect.add(option);      // IE only version.
      }
    }
    else
    {
      //thedurationSelect.innerHTML = thedurationSelect.innerHTML + '<option value="'+nights[i]+'">'+nights[i]+' nights</option>';
      option = document.createElement('option');
      option.value = nights[i];
      option.text = nights[i] + ' nights';
      try 
      {
        thedurationSelect.add(option,null); // Standards compliant.
      } catch (e) {
        thedurationSelect.add(option);      // IE only version.
      }
    }
  }  
  
  var PostURL = '/AnitePage.aspx?p=CS00020';
  aniteAjaxReqPost('destinationdiv',PostURL,false, "handleAjaxResponseDestination('PACKAGE');");
  
  PostURL = '/AnitePage.aspx?p=CS00015&WHICH=PACKAGE';
  aniteAjaxReqPost('accommdiv',PostURL,false, "DynamicFields(document.getElementById('destination').value, 'PACKAGE');");
  
  //set hidden form field, so if we submit, we can then set a cookie (we need to know for which tab they were on before, so we can preselect it on error pages)
  document.getElementById('Which').value="BH";
}


function CRFunc()
{
  if(document.getElementById('bhTab') && document.getElementById('crTab') && document.getElementById('csTab'))
  {
    document.getElementById('bhTab').className = '';
    document.getElementById('crTab').className = 'first selected';
    document.getElementById('csTab').className = '';
  }
  
  var thedurationSelect = document.getElementById('theduration');
  thedurationSelect.innerHTML = "";
  var nights = ["7", "10", "11", "14"];
  for (i = 0; i < nights.length; i++)
  {
    if (nights[i] == getCookie("theduration"))
    {
      //thedurationSelect.innerHTML = thedurationSelect.innerHTML + '<option value="'+nights[i]+'" selected="selected">'+nights[i]+' nights</option>';
      option = document.createElement('option');
      option.value = nights[i];
      option.text = nights[i] + ' nights';
      option.selected = true;
      try 
      {
        thedurationSelect.add(option,null); // Standards compliant.
      } catch (e) {
        thedurationSelect.add(option);      // IE only version.
      }
    }
    else
    {
      //thedurationSelect.innerHTML = thedurationSelect.innerHTML + '<option value="'+nights[i]+'">'+nights[i]+' nights</option>';
      option = document.createElement('option');
      option.value = nights[i];
      option.text = nights[i] + ' nights';
      try 
      {
        thedurationSelect.add(option,null); // Standards compliant.
      } catch (e) {
        thedurationSelect.add(option);      // IE only version.
      }
    }
  }  
  
  var cPos = 0;
  var GeogLevel;
  for (var idx=0; idx < OutArrAir.length; idx++) 
  {
      if (OutArrAir[idx][0] == "CRU")
      {
        CurPos = cPos+1;
        GeogLevel = OutArrAir[idx][3];
      }
      cPos++;
  }
    
  var PostURL = '/AnitePage.aspx?p=CS00019';
  aniteAjaxReqPost('destinationdiv',PostURL,false, "handleAjaxResponseDestination('CRUISE');");
  
  PostURL = '/AnitePage.aspx?p=CS00015&WHICH=CRUISE';
  aniteAjaxReqPost('accommdiv',PostURL,false, "DynamicFields(document.getElementById('destination').value, 'CRUISE');");
  
  //set hidden form field, so if we submit, we can then set a cookie (we need to know for which tab they were on before, so we can preselect it on error pages)
  document.getElementById('Which').value="CR";
}


function CSFunc()
{
  if(document.getElementById('bhTab') && document.getElementById('crTab') && document.getElementById('csTab'))
  {
    document.getElementById('bhTab').className = '';
    document.getElementById('crTab').className = '';
    document.getElementById('csTab').className = 'first selected';
  }
  
  //Cruise and Stay Duration options are limited to 14 (default)
  var thedurationSelect = document.getElementById('theduration');
  thedurationSelect.innerHTML = "";
  var nights = ["14"];
  for (i = 0; i < nights.length; i++)
  {
    if (nights[i] == getCookie("theduration"))
    {
      //thedurationSelect.innerHTML = thedurationSelect.innerHTML + '<option value="'+nights[i]+'" selected="selected">'+nights[i]+' nights</option>';
      option = document.createElement('option');
      option.value = nights[i];
      option.text = nights[i] + ' nights';
      option.selected = true;
      try 
      {
        thedurationSelect.add(option,null); // Standards compliant.
      } catch (e) {
        thedurationSelect.add(option);      // IE only version.
      }
    }
    else
    {
      //thedurationSelect.innerHTML = thedurationSelect.innerHTML + '<option value="'+nights[i]+'">'+nights[i]+' nights</option>';
      option = document.createElement('option');
      option.value = nights[i];
      option.text = nights[i] + ' nights';
      try 
      {
        thedurationSelect.add(option,null); // Standards compliant.
      } catch (e) {
        thedurationSelect.add(option);      // IE only version.
      }
    }
  }
  
  //no cookie, set default to be 14
  if (getCookie("theduration") == "")
  {
    thedurationSelect.options[0].selected = true;
  }  
  
  var cPos = 0;
  var GeogLevel;
  for (var idx=0; idx < OutArrAir.length; idx++) 
  {
      if (OutArrAir[idx][0] == "CRU")
      {
        CurPos = cPos+1;
        GeogLevel = OutArrAir[idx][3];
      }
      cPos++;
  }
    
  var PostURL = '/AnitePage.aspx?p=CS00021';
  aniteAjaxReqPost('destinationdiv',PostURL,false, "handleAjaxResponseDestination('CRUISEANDSTAY');");
  
  PostURL = '/AnitePage.aspx?p=CS00015&WHICH=CRUISEANDSTAY';
  aniteAjaxReqPost('accommdiv',PostURL,false, "DynamicFields(document.getElementById('destination').value, 'CRUISEANDSTAY');");
  
  //set hidden form field, so if we submit, we can then set a cookie (we need to know for which tab they were on before, so we can preselect it on error pages)
  document.getElementById('Which').value="CS";
}



function DynamicFields(GeogLevel, WHICH)
{
  if (GeogLevel != "")
  {
    for (var idx=0; idx < OutArrAir.length; idx++)
    {
      if (OutArrAir[idx][3] == GeogLevel)
      {
        if (OutArrAir[idx][7] == "Y")
        {
          var PostURL = '/AnitePage.aspx?p=CS00013_'+GeogLevel+'&WHICH='+WHICH;
          aniteAjaxReqPost('accommdiv',PostURL,false, "handleAjaxResponseAccommodation("+WHICH+");");
        }
        if (OutArrAir[idx][7] == "N")
        {
          var PostURL = '/AnitePage.aspx?p=CS00015_'+GeogLevel+'&WHICH='+WHICH;
          aniteAjaxReqPost('accommdiv',PostURL,false, "handleAjaxResponseAccommodation("+WHICH+");");
        }
      }
    }
  }
  
  var cPos = 0;
  var SelGeogLevel;
  for (var idx=0; idx < OutArrAir.length; idx++) 
  {
    if (OutArrAir[idx][0] == "LXR")
    {
      CurPos = cPos+1;
      SelGeogLevel = OutArrAir[idx][3];
    }
    cPos++;
  }
    
  if (GeogLevel != SelGeogLevel)
  {
    //document.getElementById('crTab').className = '';
    //document.getElementById('bhTab').className = 'first selected';
  }
  
  //incase neither of the ajax calls are called, we set the drop down again
  setDropDownByValue("search_accomm", getCookie("search_accomm")); 
}

//this function is called after all the ajax stuff for Destination is finished
function handleAjaxResponseDestination(WHICH)
{
  if(getCookie("airport") != "")
  {
    setDropDownByValue("airport", getCookie("airport")); 
  }
  if(getCookie("destination") != "")
  {
    setDropDownByValue("destination", getCookie("destination")); 
    DynamicFields(getCookie("destination", WHICH));
  }
}

//this function is called after all the ajax stuff for Accommodation is finished
function handleAjaxResponseAccommodation()
{
  if(getCookie("search_accomm") != "")
  {
    setTimeout('setDropDownByValue("search_accomm", getCookie("search_accomm"))',3000);
    //setDropDownByValue("search_accomm", getCookie("search_accomm")); 
  }
}
    
function setValues()
{
    var INCCompCode;
    var AOCompCode;
    var FOCompCode;

    document.getElementById('Search_AtopWebAction').value = 'New';
    document.getElementById('Search_OutDepAir').value = document.getElementById('airport').value;
    document.getElementById('Search_RetArrAir').value = document.getElementById('airport').value;

    document.getElementById('Search_ExternalDataLoc').value = document.getElementById('destination').value;

    for (var idx=0; idx < OutArrAir.length; idx++)
    {
      if(document.getElementById('Search_ExternalDataLoc').value == OutArrAir[idx][3])
      {
        document.getElementById('Search_ExternalDataLoc').value = OutArrAir[idx][2];
      }
    }

    for (var idx=0; idx < OutArrAir.length; idx++)
    {
      if (OutArrAir[idx][3] == document.getElementById('destination').value)
      {
        document.getElementById('Search_OutArrAir').value = OutArrAir[idx][0];
        document.getElementById('Search_RetDepAir').value = OutArrAir[idx][0];

        INCCompCode = OutArrAir[idx][4];
        AOCompCode = OutArrAir[idx][5];
        FOCompCode = OutArrAir[idx][6];

        if (OutArrAir[idx][7] == "Y")
        {
          document.getElementById('Search_Resort').value = OutArrAir[idx][0];
        }
        else
        {
          document.getElementById('Search_Resort').value = "";
        }
      }
    }
    if (document.getElementById('bhTab').className == 'first selected')
    {
      if (document.getElementById('search_accomm').value != "")
      {
        document.getElementById('Search_MainAccom').value = document.getElementById('search_accomm').value;
      }
      else
      {
        document.getElementById('Search_MainAccom').value = "";
      }

      document.getElementById('Search_AtopWebMethod').value = 'AvailSearch';
      document.getElementById('Search_AtopWebStateId').value = "";
      document.getElementById('Search_Company').value = "R";
    }
    else if (document.getElementById('crTab').className == 'first selected')
    {
      if (document.getElementById('search_accomm').value != "")
      {
        document.getElementById('Search_MainAccom').value = document.getElementById('search_accomm').value;
        document.getElementById('Search_AtopWebMethod').value = 'AvailSearch';
        document.getElementById('Search_AtopWebStateId').value = "";
        //document.searchform.action = '/book/spc/atopcweb.aspx';
      }
      else
      {
        document.getElementById('Search_MainAccom').value = "";
        document.getElementById('Search_AtopWebMethod').value = 'AvailSearch';
        document.getElementById('Search_AtopWebStateId').value = "";
        //document.searchform.action = '/book/ava/atopcweb.aspx';
      }

      document.getElementById('Search_OutArrAir').value = "LXR";
      document.getElementById('Search_RetDepAir').value = "LXR";
      document.getElementById('Search_Company').value = "C";
    }
    else if (document.getElementById('csTab').className == 'first selected')
    {
      if (document.getElementById('search_accomm').value != "")
      {
        document.getElementById('Search_MainAccom').value = document.getElementById('search_accomm').value;
      }
      else
      {
        document.getElementById('Search_MainAccom').value = "";
      }

      document.getElementById('Search_AtopWebMethod').value = 'AvailSearch';
      document.getElementById('Search_AtopWebStateId').value = "";
      
      if (document.getElementById('destination').value == 1217 || document.getElementById('destination').value == 1218) //Cruise and Stay Holidays || &nbsp;&nbsp;&nbsp;Nile Cruise and Stay
      {
        document.getElementById('Search_OutArrAir').value = "HRG";
        document.getElementById('Search_RetDepAir').value = "HRG";
      }
      if (document.getElementById('destination').value == 1219) //&nbsp;&nbsp;&nbsp;Red Sea Cruise and Stay
      {
        document.getElementById('Search_OutArrAir').value = "SSH";
        document.getElementById('Search_RetDepAir').value = "SSH";
      }
      
      document.getElementById('Search_Company').value = "S";
    }
    //shoudln't happen
    else if (document.getElementById('fo').checked)
    {
      document.getElementById('Search_AtopWebMethod').value = 'SpecificFlightOnly';
      document.getElementById('Search_Company').value = FOCompCode;
      document.getElementById('Search_AtopWebStateId').value = "";
      document.searchform.action = '/book/spc/atopcweb.aspx';
    
    }
}

function ISURememberNU()
{
    var CookieName = "ISU_Remember";
    if(document.cookie)
    {
        index = document.cookie.indexOf(CookieName);
    }
    else
    {
        index = -1;
    }

    if (index == -1)
    {
        document.cookie=CookieName+"=1; expires=Wednesday, 01-Aug-2010 08:00:00 GMT";
    }
    else
    {
        countbegin = (document.cookie.indexOf("=", index) + 1);
        countend = document.cookie.indexOf(";", index);
        if (countend == -1)
        {
            countend = document.cookie.length;
        }
        count = eval(document.cookie.substring(countbegin, countend)) + 1;
        var ISUFields = document.getElementById('AtopWebMethod').value+":"+document.getElementById('OutDepAir').value+":"+document.getElementById('Resort').value+":"+document.getElementById('MainAccom').value;
        document.cookie=CookieName+":"+ISUFields+"; expires=Wednesday, 01-Aug-2010 08:00:00 GMT";
    }
}

        
function ISUReminderNU()
{
    var CookieName = "ISU_Remember";
    if(document.cookie)
    {
        index = document.cookie.indexOf(CookieName);
        if (index != -1)
        {
            countbegin = (document.cookie.indexOf("=", index) + 1);
            countend = document.cookie.indexOf(";", index);                        
            var text1 = document.cookie.substring(countbegin, countend);
        }
    }
    return (text1+" = testa");
}


function addDays(Day,Month,Year,Duration)
{   
    var RetDate = new Date();

    RetDate.setDate(Day);
    RetDate.setMonth(Month-1);
    RetDate.setFullYear(Year);
    
    RetDate = new Date(RetDate.getTime() + Duration*24*60*60*1000);
        
    var tMonth = RetDate.getMonth();
    RetDay = RetDate.getDate();
    RetMonth = RetDate.getMonth();
    RetYear = RetDate.getFullYear();
 
    return RetDate;
}

function SetDates()
{
    var DepDay = document.getElementById('depday').value;
    var DepMonthYear = document.getElementById('depmonth').value;
    var DepMonth = DepMonthYear.substr(0,2);
    var DepYear = DepMonthYear.substr(2,2)
    var RetDate = addDays(DepDay,DepMonth,"20"+DepYear,document.getElementById('theduration').value);
    var RetDay  = RetDate.getDate();
    var tMonth = RetDate.getMonth();
    var RetMonth = getMonth(tMonth+1);
    var RetYear = RetDate.getFullYear();
    
    document.getElementById('Search_RetDepDate').value = RetDay+" "+RetMonth+" "+RetYear;
    DepDay  = document.getElementById('depday').value;
    DepMonth = getMonth(DepMonth);
    DepYear = "20"+DepYear;
    document.getElementById('Search_OutDepDate').value = DepDay+" "+DepMonth+" "+DepYear;

    setValues();
}

function getMonth(Month)
{
    var DepMonth;
    switch (Month) 
    {
        case '01':
            DepMonth = 'Jan';
            break;
        case 1:
            DepMonth = 'Jan';
            break;
        case '02':
            DepMonth = 'Feb';
            break;
        case 2:
            DepMonth = 'Feb';
            break;
        case '03':
            DepMonth = 'Mar';
            break;
        case 3:
            DepMonth = 'Mar';
            break;
        case '04':
            DepMonth = 'Apr';
            break;
        case 4:
            DepMonth = 'Apr';
            break;
        case '05':
            DepMonth = 'May';
            break;
        case 5:
            DepMonth = 'May';
            break;
        case '06':
            DepMonth = 'Jun';
            break;
        case 6:
            DepMonth = 'Jun';
            break;
        case '07':
            DepMonth = 'Jul';
            break;
        case 7:
            DepMonth = 'Jul';
            break;
        case '08':
            DepMonth = 'Aug';
            break;
        case 8:
            DepMonth = 'Aug';
            break;
        case '09':
            DepMonth = 'Sep';
            break;
        case 9:
            DepMonth = 'Sep';
            break;
        case '10':
            DepMonth = 'Oct';
            break;
        case 10:
            DepMonth = 'Oct';
            break;
        case '11':
            DepMonth = 'Nov';
            break;
        case 11:
            DepMonth = 'Nov';
            break;
        case '12':
            DepMonth = 'Dec';
            break;
        case 12:
            DepMonth = 'Dec';
            break;
        default: DepMonth = 'unknown';
    }
    return DepMonth;
}


function getNumMonth(Month)
{
    var DepMonth;
    switch (Month) 
    {
        case 'Jan':
            DepMonth = '01';
            break;
        case 'Feb':
            DepMonth = '02';
            break;
        case 'Mar':
            DepMonth = '03';
            break;
        case 'Apr':
            DepMonth = '04';
            break;
        case 'May':
            DepMonth = '05';
            break;
        case 'Jun':
            DepMonth = '06';
            break;
        case 'Jul':
            DepMonth = '07';
            break;
        case 'Aug':
            DepMonth = '08';
            break;
        case 'Sep':
            DepMonth = '09';
            break;
      case 'Oct':
          DepMonth = '10';
          break;
        case 'Nov':
            DepMonth = '11';
            break;
        case 'Dec':
            DepMonth = '12';
            break;
        default: DepMonth = 'unknown';
    }
    return DepMonth;
}

function InitOptions(GeogLevel)
{
  var UpperLevelVal = 0;
  if (document.getElementById('airport') != null)
  {
    /*Creates the airport dropdown list**********************************************************************/
    var cPos = 0;
    document.getElementById('airport').options[cPos] = new Option("Please select","");
    cPos++;

    for (var idx=0; idx < OutDepAir.length; idx++) 
    {
      document.getElementById('airport').options[cPos] = new Option(OutDepAir[idx][1], OutDepAir[idx][0]);
      cPos++;
    }
    
    /*Creates the destination dropdown list******************************************************************/
    var cPos = 0;
    document.getElementById('destination').options[cPos] = new Option("Please select","");
    cPos++;
    for (var idx=0; idx < OutArrAir.length; idx++) 
    {
      document.getElementById('destination').options[cPos] = new Option(OutArrAir[idx][2], OutArrAir[idx][3]);
      cPos++;
    }
    
    /*Gets position of the current resort if available ******************************************************/
    var cPos = 1;
    var GeogL2Found = "No";
    for (var idx=0; idx < OutArrAir.length; idx++) 
    {
      if (OutArrAir[idx][0] == GeogLevel)
      {
        CurPos = cPos;
        GeogL2Found = "Yes";
      }
      cPos++;
    }
    if (GeogL2Found == "Yes")
    {
      document.getElementById('destination').selectedIndex = CurPos;
    }
    else
    {
      /*Gets position of the current accommodation if available ***********************************************/
      var cPos = 1;
      for (var idx=0; idx < Accomm.length; idx++) 
      {
        if (Accomm[idx][1] == GeogLevel)
        {
          CurPos = cPos;
          UpperLevelVal = Accomm[idx][3];
        }
        cPos++;
      }
      
      /*Sets position of the current accomm if available ******************************************************/
      if (GeogLevel == "")
      {
        document.getElementById('search_accomm').selectedIndex = 0;
      }
      else
      {
        document.getElementById('search_accomm').selectedIndex = CurPos;
      }
      
      var cPos = 1;
      for (var idx=0; idx < OutArrAir.length; idx++) 
      {
        if (OutArrAir[idx][3] == UpperLevelVal)
        {
          CurPos = cPos;
        }
        cPos++;
      }
      
      /*Sets position of the current destiantion if available *************************************************/
      if (GeogLevel == "")
      {
        document.getElementById('destination').selectedIndex = 0;
      }
      else
      {
        document.getElementById('destination').selectedIndex = CurPos;
      }
    }
  }
    
  //only check for cookies once everything is loaded
  if (window.location.href.search("OutDepAir") ==-1) 
  {
    if (GeogLevel != "")
    {
      getSomeCookies(); //for specific resort/hotel pages resort/hotel will override
    }
    else
    {
      getAllCookies(); //other pages
      //for search result pages we want to show which tab the search post came from
      switch (gup("Which"))
      {
        case "BH":
          BHFunc();
          break;
        case "CR":
          CRFunc();
          break;
        case "CS":
          CSFunc();
          break;
          
        default:
          BHFunc();
          break;          
      }
    }
  }
  else //deep link, we will use URL variables as cookies (ie as if the user did the search themselves)
  {
      switch (gup("Which"))
      {
        case "BH":
          BHFunc();
          break;
        case "CR":
          CRFunc();
          break;
        case "CS":
          CSFunc();
          break;
          
        default:
          BHFunc();
          break;          
      }
  
    /*
    if(gup("Which") == "CR")
    {
      //setCookie("tab", "CR", 30, 0);
      CRFunc();
    }
    else
    {
      //setCookie("tab", "BH", 30, 0);
      BHFunc();
    }
    */
    setCookie("airport", gup("OutDepAir"), 30, 0);
    
    if (gup("OutArrAir") == "HRG")
    {
      setCookie("destination", 660, 0, 10);
    }
    if (gup("OutArrAir") == "LXR")
    {
      setCookie("destination", 759, 0, 10);
    }
    if (gup("OutArrAir") == "SSH")
    {
      setCookie("destination", 643, 0, 10);
    }
    
    setCookie("search_accomm", gup("MainAccom"), 0, 10);
    //02+Dec+2011
    //0123456789
    var depday   = gup("OutDepDate").substring(0, 2);
    var depmonth = gup("OutDepDate").substring(3, 6);
        depmonth = monthToNumber(depmonth);
    var depyear  = gup("OutDepDate").substring(9, 11);

    setCookie("depday", depday, 0, 10);
    setCookie("depmonth", depmonth+depyear, 0, 10);
    
    var retdepday   = gup("RetDepDate").substring(0, 2);
    var retdepmonth = gup("RetDepDate").substring(3, 6);
        retdepmonth = monthToNumber(depmonth);
    var retdepyear  = gup("RetDepDate").substring(9, 11);
    
    var departure_date    = new Date(depyear, depmonth, depday);
    var retdeparture_date = new Date(retdepyear, retdepmonth, retdepday);
    
    setCookie("theduration", (retdeparture_date - departure_date) / 1000 / (24*60*60), 0, 10); //milliseconds so divid by 1000 first
    
    setCookie("adults", gup("AdultPax"), 0, 10);
    setCookie("children", gup("ChildPax"), 0, 10);
    setCookie("infants", gup("InfantPax"), 0, 10);
  
    getAllCookies();
    
    //CALL THESE AGAIN INCASE GETALLCOOKIES SWITCHS THE TAB BACK
    /*
    if(gup("Which") == "CR")
    {
      CRFunc();
    }
    else
    {
      BHFunc();
    }
    */
    switch (gup("Which"))
    {
      case "BH":
        BHFunc();
        break;
      case "CR":
        CRFunc();
        break;
      case "CS":
        CSFunc();
        break;
        
      default:
        BHFunc();
        break;
    }
    
  }
}

//http://www.netlobo.com/url_query_string_javascript.html
//url parameter parsing
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
  {
    return "";
  }
  else
  {
    return results[1];
  }
}

function monthToNumber(depmonth)
{
  switch(depmonth)
  {
    case "Jan":
      depmonth = "01";
      break;
    case "Feb":
      depmonth = "02";
      break;        
    case "Mar":
      depmonth = "03";
      break; 
    case "Apr":
      depmonth = "04";
      break; 
    case "May":
      depmonth = "05";
      break; 
    case "Jun":
      depmonth = "06";
      break; 
    case "Jul":
      depmonth = "07";
      break; 
    case "Aug":
      depmonth = "08";
      break; 
    case "Sep":
      depmonth = "09";
      break; 
    case "Oct":
      depmonth = "10";
      break; 
    case "Nov":
      depmonth = "11";
      break; 
    case "Dec":
      depmonth = "12";
      break;         
  }
  
  return depmonth;
}

function getSomeCookies()
{
  if(getCookie("airport") != "")
  {
    setDropDownByValue("airport", getCookie("airport")); 
  }
  
  if(getCookie("depday") != "")
  {
    setDropDownByValue("depday", getCookie("depday")); 
  } 
  if(getCookie("depmonth") != "")
  {
    setDropDownByValue("depmonth", getCookie("depmonth")); 
  }
  if(getCookie("duration") != "")
  {
    setDropDownByValue("theduration", getCookie("theduration")); 
  }
  if(getCookie("adults") != "")
  {
    setDropDownByValue("adults", getCookie("adults")); 
  }
  if(getCookie("children") != "")
  {
    setDropDownByValue("children", getCookie("children")); 
  }  
  if(getCookie("infants") != "")
  {
    setDropDownByValue("infants", getCookie("infants")); 
  }
}

var isCruisePage = false;;
function getAllCookies()
{
  if(getCookie("tab") == "BH")
  {
    BHFunc();
  }
  if(getCookie("tab") == "CR" || isCruisePage)
  {
    CRFunc();
  }
  if(getCookie("tab") == "CS")
  {
    CSFunc();
  }

  if(getCookie("depday") != "")
  {
    setDropDownByValue("depday", getCookie("depday")); 
  } 
  if(getCookie("depmonth") != "")
  {
    setDropDownByValue("depmonth", getCookie("depmonth")); 
  }
  if(getCookie("duration") != "")
  {
    setDropDownByValue("theduration", getCookie("theduration")); 
  }
  if(getCookie("adults") != "")
  {
    setDropDownByValue("adults", getCookie("adults")); 
  }
  if(getCookie("children") != "")
  {
    setDropDownByValue("children", getCookie("children")); 
  }  
  if(getCookie("infants") != "")
  {
    setDropDownByValue("infants", getCookie("infants")); 
  }
}


function SendNewsletter(thisform)
{
  var result = false;
    var formdata = "";

    // Set up Ajax request variable
    try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");}  catch (e) { alert("Error: Could not load page.");}

    // Loop through form fields
    for (i=0; i < document.getElementById(thisform).length; i++)
    {
        //Build Send String
        if ((document.getElementById(thisform).elements[i].name != undefined) || (document.getElementById(thisform).elements[i].name != "") || (document.getElementById(thisform).elements[i].name != "x"))
        {
            if(document.getElementById(thisform).elements[i].type == "text")
            {
                formdata = formdata + document.getElementById(thisform).elements[i].name + "=" + escape(document.getElementById(thisform).elements[i].value) + "&";
            }
            else if(document.getElementById(thisform).elements[i].type == "textarea")
            {
                formdata = formdata + document.getElementById(thisform).elements[i].name + "=" + escape(document.getElementById(thisform).elements[i].value) + "&";
            }
            else if(document.getElementById(thisform).elements[i].type == "checkbox")
            {
                if(document.getElementById(thisform).elements[i].checked == true)
                {
                    formdata = formdata + document.getElementById(thisform).elements[i].name + "=" + escape(document.getElementById(thisform).elements[i].value) + "&";
                }
            }
            else if(document.getElementById(thisform).elements[i].type == "radio")
            {
                if(document.getElementById(thisform).elements[i].checked == true)
                {
                    formdata = formdata + document.getElementById(thisform).elements[i].name + "=" + document.getElementById(thisform).elements[i].value + "&";
                }
            }
            else
            {
                //finally, this should theoretically this is a select box.
                formdata = formdata + document.getElementById(thisform).elements[i].name + "=" + escape(document.getElementById(thisform).elements[i].value) + "&";
            }
        }
    }

    //Send Ajax Request
    xmlhttp.onreadystatechange = function()
    {
        //Check page is completed and there were no problems.
        if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
        {           
            //send lynchpin tracking confirmation
            //send double click spotlight tracking
            //if agent
            var url = top.document.location.href;
      var axel = Math.random()+"";
      var a = axel * 10000000000000;
      var doubleClickUrl = 'http://fls.doubleclick.net/activityi;src=2760025;type=count239;cat=003ne826;ord=1;num='+ a + '?';

            if(url.indexOf("agent") != -1)
            {
    var sType = 'agent-newsletter';
    var sRef = '';
    var sValue = '0.00';
    var trkURL='https://lypn.com/lp/?s=477d1be6b4d9abc7&a='+escape(sType)+'&b='+escape(sRef)+'&c='+escape(sValue);
    document.getElementById("newsletter").innerHTML = '<img width="1" height="1" border="0" alt="" src="'+trkURL+'"><IFRAME SRC="' + doubleClickUrl + '" WIDTH=1 HEIGHT=1></IFRAME>';
            }
            //else
            else
            {
    var sType = 'direct-newsletter';
    var sRef = '';
    var sValue = '0.00';
    var trkURL='https://lypn.com/lp/?s=477d1be6b4d9abc7&a='+escape(sType)+'&b='+escape(sRef)+'&c='+escape(sValue);
    document.getElementById("newsletter").innerHTML = '<img width="1" height="1" border="0" alt="" src="'+trkURL+'"><IFRAME SRC="' + doubleClickUrl + '" WIDTH=1 HEIGHT=1></IFRAME>';            
            }
            
            //alert("Your comments have been submitted");
            document.getElementById("newsletter").style.display = "none";
            document.getElementById('newsletter-sent').style.display = "block";
        }
    }

    //Make connection
    xmlhttp.open("POST", 'requestinsert.asp');

    //Set Headers
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    //Send data
    xmlhttp.send(formdata);

    //stops form from submitting normally
    return;
}

function NUSetOfferFieldsNU(Acc,Which,Detail,Dest,Island)
{
  var OfferString = Detail;
  var Spl = ',';
  var OfferArray = OfferString.split(Spl);
  document.getElementById('OutDepAir').value = OfferArray[0];
  document.getElementById('RetArrAir').value = OfferArray[0];
  document.getElementById('OutDepDate').value = OfferArray[1];
  var Duration = OfferArray[2];

  var OutDate = document.getElementById('OutDepDate').value;
  var DepDay = OutDate.substr(0,2);
  var DepMonth = OutDate.substr(3,3);
  var DepYear = OutDate.substr(7,2);
  document.getElementById('OutDepDate').value = DepDay+" "+DepMonth+" 20"+DepYear;

    DepMonth = getNumMonth(DepMonth);
  var RetDate = addDays(DepDay,DepMonth,DepYear,Duration);
  var tMonth = RetDate.getMonth();
  
    var tMonth = RetDate.getMonth();
    var RetMonth = getMonth(tMonth+1);
    var RetDay = RetDate.getDate();moveBy
  RetYear = RetDate.getFullYear();
  var RetYear = "20"+RetYear;
  document.getElementById('RetDepDate').value = RetDay+" "+RetMonth+" "+RetYear;

    document.getElementById('adults').value = document.getElementById('AdultPax1').value;
    document.getElementById('children').value = document.getElementById('ChildPax1').value;
    document.getElementById('infants').value = document.getElementById('InfantPax1').value;
    
  document.getElementById('OutArrAir').value = Dest;
  document.getElementById('RetDepAir').value = Dest;
  document.getElementById('MainAccom').value = Acc;
  document.getElementById('SelectedAccom').value = Acc;
  document.getElementById('ExternalDataLoc').value = Island;
  
  if (Which == 'FO')
  {
      document.getElementById('Company').value = OfferArray[9];
      document.getElementById('MainAccom').value = '';
      document.getElementById('MainAccom').value = '';
      document.getElementById('SelectedAccom').value = '';
      document.getElementById('AtopWebMethod').value = 'SpecificFlightOnly';
      document.searchform.action = '/book/spc/atopcweb.aspx';
  }
    else if (Which == 'AO')
    {
        document.getElementById('Company').value = OfferArray[8];
        document.getElementById('AtopWebMethod').value = 'SpecificAccomOnly';
        document.searchform.action = '/book/spc/atopcweb.aspx';
    }
    else
    {
        document.getElementById('Company').value = OfferArray[8];
        document.getElementById('AtopWebMethod').value = 'AvailSearch';   
        document.searchform.action = '/book/ava/atopcweb.aspx';
    }
   
}

function NUSubmitOfferNU(Detail,Dest)
{   
  document.getElementById('AtopWebAction').value = 'New';
    document.getElementById('AtopWebStateId').value = '';
  document.searchform.submit();
}

function getElementsByClass(searchClass,node,tag) {
  var classElements = new Array();
  if ( node == null )
    node = document;
  if ( tag == null )
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

function getElementByClass(searchClass,node,tag) {
  var classElements = new Array();
  if ( node == null )
    node = document;
  if ( tag == null )
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements[0];
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

