
// Global
var formData;
var formDataRes;
var controlRes=0;
var formPromoAddDays=1;

function hotel( cName, code )
{
	this.cname = cName;
	this.code = code;
}

function city ( cName, code )
{
	this.cname = cName;
	this.code = code;
	this.hotels = [];
}

function country( cName , code)
{
	this.cname = cName;
	this.code = code;
	this.cities = [];
}

function formdata ()
{
	
	this.countries = [];
	this.cities2 =[];
	this.hotels2=[];
	this.cbCountry = null;
	this.cbCity = null;
	this.cbHotel = null;
	this.ignorechange=false;
	this.opTextCountry = "?";
	this.opTextCity = "?";
	this.opTextHotel = "?";
	this.countryValue="*";
	this.cityValue="*";
	this.hotelValue="*";
	
}

formdata.prototype.addCountry = function ( cName, code ) 
{
	this.countries.push(new country(cName, code));
	
}

formdata.prototype.addCity = function ( cName, code ) 
{
	var lastcountry = this.countries[ this.countries.length-1 ];
	lastcountry.cities.push ( new city(cName, code) );
	this.cities2.push(new city(cName, code));
}

formdata.prototype.addHotel = function ( cName, code ) 
{
	var lastcountry = this.countries[ this.countries.length-1 ];
	var lastcity = lastcountry.cities[ lastcountry.cities.length - 1 ];
	lastcity.hotels.push( new hotel(cName, code) );
	this.hotels2.push(new hotel(cName, code));
}

formdata.prototype.listHotels = function ( )
{
	var out="", i,j,k;
	var country, city, hotel; 
	
	for (i=0; i<this.countries.length; i++)
	{
		country  = this.countries[i];
		for (j=0; j<country.cities.length; j++)
		{
			city = country.cities[j];
			
			for (k=0; k<city.hotels.length; k++)
			{
				hotel = city.hotels[k];
				
				out = out + country.cname + "." + city.cname + "." + hotel.cname + ";";
			}
		}
	}
	
	alert ( out );
}


formdata.prototype.loadFromString = function ( stringLoad )
{
	var nodes = stringLoad.split(";");
	var i;
	
	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 'P':
			
				this.addCountry( val, code );
				var pais = val;
				break;
			case 'C':
				this.addCity ( val, code );
				var cidade = val;
				break;
			case 'H':
				this.addHotel( val, code );
				break;
		}
	}	
}	

formdata.prototype.optionLanguageText = function ( tCountry, tCity, tHotel )
{
	this.opTextCountry = tCountry;
	this.opTextCity = tCity;
	this.opTextHotel = tHotel;
}


formdata.prototype.removeSubCombos = function ( what )
{
	var i;
	
	for ( ; this.cbHotel.length>0 ; ) 
	{
	
		this.cbHotel.options[this.cbHotel.length-1] = null;
	}
	
	this.cbHotel.options[0] = new Option ( this.opTextHotel, "*", false, false);

	if (what > 1)
	{
		for ( ; this.cbCity.length>0 ;) 
		{
			this.cbCity.options [ this.cbCity.length-1 ] = null;
		}
		this.cbCity.options[0] = new Option ( this.opTextCity, "*", false, false);
	}
}
	
formdata.prototype.setComboID = function ( cbcountry, cbcity, cbhotel )
{
	
	this.cbCountry = cbcountry;
	this.cbCity = cbcity;
	this.cbHotel = cbhotel;
	
	
}	

formdata.prototype.setValue = function ( ctrValue, cityValue, htlValue, promoDaysValue ) 
{
	
	this.countryValue = ( ctrValue=="")?"*":ctrValue;
	this.cityValue = ( cityValue=="" )?"*":cityValue;
	this.hotelValue = ( htlValue=="")?"*":htlValue;
	
	if ( parseInt(promoDaysValue)>0){ 	formPromoAddDays = promoDaysValue; }
	
	
}

