//$$Revision: 5 $
//$$Workfile: js.js $

function setDropDownByValue(el,val)
{
  if(typeof(el) == "string")  { 
    el = document.getElementById(el); 
  } 
  if(!el) { return false; }
  var elOptions = el.options;
  if(!elOptions) { return false; }
  var i = 0;
  for(i = 0; i < elOptions.length; i++) {
    if(elOptions[i].value == val) {
      elOptions.selectedIndex = i;
      break;
    }
  }
}


function setDropDownByText(el,val)
{
  if(typeof(el) == "string")	{ 
    el = document.getElementById(el);
  }
  if(!el) { return false; }
  var elOptions = el.options;
  if(!elOptions) { return false; }
  var i = 0;
  for(i = 0; i < elOptions.length; i++) {
    if(elOptions[i].text == val) {
      elOptions.selectedIndex = i;
      break;
    }
  }
}
function setDepDay()
{
  var i = 0;
  var date = new Date();
  var elDepDay = getID("depday");
  var optDepDay = elDepDay.options;
  var currentlySelectedDay = optDepDay[optDepDay.selectedIndex].text;
  var selectedMonth = Math.floor(parseInt(getID("depmonth").value,10)/100);
  var daysInMonth = date.getMonthDays(selectedMonth - 1);
  removeAllOptions(optDepDay);
  for(i = 0; i < daysInMonth; i++)
  {
    optDepDay[i] = new Option((i+1).toString().PadLeft('0',2),(i+1).toString().PadLeft('0',2));
  }
  
  setDropDownByText('depday',currentlySelectedDay);
}

function getDepDate()
{
  return new Date(getID("depmonth").value + "/" + getID("depday").value);
}

function dateUpdate(cal)
{
  setDropDownByValue('depmonth',cal.date.print("%m%y"));
  setDepDay();
  setDropDownByValue('depday',cal.date.print("%d"));
}

function takeYear(theDate) {
  x = theDate.getYear();
  var y = x % 100;
  y += (y < 38) ? 2000 : 1900;
  return y;
}

String.prototype.PadLeft = function(charToPad, finalLength) {
  var tmp = this;
  if(typeof(finalLength) == 'string') {
    finalLength = parseInt(finalLength,10);
  }
  while(tmp.length < finalLength)
  {
    tmp = charToPad + tmp;
  }
  return tmp;
}

String.prototype.Trim = function() {
  var tmp = this;
  tmp = this.replace(/^(\s)*/, '');
  tmp = tmp.replace(/(\s)*$/, '');
  return tmp;
}

String.prototype.ToUpper = function() {
  return this.toUpperCase();
}
String.prototype.ToLower = function() {
  return this.toLowerCase();
}

function removeAllOptions(option)
{
  if(typeof(option) == "object")
  {
    while(option.length > 0)
    {
      option.length = option.length - 1;
    }
  }
}


function getID(id)
{
  return document.getElementById(id);
}
function getPN(id)
{
  return document.getElementById(id).value;
}

function removeError(obj) {
  if(obj.className.indexOf('error') != -1)
  {
    obj.className = obj.className.substring(0,obj.className.lastIndexOf(' '));
  }
}

function radiobutton(radiobutton) {
    chosen = false
    for (var i=0; i<radiobutton.length; i++) {
      if (radiobutton[i].checked) {
              chosen = true;
          }
        }
    if (chosen == false) {
      return -1;
    } else {
      return 1;
    }
}

