/*<RMJSDep>SharedServices\ClientScripts\BrowserUtils;SharedServices\ClientScripts\WindowUtils;SharedServices\ClientScripts\DisableEnterSubmit; SharedServices\ClientScripts\Mozilla;SharedServices\ClientScripts\RMWJSResource.js.aspx;SharedServices\ClientScripts\TextBox;SharedServices\ClientScripts\AsyncServices;</RMJSDep>*/

var DialogWidth = 788;
var DialogHeight = 505;

var RMWLoadingCaption;
var RMWLoadingImage;
var RMWResizeWindowCallBack;
var RMWMainFormSubmitCallBack;
var RMWHasTextBoxWithEmptyText = false;

var RMWShowMessageInModalPopup = false;

function OnLoad(Event)
{
}

function CallClientHostNameFunction()
{
  if(ClientHostNameFunction)
    ClientHostNameFunction();
}
function pageLoad(e){



  if (!e) var e = window.event;
	DoDisableEnterSubmit(e);	
	ConfigureMainFormSubmit();
	if(RMWHasTextBoxWithEmptyText)	 
	    EnsureEmptyTextFeatures();
	eval(OnLoadFunctions);
		
	WorkAroundForAspNetAjax();
  if (Sys.Browser.name != 'Microsoft Internet Explorer')//donotlocalize
  	ConfigureFirefoxResizeWorkAround();
	
	var loadingMessage = RM.Lib.WebForms.ModalLoading.ShowLoading(document.body, '');
	loadingMessage.dispose();
	
}

function pageUnload(){
  if (Sys.Browser.name != 'Microsoft Internet Explorer')//donotlocalize
  	$removeHandler(window, 'resize', RMWResizeWindowCallBack);//donotlocalize
}

function ChangeLocation(href, loadingMessage){
  ShowLoadingCaption(loadingMessage);
  window.location.href = href;
}

function ConfigureFirefoxResizeWorkAround(){
	var mainContainer = $get('MainContainer');//donotlocalize
	if (mainContainer)
	  RM.Lib.WebForms.DivOverflowUtils._mainContainerDiv = {overflowMode : RM.Lib.WebForms.DivOverflowMode.Both, element : mainContainer};

  RMWResizeWindowCallBack = Function.createCallback(RM.Lib.WebForms.DivOverflowUtils.EnsureManagedDivsSize, null);
  
  RM.Lib.WebForms.DivOverflowUtils._enableResizeWorkAround = true;

	window.resizeBy(1,1);
	$addHandler(window, 'resize', RMWResizeWindowCallBack);//donotlocalize
	window.resizeBy(-1,-1);
}

/* No dia 3/10/2007 foi detectado que o script da microsoft que gerencia as chamdas ajax
    gerava um Bug caso ocorresse uma chamada ao servidor dentro de um resultado de uma outra chamada ao servidor(Ajax)
    segue abaixo o código que substitui parte do script da microsoft corrigindo este problema.
 **********************************************************************************************/    
function WorkAroundForAspNetAjax()
{
    if( typeof(WebForm_CallbackComplete) == "function" )//donotlocalize
    {    
        WebForm_CallbackComplete = Override_WebForm_CallbackComplete;   
    }
}

function Override_WebForm_CallbackComplete() {
    for (i = 0; i < __pendingCallbacks.length; i++) {
        callbackObject = __pendingCallbacks[i];
        if (callbackObject && callbackObject.xmlRequest && (callbackObject.xmlRequest.readyState == 4)) {
            if (!__pendingCallbacks[i].async) {
                __synchronousCallBackIndex = -1;
            }
            __pendingCallbacks[i] = null;            
            var callbackFrameID = "__CALLBACKFRAME" + i;
            var xmlRequestFrame = document.getElementById(callbackFrameID);
            if (xmlRequestFrame) {
                xmlRequestFrame.parentNode.removeChild(xmlRequestFrame);
            }
            WebForm_ExecuteCallback(callbackObject);
        }
    }
}
/*********************************************************************************************/


function EnsureEmptyTextFeatures()
{
    if ((ServerLoadedControllerId != null) && (ServerLoadedControllerId != ''))
    {
        var _serverLoadedController = document.getElementById(ServerLoadedControllerId);
        
        if (_serverLoadedController != null)
        {
            if (_serverLoadedController.value.toString().toUpperCase() == 'FALSE')
            {
                RestoreInputEmptyTextFeatures();
                RestoreTextAreaEmptyTextFeatures();
            }
            else
            {
                _serverLoadedController.value = 'FALSE';
            }
        }
    }
}

function RestoreTextAreaEmptyTextFeatures()
{
    var textAreas = document.getElementsByTagName('textarea');
    for(i=0;i<textAreas.length;i++)
    {
        var textAreaId = textAreas[i].id;
        
        if ((textAreaId != null) && (textAreaId != ''))
        {
            RestoreRMWTextBoxEmptyTextFeatures(textAreaId);
        }
    }
}

