/**
 * @author martin
 * 
 * res_req muligheder:
 * 
 * "template" + "fil.tpl"
 * "txt" 
 * "json"
 * "txt" 
 * 
 * "none" (lidt teoretisk)
 * 
 * statiske actions: 
 * "exit" dør uden at svare tilbage 
 * "fetch_std_template" svarer en template uden php-data. (f.eks. til popups) 
 */

var a = {
	rpc : "ajax_pages/rpc.php", //where to direct the default ajax request
//	action: "exit", // what are we requesting?  
//	res_req: "none", // what do we want back? 
//	template_file: "", // if a template is requested - what template? 
	returnUrl: window.location.pathname, 
// 	loaderIcon: "ajax-loader-icon", // uncomment if there is no ajax loader icon on page
	
/*	set: function(inAction, inRes_req, inTpl_file) {
		//a.rpc = inRpc;
		a.action = inAction; 
		a.res_req = inRes_req;
		if (a.res_req == "template" && !inTpl_file){
			alert("Hvilken template?");
		} else if (inTpl_file){
			a.template_file = inTpl_file;
		} 
	},
*/	
/*
	meta : function(){
		if (a.res_req=="template" && a.template_file==""){
			alert("Template ikke defineret"); 
		}
		var meta = "meta[action]="+a.action+"&meta[res_req]="+a.res_req;
		if (a.res_req == "template") {
			meta += "&meta[template_file]="+a.template_file; 
		}
		return meta;
	},
*/
	reqPost : function(callbackFunction, inputData) { 
		if (a.loaderIcon) {
			document.getElementById(a.loaderIcon).style.display='';
		}
		a.returnFunction = callbackFunction;
		if (inputData) {
			a.postdata = inputData;
			//a.postdata = a.meta()+"&"+inputData
			a.cObj = YAHOO.util.Connect.asyncRequest("POST", a.rpc, a.callback, a.postdata);
		} else {
			//a.postdata = a.meta();
			//alert("poster");
			a.cObj = YAHOO.util.Connect.asyncRequest("POST", a.rpc, a.callback);
		}
	}, 
	reqGet: function(callbackFunction, inputData) {
		if (a.loaderIcon) { document.getElementById(a.loaderIcon).style.display='';	}
		a.returnFunction = callbackFunction;
		if (inputData) {
			a.postdata = inputData;
			//a.postdata = a.meta()+"&"+inputData
			a.cObj = YAHOO.util.Connect.asyncRequest("GET", a.rpc, a.callback, a.postdata);
		} else {
			//alert("Needs function for processing post-data");
			//a.postdata = a.meta();
			a.cObj = YAHOO.util.Connect.asyncRequest("GET", a.rpc, a.callback);
		}
	},
	callback : {
		success: function(o) {
			if (a.loaderIcon) {
				document.getElementById(a.loaderIcon).style.display='none';
			}
			a.returnFunction(o);
			// alert("svar: "+o.responseText);
		}, 
		failure: function(o){
			if (confirm("Der skete en ajax-fejl. Vil du se returobjektet?")){
				//debug.printObj(o);
				debug.alertObj(o);
			}
		}, 
		upload: function(o){
			if (a.loaderIcon) {
				document.getElementById(a.loaderIcon).style.display='none';
			}
			a.returnFunction(o);
		} 
	}, 
	reload:function(o){

//		success: function(o){
			//alert("siden reloades fra ajax-wrap");
			tempUrl = a.returnUrl;
			a.returnUrl = window.location.pathname;
			window.location.replace(tempUrl); // reloader siden
		//	window.location.replace(window.location.pathname); // reloader siden
//		},
//		failure: function(o){
//			a.hideLoaderIcon();
//			alert("could not load to page");
//		}
	},
	noCallback: function(o){
		// do nothing
	},
	form : function(formId){
 		YAHOO.util.Connect.setForm(document.getElementById(formId));  
	}, 
	fileForm: function(formId) {
		YAHOO.util.Connect.setForm(document.getElementById(formId),true); 
		// ssl & IE: 
		//YAHOO.util.Connect.setForm(document.getElementById(formId), true, true);   
	},
	jdata: function(inputJson){
		return eval('(' + inputJson + ')'); 
	}, 
	hideLoaderIcon: function(){
		if (a.loaderIcon) {
			document.getElementById(a.loaderIcon).style.display='none';
		}
	}, 
	redirect: function(url){
		window.location.replace(url); 
	}
	
}

var debug = {
	printObj: function(obj) {
		//var str = "<html><body>Object: "+obj+"\n\n"; 
		var str = "Object: "+obj+"</br></br>\n\n"; 
		for (property in obj) {
			str += property + " : " + obj[property] + "</br>\n";
		}
		//str += "</body></html>"
		//var page = document.innerHTML = str;
		var debugWindow = window.open("","prtObj","location=yes");
		debugWindow.document.write(str);
		//window.(str);
	}, 
	alertObj: function(obj) {
		var str = "Object: "+obj+"\n\n"; 
		for (property in obj) {
			str += property +" : "+obj[property]+"\n";
		}
		alert(str);
	}
};