/*
|	Custom Ajax Function 
|	File:		async_ajax.js
|	Author:		Arnie Alejandro
|	Email:		einra2k2@gmail.com
|	Date:		22 DEC 07
|	Version:	1.0.8|	
*/

function f_ajax(method, url, params, id, toggle, trigger) {	
	var xmlHttp;
	var call;
	
	if(window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
		action(xmlHttp);
	}
	else if(window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		action(xmlHttp);
	}	
	function action(xmlHttp) {
		switch(method) {
			case 'get':
			get(xmlHttp);
			break;			
			case 'post':
			post(xmlHttp);
			break;
			default:
			alert('Must be "get" or "post". Yours is: '+method);	
		}
	}
	// Get Method
	function get(xmlHttp) {
		xmlHttp.open("GET", url+"&dummy="+new Date().getTime(), true);
		xmlHttp.send(null);
		fPass(xmlHttp);	
	}
	// Post Method
	function post() {
		xmlHttp.open("POST", url, true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
		fPass(xmlHttp);
	}
	function fPass(xmlHttp) {
		xmlHttp.onreadystatechange=function(){			
			if(xmlHttp.readyState == 4) {
				response = xmlHttp.responseText;				
				switch(toggle) { 					
					case 'html'	:	
								f_innerHTML(id, response);
								break;
					
					case 'trigger':								
								trigger(response);
								break;
					
					case 'edit' :	
								json = eval('('+response+')');
								f_edit_ajax(json);
								break;				
					
					case 'login' :													
								if(response == 1) {
									window.location = '/administrator'
								}else {
									f_innerHTML('login_message', 'Invalid Username or Password');
								}
								break;
					
					
					case 'alert' : 
								alert(response);
								break;	
								
					default		: 
								var noaction = 'no action';
				}				
			}			
		}
		
	}	
}

