// JavaScript Document
function getDataAtualCompleta() {
    var now = new Date();
    var days = new Array("domingo","segunda-feira","terça-feira","quarta-feira","quinta-feira","sexta-feira","sábado");
    var months = new Array("janeiro","fevereiro","março","abril","maio","junho","julho","agosto","setembro","outubro","novembro","dezembro");
    var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();  
    today = days[now.getDay()] + ", "+date+" de " +months[now.getMonth()] + " de " +(fourdigits(now.getYear())) ;
    return " " +today+ "  ";
}

function isNumber(pVal) {
  var reTipo = /^\d+$/; // Onde ... é a expressão regular apropriada
  return reTipo.test(pVal);
}

//--------------------------------------------------------------
//Permite apenas algarismos
//
function SomenteNumero(evt) {
   var whichCode = (window.Event) ? evt.which : evt.keyCode;
   
   if (whichCode == 8 || whichCode == 9 ) {
     return true;
   }
   
   if (whichCode > 31 && (whichCode < 48 || whichCode > 57)) {
      return false; // letras
   }
    
  return true;
}
function gerarSenha(){
    numPosibilidades = 999999 - 000000;
    aleat = Math.random() * numPosibilidades;
    return Math.round(parseInt(inferior) + aleat);
}

function fourdigits(number) {
    return (number < 1000) ? number + 1900 : number;
}

function comparadata(campo_inicio, campo_fim) {
    ano_inicio = campo_inicio.substring(6 ,10 );
    mes_inicio = campo_inicio.substring(3 ,5 );
    dia_inicio = campo_inicio.substring(0 ,2 );
    ano_fim = campo_fim.substring(6 ,10 );
    mes_fim = campo_fim.substring(3 ,5 );
    dia_fim = campo_fim.substring(0 ,2 );
    campo_inicio_temp = ano_inicio+mes_inicio+dia_inicio;
    campo_fim_temp = ano_fim+mes_fim+dia_fim;

    if  ( campo_inicio_temp > campo_fim_temp ) {
        alert('Atenção! A data inicial não pode ser maior que a final.');
        return false;
    }
    if ((campo_fim_temp - campo_inicio_temp) > 3) {
        alert('A diferença do período de tempo não pode ser maior que 3 dias.');
        return false;
    }
    return true;
}

function conf_data(campo_inicio, campo_fim) {
	if (!func_checkdate(campo_inicio) || document.formulario[campo_inicio].value.length < 10 ){ //check date
		alert("O formato tem que ser dd/mm/aaaa ");
		document.formulario[campo_inicio].focus();
		return false;
	}
	
	if (!func_checkdate(campo_fim) || document.formulario[campo_fim].value.length < 10 ){ //check date
		alert("O formato tem que ser dd/mm/aaaa ");
		document.formulario[campo_fim].focus();
		return false;
	}
	//document.formulario[campo].select();
	return comparadata(document.formulario[campo_inicio].value,document.formulario[campo_fim].value);
}

function func_checkdate(campo) {
	
    var object_value = document.formulario[campo].value;

    //Returns true if value is a date in the dd/mm/yyyy format
    isplit = object_value.indexOf('/');

    if (isplit == -1 || isplit == object_value.length)
        return false;

    sDay = object_value.substring(0, isplit);
    isplit = object_value.indexOf('/', isplit + 1);

    if (isplit == -1 || (isplit + 1 ) == object_value.length)
        return false;

    sMonth = object_value.substring((sDay.length + 1), isplit);

    sYear = object_value.substring(isplit + 1);

    if (!func_checkinteger(sMonth)) //check month
        return false;
    else
    if (!func_checkrange(sMonth, 1, 12)) //check month
        return false;
    else
    if (!func_checkinteger(sYear)) //check year
        return false;
    else
    if (!func_checkrange(sYear, 0, 9999)) //check year
        return false;
    else
    if (!func_checkinteger(sDay)) //check day
        return false;
    else
    if (!func_checkday(sYear, sMonth, sDay)) // check day
        return false;
    else
        return true;
}

function func_checkday(checkYear, checkMonth, checkDay) {

    maxDay = 31;

    if (checkMonth == 4 || checkMonth == 6 ||
            checkMonth == 9 || checkMonth == 11)
        maxDay = 30;
    else
    if (checkMonth == 2)
    {
        if (checkYear % 4 > 0)
            maxDay =28;
        else
        if (checkYear % 100 == 0 && checkYear % 400 > 0)
            maxDay = 28;
        else
            maxDay = 29;
    }

    return func_checkrange(checkDay, 1, maxDay); //check day
}