function validatesearch() {
  var errmess = "";
  var valid = true;	
  
  for (i = 0; i < getID("searchform").elements.length; i++) {
    removeError(getID("searchform").elements[i].parentNode);
    removeError(getID("searchform").elements[i]);
  }
  
  if(document.getElementById("searchtypebox"))
  {
    document.getElementById("searchtypebox").style.color = "#005391";
  }
  
  if (getPN("airport") == "" || getPN("airport") == null) {
    errmess += "Please select an airport. \n";
    valid = false;
  }
  if (getPN("destination") == "" || getPN("destination") == null) {
    errmess += "Please select a destination. \n";
    valid = false;
  }
  
  if (getPN("depday") == "" || getPN("depday") == null) {
    errmess += "Please select a departure day. \n";
    valid = false;
  }
  
  if (getPN("depmonth") == "" || getPN("depmonth") == null) {
    errmess += "Please select a departure month. \n";
    valid = false;
  }
  
  if (getPN("theduration") == "" || getPN("theduration") == null) {
    errmess += "Please select a duration. \n";
    valid = false;
  }
  
  if (getPN("adults") == "" || getPN("adults") == null) {
    errmess += "Please choose the number of adults. \n";
    valid = false;
  }
  
  if (getPN("children") == "" || getPN("children") == null) {
    errmess += "Please choose the number of children. \n";
    valid = false;
  }
  
  if (getPN("infants") == "" || getPN("infants") == null) {
    errmess += "Please choose the number of infants. \n";
    valid = false;
  }
  
  if (errmess != "") {
    alert(errmess);
  }

  if (valid)
  {
    if ($('image-container'))
    {
        Stop();
    }
    SetDates();
    
    //need to set the search action URL to contain search parameters
    
    if($('searchform'))
    {
      //we have a search form, get the action URL.
      var searchUrl = $('searchform').action;
      
      if(searchUrl.indexOf("?") == -1)
      {
        searchUrl += "?"
      }
          
      //add the search URL parameters:
      //styp = search type, (agent or direct) is this possible to discern?
      //paxa = ADULT PAX
      //paxc = CHILD PAX
      //paxi = INFANT PAX
      //depd = DEPARTURE DATE
      //retd = RETURN DATE
      //depa = DEPARTURE AIRPORT
      //arra = ARRIVAL AIRPORT
      //accc = ACCOMMODATION CODE
      //dura = DURATION
      //pname = PAGE NAME
      
      if($('Agent') && $('Operator') && $('Password'))
      {
        if($('Agent').value != "" && $('Operator').value != "" && $('Password').value != "")
        {
          searchUrl += "styp=agent&";
        }
        else
        {
          searchUrl += "styp=direct&"
        }
      }
      else
      {
        searchUrl += "styp=direct&";
      }
      
      if($('adults'))
      {
        if($('adults').value != "")
        {
          searchUrl += "paxa=" + $('adults').value + "&";
        }
        else
        {
          searchUrl += "paxa=0" + "&";
        }
      }
      else
      {
        searchUrl += "paxa=0" + "&";
      }
      
      if($('children'))
      {
        if($('children').value != "")
        {
          searchUrl += "paxc=" + $('children').value + "&";
        }
        else
        {
          searchUrl += "paxc=0" + "&";
        }
      }
      else
      {
        searchUrl += "paxc=0" + "&";
      }
      
      if($('infants'))
      {
        if($('infants').value != "")
        {
          searchUrl += "paxi=" + $('infants').value + "&";
        }
        else
        {
          searchUrl += "paxi=0" + "&";
        }
      }
      else
      {
        searchUrl += "paxi=0" + "&";
      }
      
      if($('Search_OutDepDate'))
      {
        if($('Search_OutDepDate').value != "")
        {
          searchUrl += "depd=" + $('Search_OutDepDate').value.replace(/ /g,"-") + "&";
        }
        else
        {
          searchUrl += "depd=0" + "&";
        }
      }
      else
      {
        searchUrl += "depd=0" + "&";
      }

      if($('Search_RetDepDate'))
      {
        if($('Search_RetDepDate').value != "")
        {
          searchUrl += "retd=" + $('Search_RetDepDate').value.replace(/ /g,"-") + "&";
        }
        else
        {
          searchUrl += "retd=0" + "&";
        }
      }
      else
      {
        searchUrl += "retd=0" + "&";
      }		
      
      if($('Search_OutDepAir'))
      {
        if($('Search_OutDepAir').value != "")
        {
          searchUrl += "depa=" + $('Search_OutDepAir').value + "&";
        }
        else
        {
          searchUrl += "depa=0" + "&";
        }
      }
      else
      {
        searchUrl += "depa=0" + "&";
      }
      
      if($('Search_OutDepAir'))
      {
        if($('Search_OutDepAir').value != "")
        {
          searchUrl += "arra=" + $('Search_OutArrAir').value + "&";
        }
        else
        {
          searchUrl += "arra=0" + "&";
        }
      }
      else
      {
        searchUrl += "arra=0" + "&";
      }
      
      if($('Search_MainAccom'))
      {
        if($('Search_MainAccom').value != "")
        {
          searchUrl += "accc=" + $('Search_MainAccom').value + "&";
        }
        else
        {
          searchUrl += "accc=0" + "&";
        }
      }
      else
      {
        searchUrl += "accc=0" + "&";
      }
      
      if($('theduration'))
      {
        if($('theduration').value != "")
        {
          searchUrl += "dura=" + $('theduration').value + "&";
        }
        else
        {
          searchUrl += "dura=0" + "&";
        }
      }
      else
      {
        searchUrl += "dura=0" + "&";
      }
      
      if (document.getElementById("Which").value == "BH")
      {
        searchUrl += "Which=BH&"; 
      }
      if (document.getElementById("Which").value == "CR")
      {
        searchUrl += "Which=CR&";
      }
      if (document.getElementById("Which").value == "CS")
      {
        searchUrl += "Which=CS&"; 
      }
      
      
      //pagename
      var pname = location.href;
      if (pname.indexOf("&pname") > 0) //strip/remove everything after pname in the url
      {
        pname = pname.substring(0, pname.indexOf("&pname"));  
        pname = pname.substring(pname.lastIndexOf("/") + 1,pname.length);
      }
      else 
      {
        //pname = pname; //do nothing
      }
          
          
      searchUrl += "pname=" + pname;
      
      $('searchform').action = searchUrl;
    
    }
    
    //save the search parameters to cookies
    setCookie("airport", getPN("airport"), 30, 0);
    
    setCookie("destination", getPN("destination"), 0, 10); //approx 10minutes
    setCookie("search_accomm", getPN("search_accomm"), 0, 10); 
    setCookie("depday", getPN("depday"), 0, 10);
    setCookie("depmonth", getPN("depmonth"), 0, 10);
    setCookie("theduration", getPN("theduration"), 0, 10);
    setCookie("adults", getPN("adults"), 0, 10);
    setCookie("children", getPN("children"), 0, 10);
    setCookie("infants", getPN("infants"), 0, 10);
  }
  return(valid);
}



