/*

Copyright 2009 Steven Glassner

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. 

**
 * author @ steeev.com
 *
 * inspired by the ever so famous SWFObject, reference sources include w3schools.com, sitepoint.com, sergiopereira.com, stackoverflow.com, openjs.com
 *
 * 07.13.09
 * v0.8 added POST functionality.
 * 
 * 07.09.09
 * v0.7 modified url query to slim down code, it made addVariable optional :)
 *
 * 07.08.09
 * v0.6 added silent request feature, if user specifies element as null.  returnFunction now accepts only the function name
 *
 * 07.07.09
 * v0.5 added returnFunction method which allows passing of function names
 *
 * 06.25.09
 * v0.4 slimmed down some of the code, get all methods working properly - hybrid object
 *
 * 06.19.09
 * v0.3 complete restructure, ended with a working beta - non-object argument based
**/
function alpheJAX(){	
	defaults();
	this.a_sess_id=Math.random();
	this.varArray=new Array();
	alpheJAX.prototype.send=function(url, action, type, method){
		var variables="";
		setElementId(action);		
		var xmlHttp=GetXmlHttpObject();		
		if (xmlHttp==null){
			return alert("alpheJAX won't work with your browser");
		}
		if (this.varArray[0]!=null){
			for (var i=0;i<this.varArray.length;i=i+2){
				variables+="&"+this.varArray[i]+"="+this.varArray[i+1];
			}
		}
		if (type==1){
			xmlHttp.onreadystatechange=stateChanged;
		}
		if (method=="GET"){
			variables="?a_sess_id="+this.a_sess_id+variables;
			xmlHttp.open(method,url+variables,s_type(type));
			xmlHttp.send(null);
		}else if(method=="POST"){
			variables="a_sess_id="+this.a_sess_id+variables;
			xmlHttp.open(method,url,s_type(type));
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-Length", variables.length);
			xmlHttp.setRequestHeader("Connection","close");
			xmlHttp.send(variables);
		}
		if (type==2){
			if(action!=""&&action!=null){
				var msg_element=document.getElementById(getElementId());
				var silent=false;
			}else{
				var silent=true;	
			}				
			if (get_func()!=null&&get_func()!=""){
				var functionname=get_func();
				if (!silent){msg_element.innerHTML=get_complete();}
				window[functionname](xmlHttp.responseText);
			}else{
				if (!silent){msg_element.innerHTML=xmlHttp.responseText;}
			}				
			return;
		}
		function GetXmlHttpObject(){
			var xmlHttp=null;
			try{
				xmlHttp=new XMLHttpRequest();
			}catch(e){
				try{
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				}catch(e){
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
			return xmlHttp;
		};
		function stateChanged(){
			if(getElementId()!=""&&getElementId()!=null){
				var msg_element=document.getElementById(getElementId());
				var silent=false;
			}else{
				var silent=true;	
			}
			if (xmlHttp.readyState==1){
				if (!silent){msg_element.innerHTML=get_setup();}
			}
			if (xmlHttp.readyState==2){
				if (!silent){msg_element.innerHTML=get_sent();}
			}
			if (xmlHttp.readyState==3){
				if (!silent){msg_element.innerHTML=get_processing();}
			}
			if (xmlHttp.readyState==4){
				if (get_func()!=null&&get_func()!=""){
					var functionname=get_func();
					if (!silent){msg_element.innerHTML=get_complete();}
					window[functionname](xmlHttp.responseText);
				}else{
					if (!silent){msg_element.innerHTML=xmlHttp.responseText;}
				}
			}
			return;
		};		
	};
	alpheJAX.prototype.addVariable=function(server_var, html_var){
		this.varArray.push(server_var, html_var);
	};
	alpheJAX.prototype.resetState=function(state, value){
		if ((state==1)||(state=="1")){
			setState_setup(value);
		}else if ((state==2)||(state=="2")){
			setState_sent(value);
		}else if ((state==3)||(state=="3")){
			setState_processing(value);
		}else if ((state==4)||(state=="4")){
			setState_complete(value);
		}
	};
	alpheJAX.prototype.returnFunction=function(func_name){
		set_func(func_name);	
	};
	function setElementId(action){
		this.element_id=action;
	};
	function getElementId(){
		return this.element_id;
	};
	function get_setup(){
		return this.state_setup;
	};
	function get_sent(){
		return this.state_sent;
	};
	function get_processing(){
		return this.state_processing;
	};
	function get_complete(){
		return this.state_complete;
	};
	function setState_sent(value){
		this.state_sent=value;
	};
	function setState_setup(value){
		this.state_setup=value;
	};
	function setState_processing(value){
		this.state_processing=value;
	};
	function setState_complete(value){
		this.state_complete=value;
	};
	function set_func(func_name){
		this.func=func_name;	
	};
	function get_func(){
		return this.func;	
	};
	function defaults(){
		this.state_setup="loading.";
		this.state_sent="loading..";
		this.state_processing="loading...";
		this.state_complete="done.";
		this.func=null;
	};
	function s_type(type){
		var return_var=true;
		if (type==1){
			return_var=true;	
		}else if(type==2){
			return_var=false;
		}
		return return_var;
	};
};
