/*OBJECT DEFINITIONS
for Booking
Object definitions for the objects involved in the booking form - 
overrall BookingStages object holding all stages
individual BookingStage holding all items in that stage
individual BookingItem
*/
function BookingStages(form){
	this.bookingForm = form;
	this.bookingStages;
	this.orderedStageNames;
	this.bookingDisplayState = true;
	this.currentBookingStage = 3;
	this.setBookingStages = function(stages) {
		this.bookingStages = stages;
	}
	this.getBookingStageByName = function(name) {
		if(this.bookingStages){return this.bookingStages[name];}return false;
	}
	this.setOrderedStageNames = function(orderedNames) {this.orderedStageNames = orderedNames;}
	this.getNextStageName = function(stageName) {
		if(this.orderedStageNames[stageName]){return this.orderedStageNames[stageName]}; return false;
	}
	this.setBookingDisplayState = function(aBool) {
    var selectDurationOption = this.getBookingStageByName('chooseDate').getBookingItemByName('stay').htmlNode.options[0];
    var selectedDestination = this.getBookingStageByName('chooseDestination').getBookingItemByName('village').value;
		this.bookingDisplayState = aBool;
		if(this.bookingDisplayState){
			if(this.currentBookingStage < 3 ){
				this.currentBookingStage = 3;
			}
		}else{
            if (selectedDestination != 'NL' && selectedDestination != 'BE' && selectedDestination != 'DE' && selectedDestination != 'FR'){
                this.currentBookingStage = 1;
            }else {
                this.currentBookingStage = 0;
            }
			//reset the duration to prompt for a new selectin
			//if(selectDurationOption.selected != true){
			//	if(document.all){
			//		var selectMenu = this.getBookingStageByName('chooseDate').getBookingItemByName('stay').htmlNode;
			//		if(selectDurationOption.text == selectMenu.options(selectMenu.options.length - 1).text){
      //
			//		}else{
			//			this.getBookingStageByName('chooseDate').getBookingItemByName('stay').htmlNode.add(selectDurationOption);
			//		}
			//	}else{
			//		this.getBookingStageByName('chooseDate').getBookingItemByName('stay').htmlNode.add(selectDurationOption , null);
			//	}
			//}
			this.setToDefaults();
			selectDurationOption.selected = true;
		}
		this.resetStageDisplays();
	}
	//end setBookingDisplayState
	this.resetStageDisplays = function(){
		//run through all the booking stages whose stageNumber is less than or equal to currentBookingStage
		//and  call a function to set their displays to block, setting all the others to none
		for (var i=0; i<this.bookingStages.length; i++)
		{
			var currentStage = this.getBookingStageByName(this.bookingStages[i]);
			var currentStageDisplayState = false;
			if(currentStage.stageNumber <= this.currentBookingStage){
				currentStageDisplayState = true;
			}
			currentStage.setDisplayForState(currentStageDisplayState);
		}
		//for (stageKey in this.bookingStages) {
		//	var currentStage = this.getBookingStageByName(stageKey);
		//	var currentStageDisplayState = false;
		//	if(currentStage.stageNumber <= this.currentBookingStage){
		//		currentStageDisplayState = true;
		//	}
		//	currentStage.setDisplayForState(currentStageDisplayState);
		//}
	}
	this.setToDefaults = function(){

		for (var bs=0; bs<this.bookingStages.length; bs++)
		{
     		this.getBookingStageByName(this.bookingStages[bs]).resetDefaults();
		}
		//for (stageKey in this.bookingStages) {
		//	this.getBookingStageByName(stageKey).resetDefaults();
		//}		
	}
	this.setSpecificStageDisplays = function(){
		//write a function that sets up the specific stage displays
	}
	//end of reset stage displays

	//this.selectDurationOption = document.createElement('OPTION');
	//this.selectDurationOption.text = "Select duration";
	//this.selectDurationOption.selected = false;
}

