// +----------------------------------------------------------------------+
// | JavaScript version 1.2                                               |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Luis Alfredo Abarca Galicia             		  |
// +----------------------------------------------------------------------+
// | Este codigo es propiedad intelectual de Luis Alfredo Abarca		  |
// | Cualquier duda o aclaracion, favor de comunicarse a:	              |
// | Cel: 04474.44.07.21.51		Email: luis_abarcag@hotmail.com           |
// +----------------------------------------------------------------------+
// | Autor: Luis Alfredo Abarca Galicia								  	  |
// +----------------------------------------------------------------------+
//
// $Id: common.js, v 1.0 18/03/2004 Exp $

	var CORE_LANG = "es";
	
	if( top.location != self.location ){ top.location = self.location; }	
	// Fechas
	var objFecha 	= new Date();
	var THIS_DAY 	= objFecha.getDate();
	var THIS_MONTH	= objFecha.getMonth();
	var THIS_YEAR 	= objFecha.getFullYear();
		
	var meses = new Array();	
	meses[0] = 31;		meses[1] = THIS_YEAR % 4 == 0 ? 29 : 28;
	meses[2] = 31;		meses[3] = 30;
	meses[4] = 31;		meses[5] = 30;
	meses[6] = 31;		meses[7] = 31;
	meses[8] = 30;		meses[9] = 31;
	meses[10] = 30;		meses[11] = 31;
	
	var meses_names = new Array("Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", 
								"Ago", "Sep", "Oct", "Nov", "Dic");
									
	if( CORE_LANG == "en" ){ 
		meses_names[0] = "Jan";
		meses_names[3] = "Apr";
		meses_names[7] = "Aug";
		meses_names[11] = "Dec";
	}
	
	var m_form;
	var m_argc;
	var m_argv;
	
	// }}}
	// {{{	Form functions
	function form_send()
	{
		var i = 0;
		m_argc = form_send.arguments.length;
		m_argv = form_send.arguments;
		
		m_form = m_argv[0];
		
		for( i = 1; i < m_argc; i++ ){
			obj = eval("m_form." + m_argv[i]);
			oName = obj.name;
			oValue = obj.value;

			if( oName == "email" || oName == "mail" || oName == "correo" ){ 
				if( oValue == "" ){ alert("Por favor escriba su e-mail"); obj.focus(); return true; }
				if( oValue.indexOf('@', 0) == -1 || oValue.indexOf('.', 0) == -1){ alert("Por favor escriba su e-mail correctamente"); obj.focus(); return; }
			}
			if( oValue == "" ){ alert("Por favor rellene los campos marcados"); obj.focus(); return true; }
		}
		
		m_form.submit();
	}
	
	// }}}
	// {{{	Form functions
	function form_check()
	{
		var i = 0;
		m_argc = form_check.arguments.length;
		m_argv = form_check.arguments;
		
		m_form = m_argv[0];
		
		for( i = 1; i < m_argc; i++ ){
			obj = eval("m_form." + m_argv[i]);
			oName = obj.name;
			oValue = obj.value;

			if( oName == "email" || oName == "mail" || oName == "correo" ){ 
				if( oValue == "" ){ alert("Por favor escriba su e-mail"); obj.focus(); return false; }
				if( oValue.indexOf('@', 0) == -1 || oValue.indexOf('.', 0) == -1){ alert("Por favor escriba su e-mail correctamente"); obj.focus(); return false; }
			}
			if( oValue == "" ){ alert("Por favor rellene los campos marcados"); obj.focus(); return false; }
		}
		return true;		
	}
	
	function eliminar(name, url, msg){
		if( msg == "" ){ msg = "Esta seguro que desea eliminar el contenido del articulo "; }		
		if( confirm(msg + name) ){ window.location = url; }
	}
	
	// }}}
	// {{{	Form functions
	function swap_visibility( name )
	{
		
		d = document;
		obj = d.getElementById(name);
		
		if( d.all ){
			if( obj.style.display == "none" ){
				obj.style.display = "block";
			}else{
				obj.style.display = "none";
			}
		}else{
			if(obj.display == "none"){
				obj.display = "block";
			}else{
				obj.display = "none";
			}
		}
	}
	
	function paginar( page ){
		var d = document;	
		var nLinks = d.links.length;

		page = parseInt(page);

		if( page < 1 || isNaN(page) || page == "undefined" ){
			page = 1;
		}
		
		for( var i = 0; i < nLinks; i++ ){
			str = new String(d.links[i]);
			buffer = str.split("&");
			
			if( buffer.length > 1 ){
				str = buffer[1];
				buffer = str.split("=");		
				str = buffer[1];
			}else{
				buffer = str.split("=");		
				str = buffer[1];
			}	
			
			if( str == page ){ 
				var l = d.getElementById(str);
				
				if( l !== null ){
					l.innerHTML = str;
					l.style.fontWeight = "bold";
				}
			}			
		}
	}
	
	// }}}
	// {{{	DOM functions
	
	function createOption( text, value, object )
	{	
		var content;
		var d = document;
		var option = d.createElement("OPTION");				
								
		content = d.createTextNode( text );
		option.appendChild( content );
		option.setAttribute("value", value);
		object.appendChild( option );
		
		return option;
	}
	
	function createOptions( inicio, fin, object )
	{
		var i;
		var lpArray = new Array();
		
		for( i=inicio; i<= fin; i++){
			lpArray[i] = createOption(i, i, object);
		}
		return lpArray;
	}
	
	function clearOptions( inicio, objArray, object )
	{	
		var len = objArray.length - 1;
				
		if(	len > 0){
			for( i = inicio; i <= len; i++ ){
				object.removeChild( objArray[i] );
			}
			
			for( i = inicio; i <= objArray.length; i++ ){
				objArray.shift();
			}
		}					
	}
	
	// }}}
	// {{{
			
	// Muestra una ventana de dialogo	
	function popUpWnd(p_url, p_eval)
	{
		var navegador = navigator.appName;
		
		l_height 	= 250;
		l_width		= 600; 	
		
		l_top 		= (screen.height - l_height)/2;
		l_left		= (screen.width - l_width)/2;

		if(p_eval){
			if(navigator.appName.substring(0,9)=="Microsoft"){
				window.showModalDialog(p_url,"","dialogWidth:"+l_width+"px;dialogHeight:"+l_height+"px;help:0;scroll:0;status:0;");
			}else{
				window.open(p_url, '', 'tollbars=0,scrollbar=0,status=0,history=0,width='+l_width+',height='+l_height+',top='+l_top+',left='+l_left);				
			}
		}else{
			window.open(p_url, '', 'tollbars=0,scrollbar=0,status=0,history=0,width='+l_width+',height='+l_height+',top='+l_top+',left='+l_left);			
		}
	} 
	
	function newWin(url, w, h)
	{		
		var left = (screen.width - w) / 2;
		var top  = (screen.height - h) / 2;
		var opts = "tollbars=0,scrollbar=0,status=0,history=0,width="+ w +",height="+ h +",top="+ top +",left="+ left;
		
		hwnd = window.open(url, '', opts);
		return hwnd;
	}
										
	// {{{
	// }}}

	/*
	 *	Calendario()
	 *	Muestra las fechas
	 *
	 *  Variables locales
	 *  @var fecha				El nuevo objeto Date					
	 *	@var horas				La hora actual
	 * 	@var minutos			El minuto actual
	 *	@var segundos			Segundos actuales	
	 *	@var meses				El mes actual del 0-11	
	 *	@var dias				El dia de la semana 0-6
	 *	@var year				El aņo actual
	 *	@var hoy				El numero de dia del mes actual
	 *
	 */
	function fecha()
	{
		var objFecha;					
		var horas;
		var minutos;
		var segundos;
		var meses;
		var dias;
		var year;
		var hoy;
		
		var n_dias = new Array(	DOMINGO, LUNES, MARTES, MIERCOLES, 
								JUEVES, VIERNES, SABADO);
									
		var n_meses = new Array( ENERO, FEBRERO, MARZO, ABRIL, MAYO, 
								 JUNIO, JULIO, AGOSTO, SEPTIEMBRE, 
								 OCTUBRE, NOVIEMBRE, DICIEMBRE);
		
		if( fecha.arguments.length > 0 ){
			var arg  = fecha.arguments;

			year	 = parseInt( arg[0].substring(0, 4) );		
			month	 = parseInt( arg[0].substring(5, 7) ) -1;
			today	 = parseInt( arg[0].substring(8, 10) );
		}else{								 									
			objFecha = new Date;		
			horas 	 = objFecha.getHours();
			minutos  = objFecha.getMinutes();
			segundos = objFecha.getSeconds();
			month 	 = objFecha.getMonth();
			day 	 = objFecha.getDay();
			year 	 = objFecha.getYear();
			today 	 = objFecha.getDate();						
		}

		if( fecha.arguments.length > 0 ){
			if( CORE_LANG == "en" ){
				return n_meses[month] + " " + today;
			}else{
				return today +" de "+n_meses[month];
			}
		}else{
			if( CORE_LANG == "en" ){
				return n_dias[day] + ", " + n_meses[month] + " " + today;
			}else{
				return n_dias[day] + ", " + today +" de "+n_meses[month];
			}
		}
	}	
	
	function Calendar()
	{
		var argc = Calendar.arguments.length;
		var argv = Calendar.arguments;		
				
		// Inicializa variables
		this.LLEGADAS 	 = 0;
		this.SALIDAS  	 = 1;
		this.m_hArrDay	 = new Array();
		this.m_hDepDay	 = new Array();
		this.m_hKids	 = new Array();
		this.m_hGuests	 = new Array();
		
		this.MAX_BOOKDAYS = 29;
						
		// Rellena variables de acuerdo a la cantidad de argumentos pasados a la funcion
		switch( argc ){
		case 0:
			day 	= THIS_DAY;
			month 	= THIS_MONTH;			
			year 	= THIS_YEAR;			
			break;
			
		default:
			day   	= argv[0];
			month 	= argv[1];
			year 	= argv[2];
			
			if( day < 1 ){
				day 	= THIS_DAY;
			}
			
			if( month < 1 ){
				month 	= THIS_MONTH;
			}
			
			if( year < 1 ){
				year 	= THIS_YEAR;
			}
			break;
		}	
		
		this.ParseDates(day, month, year);	
	}
	
	// }}}
	// {{{	

	Calendar.prototype.ParseDates = function ( days, months, years )
	{
		var arrDate 	= new Date(years, months, days);
		this.m_arrDay 	= arrDate.getDate();
		this.m_arrMonth = arrDate.getMonth();			
		this.m_arrYear 	= arrDate.getFullYear();
		
		var depDate 	= new Date(this.m_arrYear, this.m_arrMonth, parseInt(this.m_arrDay + 2));
		this.m_depDay 	= depDate.getDate();
		this.m_depMonth = depDate.getMonth();			
		this.m_depYear 	= depDate.getFullYear();			
		
		// Crea los valores para el select del mes
		
		buffer_arrMonth	= this.m_arrMonth < 10 ? "0" + parseInt(this.m_arrMonth + 1) : parseInt(this.m_arrMonth + 1);
		buffer_depMonth = this.m_depMonth < 10 ? "0" + parseInt(this.m_depMonth + 1) : parseInt(this.m_depMonth + 1);		
						
		this.m_arrMonthValue = new String(buffer_arrMonth + this.m_arrYear);
		this.m_depMonthValue = new String(buffer_depMonth + this.m_depYear);
	}	

	// }}}
	// {{{	
				
	function SetHandlers()
	{
		var argc = SetHandlers.arguments.length;
		var argv = SetHandlers.arguments;
		
		var strForm	 = argv[0];
		this.objForm = eval( strForm );
		
		if( argc == 3 ){			
			this.objArrDay 	 = eval( strForm + "." + argv[1] );
			this.objArrMonth = eval( strForm + "." + argv[2] );
		}else{
			this.objArrDay 	 = eval( strForm + "." + argv[1] );
			this.objArrMonth = eval( strForm + "." + argv[2] );
			this.objDepDay 	 = eval( strForm + "." + argv[3] );
			this.objDepMonth = eval( strForm + "." + argv[4] );		
		}
	}
	
	// }}}
	// {{{	
		
	function ShowArrDays()
	{	
		// Escribe los dias del primer mes seleccionado en el select
		if( ShowArrDays.arguments.length == 1 ){
			argv = ShowArrDays.arguments;
			this.m_hArrDay = createOptions(1, meses[argv[0]], this.objArrDay);
		}else{
			this.m_hArrDay = createOptions(1, meses[this.m_arrMonth], this.objArrDay);
		}
		
		this.objArrDay.value = this.m_arrDay;
	}
	
	// }}}
	// {{{		

	function ShowDepDays()
	{	
		// Escribe los dias del primer mes seleccionado en el select
		if( ShowDepDays.arguments.length == 1 ){
			argv = ShowDepDays.arguments;
			this.m_hDepDay = createOptions(1, meses[argv[0]], this.objDepDay);
		}else{
			this.m_hDepDay = createOptions(1, meses[this.m_depMonth], this.objDepDay);
		}
		
		this.objDepDay.value = this.m_depDay;
	}
	
	// }}}
	// {{{	
	
	Calendar.prototype.ShowArrMonth = function ()
	{
		// Escribe los dias del primer mes seleccionado en el select
		this.ShowMonths( this.objArrMonth );
		this.objArrMonth.value = this.m_arrMonthValue;
	}
	
	// }}}
	// {{{	

	Calendar.prototype.ShowDepMonth = function ()
	{		
		// Escribe los dias del primer mes seleccionado en el select
		this.ShowMonths( this.objDepMonth );
		this.objDepMonth.value = this.m_depMonthValue;
	}

	// }}}
	// {{{	

	Calendar.prototype.ShowArrDates = function ()
	{		
		this.ShowArrDays();
		this.ShowArrMonth();
	}

	// }}}
	// {{{	

	Calendar.prototype.ShowDepDates = function ()
	{		
		this.ShowDepDays();
		this.ShowDepMonth();
	}
		
	// }}}
	// {{{
	
	Calendar.prototype.ShowMonths = function ( objMonths )
	{
		var text 	= "";
		var value	= "";
		var nMes	= 0;
		
		// Escribe los meses
		for( mes = this.m_arrMonth; mes <= 11; mes++ ){
			text  = meses_names[mes] + ", " + this.m_arrYear;
			nMes  = parseInt(mes + 1);
			value = nMes < 10 ? "0" + nMes : nMes;
			value = value + "" + this.m_arrYear;
			createOption(text, value, objMonths);
		}		
	}
	
	// }}}
	// {{{
			
	/*
	 *	Realiza la busqueda
	 *
	 */
	Calendar.prototype.DoCheck = function ( form )
	{		
		var	arrMonth = new String(this.objArrMonth.value);	
		var depMonth = new String(this.objDepMonth.value);				

		var	arrDay  = parseInt(this.objArrDay.value);
		var arrMes  = parseInt( arrMonth.substring(0, 2) );
		var arrYear = parseInt( arrMonth.substring(2, 6) );
		var	depDay  = parseInt(this.objDepDay.value);					
		var depMes  = parseInt( depMonth.substring(0, 2) );						
		var depYear = parseInt( depMonth.substring(2, 6) );
		
		var MAX_DAYS = this.MAX_BOOKDAYS;
		
		/*
		 *	Chequeo de meses de entrada y salida
		 *	Verifica que sea el mismo aņo y el mismo mes.
		 *	Se verifica que el mes de salida se igual o mayor al de entrada, siempre y cuando sea el mismo aņo
		 */
		if( depYear == THIS_YEAR ){
			if( depMonth < arrMonth ){ alert(ERR_INVALID_DATES); return true; }
			
			if( arrMes == depMes ){
				// Si el dia de salida es menor al de entrada en el mismo mes
				if( depDay <= arrDay ){ alert(ERR_INVALID_DATES); return true; }
				// Si reserva mas de un mes
				if( (depDay - arrDay) >= MAX_DAYS ){ alert(ERR_DATES_LONG); return true; }
			}
			
			// Si selecciona 2 meses o mas						
			if( (depMes - arrMes) >= 2 ){ alert(ERR_DATES_LONG); return true; }
		}else{
			if( arrMes < parseInt(mes+1) ){ alert(ERR_INVALID_DATES); return true; }
		}
		
		if( arrDay < 10 ){
			arrDay = "0" + arrDay;
		}
		
		if( depDay < 10 ){
			depDay = "0" + depDay;
		}
	
		
		if( arrMes < 10 ){
			arrMes = "0" + arrMes;
		}
		
		if( depMes < 10 ){
			depMes = "0" + depMes;
		}		
		
		this.objForm.qRyAd.value = arrDay;
		this.objForm.qRyAm.value = arrMes;
		this.objForm.qRyAy.value = arrYear;
		this.objForm.qRyDd.value = depDay;
		this.objForm.qRyDm.value = depMes;
		this.objForm.qRyDy.value = depYear;		
						
		this.objForm.submit();
	}

	// }}}
	// {{{
	
	Calendar.prototype.change_salidas = function()
	{
		change_month(this.SALIDAS, this);
	}	

	// }}}
	// {{{
		
	Calendar.prototype.change_llegadas = function()
	{
		change_month(this.LLEGADAS, this);
	}
	
	// }}}
	// {{{
		
	Calendar.prototype.SetCupo = function(total, guests)
	{
		this.m_maxRoom   = total;
		this.m_maxGuests = guests;
		this.m_maxKids   = 2;		
	}	

	// }}}
	// {{{
	
	Calendar.prototype.ShowKids = function( value, objKids )
	{
		this.m_hKids = createOptions(0, value, objKids); 
	}

	// }}}
	// {{{
	
	Calendar.prototype.ShowKids2 = function( value, objKids )
	{
		var cuantos = this.m_maxRoom - value;
		var show = true;

		if( this.m_hKids.length >= 0 ){
			clearOptions( 0, this.m_hKids, objKids);
		}
		
		// Si es solo un adulto o 4 no permite los niņos
		if( 1 == value || this.m_maxRoom == value){ 
			show = false;
			this.m_hKids = createOptions(0, 0, objKids);
		}
		
		// Muestra las opciones
		if( show == true ){
			this.m_hKids = createOptions(0, cuantos, objKids); 
		}
	}

	// }}}
	// {{{

	Calendar.prototype.ShowGuests = function( objGuests )
	{
			this.m_hGuests = createOptions(1, this.m_maxGuests, objGuests); 
	}
		
	// }}}
	// {{{
	
	// Public Methods	
	Calendar.prototype.SetHandlers 		= SetHandlers;
	Calendar.prototype.ShowArrDays 	 	= ShowArrDays;
	Calendar.prototype.ShowDepDays 		= ShowDepDays;	

	// Public Properties				
	Calendar.prototype.Author 		= "Luis Alfredo Abarca Galicia";
	Calendar.prototype.Version 		= "1.0";

	// }}}
	// {{{
	
	function change_month( tipo, lpObject )
	{
		var objDays  = tipo == lpObject.LLEGADAS ? lpObject.objArrDay : lpObject.objDepDay;
		var objMonth = tipo == lpObject.LLEGADAS ? lpObject.objArrMonth : lpObject.objDepMonth;
		var newMonth = tipo == lpObject.LLEGADAS ? lpObject.objArrMonth.value : lpObject.objDepMonth.value;		
		var mes 	 = parseInt( newMonth.substring(0, 2) - 1);
		var value 	 = objDays.value;

		// Borra los dias del mes que estaba
		clearOptions( 1, tipo == lpObject.LLEGADAS ? lpObject.m_hArrDay : lpObject.m_hDepDay, objDays);

		// Escribe los dias del mes seleccionado
		if( tipo == lpObject.LLEGADAS ){
			lpObject.ShowArrDays( mes );
		}else{
			lpObject.ShowDepDays( mes );
		}
				
		// Si sobrepasa el ultimo dia del mes establece el ultimo dia, si no selecciona
		// el dia que se encontraba seleccionado, Ejm: Enero 31 pasa el 29 o 28 de Febrero
		objDays.value = value > meses[mes] ? meses[mes] : value;
	}		