/**
 *
 * Copyright World.Net Services Ltd, all rights reserved, 2004, 2005.
 *
 */

/**
 *	1. Only display Addons that have rates for the total selected booking period.
 *	
 */

/**
 * Addons (Options) Class.
 *
 */
function Addon_Grid()
{
	this.addon_grid_obj 			= document.getElementById('ADDON_GRID');
	this.addon_show_button_obj		= document.getElementById('ADDON_GRID_SHOW_BUTTON');
	this.frm	 			= window.document.forms['BOOK_FORM'];
	this.active				= false;
//	this.addon_arr				= new Array();			// Array of addon's (a.k.a. components).	
	this.addon_cvm_cov_map_arr		= new Array();
	this.addon_name_arr			= new Array();			// Array of addon name and descriptions.
	this.addon_value_arr			= new Array();			// Array contianing the selected addon numbers.
	this.mode				= "FULL";
	this.currency_symbol			= '$';
	this.list_title				= "Summary of Available Options";
	this.grid_title				= "Select Options";
	this.grid_header1			= "Details:";
	this.grid_header2			= "Rate<BR>(per person):";
	this.grid_header3			= "Adult:";
	this.grid_header4			= "Child:";
	this.grid_header5			= "Cost:";
	this.option_cost_label			= "Options Cost:";
	this.rate_type_msg			= new Array();
	this.rate_type_msg['BAO']		= "per booking";
	this.rate_type_msg['BRA']		= "per day";	
	this.step_title				= "STEP 1 OPTIONS";
	this.step_description			= "Select options";

	this.addon_rate_obj_arr			= new Array();			// Array of addon rate objects.
	this.addon_cost_obj_arr			= new Array();			// Array of addon cost objects.
	this.addon_total_cost_obj;
}

/**
 *
 */
Addon_Grid.prototype.setCurrencySymbol = function(currency_symbol) 
{
	this.currency_symbol=currency_symbol;
//	this.currency_symbol='$';
}


/**
 *
 */
Addon_Grid.prototype.init = function()
{
	// Create this when the initial list is ceated.
	this.createAddonValueArr();//EVO.

	this.set("LIST");
}


/**
 *
 */
Addon_Grid.prototype.set = function(mode) 
{
	var frm	= window.document.forms['BOOK_FORM'];
	if (!frm || !this.addon_grid_obj)
		return false;

//EVO	if(this.mode==mode)
//		return true;

	this.active=this.addon_name_arr.length>0;
	
	if(this.active) {
		this.mode=mode;
		if(this.mode=="LIST") {
			var html=this.getAddon_List();
			this.addon_grid_obj.innerHTML = html;

		}
		else {
			var html=this.getAddon_Grid();
			this.addon_grid_obj.innerHTML = html;


			var cnt=this.addon_name_arr.length;
			for(var i=0; i<cnt; i++) {
				var obj = document.getElementById('addon_rate'+i);
				if(obj)
					this.addon_rate_obj_arr[i]=obj;
			//	alert('rate obj:'+obj);
				var obj = document.getElementById('addon_cost'+i);
				this.addon_cost_obj_arr[i]=obj;
				this.addon_total_cost_obj = document.getElementById('addon_total_cost');
			}

			// Make the "Show Options" button dissaper from the screen.
			if(this.addon_show_button_obj)
				this.addon_show_button_obj.innerHTML="";
		}

		// Create the value array for everone.
//		this.createAddonValueArr();

	}
}

/**
 *
 */
