var regTicketNumber = new RegExp("^[0-9]{13,14}$");
var regFlightCode = new RegExp("^[A-Z]{2}$");
var regFlightNumber = new RegExp("^[0-9]{2,4}$");

var internetCheckin = new Array();
internetCheckin[internetCheckin.length] = new Array("ticketNumber", "regTicketNumber.test(value)", "Vyplňte prosím číslo elektronické letenky!");
internetCheckin[internetCheckin.length] = new Array("flightCode", "testFlightCode()", "Vyplňte prosím číslo letu");
internetCheckin[internetCheckin.length] = new Array("flightNumber", "regFlightNumber.test(value)", "Vyplňte prosím číslo letu");

//Exbag
var login_excess_baggage = new Array();
login_excess_baggage[login_excess_baggage.length] = new Array("locator", "value > ''", "Vyplňte prosím Rezervační kód!");
login_excess_baggage[login_excess_baggage.length] = new Array("passenger_surname", "value > ''", "Vyplňte prosím Příjmení cestujícího!");
login_excess_baggage[login_excess_baggage.length] = new Array("passenger_name", "value > ''", "Vyplňte prosím položku Jméno!");

function testFlightCode() {
    var flightCode = jQ("select[@name = 'flightCode']").val();
    if(regFlightCode.test(flightCode)) return true;
    else return false;
}

    function showCookieSlotButton()
    {
        /*
        var butt = document.getElementById("dCookieSlotButtonId");
        butt.style.display = "block";*/
        
        jQ('#dCookieSlotButtonId').show('slow');
    }
    
    function showSlot(label, i)
    {
        var dDiv = document.getElementById("dSlot"+i);
        if (dDiv.style.display == "none")
        {
            //dDiv.style.display = "block";
            jQ("#dSlot"+i).show('slow');
            
            var a = document.getElementById("dSlotA"+i);
            if (a.childNodes.length == 0)
            {
                var textNode = document.createTextNode(label);
                a.appendChild(textNode);
            }
        }
        else
            jQ("#dSlot"+i).hide('slow');
    }
    
    function getSlotData(slot)
    {
       var destFrom = readCookie('destFrom'+slot);
       var destTo = readCookie('destTo'+slot);
       var passengersCount = readCookie('passengersCount'+slot);
       
       var fromStr;
       var toStr;
       
       for (i=0; i<destinationsData.length; i++)
       {
            if (destinationsData[i][0] == destFrom)
            {
                fromStr = destinationsData[i][1];
                break;
            }
       }
       
       for (i=0; i<destinationsData.length; i++)
       {
            if (destinationsData[i][0] == destTo)
            {
                toStr = destinationsData[i][1];
                break;
            }
       }
       
       //var personStr = (passengersCount == 1)?"osoba":"osoby";
       
       if (passengersCount == 1)
            personStr = "osoba";
       else if (passengersCount < 5)
            personStr = "osoby";
       else
            personStr = "osob";
            
       return fromStr+" - "+toStr+", "+passengersCount+" "+personStr;
    }
    
    var activedPlus = false;
    
    function changeImageBackground()
    {
        var backgroundPlus = "url('/img/c4s_hp_plus_modre.gif') no-repeat left 3px";
        var backgroundMinus = "url('/img/c4s_hp_minus_modre.gif') no-repeat left 3px";
        
        if (activedPlus){
            document.getElementById("dCookieSlotButtonAId").style.background = backgroundPlus;
            activedPlus = false;
        }
        else{
            document.getElementById("dCookieSlotButtonAId").style.background = backgroundMinus;
            activedPlus = true;
        }
    }
    function zapisDestinaci(destinace){ 
       //alert(destinace);
       setDestinationTo('from1Combo','to1Combo','destToId_'+destinace,destinationsData);
    } 


    var carousel;
    jQ(document).ready(function(){
      /*jQ('#carousel a').click(function (event) {
        event.preventDefault();
        event.stopPropagation();
    	  var id = jQ(this).attr('id');
        jQ('#' + carouselTargetFieldID).val(id);
      });*/
      carousel = new Carousel();
      jQ('#scrollright').click(function (event) {
        event.preventDefault();
        event.stopPropagation();
    	  carousel.scrollRight();
      });
      jQ('#scrollleft').click(function (event) {
        event.preventDefault();
        event.stopPropagation();
    	  carousel.scrollLeft();
      });
    });
    
    function Carousel() {
      this.x = 0;
      this.index = 0;
      this.length = jQ('#destinations td').size();
      this.domobj = jQ('#destinations');
    }
    Carousel.ITEM_WIDTH = 140;
    
    Carousel.prototype = {
      scrollLeft: function() {
        if(this.index > 0) {
          if(this.index - 4 < 0) this.index = 0;
          else this.index -= 4;
          this.scroll();
        }
      },
      scrollRight: function() {
        if(this.index < this.length - 4) {
          if(this.index + 4 > this.length - 4) this.index = this.length - 4;
          else this.index += 4;
          this.scroll();
        }
      },
      scroll: function() {
        var newpos = -(this.index * Carousel.ITEM_WIDTH);
        this.domobj.animate({
          left: newpos + 'px'
        }, {
          duration: 'slow',
          queue: true
        });
      }
    }
