// JavaScript Document

function hideCombos_k(){
   //a=document.getElementsByTagName('select')
	document.getElementById('nivel_a').style.visibility='hidden';
	document.getElementById('curso_a').style.visibility='hidden';
	document.getElementById('division_a').style.visibility='hidden';
	document.getElementById('tipo_doc').style.visibility='hidden';
	document.getElementById('dia_nac').style.visibility='hidden';
	document.getElementById('mes_nac').style.visibility='hidden';
	document.getElementById('anio_nac').style.visibility='hidden';
	document.getElementById('dia_ini').style.visibility='hidden';
	document.getElementById('mes_ini').style.visibility='hidden';
	document.getElementById('anio_ini').style.visibility='hidden';
	document.getElementById('sexo').style.visibility='hidden';
   /*for(var i=0; i < a.length; i++){
   a[i].style.visibility='hidden';
   }*/
}

function changeCmb(nameObj,strFile,param){
   var msgReturn='';
	var dateVar = new Date();
	//cargamos una varible con las variables que vamos a enviar
	strNC = dateVar.getYear().toString() + dateVar.getMonth().toString() + dateVar.getDay().toString() + dateVar.getHours().toString() + dateVar.getMinutes().toString() + dateVar.getSeconds().toString() + aleatorio(1,100).toString();
	_values_send=param+"&funcion=pt"+strNC;
	_URL_= strFile;
	//document.write(_URL_+"?"+_values_send);
	_objetus.open("GET",_URL_+"?"+_values_send,true);
	//una vez enviado los valores inmediatamente llamamos a la propiedad
	//onreadystatechange
	_objetus.onreadystatechange=function() {
		  //dentro de la funcion comprobamos el estado del proceso
		  //si es 4 (terminado) pedimos lo que nos han mandado
		 if (_objetus.readyState==4){
			 //usamos la propiedad responseText para recibir en una cadena lo que nos mandaron
			 if (_objetus.status==200){
			    msgReturn=_objetus.responseText;
				 changeTextSpan(nameObj,msgReturn);
			 }else{
			    msgReturn="Hubo un error: "+_objetus.responseText;
				 changeTextSpan(nameObj,msgReturn);
		    }
			//alert(msgReturn);
			 return msgReturn;
		 }
	}
	_objetus.send(null);
}

function changeCmb2(nameObj,strFile,param){
   var msgReturn='';
	var dateVar = new Date();
	//cargamos una varible con las variables que vamos a enviar
	strNC = dateVar.getYear().toString() + dateVar.getMonth().toString() + dateVar.getDay().toString() + dateVar.getHours().toString() + dateVar.getMinutes().toString() + dateVar.getSeconds().toString() + aleatorio(1,100).toString();
	_values_send=param+"&funcion=pt"+strNC;
	_URL_= strFile;
	//document.write(_URL_+"?"+_values_send);
	objetus_k.open("GET",_URL_+"?"+_values_send,true);
	//una vez enviado los valores inmediatamente llamamos a la propiedad
	//onreadystatechange
	objetus_k.onreadystatechange=function() {
		  //dentro de la funcion comprobamos el estado del proceso
		  //si es 4 (terminado) pedimos lo que nos han mandado
		 if (objetus_k.readyState==4){
			 //usamos la propiedad responseText para recibir en una cadena lo que nos mandaron
			 if (objetus_k.status==200){
			    msgReturn=objetus_k.responseText;
				 changeTextSpan(nameObj,msgReturn);
			 }else{
			    msgReturn="Hubo un error: "+objetus_k.responseText;
				 changeTextSpan(nameObj,msgReturn);
		    }
			 //alert(msgReturn);
			 return msgReturn;
		 }
	}
	objetus_k.send(null);
}