Addon_Grid.prototype.getAddon_List = function()
{
	var cnt=this.addon_name_arr.length;
	var colspan=(cnt>3) ? 3: cnt;

	var html = '<table width="97%" cellspacing="1" cellpadding="2" border="0">';
	html		+= '<tr><td class="formtitle" colspan='+colspan+'>'+this.list_title+'</td></tr>';
	html		+= '<tr class="details_text">';

	for(var i=0,j=0; i<cnt; i++) {
		var rec=this.addon_name_arr[i];

		// Skip over mandatory components.
		if(rec[3]=='Y' )
			continue;

		// Skip over deactivate components. EVO.
		if(!this.addon_value_arr[i].active)
			continue;


		if((j>0) && !(j%colspan))
			html 	+= '</tr><tr class="details_text">';
		html	+= '<td><li>'+rec[4]+'</td>';

		j++;
	}
	for(; (j%colspan); j++)
		html 	+= '<td>&nbsp</td>';

	html	+= '</tr>';
	html	+=	'</table>';
	return html;
}


Addon_Grid.prototype.getAddon_Grid = function()
{

	var total_option_cost=999;

	var html = '<table width="97%" cellspacing="1" cellpadding="2" border="0">'+
	             '<tr>'+
	                 '<td class="formtitle" colspan="5">'+this.grid_title+'</td>'+
	             '</tr>'+
	             '<tr>'+
                         '<td class="formlabel">'+this.grid_header1+'</td>'+
                        '<td class="formlabel">'+this.grid_header2+'</td>'+
	                 '<td class="formlabel">'+this.grid_header3+'</td>'+
	                 '<td class="formlabel">'+this.grid_header4+'</td>'+
		         '<td class="formlabel">'+this.grid_header5+'</td>'+
                     '</tr>';

	var cnt=this.addon_name_arr.length;
	for(var i=0; i<cnt; i++) {
		var rec=this.addon_name_arr[i];

		// Skip over mandatory components.
		if(rec[3]=='Y' )
			continue;

		// Skip over deactivate components. EVO.
		if(!this.addon_value_arr[i].active)
			continue;


		html	+= '<tr class="details_text">';
		html	+= '<td width="400"><b>'+rec[4]+'</b><br>'+rec[5]+'</td>';

		html	+=	'<td><span id="addon_rate'+i+'"></span> ';
		html	+=	'<br>('+this.rate_type_msg[rec[2]]+')</td>';

		html	+= '<td>';
		html	+= '<select name="addon_adult_'+i+'" ';
		html 	+=  'onchange="res_calc.changeAddon('+rec[1]+',\'adult\',';
		html	+= 'window.document.forms[0].addon_adult_'+i+'.value);"';;
		html	+= '><option></option>';
		for(j=1;j<=10;j++) {
			html	+=	'<option value='+j;
			if(this.addon_value_arr[i].nAdults==j)
				html	+= ' selected';
			html	+=	'>'+j+'</option>';
		}
		html	+= '</select>';
		html	+= '</td>';

		html	+= '<td>';

		html	+= '<select name="addon_child_'+i+'" ';
		html 	+=  'onchange="res_calc.changeAddon('+rec[1]+',\'child\',';
		html	+= 'window.document.forms[0].addon_child_'+i+'.value);"';
		html	+= '><option></option>';

		for(j=1;j<=10;j++) {
			html	+=	'<option value='+j;
			if(this.addon_value_arr[i].nChildren==j)
				html	+= ' selected';
			html	+=	'>'+j+'</option>';
		}	
		html	+= '</select>';
		html	+= '</td>';

		html	+=	'<td><nobr><span id="addon_cost'+i+'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></nobr></td>';
		html	+= '</tr>';
	}


	html	+=	'<tr><td colspan="4" align="right" class="formlabel_total">'+this.option_cost_label+'</td>';
	html	+=	'<td class="formtext_total"><span id="addon_total_cost"></span></td></tr>';
	html	+=	'</table>';
	return html;
}


/**
 *
 */
