var stringDep = 'Departure date';
var stringArr = 'Return date';
var prefPositions = new Array();
var prefNames = new Array();
var booking_charters = new Array();

booking_charters[booking_charters.length] = new Array("passINF_charters", "checkCountInfants(value)", "Number of infants cannot be higher then number of adults!");
booking_charters[booking_charters.length] = new Array("from1_charters", "value > ''", "Please enter a valid From!");
booking_charters[booking_charters.length] = new Array("to1_charters", "value > ''", "Please enter a valid To!");
booking_charters[booking_charters.length] = new Array("DAY_0", "value > 0", "Please enter a valid Departure date!");
//booking_charters[booking_charters.length] = new Array("MONTH_SEL_0", "value > 0", "Please enter a valid Departure date!");
booking_charters[booking_charters.length] = new Array("DAY_1", "checkArrDate('charters')", "Please enter a valid Return date!");
//booking_charters[booking_charters.length] = new Array("MONTH_SEL_1", "checkArrDate('charters')", "Please enter a valid Return date!");
//booking_charters[booking_charters.length] = new Array("arrMonth", "checkDates('booking', 'charters')", "Return date cannot be before Departure date!");
//booking_charters[booking_charters.length] = new Array("from1Combo", "checkFrom1Combo()", "Please enter a valid From!");
booking_charters[booking_charters.length] = new Array("ID_LOCATION", "index != 0", "Please, select some Sale country!");
booking_charters[booking_charters.length] = new Array("JOURNEY_TYPE", "checkPreference(value, 'charters')", "Preference of the lowest price is applicable to roundtrip only!");

function checkCountInfants(value) {
    var countAdults = jQ("#passADT_charters").val();
    if(countAdults >= value) return true;
    else return false;
}

function switch_Return(identName) {
    var i;
    var obj = document.forms['booking_'+identName].JOURNEY_TYPE;
    if (obj) {
        for (i=0;i<obj.length;i++) {
            if (obj[i].checked) {
                if (obj[i].value == 'OJ') {
                    switch_display('hidden_'+identName+'_', 'on');
                    //removeOption('pref', 'SCP');
        			//setPreferenceCombo('AVB', true);
        			setPreferenceRadio2AVB(identName);
        			switch_display('depPref_'+identName+'_', 'off');
                } else {
                    switch_display('hidden_'+identName+'_', 'off');
                    switch_display('depPref_'+identName+'_', 'on');
                    //addOption('pref', 'SCP');
                }
                if (obj[i].value == 'OW') {
                    switch_display('oneway_'+identName+'_', 'off');
                    //removeOption('pref', 'SCP');
        			//setPreferenceCombo('AVB', false);
        	    } else {
        	        switch_display('oneway_'+identName+'_', 'on');
        	    }
        	    if (obj[i].value == 'RT') {
        	        //addOption('pref', 'SCP');
        	    }
                i = obj.length;
            }
        }
        mn_init();
    }
}

function switch_visibility(prefix, value) {
    var obj, i;
    i = 0;
    while (obj = document.getElementById(prefix+i)) {
        if (!value) {
            if (obj.style.visibility == 'hidden') obj.style.visibility = 'visible';
            else obj.style.visibility = 'hidden';
        } else obj.style.visibility = value;
        i++;
    }
}
function switch_display(prefix, value) {
    var obj, i;
    i = 0;
    while (obj = document.getElementById(prefix+i)) {
        if (!value) {
            if (obj.style.display == 'none') obj.style.display = '';
            else obj.style.display = 'none';
        } else if (value == 'on') obj.style.display = '';
        else if (value == 'off') obj.style.display = 'none';
        i++;
    }
}

function hideComboFrom(identName) {
    var o = document.forms['booking_'+identName].from1Combo+'_'+identName;
    var i = document.forms['booking_'+identName].from1+'_'+identName;
    if (o && i && (o.style.display != 'none')) {
        o.style.display = 'none';
        i.style.display = '';
    }
}
function checkFrom1Combo(identName) {
    var o = document.forms['booking_'+identName].from1Combo+'_'+identName;
    var i = document.forms['booking_'+identName].from1+'_'+identName;
    if (o && i && (o.style.display != 'none') && (i.value == '')) return false;
    else return true;
}


