var $D = YAHOO.util.Dom;
var $ = $D.get;
var $$ = function(d,s){return (d || $D).getElementsByTagName(s ||'*')};
var $E = YAHOO.util.Event;
var $C = YAHOO.util.Connect;
var $AE = function(){$E.on.apply($E,arguments)};
var $AC = function(){$D.addClass.apply($D,arguments)}
var $RC = function(){$D.removeClass.apply($D,arguments)}

/* 字符串对象的trim函数，除去首尾空格 */
String.prototype.trim = function() {
	var l = /^(\s*)/g;
	var r = /(\s*)$/g;
	var str = this;
	str = str.replace(l,'');
	str = str.replace(r,'');
	return str;
};

/*返回实际使用的字节数,一个汉字两个字节*/
String.prototype.lengthB = function() {
	var lenB, len;
	len = this.length;
	lenB=0;
	for(var i =0; i<len; i++ )
	{
		if( this.substr(i, 1)> "~" )
			lenB += 2;
		else 
			lenB += 1;
	}
	return lenB;	
};

/**
 * 根据Html元素的ID前缀来查找元素 
 */
YAHOO.util.Dom.getElementsByPrefix = function(val, tag, root) {
	var method = function(el) {
		var re = new RegExp('(?:^)' + val + '(?:.*)');
		if ( el.getAttribute("id") && re.test(el.getAttribute("id")) ) {
			return true;
		}
		return false;
	};
	return this.getElementsBy(method, tag, root);
};

function drawTable(tableObj, headRowSpan, dataList, drawRowFun, eventFunsObj){
	var length = tableObj.rows.length;
	for(var i = headRowSpan; i < length; i++) {
		tableObj.deleteRow(headRowSpan);
	}
	for(var i = 0; i < dataList.length; i++) {
		var dataItem = dataList[i];
		drawRowFun(dataItem, headRowSpan + i);
	}
	if(eventFunsObj){
		attachListEvent(tableObj, headRowSpan, eventFunsObj);
	}
};

/**
 * 表格事件响应
 */
function attachListEvent(tableObj, headRowSpan, eventFunsObj){
	var that = this;
	var list = $$(tableObj,"TR");
	var eventFunsObj = eventFunsObj || {};
	if(headRowSpan > 0) {//去掉标题行
		var results = [];
	    for (var i = headRowSpan; i < list.length; i++)
			results.push(list[i]);
		list = results;
	}
	
	//列表元素class初始化
	$D.batch(list, function(el){ $AC(el, "table_item"); });
	var mousemove = function() {
		(this.className=="table_item_click")?
			"":this.className = "table_item_over";
	};
	var mouseout = function() {
		(this.className=="table_item_click")?
			"":this.className = "table_item";
	}
	var click = function() {
		YAHOO.util.Dom.batch(list, function(el){
				el.className = "table_item";
			});
		this.className = "table_item_click";

		if(eventFunsObj.click) {
			eventFunsObj.click.call(that, this);
		}
	};
	var dbclick = function() {
		YAHOO.util.Dom.batch(list, function(el){
				//el.className = "";
			});
		this.className = "table_item_click";

		if(eventFunsObj.dbclick) {
			eventFunsObj.dbclick.call(that, this);
		}
	};
	$D.batch(list, function(el){
		$AE(el, "mouseover", mousemove);
		$AE(el, "mouseout", mouseout);
		$AE(el, "click", click);
		$AE(el, "dblclick", dbclick);
	});
};

function openDialog(url, name, option) {
	option = option || {};
	var parameter = 
		  " menubar=0, toolbar=0, directories=0" + 
		  ",scrollbars=" + (option.scrollbar!= undefined  ? option.scrollbar: "0") +
		  ",resizable=" + (option.resizable != undefined ? option.resizable: "0") +
		  ",left=" + (screen.availWidth - option.width)/2 +
		  ",top=" + (screen.availHeight - option.height)/2 +
		  ",width=" + option.width + ",height="+ option.height;
	return window.open(url, name, parameter);
	//示例 openDialog(name, url, {width: w, height:h})
};

function openModalDialog(url, arg, option) {
	if (!arg) { arg = null; }
	if (!option) { option = {}; }
	if(option.width) {
		if (option.width.toString().match(/^[-\+]?\d+(\.\d+)?$/)) {
			option.width = option.width + "px";
		}
	}
	if(option.height) {
		if (option.height.toString().match(/^[-\+]?\d+(\.\d+)?$/)) {
			option.height = option.height + "px";
		}
	}
	var param = "dialogWidth:" + (option.width?option.width:"100px") + 
				";" + "dialogHeight:" + (option.height?option.height:"100px") + 
				";" + "center:" + (option.center != undefined ?option.center:1) + 
				";"	+ "scroll:" + (option.scroll != undefined ?option.scroll:1) + 
				";"	+ "help:" + (option.help != undefined ?option.help:0) + 
				";"	+ "status:" + (option.status != undefined ?option.status:0) + 
				";";				
	return window.showModalDialog(url, arg, param);
}

function checkData() {
	if($("M_Name").value.length == 0){
		alert("请输入账号");
		return false;
	}
	return true;
}

function login() {
	if(!checkData()) return;
	var url = "login.htm?ts="+new Date();
	var ajaxObject = {
		success: function(o) {
			var s = o.responseText.trim();
			var jsonObject = s.parseJSON();
			if (jsonObject && jsonObject[0] === true){
				document.location.reload(); 
			}else{
				alert("输入的用户名或密码不正确");
			}
		},
		failure: function(o) {}
	}
	var callback = {
		success : ajaxObject.success,
		failure : ajaxObject.failure
	}
	$C.setForm("loginForm");
	var ajax = $C.asyncRequest('POST', url, callback);
}

function logout() {
	var url = "logout.htm?ts="+new Date();
	var ajaxObject = {
		success: function(o) {
			var s = o.responseText.trim();
			var jsonObject = s.parseJSON();
			if (jsonObject && jsonObject[0] === true){
				alert("注销成功");
				document.location.reload(); 
			}else{
				alert("注销失败");
			}
		},
		failure: function(o) {}
	}
	var callback = {
		success : ajaxObject.success,
		failure : ajaxObject.failure
	}
	var ajax = $C.asyncRequest('POST', url, callback);
}

$E.on("login", "click", login);
$E.on("logout", "click", logout);