Addon_Grid.prototype.createAddonValueArr = function()
{
	var cnt=this.addon_name_arr.length;
	for(var i=0; i<cnt; i++) {
		var rec=this.addon_name_arr[i];

		vrec = new Object();
		vrec.rcm_id=rec[1];
		vrec.cvm_id=0;
		vrec.cov_id=0;
		vrec.rate=0.0;
		vrec.cost=0.0;
		vrec.nAdults=0;
		vrec.nChildren=0;
		vrec.active=true;//EVO.
		vrec.prev_active=true;//Stored the previous value of action. Seg in segment.js.

		this.addon_value_arr[i]=vrec;
	}
}

/**
 *
 */
Addon_Grid.prototype.setAddonValueArr = function(addon_arr) 
{
	if(this.active==false)
		return;

	//EVO START.
	this.addon_value_arr = addon_arr;
	if(this.activeFlagChanged(addon_arr))
		this.set(this.mode); // update the display.
	//EVO END.


	var frm=this.frm;
	var currency_symbol=this.currency_symbol;
	var addon_total_cost=0.0;
	var len=addon_arr.length;
	for(var i=0; i<len; i++) {
		var obj = addon_arr[i];

		if(!obj.active) {
			// Reset this entity.
			obj.nAdults=0;
			obj.nChildren=0;	
		}
		else {
			var f='addon_rate'+i;

			if(this.addon_rate_obj_arr[i]) {
				this.addon_rate_obj_arr[i].innerHTML = this.currency_symbol+obj.rate;
				f='addon_cost'+i;
				if(obj.nAdults>0 || obj.nChildren>0) {
					var cost=this.currency_symbol+obj.cost;
					addon_total_cost+=obj.cost;
				}
				else
					cost="";
				this.addon_cost_obj_arr[i].innerHTML = cost;
			}
		}
	}
	if(len>0 && this.addon_total_cost_obj)
		this.addon_total_cost_obj.innerHTML = (addon_total_cost>0) ? currency_symbol+addon_total_cost : "";

}

/**
 * Checks whether segment.js has changed the active flag.
 */
Addon_Grid.prototype.activeFlagChanged=function(addon_arr)
{
	for(var i=0; i<addon_arr.length; i++) {
		var obj = addon_arr[i];
		if(obj.active!=obj.prev_active) 
			return true;
	}
//	return true;
	return false;
}

/**
 *
 */
Addon_Grid.prototype.getAddonValueArr = function() 
{
	// get the COV_ID's for what we have booked.
	for(var i=0; i<this.addon_value_arr.length; i++) {
		var cvm_id=this.addon_value_arr[i].cvm_id;
		var rcm_id=this.addon_value_arr[i].rcm_id;

//		alert('getAddonValueArr:: cvm_id='+cvm_id+' rcm_id='+rcm_id);


		var cov_id = this.getCOV_ID(cvm_id,rcm_id);

		this.addon_value_arr[i].cov_id=cov_id;
	}
	return this.addon_value_arr;
}

/**
 *
 */
Addon_Grid.prototype.getCOV_ID = function(cvm_id,rcm_id) 
{
	var len=this.addon_cvm_cov_map_arr.length;
	for(var i=0; i<len; i++) {
		var rec=this.addon_cvm_cov_map_arr[i];
		if(rec[1]==rcm_id && rec[2]==cvm_id) {
			return rec[0];
		}
	}
	return 0;
}

/**
 *
 */
Addon_Grid.prototype.activateAddonValueArr = function(rcm_id,type,value) 
{
//	alert ('XXchange addon: '+rcm_id+' '+type+' '+value);

	var done=false;
	var len=this.addon_value_arr.length;
	for(var i=0; i<len; i++) {
		var vrec = this.addon_value_arr[i];
		if(vrec.rcm_id==rcm_id) {
			if(type=="adult")
				vrec.nAdults=value;
			else
				vrec.nChildren=value;
			done=true;
		}
	}

	if(!done)
	alert ('Error: SetAddonValueArr did not find matching addon_value object:'+done);

}


/**
 *
 */
Addon_Grid.prototype.rollback = function() 
{
	return this.addon_value_arr;
}