formdata.prototype.loadComboPos = function (xformData)
{
	
	var iIndex=0;
	
	this.cbCountry = document.forms[0].elements[ this.cbCountry ];
	this.cbCity = document.forms[0].elements[ this.cbCity ];
	this.cbHotel = document.forms[0].elements[ this.cbHotel ];
	
	this.removeSubCombos ( 2 );
	
	// country
	if ( this.countryValue !="*" ) 
	{
		xformData.loadPosCountry();
	}
	
	xformData.loadPosCity();
	
	xformData.loadPosHotel();
	
}

formdata.prototype.loadPosCountry = function ()
{
	
	for (var i=0; i<this.countries.length; i++)
	{
		if ( this.countries[i].code == this.countryValue)
		{
			var iIndex = i+1;
		}

	}
	
	this.cbCountry.selectedIndex = iIndex;

}

formdata.prototype.loadPosCity = function ()
{
// position city

	var icountry=0;
	var icity=0;
	var numCity=0;
	var n=0;
	
	
	icountry = this.cbCountry.selectedIndex;
	
	for ( i=0;i<this.countries.length;i++)
	{
		if ( icountry == 0 )
		{
					
			country = this.countries[ i ];
			
			for (n=0; n<country.cities.length; n++ )
			{
				numCity++
				this.cbCity.options[numCity] = new Option ( country.cities[n].cname, country.cities[n].code, false, false);
				if ( this.cityValue == country.cities[n].code) 
				{
					icity = numCity;
				}
				
				
			}	
		} 
		else
		{
			if ( i==icountry )
			{
				
				country = this.countries[ i-1 ];
				for (n=0; n<country.cities.length; n++ )
				{
					this.cbCity.options[n+1] = new Option ( country.cities[n].cname, country.cities[n].code, false, false);
					if ( this.cityValue == country.cities[n].code) 
					{
						icity = n+1
					}
				}	
				break;
			}
			
		}
		
	}
	this.cbCity.selectedIndex = icity;
}

formdata.prototype.loadPosHotel = function ()
{
	
	var icountry=0;
	var icity=0;
	var ihotel=0;
	var i=0;
	var n=0;
	var country;
	var city;
	var numHotel=0;
	
	icountry = this.cbCountry.selectedIndex;
	icity = this.cbCity.selectedIndex
	
	if ( ( icountry == 0 ) && ( icity == 0 ) )
	{
		
		for (i=0; i<this.hotels2.length; i++)
		{
			this.cbHotel.options[i+1] = new Option ( this.hotels2[i].cname, this.hotels2[i].code, false, false);
			if ( this.hotelValue==this.hotels2[i].code){ ihotel=i+1 }
		}
	} 
	else if ( ( icountry != 0 ) && ( icity == 0 ) )
	{
	
		country = this.countries[ icountry - 1 ];
		
		for (i=0; i<country.cities.length; i++ )
		{
					
			city = country.cities [ i ];
			
			for (var n=0; n<city.hotels.length; n++ )
			{
				numHotel++;
				this.cbHotel.options[numHotel] = new Option ( city.hotels[n].cname, city.hotels[n].code, false, false);
				
				if ( this.hotelValue==city.hotels[n].code){ 
					ihotel=numHotel;
					}

			} 
			
		} 			
		
	}
	else if ( ( icountry == 0 ) && ( icity != 0 ) )
	{
	
		for ( i=0;i<this.countries.length;i++)
		{
			country = this.countries[i];
			
			for (n=0; n<country.cities.length; n++ )
			{
				if ( country.cities[n].code == this.cityValue )
				{
					icountry = i;
					icity = n;
				}
				if ( icountry!=0 ) break;
			}
			
			if ( icountry!=0)  { break; }
			
		}
		
		country = this.countries[ icountry ];
		city = country.cities [ icity ];
	
		for (i=0; i<city.hotels.length; i++ )
		{
			this.cbHotel.options[i+1] = new Option ( city.hotels[i].cname, city.hotels[i].code, false, false);
			if ( this.hotelValue==city.hotels[i].code){ ihotel=i+1 }
		}
	}
	else if ( ( icountry != 0 ) && ( icity != 0 ) )
	{

		country = this.countries[ icountry -1 ];
		city = country.cities [ icity -1 ];
	
		for (i=0; i<city.hotels.length; i++ )
		{
			this.cbHotel.options[i+1] = new Option ( city.hotels[i].cname, city.hotels[i].code, false, false);
			if ( this.hotelValue==city.hotels[i].code){ ihotel=i+1 }
		}
	}
	
	
	
	this.cbHotel.selectedIndex = ihotel;
	
}
	
