//function soNumero(intMax)
function soNumero()
{
	var keyCode = event.keyCode;
	if (!(keyCode > 47 && keyCode < 58))
		event.keyCode = 0;
}
	
function autoTab(input,len, e)
{
	if (e == null && !isNN) e = event;
	if (len == null && !isNN) len = input.maxLength;	
	var isNN    = (navigator.appName.indexOf("Netscape") != -1);
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter  = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	var x;

	if((input.value.length >= len) && (!containsElement(filter,keyCode)))
	{
		input.value = input.value.slice(0, len);
		if((input.form[(getIndex(input)+1) % input.form.length].disabled)== false)
			input.form[(getIndex(input)+1) % input.form.length].focus();
		else
		{
			if((input.form[(getIndex(input)+2) % input.form.length].disabled)== false)
				input.form[(getIndex(input)+2) % input.form.length].focus();
			else
				input.form[(getIndex(input)+4) % input.form.length].focus();
		}
	}
	
	function containsElement(arr, ele)
	{
		var found = false, index = 0;
		while(!found && index < arr.length)
			if(arr[index] == ele)
				found = true;
			else
		index++;
	return found;
	}

	function getIndex(input)
	{
		var index = -1, i = 0, found = false;
		while ((i < input.form.length) && (index == -1))
		if (input.form[i] == input)
			index = i;
		else
			i++;
	
		return index;
	}
	return true;
}

function validateEmail(pStr)
{
    var reEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
    if (reEmail.test(pStr)) {
        return true;
	} else if (pStr != null && pStr != "") {
	    return false;
		//alert(pStr + " NÃO é um endereço de e-mail válido.");
	}
}

function strltrim(){
	return this.replace(/^\s+/,'');
}

function strrtrim(){
	return this.replace(/\s+$/,'');
}
	
function strtrim(){
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
    
String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;

function validadeForm() {

    var Curso = document.getElementById('curso');
    var CursoID = document.getElementById('cursoid');
    var Pac_ID = document.getElementById('pac_id');
    var Codigo = document.getElementById('codigo');
    var Nome = document.getElementById('nome');
    var Email = document.getElementById('email');
    var CEmail = document.getElementById('conf_email');
    var DDD = document.getElementById('ddd');
    var Prefixo = document.getElementById('prefixo');
    var Sufixo = document.getElementById('sufixo');
    var Unidade = document.getElementById('unidade');
    var URL = document.getElementById('url');
    var Observacao = document.getElementById('obs');
	var Tipo = document.getElementById('tipo');
	var Origem = document.getElementById('origem');
    var combo_SAP = document.getElementById('combo_SAP'); // usado na validação do combo seleção de curso/formação

	Nome.value = Nome.value.trim();
    Email.value = Email.value.trim();
	//CEmail.value = CEmail.value.trim();
    
	//Validação funcional quando existe no form de inetresses o combo de seleção de academia
    if (combo_SAP.value == "1") {
		var CursoFormacao = document.getElementById('CursoFormacao'); // usado na validação do combo seleção de academia
		if (CursoFormacao.value.length == 0) {
			alert('Selecione a Academia!');
			CursoFormacao.focus();
			return false;
		}	
	}	
	//Validação funcional quando existe no form de inetresses o combo de seleção de curso
    if (combo_SAP.value == "2") {
		var CursoFormacao = document.getElementById('CursoFormacao'); // usado na validação do combo seleção de curso
		if (CursoFormacao.value.length == 0) {
			alert('Selecione o Curso!');
			CursoFormacao.focus();
			return false;
		}	
	}	
	//--
	
	if (Nome.value.length == 0) {
        alert('Nome inválido!');
        Nome.focus();
        return false;
    }

    if (!validateEmail(Email.value)) {
        alert('Endereço de e-mail inválido!');
        Email.focus();
        return false;
    }
	
	if((CEmail != null && CEmail.value != Email.value) || CEmail == null)
	{
		alert('Confirmação de e-mail deve ser igual ao e-mail');
        CEmail.focus();
        return false;
	}

    if (DDD.value.length < 2) {
        alert('DDD inválido!');
        DDD.focus();
        return false;
    }
	
	if (DDD.value == '00') {
        alert('DDD inválido!');
        DDD.focus();
        return false;
    }

    if (Prefixo.value.length < 4) {
        alert('Telefone inválido!');
        Prefixo.focus();
        return false;
    }

    if (Sufixo.value.length < 4) {
        alert('Telefone inválido!');
        Sufixo.focus();
        return false;
    }
	
	if (Prefixo.value == '0000' || Prefixo.value == '1111')
	{
        alert('Telefone inválido!');
        Prefixo.focus();
        return false;
	}
	
	if ((Prefixo.value == '2222' && Sufixo.value == '2222') || (Prefixo.value == '3333' && Sufixo.value == '3333') || (Prefixo.value == '4444' && Sufixo.value == '4444') || (Prefixo.value == '5555' && Sufixo.value == '5555') || (Prefixo.value == '6666' && Sufixo.value == '6666') || (Prefixo.value == '7777' && Sufixo.value == '7777') || (Prefixo.value == '8888' && Sufixo.value == '8888') || (Prefixo.value == '9999' && Sufixo.value == '9999'))
	{
        alert('Telefone inválido!');
        Prefixo.focus();
        return false;
	}
	
	
	

    if (Unidade.value.length == 0) {
        alert('Escolha uma unidade!');
        return false;
    }
    var Params = new String();
    Params = "nome=" + escape(Nome.value) + "&email=" + Email.value + "&conf_email=" + CEmail.value + "&ddd=" + DDD.value + "&prefixo=" + Prefixo.value + "&sufixo=" + Sufixo.value + "&curso=" + escape(Curso.value.trim()) + "&unidade=" + Unidade.value + "&obs=" + escape(Observacao.value) + '&CursoID=' + escape(CursoID.value) + '&Codigo=' + escape(Codigo.value) + '&Pac_ID=' + escape(Pac_ID.value) + '&URL=' + escape(URL.value) + '&Tipo=' + escape(Tipo.value) + '&Origem=' + escape(Origem.value);
    goAjax(URL.value, Params);
    return true;
}

function createXMLHTTP() {
    var ajax;
    try {
        ajax = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
        try {
            ajax = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (ex) {
            try {
                ajax = new XMLHttpRequest();
            }
            catch (exc) {
                alert("This browser not support Ajax");
                ajax = null;
            }
        }
    }
    return ajax;
}

var xmlhttp = createXMLHTTP();

function goAjax(url, parametros) {
    xmlhttp.open("POST", url, true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
    xmlhttp.setRequestHeader("Content-length", parametros.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.onreadystatechange = ChangeStatus;
    xmlhttp.send(parametros);
}

function ChangeStatus() {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            clearForm();
            alert('Suas informações foram enviadas com sucesso!!!\n\nAgradecemos seu interesse, logo entraremos em contato.');
        }
    }
}

function clearForm() {
    document.getElementById('nome').value = "";
    document.getElementById('email').value = "";
    document.getElementById('ddd').value = "";
    document.getElementById('prefixo').value = "";
    document.getElementById('sufixo').value = "";
    document.getElementById('unidade').options[0].selected = true;
    document.getElementById('obs').value = "";
}

