var haz_toogle=false;
window.addEvent('domready',function(){
	h = new Date(); 
	calendario(h.getFullYear(), h.getMonth(), h.getDate());
	$$('input[type=text]').addEvents({
    'focus': function(){
			this.addClass('over');
    },
    'blur': function(){
			this.removeClass('over');
    }
	});		
	$$('input[type=password]').addEvents({
    'focus': function(){
			this.addClass('over');
    },
    'blur': function(){
			this.removeClass('over');
    }
	});		
	
	if($('txt_user_cli')!=null){		
		$('txt_user_cli').addEvents({
			'focus': function(){
				if (this.value==this.title) this.value='';
			},
			'blur': function(){
				if (this.value=='') this.value=this.title;
			}
		});		
	}
	
	if ($('txt_pass_cli') != null) {
  	$('txt_pass_cli').addEvents({
  		'focus': function(){
  			if (this.value == this.title) {
  				if (Browser.Engine.trident!=true) this.type = 'password'
  				this.value = '';
  			}
  		},
  		'blur': function(){
  			if (this.value == '') {
  				this.type = 'text'
  				this.value = this.title;
  			}
  		}
  	});
		if (Browser.Engine.trident!=true) $('txt_pass_cli').set('type', 'text');
  }	
	
/*																		
	MM_preloadImages('img/fondo-menu.gif','img/telefono.gif','img/bot_ayuda_over.gif','img/fondo-pie.gif', 'img/pastilla-telefono.gif','img/moto-carreras.gif','img/hotel-logo-over.gif', 'img/yamaha-logo-over.gif','img/tecnico-logo-over.gif')																		
*/		
/*
	var sld_muestra_login = new Fx.Slide('div_muestra_login', {
		onComplete: function(){
			if (haz_toogle) {
				sld_formulario_login.toggle();
				haz_toogle=false;
			}
		}
	});
	var sld_formulario_login = new Fx.Slide('div_contenido_login', {
		onComplete: function(){
			if (haz_toogle) {
				sld_muestra_login.toggle();
				haz_toogle=false;
			}
		}
	});
	sld_formulario_login.hide();
*/


	$('a_muestra_login').addEvents({
		'click': function(ev) {
			new Event(ev).stop();
			haz_toogle=true;
//			sld_muestra_login.toggle();
			$('div_contenedor_muestra_login').tween('bottom', '-50px');
			$('div_contenedor_login').tween('bottom', '0px');

			$('txt_user').focus();
    },			
    'mouseover': function(){
			MM_swapImage('img_muestra_login','','img/mostrar-over.gif',1);
    },
    'mouseout': function(){
			MM_swapImgRestore();
    }
	});					
	$('a_oculta_login').addEvents({
		'click': function(ev) {
			new Event(ev).stop();
			haz_toogle=true;
//			sld_formulario_login.toggle();
			$('div_contenedor_muestra_login').tween('bottom', '0');
			$('div_contenedor_login').tween('bottom', '-50px');
    },			
    'mouseover': function(){
			MM_swapImage('img_oculta_login','','img/ocultar-over.gif',1);
    },
    'mouseout': function(){
			MM_swapImgRestore();
    }
	});					

	$('a_envia_login').addEvent('click', function(e) {
		/* Prevent the submit event */
		new Event(e).stop();

		if (ValidarCampo($('form_login').txt_user, 2, true, 'El campo usuario es obligatorio.', '')==false) return false;
		if (ValidarCampo($('form_login').txt_pass, 2, true, 'El campo contrasea es obligatorio.', '')==false) return false;
		var log = $('div_enviar_login').addClass('ajax-loading');
		
		var sender = new Request();
		sender.onSuccess=function(responseText, responseXML){
//			if (responseText=="") window.open(ruta_absoluta+'cms/index.php')
			if (responseText=="") self.parent.location='cms/index.php';
			else alert(responseText);
	//		log.set('html', responseText); 
			log.removeClass('ajax-loading');
/*
			var myHTMLRequest = new Request.HTML().get('formulario_empleo.php');
			myHTMLRequest.onSuccess=function(responseTree, responseElements, responseHTML, responseJavaScript){$('div_form_empleo').innerHTML=responseHTML;};
*/			
		};
		sender.send({data: $('form_login'), url: $('form_login').get('action')});
	});
	
	if($('a_envia_login_cli')!=null){			
		$('a_envia_login_cli').addEvent('click', function(e) {
			/* Prevent the submit event */
			new Event(e).stop();
	
			if (ValidarCampo($('form_login_cli').txt_user_cli, 2, true, 'El campo usuario es obligatorio.', '')==false) return false;
			if ($('form_login_cli').txt_pass_cli != null) {
	  	if (ValidarCampo($('form_login_cli').txt_pass_cli, 2, true, 'El campo contraseña es obligatorio.', '') == false) 
	  		return false;
	  	}
			var log = $('div_enviar_login_cli').addClass('ajax-loading');
			
			var sender = new Request();
			sender.onSuccess=function(responseText, responseXML){
	//			if (responseText=="") window.open(ruta_absoluta+'cms/index.php')
				if (responseText=="") self.parent.location=self.parent.location; //'cms/index.php';
				else alert(responseText);
		//		log.set('html', responseText); 
				log.removeClass('ajax-loading');
	/*
				var myHTMLRequest = new Request.HTML().get('formulario_empleo.php');
				myHTMLRequest.onSuccess=function(responseTree, responseElements, responseHTML, responseJavaScript){$('div_form_empleo').innerHTML=responseHTML;};
	*/			
			};
			sender.send({data: $('form_login_cli'), url: $('form_login_cli').get('action')});
		});	
	}
	
//	if($('a_salir_cli')!=null){
//		$('a_salir_cli').addEvent('click', function(e) {
//			/* Prevent the submit event */
/*			new Event(e).stop();
	
			var log = $('div_enviar_login_cli_on').addClass('ajax-loading');
			
			var sender = new Request();
			sender.onSuccess=function(responseText, responseXML){
				if (responseText=="") self.parent.location=self.parent.location; //'cms/index.php';
				else alert(responseText);
				log.removeClass('ajax-loading');
			};
			sender.send({url: 'logout.php'});
		});		
	}
	*/
})

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null) 	{document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];} 
}

