//this function passes the value from year box to a hidden field and invokes onchange on it
function yearEvent(event) {
	if (checkIfNumberPressed(event)) { 
		$('searchForm:dateYearHidden').value = $('searchForm:dateYear').value; 
		$('searchForm:dateYearHidden').onchange(event); 
	}
}
//checks whether a number or backspace or delete was pressed in the year text field
function checkIfNumberPressed(event) {
	//alert(event.keyCode);
	if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) || event.keyCode == 8 || event.keyCode == 46 ) {
		return true;
	}
return false;
}
//function clears the searchform on search page
function clearSearchForm() {
	if (readCookie('clear') == 'yes') {
		clearMultiChoice($('searchForm:country'), -1);
		clearMultiChoice($('searchForm:dateListbox'), -1);
		clearMultiChoice($('searchForm:periodListbox'), -1);
		clearMultiChoice($('searchForm:adultListbox'), 2);
		clearMultiChoice($('searchForm:kidsListbox'), -1);
		clearMultiChoice($('searchForm:babyListbox'), -1);
		clearMultiChoice($('searchForm:acccoTypeListbox'), -1);
		clearMultiChoice($('searchForm:boardTypeListbox'), -1);
		clearMultiChoice($('searchForm:transportTypeListbox'), -1);
		clearMultiChoice($('searchForm:maxPriceTypeListbox'), -1);
		if ($('searchForm:weekDayText') != null) {
			$('searchForm:weekDayText').innerHTML = "";
		}
	} 
}
//function gets a cookie value
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
//function clears any selection on an input element with options nested
//and selects the one with number noToSelect
//if noToSelect == -1 no selection made
function clearMultiChoice(multi, noToSelect) {
	if (multi == null) {
		return false;
	}
	//alert(multi.tagName);
	multi.selectedIndex = 0;
	var children = multi.childNodes;
	var j = -1;
	for (i = 0; i < children.length; i++) {
		if (children[i].tagName == 'OPTION' || children[i].tagName == 'option') {
			j++;
			var child = children[i];
			child.selected = false;
			var oldSelAttr = child.getAttributeNode('selected');
			if (oldSelAttr != null) {	
				child.removeAttributeNode(oldSelAttr);
			}
			if (j == noToSelect) {
				child.selected = true;
				var selAttr = document.createAttribute('selected');
				selAttr.nodeValue = 'selected';
				child.setAttributeNode(selAttr);
			}
		}
	}
}
var calendarDayCommitFormula = null;
var calendarMonthCommitFormula = null;
var calendarYearCommitFormula = null;
function initSearch() {
	var form = document.getElementById('searchForm');
	var newAttr = document.createAttribute('autocomplete');
    newAttr.nodeValue = "off";
   	form.setAttributeNode(newAttr); 
   	/*var dayText = document.getElementById('searchForm:dateDay');
   	var monthText = document.getElementById('searchForm:dateMonth');
   	var yearText = document.getElementById('searchForm:dateYear');
	if (dayText != null) {
	   	calendarDayCommitFormula = dayText.onchange;
	   	calendarMonthCommitFormula = monthText.onchange;
	   	calendarYearCommitFormula = yearText.onchange;
		var newAttr = document.createAttribute("onchange");
	    newAttr.nodeValue = 'commitCalendarFunction(event);';
	    dayText.removeAttribute("onchange");
	    dayText.setAttributeNode(newAttr); 
	   	newAttr = document.createAttribute("onchange");
	    newAttr.nodeValue = 'commitCalendarFunction(event);';
	  	monthText.removeAttribute("onchange");
	    monthText.setAttributeNode(newAttr); 
	    newAttr = document.createAttribute("onchange");
	    newAttr.nodeValue = 'commitCalendarFunction(event);';
	    yearText.removeAttribute("onchange");
	    yearText.setAttributeNode(newAttr);
	} */
}
function commitCalendarFunction(event) {
	var dayText = document.getElementById('searchForm:dateDay');
   	var monthText = document.getElementById('searchForm:dateMonth');
   	var yearText = document.getElementById('searchForm:dateYear');
	if (dayText.value == '' || monthText.value == '' || yearText.value == '') {
		$('searchForm:weekDayText').innerHTML = "";
	}
	if (dayText.value.length == 1) {
		dayText.value = '0' + dayText.value;
	}
	if (monthText.value.length == 1) {
		monthText.value = '0' + monthText.value;
	}
	if (yearText.value.length == 1) {
		yearText.value = '0' + yearText.value;
	}
	//if (dayText.value != '' && monthText.value != '' && yearText.value != '') {
		calendarDayCommitFormula(event);
		calendarMonthCommitFormula(event);
		calendarYearCommitFormula(event);
	//}
}
function changeFormRow (rowID) {
  if ($('curRowId').value == rowID) {
    $('curRowId').value = '';
    $(rowID+'_header').className = '';
    $(rowID+'_result').style.display = '';
    new Effect.toggle(rowID+'_container','blind', { duration: 0.3 })
  } else if ($('curRowId').value != "") {
    curID = $('curRowId').value;
    $(rowID+'_header').className = 'selected';
    $(curID+'_header').className = '';
    $(rowID+'_result').style.display = 'none';
    $(curID+'_result').style.display = '';
    $('curRowId').value = rowID;
    new Effect.toggle(rowID+'_container','blind', { duration: 0.3, afterFinish: function( ) { new Effect.toggle(curID+'_container','blind', { duration: 0.3 }) } })
  } else {
    $('curRowId').value = rowID;
    $(rowID+'_header').className = 'selected';
    $(rowID+'_result').style.display = 'none';
    new Effect.toggle(rowID+'_container','blind', { duration: 0.3 })
  }
}
function quickFind(inputBox, tagId){
	var inpValue = inputBox.value.toLowerCase();
    var selectBox = null;
   	var selectList = document.getElementsByTagName('select');
   	for (i = 0; i < selectList.length; i++) {
   		if (selectList[i].id.indexOf(tagId) >= 0) {
   			selectBox = selectList[i];
   		}
   	}
    if(inpValue==""){
    	scrollSelectTo(selectBox,0);
    	return true;
    }
	var options = selectBox.options;
	for(var i=0;i<options.length;i++){
        var optText = options[i].text.toLowerCase();
        if(inpValue!="" && optText.indexOf(inpValue)==0){
        	scrollSelectTo(selectBox,i);
        	break;
        }
	}
   	
}
function scrollSelectTo(s,n){
	try{
		if(s.options.length==0) return false;
		var selected = s.options[s.options.length-1].selected;
		s.options[s.options.length-1].selected=true;
		s.options[s.options.length-1].selected=false;
		s.options[s.options.length-1].selected=selected;
		var selected = s.options[n].selected;
		s.options[n].selected=true;
		s.options[n].selected=false;
		s.options[n].selected=selected;
           if(!s.multiple) s.selectedIndex=-1;
	}catch(err){}
}
function clearSelection(inputId) {
	var input =  document.getElementById(inputId);
	if (input != null) {
		for (i = 0; i < input.options.length; i++) {
			input.options[i].selected = false;
		}
	}
}