function saveObject(strFile,param,bloq){
   var msgReturn='';
	var dateVar = new Date();
	if (bloq){
		showBloqueador();
	}
	showObject('divloading');
	//cargamos una varible con las variables que vamos a enviar
	strNC = dateVar.getYear().toString() + dateVar.getMonth().toString() + dateVar.getDay().toString() + dateVar.getHours().toString() + dateVar.getMinutes().toString() + dateVar.getSeconds().toString() + aleatorio(1,100).toString();
	_values_send=param+"&funcion=pt"+strNC;
	_URL_= strFile;
	//objGeneral=nameObj;
	objRepObj=bloq;
	//document.write(_URL_+"?"+_values_send);
	_objetus.open("GET",_URL_+"?"+_values_send,true);
	//una vez enviado los valores inmediatamente llamamos a la propiedad
	//onreadystatechange
	_objetus.onreadystatechange=function() {
		  //dentro de la funcion comprobamos el estado del proceso
		  //si es 4 (terminado) pedimos lo que nos han mandado
		 if (_objetus.readyState==4){
			 //usamos la propiedad responseText para recibir en una cadena lo que nos mandaron
			 if (_objetus.status==200){
			    msgReturn=_objetus.responseText;

				 hideObjectSinDestroy('divloading');
			    //changeTextSpan(objGeneral,msgReturn);
				 showAlert(msgReturn);
			 }else{
			    //msgReturn="No se pudo cargar el objeto "+nameObj+" - Cod. 554557"+_objetus.responseText;
				 msgReturn=_objetus.responseText;
				 alert(msgReturn);
 				 hideObjectSinDestroy('divloading');
 				 hideBloqueador("bloqueador");
		    }
			 return msgReturn;
		 }
	}
	_objetus.send(null);
}

function loadObject(nameObj,strFile,param,bloq){
   var msgReturn='';
   var msgerr="";
	var dateVar = new Date();
	
	objRepObj=bloq;
	if (objRepObj){
		showBloqueador();
	}
	showObject('divloading');
	//cargamos una varible con las variables que vamos a enviar
	strNC = dateVar.getYear().toString() + dateVar.getMonth().toString() + dateVar.getDay().toString() + dateVar.getHours().toString() + dateVar.getMinutes().toString() + dateVar.getSeconds().toString() + aleatorio(1,100).toString();
	_values_send=param+"&funcion=pt"+strNC;
	_URL_= strFile;
	objGeneral=nameObj;
	//document.write(_URL_+"?"+_values_send);
	//_objetus.open("GET",_URL_+"?"+_values_send,true);
	
	//_values_send=translateCharMolestos(_values_send);
	//alert(_values_send);
	
	//_objetus.open("POST",_URL_,true);
 		_objetus.open("POST",_URL_,true);
		_objetus.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	_objetus.setRequestHeader("Content-length", _values_send.length);
      	_objetus.setRequestHeader("Connection", "close");
	
	//una vez enviado los valores inmediatamente llamamos a la propiedad
	//onreadystatechange
	_objetus.onreadystatechange=function() {
	  
		  //dentro de la funcion comprobamos el estado del proceso
		  //si es 4 (terminado) pedimos lo que nos han mandado
		 if (_objetus.readyState==4){
			 //usamos la propiedad responseText para recibir en una cadena lo que nos mandaron
			 if (_objetus.status==200){
			    msgReturn=_objetus.responseText;
				 var arrDatos=msgReturn.split("/* Codigo javascript a ejecutar con eval */");
				 msgReturn=arrDatos[0];
				 if (arrDatos.length>1){
					 executeCode(arrDatos[1]);
				 }
				 hideObjectSinDestroy('divloading');
				 if (objRepObj){// Cuando hay bloqueador, centro y muestro el objeto
				    setDataObject(objGeneral,msgReturn);
				 }else{ // No hay bloqueador, solo cambio el innerhtml
				    changeTextSpan(objGeneral,msgReturn);
				 }
				 if (arrDatos.length>2){
					 executeCode(arrDatos[2]);
				 }
			 }else{
			     msgReturn="No se pudo cargar el objeto "+nameObj+" - Cod. 554557"+_objetus.responseText;
				 //msgerr="No se pudo cargar el objeto "+nameObj+" - Cod. 554557";
				 alert("No se pudo realizar la operacion con exito");
 				 hideObjectSinDestroy('divloading');
				 if (objRepObj){
 				    hideBloqueador("bloqueador");
				 }
		    }
			 return msgReturn;
		 }
	}
	//_objetus.send(null);
	_objetus.send(_values_send);
}

