//****************************************************************
// 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
********************************************************************/
// Do focus
function doFocus(fld) {
	fld.focus();
}
function isChar(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' && ch<'A') || (ch>'Z' && ch<'a') || ch>'z') && (ch!='_')) {
		return false;	
		}
			
	}
	return true;
}
function isSpecial(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=='\'')||(ch=='\"')||(ch=='-') ) {
		return false;	
		}
			
	}
	return true;
}


function toutf8(string)
{



	// public method for url encoding
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";
	
	for (var n = 0; n < string.length; n++) {
		var c = string.charCodeAt(n);
			if (c < 128) {
			    utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
			    utftext += String.fromCharCode((c >> 6) | 192);
			    utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
			    utftext += String.fromCharCode((c >> 12) | 224);
			    utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			    utftext += String.fromCharCode((c & 63) | 128);
			}
		}
	return utftext;

}

// Check is empty or null
function isEmpty(str) {
	if(str == null || str == "") return true;
	return false;
}
// Check variable's lenth
function checkLength(str,minlen,maxlen) {
	len = str.length;
	if( len >= minlen && len <=maxlen) 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;
}

function isAllInteger(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 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;
}
function trimSpace(string)
{
	//先去掉名字前面多餘的空白
	string = string.replace(/^[\s　]+/g, "");
	//去掉名字後面多餘的空白
	string = string.replace(/[\s　]+$/g, "");
	//把中間分隔多的空白去掉
	string = string.replace(/[\s　]+/g, " ");

	return string;
}


/********************************************************************
* 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;
		if(!isAllInteger(str.substr(1,9))) 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;
	}
}

function email_check(email)
{
	reg=/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$/;
	if(email.match(reg)) return true;
	else return false;
}

//檢查手機號碼
function checkMobile(tel)	//手機號碼
{
	reg = /^[0-9]{10}$/;	//手機

	if (!tel.match(reg))	return false;
	if (tel.substring(0,2)=="09")
		return true;
	else
		return false;

}

//檢查台新卡號前六碼
function checkTaishinBin(tsb_bin){
	if(tsb_bin.value.length==6){
		if(tsb_bin.value=="377680" || tsb_bin.value=="377682" || tsb_bin.value=="400355"
		|| tsb_bin.value=="405058" || tsb_bin.value=="405059" || tsb_bin.value=="405650"
		|| tsb_bin.value=="438045" || tsb_bin.value=="457960" || tsb_bin.value=="457961"
		|| tsb_bin.value=="457964" || tsb_bin.value=="457965" || tsb_bin.value=="480447"
		|| tsb_bin.value=="480448" || tsb_bin.value=="514914" || tsb_bin.value=="540641"
		|| tsb_bin.value=="543382" || tsb_bin.value=="543782" || tsb_bin.value=="544228" 
		) {
		return true;
		}
	}else{
		return false;
	}
}


function check_select(select) {
	if ( select.options[0].selected==true )
		return false
	return true;
}
