//*** greq functions by Andy Kendall (andy _at_ kendalltech.com)***
//*** Nov, 2009 
//*** communication with backend using 'json' format

//************* NOTES ****************************
// all retfunc's need args eval'd
// some functions will use functions from default.js if they are available

var greq = {
	obj : null,
	getXmlHttpRequestObject : function() {
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();	
		}else if(window.ActiveXObject) {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			alert("Error:Your browser has XMLHttp disabled or is too old to properly display this page!");	
		}
	},
	sendreq : function(strin, strfile){
		if (strfile==null){alert("Error: no filename sent to sendreq, strin: "+strin); return;}
		if (greq.obj.readyState==0||greq.obj.readyState==4){
			var rndnum=Math.floor(Math.random()*500);
			params="postsnd="+rndnum+strin;
			greq.obj.open("POST", "proc/"+strfile, true);
			greq.obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			greq.obj.onreadystatechange=greq.handlereq;
			greq.obj.send(params);
		}
	},
	handlereq : function(){
		if (greq.obj.readyState==4){
			var retstr=greq.obj.responseText;
			if (greq.chkerrors(retstr)) return false;
			//alert(retstr);
			eval("retarr="+retstr+";");
			for(y in retarr){
				if (typeof(retarr[y].type)=="undefined"){alert("type var not found in "+y+" element"); return;}
				if (typeof(retarr[y].data)=="undefined"){alert("data var not found in "+y+" element"); return;}
				if (typeof(eval("greq.retfunc."+retarr[y].type))=="function"){
					eval("greq.retfunc."+retarr[y].type+"("+retarr[y].data+");");
				}else alert(retarr[y].type+" function not found");
			}
		}
	},
	chkerrors : function (strin){
		//checks if it's a json formatted string, returns true if there's an error
		if (strin.length==0) return true;
		if (strin.substr(0,1)=="["||strin.substr(0,1)=="{"){
			return false;
		}else{
			alert(strin);
			return true;
		}
	},
	retfunc : {
		filldiv : function(strin){
				arrinfo=eval(strin);
				if (typeof(arrinfo["divid"])!="undefined"&&typeof(arrinfo["fillstr"])!="undefined"){
					if (tmpobj=document.getElementById(arrinfo["divid"])){
						if (tmpobj)tmpobj.innerHTML=arrinfo["fillstr"];
					}
				}else alert("no divid or fillstr sent to filldiv function");
		}, 
		msg : function(strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["msg"])=="undefined") arrinfo["msg"]="No Message sent";
			if (typeof(arrinfo["title"]=="undefined")) arrinfo["title"]="Message";
			if (typeof(arrinfo["bokbutton"]=="undefined")) bokbutton=arrinfo["bokbutton"];
			else bokbutton=true;
			if (typeof(funcs.msgbox)=="function")	funcs.msgbox(arrinfo["msg"], arrinfo["title"], bokbutton);
			else alert(arrinfo["msg"]);
		},
		statusbox:function(strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["msg"])=="undefined"){alert("No msg sent to greq.loadpage (js)"); return;}
			funcs.statusbox(arrinfo["msg"]);
		},
		loadpage : function (strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["url"])=="undefined"){alert("No url sent to greq.loadpage (js)"); return;}
			if (window.location.search.indexOf("showcom")>0) extrastr="&showcom";
			else extrastr="";
			//added just for invoice
			window.location=arrinfo["url"]+extrastr;
		},
		setfocus : function (strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["ID"])=="undefined"){ alert("No ID sent to setfocus (js)"); return;}
			if (tmpobj=document.getElementById(arrinfo["ID"])){
				if (tmpobj) tmpobj.focus();
				else alert("setfocus error: ID "+arrinfo["ID"]+" not found");
			}else alert("setfocus error: ID "+arrinfo["ID"]+" not found");
		},
		setattrib : function(strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["ID"])=="undefined"){ alert("No ID sent to setattrib (js)"); return;}
			if (typeof(arrinfo["attrib"])=="undefined"||arrinfo["attrib"]==""){ alert("No attrib sent to setattrib (js)"); return;}
			if (typeof(arrinfo["value"])=="undefined"){ alert("No value sent to setattrib (js)"); return}
			if (tmpobj=document.getElementById(arrinfo["ID"])){
				if (tmpobj) tmpobj.setAttribute(arrinfo["attrib"], arrinfo["value"]);
				else alert("No element found with ID "+arrinfo["ID"]+" in setattrib (js)");
			}else alert("No element found with ID "+arrinfo["ID"]+" in setattrib (js)");
		},
		setvar : function (strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["var"])=="undefined"||arrinfo["var"]==""){ alert("No var sent to setvar (js)"); return;}
			if (typeof(arrinfo["value"])=="undefined"){ alert("No value sent to setvar (js)"); return;}
			eval(arrinfo["var"]+"=\""+arrinfo["value"]+"\";");
		},
		setval : function (strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["ID"])=="undefined"||arrinfo["ID"]==""){ alert("No ID sent to setval (js)"); return;}
			if (typeof(arrinfo["value"])=="undefined"){ alert("No value sent to setval (js)");return}
			if (tmpobj=document.getElementById(arrinfo["ID"])){
				if (tmpobj){
					tmpobj.value=arrinfo["value"];
				}
			}
		},
		setheight : function (strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["ID"])=="undefined"){alert("No ID sent to setheight (js)");return;}
			if (typeof(arrinfo["height"])=="undefined"){alert("No height sent to setheight (js)");return;}
			if (tmpobj=document.getElementById(arrinfo["ID"])){
				if (tmpobj) tmpobj.style.height=arrinfo["height"]+"px";
				if (funcs.resizebg)funcs.resizebg();
			}
		},
		setstyle : function (strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["ID"])=="undefined"){alert("No ID sent to setstyle");return;}
			if (typeof(arrinfo["stylepart"])=="undefined"){alert("No stylepart sent to setstyle");return;}
			if (typeof(arrinfo["styleval"])=="undefined"){alert("No styleval sent to setstyle");return;}
			if (tmpobj=document.getElementById(arrinfo["ID"])){
				if (tmpobj) tmpobj.style[arrinfo["stylepart"]]=arrinfo["styleval"];
			}
		},
		stylehid : function (strin){
			arrino=eval(strin);
			if (typeof(arrinfo["ID"])=="undefined"){alert("No ID sent to stylehid");return;}
			if (tmpobj=document.getElementById(arrinfo["ID"])) tmpobj.style.display="none";
		},
		styleblock : function (strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["ID"])=="undefined"){alert("No ID sent to styleblock");return;}
			if (tmpobj=document.getElementById(arrinfo["ID"])) tmpobj.style.display="block";
		},
		runfunc: function(strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["funcname"])=="undefined"){alert("No funcname sent to runfunc (js)");return;}
			if (typeof(arrinfo["strparam"])=="undefined"){alert("No strparam sent to runfunc (js)");return;}
			try{eval(arrinfo["funcname"]+"(\""+arrinfo["strparam"]+"\");");}
			catch(err){alert("Invalid function name: "+arrinfo["funcname"]+"(\""+arrinfo["strparam"]+"\"); Or there's an error inside the called function. If you are seeing this error, you might just need to refresh the browser. : "+err.description);return;}
		},
		displaydebug : function(strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["str"])=="undefined"){alert("No str sent to displaydebug (js)");return;}
			if (typeof(funcs.debugmsg)=="function") funcs.debugmsg(arrinfo["str"]);
			else alert(arrinfo["str"]);
		},
		createwin : function(strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["strid"])=="undefined"){alert("No strid sent to createwin (js)");return;}
			if (typeof(arrinfo["strcontent"])=="undefined"){alert("No strcontent sent to createwin (js)");return;}
			if (typeof(arrinfo["strtitle"])=="undefined") arrinfo["strtitle"]="Default Title";
			if (typeof(arrinfo["mtop"])=="undefined")arrinfo["mtop"]=10;
			if (typeof(arrinfo["mleft"])=="undefined")arrinfo["mleft"]=25;
			if (typeof(arrinfo["mzindex"])=="undefined")arrinfo["mzindex"]=12;
			if (typeof(arrinfo["topcolor"])=="undefined") arrinfo["topcolor"]="#AAABFF";
			if (typeof(arrinfo["mwidth"])=="undefined")arrinfo["mwidth"]=500;
			funcs.createwin(arrinfo["strid"], arrinfo["strcontent"], arrinfo["strtitle"], arrinfo["mzindex"], arrinfo["mtop"], arrinfo["mleft"], arrinfo["topcolor"], arrinfo["mwidth"]);
		},
		removewin : function (strin){
			arrinfo=eval(strin);
			if (typeof(arrinfo["strid"])=="undefined"){alert("No strid sent to removewin (js)");return;}
			funcs.removewin(arrinfo["strid"]);
		},
		trackpage : function(strin){
			try{
				pageTracker._trackPageview(strin);
			}catch(err) {}
		}
	}
};
greq.obj = greq.getXmlHttpRequestObject();
