// JavaScript Document

// 解决replace只替换一个的问题
String.prototype.ReplaceAll = function(from, to) {
	var r = new RegExp(from, "gim");
	return this.replace(r, to);
}
String.prototype.Hash = function() {
	s = encodeURI(this);
	return hex_md5(s).toUpperCase();
}

// 是否为IE
function IsIE() {
	return (window.navigator.userAgent.indexOf("MSIE")>=1);
}

// 打开模式窗口
function OpenSelectDialog(url, w, h, name) {
	var width;
	width = w ? w : 450;
	name = name ? name : "对话框";
	var o = $("#SaintSelectDialog");
	if (o.length) {
		$("#SaintSelectDialogIFrame").attr({"src" : url, "height" : "400"}).length;
		o.dialog("open");
		return;
	}
	o = $("<div id=\"SaintSelectDialog\"></div>").appendTo(document.body).attr("title", name).dialog({
		  bgiframe: true,
		  autoOpen: false,
		  width:width,
		  resizable: false,
		  draggable: false,
		  modal: true
		  });
	$("<iframe frameborder=\"0\" scrolling=\"yes\" src=\"about:blank\" id=\"SaintSelectDialogIFrame\" width=\"400\"></iframe>").appendTo(o);
	OpenSelectDialog(url, w, h, name);
}

function CloseSelectDialog() {
	$('#SaintSelectDialog').dialog('close');
}

// 获取存储路径
function SaintGetSaveDirByID(id, tplBaseDir, depth, ext) {
	var id = id.toUpperCase();
	var i = id.length;
	
	var s1 = i>0 ? id.substr(i-1, 1) : 0;
	var s2 = i>1 ? id.substr(i-2, 1) : 0;
	var s3 = i>2 ? id.substr(i-3, 1) : 0;
	
	var sSavePath = SaintTplConfig.SiteBaseURL + tplBaseDir;
	
	if (depth > 0) {
		sSavePath += "/"+s1;
	}
	if (depth > 1) {
		sSavePath += "/"+s2;
	}
	if (depth > 2) {
		sSavePath += "/"+s3;
	}
	return sSavePath + "/" + id + "." + ext;
}

// 取得域名
function SaintGetDomain() {
	var s = "";
	/*
	hash 设置或获取 href 属性中在井号“#”后面的分段。 
	host 设置或获取 location 或 URL 的 hostname 和 port 号码。 
	hostname 设置或获取 location 或 URL 的主机名称部分。 
	href 设置或获取整个 URL 为字符串。 
	pathname 设置或获取对象指定的文件名或路径。 
	port 设置或获取与 URL 关联的端口号码。 
	protocol 设置或获取 URL 的协议部分。 
	search 设置或获取 href 属性中跟在问号后面的部分。 
	*/
	try {
		return window.location.host;
	} catch (ex) {
		alert(ex.message);
	}
	return s;
}

function GetCategoryIDByDomain() {
	var domain = SaintGetDomain();
	$('#saintdebug').html("test:" + $.SaintDomainConfig.Root.Domain);
	for(var i=0; i<$.SaintDomainConfig.Root.Domain.length; i++) {
		var a = $.SaintDomainConfig.Root.Domain[i].DomainName.split('|');
		for(var j=0; j<a.length; j++) {
			if (a[j].toUpperCase() == domain.toUpperCase()) {
				return $.SaintDomainConfig.Root.Domain[i].CategoryID;
			}
		}
	}
	return "8B60D2DD6EB07375973DBB2BAE30BA3D";
}

// 取得浏览器参数
function SaintGetQuery(key) {
	var id = "";
	try {
		if (!window.location.search) {
			return "";
		}
		var queryString = window.location.search.split('?')[1];
		var aRequest = queryString.split('&');
		for (var i=0; i<aRequest.length; i++) {
			var aParam = aRequest[i].split('=');
			if (aParam[0].toLowerCase() == key.toLowerCase()) {
				id = aParam[1];
			}
		}
	} catch (ex) {
		alert("取得浏览器参数时出错：" + ex.message);
	}
	return id;
};

