var list = new Array();
		
function addList(id, obj)
{
    var exist = false;
            
    for(var x = 0; x < list.length; x++)
    {        
        if(list[x].split(',')[0] == id)
        {
            list[x] = id+","+obj.value;
            exist = true;
            break;
        }       
    }
    if(!exist)    
        list[list.length] = id+","+obj.value;
}
    
function parseElementsForm4FieldHidden(elementName,hiddenName)
{
    var field = "";
    var hours = "";
    var hidden = null;
    for (var i = 0; i < document.getElementById("formActivity").elements.length; i++)
    {
        field = document.getElementById("formActivity").elements[i].name;
        if(field.indexOf(elementName) != -1)
            hours += "-"+field.split("_")[1]+","+document.getElementById("formActivity").elements[i].value;
        if(field.indexOf(hiddenName) != -1) 
            hidden = document.getElementById("formActivity").elements[i];
    }
    if(hours.length > 2)       
        hours = hours.substring(1);
    if(hidden != null)
        hidden.value = hours;
}


function resizeiFrame(iframe, addSize)
{
    if(addSize == null || addSize == "")
        addSize = 0;
    if(navigator.appName.indexOf("Internet Explorer")>-1)
    {
            var func_temp = function()
            {
                try
                {
                    var val_temp = iframe.contentWindow.document.body.scrollHeight + 15;
                    iframe.style.height = val_temp+addSize + "px";
                }
                catch(err)
                {
                    //alert(err)
                }
            };
            setTimeout(function() {func_temp();},100);     
    }
    else
    {
        var val = iframe.contentWindow.document.body.parentNode.offsetHeight + 15;
        iframe.style.height= val+addSize + "px";
    }

}

function adjustPositionInternModal(item)
{
    var widthVisibility, leftModal, eleModal;
    widthVisibility = getWidthVisibilityArea();
    eleModal = document.getElementById(item);
    eleModal.width = "348px";
    eleModal.height = document.getElementById(item).clientHeight;   
    leftModal = (widthVisibility-348)/2;
    eleModal.style.left = leftModal + "px";
    if(is.IE){
        if(getDiffScroll()>0){
            eleModal.style.top = getPositionHeightScroll() + ((getHeightVisibilityArea() - getPositionHeightScroll())/2) - (eleModal.height/2) + "px";
        }

        else {
            eleModal.style.top = getHeightVisibilityArea() - (getHeightVisibilityArea()/2) + (eleModal.height/2) + "px";
        }
    }
    else {
        if(getDiffScroll()>0){

                eleModal.style.top = getPositionHeightScroll() + ((getHeightVisibilityArea() - getPositionHeightScroll())/2) - (eleModal.height/2) + "px";
        }
        else {
                eleModal.style.top = getHeightVisibilityArea() - (getHeightVisibilityArea()/2) + (eleModal.height/2) + "px";
        }
    }
}


function removeModalPanelTopFixedForIE6(obj) {
    // Se for IE6
    if(jQuery.browser.msie && jQuery.browser.version < 7) {
        var id = obj.id.substring(0, obj.id.indexOf("Container"));
        jQuery(obj).find("#" + id + "CDiv")[0].style.removeExpression("top");
    }
}

/**
 * Ajusta o posicionamento dos modais do template-client-modal
 */
function adjustPositionModal(item)
{
    var widthVisibility, leftModal, eleModal;
    widthVisibility = getWidthVisibilityArea();
    eleModal = document.getElementById(item);
    eleModal.width = "648px";
    leftModal = (widthVisibility-648)/2;
    eleModal.style.left = leftModal + "px";

    if(is.IE) {
        //getDiffScroll()= retorna a diferença da área visivel com o tamanho da rolagem. quando vem 0 é pq não tem rolagem.
        if(getDiffScroll()>0) {
            eleModal.style.top = (getPositionHeightScroll()+10) + "px";
        } else {
            eleModal.style.top = "10px";
        }
    } else {
        if(getDiffScroll()>0) {
            eleModal.style.top = (getPositionHeightScroll()+10) + "px";
        } else {
            eleModal.style.top = "10px";
        }
    }
}

function adjustPositionModalOna(item, width)
{
    adjustPositionModalOna(item, width, 10);
}

