﻿function disableAllGrupos()
{
    // Procurar por todas as divs que tenham no nome do ID "GrupoExclusivo__"
    var divsGrupoExclusivo = $$("div.clsGrupoExclusivoDisabled");

    // Colocar todos os campos a disabled
    divsGrupoExclusivo.each(function(div)
    {
        // Inputs
        var inputListToDisable = div.getElementsByTagName('input');
        var inputsToDisable = $A(inputListToDisable);
	     
        inputsToDisable.each(function(node)
        {            
            node.disabled = true;
            if(node.type != "checkbox")
            {
                node.className = "clsInputDisabled";
            }
            else
            {
                node.className = "clsCheckDisabled";
            }
        });
        
        // Textareas
        inputListToDisable = div.getElementsByTagName('textarea');
        inputsToDisable = $A(inputListToDisable);
	     
        inputsToDisable.each(function(node)
        {
            node.disabled = true;
            node.className = "clsInputDisabled";
        });
        
        // Selects
        inputListToDisable = div.getElementsByTagName('select');
        inputsToDisable = $A(inputListToDisable);
	     
        inputsToDisable.each(function(node)
        {
            node.disabled = true;
            node.className = "clsInputDisabled";
        });
    });
}

function disableAllCampos()
{
    // Procurar por todas as divs que tenham no nome do ID "CampoExclusivo__"
    var divsCampoExclusivo = $$("div.clsCampoExclusivoDisabled");

    // Colocar todos os campos a disabled
    divsCampoExclusivo.each(function(div)
    {
        // Inputs
        var inputListToDisable = div.getElementsByTagName('input');
        var inputsToDisable = $A(inputListToDisable);
	     
        inputsToDisable.each(function(node)
        {            
            if(node.type != "checkbox")
            {
                node.disabled = true;
                node.className = "clsInputDisabled";
            }
            else
            {
                node.readonly = true;
                node.className = "clsCheckDisabled";
            }
        });
        
        // Textareas
        inputListToDisable = div.getElementsByTagName('textarea');
        inputsToDisable = $A(inputListToDisable);
	     
        inputsToDisable.each(function(node)
        {
            node.disabled = true;
            node.className = "clsInputDisabled";
        });
        
        // Selects
        inputListToDisable = div.getElementsByTagName('select');
        inputsToDisable = $A(inputListToDisable);
	     
        inputsToDisable.each(function(node)
        {
            node.disabled = true;
            node.className = "clsInputDisabled";
        });
    });
}

