/*
http://www-128.ibm.com/developerworks/web/library/wa-ajaxintro3/
*/

/**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
**/
 
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


var iAjax = ({

	init: {
		url: window.location,
		sample: 'hello'
	},
	
	ajax: function(){
		var ajaxRequest;  // The variable that makes Ajax possible!
		 
		try {
			// Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
			
		} catch (e) {
			// Internet Explorer Browsers
			try {
				
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
				
			} catch (e) {
				
				try {
					
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
					
				} catch (e) {
					// Something went wrong
					alert("Your browser dont support Ajax.");
					return false;
				}
			}
		}
		return ajaxRequest;
	},
	
	get: function(id, query, type){
		//alert ('sdfd');
		var ajaxRequest = this.ajax();
		
		ajaxRequest.onreadystatechange = function(){
			var state = ajaxRequest.readyState;
			//var response = ajaxRequest.responseText;
			
			// The request is uninitialized (before you've called open()).
			if(state == 0){
				//alert("State is "+state);
			}
			// The request is set up, but not sent (before you've called send()).
			if(state == 1){
				//alert("State is "+state);
			}
			// The request was sent and is in process (you can usually get content headers from the response at this point).
			if(state == 2){
				//alert("State is "+state);
			}
			// The request is in process; often some partial data is available from the response, but the server isn't finished with its response.
			if(state == 3){
				//alert("State is "+state);
			}
			if(state == 4){
				// The response is complete; you can get the server's response and use it.
				//alert(ajaxRequest.getAllResponseHeaders());
				if (ajaxRequest.status == 200) {
				//	alert (ajaxRequest.responseText);
					if(type=='inner'){
						document.getElementById(id).innerHTML = ajaxRequest.responseText;
					} else {
						document.getElementById(id).value = ajaxRequest.responseText;
					}
				} else if(ajaxRequest.status == 404){
					alert("Requested URL is not found.");
				} else if(ajaxRequest.status == 403) {
					//alert("Access denied.");
				} else {
					//alert("Status is "+ajaxRequest.status);
				}
			}
		}
		
		//ajaxRequest.open("GET", this.init.url+query, true);
		ajaxRequest.open("GET", query, true);
		ajaxRequest.send(null);
	},
	
	post: function(file, objform, id, type){
		
		var params = this.getFormValues(objform);
		
		var ajaxRequest = new this.ajax();
		
		var type = (type ? type : 'inner');
		
		ajaxRequest.onreadystatechange=function(){
			if (ajaxRequest.readyState == 4){
				if(ajaxRequest.status == 200 || window.location.href.indexOf("http") == -1){
					//document.getElementById(id_display).innerHTML = ajaxRequest.responseText;
					if(type=='inner'){
						document.getElementById(id).innerHTML = trim(ajaxRequest.responseText);
					} else {
						document.getElementById(id).value = ajaxRequest.responseText;
					}
				} else {
					alert("An error has occured in making the request.");
				}
			}
		}
		
		ajaxRequest.open("POST", file, true);
		ajaxRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8"); 
		ajaxRequest.setRequestHeader("Content-length", params.length);
		ajaxRequest.setRequestHeader("Connection", "close");
		ajaxRequest.send(params);
		
		return ajaxRequest;
	},
	
	getFormValues: function(fobj){

		var str = "";
		var field;
		//var valueArr = null;
		//var val = "";
		//var cmd = "";
		//var valFunc = "";
		
		for(var i=0; i<fobj.elements.length; i++){
			
			field = fobj.elements[i];
			
			switch(field.type.toLowerCase()){
				case "text":
					str += field.name+"="+escape(field.value)+"&";
					break;
					
				case "textarea":
					str += field.name+"="+escape(field.value)+"&";
					break;
					
				case "checkbox":
					if(field.checked == true){
						str += field.name+"="+escape(field.value)+"&";
					}
					break;
					
				case "radio":
					if(field.checked == true){
						str += field.name+"="+escape(field.value)+"&";
					}
					break;
					
				case "hidden":
					str += field.name+"="+escape(field.value)+"&";
					break;
					
				case "password":
					str += field.name+"="+escape(field.value)+"&";
					break;
			}
			
			switch(field.tagName.toLowerCase()){
					
				case "select":
					str += field.name+"="+escape(field.options[field.selectedIndex].value)+"&";
					break;
					
				default:	
					str += field.name+"="+escape(field.value)+"&";
					break;
			}
			
			switch(field.className){
					
				case "selection":
					str += fobj.elements[i].name+"="+escape(fobj.elements[i].value)+"&";
					break;
			}
		}
	    
		str = str.substr(0,(str.length - 1));
		
		return str;
    }
	
});