function validateemail(form,email) {
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  var address = document.forms[form].elements[email].value;
  if(reg.test(address) == false) {
    alert('Please Enter A Valid Email Address');
  }
  else
  {
    SendNewsletter(form);
  }
}



function validatetravelagent() {
  var errmess = "";
  var valid = true;	
  
  for (i = 0; i < getID("travelagentform").elements.length; i++) {
    removeError(getID("travelagentform").elements[i].parentNode);
    removeError(getID("travelagentform").elements[i]);
  }
  if (getPN("taaccnumber") == "" || getPN("taaccnumber") == null) {
    writeError("taaccnumber");
    errmess += "Please enter your account number. \n";
    valid = false;
  }
  
  if (getPN("tapassword") == "" || getPN("tapassword") == null) {
    writeError("tapassword");
    errmess += "Please enter your password. \n";
    valid = false;
  }
  
  if (getPN("taref") == "" || getPN("taref") == null) {
    writeError("taref");
    errmess += "Please enter your reference. \n";
    valid = false;
  }
  
  if (errmess != "") {
    alert(errmess);
  }
  
  return valid;			
}

function popup(obj,display,visibility,elem,vis) {
  getID(obj).style.display = display;
  getID(obj).style.visibility = visibility;
  
  var selects = document.getElementsByTagName("select");
  for(i=0;i<selects.length;i++) {
    selects[i].style.visibility = vis;
  }
  return false;
}
