// JavaScript Document
/**
 * Javascripts comunes a la apliacion
 *
 * Aqui se colocaran las funciones javascript comunes para cualquier
 * tema utilizado en la aplicacion
 */
/**
 * Colocar el puntero en el primer cuadro de texto vacio del formulario
 */
function set_focus() {
/*    if (document.forms[1]) {
        for (i = 0; i < document.forms[1].elements.length; i++) {
            if (document.forms[1].elements[i].type == "text" || document.forms[1].elements[i].type == "password") {
                if (document.forms[1].elements[i].value == "") {
                    document.forms[1].elements[i].focus();
                    return 0;
                }
            }
        }
    }
*/}

function jsPHP_do(url) {
    var jsel = document.createElement("script");

    jsel.language = "javascript"
    jsel.type = "text/javascript"
    jsel.src = url;

    var child = document.getElementById("child");

    if (child != null) {
       child.innerHTML = "";
    }
    child.appendChild(jsel);
}

<!--
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
<!--
function acceptNum(theField){
    var punto = false;
    var newValue = "";
    var value = theField.value;
    var length = theField.value.length;

    if (length > 0) {
        for (var i = 0; i < length; ++i) {
            var new_key = value.charAt(i); //cycle through characters
            if((new_key >= "0") && (new_key <= "9") || (new_key == ",")) {
                if ((new_key == ",")) {
                    if (!punto) {
                        newValue += new_key;
                        punto = true;
                    }
                }
                else {
                    newValue += new_key;
                }
            }
        }
    }
    else {
        if((new_key >= "0") && (new_key <= "9")) {
            newValue += new_key;
        }
    }
    if (newValue.length != 0) {
        theField.value = newValue;
        return true;
    }
    return false;
}
function acceptNumNoPunto(theField){
    var newValue = "";
    var value = theField.value;
    var length = theField.value.length;

    if (length == 0) {
        theField.value = 0;
    }
    for (var i = 0; i < 20; ++i) {
        var new_key = value.charAt(i); //cycle through characters

        if((new_key >= "0") && (new_key <= "9")) {
            newValue += new_key;
        }
    }
    if (newValue != "") {
        theField.value = newValue;
        return true;
    }
    return false;
}

var punto;
function validarCampo(e, modo, campo) {
    var tecla;
    var patron;

    if (document.all) {
        tecla = e.keyCode;
    }
    else {
        tecla = e.which;
    }
    if (tecla == 8 || tecla == 13 || tecla == 9 || tecla == 0) {
        return true;
    }
    switch (modo) {
        case 'letra':
            patron = /[A-Za-zs]/;
        break;
        case 'numero':
            patron = /^\d/;
            if (campo.length <= 1 && campo != ",") {
                punto = false;
            }
            if (tecla == 44 && !punto) {
                punto = true;
                return true;
            }
        break;
        case 'nopunto':
            patron = /^\d/;
        break;
        case "RIF":
            patronTP = /^(J|G|E|V)?$/;
            patronN  = /^\d/;
            var TipoPersona = campo.substring(0,1);
            var NroRIF =     campo.substring(1);
            TipoPersona = TipoPersona.toUpperCase();

            te = String.fromCharCode(tecla);
            te = te.toUpperCase();

            if (campo.length >= 1) {
                if (!patronTP.test(TipoPersona)) {
                    return patronTP.test(te);
                }
                else {
                    return patronN.test(te);
                }
            }
            else {
                return patronTP.test(te);
            }
            /*
            if (campo.length < 1 || campo.length < 10) {
                return patronTP.test(te);
            }
            if (campo.length >= 1) {
                return patronN.test(te);
            }*/
            return false;
        break;
    }
    te = String.fromCharCode(tecla);
    return patron.test(te);
}

function comprobarResolucion() {
    var locationObj = window.location;
    var height = 0;
    var width  = 0;
    if (self.screen) {     // for NN4 and IE4
        width  = screen.width;
        height = screen.height;
    }
    else {
        if (self.java) {   // for NN3 with enabled Java
            var jkit = java.awt.Toolkit.getDefaultToolkit();
            var scrsize = jkit.getScreenSize();
            width = scrsize.width;
            height = scrsize.height;
        }
    }
    top.window.moveTo(0,0);
    ScreenAvailWidth  = screen.availWidth;
    ScreenAvailHeight = screen.availHeight;

    if (ScreenAvailWidth > 1024) {
        ScreenAvailWidth = 1024;
    }
    if (ScreenAvailHeight > 768) {
        ScreenAvailHeight = 768;
    }
    if (document.all){
        top.window.resizeTo(ScreenAvailWidth,ScreenAvailHeight);
    }
    else if (document.layers || document.getElementById){
        if (top.window.outerHeight < ScreenAvailHeight || top.window.outerWidth < ScreenAvailWidth){
            top.window.outerHeight = ScreenAvailHeight;
            top.window.outerWidth = ScreenAvailWidth;
        }
    }
    if ((width >= 800 && width <= 1920) && (height >= 600 && height <= 1080)) {
        top.window.moveTo(0,0);
        ScreenAvailWidth  = screen.availWidth;
        ScreenAvailHeight = screen.availHeight;

        if (ScreenAvailWidth > 1280) {
            ScreenAvailWidth = 1280;
        }
        if (ScreenAvailHeight > 1024) {
            ScreenAvailHeight = 1024;
        }
        if (document.all){
            top.window.resizeTo(ScreenAvailWidth,ScreenAvailHeight);
        }
        else if (document.layers || document.getElementById){
            if (top.window.outerHeight < ScreenAvailHeight || top.window.outerWidth < ScreenAvailWidth){
                top.window.outerHeight = ScreenAvailHeight;
                top.window.outerWidth = ScreenAvailWidth;
            }
        }
    }
    else {
        theLocation = locationObj.toString();
        posicion = theLocation.lastIndexOf("/");

        theLocation = theLocation.substring(posicion);
        theLocation = theLocation.replace("/", "");
        //document.writeln(theLocation);

        if (theLocation != "resolucion.php") {
            window.location.href = "resolucion.php";
        }
    }
}

