﻿

    // Agrega un input en la pagina en forma dinamica
    // id: id del DIV donde se agrega el input
    // type: tipo de input que se agrega
    // name: nombre y id del input
    // value: valor que lleva el input (texto del boton o bien texto en la caja de texto
    // clase: clase CSS a la que pertenece
    // width(opcional): ancho del input
    // tag (opcional): tag que se crea para ingresar el input
    // gblInterAgregados: Variable global que indica el numero de inputs que se han agregado
    function AddFormField(id, type, name, value, clase, width, tag) {
        if(! document.getElementById && document.createElement) { return; }
        var inhere = document.getElementById(id);
        var formfield = document.createElement("input"); 
        name = String(name + gblInterAgregados);
        formfield.id = name;
        formfield.name = name;
        formfield.type = type;
        formfield.value = value;
        formfield.className = clase;
        
        if (width != ''){
            formfield.style.width = width;
        }
       
        if(tag.length > 0) {
            var thetag = document.createElement(tag);
            thetag.id = String("divText" + gblInterAgregados)
            thetag.appendChild(formfield);
            inhere.appendChild(thetag);
            }
            else { inhere.appendChild(formfield); 
            }
        document.getElementById(name).focus();    
    } // function AddFormField()



    // Agrega un select (combo) en la pagina en forma dinamica
    // id: id del DIV donde se agrega el input
    // name: nombre y id del input
    // value: valor que lleva el select
    // clase: clase CSS a la que pertenece
    // width(opcional): ancho del select
    // tag (opcional): tag que se crea para ingresar el select
    // gblInterAgregados: Variable global que indica el numero de inputs que se han agregado
    function AddSelect(id, name, value, clase, width, tag) {
        if(! document.getElementById && document.createElement) { return; }
        var inhere = document.getElementById(id);
        var formfield = document.createElement("select"); 
        name = String(name + gblInterAgregados);
        formfield.id = name;
        formfield.name = name;
        formfield.value = value;
        formfield.className = clase;

        if (width != ''){
            formfield.style.width = width;
        }
        
        if(tag.length > 0) {
            var thetag = document.createElement(tag);  //Crea un DIV
            thetag.id = String("divSel" + gblInterAgregados)
            thetag.appendChild(formfield);      //Inserta  el Select
            inhere.appendChild(thetag);         //Inserta el DIV dentro del DIV Combo
            }
            else { inhere.appendChild(formfield); //Inserta el Select
        }
    } // function AddSelect()


// Elimina los textbox creados con addFormfield   (incluyendo el div numerado)
function EliminaTextbox(i){
        // Obtenemos el elemento
        var elimTextbox = document.getElementById("divText" + String(i));
        // Obtenemos el padre de dicho elemento con la propiedad “parentNode”
        var padreTextbox = elimTextbox.parentNode;
        // Eliminamos el hijo (elim) del elemento padre
        padreTextbox.removeChild(elimTextbox);

}

// Elimina los selects creados con addSelect    (incluyendo el div numerado)
function EliminaSelect(i){
        var elimSelect = document.getElementById("divSel" + String(i));
        var padreSelect = elimSelect.parentNode;   //Este es el DIV combo
        padreSelect.removeChild(elimSelect);
}


  //Helper function for accessing the div specified in the id parameter.
  //The function checks first if the div contains content.
  function $valor( id ) { 
    return   (document.getElementById( id ).value != "undefined") ?  
    document.getElementById( id ).value : ""; 
  }
  
  
  //Reemplaza todos caracteres "Buscar" por el caracter "Reemplazar"
  function ReemplazaCaracter(Cadena,Buscar,Reemplazar){
  var strReemplazo;
    strReemplazo = Cadena.replace(/Reemplazar/g, Buscar);   //se utiliza el operador "g" para habilitar la funcion global
    return strReemplazo;
  }
  

//funcion parser
//Entrada string separado por comas ("1,2,3,4")
//Salida array de strings (array[0]=num de elementos, array[1]="1", array[2]="2" and so on...)
function parser (InString) {
    var Sep = ",", NumSeps=1, parse; 
    for (Count=1; Count < InString.length; Count++) {
        if (InString.charAt(Count)==Sep)
            NumSeps++;
        }
        parse = new Array ();
        var Start=0, Count=1, ParseMark=0, LoopCtrl=1;
        while (LoopCtrl==1) {
            ParseMark = InString.indexOf(Sep, ParseMark);
            TestMark=ParseMark;
            if ((TestMark==0) || (TestMark==-1)){
                parse[Count]= InString.substring (Start, InString.length);
                LoopCtrl=0;
                break;
            }
            parse[Count] = InString.substring (Start, ParseMark);
            Start=ParseMark+1, ParseMark=Start, Count++;
        }
            parse[0]=Count;
            return (parse);
    }


