﻿hiSearch = Class.create({

    initialize: function (hiPrefix) {
        this.hiPrefix = hiPrefix;
        this.selRoomsRequired = $(this.hiPrefix + "lstRoomsRequired");
        
        this.selRoom1Children = $(this.hiPrefix + "lstRm1Children");
        this.selRoom2Children = $(this.hiPrefix + "lstRm2Children");
        this.selRoom3Children = $(this.hiPrefix + "lstRm3Children");
        this.selRoom4Children = $(this.hiPrefix + "lstRm4Children");
        this.selRoom5Children = $(this.hiPrefix + "lstRm5Children");
        this.selRoom6Children = $(this.hiPrefix + "lstRm6Children");
        this.selRoom7Children = $(this.hiPrefix + "lstRm7Children");
        this.selRoom8Children = $(this.hiPrefix + "lstRm8Children");
        this.selRoom9Children = $(this.hiPrefix + "lstRm9Children");
            
        this.lnkShowExtraOptions = $(this.hiPrefix + "lnkShowExtraOptions");
        this.lnkHideExtraOptions = $(this.hiPrefix + "lnkHideExtraOptions");
        this.rowEstabName = $(this.hiPrefix + "rowEstabName");
        this.rowClass = $(this.hiPrefix + "rowClass");
        this.rowBoard = $(this.hiPrefix + "rowBoard");
        this.rowPromo = $(this.hiPrefix + "rowPromo");          
        this.hidShowExtraOptions = $(this.hiPrefix + "hidAcomShowExtraOptions");
        this.btnAcomSearch = $(this.hiPrefix + "btnAcomSearch");       
      
        this.selRoom1Children.observe('change', this.childChange);
        this.selRoom2Children.observe('change', this.childChange);
        this.selRoom3Children.observe('change', this.childChange);
        this.selRoom4Children.observe('change', this.childChange);
        this.selRoom5Children.observe('change', this.childChange);
        this.selRoom6Children.observe('change', this.childChange);
        this.selRoom7Children.observe('change', this.childChange);
        this.selRoom8Children.observe('change', this.childChange);
        this.selRoom9Children.observe('change', this.childChange);
	    
        this.lnkShowExtraOptions.observe('click', this.showExtraOptions.bindAsEventListener(this));
        this.lnkHideExtraOptions.observe('click', this.hideExtraOptions.bindAsEventListener(this));

        this.inpEstabId = $(this.hiPrefix + "txtEstabId"); 
        this.inpEstabTitle = $(this.hiPrefix + "txtEstabTitle");   
        this.inpDestination = $(this.hiPrefix + "autocompletetextbox");   

        this.inpEstabTitle.observe('change', this.blankEstabId.bindAsEventListener(this));
        this.inpDestination.observe('change', this.blankEstabId.bindAsEventListener(this));
    },
    
    blankEstabId: function(e){
        // Blank estab id if we change the estab name or destination
        this.inpEstabId.value = "";
    },

    childChange: function(){
        acomShowChildAges();   
    },

    showExtraOptions: function(e){
        Event.stop(e);
        this.lnkShowExtraOptions.hide();
        this.lnkHideExtraOptions.show();
        
        this.rowEstabName.show();
        this.rowClass.show();
        this.rowBoard.show();
        this.rowPromo.show();
                  
        this.hidShowExtraOptions.value = 'True';
    },
    
    hideExtraOptions: function(e){
        Event.stop(e);
        this.lnkShowExtraOptions.show();
        this.lnkHideExtraOptions.hide();
        
        this.rowEstabName.hide();
        this.rowClass.hide();
        this.rowBoard.hide();
        this.rowPromo.hide();
                  
        this.hidShowExtraOptions.value = 'False';
    }   
})

function preInitMiniSearch(){
  if($("ACM_searchInitialised").value=="0"){
    $("ACM_searchInitialised").value="1";
    return;
   }
   initAcomSearch()
}

function initAcomSearch(){
    var empty;
  
    hiShowRooms();
}

