﻿// JScript File

function OnPageError()
{
    AJAXModalMessageobj = new AJAXModalMessage('Erro' , 400 , 400 , this , '' , 'Modal');	// We only create one object of this class
    AJAXModalMessageobj.displayStaticMessage();
}

var newwindow = '';
var isSaveAsReady = false;

window.onerror = OnPageError;
window.onerror = function (sMessage, sUrl, sLine) 
{
    //alert("An error occurred:\n" + sMessage + sLine);
    alert("An error occurred in " + sUrl + ":\n" + sMessage + sLine);
    return true;
};

var tt_flagOpacity; // Opacity support: 1=IE, 2=Khtml, 3=KHTML, 4=Moz, 5=W3C
var tt_u = "undefined";

function get_OpaSupport()
{
    tt_body = (document.getElementsByTagName ? document.getElementsByTagName("body")[0]
				: (document.body || null));
	var css = tt_body.style;
	tt_flagOpacity = (typeof(css.filter) != tt_u) ? 1
				: (typeof(css.KhtmlOpacity) != tt_u) ? 2
				: (typeof(css.KHTMLOpacity) != tt_u) ? 3
				: (typeof(css.MozOpacity) != tt_u) ? 4
				: (typeof(css.opacity) != tt_u) ? 5
				: 0;
}

openWindowURL = function (url)
{
	var  res = popitup(url , "MetadataURL" , "width=800, height=600 , dependent=1, alwaysRaised=1, location=0, titlebar=0, status=0, menubar=0, toolbar=0, resizable=1, scrollbars=1");
	return false;
}

popitup = function (url , name , params)
{
	if (!newwindow.closed && newwindow.location)
	{
		newwindow.location.href = url;
	}
	else
	{
		newwindow = window.open(url , name , params);
		if (!newwindow.opener) 
		{   
		    newwindow.opener = self;
		}
	}
	if (window.focus) 
	{
	    newwindow.focus();
    }
	return newwindow;
};

centerTheWindow = function (theWindow)
{
    var oCoords;    //ALTERADO PARA FUNCIONAR NO IE7 
    oCoords = getWindowCenterPosition((document.documentElement.clientWidth ? 
                                       document.documentElement.clientWidth : document.body.clientWidth),
                                      (document.documentElement.clientHeight ? 
                                       document.documentElement.clientHeight : document.body.clientHeight));
//    oCoords = getWindowCenterPosition(theWindow.document.body.clientWidth , theWindow.document.body.clientHeight);
    theWindow.moveTo(oCoords.left , oCoords.top);
};

getWindowCenterPosition = function (theWidth, theHeight)
{
    var oCoord = new Object();
    var xMax;
    var yMax;
    xMax = screen.availWidth;
    yMax = screen.availHeight;   
    oCoord.left = (xMax / 2) - (theWidth / 2);
    oCoord.top = (yMax / 2) - (theHeight / 2);
    return oCoord;
};

function togglePrintDiv(div , classNamePrint , classNameNoPrint)
{
    var el = document.body.getElementsByTagName('div');
    for (i = 0 ; i < el.length ; i++)
	{
	    if (el[i].className.indexOf("section") != -1)
		{
		    el[i].removeAttribute("className");
		    if (el[i].id == document.getElementById(div).id)
			{
			    //show only this section for print
			    el[i].setAttribute("className" , classNamePrint);
			    el[i].setAttribute("class" , classNamePrint);
			}
		    else
			{
			    //hide the sections from print-out
			    el[i].setAttribute("className" , classNameNoPrint);
			    el[i].setAttribute("class" , classNameNoPrint);
			}
		}
	}
}

function togglePrintIFrame(div , classNamePrint , classNameNoPrint)
{  
    var el = document.body.getElementsByTagName('div');
    for (i = 0 ; i < el.length ; i++)
    {
	    if (el[i].className.indexOf("section") != -1)
		{
            //hide the sections from print-out
			el[i].setAttribute("className" , classNameNoPrint);
			el[i].setAttribute("class" , classNameNoPrint);
        }
    }
}

function printAll(classNamePrint)
{
    var el = document.getElementsByTagName("div");
    for (i = 0 ; i < el.length ; i++)
    {
	    if (el[i].className.indexOf("print_section") != -1)
		{
    	    el[i].setAttribute("className" , classNamePrint);
		    el[i].setAttribute("class" , classNamePrint);
        }
    }
}

sendMailTo = function (EMailAdd) 
{
    locationstring = 'mai' + 'lto:' + EMailAdd;
    window.location.replace(locationstring);
};

