var URL = new Array( "orcamento.inc.php", "orcamento.php" );


function ValidaForm()
{
	var oForm = document.frmOrcamento;
	var blnSubmit = true;
	
	if ( $("tx_nome").value == "" )
	{

		alert('Por favor, informe o seu nome');
		$('tx_nome').focus();
		blnSubmit = false;
		return false;

	}	

	if ( $("tx_empresa").value == "" )
	{

		alert('Por favor, informe a empresa');
		$('tx_empresa').focus();
		blnSubmit = false;
		return false;

	}
	
	if ( $("tx_email").value == "" )
	{

		alert('Por favor, informe o seu e-mail');
		$('tx_email').focus();
		blnSubmit = false;
		return false;

	}

	if ( !validaEmail($F("tx_email")) )
	{

		alert("Por favor, informe um e-mail válido");
		$("tx_email").focus();
		blnSubmit = false;
		return false;
	
	}

	if ( $("tx_telefone").value == "" )
	{

		alert('Por favor, informe o telefone para contato');
		$('tx_telefone').focus();
		blnSubmit = false;
		return false;

	}
	else
	{
		if ( !VerificaNumeros( $F("tx_telefone") ) )
		{
			alert('O campo telefone só aceita números\nInforme DDD e o Número do telefone sem - ou ()');
			$('tx_telefone').focus();
			blnSubmit = false;
			return false;
		}
	}

	//alert( ">> " + document.frmOrcamento.nr_quantidade.length );
	for (var i = 0; i < oForm.nr_quantidade.length; i++ )
	{
		if ( ( oForm.nr_quantidade[i].value != "" ) || ( oForm.tx_descricao[i].value != "" ) || ( oForm.nr_periodo[i].value != "" ))
		{
			if ( oForm.nr_quantidade[i].value == "" )
			{
				alert('Por favor, informe a quantidade para o campo ' + ( i + 1 ) + "!");
				oForm.nr_quantidade[i].focus();
				blnSubmit = false;
				return false;
			}
			else
			{
				if ( !VerificaNumeros( oForm.nr_quantidade[i].value ) )
				{
					alert('O campo só aceita números!');
					oForm.nr_quantidade[i].value = '';
					oForm.nr_quantidade[i].focus();
					blnSubmit = false;
					return false;
				}	
			}

			if ( oForm.tx_descricao[i].value == "" )
			{
				alert('Por favor, informe a descricao para o campo ' + ( i + 1 ) + "!");
				oForm.tx_descricao[i].focus();
				blnSubmit = false;
				return false;
			}

			if ( oForm.nr_periodo[i].value == "" )
			{
				alert('Por favor, selecione o período para o campo ' + ( i + 1 ) + "!");
				oForm.nr_periodo[i].focus();
				blnSubmit = false;
				return false;
			}
		}
	}

	if ( blnSubmit )
	{
		//var strParam = "tx_email=" + $F("tx_email");
		
		//parameters: $("frmContato").serialize(true)
		var objAjaxRequest = new Ajax.Request( URL[0], { method: "post", parameters: $("frmOrcamento").serialize(true), onComplete: LoadPage, onFailure: LoadErro } );

		$("divMsg").style.display = "block";
		$("divMsg").innerHTML = "Executando Requisição";				
	
	}
		
}

function LoadErro(pError) 
{ 
	//alert('Error ' + t.status + ' -- ' + t.statusText); 
	
	$('divMsg').style.display = 'block';
	$('divMsg').innerHTML = '';
	$('divMsg').innerHTML = 'Erro: ' + pError.status + ' -- ' + pError.statusText;
	
}

function PreLoad()
{
	
	ClearObjects();

}

function LoadPage( pRetorno )
{
	
	var arrScript = pRetorno.responseText;
	arrScript = arrScript.split('|');
	
	$('divMsg').innerHTML = '';
	$('divMsg').innerHTML = arrScript[0];
	
	if ( arrScript[1] > 0 )
	{
	
		setTimeout( PreLoad, 5000 );
			
	}
			
}

function ClearObjects()
{
	$('tx_nome').value = '';
	$('tx_empresa').value = '';
	$('tx_telefone').value = '';
	$('tx_email').value = '';
	$('tx_endereco_entrega').value = '';
	$('dt_entrega').value = '';	
	$('tx_comentario').value = '';
	
	for (var i = 0; i < oForm.nr_quantidade.length; i++ )
	{
		oForm.nr_quantidade[i].value = '';
		oForm.tx_descricao[i].value = '';
		oForm.nr_periodo[i].value = '';
	}

	$('divMsg').style.display = 'none';
	

}

function Init()
{

	$('divMsg').style.display = 'none';

	Event.observe( 'btnEnviar', 'click', function(e){ ValidaForm() } );	

}

Event.observe( window, 'load', Init, false );