function checkPreference(prefValue, identName) {
    var i;
    var obj = document.forms['booking_'+identName].journey;
    if (obj) {
        for (i=0;i<obj.length;i++) {
            if (obj[i].checked) {
                if ((obj[i].value != 'RT' && obj[i].value != 'OW') && (prefValue == 'SCP')) return false;
                i = obj.length;
            }
        }
    }
    return true;
}
function checkDates(frmname, identName) {
    var date1 = correctDates(frmname, identName, 1);
    var date2 = correctDates(frmname, identName, 2);
    if (frmname == 'booking_'+identName) {
        var obj = document.forms[frmname+'_'+identName].journey;
        if (obj) {
            for (i=0;i<obj.length;i++) {
                if (obj[i].checked) {
                    if (obj[i].value == 'OW') date2 = correctDates(frmname, identName, 1);
                    i = obj.length;
                }
            }
        }
    }
    if (date1 > date2) return false;
    return true;
}
function correctDates(frmname, identName, number, force) {
    //alert(clndrSetDates);
    var dobj, mobj, pref;
    if (frmname == 'booking_'+identName) {
        if (number == 1) pref = 'dep';
        else pref = 'arr';
        eval("dobj = document.forms[frmname+'_'+identName]."+pref+"Day;");
        eval("mobj = document.forms[frmname+'_'+identName]."+pref+"Month;");
    } else if (frmname == 'hotels') {
        eval("dobj = document.forms[frmname].checkinDay;");
        eval("mobj = document.forms[frmname].checkinMonth;");
    }
    if (!force & ((dobj.value == 'DD') || (mobj.value == 'MM'))) return;
    var today = new Date();
    var d = dobj.value * 1;
    if (!d) d = today.getDate();
    var m = mobj.value * 1;
    if (!m) m = today.getMonth()+1;
    var lastDay = new Date();
	lastDay.setTime(Date.UTC(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0) + (331*24*60*60*1000));
    var y = today.getFullYear();
    if (m < today.getMonth()+1) y++;
    var date = new Date(y, m-1, d, 0, 0, 0);
    if (date > lastDay) date = lastDay;
    if (date < today) date = today;
    if (dobj.value != date.getDate()) dobj.value = date.getDate();
    if (mobj.value != date.getMonth()+1) mobj.value = date.getMonth()+1;
    if (number == 1) clndrSetDates[encodeURIComponentFull(stringDep)] = date.getDate() + '.' + String(date.getMonth()+1) + '.' + date.getFullYear();
    else if (number == 2) clndrSetDates[encodeURIComponentFull(stringArr)] = date.getDate() + '.' + String(date.getMonth()+1) + '.' + date.getFullYear();
    return date;
}

function show_note(divID, action) {
    var x = document.getElementById(divID);
    var y = document.getElementById('specoff_combofrom');
    if (x) {
        if (action == 1) {
            x.style.display = 'block';
            if (y) y.style.visibility = 'hidden';
        } else {
            x.style.display = 'none';
            if (y) y.style.visibility = '';
        }
    }
}

function setCombo(id, val) {
    var	elm = document.getElementById(id);
	if(!elm || !elm.options) return false;
    
    for(var m = 0; m < elm.options.length; m++) {
        if(elm.options[m].value == val) {
            elm.selectedIndex = m;
            return m;
        }
    }
}

function addOption(id, addVal) {
    var m;
	var	elm = document.getElementById(id);
	if(!elm || !elm.options) return false;
    if(prefPositions[id] == null) return;
    for(m = 0; m < elm.options.length; m++) {
        if(elm.options[m] && elm.options[m].value == addVal) return false;
    }
    
    var actual = elm.options[elm.selectedIndex].value;
    for(m = elm.options.length - 1; m >= prefPositions[id]; m--) {
        elm.options[m+1] = new Option(elm.options[m].text, elm.options[m].value);
    }
    elm.options[prefPositions[id]] = new Option(prefNames[addVal], addVal);
    setCombo(id, actual);
}

