/*
*ajax core
*Written by Roy Tang
*Copyright 2006, 8j Inc.
*/
var createRequest=function() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}
var queryString=function(_56,_57){
	if(arguments.length==1&&(typeof (_56)=="string"||(typeof (_56.nodeType)!="undefined"&&_56.nodeType>0))){
		var kv=MochiKit.DOM.formContents(_56);
		_56=kv[0];
		_57=kv[1];
	}else{
		if(arguments.length==1){
			var o=_56;
			_56=[];
			_57=[];
			for(var k in o){
				var v=o[k];
				if(typeof (v)!="function"){
					_56.push(k);
					_57.push(v);
				}
			}
		}
		var _5c=[];
		var len=Math.min(_56.length,_57.length);
		var _5e=urlEncode;
		for(var i=0;i<len;i++){
			v=_57[i];
			if(typeof (v)!="undefined"&&v!==null){
				_5c.push(_5e(_56[i])+"="+_5e(v));
			}
		}
		return _5c.join("&");
	}
}
var urlEncode=function(_6e){
	if(typeof (encodeURIComponent)!="undefined"){
		return encodeURIComponent(_6e).replace(/\'/g,"%27");
	}else{
		return escape(_6e).replace(/\+/g,"%2B").replace(/\"/g,"%22").rval.replace(/\'/g,"%27");
	}
}
var remoteAction=function(method, action, params, whenDone, sendRequest) {
	var theURL=action;
	if (action.charAt(0)=='/' || action.indexOf("http:")>=0) {
		theURL=action
	}else {
		theURL='/'+action
	}
	var postdata='';
	if(params) {
		postdata=queryString(params);
		if(postdata && (method=="GET")) {
			theURL=theURL + "?" + postdata;
			postdata="";
		}
	}
	var req=createRequest();
	req.onreadystatechange=function() {
		if (req.readyState==4) {
			if (whenDone) whenDone(req.responseText);
		}
	}
	sendRequest(req, theURL, postdata);
}

var asyncCommand=function(commandType) {
	return function(action, params, whenDone) {
		remoteAction(commandType, action, params, whenDone, 
			function(req, theURL, theData) {
				req.open(commandType, theURL, true);
				req.setRequestHeader('HTTP-Command-Equiv', commandType);
				req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				req.send(theData);
			})
	}
}

function setAjaxProxyReady(boo){
	ajaxProxyReady=boo;
}
function asyncProxy(action,paraArray,callback){
	if(!ajaxProxyReady){
		setTimeout(function(){return asyncProxy(action,paraArray,callback);},500);
		return false;
	}
	var myFr=$('jProxy').contentWindow || document.frames['jProxy'];
	myFr.postData(action,paraArray,callback);
	return false;
}
var asyncAction=asyncCommand("POST");
var asyncPut=asyncCommand("PUT");
var asyncDelete=asyncCommand("DELETE");

var asyncView=function(action, params, whenDone) {
	remoteAction("GET", action, params, whenDone, 
		function(req, theURL, theData) {
			if (theData) {
				var sep=( theData.indexOf("?") > 0) ? "&" : "?";
				req.open("GET", theURL + sep + theData, true);
			} else {
				req.open("GET", theURL, true);
			}
			req.send(null);
		})
}

var asyncJson=function(method, action, params, whenDone) {
	remoteAction(method, action, params, whenDone, 
		function(req, theURL, theData) {
			if (theURL.charAt(0) !='/') {
				theURL='/' + theURL;
			}
			theURL='/accept/application/x-json' + theURL;
			req.open(method, theURL, true);
			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			req.setRequestHeader('x-http-command-equiv', method);
			req.send(theData);
		});
}

var _pendingExecute={}
var execute=function(key) {
	if ( ! _pendingExecute[key]) {
		_pendingExecute[key]=true;
		asyncJson("GET", key, {}, function(results) {
			delete _pendingExecute[key];
			if (results) {
				results=evalJSON(results);
			}
			if (results.length) {
				for (var i=0; i < results.length; i++) {
					var row=results[i];
					if (row.url && (row.url !=key) ) {
						updateModel(row.url, row);
					}
				}
			}
			updateModel(key, results);
		});
	} 
}

var updateModel=function(key, results) {
	AllModelData[key]=results;
	var key_observers=AllObservers[key];
	for (var i=0;i<key_observers.length;i++) {
		var observer=key_observers[i];
		if (observer) {
			observer(results);
		}
	}
};


var deleteURL=function(command, observer) {
	asyncJson("DELETE", command, {}, observer);
}

var postURL=function(command, params, observer) {
	asyncJson("POST", command, params, observer);
}
//==prototype
Array.prototype.map=function(f) { 
	var arr=[];
	for(var i=0;i<this.length;i++)arr.push(f(this[i]));
	return arr;
}
ScriptFragment='(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)';
Object.extend = function(destination, source) {
	for(property in source)destination[property] = source[property];
	return destination;
}
Object.extend(String.prototype, {
	stripTags: function(){return this.replace(/<\/?[^>]+>/gi, '');},
	stripScripts: function(){return this.replace(new RegExp(ScriptFragment, 'img'), '');},
	extractScripts: function(){
		var matchAll = new RegExp(ScriptFragment, 'img');
		var matchOne = new RegExp(ScriptFragment, 'im');
		return (this.match(matchAll) || []).map(function(scriptTag) {
		  return (scriptTag.match(matchOne) || ['', ''])[1];
		});},
	evalScripts: function(){return this.extractScripts().map(eval);}
});
//=======end
function reNewSelect(type,value,obj,optionsAdd,defaultValue) {
	if(value=="" || value=="0"){
		obj.innerHTML=optionsAdd;
		return false;
	}
	var frm={
		myactions : "options",
		type : type,
		value : value
	};
	asyncProxy(getPostRoot()+'/comservice',frm,
		function(ans){
			setOptions(obj,ans,optionsAdd);
			if(defaultValue)obj.value=defaultValue;
		});
	return false;
}
function setOptions(obj,options,optionsAdd){
	var body=document.getElementsByTagName('body')[0];
	var vO=createElement(body,"div");
	vO.innerHTML="<select>"+optionsAdd+options+"</div>";
	var tS=vO.getElementsByTagName("select")[0];
	obj.innerHTML="";
	var op;
	for(var i=0;i<tS.options.length;i++){
		op=tS.options[i];
		obj.options[obj.options.length]=new Option(op.text,op.value);
	}
	body.removeChild(vO);
}
function getPostRoot(){
	if(postRoot)return postRoot;
	return "";
}