ShowHide = function  (elem , elemtohide , footer , imageup , imagedown)
{
    var domElemToHide = document.getElementById(elemtohide);
    if (typeof domElemToHide.direction != 'undefined')
    {
        if (domElemToHide.direction == 'up')
        {
            domElemToHide.direction = 'down';
        }        
        else
        {
            domElemToHide.direction = 'up';
        }                    
    }
    else
    {
        domElemToHide.direction = 'up';
    }        
    ShowHideClient(elem , elemtohide , footer , imageup , imagedown);
};

ShowHideClient = function  (elem , elemtohide , footer , imageup , imagedown)
{
    var domElem = document.getElementById(elem);
    var domElemToHide = document.getElementById(elemtohide);
    var domFooterElemToHide = document.getElementById(footer);
    var height = findPosY(domFooterElemToHide) - findPosY(domElemToHide);
    if (domElemToHide.direction == 'up')
    {
        setTimeout('setHeight(' + "'" + elemtohide + "' , '" + height + "' , '"  + "up" + "')" , 100);
        //domElem.src = imagedown;
        domElem.className = imagedown;
    }
    else
    {
        setTimeout('setHeight(' + "'" + elemtohide + "' , '" + height + "' , '"  + "down" + "')" , 100);
        //domElem.src = imageup;
        domElem.className = imageup;
    }
};

Disable = function  (elem)
{
    var domElem = document.getElementById(elem);
    document.getElementById(elem).style.visibility = "hidden";
};

Enable = function  (elem)
{
    document.getElementById(elem).style.visibility = "visible";
};

FadeEnable = function  (elem, value)
{
    setTimeout('Enable(' + "'" + elem + "')", value * 1000);
};

FadeMessage = function  (elem , blink , value)
{
    if (blink == "True")
    {
        document.getElementById(elem).style.textDecoration = "blink";
        document.getElementById(elem).style.Forecolor = "red";
    }
    // Alteracao ER - 16022009
    if(document.getElementById(elem))
    {
        var innerTimeOut = setTimeout('fadeOut(' + "'" + elem + "')" , value * 1000);
        return innerTimeOut;
    }
    //else
    //    HideOut(elem);
    
    //var innerTimeOut = setTimeout('fadeOut(' + "'" + elem + "')" , value * 1000);
    //return innerTimeOut;
};

FadeMessageOnLoad = function  (elem , blink , value)
{
    addDOMLoadEvent(function ()
    {
        FadeMessage(elem, blink, value);
    });
};

FadeMessageOnLoadModal = function(msg , header) {
    addDOMLoadEvent(function() {
        AJAXModalMessageobj = new AJAXModalMessage(msg, 400, 400, this, header , 'Modal'); // We only create one object of this class
        AJAXModalMessageobj.displayStaticMessage();
    });
};
  
fadeOut = function (elem) 
{
    get_OpaSupport();
    for (var i = 0 ; i <= 100 ; i++) 
    {
        setTimeout('setOpacity(' + "'" + elem + "' , " + (10 - i / 10) + ')' , 8 * i);
    }
    setTimeout('HideOut(' + "'" + elem + "'" + ')' , 810);
};

fadeIn = function (elem) 
{
    get_OpaSupport();
    setTimeout('ShowOut(' + "'" + elem + "'" + ')' , 0);
    for (var i = 0 ; i <= 100 ; i++)
    {
        setTimeout('setOpacity(' + "'" + elem + "'," + (i / 10) + ')' , 8 * i);
    }
};

HideOut = function (elem)
{
    // Alteracao ER - 16022009
    if(document.getElementById(elem) != null)
    {
        document.getElementById(elem).style.display = "none";
        document.getElementById(elem).position = "";
    }

    //document.getElementById(elem).style.display = "none";
    //document.getElementById(elem).position = "";
};

ShowOut = function (elem)
{
    document.getElementById(elem).style.display = "";
};

setHeight = function (elem , height , direction) 
{
    get_OpaSupport();
    var jheight = (height / 10);
    for (var i = 0 ; i <= height ; i++) 
    {
        if (direction == 'up')
        {
            setTimeout('setElemHeight(' + "'" + elem + "' , " + (height - i) + ')' , 5 * i);
            //setTimeout( 'setOpacity(' + "'" + elem + "'," + (10 - (i / jheight)) + ')' , 3 * i );
        }
        else
        {
            setTimeout('setElemHeight(' + "'" + elem + "' , " + i + ')' , 5 * i);
            //setTimeout( 'setOpacity(' + "'" + elem + "'," + (i / jheight) + ')' , 3 * i );
        }        
    }
};

function TipandBlink(ContentArea , msg) 
{
    document.getElementById(ContentArea).style.textDecoration = "blink underline";
    document.getElementById(ContentArea).style.Forecolor = "red";
    showTip(ContentArea , msg);
}

function showTip(elem , tip) 
{
    var my_other_tooltip = new ToolTipT(elem, tip);
}

function TipC(elem, tip) {
    var my_other_tooltip = new ToolTipT(elem, tip);
    document.getElementById(elem).style.cursor = "pointer";
}

