/************************* Uteis Ajax ****************************/
function _getActiveXPrefix() {
	if(_getActiveXPrefix.prefix)
		return _getActiveXPrefix.prefix;
	list = ["MSXML5", "MSXML4", "MSXML3", "MSXML2", "MSXML", "Microsoft"];
	for(i=0; i<list.length; i++) {
		try {
			obj = new ActiveXObject(list[i]+".XMLHTTP");
			obj = null;
			return _getActiveXPrefix.prefix = list[i];
		}
		catch(e){}
	}
}
/******************************* Box Erro ************************/
function Erro(field,msg,w){
	var obj = document.getElementById('divErro');
		obj.style.visibility = 'visible';
		obj.style.width = w;
		obj.style.height = '18px';
		obj.innerHTML = msg;
		document.getElementById(field).focus();
		var t = setTimeout("tp()",5000);
	}
function tp(){
	document.getElementById('divErro').style.visibility = 'hidden';
}

/***** Cria o objeto XMLHttpRequest ***********/
function createXMLHTTP() {
	try{
		oRequest = new XMLHttpRequest();
	}catch(ee){
		try{
			oRequest = new ActiveXObject(_getActiveXPrefix()+".XMLHTTP");
		}catch(e){
			try{
				oRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				oRequest = false;
			}
		}
	}
	return oRequest;
}

function field(campo){
	document.getElementById(campo).focus();
	document.getElementById(campo).value = '';
}

function validacadastro(campo,valor)
{
	// criacao do objeto XMLHTTP
	var oHTTPRequest = createXMLHTTP(); 
	oHTTPRequest.open("POST", "valcadastro.php", true);
	oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	oHTTPRequest.onreadystatechange=function() {
		if (oHTTPRequest.readyState==4){
		texto = oHTTPRequest.responseText;
			if(texto == 1){
				alert('Já existe um registro com esse valor');
				field(campo);
			} else if(texto == 2){}
			else if(texto == 3){ 
				if(confirm('Existem registros relacionados a este cliente, deseja excluir?')) {
							oHTTPRequest.open("POST", "delcliente.php", true);
							oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
							oHTTPRequest.onreadystatechange=function() {
								if (oHTTPRequest.readyState==4){
									texto2 = oHTTPRequest.responseText;
									alert('Registro excluído com sucesso!');
									window.location.href=texto2+'.php';
								}
							}
							oHTTPRequest.send("campo="+campo+"&valor="+valor);
				} else {}
		
			}  else if(texto == 4){
				if(confirm('Deseja excluir o registro?')) {
							oHTTPRequest.open("POST", "delcliente.php", true);
							oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
							oHTTPRequest.onreadystatechange=function() {
								if (oHTTPRequest.readyState==4){
									texto2 = oHTTPRequest.responseText;
									alert('Registro excluído com sucesso!');
									window.location.href=texto2+'.php';
								}
							}
							oHTTPRequest.send("campo="+campo+"&valor="+valor);
				} else {}
			}
		}
	}
	oHTTPRequest.send("campo="+campo+"&valor="+valor);
}


