// actualizada: 07-03-06
// JavaScript Document
	
	/* LAYERS **************************************************************/
	// Detercar browser
	var ie4 = (document.all) ? true : false;
	var ns4 = (document.layers) ? true : false;
	var ns6 = (document.getElementById && !document.all) ? true : false;
	
	function objPosX(obj){
		var curleft = 0;
		if (obj.offsetParent){
			while (obj.offsetParent){
				curleft += obj.offsetLeft;
				obj = obj.offsetParent;
			}
		}
		else if (obj.x){
			curleft += obj.x;
		}
		return curleft;
	}
		
	function objPosY(obj){
		var curtop = 0;
		if (obj.offsetParent){
			while (obj.offsetParent){
				curtop += obj.offsetTop;
				obj = obj.offsetParent;
			}
		}
		else if (obj.y){
			curtop += obj.y;
		}
		return curtop;
	}
	
	// Regresa layer
	function idRegresar(lay){
		var regresar;
		if (ie4) {regresar=document.all[lay]}
		if (ns4) {regresar=document.layers[lay]}
		if (ns6) {regresar=document.getElementById([lay])}
		return regresar;
	}
	
	function mover_y(lay, pixel){
		idRegresar(lay).style.left=pixel+'px';
	}
	
	function mover_x(lay, pixel){
		idRegresar(lay).style.top=pixel+'px';
	}
	
	// Ocultar layer
	function idOcultar(lay) {
		if (ie4) {document.all[lay].style.visibility = "hidden";}
		if (ns4) {document.layers[lay].visibility = "hide";}
		if (ns6) {document.getElementById([lay]).style.display = "none";}
	}
	
	// Ver layer
	function idVer(lay) {
		if (ie4) {document.all[lay].style.visibility = "visible";}
		if (ns4) {document.layers[lay].visibility = "show";}
		if (ns6) {document.getElementById([lay]).style.display = "block";}
	}
	
	// Escribir layer
	function idEscribir(lay,txt) {
		if (ie4) {
			document.all[lay].innerHTML = txt;
		}
		if (ns4) {
			document[lay].document.write(txt);
			document[lay].document.close();
		}
		if (ns6) {
			over = document.getElementById([lay]);
			range = document.createRange();
			range.setStartBefore(over);
			domfrag = range.createContextualFragment(txt);
			while (over.hasChildNodes()) {
				over.removeChild(over.lastChild);
			}
			over.appendChild(domfrag);
		}
	}
	/* LAYERS **************************************************************/
	
	
	function documento_visible_ancho(){
		var myWidth = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
		} 
		else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
		} 
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
		}
		
		return myWidth;
	}
	
	function documento_visible_alto(){
		var myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myHeight = window.innerHeight;
		} 
		else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myHeight = document.documentElement.clientHeight;
		} 
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myHeight = document.body.clientHeight;
		}
		
		return myHeight;
	}
	
	
	