function changecolor(id, color) { 
	objTemp = document.getElementById(id);
	if (objTemp!=null){
		objTemp.style.background = color; 
	}
}

function isUnsignedInteger(s) {
	return (s.toString().search(/^[0-9]+$/) == 0);
}

function translateCharMolestos(texto){
   texto = texto.replace(/&/g,"*am*");
   texto = texto.replace(/=/g,"*eq*");
   texto = texto.replace(/\+/g,"*pl*");
   
   texto = texto.replace(/á/g,"*ace_a*");
   texto = texto.replace(/é/g,"*ace_e*");
   texto = texto.replace(/í/g,"*ace_i*");
   texto = texto.replace(/ó/g,"*ace_o*");
   texto = texto.replace(/ú/g,"*ace_u*");
   
   texto = texto.replace(/ñ/,"*ace_n*");
   
   return texto;
}


function htmlentities( s ){
    // Convert all applicable characters to HTML entities
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_htmlentities/
    // +       version: 804.1712
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: htmlentities('Kevin & van Zonneveld');
    // *     returns 1: 'Kevin &amp; van Zonneveld'

    var div = document.createElement('div');
    var text = document.createTextNode(s);
    div.appendChild(text);
    return div.innerHTML;
}

function html_entity_decode( string ) {
    // Convert all HTML entities to their applicable characters
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_html_entity_decode/
    // +       version: 804.1712
    // +   original by: john (http://www.jd-tech.net)
    // +      input by: ger
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: html_entity_decode('Kevin &amp; van Zonneveld');
    // *     returns 1: 'Kevin & van Zonneveld'

    var ret, tarea = document.createElement('textarea');
    tarea.innerHTML = string;
    ret = tarea.value;
    return ret;
}

function setAlturaBlocker(){
	objTemp=document.getElementById("bodycontent");
	objTemp2=document.getElementById("bloqueador");
	if ((objTemp2!=null) && (objTemp!=null)) {
		var altob=altoBrowser();
		if (altob>objTemp.offsetHeight){
			objTemp2.style.height=altob;
		}else{
			objTemp2.style.height=objTemp.offsetHeight;
		}
	}
}

function executeCode(strCode){
	eval(strCode);
}

function setDataObject(nombreObj,strDiv){
	objTemp=document.getElementById(nombreObj);
	if (objTemp!=null){
		objTemp.innerHTML=strDiv;
		showObject(nombreObj);
	}
}

function showCombos(){
	a=document.getElementsByTagName('select')
	for(var i=0; i < a.length; i++){
		a[i].style.visibility='visible';
	}
/* ESTO ES SOLO PARA IMM -- RESTO DE SITIOS BORRAR !!! */
	objTemp=document.getElementById('cuadronuevomsg');
	if (objTemp!=null){
		objTemp2=document.getElementById('newmsgtipouser');
		if (objTemp2!=null){
			objTemp2.style.visibility=objTemp.style.visibility;
		}
		objTemp2=document.getElementById('nivel');
		if (objTemp2!=null){
			objTemp2.style.visibility=objTemp.style.visibility;
		}
		objTemp2=document.getElementById('cursos');
		if (objTemp2!=null){
			objTemp2.style.visibility=objTemp.style.visibility;
		}
		objTemp2=document.getElementById('divisiones');
		if (objTemp2!=null){
			objTemp2.style.visibility=objTemp.style.visibility;
		}
	}
}

