/* more or less Fields */
// this is taken from quirksmode.org/quirksmode.js - Peter Paul Koch

var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&&
navigator.userAgent.indexOf('Mac') != -1
);

var W3CDOM = (!bugRiddenCrashPronePieceOfJunk && 
document.getElementsByTagName && document.createElement);

window.onload = initialize;

function initialize () {
/* Hide nifty stuff from old browsers (although actually we're not hiding
   anything */
if (!W3CDOM) {
    alert('You need to UPGRADE your browser to use this app !\n'
            +'Try with Opera 7.21, Mozilla 1.4, Firebird 0.7, Internet Explorer 6, etc.\n\n'
            +'Why ? because it uses a lot of javascript and manipulation of the DOM\n\n'
            +'Besides what would you prefer for a car: a Ford Pinto \'76 or a Viper R/T \'02 ? huh ?!');
        location.href='upgrade.html';           
}
/* If the page has an init() function, execute it */    
    if (self.init) self.init();
}

/* GENERIC JS CODE */
// this one from dithered.com
function isDefined(property) {
  return (typeof property != 'undefined');
}

function xor(eid, state) {
    elm = document.getElementById(eid).style;
    elm.display = state;
}


/* MAIN APP JS CODE */
// counts the number of fields; used to create the ID value of a field
var counter = 0;

function check() {
    if (counter >= 5) {
        alert('Sólo puede ingresar 5 recipientes como máximo !');
        return false;
    }
    return true;
}

// creates a form "field" (in this case a field consists of several DIV tags
// and form elements)
function moreFields() {
    if (check()) {
        var newFields = document.getElementById('readroot').cloneNode(true);
        newFields.style.display = 'block';
        newFields.className = "thefields";
        var insertHere = document.getElementById('writeroot');
        insertHere.parentNode.insertBefore(newFields,insertHere);
        counter++; // global
    }
}

function lessFields(less) {
    nodeparent = document.getElementById(less);
    nodes = nodeparent.childNodes;
    toKill = new Array;
    for (var i=0, c=nodes.length; i < c; i++) {
        if (nodes[i].className == "thefields") {
            toKill[toKill.length] = nodes[i];
        }
    }
    killMe = toKill[toKill.length-1];
    nodeparent.removeChild(killMe);
    decrement();
}

function decrement() {
    if (!(counter == 0)) {
        counter--;
    }
}

/* end more or less fields */
