//**************************************************************** 
// General purpose functions
//
// Company: Linkway Technology Inc.
// Author : Jackie Yang
// Emial  : jackie_yang@email.linkway.com.tw
//
//**************************************************************** 
// Log of changes: 
//   2002/04/30 - First release
//       
//**************************************************************** 

/********************************************************************
* Choose Radio and open URL 
********************************************************************/
// Open URL by form submit
function fmOpen(opUrl, sbmForm) {
	// submit to opUrl
	sbmForm.action = opUrl;
	sbmForm.submit();
}
// Radio choose & open URL
function rdOpen(opUrl, chkForm, obj) {
	// get selected value
	var row = getRadio(chkForm);
	if(row < 0) {
		alert("资料未选取!");
		return;
	}
	// process
	obj.setRow(row);
	// submit to opUrl
	chkForm.action = opUrl;
	chkForm.submit();
}
// Radio choose & delete the row
function rdDelete(opUrl, chkForm, obj) {
	// get selected value
	var row = getRadio(chkForm);
	if(row < 0) {
		alert("资料未选取!");
		return;
	}
	if(!confirm('是否确认刪除此笔资料?')) return;
	// process
	obj.deleteRow(row);
	// submit to opUrl
	chkForm.action = opUrl;
	chkForm.submit();
}
// Radio choose & process the row
function rdProcess(opUrl, chkForm, obj) {
	// get selected value
	var row = getRadio(chkForm);
	if(row < 0) {
		alert("资料未选取!");
		return;
	}
	if(!confirm('是否确认处理此笔资料?')) return;
	// process
	obj.setRow(row);
	// submit to opUrl
	chkForm.action = opUrl;
	chkForm.submit();
}

/********************************************************************
* Choose CheckBox and open URL 
********************************************************************/
// CheckBox choose & open URL
function cbOpen(opUrl, chkForm, obj) {
	// get selected values
	var rowList = getCheckBox(chkForm);
	var rowCount = rowList.length;
	if(rowCount < 1) {
		alert("资料未选取!");
		return;
	}
	// only one check
	if(rowCount > 1) {
		alert("只能选取一笔资料!");
		return false;
	}
	// process
	obj.setRowList(rowList);
	// submit to opUrl
	chkForm.action = opUrl;
	chkForm.submit();
}
// CheckBox choose & delete the rows
function cbDelete(opUrl, chkForm, obj) {
	// get selected values
	var rowList = getCheckBox(chkForm);
	var rowCount = rowList.length;
	if(rowCount < 1) {
		alert("资料未选取!");
		return;
	}
	if(!confirm('是否确认刪除此'+rowCount+'笔资料?')) return;
	// process
	for(var i=0;i < rowCount;i++) {
		obj.deleteRow(rowList[rowCount - 1 - i]);
	}
	// submit to opUrl
	chkForm.action = opUrl;
	chkForm.submit();
}
// CheckBox choose & process the rows
function cbProcess(opUrl, chkForm, obj) {
	// get selected values
	var rowList = getCheckBox(chkForm);
	var rowCount = rowList.length;
	if(rowCount < 1) {
		alert("资料未选取!");
		return;
	}
	if(!confirm('是否确认处理此'+rowCount+'笔资料?')) return;
	// process
	obj.setRowList(rowList);
	// submit to opUrl
	chkForm.action = opUrl;
	chkForm.submit();
}

/********************************************************************
* Open URL 
********************************************************************/
// Open URL in self window
function openS(opUrl) {
	window.self.location=opUrl;
}
// Open URL in parent frame
function openP(opUrl) {
	window.parent.location=opUrl;
}
// Open URL in top frame
function openT(opUrl) {
	window.top.location=opUrl;
}

/********************************************************************
* Selection checking for Raido
********************************************************************/
// Check is Radio checked
function isRadio(chkForm) {
	if(getRadio(chkForm) < 0) {
		// without Radio field or none of Radio checked
		alert("资料未选取!");
		return false;
	}
	return true;
}
// Get the checked Radio index
function getRadio(chkForm) {
	// parse each form's fields
	var row = 0;
	for(var i=0; i < chkForm.length; i++) {
		// check if type is Radio
		if(chkForm.elements[i].type =='radio') {
			// check if checked
			if(chkForm.elements[i].checked) {
				return row;
			}
			row++;
		}
	}
	return -1;
}

