// ####### AJAX #######

function ajaxPost( form , t , action , actionparams , clear ){
	if( typeof( action ) == "undefined" ){
		action = "alert";
	}
	if( typeof( actionparams ) == "undefined" ){
		actionparams = false;
	}
	if( typeof( clear ) == "undefined" ){
		clear = false;
	}

	var objForm = document.getElementById( form );
	var params = "t=" + t;
	
	for ( var i = 0 ; i < objForm.elements.length ; i++ ){
		var obj = objForm.elements[ i ];
		if( obj.tagName == "INPUT" ){
			if( obj.type == "text" || obj.type == "password" || obj.type == "hidden" ){
				params += "&" + obj.name + "=" + obj.value;
				if( clear ){
					obj.value = "";
				}
			}
		}else if( obj.tagName == "TEXTAREA" ){
			params += "&" + obj.name + "=" + obj.value;
			if( clear ){
				obj.value = "";
			}
		}else if( obj.tagName == "SELECT" ){
			params += "&" + obj.name + "=" + obj.options[ obj.selectedIndex ].value;
			if( clear ){
				obj.selectedIndex = 0;
			}
		}
	}
	
	loadXMLDoc( params , action , actionparams );
}

function loadXMLDoc(params, action, actionparams){
	url = "_ajax.php";
	if(typeof(params) == "undefined" ){
		params = "";
	}
	xmlhttp=null;
	if (window.XMLHttpRequest) {
		// code for Firefox, Opera, IE7, etc.
  		xmlhttp=new XMLHttpRequest();
  	}else if (window.ActiveXObject) {
  		// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	if (xmlhttp!=null) {
		if( action == "innerhtml" || action == "innerhtmlalert" ){
			document.getElementById(actionparams).innerHTML = "<center><img src=\"img/ajax-loader.gif\" id=\"ajaxload\" /></center>";
		}
  		xmlhttp.onreadystatechange = function(){state_Change(xmlhttp, action, actionparams);};
		xmlhttp.open("POST", url, true);
		//Send the proper header information along with the request
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Accept-Charset","UTF-8");
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.send((params == "")?null:params);
  	}else{
  		alert("Seu browser não suporta XMLHTTP.");
  	}
}
 function state_Change(xmlhttp, action, actionparams){
	if (xmlhttp.readyState==4){
		// 4 = "loaded"
  		if (xmlhttp.status==200){
  			// 200 = "OK"
  			switch(action){
  				case "innerhtml":
  					document.getElementById(actionparams).innerHTML = xmlhttp.responseText;
  				break;
  				case "innerhtmlalert":
  					resp = xmlhttp.responseText.split(";!;!;!;!;");
  					if(resp[0] != ""){
						alert(resp[0]);
  					}
  					document.getElementById(actionparams).innerHTML = resp[1];
  				break;
  				case "okredirect":
  					if( xmlhttp.responseText == "ok" ){
						document.location.href = actionparams;
  					}else{
						alert( xmlhttp.responseText );
  					}
  				break;
  				case "okeval":
  					if( xmlhttp.responseText == "ok" ){
						eval( actionparams );
  					}else{
						alert( xmlhttp.responseText );
  					}
  				break;
				default:
					alert(xmlhttp.responseText);
				break;
  			}
    	}else{
    		alert("Erro ao receber informação, tente novamente."/* + xmlhttp.statusText*/);
    	}
  	}
}