// 显示Loading框，自动生成一个浮动窗口
//eg: pos={x:200,y:100,type:center/left/right}
// status = loading/failed/success
function SaintShowLoading(id, status, text, css) {
	var jDiv = $("#" + id);
	if (!jDiv.length) {
		jDiv = $("<div></div>").appendTo(document.body); 
		jDiv.attr({ "id" : id }); 
		jDiv.css({ "background":"#CCC", "padding":"5px", "position":"absolute" });
		
		if (css) { jDiv.css(css); }
	}
	jDiv.html(text);
	jDiv.show();
}

// 格式化日期
function SaintFormatDate(f, date) {
	// Y M d H I S
	if (!date) {
		var date = new Date();
	}
	
	var y = date.getFullYear();
	var m = date.getMonth()+1;
	var d = date.getDate();
	var h = date.getHours();
	var i = date.getMinutes();
	var s = date.getSeconds();
	
	// f = f.replace(new RegExp("Y", "gim"), y);
	// f = f.replace(new RegExp("y", "gim"), y.substring(2, 2));
	f = f.ReplaceAll("Y", y.toString().substr(2,2));
	f = f.ReplaceAll("m", m);
	f = f.ReplaceAll("d", d);
	f = f.ReplaceAll("h", h);
	f = f.ReplaceAll("i", i);
	f = f.ReplaceAll("s", s);
	return f;
};

// AJAX请求后对Request进行分析得到字符串
function SaintAjaxError(req) {
	switch (req.status) {
		case 200: return "OK"; 
		case 202: return "Accepted"; 
		case 400: return "Bad Request";
		case 403: return "Forbidden";
		case 404: return "文件不存在";
		case 405: return "Method Not Allowed";
		case 408: return "Request Timeout";
		case 500: return "Internal Server Error";
		case 501: return "Not Implemented";
		case 502: return "Bad Gateway";
		case 503: return "Service Unavailable";
	}
	return req.status;
}

// XML节点到JSON对象的转换，目前只支持单层次节点
function XmlNodeToJSON(node) {
	var json = "";
	var children = $(node).children();
	if (children.length > 0) {
		var a = new Object();
		children.each(function (i) { 
			var id = this.tagName;
			var s = $(this).text();
			eval("a." + id + " = s;");
		});
		return a;
	}
	
	return null;
}

function SaintGetFirstTagName(content) {
	var tagReg = new RegExp("<([^\\s>]+)","gim");
	if (matches = tagReg.exec(content)) {
		return matches[1].toUpperCase();
	}
	return "";
}

function SaintDropdownJump(o) {
	if ( parseInt(o.value) == -1 ) {
		return;
	}
	var f = document.getElementById("DropdownTempForm");
	if (!f) {
		f = document.createElement("form");
		f.method = "get";
		f.target = "_blank";
		f.id = "DropdownTempForm";
		var doc = document.documentElement;
		doc.appendChild(f);
		input = document.createElement("input");
		f.appendChild(input);
	}
	//var input = f.childNodes[0]; 
	//input.name = "id";
	//input.value = o.value;
	f.action = o.value;
	if (o.value && o.value.length) {
		f.submit();
	}
	o.selectedIndex = 0;
}

// 禁用浏览器默认行为
function PreventDefault(e) {
	if (window.event) {
		window.event.returnValue = false;
	} else {
		e.preventDefault();
	}
}

// 禁止事件向上递归触发
function StopPropagation(e) {
	if (window.event) {
		window.event.cancelBubble = true;
	} else {
		e.stopPropagation();
	}
}

function SaintNumUpDown(textBox, op) {
	op = $.extend({
		maxValue:999,
		minValue:0,
		value:1
	}, op);
	if (!textBox) {
		alert("no textBox");
		return;
	}
	var v = parseInt(textBox.val());
	if ('' + v == 'NaN') {
		v = 0;
	}
	textBox.val(op.value + v * 1);
	if (textBox.val() > op.maxValue) {
		textBox.val(op.maxValue);
	}
	if (textBox.val() < op.minValue) {
		textBox.val(op.minValue);
	}
}