var System = {};
var ua = navigator.userAgent.toLowerCase();
var s;
(s = ua.match(/msie ([\d.]+)/)) ? System.ie = s[1] :
(s = ua.match(/firefox\/([\d.]+)/)) ? System.firefox = s[1] :
(s = ua.match(/chrome\/([\d.]+)/)) ? System.chrome = s[1] :
(s = ua.match(/opera.([\d.]+)/)) ? System.opera = s[1] :
(s = ua.match(/version\/([\d.]+).*safari/)) ? System.safari = s[1] : 0;

String.prototype.trim = function(){
var reExtraSpace = /^\s*(.*?)\s+$/;
return   this.replace(/(^\s*)|(\s*$)/g,"");
}

var ajax=null;
function $(id){
return document.getElementById(id);
}

function $N(name){
return document.getElementsByName(name);
}

var ajaxCompleted=false;
function AjaxRequest(url,data,s,err){
ajaxCompleted=false;
ShowMessage("");
url+="&isajax=1&rn="+new Date();
if(ajax==null){
if(window.XMLHttpRequest){    
ajax = new XMLHttpRequest();
}
else if(window.ActiveXObject){
try{
ajax = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
alert("Ajax创建失败!");
}
}
}
}
ajax.open("POST",url,true);
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.onreadystatechange=function(){
if(ajax.readyState==3){
ajaxCompleted=true;
}
if(ajax.readyState==4){
if(System.firefox){
ajaxCompleted=navigator.onLine;
}
else if(!System.ie){
ajaxCompleted=true;
}
if(ajaxCompleted){
if(ajax.responseText=="0"){
Login("当前处于非登录状态,请先登录");
EndTrading();
return;
}
if(s){
EndTrading();
s(ajax.responseText);
}
}
else{
ShowMessageDialog("提示","当前浏览器处于脱机状态,请设为正常状态");
EndTrading();
}
}
}

try{
Trading();
ajax.send(data);
}
catch(e){
}
}

function Go(url){window.location=url+"&rn="+new Date();}

