// DADOS DO USUARIO JÁ CADASTRADO
var IDUsuario = "";
var strNome = "";
var strDDD = "";
var strTelefone = "";
var strEmail = "";
var strCEP = "";

var LastValor_Min = "";
var LastValor_Max = "";
var LastCategoria = "";
var ModoCalculo = "parcela"; // DEFAULT

function checkMail(mail)
{        
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	
	if(typeof(mail) == "string")
	{
		if(er.test(mail))
		{
			return true;
		}
		else
		{
			return false;
		}		
	}
	else if(typeof(mail) == "object")
	{
		if(er.test(mail.value))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

function checkDDD(numddd)
{
	var aDDD = new Array(11,12,13,14,15,16,17,18,19,21,22,24,27,28,31,32,33,34,35,37,38,41,42,43,44,45,46,47,48,49,51,53,54,55,61,61,62,63,64,65,66,67,68,69,71,73,74,75,77,79,81,82,83,84,85,86,87,88,89,91,92,93,94,95,96,97,98,99);
	
	for(i = 0 ; i < aDDD.length ; i++)
	{
		if(parseInt(numddd) == parseInt(aDDD[i].toString()))
		{
			return false;
		}
	}
	
	return true;
}

function checkTelefone(telefone)
{        
	var er = new RegExp(/\(\d{2}\)\ \d{8}/);
	
	if(typeof(telefone) == "string")
	{		
		if(er.test(telefone))
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}
	else if(typeof(telefone) == "object")
	{
		if(er.test(telefone.value))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

function checkTelefoneSequencia(telefone)
{
	var strSeqInvalidas = new Array("00000000","11111111","22222222","33333333","44444444","55555555","66666666","77777777","88888888","99999999","12312312","12345678","32132132","11112222");
	
	for(i = 0 ; i < strSeqInvalidas.length ; i++)
	{
		if(telefone == strSeqInvalidas[i])
		{
			return false;
		}
	}
	
	return true;
}

function checkValores(strRSMin, strRSMax)
{
	var tRSMin = parseInt( strRSMin.split(",")[0].toString().replace(".",""));
	var tRSMax = parseInt( strRSMax.split(",")[0].toString().replace(".",""));
	
	return (tRSMin < tRSMax) ? true : false;	
}

function Cadastrar_Usuario()
{
	
	if(document.FormSimulador.CbCategorias.value == "0" || document.FormSimulador.CbCategorias.value == "0")
	{
		alert("Selecione o que você Deseja!\nImóvel, Automóvel, Motos, etc...");
		document.FormSimulador.CbCategorias.focus();
	}
	else if(document.FormSimulador.TxtNome.value == "")
	{
		alert("Informe seu Nome !");
		document.FormSimulador.TxtNome.focus();
	}
	else if(document.FormSimulador.TxtNome.value.length <= 3)
	{
		alert("Nome Inválido !\nPor favor informe seu nome corretamente, sem ser apelido ou abreviação.");
		document.FormSimulador.TxtNome.select();
	}
	else if(document.FormSimulador.TxtValor_Min.value == "" || document.FormSimulador.TxtValor_Min.value == "0,00")
	{
		alert("Informe o Valor Mínimo !");
		document.FormSimulador.TxtValor_Min.focus();
	}
	else if(document.FormSimulador.TxtValor_Max.value == "" || document.FormSimulador.TxtValor_Max.value == "0,00")
	{
		alert("Informe o Valor Máximo !");
		document.FormSimulador.TxtValor_Max.focus();
	}
	else if(document.FormSimulador.TxtValor_Min.value.length <= 4)
	{
		alert("Valor Inválido !");
		document.FormSimulador.TxtValor_Min.select();
	}
	else if(document.FormSimulador.TxtValor_Max.value.length <= 4)
	{	
		alert("Valor Inválido !");
		document.FormSimulador.TxtValor_Max.select();
	}
	else if(!checkValores(document.FormSimulador.TxtValor_Min.value,document.FormSimulador.TxtValor_Max.value))
	{
		alert("Valor Máximo Inválido ! O valor deve ser maior que o valor mínimo.");
		document.FormSimulador.TxtValor_Max.select();
	}
	else if(document.FormSimulador.TxtEmail.value == "")
	{
		alert("Preencha o seu E-mail !");
		document.FormSimulador.TxtEmail.focus();
	}
	else if(!checkMail(document.FormSimulador.TxtEmail))
	{
		alert(" O E-mail está Inválido ! Informe um E-mail correto.");
		document.FormSimulador.TxtEmail.select();
	}	
	else if(document.FormSimulador.TxtDDD.value == "")
	{
		alert("Preencha o DDD do seu Telefone !");
		document.FormSimulador.TxtDDD.focus();
	}
	else if(checkDDD(document.FormSimulador.TxtDDD.value))
	{
		alert("DDD Inválido ! Informe um DDD válido !");
		document.FormSimulador.TxtDDD.select();
	}
	else if(document.FormSimulador.TxtTelefone.value == "")
	{
		alert("Preencha o seu Telefone !");
		document.FormSimulador.TxtTelefone.focus();
	}
	else if(!checkTelefone("(" + document.FormSimulador.TxtDDD.value + ") " + document.FormSimulador.TxtTelefone.value))
	{
		alert("Telefone Inválido ! Informe um Telefone correto.");
		document.FormSimulador.TxtTelefone.select();
	}	
	else if(!checkTelefoneSequencia(document.FormSimulador.TxtTelefone.value))
	{
		alert("Telefone Inválido ! Informe um Telefone correto.");
		document.FormSimulador.TxtTelefone.focus();
	}
	else
	{
		var oHTTPRequest2 = createXMLHTTP(); 
		oHTTPRequest2.open("post", "global/Simulador/Execs/Cadastrar_Usuario.asp", true); //enviamos para a página que faz o select do que foi digitado e traz a lista preenchida.
		oHTTPRequest2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		oHTTPRequest2.onreadystatechange = function()
		{
			if (oHTTPRequest2.readyState == 4)
			{	
				IDUsuario = oHTTPRequest2.responseText;	
				Simular();			
			}
		}
		
		strNome = escape(document.FormSimulador.TxtNome.value);
		strDDD = document.FormSimulador.TxtDDD.value;
		strTelefone = document.FormSimulador.TxtTelefone.value;
		strEmail = escape(document.FormSimulador.TxtEmail.value);
		strCEP = document.FormSimulador.cep.value;
		
		LastValor_Min = document.FormSimulador.TxtValor_Min.value;
		LastValor_Max = document.FormSimulador.TxtValor_Max.value;
		LastCategoria = document.FormSimulador.CbCategorias.value;		
		
		var strVariaveisForm = "revenda=" + IDRevenda;
		strVariaveisForm += "&nome=" + strNome;
		strVariaveisForm += "&ddd=" + strDDD;
		strVariaveisForm += "&telefone=" + strTelefone;
		strVariaveisForm += "&email=" + strEmail;
		strVariaveisForm += "&cep=" + strCEP;
		
		var ObjAjax = document.getElementById("OutputAjax");
		ObjAjax.innerHTML = strHTMLBussy
		
		oHTTPRequest2.send(strVariaveisForm);
	}

}

function Simular()
{
	if(IDUsuario == "")
	{
		Cadastrar_Usuario();		
	}
	else
	{
		if(LastValor_Min == "")
		{
			LastValor_Min = document.FormSimulador.TxtValor_Min.value;
			LastValor_Max = document.FormSimulador.TxtValor_Max.value;
			LastCategoria = document.FormSimulador.CbCategorias.value;
		}
		
		var ObjAjax = document.getElementById("OutputAjax");
		ObjAjax.innerHTML = strHTMLBussy
		
		var strVariaveisForm = "resultado.asp?categoria=" + LastCategoria;
		strVariaveisForm += "&valor_inicial=" + LastValor_Min;
		strVariaveisForm += "&valor_final=" + LastValor_Max;
		strVariaveisForm += "&revenda=" + IDRevenda;
		strVariaveisForm += "&usuario=" + IDUsuario;
		strVariaveisForm += "&modocalculo=" + ModoCalculo;
		strVariaveisForm += "&vendedor=" + IDVendedor;
		
		document.location.href = strVariaveisForm;
		
	}    
}

function CarregaForm_Simulador(idDefaultCategoria)
{
	var ObjAjax = document.getElementById("OutputAjax");
	ObjAjax.innerHTML = strHTMLBussy
	
	var oHTTPRequest2 = createXMLHTTP(); 
	oHTTPRequest2.open("post", "global/Simulador/Execs/Formulario.asp", true); //enviamos para a página que faz o select do que foi digitado e traz a lista preenchida.
	oHTTPRequest2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	oHTTPRequest2.onreadystatechange = function()
	{
		if (oHTTPRequest2.readyState == 4)
		{	
			ObjAjax.innerHTML = oHTTPRequest2.responseText;	
			
			LastValor_Min = LastValor_Max = LastCategoria = "";
			
			if(strNome != "")
			{
				document.FormSimulador.TxtNome.value = strNome;
				document.FormSimulador.TxtDDD.value = strDDD;
				document.FormSimulador.TxtTelefone.value = strTelefone;
				document.FormSimulador.TxtEmail.value = strEmail;
				document.FormSimulador.cep.value = strCEP;
				
				document.FormSimulador.cep.disabled = document.FormSimulador.TxtNome.disabled = document.FormSimulador.TxtDDD.disabled = document.FormSimulador.TxtTelefone.disabled = document.FormSimulador.TxtEmail.disabled = true;
				
				document.FormSimulador.TxtValor_Min.select();
			}
		}
	}
	
	var strVariaveisForm = "revenda=" + IDRevenda + "&categorias=" + escape(strCategorias) + "&id_categdefault=" + idDefaultCategoria + "&modocalculo=" + ModoCalculo;
	
	oHTTPRequest2.send(strVariaveisForm);
}

function OpenRiaSimulador(strid)
{
	striduser = IDUsuario;

	var altura = (600);
 	var largura = (800);
	
	strURL = "http://www.econsorcio.com.br/MeuConsorcio/Rias/Mitsubishi/Container_Mitsubishi.aspx?SessionID=364";
	
	if(strid != "")
	{
		strURL += "&BemIndex=" + strid;
	}	
	
	if(striduser != "")
	{
		strURL += "&UserIndex=" + striduser;		
	}
		
	strParams = "width=" + largura + ", height=" + altura + ", top=0, left=0, dependent=yes, status=no";
	
	if(window.screen.availHeight <= altura)
	{
		strParams += ", resizable=yes, scrollbars=yes";		
	}
	
	window.open(strURL, "ria", strParams);
}

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function MascaraMoeda(fld, milSep, decSep, e) 
{
	if(fld.value.length >= 10)
	{
		return false;
	}
	
	var sep = 0; 		
	var key = "";		
	var i = j = 0; 		
	var len = len2 = 0; 		
	var strCheck = "0123456789"; 		
	var aux = aux2 = ""; 		
	var whichCode = ""; 
	
	if (isIE) 
	{
		whichCode = e.keyCode;       
	}
	else if (isWin)
	{
		whichCode = e.which; 
	}
	else if (isOpera)
	{
		whichCode = e.charCode;
	}
	
	if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
	{
		return true; 
	}	
	
	key = String.fromCharCode(whichCode);// Valor para o código da Chave 
	
	if (strCheck.indexOf(key) == -1) // Chave inválida 
	{
		return false; 
	}
	
	len = fld.value.length; 
	
	for(i = 0; i < len; i++) 
	
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break; 
	
	aux = ''; 
	
	for(; i < len; i++) 
	
	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i); 
	
	aux += key; 
	
	len = aux.length; 
	
	if (len == 0) fld.value = ''; 
	
	if (len == 1) fld.value = '0'+ decSep + '0' + aux; 
	
	if (len == 2) fld.value = '0'+ decSep + aux; 
	
	if (len > 2) 		
	{ 
	
		aux2 = ''; 
		
		for (j = 0, i = len - 3; i >= 0; i--) 
		{ 
		
			if (j == 3) 
			{ 		
				aux2 += milSep; 			
				j = 0; 		
			} 
		
			aux2 += aux.charAt(i); 		
			j++; 
		} 
	
		fld.value = ''; 
		
		len2 = aux2.length; 
	
		for (i = len2 - 1; i >= 0; i--) 
	
		fld.value += aux2.charAt(i); 
	
		fld.value += decSep + aux.substr(len - 2, len); 
	
	}
	
	return false;	
}