function submitDirectSearch(event){
	var input = document.getElementById('searchForm2:keyWordSearchInput');
	var submitCommandLink = document.getElementById('searchForm2:keyWordSearchSubmitButton');

	if (event == null)
		event = window.event;
	if (input != null && submitCommandLink != null) {
		checkIfDirectBtnIsToBeEnabled(input);
		if (event.keyCode == 13 && input.value.length > 2) {
			submitCommandLink.onclick();
		}
	}
}
//Function to disable or enable the search by key word button, based on the length of text in the input field.
function checkIfDirectBtnIsToBeEnabled(input) {
	if (input.value.length > 2) {
		enableDirectSearchBtn();
	} else {
		disableDirectSearchBtn();
	}	
}
//Function to replace the direct search button with a disabled version.
function enableDirectSearchBtn() {
	var submitCommandLink = document.getElementById('searchForm2:keyWordSearchSubmitButton');
	var submitCommandLinkDis = document.getElementById('searchForm2:keyWordSearchSubmitButtonDis');
	submitCommandLink.style.display = '';
	submitCommandLinkDis.style.display = 'none';
}
//Function to replace the disabled direct search button with a enabled version.
function disableDirectSearchBtn() {
	var submitCommandLink = document.getElementById('searchForm2:keyWordSearchSubmitButton');
	var submitCommandLinkDis = document.getElementById('searchForm2:keyWordSearchSubmitButtonDis');
	submitCommandLink.style.display = 'none';
	submitCommandLinkDis.style.display = '';
}
function modeChangeUpdateCriteria(){
	countriesList = document.getElementById('searchForm:country');
	if(countriesList!=null && countriesList.selectedIndex!=-1){
		modeChangeSubmitOtherSelection();
	}else{
		modeChangeSubmitOtherSelectionForward();
	}
}
function changeDirectSearchInfo() {
	var allInputs = document.getElementsByTagName('input');
	var radios = new Array();
	var j = 0;
	for(var index=0; index<allInputs.length; index++) {
		if (allInputs[index].type == 'radio') {
			radios[j++] = allInputs[index];
		}
	} 
	if (radios[0].checked) {
		$('directSearchInfo1').style.display = 'none';
		$('directSearchInfo2').style.display = 'block';
	} 
	if (radios[1].checked) {
		$('directSearchInfo1').style.display = 'block';
		$('directSearchInfo2').style.display = 'none';
	}
}