function removeOption(id, removeVal) {
	var	elm = document.getElementById(id);
	if(!elm || !elm.options) return false;
    
    var actual = elm.options[elm.selectedIndex].value;
    for(var m = 0; m < elm.options.length; m++) {
        if(elm.options[m].value == removeVal) {
            prefNames[removeVal] = elm.options[m].text;
            elm.options[m] = null;
            prefPositions[id] = m;
            if(actual == removeVal) elm.selectedIndex = 0;
            else setCombo(id, actual);
            break;
        }
    }
}
function setPreferenceCombo(wanted, keepClass, unwanted) {
	var combo = document.getElementById('pref');
	if (!combo || !combo.options) return;
	var actual = combo.options[combo.selectedIndex].value;
	if (unwanted && actual != unwanted) return;
	if (!wanted) wanted = actual;

	if (keepClass) {
		if (actual.substring(actual.length - 2) == 'YC') wanted += 'YC';
		else if (actual.substring(actual.length - 1) == 'C') wanted += 'C';
	}
	var found = false;
	for(var m = 0; m < combo.options.length; m++) {
		if(combo.options[m].value.substr(0, wanted.length) == wanted) { //find first matching by start of wanted preference
			wanted = combo.options[m].value;
			found = true;
		}
	}
	//alert(wanted);	
	combo.value = (found) ? wanted : combo.options[0].value;
	//switchVisibility('depTimes', (wanted.substr(0, 3) == 'AVB') ? 1 : 0);
}
function setPreferenceRadio2AVB(identName) {
	var el = document.getElementById('prefAvbID_'+identName);
	if (!el) return;
	el.checked = true;
}
function checkArrDate(identName) {
    var i;
    var journey = '';
    var dobj = document.forms['booking_'+identName].DAY_0;
    var mobj = document.forms['booking_'+identName].MONTH_SEL_0;
    var obj = document.forms['booking_'+identName].JOURNEY_TYPE;
    if (obj) {
        for (i=0;i<obj.length;i++) {
            if (obj[i].checked) {
                journey = obj[i].value;
                i = obj.length;
            }
        }
    }
    if (journey == 'OW') return true;
    if ((dobj.value == '') || (mobj.value == '')) return false;
    return true;
}

function transcode(frmname, identName) {
    var k, co, reg, x, i;
    var elname = new Array();
    elname['booking_'+identName] = new Array('from1', 'from2', 'to1', 'to2');
    //elname['hotels'] = new Array('FreeSearch');
    var n = new Array('a', 'A', 'a', 'A', 'c', 'C', 'd', 'D', 'e', 'E', 'e', 'E', 'e', 'E', 'i', 'I', 'l', 'L', 'l', 'L', 'n', 'N', 'o', 'O', 'o', 'O', 'o', 'O', 'r', 'R', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'u', 'U', 'u', 'U', 'y', 'Y', 'z', 'Z');
    var o = new Array('u00E1', 'u00C1', 'u00E4', 'u00C4', 'u010D', 'u010C', 'u010F', 'u010E', 'u00E9', 'u00C9', 'u00EB', 'u00CB', 'u011B', 'u011A', 'u00ED', 'u00CD', 'u013A', 'u0139', 'u013E', 'u013D', 'u0148', 'u0147', 'u00F3', 'u00D3', 'u00F4', 'u00D4', 'u00F6', 'u00D6', 'u0155', 'u0154', 'u0159', 'u0158', 'u0161', 'u0160', 'u0165', 'u0164', 'u00FA', 'u00DA', 'u016F', 'u016E', 'u00FC', 'u00DC', 'u00FD', 'u00DD', 'u017E', 'u017D');
    for (i=0;i<elname[frmname].length;i++) {
        eval("x = document.forms['"+frmname+"']."+elname[frmname][i]+";");
        co = x.value;
        for (k=0;k<n.length;k++) {
            reg = new RegExp(' \\ '+o[k], 'gm');
            co = co.replace(reg, n[k]);
        }
        x.value = co;
    }
    return true;
}
/* NEW functions*/
function checkDay2(p_sourceDayID, p_sourceMonthYearID, p_targetDayID, p_targetMonthYearID) {
	var fromDay = null;
	var fromMonthYear = null;
	var toDay = null;
	var toMonthYear = null;
	//alert(p_sourceDayID+p_sourceMonthYearID+p_targetDayID+p_targetMonthYearID);
	
	fromDay = document.getElementById(p_sourceDayID);
	fromMonthYear = document.getElementById(p_sourceMonthYearID);
	toDay = document.getElementById(p_targetDayID);
	toMonthYear = document.getElementById(p_targetMonthYearID);
	try {
		if(fromMonthYear.selectedIndex > toMonthYear.selectedIndex) {
		  	if(fromDay.value < toDay.value) {
              toDay.value = fromDay.value;
            }
			toMonthYear.selectedIndex = fromMonthYear.selectedIndex;
		}
		
		if(fromDay.value > 31) {
		  fromDay.value = 31;
		}
		if(fromDay.value < 0) {
		  fromDay.value = 1;
		}
		if(toDay.value > 31) {
		  toDay.value = 31;
		}
		if(toDay.value < 0) {
		  toDay.value = 1;
		}
		
		if((fromDay.value*1  > toDay.value*1) && (fromMonthYear.selectedIndex == toMonthYear.selectedIndex)) {
		  toDay.value = fromDay.value;
		}
	} catch (eException) {
	}
	//createCalendarLink(0, 'depCalendar', null, fromMonthYear.options[fromMonthYear.selectedIndex].value, fromDay.value);
	//createCalendarLink(1, 'arrCalendar', null, toMonthYear.options[toMonthYear.selectedIndex].value, toDay.value);
}

