
function hotel( cName, code, parent )
{
	this.cname = cName;
	this.code = code;
	this.parent = parent;
	this.promotions = [];
}

function city ( cName, code )
{
	this.cname = cName;
	this.code = code;
	this.hotels = [];
}

function promotion( cName , code, parent)
{
	this.cname = cName;
	this.code = code;
	this.parent = parent;
}


function formdata ( stringLoad )
{
	
	this.stringLoad = stringLoad;
	this.cities = [];
	
	this.sortCities = [];
	this.sortHotels = [];
	this.sortPromotions = [];
	
	/* cb id names */
	this.cbCity = null;
	this.cbHotel = null;
	this.cbPromotion = null;

	/* hd id names */
	this.hdCity = null;
	this.hdHotel = null;
	this.hdPromotion = null;
	
	this.ignorechange=false;

	/* cb option name */
	this.opTextCity = "?";
	this.opTextHotel = "?";
	this.opTextPromotion = "?";
	
	/* initial/selected values */
	this.iniCity = "";
	this.iniHotel = "";
	this.iniPromotion = "";
	
	/* initial disabled values */
	this.disCity = "";
	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.findCityByID = function ( code ) 
{
	var i;

	for (i=0; i<this.sortCities.length; i++)
	{
		if ( this.sortCities[i].code == code ) return this.sortCities[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.addCity = function ( cName, code ) 
{
	var current_city = new city(cName, code);
	this.cities.push ( current_city );
	this.sortCities.push ( current_city );
	
	return current_city;
}

formdata.prototype.addHotel = function ( cName, code, parent ) 
{
	var current_hotel = new hotel(cName, code, parent);
	parent.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;
}

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_city, 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 'C':
				last_city = this.addCity ( val, code);
				break;
			case 'H':
				last_hotel = this.addHotel( val, code, last_city );
				break;
			case 'P':
				last_promotion = this.addPromotion( val, code, last_hotel );
				break;
		}
	}
	
	this.sortCities.sort ( this.objectCompare );
	this.sortHotels.sort ( this.objectCompare );
	this.sortPromotions.sort ( this.objectCompare );
}

formdata.prototype.optionLanguageText = function ( tCity, tHotel, tPromotion )
{
	this.opTextCity = tCity;
	this.opTextHotel = tHotel;
	this.opTextPromotion = tPromotion;
}


formdata.prototype.removeSubCombos = function ( what )
{
	var i;
	
	if( this.cbPromotion != null)
	{
		for ( ; this.cbPromotion.length>0 ; ) 
		{
			this.cbPromotion.options[this.cbPromotion.length-1] = null;
		}
	}
	if (what > 1)
	{
		if(this.cbHotel!=null)
		{
			for ( ; this.cbHotel.length>0 ;) 
			{
				this.cbHotel.options [ this.cbHotel.length-1 ] = null;
			}
		}
	}
}

formdata.prototype.setDefaults = function ( iniCity, iniHotel, iniPromotion )
{
	this.iniCity = iniCity;
	this.iniHotel = iniHotel;
	this.iniPromotion = iniPromotion;
}

formdata.prototype.setEnabled = function ( enableCity, enableHotel, enablePromotion )
{
	this.disCity = !enableCity;
	this.disHotel = !enableHotel;
	this.disPromotion = !enablePromotion;
}
	
formdata.prototype.setComboID = function ( cbcity, cbhotel, cbPromotion )
{
	this.cbCity = cbcity;
	this.cbHotel = cbhotel;
	this.cbPromotion = cbPromotion;
}	

formdata.prototype.setHiddenID = function ( hdcity, hdhotel, hdpromotion )
{
	this.hdCity = hdcity;
	this.hdHotel = hdhotel;	
	this.hdPromotion = hdpromotion;	
}	

formdata.prototype.loadComboCity = function (  )
{
	if(this.cbCity!=null)
	{
		var selected = false;
		
		this.cbCity.options[0] = new Option ( this.opTextCity, "*", false, false);
		for (i=0; i<this.sortCities.length; i++)
		{
			selected = ( this.sortCities[i].code == this.iniCity );
			this.cbCity.options[i+1] = new Option ( this.sortCities[i].cname, this.sortCities[i].code, false, selected );
		}
		this.cbCity.disabled = this.disCity;
	}
}

formdata.prototype.loadComboHotel = function ( city_changed )
{
	var selected = false;
	if(this.cbHotel!=null)
	{
		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.loadComboHotelByID = function ( city_changed, id )
{
	city_changed.hotels.sort ( this.objectCompare );

	for (i=0; i<city_changed.hotels.length; i++)
	{
		selected = ( city_changed.hotels[i].code == id );
		this.cbHotel.options[i+1] = new Option ( city_changed.hotels[i].cname, city_changed.hotels[i].code, false, selected);
	}
}

formdata.prototype.loadPromotionParents  = function ( promotion_changed )
{
	this.ignorechange = true;

	this.setDefaults ( promotion_changed.parent.parent.code, promotion_changed.parent.code, promotion_changed.code );

	this.removeSubCombos ( 2 );
							
	for (i=0; i<this.cbCity.options.length; i++ )
	{
		if ( this.cbCity.options[i].value == this.iniCity )
		{
			this.cbCity.selectedIndex = i;
			break;
		}
	}

	this.loadComboHotel ( promotion_changed.parent.parent );	
	//this.loadComboPromotion ( null, promotion_changed.parent );	

	this.ignorechange = false;
}

formdata.prototype.loadComboPromotion = function ( city_changed, hotel_changed )
{
	var selected = false;

	if(this.cbPromotion!=null)
	{
		this.cbPromotion.options[0] = new Option ( this.opTextPromotion, "*", false, false);

		if ( city_changed != null )
		{	
			var promoAdd = '';
			
			for (j=1, i=0; i<this.sortPromotions.length; i++)
			{
				if ( this.sortPromotions[i].parent.parent.code == city_changed.code )
				{
            		selected = ( this.sortPromotions[i].code == this.iniPromotion );
	            	
            		if(promoAdd.indexOf(';'+this.sortPromotions[i].code+';')<0)
            		{
            			promoAdd = promoAdd + ';' + this.sortPromotions[i].code + ';';
						this.cbPromotion.options[j++] = new Option ( this.sortPromotions[i].cname, this.sortPromotions[i].code, false, selected );
					}
				}
			}
		}
		else if (hotel_changed != null )
		{
			this.loadComboCity ( );
			
			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);
			}

			/* exceptional case */		
			
			if ( this.cbCity.options[ this.cbCity.selectedIndex ].value != hotel_changed.parent.code )
			{
				for (i=0; i<this.cbCity.options.length; i++ )
				{
					if ( this.cbCity.options[i].value == hotel_changed.parent.code )
					{
						this.cbCity.selectedIndex = i;
						this.setDefaults ( hotel_changed.parent.code, hotel_changed.code, this.cbPromotion.code );
						
						this.cbCity.onchange ( this.cbCity, this );
						
						break;
					}
				}				
			}			
		}
		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 + ';';
					
					if(this.iniPromotion!=null && this.iniPromotion!='')
					{
						selected = ( this.sortPromotions[i].code == this.iniPromotion );
	       				this.cbPromotion.options[pos] = new Option ( this.sortPromotions[i].cname, this.sortPromotions[i].code, false, selected);
					}
					else
					{	
						this.cbPromotion.options[pos] = new Option ( this.sortPromotions[i].cname, this.sortPromotions[i].code, false, false);
					}
					pos++;
				}
			}
		}
		this.cbPromotion.disabled = this.disPromotion;
	}
	else
	{
		if (hotel_changed != null & this.cbCity!=null)
		{
			if ( this.cbCity.options[ this.cbCity.selectedIndex ].value != hotel_changed.parent.code )
			{
				for (i=0; i<this.cbCity.options.length; i++ )
				{
					if ( this.cbCity.options[i].value == hotel_changed.parent.code )
					{
						this.cbCity.selectedIndex = i;
						this.setDefaults ( hotel_changed.parent.code, hotel_changed.code, null );
						this.cbCity.onchange ( this.cbCity, this );
						
						break;
					}
				}
			}
		}
	}
}