// Array de los meses
var monthNames = new makeArray(12);
monthNames[0] = "Enero";
monthNames[1] = "Febrero";
monthNames[2] = "Marzo";
monthNames[3] = "Abril";
monthNames[4] = "Mayo";
monthNames[5] = "Junio";
monthNames[6] = "Julio";
monthNames[7] = "Agosto";
monthNames[8] = "Septiembre";
monthNames[9] = "Octubre";
monthNames[10] = "Noviembre";
monthNames[11] = "Diciembre";

// Array de los das

var dayNames = new makeArray(7);
dayNames[0] = "Domingo";
dayNames[1] = "Lunes";
dayNames[2] = "Martes";
dayNames[3] = "Mircoles";
dayNames[4] = "Jueves";
dayNames[5] = "Viernes";
dayNames[6] = "Sbado";

var now = new Date();
var year = now.getYear();

if (year < 2000) year = year + 1900;

function makeArray(len) {
	for (var i = 0; i < len; i++) this[i] = null;
	this.length = len;
}

var arr_eventos = new Array();
var c_dia=0;
var c_mes=1;
var c_anio=2;
var c_vinculo=3;
var c_circuito=4;

function calendario(anio,mes,hoy){
	var ajax = ajaxobj();
//	ajax.open("GET", "eventos.xml", true);
	ajax.open("GET", "eventos.xml.php?mes="+mes+"&anio="+anio, true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {

			if (ajax.responseXML.documentElement==null) {
				//para IE
				xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async="false";
				xmlDoc.loadXML(ajax.responseText);
				var datos = xmlDoc;
			} else {
				//para los navegadores normales y decentes
				var datos = ajax.responseXML.documentElement;
			}
			eventos = datos.getElementsByTagName("evento");

			for (i=0;i<eventos.length;i++) {
				var arr_aux=new Array(5);
				arr_aux[c_dia] = eventos[i].getElementsByTagName("dia").item(0).firstChild.data;
				arr_aux[c_mes] = eventos[i].getElementsByTagName("mes").item(0).firstChild.data;
				arr_aux[c_anio] = anio;
				arr_aux[c_vinculo] = eventos[i].getElementsByTagName("vinculo").item(0).firstChild.data;
				arr_aux[c_circuito] = eventos[i].getElementsByTagName("circuito").item(0).firstChild.data;
				arr_eventos[i]=arr_aux;
			}
			carga_calendario(anio,mes,hoy);
		}
	}
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(null);
}
function carga_calendario(anio,mes,hoy){
	anio= parseInt(anio);
	mes = parseInt(mes);
	hoy = parseInt(hoy);
	
	fecha_actual = new Date(); 
	mes_actual=fecha_actual.getMonth();
	anio_actual=fecha_actual.getFullYear();
	
	var arr_eventos_mes = new Array();
	for(var i=0; i<arr_eventos.length; i++) {
		if(arr_eventos[i][c_mes]==mes+1 && arr_eventos[i][c_anio]==anio) arr_eventos_mes.push(arr_eventos[i]);
  }

	p = new Date(anio,mes,1);
	diaSemanaPrimeroMes = p.getDay();
	var bisiesto = false;
	if (anio % 4 == 0 && anio % 100 != 0) bisiesto = true;
	if (anio % 100 == 0 && anio % 400 == 0) bisiesto = true;
	
	var diasMes = [];
	diasMes[0] = 31;
	(bisiesto)? diasMes[1]= 29:diasMes[1]= 28;
	diasMes[2]= 31;
	diasMes[3]= 30;
	diasMes[4]= 31;
	diasMes[5]= 30;
	diasMes[6]= 31;
	diasMes[7]= 31;
	diasMes[8]= 30;
	diasMes[9]= 31;
	diasMes[10]= 30;
	diasMes[11]= 31;

	var nombreMes = [];
	nombreMes[0] = "Enero";
	nombreMes[1] = "Febrero";
	nombreMes[2] = "Marzo";
	nombreMes[3] = "Abril";
	nombreMes[4] = "Mayo";
	nombreMes[5] = "Junio";
	nombreMes[6] = "Julio";
	nombreMes[7] = "Agosto";
	nombreMes[8] = "Septiembre";
	nombreMes[9] = "Octubre";
	nombreMes[10] = "Noviembre";
	nombreMes[11] = "Diciembre";

	finmes = diasMes[mes];
	
	switch (diaSemanaPrimeroMes){
		case 0:
			diaSemanaPrimeroMes = 6;
			break;
		default:
			diaSemanaPrimeroMes = diaSemanaPrimeroMes -1;
			break;
	}

	var textoForm = "";
	textoForm += '<form>';
	textoForm += '<select name="cbo_mes" id="cbo_mes" onchange="calendario(this.form.cbo_anio.value,this.form.cbo_mes.value,'+hoy+')">';
	for (i=0;i<12;i++) {
		textoForm += '<option value="'+i+'"'+ (i==mes ? ' selected="selected"' : '')+'>'+nombreMes[i]+'</option>';
	}
	textoForm += '</select>&nbsp;&nbsp;';
	textoForm += '<select name="cbo_anio" id="cbo_anio" onchange="calendario(this.form.cbo_anio.value,this.form.cbo_mes.value,'+hoy+')">';
	for (i=1;i>-1;i--) {
		textoForm += '<option value="'+ (anio_actual+i)+'"'+ (anio_actual+i==anio ? ' selected="selected"' : '')+'>'+ (anio_actual+i)+'<\/option>';
	}
	textoForm += '<\/select>';
	textoForm += '</form>';

	var texto = "";
//	texto += '<table cellspacing="3" cellpadding="0">';
	texto += '<table>';
//	texto += '	<tr align="center" valign="middle" class="calendario-dias">';
	texto += '	<tr class="calendario-dias">';
	texto += '		<td>L</td>';
	texto += '		<td>M</td>';
	texto += '		<td>X</td>';
	texto += '		<td>J</td>';
	texto += '		<td>V</td>';
	texto += '		<td>S</td>';
	texto += '		<td>D</td>';
	texto += '	</tr>';
//	texto += '	<tr align="center" valign="middle">';
	texto += '	<tr>';

	z = 1;
	j = 1;
	for (i = 0;i<42;i++) {
		var hayEvento=false
		for(var ind_eventos=0; ind_eventos<arr_eventos_mes.length; ind_eventos++) {
			if (z==arr_eventos_mes[ind_eventos][c_dia]) {
				hayEvento=true
				break;
			}
		}
		var esSabado = j == 6 || j == 13 || j == 20 || j == 27 || j == 34;
		var esDomingo = j == 7 || j == 14 || j == 21 || j == 28 || j == 35;
		if (hayEvento)	{
			if ( i < diaSemanaPrimeroMes ){
				if (esSabado) texto += '<td class="calendario-sabado"></td>';
				else if (esDomingo) texto += '<td class="calendario-domingo"></td>';
				else texto += '<td class="calendario-numeros"></td>';
			} else {
				texto += '<td><a href="'+arr_eventos_mes[ind_eventos][c_vinculo]+'" class="calendario-link tip_tucmsweb">'+z+'<span>'+arr_eventos_mes[ind_eventos][c_circuito]+'</span></a></td>';
				z++;
			}
		} else {
			if ( i < diaSemanaPrimeroMes ){
				if (esSabado) texto += '<td class="calendario-sabado"></td>';
				else if (esDomingo) texto += '<td class="calendario-domingo"></td>';
				else texto += '<td class="calendario-numeros"></td>';
			}
			if ( i == diaSemanaPrimeroMes ){
				
				if ( z == hoy && mes==mes_actual){
					texto += '<td class="calendario-hoy">'+z+'</td>';
				} else{
					if (esSabado) texto += '<td class="calendario-sabado">'+z+'</td>';
					else if (esDomingo) texto += '<td class="calendario-domingo">'+z+'</td>';
					else texto += '<td class="calendario-numeros">'+z+'</td>';
				}
				z++;
			}
			if ( i > diaSemanaPrimeroMes ){
				if (z <= finmes){
					if ( z == hoy && mes==mes_actual){
						texto += '<td class="calendario-hoy">'+z+'</td>';
					} else {
						if (esSabado) texto += '<td class="calendario-sabado">'+z+'</td>';
						else if (esDomingo) texto += '<td class="calendario-domingo">'+z+'</td>';
						else texto += '<td class="calendario-numeros">'+z+'</td>';
					}
				} else texto += '<td></td>';
				z++;
			}
		}
//		if (esDomingo) texto += '</tr><tr align="center" valign="middle">';
		if (esDomingo) texto += '</tr><tr>';
		j++;
	}
	texto += "</tr></table>";
	if ($("div_cal_form_dcha")!=null) $("div_cal_form_dcha").innerHTML = textoForm;
	if ($("div_cal_dcha")!=null) $("div_cal_dcha").innerHTML = texto;
//	document.getElementById("div_cal_form_dcha").innerHTML = textoForm;
//	document.getElementById("div_cal_dcha").innerHTML = texto;
}
function ajaxobj() {
	try {
		_ajaxobj = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			_ajaxobj = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			_ajaxobj = false;
		}
	}
   
	if (!_ajaxobj && typeof XMLHttpRequest!='undefined') {
		_ajaxobj = new XMLHttpRequest();
	}
	
	return _ajaxobj;
}