function showExclusivo(grupoToEnable, grupoPai)
{
    /////////// DESACTIVAR //////////
    
    // Inputs
    var inputListToDisable = $(grupoPai).getElementsByTagName('input');
    var inputsToDisable = $A(inputListToDisable);
     
    inputsToDisable.each(function(node)
    {
        node.disabled = true;        
        if(node.type != "checkbox")
        {
            node.className = "clsInputDisabled";
        }
        else
        {
            node.className = "clsCheckDisabled";
        }
    });
    
    // Textareas
    inputListToDisable = $(grupoPai).getElementsByTagName('textarea');
    inputsToDisable = $A(inputListToDisable);
     
    inputsToDisable.each(function(node)
    {
        node.disabled = true;
        node.className = "clsInputDisabled";
    });
    
    // Selects
    inputListToDisable = $(grupoPai).getElementsByTagName('select');
    inputsToDisable = $A(inputListToDisable);
     
    inputsToDisable.each(function(node)
    {
        node.disabled = true;
        node.className = "clsInputDisabled";
    });
    
	/////////// ACTIVAR OS CAMPOS DO GRUPO SELECCIONADO //////////////
	
    // Inputs
    var inputListToEnable = $(grupoToEnable).getElementsByTagName('input');
    var inputsToEnable = $A(inputListToEnable);
     
    inputsToEnable.each(function(node)
    {
        node.disabled = false;
        if(node.type != "checkbox")
        {
            node.className = "clsInputEnabled";
        }
        else
        {
            node.className = "clsCheckEnabled";
        }
    });
    
    // Textareas
    inputListToEnable = $(grupoToEnable).getElementsByTagName('textarea');
    inputsToEnable = $A(inputListToEnable);
     
    inputsToEnable.each(function(node)
    {        
        node.disabled = false;
        node.className = "clsInputEnabled";        
    });
    
    // Selects
    inputListToEnable = $(grupoToEnable).getElementsByTagName('select');
    inputsToEnable = $A(inputListToEnable);
     
    inputsToEnable.each(function(node)
    {        
        node.disabled = false;
        node.className = "clsInputEnabled";        
    });
    
    ///////////////////////////////////////////////////////////////////
    
    // Mudar o estilo das divs para disabled
    var divsGrupoExclusivo = document.getElementsByClassName("clsGrupoExclusivoEnabled", $(grupoPai));

    divsGrupoExclusivo.each(function(div)
    {
        div.className = "clsGrupoExclusivoDisabled";
    });
    
    // Mudar o estilo da div do grupo exclusivo seleccionado para enabled
    $(grupoToEnable).className = "clsGrupoExclusivoEnabled";
    
    ///////////////////////////////////////////////////////////////////
    
    var divsCampoExclusivo = document.getElementsByClassName('clsCampoExclusivoDisabled', $(grupoToEnable));
	     
    // Inputs	     
    divsCampoExclusivo.each(function(nodeCampo)
    {
        inputListToDisable = nodeCampo.getElementsByTagName('input');
        inputsToDisable = $A(inputListToDisable);
         
        inputsToDisable.each(function(node)
        {
            if(node.type != "checkbox")
            {
                node.disabled = true;
                node.className = "clsInputDisabled";
            }
            else
            {
                node.className = "clsCheckDisabled";
            }
        });
    });
    
    // Textareas
    divsCampoExclusivo.each(function(nodeCampo)
    {        
        inputListToDisable = nodeCampo.getElementsByTagName('textarea');
        inputsToDisable = $A(inputListToDisable);
         
        inputsToDisable.each(function(node)
        {
            node.disabled = true;
            node.className = "clsInputDisabled";
        });
    });
    
    // Selects
    divsCampoExclusivo.each(function(nodeCampo)
    {        
        inputListToDisable = nodeCampo.getElementsByTagName('select');
        inputsToDisable = $A(inputListToDisable);
         
        inputsToDisable.each(function(node)
        {
            node.disabled = true;
            node.className = "clsInputDisabled";
        });
    });
    
    ///////////////////////////////////////////////////////////////////
    
    divsCampoExclusivo = document.getElementsByClassName('clsCampoExclusivoEnabled', $(grupoToEnable));
     
    // Inputs
    divsCampoExclusivo.each(function(nodeCampo)
    {
        inputListToEnable = nodeCampo.getElementsByTagName('input');
        inputsToEnable = $A(inputListToEnable);
         
        inputsToEnable.each(function(node)
        {        
            if(node.type != "checkbox")
            {
                node.disabled = false;
                node.className = "clsInputEnabled";
            }
            else
            {
                node.className = "clsCheckEnabled";
            }
        });
    });
    
    // TextAreas
    divsCampoExclusivo.each(function(nodeCampo)
    {
        inputListToEnable = nodeCampo.getElementsByTagName('textarea');
        inputsToEnable = $A(inputListToEnable);
         
        inputsToEnable.each(function(node)
        {        
            node.disabled = false;
            node.className = "clsInputEnabled";        
        });
    });
    
    // Selects
    divsCampoExclusivo.each(function(nodeCampo)
    {
        inputListToEnable = nodeCampo.getElementsByTagName('select');
        inputsToEnable = $A(inputListToEnable);
         
        inputsToEnable.each(function(node)
        {        
            node.disabled = false;
            node.className = "clsInputEnabled";        
        });
    });
}

