/**************************************************************************************

								 多条记录翻页
  
**************************************************************************************/
var changedFlag = false;

function toFirst() {
	document.all("pageAttribute.pageNo").value = 1;
	submitForm();
}

function toPrior() {
	document.all("pageAttribute.pageNo").value = document.all("pageAttribute.pageNo").value * 1 - 1;
	submitForm();
}

function toNext() {
	document.all("pageAttribute.pageNo").value = document.all("pageAttribute.pageNo").value * 1 + 1;
	submitForm();
}

function toLast() {
	document.all("pageAttribute.pageNo").value = document.all("pageAttribute.pageCount").value;
	submitForm();
}

function changeQuery() {
	changedFlag = true;
}

function submitForm() {
	var theForm = document.forms[0];
	if(changedFlag) theForm.all("pageAttribute.pageNo").value = 1;
	var pageNo = theForm.all("pageAttribute.pageNo").value.trim();
	var pageCount = theForm.all("pageAttribute.pageCount").value.trim();	
	if(isNaN(pageNo) || pageNo <= 0 || pageNo.indexOf(".") >=0 || pageNo > pageCount) {
		alert("请输入正确的页数！");
		theForm.all("pageAttribute.pageNo").focus();
	} else {
		// 检查表单的接口
		// 如果存在表但接口validate(theForm)，则调用该接口
		// 该接口返回布尔值，true通过，false未通过
		try {
			var blResult = validate(theForm);
			if(!blResult) return;
		} catch (e) {}
		theForm.submit();
	}
}
/**************************************************************************************

								 RECORD DATA OPERATION
  
**************************************************************************************/
function doAdd(unitCh, unitEn, nWidth, nHeight) {
	var rtValue = showDialog(unitEn + 'Add.whale', 'PopWin', nWidth, nHeight);
	if(rtValue != null && rtValue.trim() != "") {
		// alert(rtValue);
		submitForm();
	}
	
	return (rtValue != null)?rtValue.trim():null;
}

function doDetail(unitCh, unitEn, nWidth, nHeight) {
	if(getSelectedItem() == "") {
		alert("请选取一条" + unitCh + "记录然后执行本操作！");
		return null;
	}
	
	var rtValue = showDialog(unitEn + 'Detail.whale?' + unitEn + '.id=' + getSelectedItem().id.substr(4), 'PopWin', nWidth, nHeight);
	return (rtValue != null)?rtValue.trim():null;
}

function doView(unitCh, unitEn, nWidth, nHeight) {
	if(getSelectedItem() == "") {
		alert("请选取一条" + unitCh + "记录然后执行本操作！");
		return null;
	}
	
	var rtValue = showDialog(unitEn + 'View.whale?' + unitEn + '.id=' + getSelectedItem().id.substr(4), 'PopWin', nWidth, nHeight);
	return (rtValue != null)?rtValue.trim():null;
}

function doUpdate(unitCh, unitEn, nWidth, nHeight) {
	if(getSelectedItem() == "") {
		alert("请选取一条" + unitCh + "记录然后执行本操作！");
		return null;
	}

	var rtValue = showDialog(unitEn + 'Update.whale?' + unitEn + '.id=' + getSelectedItem().id.substr(4), 'PopWin', nWidth, nHeight);
	if(rtValue != null && rtValue.trim() != "") {		
		submitForm();
	}
	
	return (rtValue != null)?rtValue.trim():null;
}

function doDelete(unitCh, unitEn, nWidth, nHeight) {
	if(getSelectedItem() == "") {
		alert("请选取一条" + unitCh + "记录然后执行删除！");
		return null;
	}
	
	if(confirm("你确认删除该" + unitCh + "记录？")) {
		var rtValue = showDialog(unitEn + 'Delete.whale?' + unitEn + '.id=' + getSelectedItem().id.substr(4), 'PopWin', nWidth, nHeight);
		if(rtValue != null && rtValue.trim() != "") {		
			submitForm();			
		}
		
		return (rtValue != null)?rtValue.trim():null;
	}
	
	return null;
}

