
function hotel( cName, code )
{
	this.cname = cName;
	this.code = code;
	this.promotions = [];
}

function promotion( cName , code, parent)
{
	this.cname = cName;
	this.code = code;
	this.parent = parent;
}


function formdata ( stringLoad )
{
	
	this.stringLoad = stringLoad;
	this.hotels = [];
	
	this.sortHotels = [];
	this.sortPromotions = [];
	
	/* cb id names */
	this.cbHotel = null;
	this.cbPromotion = null;

	/* hd id names */
	this.hdHotel = null;
	this.hdPromotion = null;
	
	this.ignorechange=false;

	/* cb option name */
	this.opTextHotel = "?";
	this.opTextPromotion = "?";
	
	/* initial/selected values */
	this.iniHotel = "";
	this.iniPromotion = "";
	
	/* initial disabled values */
	this.disHotel = "";
	this.disPromotion = "";
	
}

formdata.prototype.init = function ()
{
	this.loadFromString ( this.stringLoad );
	this.loadCombos ( 0, null );	
}

formdata.prototype.findPromotionByID = function ( code ) 
{
	var i;


	for (i=0; i<this.sortPromotions.length; i++)
	{
		if ( this.sortPromotions[i].code == code ) return this.sortPromotions[i];
	}

	return null;
}

formdata.prototype.findHotelByID = function ( code ) 
{
	var i;

	
	for (i=0; i<this.sortHotels.length; i++)
	{
		if ( this.sortHotels[i].code == code ) return this.sortHotels[i];
	}

	return null;
}


formdata.prototype.addHotel = function ( cName, code ) 
{
	var current_hotel = new hotel(cName, code);
	this.hotels.push( current_hotel );
	this.sortHotels.push( current_hotel );

	return current_hotel;
}

formdata.prototype.addPromotion = function ( cName, code, parent ) 
{
	var current_promotion = new promotion(cName, code, parent);
	parent.promotions.push(current_promotion);
	this.sortPromotions.push(current_promotion);
	
	return current_promotion;
}
//todojc
formdata.prototype.listHotels = function ( )
{
	var out="", i,j,k;
	var city, hotel, promotion; 
	
	for (j=0; j<country.cities.length; j++)
	{
		city = country.cities[j];
		
		for (k=0; k<city.hotels.length; k++)
		{
			hotel = city.hotels[k];
			
			for (i=0; i<this.promotions.length; i++)
			{
				promotion  = this.promotions[i];
				out = out + country.cname + "." + city.cname + "." + hotel.cname + "." + promotion.cname + ";";
			}
		}
	}
	
	alert ( out );
}

formdata.prototype.objectCompare = function ( a, b )
{
	if ( a.cname < b.cname ) return -1;
	else if (a.cname > b.cname ) return 1;
	else return 0;
}


formdata.prototype.loadFromString = function ( stringLoad )
{
	var nodes = stringLoad.split(";");
	var i;
	var last_hotel, last_promotion;
	
	for (i=0; i<nodes.length; i++)
	{
		var splits = nodes[i].split(":");
		var nodetype = splits[0].substr(0,1);
		var val = splits[0].substr(1,splits[0].length-1);
		var code = splits[1];
		switch ( nodetype )	
		{
			case 'H':
				last_hotel = this.addHotel( val, code);
				break;
			case 'P':
				last_promotion = this.addPromotion( val, code, last_hotel );
				break;
		}
	}
	
	this.sortHotels.sort ( this.objectCompare );
	this.sortPromotions.sort ( this.objectCompare );
}

formdata.prototype.optionLanguageText = function ( tHotel, tPromotion )
{
	this.opTextHotel = tHotel;
	this.opTextPromotion = tPromotion;
}


formdata.prototype.removeSubCombos = function ( )
{
	var i;
	
	if( this.cbPromotion != null)
	{
		for ( ; this.cbPromotion.length>0 ; ) 
		{
			this.cbPromotion.options[this.cbPromotion.length-1] = null;
		}
	}	
}

formdata.prototype.setDefaults = function ( iniHotel, iniPromotion )
{
	this.iniHotel = iniHotel;
	this.iniPromotion = iniPromotion;
}

formdata.prototype.setEnabled = function ( enableHotel, enablePromotion )
{
	this.disHotel = !enableHotel;
	this.disPromotion = !enablePromotion;
}
	
formdata.prototype.setComboID = function ( cbhotel, cbPromotion )
{
	this.cbHotel = cbhotel;
	this.cbPromotion = cbPromotion;
}	

formdata.prototype.setHiddenID = function ( hdhotel, hdpromotion )
{
	this.hdHotel = hdhotel;	
	this.hdPromotion = hdpromotion;	
}	