function hiShowRooms(){
   roomCount = $(hiPrefix + "lstRoomsRequired").value;

   if(roomCount==1){
        $(hiPrefix + "rm2Row").hide();
        $(hiPrefix + "rm3Row").hide();
        $(hiPrefix + "rm4Row").hide();
        $(hiPrefix + "rm5Row").hide();
        $(hiPrefix + "rm6Row").hide();
        $(hiPrefix + "rm7Row").hide();
        $(hiPrefix + "rm8Row").hide();
        $(hiPrefix + "rm9Row").hide();
    }
    else if(roomCount==2){
    	$(hiPrefix + "rm2Row").show();
        $(hiPrefix + "rm3Row").hide();
        $(hiPrefix + "rm4Row").hide();
        $(hiPrefix + "rm5Row").hide();
        $(hiPrefix + "rm6Row").hide();
        $(hiPrefix + "rm7Row").hide();
        $(hiPrefix + "rm8Row").hide();
        $(hiPrefix + "rm9Row").hide();
    }
    else if(roomCount==3){
    	$(hiPrefix + "rm2Row").show();
        $(hiPrefix + "rm3Row").show();
        $(hiPrefix + "rm4Row").hide();
        $(hiPrefix + "rm5Row").hide();
        $(hiPrefix + "rm6Row").hide();
        $(hiPrefix + "rm7Row").hide();
        $(hiPrefix + "rm8Row").hide();
        $(hiPrefix + "rm9Row").hide();
    }
    else if(roomCount==4){
    	$(hiPrefix + "rm2Row").show();
        $(hiPrefix + "rm3Row").show();
        $(hiPrefix + "rm4Row").show();
        $(hiPrefix + "rm5Row").hide();
        $(hiPrefix + "rm6Row").hide();
        $(hiPrefix + "rm7Row").hide();
        $(hiPrefix + "rm8Row").hide();
        $(hiPrefix + "rm9Row").hide();
    }
    else if(roomCount==5){
    	$(hiPrefix + "rm2Row").show();
        $(hiPrefix + "rm3Row").show();
        $(hiPrefix + "rm4Row").show();
        $(hiPrefix + "rm5Row").show();
        $(hiPrefix + "rm6Row").hide();
        $(hiPrefix + "rm7Row").hide();
        $(hiPrefix + "rm8Row").hide();
        $(hiPrefix + "rm9Row").hide();
    }
    else if(roomCount==6){
    	$(hiPrefix + "rm2Row").show();
        $(hiPrefix + "rm3Row").show();
        $(hiPrefix + "rm4Row").show();
        $(hiPrefix + "rm5Row").show();
        $(hiPrefix + "rm6Row").show();
        $(hiPrefix + "rm7Row").hide();
        $(hiPrefix + "rm8Row").hide();
        $(hiPrefix + "rm9Row").hide();
    }
    else if(roomCount==7){
    	$(hiPrefix + "rm2Row").show();
        $(hiPrefix + "rm3Row").show();
        $(hiPrefix + "rm4Row").show();
        $(hiPrefix + "rm5Row").show();
        $(hiPrefix + "rm6Row").show();
        $(hiPrefix + "rm7Row").show();
        $(hiPrefix + "rm8Row").hide();
        $(hiPrefix + "rm9Row").hide();
    }
    else if(roomCount==8){
    	$(hiPrefix + "rm2Row").show();
        $(hiPrefix + "rm3Row").show();
        $(hiPrefix + "rm4Row").show();
        $(hiPrefix + "rm5Row").show();
        $(hiPrefix + "rm6Row").show();
        $(hiPrefix + "rm7Row").show();
        $(hiPrefix + "rm8Row").show();
        $(hiPrefix + "rm9Row").hide();
    }
    else if(roomCount==9){
    	$(hiPrefix + "rm2Row").show();
        $(hiPrefix + "rm3Row").show();
        $(hiPrefix + "rm4Row").show();
        $(hiPrefix + "rm5Row").show();
        $(hiPrefix + "rm6Row").show();
        $(hiPrefix + "rm7Row").show();
        $(hiPrefix + "rm8Row").show();
        $(hiPrefix + "rm9Row").show();
    }
   
 
   acomShowChildAges(); 
}

function acomShowChildAges(){

    var childFound = false;
    var maxChildCount = 0;

    // Get total rooms
    roomCount = $(hiPrefix + "lstRoomsRequired").value;
   
    // Loop through all rooms
    for (iRoom=1; iRoom<10; iRoom++){
    
        // Get children required
        var childCount = $(hiPrefix + "lstRm" + iRoom + "Children").value;
        
        // Get child row
        var childRow = $(hiPrefix + "rm" + iRoom + "ChildAgesRow");
        
        // Show or hide child row
        if (childCount > 0 && iRoom <= roomCount) {
            childRow.show();
            childFound = true;
        }
        else {
            childRow.hide();
        }
            
        // Loop through children
        for (iChild=1; iChild<6; iChild++) {
        
            // Get child list
            var childList = $(hiPrefix + "lstRm" + iRoom + "ChildAge" + iChild);
        
            // Show or hide child list
            if (iChild <= childCount) {
                if (iChild > maxChildCount){maxChildCount = iChild};
                childList.show()
            }
            else {
                childList.hide()
            } 
        }
    }   

    // Loop through children
    for (iChild=1; iChild<6; iChild++) {
        // Get child title
        var childTitle = $(hiPrefix + "Child" + iChild + "AgeTitle").show();
        // Show or hide child title
        if (iChild <= maxChildCount){childTitle.show();}
        else {childTitle.hide();}
    }

    if(childFound==true){
        $(hiPrefix + "rwChildAgesTitle").show();
        $(hiPrefix + "rwChildAges").show();
    }
    else {
        $(hiPrefix + "rwChildAgesTitle").hide();
        $(hiPrefix + "rwChildAges").hide();
    }
}

function acomSetDefDate(){
	if($(hiPrefix + "txtCheckOutDate").value==""){
		$(hiPrefix + "txtCheckOutDate").value = $(hiPrefix + "txtCheckInDate").value;
	}
}
