function AgregarDocumentOnKeyPressHandler() {
    if (document.addEventListener) { 
        document.addEventListener('keypress', HandlerOnKeyPress, false); 
    } else if (document.attachEvent) { 
        document.attachEvent('onkeypress', HandlerOnKeyPress); 
    } 
}
function HandlerOnKeyPress(e){
    if (sIdBotonAceptar){
        PresionarBotonAceptar(e,sIdBotonAceptar);
    }
    if (sIdBotonCancelar){
        PresionarBotonCancelar(e,sIdBotonCancelar);
    }
}
function PresionarBotonAceptar(oEvento,sIdElemento){
	var sScript = 'document.getElementById("' + sIdElemento + '").onclick()';	
	EjecutarScriptOnPressButton(oEvento,13,sScript); //Tecla ENTER
	EjecutarScriptOnPressButton(oEvento,32,sScript); //Tecla SPACE
}
function PresionarBotonCancelar(oEvento,sIdElemento){
	var sScript = 'document.getElementById("' + sIdElemento + '").onclick()';	
	EjecutarScriptOnPressButton(oEvento,27,sScript); //Tecla ESC
}
function EjecutarScriptOnPressButton(oEvento,iKeyCode,sScript){
    var iEventKeyCode;
    if (!oEvento) {
        var oEvento = window.event;
    }
    if (oEvento.keyCode) { 
        iEventKeyCode = oEvento.keyCode; 
    } else if (oEvento.which) { 
        iEventKeyCode = oEvento.which; 
    } 
  	if (oEvento.keyCode == iKeyCode){
		eval(sScript);
		
		CancelarEvento(oEvento);
	}
	return;
}
function AgregarAjustarPosicionEtiquetaProcesandoForDocumentOnResizeHandler() {
    if (document.addEventListener) { 
        window.addEventListener('resize', AjustarPosicionEtiquetaProcesando, false); 
    } else if (document.attachEvent) { 
        window.attachEvent('onresize', AjustarPosicionEtiquetaProcesando); 
    } 
}
function AgregarAjustarPosicionEtiquetaProcesandoForDocumentOnScrollHandler() {
    if (document.addEventListener) { 
        window.addEventListener('scroll', AjustarPosicionEtiquetaProcesando, false); 
    } else if (document.attachEvent) { 
        window.attachEvent('onscroll', AjustarPosicionEtiquetaProcesando); 
    } 
}
function CancelarEvento(oEvento){
	if (oEvento){		
		oEvento.value = false;		
		oEvento.returnValue = false;
		if (oEvento.preventDefault) {
        oEvento.preventDefault();
    }

		oEvento.cancelBubble = true;
		
	}
}
function AbrirPopUpModal(sPagina, oParametros, iAncho, iAltura, bScrollBars, bResizable, bStatus) {
    var sScrollBars = 'no';
    var sStatus = 'no';
    var sResizable = 'no';
    if (bScrollBars){
        sScrollBars = 'yes';
    }
    if (bStatus){
        sStatus = 'yes';
    }
    if (bResizable){
        sResizable = 'yes';
    }
    if (window.showModalDialog) {
        window.showModalDialog(sPagina,oParametros,'dialogWidth:' + iAncho + 'px;dialogHeight:' + iAltura + 'px;resizable:' + sResizable + ';status:' + sStatus + ';help:no;center:yes;');
    } else {
        window.open(sPagina,null,'height=' + iAncho + ',width=' + iAncho + 'px,modal=yes');
    }
} 
function TextoCambiante(sNombreElemento, sTexto){
	if (document.getElementById(sNombreElemento).innerHTML == '&nbsp;' || document.getElementById(sNombreElemento).innerHTML == '' || document.getElementById(sNombreElemento).innerHTML == sTexto + '...'){
		document.getElementById(sNombreElemento).innerHTML = sTexto;
	} else {
		document.getElementById(sNombreElemento).innerHTML = document.getElementById(sNombreElemento).innerHTML + '.';
	}
	setTimeout('TextoCambiante("' + sNombreElemento + '","' + sTexto + '");', 500);
}
function TextoEstadoCambiante( sTexto){
	if (window.status == '&nbsp;' || window.status == '' || window.status == sTexto + '...'){
		window.status = sTexto;
	} else {
		window.status = window.status + '.';
	}
	setTimeout('TextoEstadoCambiante("' + sTexto + '");', 500);
}
function ReemplazarEnCadena(sCadena, sCadenaAReemplazar, sCadenaDeReemplazo){
	while (sCadena.indexOf(sCadenaAReemplazar) != -1){
		sCadena = sCadena.replace(sCadenaAReemplazar,sCadenaDeReemplazo);
	}
	return sCadena;
}
function GetRadioButtonSeleccionado(sNombreRadioButton){
	var oArrElementosRadioButton = document.getElementsByName(sNombreRadioButton);
	for (var i=0; i < oArrElementosRadioButton.length; i++){
		if (oArrElementosRadioButton[i].checked == true){
			return oArrElementosRadioButton[i];
		}
	}
}
function FechaDateDMA(dtFecha){
	if (dtFecha == null){
		return '';
	} else {
		return (dtFecha.getDate().toString().length == 1 ? '0' + dtFecha.getDate().toString() : dtFecha.getDate().toString()) + '/' + ((parseFloat(dtFecha.getMonth()) + 1).toString().length == 1 ? '0' + (parseFloat(dtFecha.getMonth()) + 1).toString() : (parseFloat(dtFecha.getMonth()) + 1).toString()) + '/' + (dtFecha.getFullYear().toString());
	}
}
function FormatDateYMD(dtFecha){
	if (dtFecha == null){
		return '';
	} else {
		return (dtFecha.getFullYear().toString()) + '-' + ((parseFloat(dtFecha.getMonth()) + 1).toString().length == 1 ? '0' + (parseFloat(dtFecha.getMonth()) + 1).toString() : (parseFloat(dtFecha.getMonth()) + 1).toString()) + '-' + (dtFecha.getDate().toString().length == 1 ? '0' + dtFecha.getDate().toString() : dtFecha.getDate().toString());
	}
}
function DateFromTextDMA(sFecha){
	var sDia;
	var sMes;
	var sAno;
	var sSep = '/';
	var iPos1;
	var iPos2;
	if (sFecha == '' || sFecha.indexOf(sSep) == -1) {
		return null;
	} else {	
		iPos1 = sFecha.indexOf(sSep);
		iPos2 = sFecha.indexOf(sSep,iPos1 + 1);
		sDia = sFecha.substring(0, iPos1);
		sMes = sFecha.substring(iPos1 + 1, iPos2);
		sAno = sFecha.substr(iPos2 + 1);
	}	
	var dtTempFecha= new Date(sAno, (parseFloat(sMes) -1) , sDia);
	if (dtTempFecha.getDate() != parseFloat(sDia) || dtTempFecha.getMonth() != parseFloat(sMes) -1 || dtTempFecha.getFullYear() != parseFloat(sAno)){
		return null;
	}
	else{
		return dtTempFecha;
	}
}
function DateFromTextMDA(sFecha){
	var sDia;
	var sMes;
	var sAno;
	var sSep = '/';
	var iPos1;
	var iPos2;
	if (sFecha == '' || sFecha.indexOf(sSep) == -1) {
		return null;
	} else {	
		iPos1 = sFecha.indexOf(sSep);
		iPos2 = sFecha.indexOf(sSep,iPos1 + 1);
		sMes = sFecha.substring(0, iPos1);
		sDia = sFecha.substring(iPos1 + 1, iPos2);
		sAno = sFecha.substr(iPos2 + 1);
	}	
	var dtTempFecha= new Date(sAno, (parseFloat(sMes) -1) , sDia);
	if (dtTempFecha.getDate() != parseFloat(sDia) || dtTempFecha.getMonth() != parseFloat(sMes) -1 || dtTempFecha.getFullYear() != parseFloat(sAno)){
		return null;
	}
	else{
		return dtTempFecha;
	}
}
function StartsWith(sCadena, sCadenaComienzo){
	var oRegExp = new RegExp('^' + sCadenaComienzo + '');
	return sCadena.match(oRegExp);
}
function LimpiarControles(bIncluirHidden, bIncluirInvisibles, sFormSectionID) {
	var oTiposElementos = new Array();
	var oElementos;
	var j = 0;


	oTiposElementos[0] = $("input");
	oTiposElementos[1] = $("textarea");
	oTiposElementos[2] = $("select");

	for (var j=0; j < oTiposElementos.length; j++){

		oElementos = oTiposElementos[j];
		for (var i = 0; i < oElementos.length; i++) {
	
			if (oElementos[i].getAttribute('type') == 'button' || oElementos[i].getAttribute('type') == 'submit' || oElementos[i].getAttribute('type') == 'reset'){
  				continue;
			}else if (!bIncluirInvisibles && EstaControlInvisible(oElementos[i])){				
    			continue;
			}else if (sFormSectionID != undefined && sFormSectionID != null && sFormSectionID != '' && oElementos[i].getAttribute('formSectionID') != sFormSectionID){
    			continue;
			}else if (oElementos[i].getAttribute('type') == 'radio' || oElementos[i].getAttribute('type') == 'checkbox'){
				oElementos[i].checked = false;
      } else if ((oElementos[i].getAttribute('type') != 'hidden') || (oElementos[i].getAttribute('type') == 'hidden' && bIncluirHidden)) {
				oElementos[i].value = '';
			} 
		
			if(j==2){
				oElementos[i].selectedIndex = 0;
				if (oElementos[i].getAttribute('multiple')){
						oElementos[i].selectedIndex = -1;
				}		
			}
			if (oElementos[i].className == 'campoError') {
				oElementos[i].className = 'campo';
			}
			LimpiarMensajeError(oElementos[i]);
		}
	}
	return;
}
function ObtenerCantidadSeleccionados(oSelectMultiple){
	var iCantidadSeleccionados = 0;
	for (var i = 0; i <= oSelectMultiple.options.length-1; i++) {
		if (oSelectMultiple.options(i).getAttribute('selected')){
			iCantidadSeleccionados += 1;
		}
	}	
	return iCantidadSeleccionados;
}
function VaciarCombo(oSelect){
	for (var i = oSelect.options.length-1; i >= 0; i--) {
		oSelect.options[i] = null;
	}	
}
function LeftTrim(sCadena) {
	while (sCadena.substring(0,1) == ' ') {
		sCadena = sCadena.substring(1, sCadena.length);
	}
	return sCadena;
}
function RightTrim(sCadena) {
	while (sCadena.substring(sCadena.length-1, sCadena.length) == ' ') {
		sCadena = sCadena.substring(0,sCadena.length-1);
	}
	return sCadena;
}
function Trim(sCadena) {
	while (sCadena.substring(0,1) == ' ') {
		sCadena = sCadena.substring(1, sCadena.length);
	}
	while (sCadena.substring(sCadena.length-1, sCadena.length) == ' ') {
		sCadena = sCadena.substring(0,sCadena.length-1);
	}
	return sCadena;
}
function EliminarHtmlOption(oHtmlSelect,iIndice) {
    oHtmlSelect.options[iIndice] = null;
}
function AgregarHtmlOption(oHtmlSelect,sTexto,sValor) {
    var bSeleccionadoPorDefecto = false;
    var bSeleccionado = false;
    var oHtmlOption = new Option(sTexto, sValor, bSeleccionadoPorDefecto, bSeleccionado)
    oHtmlSelect.options[oHtmlSelect.length] = oHtmlOption;
    oHtmlSelect.options[oHtmlSelect.length-1].selected = false;
}
function OrdenarHtmlOptions(oHtmlSelect) {
    var oArrHtmlOptions = new Array();
    for (var i=0; i < oHtmlSelect.options.length; i++){
        oArrHtmlOptions[i] = new Array(oHtmlSelect[i].value, oHtmlSelect[i].text);
	}
    oArrHtmlOptions.sort(function(a,b) { return a[0]-b[0]; });
    for (var i=oHtmlSelect.options.length-1;i>-1;i--){
        EliminarHtmlOption(oHtmlSelect,i);
	}
    for (var i=0; i<oArrHtmlOptions.length; i++){
        AgregarHtmlOption(oHtmlSelect,oArrHtmlOptions[i][1], oArrHtmlOptions[i][0])
	}
}
function FromStringToBoolean(sBoolean) {
	if (sBoolean == 'true' || sBoolean == 'True' || sBoolean == '1' || sBoolean == 1){
		return true;
	}
	return false;
}
function EstaControlInvisible(oElementoHtml){
    if (oElementoHtml == undefined || oElementoHtml == null) {
        return false;
    }
    if (oElementoHtml.style != undefined && oElementoHtml.style.display != undefined && oElementoHtml.style.display == 'none') {
        return true;
    }
    return EstaControlInvisible(oElementoHtml.parentNode);
}
function TraerAnchoVentanaBrowser(){
    if (typeof(window.innerWidth) == 'number') {
        return window.innerWidth; //Non-IE
    } else if (document.documentElement && document.documentElement.clientWidth) {
        return document.documentElement.clientWidth; //IE 6+ in 'standards compliant mode'
    } else if (document.body && document.body.clientWidth) {
        return document.body.clientWidth; //IE 4 compatible
    }
    return 0;
}
function TraerAlturaVentanaBrowser(){
    if( typeof( window.innerWidth ) == 'number' ) {
        return window.innerHeight; //Non-IE
    } else if (document.documentElement && document.documentElement.clientHeight) {
        return document.documentElement.clientHeight; //IE 6+ in 'standards compliant mode'
    } else if (document.body && document.body.clientHeight  ) {
        return document.body.clientHeight; //IE 4 compatible
    }
    return 0;
}
function TraerCoordenadasScrollX(){ 
    if (typeof(window.pageYOffset) == 'number' ) {
        return window.pageXOffset; //Netscape compliant
    } else if (document.body && document.body.scrollLeft) {
        return document.body.scrollLeft; //DOM compliant
    } else if (document.documentElement && document.documentElement.scrollLeft) {
        return document.documentElement.scrollLeft; //IE6 standards compliant mode
    }
    return 0;
}
function TraerCoordenadasScrollY(){ 
    if( typeof(window.pageYOffset) == 'number' ) {
        return window.pageYOffset; //Netscape compliant
    } else if (document.body && document.body.scrollTop) {
        return document.body.scrollTop; //DOM compliant
    } else if (document.documentElement && document.documentElement.scrollTop) {
        return document.documentElement.scrollTop; //IE6 standards compliant mode
    }
    return 0;
}
function ActualizarMensaje(sIdsEtiquetas,sTexto,sClaseClass){
 if (sIdsEtiquetasError != '') {
	    var oArrIdsEtiquetas = sIdsEtiquetas.split(';');
	    for (var i = 0; i < oArrIdsEtiquetas.length; i++){
          document.getElementById(oArrIdsEtiquetas[i]).style.display = '';
	        document.getElementById(oArrIdsEtiquetas[i]).innerHTML = sTexto;
        }
    }
}
function LimpiarMensaje(sIdsEtiquetas){
 if (sIdsEtiquetasError != '') {
	    var oArrIdsEtiquetas = sIdsEtiquetas.split(';');
	    for (var i = 0; i < oArrIdsEtiquetas.length; i++){
            document.getElementById(oArrIdsEtiquetas[i]).innerHTML = '';
            document.getElementById(oArrIdsEtiquetas[i]).style.display = 'none';
        }
    }
}