formdata.prototype.loadCombos = function ( Inicialize )
{
	var i;
	var iNum=0;
	
	if ( Inicialize == '1 ' )
	{
		this.cbCountry = document.forms[0].elements[ this.cbCountry ];
		this.cbCity = document.forms[0].elements[ this.cbCity ];
		this.cbHotel = document.forms[0].elements[ this.cbHotel ];
	}
	
	this.removeSubCombos ( 2 );
	
	this.cbCountry.options[0] = new Option ( this.opTextCountry, "*", false, false);
	iNum=0;
	for (i=0; i<this.countries.length; i++)
	{
	
		this.cbCountry.options[i+1] = new Option ( this.countries[i].cname, this.countries[i].code, false, false);
		if ( this.countries[i].code==this.countryValue ) { iNum = i + 1 };
		
	}
	
	this.cbCountry.selectedIndex = iNum;
	iNum=0
	for (i=0; i<this.cities2.length; i++)
	{
		this.cbCity.options[i+1] = new Option ( this.cities2[i].cname, this.cities2[i].code, false, false);
		if ( this.cities2[i].code==this.cityValue ) { iNum = i + 1 };
	}
	
	this.cbCity.selectedIndex = iNum;
	iNum=0;
	for (i=0; i<this.hotels2.length; i++)
	{
		this.cbHotel.options[i+1] = new Option ( this.hotels2[i].cname, this.hotels2[i].code, false, false);
		if ( this.hotels2[i].code==this.hotelValue ) { iNum = i + 1 };
	}
	
	
	this.cbHotel.selectedIndex = iNum;
				
}


formdata.prototype.loadCombo = function ( icountry, icity, itype )
{
	var i;
	var country, city;
	var numCity=0;
	
	switch ( itype )
	{
		case 1:
		
			// country changed
			country = this.countries[icountry];
			for (i=0; i<country.cities.length; i++ )
			{
				this.cbCity.options[i+1] = new Option ( country.cities[i].cname, country.cities[i].code, false, false);
				
				city = country.cities [ i ];
					
				for (var ii=0; ii<city.hotels.length; ii++ )
				{
					numCity++
					this.cbHotel.options[numCity] = new Option ( city.hotels[ii].cname, city.hotels[ii].code, false, false);

				} 
				
			} 	
			
			
			
			break;
			
		case 2:
		
			// city changed
			if ( this.cbCountry.selectedIndex-1 <0 ) 
			{
				var cityValue = this.cbCity.value;
				icountry=0;
				icity=0;
				
				for ( i=0;i<this.countries.length;i++)
				{
					country = this.countries[i];
					
					for (ii=0; ii<country.cities.length; ii++ )
					{
						
						if ( parseInt(country.cities[ii].code) == parseInt(cityValue) )
						{
							icountry = i;
							icity = ii;
						}
						if ( icountry!=0 ) break;
					}
					
					if ( icountry!=0)  { break; }
					
				}
				
				country = this.countries[ icountry ];
				city = country.cities [ icity ];
			
				for (i=0; i<city.hotels.length; i++ )
				{
					this.cbHotel.options[i+1] = new Option ( city.hotels[i].cname, city.hotels[i].code, false, false);
				}
				
			}
			else
			{
				
				country = this.countries[ this.cbCountry.selectedIndex-1 ];
				city = country.cities [ icity ];
			
				for (i=0; i<city.hotels.length; i++ )
				{
					this.cbHotel.options[i+1] = new Option ( city.hotels[i].cname, city.hotels[i].code, false, false);
				}
			} 				
			break;
			
		case 3:
		
			country = this.countries[icountry];
			for (i=0; i<country.cities.length; i++ )
			{
				
				city = country.cities [ i ];
					
				for (var ii=0; ii<city.hotels.length; ii++ )
				{
					numCity++

					this.cbHotel.options[numCity] = new Option ( city.hotels[ii].cname, city.hotels[ii].code, false, false);

				} 
				
			} 	
			
			break;
		
	}
	
}

