// Metodus: Internet Productivo, Usabilidad y Desarrollo Web http://www.metodus.com
// License: http://www.opensource.org/licenses/mit-license.php

Metodus = function() {};

Metodus.select = function(elmt) {	
   var villaID = elmt.value; // ID de la Villa

   // Nombre de la Villa
   var villaName = document.getElementById('artTitle-' + villaID).value;

   // Categoria de la Villa: 2 o 3 habitaciones
   var villaCategory = document.getElementById('artCategory-' + villaID).value;
   if (villaCategory == "two-bedroom") {
     villaCategory = "Two Bedroom Villa, Up to 4 people";
   }
   else {
     villaCategory = "Three Bedroom Villa, Up to 6 people";
   }

   // Obtener el div donde se incluiran las villas
   var divSelVillas = document.getElementById('selected_villas');

   // Crear un elemento P donde se mostrara la info
   var newVilla = document.createElement("p");
   newVilla.setAttribute('id', 'pSelVilla' + villaID);
   newVilla.innerHTML = '<strong>' + villaName + '</strong> ' + villaCategory + '<a href="javascript:Metodus.remove(' + villaID + ')">Remove</a>';

   // Agregar la info al Div
   divSelVillas.appendChild(newVilla); 

   var villaHidden = document.createElement("input");
   divSelVillas.appendChild(villaHidden); 
   villaHidden.setAttribute('type', 'hidden');
   villaHidden.setAttribute('id', 'iSelVilla'+villaID);
   villaHidden.setAttribute('name', 'iSelVilla'+villaID);
   villaHidden.setAttribute('value', villaName + ", " + villaCategory);
};

Metodus.remove = function(elmt) {
   // Obtener el div donde se encuentran las villas
   var divSelVillas = document.getElementById('selected_villas');
   pVilla = document.getElementById('pSelVilla' + elmt);
   divSelVillas.removeChild(pVilla);

   iVilla = document.getElementById('iSelVilla' + elmt);
   divSelVillas.removeChild(iVilla);
};


Metodus.parse = Class.create();
Metodus.parse.prototype = {
	initialize: function() {
		str = document.URL;
		x = str.indexOf("?");

		if (x == -1) return; // nothing to parse
		
		x++;
		y = str.length;
		p = str.substring(x, y);

		x = p.indexOf("=");
		a = p.substring(0, x);

		x++;
		y = p.length;
		b = p.substring(x, y);

		el = $(a);
		if (el != null) { 
			el.value = unescape(b);
			el.onchange();
		}
	}
}