function executePage(strFile,param){
   var msgReturn='';
	var dateVar = new Date();

   try {
      _objetusEP = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
       try {
		    _objetusEP= new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
		    _objetusEP= false;
      }
   }
   if (!_objetusEP && typeof XMLHttpRequest!='undefined') {
	   _objetusEP = new XMLHttpRequest();
   }

	//cargamos una varible con las variables que vamos a enviar
	strNC = dateVar.getYear().toString() + dateVar.getMonth().toString() + dateVar.getDay().toString() + dateVar.getHours().toString() + dateVar.getMinutes().toString() + dateVar.getSeconds().toString() + aleatorio(1,100).toString();
	_values_send=param+"&funcion=pt"+strNC;
	_URL_= strFile;
	//document.write(_URL_+"?"+_values_send);
	_objetusEP.open("GET",_URL_+"?"+_values_send,true);
	//una vez enviado los valores inmediatamente llamamos a la propiedad
	//onreadystatechange
	_objetusEP.onreadystatechange=function() {
		  //dentro de la funcion comprobamos el estado del proceso
		  //si es 4 (terminado) pedimos lo que nos han mandado
		 if (_objetusEP.readyState==4){
			 //usamos la propiedad responseText para recibir en una cadena lo que nos mandaron
			 if (_objetusEP.status==200){
				 msgReturn=_objetusEP.responseText;
				 //alert(msgReturn);
				 executeCode(msgReturn);
			 }else{
				 msgReturn="No se pudo cargar el objeto - Cod. 554557"+_objetusEP.responseText;
			 }
			 //alert(msgReturn);
			 return msgReturn;
		 }
	}
	_objetusEP.send(null);
}

function hideCombos(){
	a=document.getElementsByTagName('select');
	for(var i=0; i < a.length; i++){
		a[i].style.visibility='hidden';
	}
}

function changeTextSpan(spName,msgText){
	objC=document.getElementById(spName);
	if (objC!=null){
		objC.innerHTML=msgText;
	}
}

function hideBloqueador(nombdiv){
	objC=document.getElementById(nombdiv);
	if (objC!=null){
		objC.style.visibility='hidden';
	}
	showCombos();
}

function showAlert(msgAlert){
	objC=document.getElementById("divalert");
	if (objC!=null){
		hideCombos();
		objTemp=document.getElementById("sp_alert");
		if (objTemp!=null){
			objTemp.innerHTML=msgAlert;
		}
		centrarObj("divalert");
		/*topSc=giveMyScrollTop();
		altoB=altoBrowser();
		anchoB=anchoBrowser();
		winl = (anchoB-objC.offsetWidth)/2;
		wint = ((altoB-objC.offsetHeight)/2)+topSc; // 423 -> Alto del modulo  ---  116 -> alto de barras del explorer
		objC.style.top=wint;
		objC.style.left=winl;*/
		objC.style.visibility='visible';
		
		//setTimeout('hideAlert()',5000); 
	}else{
		alert(msgAlert);
	}
}

function addElement(divfather,divson,textodiv) {
	var ni = document.getElementById(divfather);
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id',divson);
	newdiv.innerHTML = textodiv;
	ni.appendChild(newdiv);
}

function removeElement(divson) {
//	var d = document.getElementById(divfather);
//	if (d!=null){
		var olddiv = document.getElementById(divson);
		if (olddiv!=null){
			var padre = olddiv.parentNode;
			padre.removeChild(olddiv);
			//d.removeChild(olddiv);
		}
//	}
}

function hideSingleObject(nombreObj,overwrite){
	objC=document.getElementById(nombreObj);
	if (objC!=null){
		objC.style.visibility='hidden';
		if (overwrite){
			objC.innerHTML="&nbsp;";
		}
	}
}

function changeVisibilityObject(nombreObj){
	objC=document.getElementById(nombreObj);
	if (objC!=null){
		if (objC.style.visibility=='hidden'){
			objC.style.visibility='visible';
		}else{
			objC.style.visibility='hidden';
		}
	}
}

function hideObjectSinDestroy(nombreObj){
	objC=document.getElementById(nombreObj);
	if (objC!=null){
		objC.style.visibility='hidden';
	}
}

function showObjectSinDestroy(nombreObj){
	objC=document.getElementById(nombreObj);
	if (objC!=null){
		objC.style.visibility='visible';
	}
}

function hideObject(nombreObj){
	objC=document.getElementById("bloqueador");
	if (objC!=null){
		objC.style.visibility='hidden';
	}
	objC=document.getElementById(nombreObj);
	if (objC!=null){
		objC.style.visibility='hidden';
		objC.innerHTML="&nbsp;";
	}
	showCombos();
}