function setOpacity(ContentArea , value) 
{
	if (tt_flagOpacity == 1)
	{
		// Hack for bugs of IE:
		// A DIV cannot be made visible in a single step if an opacity < 100
		// has been applied while the DIV was hidden.
		// Moreover, in IE6, applying an opacity < 100 has no effect if the
		// concerned element has no layout (position, size, zoom, ...).
		if (value < 100)
		{
			var bVis = document.getElementById(ContentArea).style.visibility != "hidden";
			document.getElementById(ContentArea).style.zoom = "100%";
			if (!bVis)
			{
				document.getElementById(ContentArea).style.visibility = "visible";
            }
			document.getElementById(ContentArea).style.filter = "alpha(opacity=" + (value * 10) + ")";
			if (!bVis)
			{
				document.getElementById(ContentArea).style.visibility = "hidden";
            }
		}
		else
        {
			document.getElementById(ContentArea).style.filter = "";
        }
	}
	else
	{
		//value /= 100.0;
		switch (tt_flagOpacity)
		{
		case 2:
			document.getElementById(ContentArea).style.KhtmlOpacity = value / 10; 
			break;
		case 3:
			document.getElementById(ContentArea).style.KHTMLOpacity = value / 10; 
			break;
		case 4:
			document.getElementById(ContentArea).style.MozOpacity = value / 10; 
			break;
		case 5:
            document.getElementById(ContentArea).style.opacity = value / 10; 
            break;
		}
	}
}

setElemHeight = function (ContentArea , value) 
{
    document.getElementById(ContentArea).style.height = value + 'px'; 
};

SwitchView  = function (ControlToShow , ControlToHide) 
{
    setOpacity(ControlToShow , 0);
    setTimeout('fadeOut(' + "'" + ControlToHide + "')" , 0);
    setTimeout('HideOut(' + "'" + ControlToHide + "')" , 820);
    setTimeout('fadeIn(' + "'" + ControlToShow + "')" , 820);
    setTimeout('ShowOut(' + "'" + ControlToShow + "')" , 820);
};

addDOMLoadEvent = (function ()
{
    var load_events = [];
    var load_timer;
    var script;
    var done;
    var exec;
    var old_onload;
    init = function () 
    {
        done = true;
        // kill the timer
        clearInterval(load_timer);
        // execute each function in the stack in the order they were added
        while (exec = load_events.shift())
        {
            exec();
        }
        if (script) 
        {
            script.onreadystatechange = '';
        }
    };

    return function (func) 
    {
        // if the init function was already ran, just run this function now and stop
        if (done) 
        {
            return func();
        }
        if (!load_events[0]) 
        {
            // for Mozilla/Opera9
            if (document.addEventListener)
            {
                document.addEventListener("DOMContentLoaded", init, false);
            }
            // for Internet Explorer (using conditional comments)
            //  /*@cc_on @*/
            //  /*@if (@_win32)
            //      document.write('<script id=__ie_onload defer src=javascript:void(0)><\/script>');
            //      var script = document.getElementById('__ie_onload');
            //      script.onreadystatechange = function() {
            //      if (this.readyState == 'complete' ) {
            //      init(); // call the onload handler
            //      }
            // };
            // /*@end @*/

            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) 
            { // sniff
                load_timer = setInterval(function () 
                {
                    if (/loaded|complete/.test(document.readyState))
                    {
                        init(); // call the onload handler
                    }
                }, 10);
            }

            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function () 
            {
                init();
                if (old_onload) 
                {
                    old_onload();
                }
            };
        }
        load_events.push(func);
    };
})();

function IsIE()
{
    return (navigator.appName.toUpperCase() == "MICROSOFT INTERNET EXPLORER");
}

function PrintThisPage(DivLayer) 
{ 
    DivLayer.focus();
	DivLayer.print();
} 

function getIframeID(el)
{
    var myTop;
    if (window.frameElement) 
    {
        myTop = window.frameElement;
    } 
    else if (window.top) 
    {
        myTop = window.top;
        var myURL = location.href;
        var iFs = myTop.document.getElementsByTagName('iframe');
        var x, i = iFs.length;
        while (i--)
        {
            x = iFs[i];
            if (x.src && x.src == myURL)
            {
                myTop = x;
                break;
            }
        }
    }
    if (myTop)
    {
        return 'The iframe ' + ((myTop.id)? 'has ID=' + myTop.id : 'is anonymous');
    } 
    else 
    {
        return 'Couldn\'t find the iframe';
    }
}

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (1)
        {
            curtop += obj.offsetTop;
            if (!obj.offsetParent)
            {
                break;
            }
            obj = obj.offsetParent;
        }
    }
    else 
    {
        if (obj.y)
        {
            curtop += obj.y;
        }
    }
    return curtop;
}



