//Var declarations
strStatus = '::: Merlin :::';
self.status = strStatus;
var strSistemaVar = 'MerlimCRM';

// Global functions
function _$(objID){
	return document.getElementById(objID);
}

function _valCampoForm(frmForm, objID){
	frmForm = document.forms[frmForm.id];
	if(frmForm){
		return eval('frmForm.'+ objID);
	}
	return false;
}


function lembrete(){
	popUp('usuarios_lembrete_senha_sel_form.asp', strSistemaVar + 'LembreteSenha', 'top, left, status, scrollbars, resizable, width, height', '100, 100, yes, auto, no, 320, 150');
}

function alterarSenha(){
	popUp('usuarios_alterar_senha_upd_form.asp', strSistemaVar + 'AlterarSenha', 'top, left, status, scrollbars, resizable, width, height', '100, 100, yes, auto, no, 320, 150');	
}
			
function main_clientes_novo(){
	document.location.href = 'clientes_novo_ins_form.asp';
}

// PAGINA USADAS DIVERSAS VEZES EM CATEGORIA DIFERENTES
function buscaEmpreendimento(){
	popUp('empreendimentos_busca_sel_form_001.asp', strSistemaVar + 'BuscaEmpreendimento', 'top, left, status, scrollbars, resizable, width, height', '100, 100, yes, auto, no, 400, 350');
}

function exportarEmpresa(IDCliente, IDUsuario){
	popUp('empresas_exportar.aspx?IDCliente='+ IDCliente + '&IDUsuario='+ IDUsuario, strSistemaVar + 'ExportarEmpresa', 'top, left, status, scrollbars, resizable, width, height', '100, 100, no, no, no, 250, 10');
}

function exportarContato(IDCliente, IDUsuario){
	popUp('contatos_exportar.aspx?IDCliente='+ IDCliente + '&IDUsuario='+ IDUsuario, strSistemaVar + 'ExportarContato', 'top, left, status, scrollbars, resizable, width, height', '100, 100, no, no, no, 250, 10');
}
			
function contato(){
	
}		

function iFrameRedirect(iFrame, URLToGo){
	iFrame.document.location.href = URLToGo;
}

function changeBG(obj, bgColor, cursor){
	var cursor = (cursor == undefined || cursor == null ? 'default' : cursor);
	obj.style.backgroundColor = bgColor;
	obj.style.cursor = cursor;
}

function popUp(page, name, properties, values){
	var strPopUp 	= '';
	var properties 	= properties.split(', ');
	var values 	= values.split(', ');
	
	strPopUp += '\'';
	for(var i = 0; i < properties.length; i++){
		strPopUp += properties[i] + '=' + values[i] + ', ';
	}
	strPopUp = strPopUp.substring(0, (strPopUp.length-2));
	strPopUp += '\'';
	window.open(page, name, strPopUp);
}

// Control Functions

function maximo(text, len){
	return (text.length <= len);
}

// General Validation Functions
function sessionValidation(user){
	if(user == ''){
		document.location = 'clientes_sessao_expirada.htm';
	}
}

function isEmpty(valor){
	return (valor == '' || valor == null);
}

function isNumeric(asciiCode){
	return ((asciiCode >= 48) && (asciiCode <= 57));
}

function isCurrency(asciiCode){
	return ((asciiCode >= 48 && asciiCode <= 57) || asciiCode == 44 || asciiCode == 46);
}

function isEmail(campo){
	if(isEmpty(campo.value))
		return;
	 
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(campo.value)){
		return true;
	}
	alert('Endereço de e-mail Inválido!');
	campo.select();
	campo.focus();
	return false;
}

function isCEP(campo){
	for(var i=0; i < campo.length; i++){
		if(campo[i].value.length < campo[i].maxLength){
			alert("CEP Inválido");
			campo[i].select();
			campo[i].focus();
			return false;
		}
	}
}

function isCNPJ(campo){
	var valido = 0;
   
	if(isEmpty(campo.value))
		return;
	
	var digito = campo.value.length-2;
	if(isDigitoCNPJ(campo.value, digito)){
      digito = campo.value.length-1;
      valido = isDigitoCNPJ(campo.value, digito) ? 1 : 0;
	}
	
	if(((!valido) && !isCPF(campo)) || (campo.value == '00000000000000')){
		alert('CNPJ/CPF Inválido');
		campo.select();
		campo.focus;
	}
}

function isCPF(campo){
	var CPF = campo.value

	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
	CPF == "22222222222" || CPF == "33333333333" || CPF == "44444444444" ||
	CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
	CPF == "88888888888" || CPF == "99999999999")
		return false;

	var soma = 0;
	
	for (i=0; i < 9; i ++)
		soma += parseInt(CPF.charAt(i)) * (10 - i);
	
	resto = 11 - (soma % 11);
	
	if (resto == 10 || resto == 11)
		resto = 0;
	
	if (resto != parseInt(CPF.charAt(9)))
		return false;
	
	soma = 0;
	
	for (i = 0; i < 10; i ++)
		soma += parseInt(CPF.charAt(i)) * (11 - i);
	
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	
	if (resto != parseInt(CPF.charAt(10)))
		return false;
	
	return true;
}

function isDigitoCNPJ(CNPJ, digito){
	var dig = 0;
	var ind = 2;
	for(f=digito;f>0;f--){
    	dig += parseInt(CNPJ.charAt(f-1))*ind;
    	if(ind>8)
			ind = 2; 
		else
		 	ind++;
	}
   	dig %= 11;
	if(dig<2)
  		dig = 0;
 	else
		dig = 11-dig;
 	
	return dig == parseInt(CNPJ.charAt(digito))
}

