// Ajax 1.0 - GPS Website em janeiro de 2010
function Ajax(url,id) {
    if (document.getElementById) {
        var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
    }
    if (x) {
        x.onreadystatechange = function() {
        if (x.readyState == 4 && x.status == 200) {
        el = document.getElementById(id);
        el.innerHTML = x.responseText;
      }
    }
        x.open("GET", url, true);
        x.send(null);
    }
    }

// Auto-Tab 1.0
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
    var keyCode = (isNN) ? e.which : e.keyCode; 
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
}
function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
}
function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
    }
    return true;
}

// Enter to Tab 1.0
function handleEnter (field, event) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {
    var i;
    for (i = 0; i < field.form.elements.length; i++)
    if (field == field.form.elements[i])
    	break;
    i = (i + 1) % field.form.elements.length;
    field.form.elements[i].focus();
    return false;
    } 
    else
    return true;
    }
    
// General Image Pre-Load 1.0
function PreLoadImg() {
	var lo_documentImages = document.images;
	var ll_loop = eval(0);
	var lo_currentImage = new Image();
	var lo_newImage = new Image();
	for( ll_loop = 0; ll_loop < lo_documentImages.length; ll_loop ++ )
	{
	lo_currentImage = lo_documentImages[ ll_loop ];
	lo_newImage = new Image();
	lo_newImage.src = lo_currentImage.src;
	}
}

// Expand the Window 1.0
function FullWindow() {
    window.moveTo(0, 0);
    window.resizeTo(screen.availWidth, screen.availHeight);
}

// SideMenu 1.0
if (document.getElementById) {
    document.write('<style type="text/css">\n')
    document.write('.SideMenuOptions{display: none;}\n')
    document.write('</style>\n')
}
function SwitchMenu(obj) {
    if (document.getElementById) {
        var el = document.getElementById(obj);
        var ar = document.getElementById("SideMenu").getElementsByTagName("span");
        if (el.style.display != "block") {
            for (var i = 0; i < ar.length; i++) {
                if (ar[i].className == "SideMenuOptions")
                    ar[i].style.display = "none";
            }
            el.style.display = "block";
        } else {
            el.style.display = "none";
        }
    }
}

// Image Expander (Zoom) 1.0
function ImageExpander(oThumb, sImgSrc) {
    this.oThumb = oThumb;
    this.oThumb.expander = this;
    this.oThumb.onclick = function() { this.expander.expand(); }
    this.smallWidth = oThumb.offsetWidth;
    this.smallHeight = oThumb.offsetHeight;
    this.bExpand = true;
    this.bTicks = false;
    if (!window.aImageExpanders) {
        window.aImageExpanders = new Array();
    }
    window.aImageExpanders.push(this);
    this.oImg = new Image();
    this.oImg.expander = this;
    this.oImg.onload = function() { this.expander.onload(); }
    this.oImg.src = sImgSrc;
}
ImageExpander.prototype.onload = function() {
    this.oDiv = document.createElement("div");
    document.body.appendChild(this.oDiv);
    this.oDiv.appendChild(this.oImg);
    this.oDiv.style.position = "absolute";
    this.oDiv.expander = this;
    this.oDiv.onclick = function() { this.expander.toggle(); };
    this.oImg.title = "Clique para reduzir...";
    this.bigWidth = this.oImg.width;
    this.bigHeight = this.oImg.height;
    if (this.bExpand) {
        this.expand();
    }
    else {
        this.oDiv.style.visibility = "hidden";
        this.oImg.style.visibility = "hidden";
    }
}
ImageExpander.prototype.toggle = function() {
    this.bExpand = !this.bExpand;
    if (this.bExpand) {
        for (var i in window.aImageExpanders)
            if (window.aImageExpanders[i] !== this)
            window.aImageExpanders[i].reduce();
    }
}
ImageExpander.prototype.expand = function() {
    this.bExpand = true;

    for (var i in window.aImageExpanders)
        if (window.aImageExpanders[i] !== this)
        window.aImageExpanders[i].reduce();

    if (!this.oDiv) return;

    this.oThumb.style.visibility = "hidden";

    this.x = this.oThumb.offsetLeft + 345;
    this.y = this.oThumb.offsetTop + 285;
    this.w = this.oThumb.clientWidth;
    this.h = this.oThumb.clientHeight;

    this.oDiv.style.zIndex = "999999";
    this.oDiv.style.left = this.x + "px";
    this.oDiv.style.top = this.y + "px";
    this.oImg.style.width = this.w + "px";
    this.oImg.style.height = this.h + "px";
    this.oDiv.style.visibility = "visible";
    this.oImg.style.visibility = "visible";

    if (!this.bTicks) {
        this.bTicks = true;
        var pThis = this;
        window.setTimeout(function() { pThis.tick(); }, 1);
    }
}
ImageExpander.prototype.reduce = function() {
    this.bExpand = false;
}
ImageExpander.prototype.tick = function() {
    var cw = document.body.clientWidth;
    var ch = document.body.clientHeight;
    var cx = document.body.scrollLeft + cw / 2;
    var cy = this.oThumb.offsetTop + 600;

    var tw, th, tx, ty;
    if (this.bExpand) {
        tw = this.bigWidth;
        th = this.bigHeight;
        if (tw > cw) {
            th *= cw / tw;
            tw = cw;
        }
        if (th > ch) {
            tw *= ch / th;
            th = ch;
        }
        tx = cx - tw / 2;
        ty = cy - th / 2;
    }
    else {
        tw = this.smallWidth;
        th = this.smallHeight;
        tx = this.oThumb.offsetLeft + 345;
        ty = this.oThumb.offsetTop + 285;
    }
    var nHit = 0;
    var fMove = function(n, tn) {
        var dn = tn - n;
        if (Math.abs(dn) < 3) {
            nHit++;
            return tn;
        }
        else {
            return n + dn / 10;
        }
    }
    this.x = fMove(this.x, tx);
    this.y = fMove(this.y, ty);
    this.w = fMove(this.w, tw);
    this.h = fMove(this.h, th);

    this.oDiv.style.left = this.x + "px";
    this.oDiv.style.top = this.y + "px";
    this.oImg.style.width = this.w + "px";
    this.oImg.style.height = this.h + "px";

    if (!this.bExpand && (nHit == 4)) {
        this.oImg.style.visibility = "hidden";
        this.oDiv.style.visibility = "hidden";
        this.oThumb.style.visibility = "visible";

        this.bTicks = false;
    }

    if (this.bTicks) {
        var pThis = this;
        window.setTimeout(function() { pThis.tick(); }, 1);
    }
}

