/*
	用于ajax和后台交互用
*/
var ajaxControl = {
	
	sendAjaxCommond:function(_requestURL, _function) {
		//alert(_function);
	    new Ajax.Request(_requestURL, {
	        method:"get",
	        onSuccess: function(transport) {
	            var response = transport.responseText || "";
	            if (response != "") {
	            	jupaointerface.response = response;
	            	//执行函数
	            	if (_function) { 
	            		_function.call(this, response); 
	            	}
	            }else{
	            	alert("返回数据为空！");
	            }
	            delete response;
	        },
	        onFailure: function() {
	            alert('Something went wrong..., when you send Ajax commond!');
	        },
	        onException:function(rquest, exception) {
	            alert('Something went Error:' + exception.message + ', when you send Ajax commond：\n' + _requestURL);
	        }
	    });
	},
	
	postAjax: function(_requestURL, paramaters, _function) { 
	    new Ajax.Request(_requestURL, {
	        method:"post",
	        parameters :paramaters,
	        onSuccess: function(transport) {
	            var response = transport.responseText || "";
	            if (response != "") {
	            	if (_function) { 
	            		_function.call(this, response); 
	            	}
	            }
	        },
	        onFailure: function() {
	            alert('Something went wrong..., when you post Ajax commond!');
	        },
	        onException:function(rquest, exception) {
	            alert('Something went Error:' + exception + ', when you post Ajax commond：\n' + _requestURL);
	        }
	    });
	},
	
	updateHTML: function(_element, _URL, _function) {
	    var _url = _URL + "&token=" + (+new Date);
	    new Ajax.Request(_url, {
	        method:"get",
	        onSuccess: function(transport) {
	            var result = transport.responseText || "error";
	            if (result !== "error" ) {
	                setInnerHTML($(_element), result + "");
	                $(_element).update(result);
	                jupaoInterface._templates[_URL] = result + "";
	                if (_function) { _function.call(); }
	            }
	            delete result;
	        },
	        onFailure: function() {
	            alert('Something went Failure..., when you get HTML Template !\n' + _URL);
	        },
	        onException:function(rq, exeption) {
	            alert('Something Exception：' + exeption + ' , when you get HTML Template !\n' + _URL);
	        }
	    });
	},
	
	//转换编码，使<script></script>标签能够执行
	setInnerHTML: function (el, htmlCode) {
	    var ua = navigator.userAgent.toLowerCase();
	    if (ua.indexOf('msie') >= 0 && ua.indexOf('opera') < 0) {
	        htmlCode = '<div style="display:none">for IE</div>' + htmlCode;
	        htmlCode = htmlCode.replace(/<script([^>]*)>/gi, '<script$1 defer>');
	        el.innerHTML = htmlCode;
	        el.removeChild(el.firstChild);
	    } else {
	        var el_next = el.nextSibling;
	        var el_parent = el.parentNode;
	        el_parent.removeChild(el);
	        el.innerHTML = htmlCode;
	        if (el_next) {
	            el_parent.insertBefore(el, el_next);
	        } else {
	            el_parent.appendChild(el);
	        }
	    }
	}
}