/** generic routines **/

function getPageCoords (element) 
{
    var coords = {x: 0, y: 0};
    
	while (element) {
		coords.x += element.offsetLeft;
		coords.y += element.offsetTop;
		element = element.offsetParent;
    }

    return coords;
}

function setEventHandler(element, evt, code)
{
    if( element.addEventListener ) {
	element.addEventListener( evt, code, true );
    } else if( element.attachEvent ) {
	element.attachEvent( "on" + evt, code );
    } else {
	/** this part is hard-coded for obvious reasons **/
	switch( evt ) {
	    case	"click"	    :
		element.onclick = code;
		break;

	    case	"change"    :
		element.onchange = code;
		break;

	    case	"unload"    :
		element.onunload = code;
		break;
	}
    }
}

function onPrint()
{
    window.print();
}

function onSendto()
{
    var link = document.title + "\n" + document.location.href;
    document.location = "mailto:?body=" + escape(link);
}

var zoomers = new Array();
var handler = false;

function onWindowResize()
{
    for( var j = 0; j < zoomers.length; j ++ ) {
	xy = getPageCoords( zoomers[ j ].p );

	if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
	    xy.x += 12;
	    xy.y += 12;
	}

	zoomers[ j ].i.style.left = xy.x + "px";
	zoomers[ j ].i.style.top = xy.y + "px";
    }
}

function zoomer(txt)
{
    var imgs = document.getElementsByTagName( "IMG" );

    for( var j = 0; j < imgs.length; j ++ ) {
	if( imgs[ j ].getAttribute( "zoom" ) == "yes" ) {
	    var zimg = document.createElement( "IMG" );
	    zimg.src = "/layout/zoom.gif";
	    zimg.title = txt;
	    zimg.alt = txt;
	    zimg.border = 0;
	    zimg.style.position = "absolute";
	    zimg.style.border = "none";
	    zimg.style.margin = "0px";
	    zimg.width = 20;
	    zimg.height = 20;
	    var xy = getPageCoords( imgs[ j ] );

	    if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
		xy.x += 12;
		xy.y += 12;
	    }

	    zimg.style.left = xy.x + "px";
	    zimg.style.top = xy.y + "px";
	    zimg.style.width = "20px";
	    zimg.style.height = "20px";
	    var pn;
	    if( imgs[ j ].parentNode !== undefined )
		pn = imgs[ j ].parentNode;
	    else if( imgs[ j ].parentElement !== undefined )
		pn = imgs[ j ].parentElement;

	    pn.appendChild( zimg );

	    zoomers[ zoomers.length ] = {
		i : zimg,
		p : imgs[ j ]
	    };
	}
    }

    if( !handler ) {
	setEventHandler( window, "resize", onWindowResize );
	handler = true;
    }
}

function onFirmChange(sb)
{
    var co = sb.options[ sb.selectedIndex ].value;
    if( co != 0 ) {
	document.location = "/" + co + ",Firmatario.html";
    }
}

function getDocMetrics(){
    var metrics = {
        offX : 0,
        offY : 0,
        width : 0,
        height: 0
    };

    if( window.pageXOffset !== undefined ) {
        metrics.offX = window.pageXOffset;
    } else if( document.documentElement.scrollLeft !== undefined ) {
        metrics.offX = document.documentElement.scrollLeft;
    } else if( document.body.scrollLeft !== undefined ) {
        metrics.offX = document.body.scrollLeft;
    }

    if( window.pageYOffset !== undefined ) {
        metrics.offY = window.pageYOffset;
    } else if( document.documentElement.scrollTop !== undefined ) {
        metrics.offY = document.documentElement.scrollTop;
    } else if( document.body.scrollTop !== undefined ) {
        metrics.offY = document.body.scrollTop;
    }

    if( window.innerWidth ) {
        metrics.width = window.innerWidth ;
        metrics.height = window.innerHeight;
    } else if( document.documentElement.clientWidth ) {
        metrics.width = document.documentElement.clientWidth;
        metrics.height = document.documentElement.clientHeight;
    } else if( document.body.clientWidth ) {
        metrics.width = document.body.clientWidth;
        metrics.height = document.body.clientHeight;
    }

    return metrics;
}