function Action(name){document.client.action="?_action="+name+"&rn="+new Date();document.client.submit();}
var EasyMask={
    init:function(){
        var k = function(i, v) { return $(i) ? $(i) : v ? document.createElement("div") : document.createElement("iframe"); };
        var a = arguments.length ? arguments[0] : new Object;
        var h = window.innerHeight || self.innerHeight || (document.documentElement && document.documentElement.clientHeight && document.documentElement.offsetHeight) || document.body.clientHeight;
        var w = window.innerWidth || self.innerWidth || (document.documentElement && document.documentElement.clientWidth && document.documentElement.offsetWidth) || document.body.clientWidth;
        var t = document.documentElement.scrollTop;
        var l = document.documentElement.scrollLeft;
        var o = a.opacity ? a.opacity : 7;
        var m = a.molor ? a.molor : '#FFFFFF';
        var e = a.width ? a.width < 100 ? 100 : a.width : 100;
        var f = a.height ? a.height < 50 ? 50 : a.height : 50;
        var b = a.bgcolor ? a.bgcolor : "#7D0000";
	    var pa= a.padding ? a.padding : "6";
        var mo = a.mode ? a.mode : 1;
		var sign = a.sign ? a.sign : "";		
		var apppendindex= a.sign ? 10 : 0;	
        var c = k(sign+"resourceMasks", true);
        var z = k(sign+"resourceContents", true);
        var g = k(sign+"resourceShadows", true);
        var top = (h - f) / 2 + t;
        var left = (w - e) / 2;
        c.id = sign+"resourceMasks";
		if(mo==1){
        c.onclick=EasyMask.close;
		}
		c.tabIndex=-1;	
        c.onkeydown=EasyMask.enter;
        c.oncontextmenu = function(){ return false;};
        c.style.cssText = "display:none;position:absolute;left:0;top:" + t + "px;width:" + w + "px;height:" + h + "px;background:" + m + ";z-index:"+(999998+apppendindex)+";filter:alpha(opacity=" + o + "0);opacity:0." + o + ";";
        z.id = sign+"resourceContents";
		z.tabIndex=-1;	
        z.onkeydown=EasyMask.enter;
        z.style.cssText = 'display:none;outline:none;background:#fff;border:1px solid ' + b + ';z-index:'+(999999+apppendindex)+';position:absolute;width:' + e + 'px;height:' + f + 'px;top:' + top + 'px;left:' + left + 'px;';
        var i = '<div style="height:25px;background:' + b + ';color:#fff;font:bold 12px/25px Arial;text-indent:6px;overflow:hidden;padding-right:8px;">' + (a.title ? a.title : "") + '<span style="position:absolute;right:0;top:0px;cursor:pointer;padding:0 6px 0 3px;" onmouseover="this.style.color=\'red\';" onmouseout="this.style.color=\'#fff\';" title="关闭" onclick="EasyMask.close()">X</span></div>';
        i += '<div class="dialogcontent" style="height:'+(f-25-pa*2)+'px;overflow:auto;padding:'+pa+'px;">' + (a.detail ? a.detail : "") + '</div>';
        z.innerHTML = i;
        g.id = sign+"resourceShadows";
        g.style.cssText = 'display:none;background:#000;z-index:'+(999996+apppendindex)+';position:absolute;width:' + e + 'px;height:' + f + 'px;top:' + (top + 6) + 'px;left:' + (left + 6) + 'px;filter:alpha(opacity=30);opacity:0.3;';
		EasyMask.app(c)(z)(g);
        $(sign+"resourceMasks").style.display="";
        $(sign+"resourceContents").style.display="";
        $(sign+"resourceShadows").style.display="";
		$(sign+"resourceContents").focus(); 
    },
    close:function(){
		if($("messageresourceMasks")){
			EasyMask.del('messageresourceMasks', 'messageresourceContents', 'messageresourceHideSelect', 'messageresourceShadows');
		}
		else{
			EasyMask.del('resourceMasks', 'resourceContents', 'resourceHideSelect', 'resourceShadows');
		}
    },
    enter:function(e){
	var key = (document.all) ? window.event.keyCode:e.which;
		if(key==13){
			var targ
			if (!e) var e = window.event
			if (e.target) targ = e.target
			else if (e.srcElement) targ = e.srcElement
			if (targ.nodeType == 3) // defeat Safari bug
			   targ = targ.parentNode
			var tname=targ.tagName.toLowerCase();
			if(tname!="button"){
				CancelEvent(e);
				if($("yes")){
					$("yes").click();
				}
				else{
					EasyMask.close();
				}
			}
		}
    },
    app:function(v){
        document.body.appendChild(v);
        return EasyMask.app;
    },
    del:function(){
        for (var i = 0; i < arguments.length; i++) {
            var id = $(arguments[i]);
            id && id.parentNode.removeChild(id);
        }
    },
    hidden:function(){
    $("resourceMasks").style.display="none";
    $("resourceContents").style.display="none";
    $("resourceShadows").style.display="none";
    }
}

function Trading() {
if($("Masks")){
$("Masks").style.display="";
}
else{
var k = function(i, v) { return $(i) ? $(i) : v ? document.createElement("div") : document.createElement("iframe"); };
var a = arguments.length ? arguments[0] : new Object;
var h = window.innerHeight || self.innerHeight || (document.documentElement && document.documentElement.clientHeight && document.documentElement.offsetHeight) || document.body.clientHeight;
var w = window.innerWidth || self.innerWidth || (document.documentElement && document.documentElement.clientWidth && document.documentElement.offsetWidth) || document.body.clientWidth;
var t = document.documentElement.scrollTop;
var l = document.documentElement.scrollLeft;
var c = document.createElement("div");
c.id = "Masks";
c.oncontextmenu = function(){ return false;};
c.style.cssText = "position:absolute;left:0;top:" + t + "px;width:" + w + "px;height:" + h + "px;background:#ffffff;z-index:999990;filter:alpha(opacity=70);opacity:0.7;";
c.innerHTML="<img src=\"loading.gif\" style=\"position:absolute;top:"+(t+h/2-9)+"px;left:"+(l+w/2-110)+"px;\"/>";
document.body.appendChild(c);	
}
}

function EndTrading(){
if($('Masks')){
$('Masks').style.display='none';
}
}

function FileNameKeyDown(e){
var isShiftKey=false;
var key=0;
if(e){
isShiftKey=e.shiftKey;
key=e.which;
}
else{
isShiftKey=event.shiftKey;
key=window.event.keyCode
}
if(key==59||key==106||key==107||key==111||key==187||key==188||key==191||key==219||key==220||key==221||key==222||key==226||(isShiftKey&&(key==48||key==49||key==50||key==51||key==52||key==53||key==54||key==55||key==56||key==57||key==186||key==188||key==190||key==191))){
CancelEvent(e);
}
}

