///AjaxMessage
///Summary: AjaxMessage object definition. Used to make Ajax requests
function AjaxMessage(){
	var req;
	var url='';
	var datakeys=new Array();
	var datavalues=new Array();
	var fncallbk=null;
	var fnOnError=null;
	
	this.SetURL=setUrl;							//Set property, used to set the URL of the handler to request
	this.Post=postXMLDoc;						//method, posts form elements to specified URL : Post()
	this.Get=loadXMLDoc;						//method, gets specified URL: Get()
	this.AddPostElement=addData;				//method, add a name/value pair to be sent when Post is called: AddPostElement(key,value)
	this.AddInputAsElement=_addinput;			//method, add a name/value pair to be sent when Post is called,but get the name from the input's id and the value from the input's value: AddInputAsElement(htmlobj(object))
	this.AddInputIDAsElement=_addinputid;		//method, overloads AddInputAsElement: AddInputAsElementID(htmlobjID(string))
	this.AddPostArrayElement=_addarray;			//method, add a name/value where the value is an array
	this.SetOnDataReturn=setfunc;				//Set property, to set OnDataReturn event handler
	this.ResponseString=null;					//Get property, string, the string returned from the get or post
	this.SetOnError=_setOnError;				//Set property, to set the OnError event handler
	this.Abort=_abort;							//method, cancels the current request
	this.ThrowErrorOnServerError=false;			//bool, true= an error will be thrown, false= error appears as alert box
	this.HadError=false;						//bool, true= and error occured during the request
	this.ErrorMessage=null;						//string, the error message text when HadError=true
	this.Action=null;							//int, the Action parameter sent along with the request
	this.CommandArgument=null;					//string, the CommandArgument parameter sent along with the request
	this.UseSecureConnection=false;
	
	///this is really only used by the dLogin dialog so that it won't set the current request as the login request
	this.AllowSetCurrentAjaxRequest=true; 
	
	function _abort(){
		req.abort();
	}
	
	function _setOnError(f){
		fnOnError=f;
	}
	
	function setfunc(f){
		fncallbk=f;
	}
	
	function dispose(){
		req=null;
		url='';
		divID='';
	}

	function setUrl(val){
		url=val;
	}
	
	function _addinputid(inputid){
		_addinput(document.getElementById(inputid));
	}
	function _addinput(input){
		if(input.type=='text' || input.type=='password' || input.type=='hidden'){
			addData(input.id,input.value);
		}
		if(input.tagName.toLowerCase()=='select'){
			if(input.options.length>0)
				addData(input.id,input.options[input.selectedIndex].value);			
		}
	}
	function addData(Key,Value){
		var bAdd=true;
		for(var i=0;i<datakeys.length;i++){
			if(datakeys[i]==Key){
				bAdd=false;
				break;
			}
		}
//		if(isNaN(Value)){
//			if(Value.indexOf('&')>-1){
//				Value=Value.replace(/&/g,'');
//			}
//		}
		Value=escape(Value);
		if(bAdd){
			datakeys[datakeys.length]=Key;
			datavalues[datavalues.length]=Value;
		}
	}
	function _addarray(Key,Array){
		var s='';
		for(var i=0;i<Array.length;i++){
			s+=Array[i]+',';
		}
		if(s!='')
			s=s.substring(0,s.length-1);
		addData(Key,s);
	}
		
	function postXMLDoc(data) {
		getReq();
		if(req) {
			if(url==''){
				alert('URL has not been set!');
				return;
			}
			if(this.Action!=null)
				addData('action',this.Action);
			if(this.CommandArgument!=null)
				addData('commandarg',this.CommandArgument);
			if(parent.thisPage)
				addData('singlesignon',parent.thisPage.SingleSignOnCapable);			
			else
				addData('singlesignon',thisPage.SingleSignOnCapable);
			if(data==null){
				data='';
				for(var i=0;i<datavalues.length;i++){
					if(data!=''){data+='&';}
					data+=datakeys[i]+'='+datavalues[i];
				}
			}
			req.onreadystatechange = processReqChange;
			if(this.UseSecureConnection)
				url='https://secure.sightlines.com'+url;
			req.open("POST", url, true);
			req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			req.send(data);
			if(this.AllowSetCurrentAjaxRequest){
				var cr=new CurrentAjaxRequest();
				cr.URL=url;
				cr.DataKeys=datakeys;
				cr.DataValues=datavalues;
				cr.OnCallBack=fncallbk;
				cr.OnError=fnOnError;
				cr.IsPost=true;
				cr.ThrowErrorOnServerError=this.ThrowErrorOnServerError;
				cr.HadError=this.HadError;
				cr.ErrorMessage=this.ErrorMessage;
				thisPage.CurrentAjaxRequest=cr;
			}
		}
	}
	function loadXMLDoc() {
		getReq();
		if(req) {
			if(url==''){
				alert('URL has not been set!');
				return;
			}
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send("");
			if(this.AllowSetCurrentAjaxRequest){
				//create a CurrentAjaxRequest object to hold the AjaxMessage parms
				//so we can recreate and execute the last request after the user logs in
				//if they were required too re-login after a session timeout.
				var cr=new CurrentAjaxRequest();
				cr.URL=url;
				cr.DataKeys=datakeys;
				cr.DataValues=datavalues;
				cr.OnCallBack=fncallbk;
				cr.OnError=fnOnError;
				cr.IsPost=false;
				cr.ThrowErrorOnServerError=this.ThrowErrorOnServerError;
				cr.HadError=this.HadError;
				cr.ErrorMessage=this.ErrorMessage;
				thisPage.CurrentAjaxRequest=cr;
			}
		}
	}

	function getReq(){
		req = false;
		// branch for native XMLHttpRequest object
		if(window.XMLHttpRequest) {
    		try {
				req = new XMLHttpRequest();
			} catch(e) {
				req = false;
			}
		// branch for IE/Windows ActiveX version
		} else if(window.ActiveXObject) {
       		try {
        		req = new ActiveXObject("Msxml2.XMLHTTP");
      		} catch(e) {
        		try {
          			req = new ActiveXObject("Microsoft.XMLHTTP");
        		} catch(e) {
          			req = false;
        		}
			}
		}
		if(!req){
			throw 'XMLHttpRequest object could not be initialized';
		}
	}



	function processReqChange() {
		if(req==null){return;}
		if (req.readyState == 4) {
			if(req.status==500){
				this.HadError=true;
				//alert(req.responseText);
				this.ErrorMessage="Server Error!:"+_FormatServerError(req.responseText);
			}else if(req.status==404){
				this.HadError=true;
				this.ErrorMessage="Server Error!:Handler page was not found";
			}else if (req.status == 200) {
				var resp=new AjaxResponse();
				resp=eval('('+req.responseText+')');
				this.ResponseString=req.responseText;
				if(resp.IsError){
					if(fnOnError){fnOnError(resp);}
				}else{
					if(resp.SessionTimedOut)
						thisPage.InvokeLoginDialog();
					else
						if(fncallbk){fncallbk(resp);}			
				}
			} else {
				this.HadError=true;
				this.ErrorMessage="There was a problem retrieving the data:\n" + req.statusText;
			}
			if(this.HadError){
				if(this.ThrowErrorOnServerError){
					throw this.ErrorMessage;
				}else{
					if(fnOnError){
						var resp=new AjaxResponse();
						resp.IsError=true;
						resp.Message=this.ErrorMessage;
						fnOnError(resp);
					}else{
						alert(this.ErrorMessage);
					}
				}
			}
			dispose();
		}
	}
	function _FormatServerError(text){
		var ret='';
		var s=text.indexOf('<title>')+7;
		var e=text.indexOf('</title>',s);
		ret=text.substring(s,e);
		return ret;
	}
}
///AjaxResponse
///Summary: AjaxResponse object definition, returned by the AjaxMessage Object, coresponds to code/AjaxRequest.cs
function AjaxResponse(){
	this.IsError=false;				//bool
	this.Message=null;				//string
	this.CommandArgument=null;		//string
	this.SessionTimedOut=false;		//bool
	this.Action=null;				//int
	this.PostElements=new Array();	//Array of FormElements
}
///FormElement
///Summary: FormElement object definition. Items in the PostElements array of an AjaxResponse.
function FormElement(){
	this.Name=null;					//string
	this.Value=null;				//string
}