function ChecarForm(form, nomes, ajax){
var ValidaOK = "N";
var sendAjax = "";
nomes = ' ' + nomes + ' ';

for (i=0; i<form.length; i++){
	//var TextBoxes = form[i].name.indexOf("Txt");
	//var ComboBoxes = form[i].name.indexOf("Cmb");
	//if (TextBoxes != -1 || ComboBoxes != -1)
		//{
		if ((form[i].type != "submit") && (form[i].type != "reset")){
			var lbl = document.getElementById('lbl_' + form[i].name);
			sendAjax += '&' + form[i].name + '=' + escape(form[i].value);
			if (nomes.indexOf(form[i].name) > 0){
				if (form[i].value.length < 3){
					var nome = lbl.innerHTML;
					nome = nome.replace("<u>", "");
					nome = nome.replace("</u>", "");
					
					lbl.style.color = 'red';
					ValidaOK = "S";
					form[i].focus();
				}else if (form[i].name == "email" && doEmail(form[i].value, 1) == false){
					if (lbl) lbl.style.color = 'red';
					ValidaOK = "S";
					form[i].focus();
					
					alert('O e-mail informado é inválido!');
				}else{
					if (lbl) lbl.style.color = '';
				}
			}
		}
}
if (ValidaOK == "S"){
	alert("Alguns campos obrigatórios não foram preenchidos ou preenchidos incorretamente\nFavor informar os campos destacados em vermelho com no mínimo 3 digitos!");
	return false
}else{
	if (ajax == 'S'){
		
		try{
    	xhttp = new XMLHttpRequest();
		}catch(ee){
				try{
						xhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}catch(e){
						try{
								xhttp = new ActiveXObject("Microsoft.XMLHTTP");
						}catch(E){
								xhttp = false;
						}
				}
		}
		
		if (!xhttp){
			return true;
		}else{
			xhttp.open('POST', form.action + '?ajax=S', true);
			//xhttp.setRequestHeader("Content-Type: text/html;  charset=ISO-8859-1", true);
			xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			xhttp.onreadystatechange = function(){
				if (xhttp.readyState == 4){
					if (xhttp.status == 200){
						var texto = xhttp.responseText;
						if (texto == 'OK'){
							form.reset();
							alert('Mensagem enviada com sucesso!\nAgradecemos o contato!');
						}else{
							alert('Devido à erros desconhecidos, sua mensagem não pode ser enviada!\nPor favor tente novamente mais tarde ou envie um e-mail para \"homecam@homecam.com.br\"');
						}
					}else{
						alert('Devido à erros desconhecidos, sua mensagem não pode ser enviada!\nPor favor tente novamente mais tarde ou envie um e-mail para \"homecam@homecam.com.br\"...');
					}
				}
			}
			
			sendAjax = sendAjax.substr(1);
			xhttp.send(sendAjax);
			return false;
		}
		
		
		/*
		iHeight = 400;
		iWidth = 500;
		iTop = (screen.height - iHeight) / 2;
		iLeft = (screen.width - iWidth) / 2;
		if (! window.focus)return true;
		popupWin = window.open(form.action,form.id,'toolbar=no,location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, left=' + iLeft + ', top=' + iTop + ', height=' + iHeight + ',width=' + iWidth + '');
		if (!popupWin.opener) popupWin.opener = self;
		form.target=form.id;
		popupWin.focus();
		*/
	}
}
}

var reEmail1 = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
var reEmail2 = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
var reEmail3 = /^[\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}\])$/;
var reEmail = reEmail3;
			 
function doEmail(pStr, pFmt){
	//eval("reEmail = reEmail" + pFmt);
	if (reEmail.test(pStr)) {
		return true;
	} else if (pStr != "") {
		return false;
	}
}

function escreveBotoes(nomeForm, checagem){
	document.write('<input class="botao" value="Enviar Dados" type="button" onclick="javascript: return ChecarForm(' + nomeForm + ', \'' + checagem + '\',\'S\');"></input>&nbsp;<input class="botao" value="Limpar" type="reset" onclick="javascript: window.document.' + nomeForm + '.focus()"></input>');
}