function CancelEvent(e){
if(e&&e.preventDefault){
e.preventDefault();
return false;
}
else{
window.event.returnValue = false;
return false;
}
}

function Publish(){
var content="<table style=\"width:379px;\"><tr><td colspan=\"2\"><br>&nbsp;&nbsp;&nbsp;可运行部分将被压缩到 /setup.zip<br>&nbsp;&nbsp;&nbsp;源代码部分将被压缩到 /code.zip<br>&nbsp;&nbsp;&nbsp;确实要发布程序吗?<br><br></td></tr><tr><td></td><td align=\"right\"><button type=\"button\" id=\"yes\" onclick=\"EasyMask.close();PublishOK();\">&nbsp;是&nbsp;</button>&nbsp;<button type=\"button\" onclick=\"EasyMask.close();\">&nbsp;否&nbsp;</button></td></tr></table>";
EasyMask.init({width:395,height:147,title:"发布",detail:content});
$("yes").focus();
}

function PublishReturn(r){
if(r.substring(0,1)==' '){
if($("paste")){
var temp=r.split("|-|");　
ui=eval(temp[0]);
$("main").innerHTML=CreateListUI();
ShowMessage(temp[1]);
}
else{
ShowMessage(r);
}
}
else{
ShowMessageDialog("提示",r);
}
}

function PublishOK(r){
EasyMask.close();
AjaxRequest("?_action=publish","",PublishReturn,null);
}

function Compile(){
if($("paste")){
if(compileMode==1){
Compile1();
}
else if(compileMode==2){
Compile2();
}
else{
Compile2();
}
}
else{
if(compileMode==1){
Compile1();
}
else if(compileMode==2){
Compile2();
}
else if(compileMode==3){
if(document.client.BSDevelopExtension.value==".esp"){
Compile3();
}
else{
Compile2();
}
}
}
}

function Compile1(){
var content="<button id=\"compileall\" type=\"button\" onclick=\"CompileAll();\">编译所有</button><button type=\"button\" onclick=\"EasyMask.close();\">取消</button>";
EasyMask.init({width:145,height:70,title:"编译",detail:content});
$("compileall").focus(); 
}

function Compile2(){
var content="<button id=\"compileall\" type=\"button\" onclick=\"CompileAll();\">编译所有</button><button id=\"compiledirectory\" type=\"button\" onclick=\"CompileDirectory();\">编译当前所在的目录</button><button type=\"button\" onclick=\"EasyMask.close();\">取消</button>";
EasyMask.init({width:285,height:70,title:"编译",detail:content});
$("compiledirectory").focus(); 
}

function Compile3(){
var content="<button id=\"compileall\" type=\"button\" onclick=\"CompileAll();\">编译所有</button><button id=\"compiledirectory\" type=\"button\" onclick=\"CompileDirectory();\">编译当前所在的目录</button><button id=\"compilefile\" type=\"button\" onclick=\"CompileFile();\">编译当前页面</button><button type=\"button\" onclick=\"EasyMask.close();\">取消</button>";
EasyMask.init({width:388,height:70,title:"编译",detail:content});
$("compilefile").focus(); 
}

function CompileAll(){
EasyMask.close();
AjaxRequest("?_action=compileall","",CompileReturn,null);
}

function CompileSite(){
EasyMask.close();
AjaxRequest("?_action=compilesite","",CompileReturn,null);
}

function CompileDirectory(){
EasyMask.close();
var content="<table style=\"width:215px;\"><tr><td colspan=\"2\"><br>&nbsp;&nbsp;&nbsp;要同时编译子目录吗?<br><br><br></td></tr><tr><td></td><td align=\"right\"><button id=\"yes\" type=\"button\" onclick=\"EasyMask.close();CompileDirectory1();\">&nbsp;是&nbsp;</button>&nbsp;<button type=\"button\" onclick=\"EasyMask.close();CompileDirectory2();\">&nbsp;否&nbsp;</button></td></tr></table>";
EasyMask.init({width:230,height:130,title:"确认编译子目录",detail:content});
$("yes").focus();
}

function CompileDirectory1(){
AjaxRequest("?_action=compiledirectory&isinc=1","",CompileReturn,null);
}

function CompileDirectory2(){
AjaxRequest("?_action=compiledirectory","",CompileReturn,null);
}

function CompileFile(){
EasyMask.close();
AjaxRequest("?_action=compilefile&BSDevelopName="+escape(document.client.BSDevelopName.value)+"&BSDevelopExtension="+escape(document.client.BSDevelopExtension.value),"",CompileReturn,null);
}