//Funciones para mostrar una ventana popup
function move_box(an, box) {
  var cleft = 80;
  var ctop = -22;
  var obj = an;
  while (obj.offsetParent) {
    cleft += obj.offsetLeft;
    ctop += obj.offsetTop;
    obj = obj.offsetParent;
  }
  box.style.left = cleft + 'px';
  ctop += an.offsetHeight + 8;
  //ctop += an.document.body.offsetHeight + 8;
  if (document.body.currentStyle && document.body.currentStyle['marginTop']) {
    ctop += parseInt(document.body.currentStyle['marginTop']);
  }
  box.style.top = ctop + 'px';
}       //move_box


function show_hide_box(an, width, height, borderStyle) {
  var href = an.href;
  var boxdiv = document.getElementById(href);

  //Si ya existe el elemento
  if (boxdiv != null) {
    //Si esta oculto muestralo
    if (boxdiv.style.display=='none') {
      move_box(an, boxdiv);
      boxdiv.style.display='block';
      an.innerHTML = "Cerrar"
    } 
    else {
        //Ocultarlo
        boxdiv.style.display='none';
        an.innerHTML = "Instrucciones";
    }
    return false;
  }

  //No existe por lo tanto, crear el elemento
  boxdiv = document.createElement('div');
  boxdiv.setAttribute('id', href);
  boxdiv.style.display = 'block';
  boxdiv.style.position = 'absolute';
  boxdiv.style.width = width + 'px';
  boxdiv.style.height = height + 'px';
  boxdiv.style.border = borderStyle;
  boxdiv.style.backgroundColor = 'fff';

  var contents = document.createElement('iframe');
  contents.scrolling = 'no';
  contents.frameBorder = '0';
  contents.style.width = width + 'px';
  contents.style.height = height + 'px';
  contents.src = href;

  boxdiv.appendChild(contents);
  document.body.appendChild(boxdiv);
  move_box(an, boxdiv);
  
  an.innerHTML = "Cerrar"
  return false;
}


 //*********************************************************************************  
 // Function que valida que un campo contenga un string y no solamente un " "  
 // Es tipico que al validar un string se diga  
 //    if(campo == "") ? alert(Error)  
 // Si el campo contiene " " entonces la validacion anterior no funciona  
 //*********************************************************************************  
  
 //valida que el campo no este vacio y no tenga solo espacios en blanco  
 function ValidaCampoVacio(F) {  
           
         if( vacio(F.value) == false ) {  
                 alert("No puede haber campos vacios")  
                 return false;
         } else {  
                 return true;
         }  
           
 }  
 
  //busca caracteres que no sean espacio en blanco en una cadena  
 function vacio(q) {  
         for ( i = 0; i < q.length; i++ ) {  
                 if ( q.charAt(i) != " " ) {  
                         return true  
                 }  
         }  
         return false  
 }  

 
 
 /* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Steve Chipman | http://slayeroffice.com/ */

// constants to define the title of the alert and button text.
var ALERT_TITLE = "Info!";
var ALERT_BUTTON_TEXT = "Cerrar";

// over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts
/*if(document.getElementById) {
  window.alert = function(txt) {
    createCustomAlert(txt);
  }
}
*/

function createCustomAlert(txt) {
  // shortcut reference to the document object
  d = document;

  // if the modalContainer object already exists in the DOM, bail out.
  if(d.getElementById("modalContainer")) return;

  // create the modalContainer div as a child of the BODY element
  mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
  mObj.id = "modalContainer";
   // make sure its as tall as it needs to be to overlay all the content on the page
  mObj.style.height = document.documentElement.scrollHeight + "px";

  // create the DIV that will be the alert 
  alertObj = mObj.appendChild(d.createElement("div"));
  alertObj.id = "alertBox";
  // MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
  if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
  // center the alert box
  alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

  // create an H1 element as the title bar
  h1 = alertObj.appendChild(d.createElement("h1"));
  h1.appendChild(d.createTextNode(ALERT_TITLE));

  // create a paragraph element to contain the txt argument
  msg = alertObj.appendChild(d.createElement("p"));
  msg.innerHTML = txt;
  
  // create an anchor element to use as the confirmation button.
  btn = alertObj.appendChild(d.createElement("a"));
  btn.id = "closeBtn";
  btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
  btn.href = "#";
  // set up the onclick event to remove the alert when the anchor is clicked
  btn.onclick = function() { removeCustomAlert();return false; }
}

// removes the custom alert from the DOM
function removeCustomAlert() {
  document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}


/******************************************************************************
funciones para validar que la entrada de un campo no este vacia
******************************************************************************/
  // Preload image
  var empty = new Image(); empty.src = "images/fieldempty.gif";  
  var haveerrors = 0;
  
  function showImage(imagename, imageurl, errors) {
    document[imagename].src = imageurl;
    if (!haveerrors && errors) haveerrors = errors;
  }
  
  function validaCampo(campo) {
    haveerrors = 0;
    (document.getElementById(campo).value.length < 1) // validate first name length
        ? showImage("firstnameerror", "images/fieldempty.gif", true)   // no semi-colon after this line!
        : showImage("firstnameerror", "images/blankimage.gif", false); // true = errors, false = no errors
    return (!haveerrors);
  }