/********************************************************************
* Selection checking for CheckBox
********************************************************************/
// Check is CheckBox checked
function isCheckBox(chkForm, onlyOne) {
	var chkList = getCheckBox(chkForm);
	var chkCount = chkList.length;
	if(chkCount < 1) {
		alert("资料未选取!");
		return false;
	}
	// Only one check
	if(onlyOne && chkCount > 1) {
		alert("只能选取一笔资料!");
		return false;
	}
	return true;
}
// Get the checked CheckBox indexs
function getCheckBox(chkForm) {
	var chkList = new Array();
	var chkCount = 0;
	// parse each form's fields
	var row = 0;
	for(var i=0; i < chkForm.length; i++) {
		// check if type is CheckBox
		if(chkForm.elements[i].type =='checkbox') {
			// check if checked
			if(chkForm.elements[i].checked) {
				chkList[chkCount] = row;
				chkCount++;
			}
			row++;
		}
	}
	return chkList;
}

/********************************************************************
* Validation checking
********************************************************************/
// Check is empty or null
function isEmpty(str) {
	if(str == null || str == "") return true;
	return false;
}
// Check is a positive intger
function isPosInteger(val) {
	if(val == null) return false;
	var str = val.toString();
	for(var i=0; i < str.length;i++) {
		var ch = str.charAt(i);
		if(ch <= '0' || ch > '9') return false;
	}
	return true;
}
// Check is an intger
function isInteger(val) {
	if(val == null) return false;
	var str = val.toString();
	for(var i=0; i < str.length;i++) {
		var ch = str.charAt(i);
		if(i == 0 && ch == '-') continue;
		if(ch <= '0' || ch >= '9') return false;
	}
	return true;
}
// Check is a number(include '-', '.')
function isNumber(val) {
	if(val == null) return false;
	var oneDecimal = false;
	var str = val.toString();
	for(var i=0; i < str.length;i++) {
		var ch = str.charAt(i);
		if(i == 0 && ch == '-') continue;
		if(ch == '.' && !oneDecimal) {
			oneDecimal = true;
			continue;
		}
		if(ch <= '0' || ch >= '9') return false;
	}
	return true;
}
// Check is legal date format
function isDate(str) {
	// check format
	var re_date = /^(\d+)\/(\d+)\/(\d+)$/;
	if (!re_date.exec(str)) return false;
	// check month & date
	if (RegExp.$2 > 12 || RegExp.$3 > 31) return false;
	// check legal date
	var tmpDate = new Date(RegExp.$1, RegExp.$2-1, RegExp.$3);
	var tmpYear = tmpDate.getFullYear();
	var tmpMnth = tmpDate.getMonth() + 1;
	var tmpDay  = tmpDate.getDate();
	if(isNaN(tmpYear) || isNaN(tmpMnth) || isNaN(tmpDay)) return false;
	if(tmpYear != RegExp.$1 || tmpMnth != RegExp.$2 || tmpDay != RegExp.$3) return false;
	return true;
}

/********************************************************************
* Input focus utilities
********************************************************************/
// Set focus & select input text
function doSelect(fld) {
	fld.focus();
	fld.select();
}
// View check respond window
function viewCheck() {
   var strFeatures = "center:yes;dialogWidth:682px;dialogHeight:445px;status:off;help:no";
   var w_check = window.showModalDialog("../check/check.jsp", window, strFeatures);
}

function CheckId(str)
{
	var str1 = new Array(10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29,32,30,31,33);
	if(str.length == 10)
	{
		var temp1 =0;
		str = str.toUpperCase(str);
		temp = str.charCodeAt(0)-65;
		if(temp<0 || temp >25) return false;
		temp = str1[temp];
		temp = Math.floor(temp/10) + (temp%10)*9;
		for(var i=1;i<=9;i++) 
			temp1 += (9-i)?str.charAt(i)*(9-i):parseInt(str.charAt(i));
		if((temp+temp1) % 10) return false;
		else return true;
	}
	else return false;
}


// in:20010613 out:2001/06/13
function dateformats(indate){
	if(indate.value.length==8){
		str = indate.value.substring(0,4)+"/"+indate.value.substring(4,6)+"/"+indate.value.substring(6,8);
		indate.value=str;
		return true;
	}else if(indate.value.length==10 && indate.value.substring(4,5)=="/" && indate.value.substring(7,8)=="/"){
		return true;
	}else{
		return false;
	}
}

// in:start:2001/06/13 stop :2001/06/13 check stop > start->return true
function betweenDate(start,stop){
	if(start.value.length!=10)
		return false;
	if(stop.value.length!=10)
		return false;
	startdate = start.value.substring(0,4)+start.value.substring(5,7)+start.value.substring(8,10);
	stopdate = stop.value.substring(0,4)+stop.value.substring(5,7)+stop.value.substring(8,10);
	if(stopdate >= startdate)
		return true;
	else
		return false;
}

function email_check(email)
{
	reg=/^[_.0-9a-zA-Z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$/;
	if(Trim(email).match(reg)) return true;
	else return false;
}

/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}