function adjustPositionModalOna(eleModal, width, top)
{
    var widthVisibility, leftModal, eleModal;   
    widthVisibility = getWidthVisibilityArea();
    eleModal.width = width+"px";
    leftModal = (widthVisibility-width)/2;
    //eleModal.style.left = leftModal + "px";
    
    if(is.IE)
    {
        if(getDiffScroll()>0) //getDiffScroll()= retorna a diferença da área visivel com o tamanho da rolagem. quando vem 0 é pq não tem rolagem.
        {
            eleModal.style.top = (getPositionHeightScroll()+top) + "px";
        }
        else
        {
            eleModal.style.top = top+"px";
        }
    }
    else
    {
        if(getDiffScroll()>0)
        {
            eleModal.style.top = (getPositionHeightScroll()+top) + "px";
        }

        else
        {
            eleModal.style.top = top+"px";
        }
    }
}

function getWidthVisibilityArea()
{
	return parseInt(getVisibilityArea("width"));
}

function getHeightVisibilityArea()
{
	return parseInt(getVisibilityArea("height"));
}

function getVisibilityArea(type)
{
	var myWidth = 0;
	var myHeight = 0;

  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;

    if(is.NS)// firefox area visivel + diferença do scroll "window.scrollMaxX"
    {
     	myWidth = myWidth + window.scrollMaxX;
    }

  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;

  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
    if(type == 'width')
		return myWidth;

	if(type == 'height')
		return myHeight;

    return null;
}

function getDiffScroll()
{
    var diff, heightBody, scroll;
	heightBody = document.body.clientHeight;
	
	if(is.IE)//IE
	{
		heightBody = document.body.clientHeight;
        scroll = document.documentElement.clientHeight;
        if(heightBody <= scroll)
            diff = 0;
        else
            diff = heightBody - scroll;
	}
	else // firefox
	{
        heightBody = document.body.clientHeight;
        scroll = window.innerHeight;
        if(heightBody <= scroll)
            diff = 0;
        else
            diff = heightBody - scroll;
	}

    return diff;
}

function getPositionHeightScroll()
{
    //var hscroll = (document.all ? document.scrollLeft : window.pageXOffset); // pega a posição da scroll horizontal no FF
    var positionHeightScroll;

    if(is.IE)// IE
      {
        positionHeightScroll = document.documentElement.scrollTop; // pega a posição da scroll vertical no IE

      }
     else//Firefox
      {
        positionHeightScroll = (document.all ? document.scrollTop : window.pageYOffset); // pega a posição da scroll vertical no FF
      }

     return positionHeightScroll;
}

function BrowserCheck() {
    var b = navigator.userAgent;
    if (b.indexOf('Firefox')>0) this.b = "NS";
    else if (b.indexOf('MSIE')>0) this.b = "IE";
    else if (b == "Chrome") this.b = "CH";
    else this.b = b;
    this.v = parseInt(navigator.appVersion);
    this.NS = (this.b == "NS" && this.v>=4);
    this.NS4 = (this.b == "NS" && this.v == 4);
    this.NS5 = (this.b == "NS" && this.v == 5);
    this.IE = (this.b == "IE" && this.v>=4);
    this.IE7 = (navigator.userAgent.indexOf('MSIE 7')>0);
    this.IE8 = (navigator.userAgent.indexOf('MSIE 8')>0);
    this.IE9 = (navigator.userAgent.indexOf('MSIE 9')>0);
    this.NS6 = (navigator.userAgent.indexOf('Firefox/6')>0);
    this.NS7 = (navigator.userAgent.indexOf('Firefox/7')>0);
    this.NS8 = (navigator.userAgent.indexOf('Firefox/8')>0);
    this.NS9 = (navigator.userAgent.indexOf('Firefox/9')>0);
    this.CH12 =(navigator.userAgent.indexOf('Chrome/12')> -1);
    this.CH13 =(navigator.userAgent.indexOf('Chrome/13')> -1);
    this.CH14 =(navigator.userAgent.indexOf('Chrome/14')> -1);
    this.CH15 =(navigator.userAgent.indexOf('Chrome/15')> -1);
    this.CH16 =(navigator.userAgent.indexOf('Chrome/16')> -1);
    if (this.IE5 || this.NS5) this.VER5 = true;
    if (this.IE4 || this.NS4) this.VER4 = true;
    this.OLD = (! this.VER5 && ! this.VER4) ? true : false;
    this.min = (this.NS||this.IE);
}
var is = new BrowserCheck();