formdata.prototype.countryChange = function (xformData)
{

	var si = xformData.cbCountry.selectedIndex-1; 	

	xformData.ignorechange = true;
	
	xformData.removeSubCombos ( 2 );
	
	if ( si >=0 )
	{
		xformData.loadCombo ( si, '', 1);			
	}
	else
	{
		xformData.loadCombos(0);
	}

	xformData.ignorechange = false;
}

formdata.prototype.cityChange = function (xformData)
{
	if ( !xformData.ignorechange )
	{
		var si = xformData.cbCity.selectedIndex-1; 	
		
		if ( si >=0 ) 
		{
			xformData.removeSubCombos ( 1 );
			xformData.loadCombo ( '', si, 2);			
		}
		else
		{
			if ( ( xformData.cbCountry.selectedIndex==0 ) && ( xformData.cbCity.selectedIndex==0 ) ) 
			{
				xformData.removeSubCombos ( 2 );
				xformData.loadCombos(0);
				
			}else
			{
				if ( xformData.cbCity.selectedIndex==0 )
				{
					xformData.removeSubCombos ( 1 );
					si = xformData.cbCountry.selectedIndex - 1
					xformData.loadCombo ( si, '', 3);
				}
				
			}
			
		}
		
	}
}

function countryChange(xformData)
{
	formdata.prototype.countryChange(xformData)
	
}
function cityChange(xformData)
{
	formdata.prototype.cityChange(xformData);
}

function hotelChange()
{

}

function _onLoad()
{
		
		switch ( controlRes ) 
		{
			case 1:
			
				formDataRes.loadFromString ( ResstringLoad );
				formDataRes.loadCombos(1);
				formData.loadFromString ( stringLoad );
				formData.loadCombos(1);	
				break;
					
			case 2: 
			
				formData.loadFromString ( stringLoad );
				formData.loadComboPos(formData);
				break;
				
			default:
			
				formData.loadFromString ( stringLoad );
				formData.loadCombos(1);	
				break;
				
		}
		
}

		
function changesDates(cboDay, cboMonth, cboYear, addDays, dDays, dMonth, dYear)
		{
		
			
			var cbDays = document.form.elements[ cboDay ];
			var cbMonth = document.form.elements[ cboMonth ];
			var cbYear = document.form.elements[ cboYear ];
		
			var iYear = cbYear.options[cbYear.selectedIndex].value;
			var iMonth = cbMonth.options[cbMonth.selectedIndex].value;
			iMonth = parseInt(iMonth) - 1 ;
			var iDay = cbDays.options[cbDays.selectedIndex].value; 
			var today = new Date(iYear, iMonth, iDay);
			var myDate=new Date(iYear, iMonth, iDay)
			
			myDate.setDate(myDate.getDate() + parseInt(addDays) ); 
						
			document.form.elements[ dDays ].options[myDate.getDate()-1].selected=true;
			document.form.elements[ dMonth ].options[myDate.getMonth()].selected=true;
			
			iYear=  myDate.getYear() - today.getYear() ;
			if ( iYear == 0 ) 
			{
				iYear = cbYear.selectedIndex;
			}
			document.form.elements[ dYear ].options[iYear].selected=true;
			
			if (document.form.cal2ctrl!=undefined)
			{
				document.form.cal2ctrl.value= myDate.getDate() + '-' + parseInt(myDate.getMonth ()+1) + '-' + myDate.getFullYear();
			}
			
			
		}		
		