function BookingStage(stage, name) {
	this.completed = false;
	this.stage = stage;
	this.name = name;
	this.toString = function() {return "BookingStage " + this.name + " completed is " + this.completed};
	this.stageNumber = 0;
	this.bookingItems;
	this.bookingStageDisplayState = false;
	this.getName = function(){return this.name;}
	this.setCompleted = function(completed){this.completed = completed}
	this.getBookingItems = function() { try{if(this.bookingItems){return this.bookingItems;} return false} catch(e){alert('no booking items'); return false;}}
	this.getBookingItemByName = function(name) {if(this.getBookingItems()){return this.getBookingItems()[name]} return false}
	this.setCompleted = function(stageState){
	
	var x=0;
	for (x=0; x<this.bookingItems.length; x++)
	{
      if (this.bookingItems[x].name != "") this.bookingItems[x].setChanged(true);	
	}
	
//    for (itemKeys in this.bookingItems){
//      if (this.bookingItems[itemKeys].name != "") this.bookingItems[itemKeys].setChanged(true);
//		}
		this.completed = stageState;
	}
	this.isCompleted = function() {
		var x1=0;
		for (x1=0; x1<this.bookingItems.length; x1++)
		{
			if(!this.bookingItems[x1].changed){
				return false;
			}
		}
		//for (itemKeys in this.bookingItems){
		//	if(!this.bookingItems[itemKeys].changed){
		//		return false;
		//	}
		//}
		this.bookingStageDisplayState = true;
		return true;
	}
	this.setStageNumber = function(aNumber) {this.stageNumber = aNumber};
	this.getStageNumber = function() {return this.stageNumber};
  this.setEnabledForState = function(aBool) {
    var divs = this.stage.getElementsByTagName('DIV');
    for (var i=0;i < divs.length; i++) {
      var div = divs[i];
      if (aBool) {
        div.className = "button";
        div.onclick = next;
      }
      else {
        div.className = "disabled button";
        div.onclick = function() {
          cpAlert(labels.lbl_booktool_incomplete);
        }
        return false;
      }
    }
  }
	this.setDisplayForState = function(aBool) {
		var displayValue;
		if(aBool){
      displayValue = 'block';
		}else{
			displayValue = 'none';
		}
    this.stage.style.display = displayValue;
	}
	this.resetDefaults = function(){
		var x2=0;
		for (x2=0; x2<this.bookingItems.length; x2++)
		{
			if(itemKeys == 'adult'){ //HACKKKK!!
				setAdultOptionsForSelectFromMinToMax(this.bookingItems[x2].htmlNode , 0 , 10 , 0 , true);
			}
			if(/*itemKeys == 'children' ||*/ itemKeys == 'pets'){
				setSelectOptionsFromMinToMax(this.bookingItems[x2].htmlNode , 0 , 0 , 0);
			}
			this.bookingItems[x2].resetValue();

		}
		
		//for (itemKeys in this.bookingItems){
		//	if(itemKeys == 'adult'){ //HACKKKK!!
		//		setAdultOptionsForSelectFromMinToMax(this.bookingItems[itemKeys].htmlNode , 0 , 10 , 0 , true);
		//	}
		//	if(/*itemKeys == 'children' ||*/ itemKeys == 'pets'){
		//		setSelectOptionsFromMinToMax(this.bookingItems[itemKeys].htmlNode , 0 , 0 , 0);
		//	}
		//	this.bookingItems[itemKeys].resetValue();
		//}
		
	}
}
BookingStage.prototype.setBookingItems = function(bookingItemList) {this.bookingItems = bookingItemList;}

function BookingItem(selectEl , name) {
	this.htmlNode = selectEl;
	this.name = name;
	this.value = selectEl.value;
	this.defaultValue = selectEl.value;
	this.toString = function() {return "BookingItem " + this.name + " completed is " + this.changed + ' with value:\n|' + this.htmlNode.value +'|'};
	this.setValue = function(aValue) {
		this.value = aValue;
    var optionsLength = (typeof this.htmlNode.options == 'object') ? this.htmlNode.options.length : 0;
		for (var i=0; i<optionsLength; i++){
			this.htmlNode.options.item(i).selected = false;
			if(this.value == this.htmlNode.options.item(i).value){
				this.htmlNode.options.item(i).selected = true;
			}
		}
	}
	this.getName = function() {return name};
	this.changed = false;
	this.setChanged = function(newValue) {this.changed = newValue;}
	this.resetValue = function(){
		this.setValue(this.defaultValue);
	}
}
