function ajaxSubmit(frm,debug){
	frm= jq(frm);
	var handler = _handler(frm);
	
	//alert("submitting:"+handler+' '+frm.hasClass("submitting"));
	if(!frm.hasClass("filterForm")){
			if(!frm.valid()) return false;
						}
	//debug=true;
	if(!frm.hasClass("submitting")){
		frm.addClass("submitting");
		switch(handler){
			case "pagxbukket":
				presubmitPaginator(frm);
				var data = $(frm).serializeArray();
				//dump(data,"POSTING");
				//alert("posting");
				if(debug){
					dump(data);
					$.post("ajax.php?debug=1", data, ajaxResponse, null);
				} else {
					$.post("ajax.php", data, ajaxResponse,'json');
				}
			break;
			case "logout":
				var data = frm.serializeArray();
				$.post("ajax.php", data);
				//$.post("ajax.php?debug=1", data, ajaxResponse, null);
				document.location= thishref;
			break;
			case "browsecats":
			case "filterForm":
			case "sortDD":
				var data = $(frm).serializeArray();
				//alert("posting");
				dump(data);
				var tar = $(frm).find(":input[name=tar]").val();
				//alert(tar);
				tar = $("#"+tar);
				init(tar,true);
				pagShowLoader(tar);
				fltrStatus(tar,"Loading...<br>");
				$.post("ajax.php", data, ajaxResponse,'json');
				//$.post("ajax.php?debug=1", data, ajaxResponse, null);
			break;
			default:
				var data = $(frm).serializeArray();
				//alert("posting");
				if(!$(frm).find(":hidden[name=tar]").size()){
					data.push({name:'tar',value:$(frm).attr("id")});
				}
				$(frm).announce("Saving...","info");
				dump(data);
				if(debug){
					dump(data);
					$.post("ajax.php?debug=1", data, ajaxResponse, null);
				} else {
					$.post("ajax.php", data, ajaxResponse,'json');
				}
			break;
		}
	}
	return false;
}


function ajaxResponse(response){
	//alert(response);
	
	//dump(response,"ajaxResponse");
	var msg= response.msg;
	var cls = "success";
		
	var frm = $("#frm_"+response.handler);
	if(!frm.size())	frm = $("#frm_"+response.tar);
	if(!frm.size())	frm = $("#"+response.tar);
	if(frm.size()){
		frm.removeClass("submitting");
	
		//alert("submitted response: " +' '+ response.handler+' '+ typeof response.msg+' '+frm.attr("class"));
		if(response.success){
			switch(response.handler){
				case "browsecats":
				case "pagxbukket":
				case "filterForm":
				case "sortDD":
					if(response.tar && response.html){
						//alert(response.html);
						
						replaceContents(response.tar,response.html,false);
						updatePagXbktTxt(response.tar,response.paginfo);
						pagxbktRenumber(response.tar);
					}			
					//dump(response,"pagxbukket");
				break;
				case "logout":
					document.location= thishref;
				break;		
				default:
					//alert(response.tar);
					customresponse= custom_frmSubmitted(frm,response);
					if(customresponse){
						msg= customresponse.msg;
						cls= customresponse.cls;
					} else {
						if(response.paginfo && response.tar){
							updatePagXbktTxt(response.tar,response.paginfo);
						}	
						dump(response,"default");
					}
				break;
			}
		} else {
			cls="error";
		}
		//alert('cls='+cls);
		if(msg!=undefined) frm.announce(msg,cls);
	} else {
		dump(response,"ajaxResponse");
		alert("unable to locate submitted form");
	}
}






var output;
function dump(o,title){
	if(!title) title="DUMP";
	output="";
	read(o,0,1);
	//$('#dump').hide();
	if($("#dump").size()) {
		$('#dump')//.css({"width":"300px","margin":"10px","border":"1px solid blue","float":"none","clear":"both"})
		.prepend("<div style='float:none;display:block;'>"+title+"<br>"+output+"</div>");
	}
}
function read(o,d,f) {  
	if(!d)d=0;
	var nextd= d+1;
	var indent='\n<br>';
	var tab='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	for(t=0;t<=nextd;t++){
		indent+=tab;
	}
	var type = typeof o;     
	
	if (type == "object") {
		if($(o).attr("tagName")!=undefined){  
			var params={
				'tagName':$(o).attr("tagName"),
				'id':$(o).attr("id"),
				'class':$(o).attr("class")
				};
			output+=indent+"<strong>DOM object</strong>";     
			if(d<10) read(params,nextd,f);   
		} else {
			for(var key in o){
				output+=indent+"<strong>"+ key+"</strong>";     
				if(d<10 && key.indexOf("jQuery")==-1) read(o[key],nextd,f);        
			} 
		}
	} else {
		//if(f || type!="function") output+=indent+o;
		if(f) output+=indent+o;
			
	}
}