function getElementsByClassName(strTagName, strClassName)
{
	var currentElements = (strTagName == "*" && document.all)? document.all : document.getElementsByTagName(strTagName);
	var findedElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<currentElements.length; i++)
	{
	    oElement = currentElements[i];
        if(oRegExp.test(oElement.className)) {
            findedElements.push(oElement);
        }
    }

	return findedElements;
}

function pageLoadPortal()
{
    // Adiciona a função no evento onfocus() de todos os input type text
    gwaHTMLEventManager.addEventByTag("input", "text", "focus", "setFocus(this);");

    // Adiciona a função no evento onblur() de todos os input type text
    gwaHTMLEventManager.addEventByTag("input", "text", "blur", "setBlur(this);");

    // Adiciona a função no evento onfocus() de todos os input type password
    gwaHTMLEventManager.addEventByTag("input", "password", "focus", "setFocus(this);");

    // Adiciona a função no evento onblur() de todos os input type password
    gwaHTMLEventManager.addEventByTag("input", "password", "blur", "setBlur(this);");

    // Adiciona a função no evento onblur() de todos os textArea
    gwaHTMLEventManager.addEventByTag("textarea", "textarea", "focus", "setFocus(this);");

    // Adiciona a função no evento onblur() de todos os textArea
    gwaHTMLEventManager.addEventByTag("textarea", "textarea", "blur", "setBlur(this);");

    // Encaminha para uma página de erro quando o browser não for suportado
    if(!is.IE7 && !is.IE8 && !is.IE9 && !is.NS6 && !is.NS7 && !is.NS8 && !is.NS9 && !is.CH12 && !is.CH13 && !is.CH14 && !is.CH15 && !is.CH16) { 
        if(location.href.indexOf("error_browserNotSupported")==-1 && typeof(requestContextPath) != "undefined")
            location.href = requestContextPath+"/resources/errors/error_browserNotSupported.jsf";
    }
}

function setFocus(element)
{
	element.className = element.className + " pg_inputFocus";
}

function setBlur(element)
{
    var className = element.className;
    var index = className.indexOf(" pg_inputFocus");
    element.className = className.substring(0, index);
}

function changeFontSize(elementId, resizeTo) {
    gwaUtility.changeFontSize(elementId, resizeTo);
}

function focusMessagesSystem()
{
    //Qualquer alteração no modelo de apresentação das mensagens de retorno do framework,
    //devem ser analisadas e motidicado a classe e a tag HTML utilizada.
    var d = getElementsByClassName("span","rich-messages-label");
    for(var i=0; i<d.length; i++) {
        var element = d[i];
        var x = findPosX(element);
        var y = findPosY(element);
        if(x!=-1 && y!=-1 && x!=0 && y!=0) {
            //alert("Indice: "+ i + "\n" + "Final: "+"\n"+ "X: " + x + "\n" + "Y: " + y);
            window.scrollTo(x, y-5);
            break;
        }
    }
}

function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1)
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

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;
}

function changeBGColor(obj, newColor) {
    obj.style.backgroundColor = newColor;
}

// ============================================================================

var showMessagesBlock = false;

function setShowMessagesBlock(value) {
    showMessagesBlock = value;
}

function toogleMessagesLogin(value) {
    var portalLogin = document.getElementById("messagesLogin");
    if(portalLogin!=null && portalLogin!=undefined) {
        portalLogin.style.display = value;
    }
    var mercatusLogin = document.getElementById("mercatusMessagesLogin");
    if(mercatusLogin!=null && mercatusLogin!=undefined) {
        mercatusLogin.style.display = value;
    }
}

function toogleMessagesOther(value) {
    var messagesGeneric = document.getElementById("messagesGeneric");
    if(messagesGeneric!=null && messagesGeneric!=undefined) {
        messagesGeneric.style.display = value;
    }
}

function processMessages() {
    if(showMessagesBlock) {
        toogleMessagesLogin("none");
        toogleMessagesOther("block");
    } else {
        toogleMessagesLogin("block");
        toogleMessagesOther("none");
    }
}

function TextCommentsValid(e){
	var key;
	if(window.event)
		key = e.keyCode;
	else 
		key = e.which;
	
	if(key == 47 || key == 92 || key == 124)
		return false;
	else 
		return true;
}


function transformWordLowerCase(e,element){
	element.value = element.value.toLowerCase();
}

