/*

Copyright 2010 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, onlamp.com
 *
 * 03.05.10
 * v1.0 release
 *
 * 09.17.09
 * v0.9	renamed returnFunction to callback, fixed ? bug w/ post.  slimmed s_type conditional.  resetState now requires integer. reverted back to buggy async calls.  added escape on array values for protection.
 * note to self:  thinking of removing element id, might want to keep everything silent and allow callback to decide what to do. // scratch that
 *
 * 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.  callback now accepts only the function name
 *
 * 07.07.09
 * v0.5 added callback 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
**

+known issues:
	1.  multiple asyncronous calls (w/ different object names) results in re-use of xmlHttp even when call is incomplete - state changed functions get shared;

O.o  notes:
	1.  ie only allows 2 open connections.

**/
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;
		xmlHttp=GetXmlHttpObject();
		if (this.varArray[0]!=null){
			for (var i=0;i<this.varArray.length;i=i+2){
				variables+="&"+this.varArray[i]+"="+escape(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(){			
				try{
					xmlHttpObj=new XMLHttpRequest();
				}catch(e){
					try{
						xmlHttpObj=new ActiveXObject("Msxml2.XMLHTTP");
					}catch(e){
						xmlHttpObj=new ActiveXObject("Microsoft.XMLHTTP");
					}
				}
			return xmlHttpObj;
		};
		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){
			setState_setup(value);
		}else if (state==2){
			setState_sent(value);
		}else if (state==3){
			setState_processing(value);
		}else if (state==4){
			setState_complete(value);
		}
	};
	alpheJAX.prototype.callback=function(func_name){
		set_func(func_name);	
	};
	function setIndex(val){
		this.index=val;
	}
	function getIndex(){
		return this.index;
	}
	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==2){
			return_var=false;
		}
		return return_var;
	};
};