/****************************************************/
/*********** C4S marketing end **********************/
/****************************************************/
// *********************************** //
// Funkce pro prediktivni vyhledavani  //
// *********************************** //

    var gDestinationListIATA = Array();
    var gDestinationListCities = Array();
    var gDestinationListCitiesIndex = Array();
    var gDestinationListCountries = Array();
    var gDestinationListCountriesIndex = Array();
    var gDestinationListAirports = Array();
    var gDestinationListAirportsIndex = Array();
    
    var nMinOptionLinesCount;
    var nMaxOptionLinesCount;
    var nMaxAvailOptionsCount;
    var aActiveFields = new Array();
  
// Najde vnoreny element - vrati ID elementu.
  function searchChildNodeByTagName(p_parentObjectID, p_tagName) {

    var oTargetObject = null;
    var oTargetSelect = null;
    var sNodeName = "";
    var sNodeID = null;

    try {
      oTargetObject = document.getElementById(p_parentObjectID);
    } catch(eException) {
    }

    for(nCounter = 0; nCounter < oTargetObject.childNodes.length; nCounter++) {
      sNodeName = oTargetObject.childNodes[nCounter].nodeName;
      sNodeName = sNodeName.toUpperCase();
      if(sNodeName = p_tagName.toUpperCase()) {
        try {
          sNodeID = oTargetObject.childNodes[nCounter].id;
        } catch(eException) {
        }
      }
    }
    return sNodeID;
  }
  
  // Zobrazi nalezene shody (zadava se pouze id DIVu s napovedou a reference na INPUT).
  function showAvailableDestinationsFast(p_targetObjectID, p_referenceObject, p_eEvent) {
  
    var sNodeID = null;

    sNodeID = searchChildNodeByTagName(p_targetObjectID, "SELECT");
 
    // Zavola metodu s doplnenym selectem.
    showAvailableDestinations(p_targetObjectID, sNodeID, p_referenceObject, p_eEvent);
  }
  
  // Zobrazi nalezene shody.
  function showAvailableDestinations(p_targetObjectID, p_targetSelectID, p_referenceObject, p_eEvent) {

    var sWrittenString = null;
    var oTargetObject = null;
    var aResultList = null;
    var bIataCodeFound = false;
    var nKeyCode = null;
    
    try {
      sWrittenString = p_referenceObject.value;
      oTargetObject = document.getElementById(p_targetObjectID);
      oTargetSelect = document.getElementById(p_targetSelectID);
    } catch(eException) {

    }

    try {
      nKeyCode = p_eEvent.keyCode;
    } catch(eException) {
      try {
        nKeyCode = p_eEvent.which;
      } catch(eException2) {
      }
    }
    
    // Po stisku sipky dolu presune vyber na moznosti.
    if(nKeyCode == 40) {
      setSelectBoxActive(p_targetSelectID);
    }
	
	// Po stisku klavesy Enter nastavi hodnotu z nabidky.
    if(nKeyCode == 13) {
      try {
        if(document.getElementById(p_targetSelectID).options.length > 0) {
          p_referenceObject.value = document.getElementById(p_targetSelectID).options[document.getElementById(p_targetSelectID).selectedIndex].innerHTML;
        }
      } catch(eException) {
      }
      hideAvailableDestinationsById(p_targetObjectID, true);
      return false;
    }

    resetDestinationFields(oTargetSelect);
    
    // Pokud je znaku mene jak 3, skryje napovedu.
    if(sWrittenString.length < 3) {
      hideAvailableDestinations(oTargetObject);
    }
    
    // Pokud je znaku 3 a vice, zobrazi napovedu.
    if(sWrittenString.length >= 3) {
      // Pro 3 znaky hleda podle IATA kodu.
      aResultList = searchInIataCodes(sWrittenString);

      // Hleda podle nazvu letiste.
      if(aResultList.length == 0) {
        aResultList = searchInAirportNames(sWrittenString);
      } else {
        aResultList = mergeArrays(aResultList, searchInAirportNames(sWrittenString));
      }

      // Hleda podle nazvu mesta.
      if(aResultList.length == 0) {
        aResultList = searchInCityNames(sWrittenString);
      } else {
        aResultList = mergeArrays(aResultList, searchInCityNames(sWrittenString));
      }

      // Hleda podle nazvu zeme.
      if(aResultList.length == 0) {
        aResultList = searchInCountryNames(sWrittenString);
      } else {
        aResultList = mergeArrays(aResultList, searchInCountryNames(sWrittenString));
      }

      // Zobrazi vysledky - pokud je nalezena alespon 1 moznost. 
      if(aResultList.length > 0) {
        for(nCounter = 0; nCounter < aResultList.length; nCounter++) {
          addDestinationElementRecord(oTargetObject, oTargetSelect, gDestinationListCities[aResultList[nCounter]], gDestinationListIATA[aResultList[nCounter]], gDestinationListAirports[aResultList[nCounter]], gDestinationListCountries[aResultList[nCounter]]);
        }
        if(aResultList.length <= nMinOptionLinesCount) {
          oTargetSelect.size = nMinOptionLinesCount;
        }
      }
    }
  }
  
  // Prida radek do vysledku.
  function addDestinationElementRecord(p_targetObject, p_targetSelect, p_City, p_IataCode, p_Airport, p_Country) {
  
    var nLastOptionIndex = p_targetSelect.options.length;
    
    if(nLastOptionIndex < 0) {
      nLastOptionIndex = 1;
    }
    
    if(nLastOptionIndex >= 0) {
      p_targetObject.style.visibility = "visible";
      p_targetObject.style.display = "block";
    }
    if((p_IataCode != "undefined") && (p_IataCode != null)){
      p_targetSelect.size = nLastOptionIndex + 1;
      p_targetSelect.options[nLastOptionIndex] = new Option(p_City + ', ' + p_Airport + ', ' + p_Country + ' (' + p_IataCode + ')', p_IataCode);
    }
    
    // Pokud je pocet radku vetsi, nez povoleny limit, vytvori scrollbar.
    if(p_targetSelect.size > nMaxOptionLinesCount) {
      p_targetSelect.size = nMaxOptionLinesCount;
    }
  }
  
  // Odstrani zaznamy z nabidky.
  function resetDestinationFields(p_targetSelect) {
  
    p_targetSelect.options.length = 0;
  }

  // Prohleda IATA kody.
  function searchInIataCodes(p_searchString) {
  
    var aIataCodesList = new Array();
    var nIndex = 0;
    
    for(nCounter = 0; nCounter < gDestinationListIATA.length; nCounter++) {
      if(gDestinationListIATA[nCounter] == p_searchString.toUpperCase()) {
        aIataCodesList[nIndex] = nCounter;
        nIndex += 1;
      }
    }

    return aIataCodesList;
  }
  
  // Prohleda nazvy letist.
  function searchInAirportNames(p_searchString) {
  
    var aAirportNamesList = new Array();
    var nIndex = 0;
    var nWordLength = p_searchString.length;
    var sMatchWord = null;
    var sMatchWord2 = null;
    
    for(nCounter = 0; nCounter < gDestinationListAirports.length; nCounter++) {
      sMatchWord = gDestinationListAirports[nCounter].substring(0, nWordLength);
      sMatchWord = sMatchWord.toUpperCase();
      sMatchWord2 = gDestinationListAirportsIndex[nCounter].substring(0, nWordLength);
      sMatchWord2 = sMatchWord2.toUpperCase()
      if((sMatchWord == p_searchString.toUpperCase()) || (sMatchWord2 == p_searchString.toUpperCase())) {
        aAirportNamesList[nIndex] = nCounter;
        nIndex += 1;
      }
    }

    return aAirportNamesList;
  }
  
  // Prohleda nazvy mest, vcetne diakritiky.
  function searchInCityNames(p_searchString) {
  
    var aCityNamesList = new Array();
    var nIndex = 0;
    var nWordLength = p_searchString.length;
    var sMatchWord = null;
    var sMatchWord2 = null;
    
    for(nCounter = 0; nCounter < gDestinationListCitiesIndex.length; nCounter++) {
      sMatchWord = gDestinationListCities[nCounter].substring(0, nWordLength);
      sMatchWord = sMatchWord.toUpperCase();
      sMatchWord2 = gDestinationListCitiesIndex[nCounter].substring(0, nWordLength);
      sMatchWord2 = sMatchWord2.toUpperCase()
      if((sMatchWord == p_searchString.toUpperCase()) || (sMatchWord2 == p_searchString.toUpperCase())) {
        aCityNamesList[nIndex] = nCounter;
        nIndex += 1;
      }
    }

    return aCityNamesList;
  }
  
  // Prohleda nazvy zemi, vcetne diakritiky.
  function searchInCountryNames(p_searchString) {
  
    var aCountryNamesList = new Array();
    var nIndex = 0;
    var nWordLength = p_searchString.length;
    var sMatchWord = "";
    
    for(nCounter = 0; nCounter < gDestinationListCountries.length; nCounter++) {
      sMatchWord = gDestinationListCountries[nCounter].substring(0, nWordLength);
      sMatchWord = sMatchWord.toUpperCase();
      sMatchWord2 = gDestinationListCountriesIndex[nCounter].substring(0, nWordLength);
      sMatchWord2 = sMatchWord2.toUpperCase()
      if((sMatchWord == p_searchString.toUpperCase()) || (sMatchWord2 == p_searchString.toUpperCase())) {
        aCountryNamesList[nIndex] = nCounter;
        nIndex += 1;
      }
    }

    return aCountryNamesList;
  }
  
  // Skryje napovedu (dle ID objektu).
  function hideAvailableDestinationsById(p_targetObjectID, p_forceHide) {
  
    // ID selectboxu vnoreneho do vrstvy, ktera se skryva. 
    var sChildSelectBoxID = null;
    var bHidingEnabled = true;
    
    // Vrstva, ktera se skryva.
    p_targetObject = document.getElementById(p_targetObjectID);
    // Ziska ID selectu vnoreneho do vrstvy.
    sChildSelectBoxID = searchChildNodeByTagName(p_targetObjectID, "SELECT");
    try {
      // Pokud ma select nastaven priznak "focused", nastavi priznak "aktivni". 
      if(aActiveFields[sChildSelectBoxID] == true) {
        bHidingEnabled = false;
      }
    } catch(eException) {
    }
    // Pokud neni selectbox oznacen jako aktivni, skryje vrstvu s danou prediktivni napovedou.
    if(bHidingEnabled || p_forceHide) {
      try {
      	setTimeout("hideAvailableDestinations(p_targetObject);", 100);
        //hideAvailableDestinations(p_targetObject);
        aActiveFields[sChildSelectBoxID] = null;
      } catch(eException) {
      }
    }
  }
  
  // Skryje napovedu.
  function hideAvailableDestinations(p_targetObject) {
  
    p_targetObject.style.visibility = "hidden";
    p_targetObject.style.display = "none";
  }
  
  // Slouci do pole neduplicitni polozky.
  function mergeArrays(p_array1, p_array2) {
  
    var aResultArray = new Array();
    var bItemAlreadyPresent = false;
    
    aResultArray = p_array1;
    // Pokud druhe pole nic neobsahuje, vraci rovnou pouze prvni pole. 
    if(p_array2 == null) {
      return aResultArray;
    }
    
    for(nCounter = 0; nCounter < p_array2.length; nCounter++) {
      bItemAlreadyPresent = false;
      for(nCounter2 = 0; nCounter2 < aResultArray.length; nCounter2++) {
        if(p_array2[nCounter] == aResultArray[nCounter2]) {
          bItemAlreadyPresent = true;
        }
      }
      if(bItemAlreadyPresent == false) {
        aResultArray[aResultArray.length] = p_array2[nCounter];
      }
    }
    return aResultArray;
  }
  
  // Nastavi hodnotu do pole.
  function setLocationValue(p_targetObjectID, p_targetListID, p_sourceObject, eEvent) {
  
    var oTargetObject = null;
    var sEventType = null;
    var nKeyCode = null;
    
    // Zjisti typ udalosti.
    try {
      sEventType = eEvent.type;
    } catch(eException) {
    }
    
    // Zjisti stisknutou klavesu (detekce Enter).
    if(sEventType == "keypress") {
      try {
        nKeyCode = eEvent.keyCode;
      } catch(eException) {
        try {
          nKeyCode = eEvent.which;
        } catch(eException2) {
        }
      }
    }
    
    // Pokud je vybrana nabidka kliknutim, nebo klavesou Enter, nastavi se vybrana hodnota.
    if((sEventType == "click") || (nKeyCode == 13)) {
      try {
        oTargetObject = document.getElementById(p_targetObjectID);
        oTargetObject.value = p_sourceObject.options[p_sourceObject.options.selectedIndex].innerHTML;
        hideAvailableDestinations(document.getElementById(p_targetListID));
        oTargetObject.focus();
      } catch(eException) {
      }
    }
    return false;
  }
  
  // Nastavi pole s vyberem jako aktivni.
  function setSelectBoxActive(p_targetListID) {
  
    document.getElementById(p_targetListID).focus();

    // Nastavi do globalni promenne ID pole, ktere je aktivni. 
    aActiveFields[p_targetListID] = true;
  }