function showCampoExclusivo(campoToEnable, campoPai)
{
    // Mudar o estilo das divs para disabled
    divsCampoExclusivo = document.getElementsByClassName("clsCampoExclusivoEnabled", $(campoPai));
    divsCampoExclusivo.each(function(div)
    {
        div.className = "clsCampoExclusivoDisabled";
        
        // Inputs
        var inputListToDisable = $(div).getElementsByTagName('input');
        var inputsToDisable = $A(inputListToDisable);
	     
        inputsToDisable.each(function(node)
        {
            if(node.type != "checkbox")
            {                
                node.disabled = true;
                node.className = "clsInputDisabled";
            }
            else
            {
                node.readonly = true; //////////////////////////////////
                node.className = "clsCheckDisabled";
            }
            
            if(div.id != campoToEnable)
            {
                node.value = "";
                node.checked = false;
            }
        });
        
        // Textareas
        inputListToDisable = $(div).getElementsByTagName('textarea');
        inputsToDisable = $A(inputListToDisable);
	     
        inputsToDisable.each(function(node)
        {
            node.disabled = true;
            node.className = "clsInputDisabled";
            
            if(div.id != campoToEnable)
            {
                node.value = "";
            }
        });
        
        // Selects
        inputListToDisable = $(div).getElementsByTagName('select');
        inputsToDisable = $A(inputListToDisable);
	     
        inputsToDisable.each(function(node)
        {
            node.disabled = true;
            node.className = "clsInputDisabled";
            
            if(div.id != campoToEnable)
            {
                node.selectedIndex = 0;
            }
        });
    });
    
    // Mudar o estilo da div do campo exclusivo seleccionado para enabled
    $(campoToEnable).className = "clsCampoExclusivoEnabled";
    
    // Activar agora os inputs do campo exclusivo seleccionado
    var inputListToEnable = $(campoToEnable).getElementsByTagName('input');
    var inputsToEnable = $A(inputListToEnable);
     
    inputsToEnable.each(function(node)
    {
        if(node.type != "checkbox")
        {
            node.disabled = false;
            node.className = "clsInputEnabled";
        }
        else
        {
            node.readonly = false; //////////////////////////////////
            node.className = "clsCheckEnabled";
        }
        try { node.focus(); } catch(Err) { };
    });
    
    // Textareas
    inputListToEnable = $(campoToEnable).getElementsByTagName('textarea');
    inputsToEnable = $A(inputListToEnable);
     
    inputsToEnable.each(function(node)
    {
        node.disabled = false;
        node.className = "clsInputEnabled";
        try { node.focus(); } catch(Err) { };
    });
    
    // Selects
    inputListToEnable = $(campoToEnable).getElementsByTagName('select');
    inputsToEnable = $A(inputListToEnable);
     
    inputsToEnable.each(function(node)
    {
        node.disabled = false;
        node.className = "clsInputEnabled";
        try { node.focus(); } catch(Err) { };
    });
}

function mostrar_esconder_div(id) {

    var div_id = "div_" + id;
    var img_id = "img_" + id;
    
    if (document.getElementById) { // DOM3 = IE5, NS6
       
        if (document.getElementById(div_id).style.display == 'none')
        {
            document.getElementById(div_id).style.display = 'block';
            document.getElementById(img_id).src = "../../../modulos/WSAutarquia/images/minus.gif";
        }
        else
        {
            document.getElementById(div_id).style.display = 'none';
            document.getElementById(img_id).src = "../../../modulos/WSAutarquia/images/plus.gif";
        }
    }
}

//retorna um array de elementos cuja CssClass é a indicada
//searchClass – Css Class a procurar
//node - É possível indicar um nó de raiz para a procura
//tag - É possível indicar um tipo de elementos a procurar
function getElementsByClass(searchClass, tag, node) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