function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    }
}
comprobarResolucion();


function AddAccesorio() {
	var NroAccesorios = document.getElementById("TxtNroAccesorios").value;
	var NroTotalAccesorios = document.getElementById("TxtNroTotalAccesorios").value;
    var table = document.getElementById("Accesorios");
    var tbody = table.getElementsByTagName("tbody")[0];
	var Row = document.getElementById("AccesorioRow");

    AccesID = NroAccesorios;
    AccesID++;

    if (NroTotalAccesorios >= AccesID) {
        var newRow = Row.cloneNode(true);
        
        newRow.id = "AccesorioRow"+NroAccesorios;
        newRow.getElementsByTagName("select")[0].id    = "Accesorio"+AccesID;
        newRow.getElementsByTagName("select")[0].name  = "Accesorio"+AccesID;
        newRow.getElementsByTagName("select")[0].value = "";
        newRow.getElementsByTagName("input")[0].id    = "ValorAccesorio"+AccesID;
        newRow.getElementsByTagName("input")[0].name  = "ValorAccesorio"+AccesID;
        newRow.getElementsByTagName("input")[0].value = "";
    
    	tbody.appendChild(newRow);
    
    	document.getElementById("TxtNroAccesorios").value = AccesID;
   }
}

/*
 * Obtener el nombre de la Empresa correspondiente a el RIF dado
 */
function doRIF(theField) {
    var newValue = "";
    var fact = false;
	var valueField = theField.value;
    var TipoPersona = valueField.substring(0,1);
   	valueField = valueField.substring(1, valueField.length);
    TipoPersona = TipoPersona.toUpperCase();
    
   	newID = theField.id;
    theID = newID.substring(7, 8);

    while(valueField.length < 9) {
    	valueField = "0"+valueField;
    }
    newValue = TipoPersona+valueField;
    patron = /^(J|G|E|V)?\d{9}$/

    if (patron.test(newValue)) {
    	if (document.getElementById("TxtRIFTomador")) {
        	fact = true;
	    	document.getElementById("TxtRIFTomador").value = newValue;
        }
        else {
        	document.getElementById(newID).value = newValue;
        }
    }
    else {
    	document.getElementById("TxtRIFTomador").value = "";
    }
    return true;
}

function initDoc() {
    var newValue = "";
	var valueFieldOrg = document.getElementById("TxtRIFTomador").value;
    var TipoPersona = valueFieldOrg.substring(0,1);
    var IdPersona = false;
   	valueField = valueFieldOrg.substring(1, valueFieldOrg.length);
    TipoPersona = TipoPersona.toUpperCase();
	valueFieldNew = valueField;
    
    newValue = TipoPersona+valueField;
	patron = /^(J|G|E|V)?\d{9}$/
    
    if (TipoPersona == "J" || TipoPersona == "G") {
    	newPersona = "J";
    }
    else {
    	newPersona = "F";
    }

	var options = document.getElementById("TxtTipoPersonaCbx").options.length;

    for (i=0; i < options; i++) {
     	if (document.getElementById("TxtTipoPersonaCbx").options[i].value == newPersona) {
          	document.getElementById("TxtTipoPersonaCbx").options[i].selected = true;
            IdPersona = true;
        }
    }
    document.getElementById("TxtTipoPersona").value = newPersona;
    
    if (!IdPersona) {
        document.getElementById("TxtTipoPersonaCbx").options[1].selected = true;
        document.getElementById("TxtTipoPersona").value = "F";
    }

	if (isNaN(TipoPersona)) {
    	while (valueField.length < 9) {
        	valueField = "0"+valueField;
        }
    	document.getElementById("TxtRIFTomador").value = TipoPersona+valueField;
    }
}
function checkYear(theValue) {
	postError = document.getElementById("post_error");

	if (theValue < 2000) {
		postError.parentNode.className = "tabForm";	
		postError.parentNode.style.padding = "10px";
		postError.style.display = "inline";
	}
	else {
		postError.parentNode.className = "";	
		postError.parentNode.style.padding = "0px";
		postError.style.display = "none";
	}
}