/*Funkce pro obsluhu radio buttonu pri zvolene business class*/
function switchAvailOptions(sourceObject, identName) {
  if(sourceObject.checked) {
    try {
      document.getElementById('prefCabinHidden_'+identName).value = sourceObject.value;
      document.getElementById('prefAvbID_'+identName).checked = true;
      document.getElementById('prefScpID_'+identName).disabled = true;
    } catch(eException) {
    }
  } else {
    try {
      document.getElementById('prefCabinHidden_'+identName).value = 'YC';
      document.getElementById('prefScpID_'+identName).disabled = false;
    } catch(eException) {
    }
  }
}

// *********************************** //
// 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;
  }
function clearContent2(elname, frmname, type) {
    var x;
    eval("x = document.forms['"+frmname+"']."+elname+";");
    if (x) {
        if (x.value) {
            if (type == 'DD' && x.value == 'DD') x.value = '';
            else if (type == 'MM' && x.value == 'MM') x.value = '';
        }
    }
}  
/**** funkce pro new calendar_3 *****/
function openCalendar(id,inp1,inp2){
	var obj = arrayCalendars[id];
	var d1=document.getElementById(inp1);
	var d2=document.getElementById(inp2);
    var s = new Date();
    if(d1.value) s.setDate(d1.value);
    if(d2.value){
        d2Conv=d2.value.split("/");
        s.setMonth(d2Conv[0]-1);
        s.setFullYear(d2Conv[1]);
    }
    obj.value.setTime(s.getTime());
    obj.returnBack="doFromCalendar('"+id+"','"+inp1+"','"+inp2+"')";	
	obj.runCalendar();
}
function doFromCalendar(id,inp1,inp2){
	var calend = arrayCalendars[id];
	var d1 = document.getElementById(inp1);
	var d2 = document.getElementById(inp2);	    
    setDateToInputs(inp1,inp2,calend.value); // nastavi inputy 
	if(id=='clnd_hpTermFrom'){
	    var calendTo=arrayCalendars['clnd_hpTermTo'];
        calendTo.setMindate(calend.value);
        if(calend.value > calendTo.value || calendTo.value == null){
	        calendTo.value = calend.value;
	        setDateToInputs('dayToID'+inp1.substr(inp1.indexOf("_")),'monthYearToID'+inp2.substr(inp2.indexOf("_")),calendTo.value);
	    }	
	}
}
function setDateToInputs(inp1,inp2,dateVal){
    document.getElementById(inp1).value = dateVal.getDate();
    var d2=document.getElementById(inp2);
    var d2val=(dateVal.getMonth()+1)+"/"+dateVal.getFullYear();
    for(var n = 0; n < d2.options.length; n++)  d2.options[n].selected = false;
    for(n = 0; n < d2.options.length; n++){
	    if(d2.options[n].value == d2val){
	        d2.options[n].selected = true;
	        break;
	    }
	}
}
function checkDatesWithCalend(type,id_from,id_to,day_from,month_from,day_to,month_to){
    var calFrom = arrayCalendars[id_from];
    var calTo = arrayCalendars[id_to];
    var dayFrom = document.getElementById(day_from);
    var monthFrom = document.getElementById(month_from);
    var dayTo = document.getElementById(day_to);
    var monthTo = document.getElementById(month_to);
    var d1=monthFrom.value.split("/");
    var d2=monthTo.value.split("/");    
    var dFrom = new Date();    
        dFrom.setDate(dayFrom.value);
        dFrom.setMonth(d1[0]-1);
        dFrom.setFullYear(d1[1]);
    var dTo = new Date();
        dTo.setDate(dayTo.value);
        dTo.setMonth(d2[0]-1);
        dTo.setFullYear(d2[1]);        
    if(type=='from'){
        if(dFrom >= calFrom.mindate && dFrom <= calFrom.maxdate)    calFrom.value=dFrom;
        else if(dFrom > calFrom.maxdate) calFrom.value=calFrom.maxdate;
        else calFrom.value=calFrom.mindate;
        doFromCalendar(id_from,day_from,month_from);
    }else if(type=='to'){
        if(dTo >= calTo.mindate && dTo <= calTo.maxdate && dTo >= dFrom) calTo.value=dTo;
        else if(dTo > calTo.maxdate) calTo.value=calTo.maxdate;        
        else calTo.value=calTo.mindate;
        setDateToInputs(day_to,month_to,calTo.value);
    }        
}