//
// Function handles the ABC button style change. 
//
// parameters:
// - selected, is the current ABC selection, A, B or C.
// - path, context path
// - postFix, postfix for the element, possible values: 
//		empty string or null for none. 
//		SP for SearchPage 
//		AP for accommodation page
//
function changeABC3args(selected, path, postElem){
	var buttonA = document.getElementById('middleHeader:panelGridA_' + postElem);
	var buttonB = document.getElementById('middleHeader:panelGridB_' + postElem);
	var buttonC = document.getElementById('middleHeader:panelGridC_' + postElem);
	
	if(selected != null && buttonA != null && buttonB != null && buttonC != null && path != null){
		if(selected == 'A'){
			buttonA.style.backgroundImage = "url(" + path + "/abc-button-active-a.png)";
			buttonB.style.backgroundImage = "url(" + path + "/abc-button-inactive-b.png)";
			buttonC.style.backgroundImage = "url(" + path + "/abc-button-inactive-c.png)";
		}else if(selected == 'B'){
			buttonA.style.backgroundImage = "url(" + path + "/abc-button-active-a.png)";
			buttonB.style.backgroundImage = "url(" + path + "/abc-button-active-b.png)";
			buttonC.style.backgroundImage = "url(" + path + "/abc-button-inactive-c.png)";
		}else if(selected == 'C'){
			buttonA.style.backgroundImage = "url(" + path + "/abc-button-active-a.png)";
			buttonB.style.backgroundImage = "url(" + path + "/abc-button-active-b.png)";
			buttonC.style.backgroundImage = "url(" + path + "/abc-button-active-c.png)";
		}	
	}	
}


// Function handles the ABC button style change.
//
// parameters:
// - selected, is the current ABC selection, A, B or C.
// - path, context path
function changeABC(event, selected, path){
	
	var buttonA = document.getElementById('middleHeader:panelGridA');
	var buttonB = document.getElementById('middleHeader:panelGridB');
	var buttonC = document.getElementById('middleHeader:panelGridC');
	
	if(selected != null && buttonA != null && buttonB != null && buttonC != null && path != null){
		if(selected == 'A'){
			buttonA.style.backgroundImage = "url(" + path + "/abc-button-active-a.png)";
			buttonB.style.backgroundImage = "url(" + path + "/abc-button-inactive-b.png)";
			buttonC.style.backgroundImage = "url(" + path + "/abc-button-inactive-c.png)";
		}else if(selected == 'B'){
			buttonA.style.backgroundImage = "url(" + path + "/abc-button-active-a.png)";
			buttonB.style.backgroundImage = "url(" + path + "/abc-button-active-b.png)";
			buttonC.style.backgroundImage = "url(" + path + "/abc-button-inactive-c.png)";
		}else if(selected == 'C'){
			buttonA.style.backgroundImage = "url(" + path + "/abc-button-active-a.png)";
			buttonB.style.backgroundImage = "url(" + path + "/abc-button-active-b.png)";
			buttonC.style.backgroundImage = "url(" + path + "/abc-button-active-c.png)";
		}	
	}	
}

function countryChangeUpdateCriteria(){
	districtsList = document.getElementById('searchForm:region');
	if(districtsList!=null && districtsList.selectedIndex!=-1){
		countryChangeSubmitOtherSelection();
	}else{
		countryChangeSubmitOtherSelectionForward();
	}
			
}

function districtChangeUpdateCriteria(){
	placesList = document.getElementById('searchForm:city');
			
	if(placesList!=null && placesList.selectedIndex!=-1){
		districtChangeSubmitOtherSelection();
	}else{
		districtChangeSubmitOtherSelectionForward();
	}
		
}


function placeChangeUpdateCriteria(){
	accosList = document.getElementById('searchForm:acco');
	
	if(accosList!=null && accosList.selectedIndex!=-1){
		placeChangeSubmitOtherSelection();
	}else{
		placeChangeSubmitOtherSelectionForward();
	}
}



