// JavaScript Document

function load_ventana(id, tema, titulo, arriba, derecha, ancho, alto, cerrar, url){
	win = new Window(id, {className: tema, title: titulo, 
								  top:arriba, right:derecha, width:ancho, height:alto, 
								  closable: cerrar, url: url, showEffectOptions: {duration:1}})
	win.show();
}

function enviar_form_mail(formulario){
	parametros=Form.serialize(formulario);
	//alert(parametros);
	new Ajax.Updater('div_temporal', 'envia_fcontacto.php?'+parametros, {asynchronous:true, evalScripts:true});
}
function enviar_form_cotizacion(formulario){
	parametros=Form.serialize(formulario);
	//alert(parametros);
	new Ajax.Updater('div_temporal', 'envia_fcotizar.php?'+parametros, {asynchronous:true, evalScripts:true});
}
function on_form_comentar(objeto){
	new Ajax.Updater('div_contenedor', 'form_comentar.php?objeto='+objeto, {asynchronous:true, evalScripts:true});
}
function cancelar_comentario(){
	$('div_contenedor').innerHTML="";
}
function enviar_comentario(){
	parametros=Form.serialize("frm_comentario");
	new Ajax.Updater('div_contenedor', 'enviar_comentarios.php?'+parametros, {asynchronous:true, evalScripts:true});
}
function ver_comentarios(objeto){
	new Ajax.Updater('div_contenedor', 'ver_comentarios.php?objeto='+objeto, {asynchronous:true, evalScripts:true});
}
function on_calendario(nombre, fdia){
	this.nombre=nombre;
	this.estilo="calendario";
	this.oneDate = new Date();
	this.hoy_anyo=-1;
	this.hoy_mes=-1;
	this.hoy_dia=-1;
	this.anyo=-1;
	this.mes=-1;
	this.dia=-1;
	this.anyos = [1960, 2015];
	this.capa="";
	this.fdia=fdia;
	
	this.on_meses = function(meses){
		for(i=0;i<meses.length;i++){
			document.getElementById(this.nombre+"_oncalmeses").options[i] = new Option(meses[i],parseInt(i));
		}
	},
	
	this.on_anyos = function(anyos){
		for(i=this.anyos[0];i<=this.anyos[1];i++){
			document.getElementById(this.nombre+"_oncalanyos").options[parseInt(i-this.anyos[0])] = new Option(i,i);
			
		}
	},

	this.on_cambiames = function(signo){
		if(signo=='+'){
			if(this.mes==11){
				this.anyo++;
				this.mes=0;
			}else{
				this.mes++;
			}
		}else{
			if(this.mes==0){
				this.anyo--;
				this.mes=11;
			}else{
				this.mes--;
			}
		}
		this.generar();
	},
	
	this.cuerpotabla = function(){
	
		var dias_es = ['Dom','Lun','Mar','Mie','Jue','Vie','Sab','Dom'];
		var dias_cor = ['D','L','M','M','J','V','S','D'];
	
		if(this.fdia=="esp")
			this.fdia=dias_es;
		else if(this.fdia=="cor")
			this.fdia=dias_cor;
		else
			this.fdia=dias_es;
			
		
		document.getElementById("div_"+this.nombre).innerHTML="<table border='0' cellpadding='0' cellspacing='0' class='"+this.estilo+"' id='tbl_"+this.nombre+"' ><thead><tr><th height='10' colspan='7'><table width='100%' border='0' cellpadding='0' cellspacing='0' class='calendarioh'><tr><th width='13%'><input name='bt_ant' type='button' id='bt_ant' value='&lt;' onclick='"+this.nombre+".on_cambiames(\"-\");' /></th><th width='50%'><select name='"+this.nombre+"_oncalmeses' id='"+this.nombre+"_oncalmeses' onchange='"+this.nombre+".mes=this.value;"+this.nombre+".generar();'></select></th><th width='50%'><select name='"+this.nombre+"_oncalanyos' id='"+this.nombre+"_oncalanyos' onchange='"+this.nombre+".anyo=this.value;"+this.nombre+".generar();'></select></th><th width='13%'><input name='bt_sig' type='button' id='bt_sig' value='&gt;' onclick='"+this.nombre+".on_cambiames(\"+\");' /></th></tr></table></th></tr><tr><th bgcolor='#FFFFFF'>"+this.fdia[0]+"</th><th bgcolor='#FFFFFF'>"+this.fdia[1]+"</th><th bgcolor='#FFFFFF'>"+this.fdia[2]+"</th><th bgcolor='#FFFFFF'>"+this.fdia[3]+"</th><th bgcolor='#FFFFFF'>"+this.fdia[4]+"</th><th bgcolor='#FFFFFF'>"+this.fdia[5]+"</th><th bgcolor='#FFFFFF'>"+this.fdia[6]+"</th></tr></thead><tbody></tbody><tfoot><tr><th colspan='7' bgcolor='#FFFFFF'><div id='txt_"+this.nombre+"'></div></th></tr></tfoot></table>";
	},
	
	this.generar = function(){
		this.cuerpotabla();
		this.oneDate = new Date();
		var meses = ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'];	
		var diasmeses = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
		
		this.hoy_anyo = this.oneDate.getYear();
		this.hoy_mes = this.oneDate.getMonth();
		this.hoy_dia = this.oneDate.getDate();
		this.hoy_anyo += (this.hoy_anyo < 1000) ? 1900 : 0;
				
		document.getElementById("txt_"+this.nombre).innerHTML="Hoy es: "+ ((this.hoy_dia<10)?"0"+this.hoy_dia:this.hoy_dia) +"/"+ ((this.hoy_mes<10)?"0"+parseInt(this.hoy_mes+1):parseInt(this.hoy_mes+1)) +"/"+this.hoy_anyo;
	
		this.on_meses(meses);
		this.on_anyos();
		
		//Cambia el mes y aņo cuando es seleccionado por el usuario
		if(this.mes>-1)
			this.oneDate.setMonth(this.mes);
		if(this.anyo>-1)
			this.oneDate.setYear(this.anyo);
		
		//Establece el dia a 1				
		this.oneDate.setDate(1);

		this.anyo = this.oneDate.getYear();
		this.mes = this.oneDate.getMonth();
		this.dia = this.oneDate.getDate();
	
		//Calcula aņo bisiesto
		if (((this.anyo % 4 == 0) && (this.anyo % 100 != 0)) || (this.anyo % 400 == 0))
			diasmeses[1] = 29; 
			
		var diasemana = this.oneDate.getDay();
		this.oneDate.setDate(diasmeses[this.mes]);
		var ultimodia = this.oneDate.getDay();
		
		//Ajusta el aņo para motores gecko
		this.anyo += (this.anyo < 1000) ? 1900 : 0;
		
		//Estable el valor de mes y aņo para las listas
		document.getElementById(this.nombre+"_oncalanyos").value=this.anyo;
		document.getElementById(this.nombre+"_oncalmeses").value=parseInt(this.mes);
		
		tabla_calendario = document.getElementById("tbl_"+this.nombre);
		tbl_body = tabla_calendario.tBodies[0];
		
		i=0; //apartir de que fila
		while (i<tbl_body.rows.length) {
			 tbl_body.deleteRow(i);
		}
		
		var numerodias=parseInt(diasmeses[this.mes]+diasemana+6-ultimodia);
		var a=(0-diasemana);
		
		for(i=0;i<(numerodias/7);i++){
		var newRow = tbl_body.insertRow(i);
			for(d=0;d<7;d++){
				newCell = newRow.insertCell(d);
				a++;
				if(a<1){
					newCell.className='dias_fuera';
					if(this.mes==0){
						midia = (diasmeses[11]+a);
						mimes = "12";
						mianyo = (this.anyo-1);
					}
					else{
						midia = (diasmeses[this.mes-1]+a);
						mimes = this.mes;
						mianyo = this.anyo;
					}
				}
				else if(a>diasmeses[this.mes]){
					newCell.className='dias_fuera';
					if(this.mes==11){
						midia = (a-diasmeses[this.mes]);
						mimes = "01";
						mianyo = parseInt(this.anyo+1);
					}
					else{
						midia = (a-diasmeses[this.mes]);
						mimes = parseInt(this.mes+2);
						mianyo = this.anyo;
					}
				}
				else{
					midia = a;
					mimes = parseInt(this.mes+1);
					mianyo = this.anyo;
				}
				
				if(parseInt(midia)<10)
					midia="0"+midia;
				if(parseInt(mimes)<10)
					mimes="0"+mimes;
				
				newCell.innerHTML="<a href=#>"+midia+"</a>";
				newCell.innerHTML=newCell.innerHTML+"<div id="+midia+"/"+mimes+"/"+mianyo+"></div>";
			}
		}
	}
}