function customRequiredCheckboxCampo(source, args) 
{   
    //procura os elementos aos quais foram atribuidas a class de marcação
    //Os elementos devolvidos serão sempre do tipo span.
    //A referência só é usada para acelerar o processo
    var spanArr = getElementsByClass(args.Value, "span");
    
    //Verifica em todos as checkbox se existe pelo menos uma seleccionada
    //ou em input do tipo text ou textareas
    for(i=0; i<spanArr.length; i++)
    {
        //Os elementos input estão no interior de cada Span e são os únicos
        var inputArray = spanArr[i].getElementsByTagName("input")
        
        //Supostamente só existirá um input type checkbox/input.text dentro de cada span
        if(inputArray.length > 0)
        {
            if(inputArray[0].type == "checkbox")
            {
                if(inputArray[0].checked)
                {
                    args.Isvalid = true;
                    return;
                }
            }
            else if(inputArray[0].type == "text")
            {
                if(TrimString(inputArray[0].value) != "")
                {
                    args.Isvalid = true;
                    return;
                }
            }
        }
        else
        {
            inputArray = spanArr[i].getElementsByTagName("textarea")
            
            if(inputArray.length > 0)
            {
                if(TrimString(inputArray[0].value) != "")
                {
                    args.Isvalid = true;
                    return;
                }
            }

            inputArray = spanArr[i].getElementsByTagName("select")
            
            if(inputArray.length > 0)
            {
                var select = inputArray[0];
                
                if (select.selectedIndex > -1)
                    if(TrimString(select[select.selectedIndex].value) != "")
                    {
                        args.Isvalid = true;
                        return;
                    }
            }
        }
    }
    
    args.IsValid = false;
    return;
}


/**********************************************************************/
/*** PHONE NUMBER CHECK
/**********************************************************************/

// Declaring required variables
var digits = "0123456789";

// non-digit characters which are allowed in phone numbers
//var phoneNumberDelimiters = "()- ";
var phoneNumberDelimiters = "()-";

// characters which are allowed in international phone numbers
// (a leading + is OK)
// var validWorldPhoneChars = phoneNumberDelimiters + "+";
var validWorldPhoneChars = phoneNumberDelimiters;

// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 9;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
    var i;
    var returnString = "";
    var iPlusCheck = true;
    
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        
        while(c == ' ')
        {
            i++;
            
            if( i == s.length)
                return returnString
            
            c = s.charAt(i);
        }
        
        if(iPlusCheck)
        {
            if (c == '+')
            {
                i++;
                
                if( i == s.length)
                    return returnString;   
                
                c = s.charAt(i);                
            }
            
            iPlusCheck = false;
        }
        
        if (bag.indexOf(c) == -1)
        {
            returnString += c;
        }
    }
    return returnString;
}

function checkInternationalPhone(source, args)
{
    var s = stripCharsInBag(args.Value,validWorldPhoneChars);
    
    args.IsValid = (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
/**********************************************************************/

//Aplicado em Agendas para corrigir bug do IE com o filtro de opacity
//change the opacity for different browsers
function changeOpacity(opacity, id)
{
    var object = document.getElementById(id).style;

    object.opacity = (opacity / 100);

    object.MozOpacity = (opacity / 100);

    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")"; try{ object.removeAttribute("filter"); } catch(err) {}
}

//JPM - Script Alterado por causa do tamanho das janelas diferir entre ie6 e ie7.
var agt = navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion); //JPM: Novo
var is_mozilla = (agt.indexOf('firefox') != -1);
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)); //JPM: alterado: adicionei opera.
var is_ie7 = (is_ie && (is_major == 4) && (agt.indexOf("msie 7.") != -1)); //JPM: Novo

function openWindow(title, width, height, url, openerPage) {
    var left = parseInt((screen.availWidth / 2) - (width / 2));
    var top = parseInt((screen.availHeight / 2) - (height / 2));
    window.open (url,"janelanova","location=1,status=1,scrollbars=1,width="+ width +",height="+ height + ",left=" + left + ",top="+top); 
}