function EsEmail(email){
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
		return true;
	}
	else {
		return false;
	}
}
function EsNumero(numstr){
	if (/^\d+$/.test(numstr)){
		return true;
	}
	else {
		return false;
	}
}
function EsTelefono(numTel){
	if (/^([0-9 \-\(\)#\*])*$/.test(numTel)){
		return true;
	}
	else {
		return false;
	}
}
function ValidarUser(user){
	if (/^([a-zA-Z0-9_]|[^ áéíóúÁÉÍÓÚàèìòùäëïöüÄËÏÖÜñÑ`´¨\|\+\-¿¡°¬!"'\\#\$%&\/\(\)=\?¡@;,:\.<>\*\[\]])*$/.test(user)){
		return false;
	}
	else {
		return true;
	}
}
function ValidarLogin(form){
	if(form.user.value==""){
		alert("ALERTA:\nPor favor escribe tu nombre de usuario.");
		form.user.focus();
		return false;
	}
	else if(ValidarUser(form.user.value)){
		alert("ERORR:\nEl nombre de usuario se encuentra mal escrito.\nPor favor escribelo correctamente.");
		form.user.focus();
		form.user.select();
		return false;
	}
	else if(form.pass.value==""){
		alert("ALERTA:\nPor favor escribe tu contraseña.");
		form.pass.focus();
		return false;
	}
	else{
		return true;
	}
}
function EnviarLogin(form, button){
	button.disabled = true;
	if(ValidarLogin(form)){
		form.submit();
	}
	else{
		button.disabled = false;
	}
}
function ValidarBusqueda(form){
	if(form.buscar.value==''){
		alert("ALERTA:\nPor favor escribe lo que estas buscando.");
		form.buscar.focus();
		return false;
	}
	else{
		return true;	
	}
}
function EnviarBusqueda(form, button){
	button.disabled = true;
	if(ValidarBusqueda(form)){
		form.submit();
	}
	else{
		button.disabled = false;
	}
}



/*** CALENDARIO *************************************************************************/
function obtenerHora() {
	var now = new Date();
	var hour = now.getHours();
	var minute = now.getMinutes();
	now = null;
	var ampm = "";

	if (hour >= 12) {
		hour -= 12;
		ampm = "PM";
	} 
	else{
		ampm = "AM";
		hour = (hour == 0) ? 12 : hour;
	}

	if (minute < 10){
		minute = "0" + minute; // do not parse this number!
	}

	return hour + ":" + minute + " " + ampm;
}

function leapYear(year) {
	if (year % 4 == 0){ // basic rule
		return true; // is leap year
	}
	else {
		return false; // is not leap year
	}
}

function getDays(month, year) {
	var ar = new Array(12);
	ar[0] = 31; // January
	ar[1] = (leapYear(year)) ? 29 : 28; // February
	ar[2] = 31; // March
	ar[3] = 30; // April
	ar[4] = 31; // May
	ar[5] = 30; // June
	ar[6] = 31; // July
	ar[7] = 31; // August
	ar[8] = 30; // September
	ar[9] = 31; // October
	ar[10] = 30; // November
	ar[11] = 31; // December

	return ar[month];
}

function getMonthName(month) {
	var ar = new Array(12);
	ar[0] = "Enero";
	ar[1] = "Febrero";
	ar[2] = "Marzo";
	ar[3] = "Abril";
	ar[4] = "Mayo";
	ar[5] = "Junio";
	ar[6] = "Julio";
	ar[7] = "Agosto";
	ar[8] = "Septiembre";
	ar[9] = "Octubre";
	ar[10] = "Noviembre";
	ar[11] = "Diciembre";

	return ar[month];
}

/*
function setCal() {
	var now = new Date();
	var year = now.getYear();
	if (year < 1000){
		year+=1900;
	}
	var month = now.getMonth();
	var monthName = getMonthName(month);
	var date = now.getDate();
	now = null;
	
	var firstDayInstance = new Date(year, month, 1);
	var firstDay = firstDayInstance.getDay();
	firstDayInstance = null;
	
	var days = getDays(month, year);
	
	calendario=drawCal(firstDay + 1, days, date, monthName, year, month);
	idEscribir('layer_celendario', calendario);
}
*/

function drawCal(firstDay, lastDate, date, monthName, year, mes, day_function, layer_name) {
	var text = ""; // initialize accumulative variable to empty string
	
	//dia anteriror
	if(mes==0){
		year_pre=(year-1);
		mes_pre=11;
	}
	else {
		year_pre=year;
		mes_pre=mes-1;
	}
	
	// dia siguiente
	if(mes==11){
		year_nex=year+1;
		mes_nex=0;
	}
	else{
		year_nex=year;
		mes_nex=mes+1
	}

	text += '	<table border="1" cellpadding="0" cellspacing="1" id="calendario_tabla">';
	text += '	  <tr>';
	text += '		<td colspan="7" align="center" valign="middle">';
	text += '		  <table width="100%" border="0" cellpadding="0" cellspacing="2" id="calendario_cabecera">';
	text += '			<tr>';
	text += '			  <td align="left" valign="middle"><a href="javascript:cal_print_month('+year_pre+','+mes_pre+',' +"'"+day_function+"'"+', ' +"'"+layer_name+"'"+' );" title="Mes anterior">&nbsp;&lt;&lt;</a></td>';
	text += '			  <td align="center" valign="middle" id="cal_titulo">'+monthName + ' ' + year+'</td>';
	text += '			  <td align="right" valign="middle"><a href="javascript:cal_print_month('+year_nex+','+mes_nex+',' +"'"+day_function+"'"+', ' +"'"+layer_name+"'"+' );" title="Mes siguiente">&gt;&gt;&nbsp;</a></td>';
	text += '			</tr>';
	text += '		  </table>';
	text += '		</td>';
	text += '	  </tr>';
	
	var weekDay = new Array(7);
	weekDay[0] = "Lun";
	weekDay[1] = "Mar";
	weekDay[2] = "Mie";
	weekDay[3] = "Jue";
	weekDay[4] = "Vie";
	weekDay[5] = "Sab";
	weekDay[6] = "Dom";

	text += '<tr valign="center" align="middle" id="calendario_dias_semana">';
	for (var dayNum = 0; dayNum < 7; ++dayNum) {
		text += '<td align="center">' + weekDay[dayNum] + '</td>';
	}
	text += '</tr>';
	
	var digit = 1;
	var curCell; // = 1; // cuando empiece la semana en domingo, el valor tiene que ser 1
	
	// Fix, repara que empice en lunes
	if((firstDay-1)==0){
		curCell=1;
		firstDay=7;
	}
	else{
		curCell=2;
	}

	for (var row = 0; row <= Math.ceil((lastDate + (firstDay - 1)) / 7); ++row) {
		text += '<tr valign="top" align="right" id="calendario_dias_mes">';
		for (var col = 1; col <= 7; ++col) {
			if (digit > (lastDate)){
				break
			}
			if(curCell < firstDay){
				text += '<td>&nbsp;</td>';
				curCell++;
			}
			else {
				/************** Cuando sea el dia igual al que se marca ****************/
				if (digit == date) { // current cell represent today's date

					text += '<td><a href="javascript:'+day_function+'('+year+','+(mes)+','+digit+');" title="'+digit+'/'+monthName+'/'+year+'" class="dia_hoy">'+digit+'</a></td>';
				}
				else{
					text += '<td><a href="javascript:'+day_function+'('+year+','+(mes)+','+digit+');" title="'+digit+'/'+monthName+'/'+year+'">'+digit+'</a></td>';
				}
				digit++;
			}
		}
		text += '</tr>';
	}
	
	var fecha_hoy = new Date();
	fecha_hoy_year=fecha_hoy.getYear();
	if (fecha_hoy_year < 1000){
			fecha_hoy_year+=1900;
	}
	fecha_hoy_month=fecha_hoy.getMonth();
	fecha_hoy=null;
	
	if(year==fecha_hoy_year && mes==fecha_hoy_month){
	}
	else{
		text += '<tr>';
		text += '<td colspan="7" align="center" valign="middle" id="cal_dia_hoy">';
		text += '<a href="javascript:cal_print_month('+fecha_hoy_year+','+fecha_hoy_month+', '+"'"+day_function+"'"+', ' +"'"+layer_name+"'"+');" title="Regresar a la fecha de hoy">Ir al dia de hoy</a>';
		text += '</td>';
		text += '</tr>';
	}

	text += '</table>';

	//document.write(text); 
	return text;
}
function cal_print_month(year, month, day_function, layer_name){
	var fecha_hoy = new Date();
	
	fecha_hoy_year=fecha_hoy.getYear();
	if (fecha_hoy_year < 1000){
			fecha_hoy_year+=1900;
	}
	
	if(year==fecha_hoy_year && month==fecha_hoy.getMonth()){
		var now = new Date();
		var year = now.getYear();
		if (year < 1000){
			year+=1900;
		}
		var month = now.getMonth();
		var monthName = getMonthName(month);
		var date = now.getDate();
		now = null;
		
		var firstDayInstance = new Date(year, month, 1);
		var firstDay = firstDayInstance.getDay();
		firstDayInstance = null;
		
		var days = getDays(month, year);
		
		calendario=drawCal(firstDay + 1, days, date, monthName, year, month, day_function, layer_name);
		idEscribir(layer_name, calendario);
	}
	else{
		var fecha_nueva = new Date(year, month, 1);
		var primerDia = fecha_nueva.getDay();
		var mes_num = fecha_nueva.getMonth();
		var mes_nom = getMonthName(mes_num);
		var num_dias = getDays(mes_num, year);
		fecha_nueva = null;
		
		calendario=drawCal(primerDia + 1, num_dias, 0, mes_nom, year, mes_num, day_function, layer_name);
		idEscribir(layer_name, calendario);
	}
}
/*** CALENDARIO *************************************************************************/
