////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// aplicacion.js / Librería de funcinones para el sistema Argos.
// Construida por Unnica Desarrollo Digital y Diseño S.L.
//	Todos los derechos reservados.
//
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
var no_hay_respuesta = "No hay respuesta del servidor. Verifique la conexion.";
var Utf8 = {

    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}
function redondear(cantidad, decimales) {
//lo redondea a 2 decimales
	var cantidad = parseFloat(cantidad);
	var decimales = parseFloat(decimales);
	decimales = (!decimales ? 2 : decimales);
	return Math.round(cantidad * Math.pow(10, decimales)) / Math.pow(10, decimales);
}
function ver_capa(capa){
	if($(capa).style.display == 'none'){
		Effect.SlideDown(capa,{ duration: 0.3 });
		//$(capa).style.display = '';
	}
	else{
		Effect.SlideUp(capa,{ duration: 0.3 });
		//$(capa).style.display = 'none';
	}
	
}

function ejecutar(archivo_procesamiento, pars, funcion_salida, mensaje_salida, padre){
	
			
				var myAjax = new Ajax.Request(
				archivo_procesamiento, 
				{
					method: 'post', 
					parameters: pars, 
					onComplete: function(originalRequest){
						switch(originalRequest.responseText){
							case '1':	
								alert(mensaje_salida);
								if(padre){
									window.opener.procesar(funcion_salida,pars,'');
								}
								else{
									procesar(funcion_salida,pars,'');
								}
								
							break;
							case '0':
								alert("Se ha producido un error en el procesamiento.\nPor favor, pruebe más tarde.");
							break;
							default:
								alert(originalRequest.responseText);
							break;
						}
					}
				});
	
			
		}


function ver_pagina(pagina,pars,target){
	//ponemos el icono de cargando
	$(target).innerHTML = "<img src='images/loading.gif' width='32' height='32' />";
	var myAjax = new Ajax.Updater(
			target, 
			pagina, 
			{
				method: 'get', 
				parameters: pars
			});
	
}

function popUp(URL, ancho, alto){
	//alert(URL);
		ancho += 20;
		alto += 40;
		w = screen.availWidth;
		h = screen.availHeight;
	//alert(h);
	//alert(w);
		leftd = (w/2)-(ancho/2);
		topd = (h/2)-(alto/2);
		parametros = 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=' + ancho + ',height=' + alto + ',left = '+leftd+',top = '+topd+'';
		//ventana =  window.open(URL, 'Imagen', parametros);
		ventana =  window.open(URL, '', parametros);
		if (!ventana){
			alert("La ventana de descargas no se mostró debido a un bloqueador de PopUp.\nDesactívelo para poder descargar el archivo.");
		}
	}
	


function cadena_variables(formulario){
	
	var ob;
	var sql = "";
	var campos = $(formulario).elements.length;
	//alert("campos:"+campos);
	for(i=0;i<campos;i++){
		ob = $(formulario).elements[i];
		switch($(ob).type){
			case 'select-one':
				sql += "&"+$(ob).name+"="+$(ob).options[$(ob).selectedIndex].value;
			break;
			case 'checkbox':
				if($(ob).checked){
					sql += "&"+$(ob).name+"=1";
				}
				else{
					sql += "&"+$(ob).name+"=0";
				}	
			break;
			case 'radio':
				if($(ob).checked){
					sql += "&"+$(ob).name+"="+$(ob).value;
				}
			break;
			case 'submit':
			
			break;
			default:
				sql += "&"+$(ob).name+"="+$(ob).value;
			break;
		}
			
	}
	return sql;
}