function showObject(nombreObj){
	centrarObj(nombreObj);
	objC=document.getElementById(nombreObj);
	if (objC!=null){
		objC.style.visibility='visible';
	}
}

function showBloqueador(){
	hideCombos();
	setAlturaBlocker();
	objC=document.getElementById("bloqueador");
	if (objC!=null){
		objC.style.visibility='visible';
	}
}

function showObjectBloq(nombreObj){
	showBloqueador();
	centrarObj(nombreObj);
	objC=document.getElementById(nombreObj);
	if (objC!=null){
		objC.style.visibility='visible';
	}
}

function giveMyScrollTop(){
	topSc=(document.all)?(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop):window.pageYOffset;
	return topSc;
}

function centrarObj(nombreObj){
	objC=document.getElementById(nombreObj);
	topSc=giveMyScrollTop();
	if (objC!=null){
		altoB=altoBrowserVisible();
		anchoB=anchoBrowser();
		alturaObj=objC.offsetHeight;
		winl = (anchoB-objC.offsetWidth)/2;
		wint = ((altoB-(alturaObj))/2)+topSc; // 423 -> Alto del modulo  ---  116 -> alto de barras del explorer
		if (wint<0){ wint=0; }
		objC.style.top=wint;
		objC.style.left=winl;
	}
}

function hideAlert(){
	if (ocultarBloq){
		hideBloqueador("bloqueador");
		ocultarBloq=false;
	}
	objTemp=document.getElementById("divalert");
	if (objTemp!=null){
		objTemp.style.visibility='hidden';
	}
	showCombos();
}

function anchoBrowser() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function altoBrowser(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.documentElement.scrollHeight;//+document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );     
	return yWithScroll;//arrayPageSizeWithScroll; 
}

/* LA FUNCION DE ARRIBA DETERMINA EL ALTO DE LA PAGINA CON SCROLL, ESTA SOLAMENTE LA PARTE VISIBLE */
function altoBrowserVisible() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function isDate(dateStr) {// dd/mm/yyyy
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) {
		//alert("Please enter your birth date as dd/mm/yyyy. Your current selection reads: " + dateStr);
		return false;
	}

	day = matchArray[1]; // p@rse date into variables
	month = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) { // check month range
		//alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31) {
		//alert("Day must be between 1 and 31.");
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		//alert("Month "+month+" doesn`t have 31 days!");
		return false;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			//alert("February " + year + " doesn`t have " + day + " days!");
			return false;
		}
	}
	return true; // date is valid
}

function objetus() {
   try {
      objetus = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
       try {
		    objetus= new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
		    objetus= false;
      }
   }
   if (!objetus && typeof XMLHttpRequest!='undefined') {
	   objetus = new XMLHttpRequest();
   }
	return objetus;
}

function objetus_k() {
   try {
      objetus_k = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
       try {
		    objetus_k= new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
		    objetus_k= false;
      }
   }
   if (!objetus_k && typeof XMLHttpRequest!='undefined') {
	   objetus_k = new XMLHttpRequest();
   }
	return objetus_k;
}

function aleatorio(inferior,superior){ 
    numPosibilidades = superior - inferior;
    var aleat = Math.random() * numPosibilidades ;
    aleat = Math.round(aleat);
    return parseInt(inferior) + aleat;
}

//This prototype is provided by the Mozilla foundation and//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
if (!Array.prototype.indexOf){
	Array.prototype.indexOf = function(elt /*, from*/)  {
		var len = this.length;
		var from = Number(arguments[1]) || 0;
		from = (from < 0)
			? Math.ceil(from)
			: Math.floor(from);
		if (from < 0)
			from += len;
		for (; from < len; from++){
			if (from in this &&
				 this[from] === elt)
			return from;
		}
		return -1;
	};
} // End if de si exsite la funcion indexOf

var ocultarBloq=false;
var objGeneral='';
var objRepObj=''; // Determino si tiene que mostrar o ocultar el bloqueador
// Aca comienza la acción!
var _objetus=objetus();
var objetus_k=objetus_k();