function func_checkinteger(object_value) {
    //Returns true if value is a number or is NULL
    //otherwise returns false 

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
    var decimal_format = ".";
    var check_char;

    //The first character can be + -  blank or a digit.
    check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
    return func_checknumber(object_value);
    else
    return false;
}

function func_numberrange(object_value, min_value, max_value) {
    // check minimum
    if (min_value != null)
    {
        if (object_value < min_value)
        return false;
    }

    // check maximum
    if (max_value != null)
    {
    if (object_value > max_value)
        return false;
    }
    
    //All tests passed, so...
    return true;
}

function func_checknumber(object_value) {
    //Returns true if value is a number or is NULL
    //otherwise returns false 

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
    var start_format = " .+-0123456789";
    var number_format = " .0123456789";
    var check_char;
    var decimal = false;
    var trailing_blank = false;
    var digits = false;

    //The first character can be + - .  blank or a digit.
    check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
    if (check_char == 1)
        decimal = true;
    else if (check_char < 1)
        return false;
        
    //Remaining characters can be only . or a digit, but only one decimal.
    for (var i = 1; i < object_value.length; i++)
    {
        check_char = number_format.indexOf(object_value.charAt(i))
        if (check_char < 0)
            return false;
        else if (check_char == 1)
        {
            if (decimal)    // Second decimal.
                return false;
            else
                decimal = true;
        }
        else if (check_char == 0)
        {
            if (decimal || digits) trailing_blank = true;
        // ignore leading blanks

        }
        else if (trailing_blank)
            return false;
        else
            digits = true;
    } 
    //All tests passed, so...
    return true;
}

function func_checkrange(object_value, min_value, max_value)
{
    //if value is in range then return true else return false
    
    if (object_value.length == 0) {
        return true;
	}
    
    if (!func_checknumber(object_value)) {
        return false;
    } else {
        return (func_numberrange((eval(object_value)), min_value, max_value));
    }
    
    //All tests passed, so...
    return true;
}

// -----------------------------------------------------------------
//nome antigo do metodo = ver_tipo
function selecionaTipoAssinaturaPrincipal(frm) {
    //alert(frm.name);
    if (frm.cbomodalidade.value < 3) {
        frm.tipoAssinaturaPrincipal.value = 1;
    } else {
        frm.tipoAssinaturaPrincipal.value = 2;       
    }
}

// -----------------------------------------------------------------
// usado na pagina assinatura_3_escolhe_ramo.asp
//
function insereRamo(frm) {
    var ramo = frm.ramo;
    var estado = frm.estado;
    var resultadoRamo = frm.resultadoRamo;  
    var newValue = ramo.options[ramo.selectedIndex].value;    
    var existe = false;  
    
    for (i=0;i<resultadoRamo.length;i++) {
        if (newValue == resultadoRamo.options[i].value){
            existe = true;
            alert("Opção já Existe!");
            return ;
        }
    }
  
    if ( !existe ) {
        var newOpt = document.createElement('option');
        newOpt.text = ramo.options[ramo.selectedIndex].text;
        newOpt.value = newValue;
        //resultadoRamo.size = resultadoRamo.size+1;
        try {
			// faz empilhamento  
			if ( resultadoRamo.length > 0 ) {
				resultadoRamo.add(newOpt, resultadoRamo.options[0]); // standards compliant; doesn't work in IE
			} else {
				// faz enfileramento
	            resultadoRamo.add(newOpt, null); // standards compliant; doesn't work in IE			
			}
        } catch(ex) {
            resultadoRamo.add(newOpt); // IE only
        }
    }
}

function removeRamo(frm) {
    var resultadoRamo = frm.resultadoRamo;
	if ( resultadoRamo.selectedIndex >= 0) {
		if ( resultadoRamo.options[resultadoRamo.selectedIndex].value != null) {
			try {
				resultadoRamo.remove(resultadoRamo.selectedIndex); // standards compliant; doesn't work in IE
			} catch(ex) {
				resultadoRamo.remove(resultadoRamo.selectedIndex); // IE only
			}
		    //if ( resultadoRamo.size > 6 ) resultadoRamo.size = resultadoRamo.size-1;
	    }
	}
}

