// JavaScript Document
function borrar(){
return confirm("Esta seguro que desea borrar el registro?");
}

function borraCombo(p_combo){
	var combo = document.getElementById(p_combo);
	while( combo.options.length > 0 ){
		combo.remove(0);
	}
}

function refrescar() {
//window.parent.location.reload();
//alert(window.parent);
window.opener.submitir();
}

function submitir() {
document.all.frm.submit();
}

function limpiaCamposTabla(){
	for (i=0; i<document.all.camposTabla.childNodes.length; i++) {
		elem = document.all.camposTabla.childNodes[i];
		if ( elem.tagName == 'INPUT' && elem.type == 'hidden' && elem.name.substring(elem.name.length -3) != '_op' ){
			elem.value = '';
		}
	}
}

var submitido = false;


if(navigator.appName == 'Microsoft Internet Explorer'){
	window.attachEvent('onload', window_load);
	document.attachEvent('onkeypress', aceptar_xxx);
} else {

	window.addEventListener('load', window_load_firefox, false);
	document.addEventListener('keyup', aceptar_xxx_firefox, false);
}



function window_load(){
	
	if( document.getElementsByName("frm").length > 0 ){
		document.getElementsByName("frm")[0].attachEvent('onsubmit', marcar_submitido_xxx);
	} else if(document.getElementsByName("form").length > 0){
		document.getElementsByName("form")[0].attachEvent('onsubmit', marcar_submitido_xxx);
	}
	
}

function window_load_firefox(){

	if( document.getElementsByName("frm").length > 0 ){
		document.getElementsByName("frm")[0].addEventListener('submit', marcar_submitido_xxx, false);
	} else if(document.getElementsByName("form").length > 0){
		document.getElementsByName("form")[0].addEventListener('submit', marcar_submitido_xxx, false);
	}
	
}

function marcar_submitido_xxx(){
	//alert('marcar_submitido_xxx');
	submitido = true;
}

var reloj;
function aceptar_xxx(){
	
	if (window.event.keyCode == 13){
		if (document.all.__aceptar) {
			reloj = window.setInterval(submitir_xxx, 200);
		}
	}
}

function aceptar_xxx_firefox(e){
	
	var KeyID = e.keyCode;
	
	if(KeyID == 13){
		if(document.getElementById("__aceptar")){
			reloj = window.setInterval(submitir_xxx, 200);
		}
	}
	
}

function submitir_xxx(){
	window.clearInterval(reloj);
	
	if(document.getElementById("__aceptar")){
		if(!submitido) {  document.getElementById("__aceptar").onclick(); }
	}

}

function verCarritos(usuario){
	location.href = "presupuesto_cliente.jsp?usuario=" + usuario;
}

function verCarritosPorModulo(usuario, id_modulo){
	location.href = "presupuesto_cliente.jsp?usuario=" + usuario + "&id_modulo=" + id_modulo;
}

function verConfirmacionesAnteriores(usuario) {
	location.href = "misComprasAnt_redireccion.jsp?usuario=" + usuario;
}

function aMayusculas(control) {
	window.event.keyCode = String.fromCharCode(window.event.keyCode).toUpperCase().charCodeAt(0);
}

function nuevaConsulta(accion, subconsulta, orden){
	var form = document.forms[0];
	
	form.servlet_accion.value = accion;
	form.servlet_subconsulta.value = subconsulta;
	form.servlet_orden.value = orden; 
	form.servlet_pagina.value = "1";
	form.servlet_consultaRealizada.value = "true";
	
	form.action = "Consultas";
	
	form.submit();
}

function copiaCombo(origen, destino){
	//copia un combo ...
	
	var l = destino.options.length;
	for(var i=0; i<l; i++){
			destino.options.remove(0);
	}
	
	l=origen.options.length;
	for(var i=0; i<l; i++){
		var oOption = document.createElement("OPTION");
		destino.options.add(oOption);
		oOption.innerText = origen.options(i).text;
		oOption.value = origen.options(i).value;
	}

}

function verificarPassword (control1, control2) {
	if (control1 == control2){
		document.all.frm.submit()
	} else {
		alert('Los passwords no coinciden');
	}
}

function isDate(fecha) {
        // valida que sea formato: dd/mm/aaaa
        var dias = new Array(12);
        
        dias[0] = 31;
        dias[1] = 0;
        dias[2] = 31;
        dias[3] = 30;
        dias[4] = 31;
        dias[5] = 30;
        dias[6] = 31;
        dias[7] = 31;
        dias[8] = 30;
        dias[9] = 31;
        dias[10] = 30;
        dias[11] = 31;
        // el largo con las barras tiene que ser de 10
        if (fecha.length != 10) {
            return false;
        }
        //tiene que tener 2 barras "/"
        if (fecha.indexOf("/") == -1) {
            return false;
        } else if ( fecha.indexOf("/", fecha.indexOf("/") + 1) == -1 ) {
            return false;
        }
        var dia = 0;
        var mes = 0;
        var anio = 0;
        dia = parseInt(fecha.substring(0,2),10);
        mes = parseInt(fecha.substring(3,5),10);
        anio = parseInt(fecha.substring(6,10));
        
        // año de 0 - 5000
        if (anio < 0 || anio > 5000) {
            return false;
        }
        // mes de 1 - 12
        if (mes < 1 || mes > 12) {
            return false;
        }
        // analizo febrero
        if ( anio % 4 == 0) {
            // es año biciesto
            dias[1] = 29;
        }
        else {
            //año regular
            dias[1] = 28;
        }
        //dia entre los valores del array
        if (dia < 1 || dia > dias[mes-1]) {
            return false;
        }
        return true;
    }

function muestraEscondeFromCheckbox(p_objCheckBox, p_obj){
	objCheckBox = eval("document.all." + p_objCheckBox);
	obj = eval("document.all." + p_obj);
	
	if(objCheckBox.checked){
			obj.style.display = "";
	} else {
			obj.style.display = "none";
	}
}

function trim(s) {
  while (s.substring(0,1) == ' ' || s.substring(0,1) == '\n') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ' || s.substring(s.length-1,s.length) == '\n') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function seleccionaEnCombo(p_combo, valor){
	var combo = document.getElementById(p_combo);
	for(var i=0; i<combo.options.length; i++){
		if(combo.options[i].value == valor){
			combo.selectedIndex = i;
		}
	}
}

function reemplazar(campo, buscar, reemplpor){
	var res = campo;
	while(res.indexOf(buscar) != -1){
		res = res.replace(buscar, reemplpor);
	}
	return res;
}

function reemplazarForXML(campo){
	var res = campo;

    res = reemplazar(res, "'", "´");
    res = reemplazar(res, "\"", "´");

/*	res = reemplazar(res, "&amp;","-");
	res = reemplazar(res, "&", "-");
	res = reemplazar(res, "<", "-");
	res = reemplazar(res, ">", "-");
	res = reemplazar(res, "'", "-");
	res = reemplazar(res, "\"", "-");
	
	res = reemplazar(res, "á","a");
	res = reemplazar(res, "é","e");
	res = reemplazar(res, "í","i");
	res = reemplazar(res, "ó","o");
	res = reemplazar(res, "ú","u");

	res = reemplazar(res, "Á","A");
	res = reemplazar(res, "É","E");
	res = reemplazar(res, "Í","I");
	res = reemplazar(res, "Ó","O");
	res = reemplazar(res, "Ú","U");
*/
	
	
	return res;
}

function URLEncode( plaintext )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode( )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = document.URLForm.F2.value;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   document.URLForm.F1.value = plaintext;
   return false;
};