function isEnter(asciiCode){
	return (asciiCode == 13);
}

// Form functions
function frmReset(frm){
	frm.reset();
}

function checkAll(frm){
	for(i=0;i<frm.length;i++){
		if(frm.elements[i].type == 'checkbox')
			frm.elements[i].checked = true;
	}
}

function pulaCampo(me, next){
	if(me.value.length == me.maxLength){
		next.focus();
	}
}

function validaCompletoTexto(frm){
	// Validates Text with field's name in the error message
	var msg = '';
	var i = 0;
	
	for(i=0; i<frm.length; i++){
		field = frm.elements[i];
		if(isEmpty(field.value)){
			msg += 'Por favor, preencha o campo ';
			msg += field.name;
			msg += '!';
			alert(msg);
			field.focus();
			return false;
		}
	}
	
	frm.submit();
}

String.prototype.trim = function() {
	return this.replace(/^\s*(.*)/, "$1").replace(/(.*?)\s*$/, "$1");
}

function validaExcecao(frm, excecao){
	// Validates Text 'n Combo with deafult error message
	var msg = 'Por favor, preencha todos os campos obrigatórios!';
	var i = 0, j = 0;
	var opcionais = excecao.split(', ');
	var isOpcional = 0;
	
	for(i=0; i<frm.length; i++){
		field = frm.elements[i];
		for(j=0; j<opcionais.length; j++){
			isOpcional = (opcionais[j].trim() == field.name) ? 1 : 0;
			if(isOpcional) 
				break;
		}

		if(!isOpcional) 
			if(((field.type == 'text' || field.type == 'textarea') && isEmpty(field.value)) || (field.type == 'select-one' && isEmpty(field[field.selectedIndex].value))){
				alert(msg);
				if(field.style.display != 'none'){
					field.focus();
				}
				return false;
			}
	}
	
	if(validaExcecao.arguments.length == 2)
		frm.submit();
}

function validaNovoCampo(frm, pagina){
	if(frm.ID.length == 4){
		alert('Já existem 4 campos disponíveis.\nCaso deseje incluir um novo, exclua um dos campos existentes, e tente novamente!');
		return false;
	}
	else
		document.location.href = pagina;
}

function isDate (year, month, day) 
{   // catch invalid years (not 2- or 4-digit) and invalid months and days. 
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false; 

    // Explicitly change type to integer to make code work in both 
    // JavaScript 1.1 and JavaScript 1.2. 
    var intYear = parseInt(year); 
    var intMonth = parseInt(month); 
    var intDay = parseInt(day); 

    // catch invalid days, except for February 
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false; 

    return true; 
}

function _onClickTdMenu(objTdMenuClicar, strURL){
	var objTdMenu = null;
	var totalMenus = 20;
	objTdMenuClicar = _$(objTdMenuClicar);

	for (var intContTdMenu = 1; intContTdMenu <= totalMenus; intContTdMenu++) {
		objTdMenu = _$('tdMenu_' + intContTdMenu);
		if(objTdMenu != undefined && objTdMenu != null){
			//debugTD(objTdMenu, objTdMenuClicar);
			if(objTdMenu != objTdMenuClicar){
				objTdMenu.className = 'tdMenu';
				objTdMenu.onmouseover = function(){
					this.className = 'tdMenu2';
				}
				objTdMenu.onmouseout = function(){
					this.className = 'tdMenu';
				}
			}
		}
	}
	if(objTdMenuClicar){
		objTdMenuClicar.className = 'tdMenu3';
		objTdMenuClicar.onmouseover = null;
		objTdMenuClicar.onmouseout = null;

		var objTarget = _$('main');
		if (objTarget) {
			iFrameRedirect(objTarget, strURL);
		}
	}
}

function debugTD(objTd){
	var objArgumentos = debugTD.arguments;
	var strDebug = '';
	strDebug = objTd.id + ' - ' + objTd.style.color;
	if(objArgumentos){
		var intTotalArgs = objArgumentos.length;
		if(intTotalArgs > 1){
			strDebug += '\n\nArgumentos\n=====================\n';
			for(var intContArgs = 0; intContArgs < intTotalArgs; intContArgs++){
				if(objArgumentos[intContArgs])
					strDebug += '• ' +  intContArgs + ' = ' + objArgumentos[intContArgs] + ' => ' + objArgumentos[intContArgs].id + '\n';
				else
					strDebug += '• ' +  intContArgs + ' = ' + objArgumentos[intContArgs] + '\n';
			}
			strDebug += '• ' +  (document.getElementById(objArgumentos[0].id) == document.getElementById(objArgumentos[1].id));
		}
	}
	if(strDebug != ''){
		alert(strDebug);
	}
}

function alertaErro(strItens){
	alert('Por favor verifique os campos preenchendo-os corretamente e tente novamente:\n\n' + strItens);
}

function verifica_IE(){
var blnRetorno = browser_IE();
	if(!blnRetorno){
		alert('Você está utilizando um navegador não compatível com nossos sistemas.\n\nPor favor utilize o navegador \"Microsoft Internet Explorer\"\nou fale com nosso suporte: (11) 3879-7777');
	}
	return blnRetorno;
}

function browser_IE(){
	return (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
}