function insereRamoUF(frm) {
    var ramo = frm.ramo;
    var estado = frm.estado;
    var resultadoRamoUF = frm.resultadoRamoUF;  
    var newValue = ramo.options[ramo.selectedIndex].value + "-" + estado.options[estado.selectedIndex].value;    
    var existe = false; 
    
    for (i=0;i<resultadoRamoUF.length;i++) {
        if (newValue == resultadoRamoUF.options[i].value){
            existe = true
            alert("Opção já Existe!");
            return ;
        }
    }
  
    if ( !existe ) {
        var newOpt = document.createElement('option');
        newOpt.text = ramo.options[ramo.selectedIndex].text + " - " + estado.options[estado.selectedIndex].text;
        newOpt.value = newValue;
		try {
			if ( resultadoRamoUF.length > 0 ) {
				// faz empilhamento  
				resultadoRamoUF.add(newOpt, resultadoRamoUF.options[0]); 
			} else {
				// faz enfileramento
				resultadoRamoUF.add(newOpt, null); // standards compliant; doesn't work in IE			
			}
        } catch(ex) {
            resultadoRamoUF.add(newOpt); // IE only
        }		
    }
}

function removeRamoUF(frm) {
    var resultadoRamoUF = frm.resultadoRamoUF;
	if ( resultadoRamoUF.selectedIndex >= 0) {
		if ( resultadoRamoUF.options[resultadoRamoUF.selectedIndex].value != null) {
			try {
				resultadoRamoUF.remove(resultadoRamoUF.selectedIndex); // standards compliant; doesn't work in IE
			}
			catch(ex) {
				resultadoRamoUF.remove(resultadoRamoUF.selectedIndex); // IE only
			}
		    //if ( resultadoRamoUF.size > 6 ) resultadoRamoUF.size = resultadoRamo.size-1;
		}
	}
}

function mascara2(data2) { 
    var mdata2 = ''; 
        mdata2 = mdata2 + data2; 
    if (mdata2.length == 2){ 
        mdata2 = mdata2 + '/'; 
        document.forms[0].data2.value = mdata2; 
    } 
    if (mdata2.length == 5){ 
        mdata2 = mdata2 + '/'; 
        document.forms[0].data2.value = mdata2; 
    } 
    if (mdata2.length == 10){ 
        return true;
    } 
} 

function mascara(data) { 
    var mdata = ''; 
    mdata = mdata + data; 
    if (mdata.length == 2){ 
        mdata = mdata + '/'; 
        document.forms[0].data.value = mdata; 
    } 
    if (mdata.length == 5){ 
        mdata = mdata + '/'; 
        document.forms[0].data.value = mdata; 
    } 
    if (mdata.length == 10){ 
        return true;
    } 
} 

function mascara(data)
{ 
	var mdata = ''; 
	mdata = mdata + data; 
	if (mdata.length == 2){ 
	  mdata = mdata + '/'; 
	  document.forms[0].data.value = mdata; 
	} 
	if (mdata.length == 5){ 
	  mdata = mdata + '/'; 
	  document.forms[0].data.value = mdata; 
	} 
	if (mdata.length == 10){ 
	  return true;
	} 
}

function mascara2(data2)
{ 
	var mdata2 = ''; 
	mdata2 = mdata2 + data2; 
	if (mdata2.length == 2){ 
	  mdata2 = mdata2 + '/'; 
	  document.forms[0].data2.value = mdata2; 
	} 
	if (mdata2.length == 5){ 
	  mdata2 = mdata2 + '/'; 
	  document.forms[0].data2.value = mdata2; 
	} 
	if (mdata2.length == 10){ 
	  return true;
	} 
} 

//-------------------------------------------------------------------
// isArray(obj)
// Returns true if the object is an array, else false
//-------------------------------------------------------------------
function isArray(obj)
{
    return (typeof(obj.length)=="undefined") ? false : true;
}

//-------------------------------------------------------------------
// isElement(obj)
// Returns true if the object is an element, else false
//-------------------------------------------------------------------
function isElement(obj)
{
    return (typeof(obj)=="undefined") ? false : true;
}

//  *********************************** ASSINATURAS ******************
function CopiaEnderecos(frm) {
  frm.logradouro_d.value =  frm.logradouro.value;
  frm.endereco_d.value =  frm.endereco.value;
  frm.numero_d.value =  frm.numero.value;
  frm.complemento_d.value =  frm.complemento.value;
  frm.bairro_d.value =  frm.bairro.value;               
  frm.cep_d.value =  frm.cep.value; 
  frm.estado_d.value =  frm.estado.value;
  frm.cidade_d.value =  frm.cidade.value; 
}