formdata.prototype.loadCombos = function ( Initialize, objchanged )
{
	var i;
	
	switch ( Initialize)
	{
		case 0:
		{	
			this.cbCity = document.forms[0].elements[ this.cbCity ];
			this.cbHotel = document.forms[0].elements[ this.cbHotel ];
			this.cbPromotion = document.forms[0].elements[ this.cbPromotion ];
			
			this.hdCity = document.forms[0].elements[ this.hdCity ];
			this.hdHotel = document.forms[0].elements[ this.hdHotel ];
			this.hdPromotion = document.forms[0].elements[ this.hdPromotion ];
			
            var city = this.findCityByID ( this.iniCity );
			var hotel = this.findHotelByID( this.iniHotel );
            
			this.loadComboCity ( );
			this.loadComboHotel ( city );	
			this.loadComboPromotion (null, hotel);				

			break;
		}
		case 1:
		{
			// city changed
			var city_changed = objchanged;
			this.removeSubCombos ( 2 );
			this.loadComboHotel ( city_changed );
			this.loadComboPromotion ( city_changed, null );
			break;
		}
		case 2:
		{
			// Hotel changed
			var hotel_changed = objchanged;
			this.removeSubCombos ( 2 );
			this.loadComboHotelByID ( hotel_changed.parent, hotel_changed.code );
			this.loadComboPromotion ( null, hotel_changed );
			break;
		}
		case 3:
		{
			var promotion_changed = objchanged;
			//this.loadPromotionParents ( promotion_changed );
			break;
		}
	}
}



function _onLoad()
{
    formData1.init();
}

function regionChange(o, master)
{
	if (!master.ignorechange )
	{
		master.ignorechange = true;
		
		var city = master.findCityByID ( o.value );
		master.loadCombos (1, city);	
		master.ignorechange = false;
	}
}

function hotelChange(o, master)
{
	if (!master.ignorechange )
	{
		master.ignorechange = true;
		
		var hotel = master.findHotelByID ( o.value );
		if ( hotel != null )
		{
			master.loadCombos (2, hotel);
		}
		else
		{
			if(master.cbPromotion!=null)
			{	
				master.cbPromotion.value = '*' ;
			}
		}

		master.ignorechange = false;
	}
}

function promotionChange(o, master)
{
	if (!master.ignorechange )
	{
		master.ignorechange = true;

		var promotion = master.findPromotionByID ( o.value );
		if (promotion != null )
		{
			master.loadCombos (3, promotion);	
		}
	
		master.ignorechange = false;
		
	}
}

function updateHidden( master)
{

    master.hdHotel.value = master.cbHotel.value;
    master.hdCity.value = master.cbCity.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;
      }
}

