

trivago_dealform = new function()
{
	//
	//
	//

	this.Init = function()
	{
		if (typeof currentOnload == 'undefined')
		{
			var currentOnload = Array();
			if (window.onload)
			{
				currentOnload.push(window.onload);
			}
		}
		currentOnload.push(trivago_dealform.DisplayDealform);
		
		window.onload = function() { while(doOnload = currentOnload.pop()) { doOnload(); } };
	}
	
	//
	//
	//
	
	this.IsValidDealform = function()
	{
		var container   = document.getElementById('trivago_dealform'),
			target_link = 'http://www.trivago.es',
			link;

		return ((container) && ((link = container.getElementsByTagName('a')[0]) && link.href.substring(0, target_link.length) == target_link)) ? true : false;
	}
	
	//
	//
	//
	
	this.DisplayDealform = function()
	{
		if(!trivago_dealform.IsValidDealform())
		{
			alert(decodeURI('invalid_certificate'));
			return false;
		}
		
		trivago_dealform.ClearDealform();
		var node_li;
		var container = document.getElementById('trivago_dealform');
		var node_form = document.createElement('form');
		var node_ul   = document.createElement('ul');
		
		node_form.action = 'http://www.trivago.es/external_rpc.php';
		node_form.target = '_blank';
		node_form.method = 'get';
		node_form.setAttribute('accept-charset', 'UTF-8');

		trivago_dealform.Elements.AppendHiddenFields(node_form);

		if(!strip_css)
		{
			node_ul.style['listStyleType'] 		= 'none';
			node_ul.style['padding']       		= '0px';
			node_ul.style['margin']        		= '0px';

			container.style['width']           	= '230px';
			container.style['border']          	= '2px solid #6AAACD';
			container.style['backgroundColor'] 	= '#C8DCE6';
			container.style['padding']         	= '10px';
			container.style['fontFamily']      	= 'Arial';
			container.style['fontSize']        	= '12px';
		}

		container.appendChild(node_form);
		node_form.appendChild(node_ul);
		
		node_li = document.createElement('li');
		node_ul.appendChild(node_li);
		trivago_dealform.Elements.AppendTextField(node_li);
		
		node_li = document.createElement('li');
		node_ul.appendChild(node_li);
		trivago_dealform.Elements.AppendDate(node_li, true);
		
		node_li = document.createElement('li');
		node_ul.appendChild(node_li);
		trivago_dealform.Elements.AppendDate(node_li, false);
		
		node_li = document.createElement('li');
		node_ul.appendChild(node_li);
		trivago_dealform.Elements.AppendRoomType(node_li);
		
		node_li = document.createElement('li');
		node_ul.appendChild(node_li);
		trivago_dealform.Elements.AppendButtonSubmit(node_li);
	}
	
	//
	//
	//
	
	this.ClearDealform = function()
	{
		var container = document.getElementById('trivago_dealform'),
			node;
		
		while(node = container.firstChild) { container.removeChild(node); }
	}
	
	//
	//
	//
	
	this.Elements = new function()
	{
		//
		//
		//
		
		this.AppendTextField = function(parent_node)
		{
			var node = document.createElement('input');
			var default_value = decodeURI('Introduce%20una%20ciudad%20o%20regi%C3%B3n');			
			if(!strip_css)
			{
				node.style['width']        = '230px';
				node.style['marginBottom'] = '5px';
			}
			
			node.className = 'querystring';
			node.name      = 'q';
			node.value     = default_value;
			
			node.onfocus = function(){
					if(node.value == default_value)
					{
						node.value = '';
					}
			}
			
			node.onblur = function(){
					if(node.value == '')
					{
						node.value = default_value;
					}
			}

			parent_node.appendChild(node);
		}
		
		//
		//
		//
		
		this.AppendDate = function(parent_node, mode_from_date)
		{
			var node_strong          = document.createElement('strong'),
				node_day             = document.createElement('select'),
				node_month           = document.createElement('select'),
				month_names          = new Array("Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dic"),
				max_displayed_months = 18,
				year                 = 2010,
				month                = 02-1,
				month_selected_index = 0;
			
			//fill day-field
			for(var i=1; i<=31; i++)
			{
				node_day.options[i-1] = new Option(i, i);
			}
			node_day.selectedIndex = (mode_from_date ? 14 : 15) - 1;
			
			
			//fill month-field
			for(var i=0; i<max_displayed_months; i++)
			{
				node_month.options[i] = new Option(month_names[month]+ ' '+year, (month+1) + '.'+year);

				if(((month+1) + '.'+year) == (mode_from_date ? '02.2010' : '02.2010'))
				{
					month_selected_index = i;
				}
				
				month++;
				if(month == 12)
				{
					month = 0;
					++year;
				}
			}
			node_month.selectedIndex = month_selected_index;
	
	
			node_strong.innerHTML = mode_from_date ? decodeURI('Llegada') : decodeURI('Salida');

			
			if(!strip_css)
			{
				node_month.style['marginBottom'] = node_day.style['marginLeft'] = node_month.style['marginLeft'] = node_day.style['marginBottom'] = '5px';
			}
		
			node_day.className   = 'day';
			node_month.className = 'month';
			
			node_day.name		 = (mode_from_date ? 'from' : 'to') + '_day';
			node_month.name      = (mode_from_date ? 'from' : 'to') + '_month_year';

			parent_node.appendChild(node_strong);
			parent_node.appendChild(node_day);
			parent_node.appendChild(node_month);
		}
		
		//
		//
		//
		
		this.AppendRoomType = function(parent_node)
		{
			var node = document.createElement('select');
			
			node.options[0] = new Option(decodeURI('Habitaci%C3%B3n%20indiv.'), 1);
			node.options[1] = new Option(decodeURI('Habitaci%C3%B3n%20doble'), 7);
			
			if(!strip_css)
			{
				node.style['width']        = '230px';
				node.style['marginBottom'] = '5px';
			}

			node.className     = 'room_type';
			node.name		   = 'room_type';
			node.selectedIndex = 1;
				
			parent_node.appendChild(node);
		}
		
		//
		//
		//
		
		this.AppendButtonSubmit = function(parent_node)
		{
			var node = document.createElement('input');
			
			node.type      = 'submit';
			node.value     = decodeURI('Comparar%20precios%20de%20hoteles');
			node.className = 'button';
			
			if(!strip_css)
			{
				node.style['width'] = '230px';
			}
			
			parent_node.appendChild(node);
		}
		
		//
		//
		//
		
		this.AppendHiddenFields = function(parent_node)
		{
			var node;
			
			node = document.createElement('input');
			node.type  = 'hidden';
			node.name  = 'action';
			node.value = 'dealform_redirect';
			parent_node.appendChild(node);
			
			node = document.createElement('input');
			node.type  = 'hidden';
			node.name  = 'timestamp';
			node.value = Math.round(new Date()/1000);
			parent_node.appendChild(node);
		}

	}
	
	
	var strip_css = 1;
	
}


trivago_dealform.Init();