/**************************************************************************************

								   TABLE LINE SELECT
  
**************************************************************************************/
var old_item = "";
var old_color = "";
var old_background = "#DADAFF";

function changeItem() {
	target = event.srcElement.parentElement;
	target2 = event.srcElement.parentElement.parentElement;
	if(target.id == null || target.id.indexOf("line") != 0) {
		if(target2.id == null || target2.id.indexOf("line") != 0) return;
		target = target2;
	}

	setSelectedItem(target);
}

function setSelectedItem(ele) {
	// recover the style of old item
	if( old_item != "") {
		old_item.style.background = old_background;
		old_item.style.color = old_color;
	}

	// deal with the new item
	if(ele != null && ele != "") {
		old_color = ele.style.color;
		if( old_color == "" ) old_color = "#000000";
		ele.style.color = "#FFFFFF";
		old_background = ele.style.background;
		if( old_background == "" ) old_background = "#DADAFF";
		ele.style.background = "#00008B";
	}

	old_item = ele;
}

function getSelectedItem() {
	return old_item;
}

/**************************************************************************************

								   ELEMENT OPERATION
  
**************************************************************************************/
function showDialog(strUrl, argsVariable, nWidth, nHeight){
	var nDefaultWidth = 600;
	var nDefaultHeight = 450;
	if(nWidth == null || isNaN(nWidth)) nWidth = nDefaultWidth;
	if(nWidth == null || isNaN(nWidth)) nHeight = nDefaultHeight;
	var feature = "dialogWidth:" + nWidth + "px;dialogHeight:" + nHeight + "px;center:yes";
	return showModalDialog("dialog.whale?url=" + strUrl, argsVariable, feature);
}

function getRadio(eleName) {
	var arrEle = document.all(eleName);
	if(arrEle == null) { // no item list
		return null;
	}
	
	if(arrEle.length == null || arrEle.length == 1) {
		if(arrEle.checked) {
			return arrEle.value;
		}
	} else {		
		for(var nIndex = 0; nIndex < arrEle.length; nIndex++) {
			if(arrEle[nIndex].checked) {
				return arrEle[nIndex].value;
			}
		}
	}
	
	return null;
}

function addOption(selectEle) {
	var optValue = prompt("请输入新的选项名称：","");
	if(optValue != null) optValue = optValue.trim();
	
	if(optValue == null || optValue == "") {
		alert("不能用空值创建新的选项！");
	} else {
		// check whether the item exist
		for(var i = 0; selectEle.length > 0 && i < selectEle.length; i++) {
			if(selectEle.options[i].value == optValue) {
				alert("输入的选项已经存在！");
				return;
			}
		}
		// create option
		var opt = new Option();
		opt.value = optValue;
		opt.text = optValue;
		
		selectEle.options.add(opt);
		selectEle.options[selectEle.length - 1].selected = true;
	}
}

/**************************************************************************************

								   VARIABLE PROCESS
  
**************************************************************************************/
// 为JScript的String添加trim(), trimLeft(), trimRight()函数
String.prototype.trim = function() {
    // 用正则表达式将前后空格
    // 用空字符串替代。
    return this.trimLeft().trimRight();
}

String.prototype.trimLeft = function() {
	return this.replace(/^\s+/, "");
}

String.prototype.trimRight = function() {
	return this.replace(/\s+$/, "");
}
/**************************************************************************************

								   DHTML效果
  
**************************************************************************************/
// 图片效果strength
var imageStrength = 2;
// 当鼠标移动到图片上的效果，传入图片Object
function imageMouseOn(ele) {
	ele.width = ele.width - imageStrength * 2;
	ele.height = ele.height - imageStrength * 2;
	ele.style.cursor = "hand";
	ele.style.filter = "progid:DXImageTransform.Microsoft.Glow(Color=blue,Strength=" + imageStrength + ")";
}
// 和imageMouseOn方法共用，当鼠标从图片上移开，取消图片效果，恢复原状
// 传入图片Object
function imageMouseOut(ele) {
	ele.style.filter = "";
	ele.width = ele.width + imageStrength * 2;
	ele.height = ele.height + imageStrength * 2;
}