function RestoreInputEmptyTextFeatures()
{
    var inputs = document.getElementsByTagName('input');
    for(i=0;i<inputs.length;i++)
    {
        var currentInput = inputs[i];
        
        if (currentInput.type.toString().toUpperCase() == 'TEXT')
        {
            var inputId = currentInput.id;
            
            if ((inputId != null) && (inputId != ''))
            {
                RestoreRMWTextBoxEmptyTextFeatures(inputId);
            }
        }
    }
}

function RestoreRMWTextBoxEmptyTextFeatures(controlId)
{
    var _rmwTextBox = new RMWTextBox(controlId);
    if (_rmwTextBox != null) 
        if(!_rmwTextBox.getShowingEmptyText())
            _rmwTextBox.clearEmptyTextStyle(true);
        else
            _rmwTextBox.restoreEmptyTextStyle(true);
}

//Método específico para o FireFox
//Quando faz history.back, o OnLoad não é disparado e sim este método
function OnPageShow(persisted)
{
    if (persisted)
        HideLoadingCaption();
}

function ConfigureMainFormSubmit()
{
  $addOnsubmitHanlder(MainFormSubmit);
}

window.detailsWindowByTransactionId = [];

function ClearDetailTransaction(TransactionId){
  if (window.detailsWindowByTransactionId &&
      window.detailsWindowByTransactionId[TransactionId] &&
      window.detailsWindowByTransactionId[TransactionId].closed)
  {
    tc_submiting = false;
    var xmlHttp = GetXmlHttp();
    var asyncResponsePageUrl = GetAsyncResponserPageUrl();

    xmlHttp.open("GET",asyncResponsePageUrl + "?ServiceId=ClearTransaction&TransactionId=" + TransactionId, true);
    xmlHttp.send(null);
  }
}

function SetupLicenceTimer() {
    window.setInterval("RenewLicence('" + LoginId + "');", TimerInterval);
}

function RenewLicence(LoginId) {
  var xmlHttp = GetXmlHttp();
  var asyncResponsePageUrl = GetAsyncResponserPageUrl();

  xmlHttp.open("GET", asyncResponsePageUrl + "?ServiceId=RenewLicence&LoginId=" + LoginId + "&RandomId=" + Math.random(), true);
  xmlHttp.send(null);
}

var ShowLoadingMessageOnSubmit = true;
function MainFormSubmit()
{
    var internalPageIsValid = true;
    if ((typeof(Page_IsValid) != null) && (typeof(Page_IsValid) != "undefined"))//donotlocalize
        eval("internalPageIsValid = Page_IsValid;")//donotlocalize
    
    if (internalPageIsValid && ShowLoadingMessageOnSubmit)
	    ShowLoadingCaption(SUBMIT_LOADING_MESSAGE);

	if (OnSubmitFunctions!=null)
		eval(OnSubmitFunctions);
}

var $LoadingPanel = null;
function ShowLoadingCaption(loadingMessage)
{
    $LoadingPanel = RM.Lib.WebForms.ModalLoading.ShowLoading(document.body, loadingMessage);
}

function HasPreviousPage()
{
    var noHistoryLength = (isIE()) ? 0 : 1;
    
    return (history.length > noHistoryLength);        
}

function HideLoadingCaption()
{
}

function SetBrowserCSS()
{
	if (isIE())
	{
		var NSCSSConst = "NS.CSS";
		var IECSSConst = "IE.CSS";
		
		for(var i=0; i<document.styleSheets.length; i++)
		{
			var styleSheet = document.styleSheets[i];
			if ((styleSheet.href!=null) &&
				(styleSheet.href.toUpperCase().indexOf(NSCSSConst)!=-1))
				styleSheet.href = styleSheet.href.toUpperCase().replace(NSCSSConst,IECSSConst);
		}
	}
}

function ConfigureWindowOnLoad()
{
}

function OpenEditPageWindow(ShowingDetail, Url) {
    //GetCenterPositionLeft
    var Features = 'height=' + DialogHeight + ', width=' + DialogWidth + ', left=' + GetCenterPositionLeft(DialogWidth + 8) + ' , top=' + GetCenterPositionTop(DialogHeight + 100) + ', resizable=yes, status=yes'; //donotlocalize
    if (ShowingDetail == "TRUE")
        ShowModalWindow(Url, '', Features, false);
    else
        OpenWindow(Url, '', Features);
}

function OpenWindowTargetBlank(Url) {
    var Features = 'height=' + DialogHeight + ', width=' + DialogWidth + ', left=' +
        GetCenterPositionLeft(DialogWidth + 8) + ' , top=' + GetCenterPositionTop(DialogHeight + 100) +
        ', resizable=yes, status=yes, scrollbars=yes'; //donotlocalize
    OpenWindow(Url, '', Features);
}

function ShowAlertMessage(Message)
{
    if(RMWShowMessageInModalPopup)
        return $ShowModalMessageBox(RM.Lib.WebForms.ModalPanelWindowType.Alert, Message)
    else
    {
        alert(Message);
        return null;
    }
}

function HasTextBoxWithEmptyText()
{
  RMWHasTextBoxWithEmptyText = true;
}