var CompileResult;
function CompileReturn(r){
CompileResult=r;
AjaxRequest("edit.ashx?_action=generatetemplatemapdll","",GenerateTemplateMapDllReturn,null);
}

function GenerateTemplateMapDllReturn(r){
AjaxRequest("edit.ashx?_action=generatetemplatemapdll","",CompileEnd,null);
}

function CompileEnd(r){
if(CompileResult.substring(0,1)==' '){
if($("paste")){
var temp=CompileResult.split("|-|");　
ui=eval(temp[0]);
$("main").innerHTML=CreateListUI();
EasyMask.init({mode:2,width:700,height:500,title:"编译结果",detail:"<a href=\"compileresult/"+temp[1]+"\" target=\"_blank\">在新窗口中打开</a><br/><iframe src=\"compileresult/"+temp[1]+"\" frameborder=\"0\" scrolling=\"auto\" width=\"100%\" height=\"445\"></iframe>"});
}
else{
EasyMask.init({mode:2,width:700,height:500,title:"编译结果",detail:"<a href=\"compileresult/"+CompileResult.trim()+"\" target=\"_blank\">在新窗口中打开</a><br/><iframe src=\"compileresult/"+CompileResult.trim()+"\" frameborder=\"0\" scrolling=\"auto\" width=\"100%\" height=\"445\"></iframe>" });
}
}
else{
ShowMessageDialog("提示",CompileResult);
}
}

function About(){
var content="<table style=\"width:234px;\"><tr><td colspan=\"2\"><img src=\"logo.gif\"/><br></td></tr><tr><td colspan=\"2\">版本:2.3<br></td></tr><tr><td colspan=\"2\"><label>官方网站:</label><a href=\"http://www.hailisoft.com\" target=\"_blank\">http://www.hailisoft.com</a><br><br></td></tr><tr><td><label>&copy;2010-2012 </label><a href=\"http://www.hailisoft.com\" target=\"_blank\">海狸软件</a></td><td align=\"right\"><button id=\"yes\" type=\"button\" onclick=\"EasyMask.close();\">确定</button></td></tr></table>";
EasyMask.init({width:250,height:170,title:"关于",detail:content});
$("yes").focus();
}

function Exit(){
var content="<table style=\"width:214px;\"><tr><td colspan=\"2\"><br>&nbsp;&nbsp;&nbsp;确实要退出系统吗?<br><br><br></td></tr><tr><td></td><td align=\"right\"><button id=\"yes\" type=\"button\" onclick=\"EasyMask.close();window.location='login.ashx?_action=exit';\">&nbsp;是&nbsp;</button>&nbsp;<button type=\"button\" onclick=\"EasyMask.close();\">&nbsp;否&nbsp;</button></td></tr></table>";
EasyMask.init({width:230,height:131,title:"确认退出系统",detail:content});
$("yes").focus();
}

function Login(t){
var content="密码:<input id='password' onKeyDown=\"if(event.keyCode==13)LoginOK();\" type=\"password\"/><button type=\"button\" onclick=\"LoginOK();\">登录</button>";
EasyMask.init({width:250,height:70,title:t,detail:content});
$("password").focus(); 
}

function LoginOK(){
if($("password").value!=""){
AjaxRequest("login.ashx?_action=ajaxlogin&password="+$("password").value,"",LoginReturn,null);
EasyMask.close();
}
}

function LoginReturn(r){
if(r=="1"){
ShowMessage("登录成功");
}
else{
Login("密码错误");
}
}

function Cancel(){
window.location="index.ashx";
}

function ShowError(no){
id="error"+no;
if($(id).style.display=="block"){
$(id).style.display="none";
}
else{
$(id).style.display="block";
}
}

function ShowMessageDialog(title,text){
var content="<table style=\"width:384px;height:105px;\"><tr><td colspan=\"2\" vlign=\"top\" style=\"padding-left:10px;\">"+text.trim()+"</td></tr><tr><td></td><td align=\"right\" style=\"vertical-align:bottom;\"><button type=\"button\" id=\"yes\" onclick=\"EasyMask.close();\">确定</button></td></tr></table>";
EasyMask.init({ width: 400, height: 150, title: title, detail: content, sign:"message"});
$("yes").focus();
}

function ShowMessage(text){
if($("message")){	
if(text==""){
$("message").innerHTML="";
}
else{
$("message").innerHTML="提示:"+text.trim();
}
}
}