formdata.prototype.loadComboHotel = function ( city_changed )
{
	var selected = false;

	this.cbHotel.options[0] = new Option ( this.opTextHotel, "*", false, false);

	if ( city_changed != null )	{

	    city_changed.hotels.sort ( this.objectCompare );

		for (i=0; i<city_changed.hotels.length; i++)
		{
			selected = ( city_changed.hotels[i].code == this.iniHotel );
			this.cbHotel.options[i+1] = new Option ( city_changed.hotels[i].cname, city_changed.hotels[i].code, false, selected );
		}
	}
	else
	{
		this.cbHotel.options[0] = new Option ( this.opTextHotel, "*", false, false);
		for (i=0; i<this.sortHotels.length; i++)
		{
			selected = ( this.sortHotels[i].code == this.iniHotel );
			this.cbHotel.options[i+1] = new Option ( this.sortHotels[i].cname, this.sortHotels[i].code, false, selected);
		}
	}
	this.cbHotel.disabled = this.disHotel;
}


formdata.prototype.loadPromotionParents  = function ( promotion_changed )
{
	this.ignorechange = true;

	this.setDefaults ( promotion_changed.parent.code, promotion_changed.code );

	this.removeSubCombos ( );
							
	this.loadComboHotel ( promotion_changed.parent.parent );	
	
	this.ignorechange = false;
}

formdata.prototype.loadComboPromotion = function ( hotel_changed )
{
	var selected = false;

	if(this.cbPromotion!=null)
	{
	this.cbPromotion.options[0] = new Option ( this.opTextPromotion, "*", false, false);

	if (hotel_changed != null )
	{
		hotel_changed.promotions.sort ( this.objectCompare );
			
		for (i=0; i<hotel_changed.promotions.length; i++)
		{
			selected = ( hotel_changed.promotions[i].code == this.iniPromotion );
	       	this.cbPromotion.options[i+1] = new Option ( hotel_changed.promotions[i].cname, hotel_changed.promotions[i].code, false, selected);
		}		
	}
	else
	{
		var promoAdd = '';
		var pos = 1;
		this.cbPromotion.options[0] = new Option ( this.opTextPromotion, "*", false, false);
		for (i=0; i<this.sortPromotions.length; i++)
		{
			if(promoAdd.indexOf(';'+this.sortPromotions[i].code+';')<0)
            {
				promoAdd = promoAdd + ';' + this.sortPromotions[i].code + ';';
				this.cbPromotion.options[pos] = new Option ( this.sortPromotions[i].cname, this.sortPromotions[i].code, false, false);
				pos++;
			}
		}
	}
	this.cbPromotion.disabled = this.disPromotion;
}
}


formdata.prototype.loadCombos = function ( Initialize, objchanged )
{
	var i;
	
	switch ( Initialize)
	{
		case 0:
		{		
			this.cbHotel = document.forms[0].elements[ this.cbHotel ];
			this.cbPromotion = document.forms[0].elements[ this.cbPromotion ];
			
			this.hdHotel = document.forms[0].elements[ this.hdHotel ];
			this.hdPromotion = document.forms[0].elements[ this.hdPromotion ];
			
            var hotel = this.findHotelByID( this.iniHotel );
            
			this.loadComboHotel ( );	
			this.loadComboPromotion (hotel);				

			break;
		}
		case 1:
		{
			// Hotel changed
			var hotel_changed = objchanged;
			this.removeSubCombos ( );
			this.loadComboPromotion ( hotel_changed );
			break;
		}
		case 2:
		{
			var promotion_changed = objchanged;
			break;
		}
	}
}

function _onLoad()
{
    formData1.init();
}

function hotelChange(o, master)
{
	if (!master.ignorechange )
	{
		master.ignorechange = true;
		
		var hotel = master.findHotelByID ( o.value );
		if ( hotel != null && hotel.code!='*')
		{
			master.loadCombos (1, hotel);
		}
		else
		{
			master.cbPromotion.value = '*' ;
			master.loadCombos (1, null);
		}

		master.ignorechange = false;
	}
}

function promotionChange(o, master)
{
	if (!master.ignorechange )
	{
		master.ignorechange = true;

		var promotion = master.findPromotionByID ( o.value );
		if (promotion != null )
		{
			master.loadCombos (2, promotion);	
		}
	
		master.ignorechange = false;
	}
}

function updateHidden( master)
{
    master.hdHotel.value = master.cbHotel.value;
    if(master.hdPromotion!=null && master.cbPromotion!=null)
    {
		var textSelected = master.cbPromotion.options[master.cbPromotion.selectedIndex].text;
		master.hdPromotion.value = master.cbPromotion.value + '#' + textSelected;
	}    
}

function addOnloadEvent(fnc)
{
      if ( typeof window.addEventListener != "undefined" )
      {
            // mozilla
            window.addEventListener( "load", fnc, false );
      }
      else if ( typeof window.attachEvent != "undefined" ) 
      {
            //ie
            window.attachEvent( "onload", fnc );
      }
      else 
      {
            if ( window.onload != null ) 
            {
                  var oldOnload = window.onload;
                  window.onload = function ( e ) 
                  {
                        oldOnload( e );
                        window[fnc]();
                  };
            }
            else window.onload = fnc;
      }
}

