﻿/**
* system : fsc
* sub-system : common utility
* 
*/

//var root = "/fsc";
var root = "";

/** system date geting function */
function gfn_GetSysDate(){
	var yyyy;
	var mm;
	var dd;
	var tm;
	var dt;

	if ( navigator.userAgent.indexOf("MSIE")>=0 ) {
		yyyy=Date().substring(20,24);
	}
	else if ( navigator.userAgent.indexOf("Mozilla")>=0 ) {
		yyyy=Date().substring(45,50);
	}
	else {
		yyyy="####";
	}
	mm = Date().substring(4,7)
	dd = Date().substring(8,10);
	tm = Date().substring(11,19);
 
	if(mm=="Jan"){mm="01";}
	else if(mm=="Feb"){mm="02";}
	else if(mm=="Mar"){mm="03";}
	else if(mm=="Apr"){mm="04";}
	else if(mm=="May"){mm="05";}
	else if(mm=="Jun"){mm="06";}
	else if(mm=="Jul"){mm="07";}
	else if(mm=="Aug"){mm="08";}
	else if(mm=="Sep"){mm="09";}
	else if(mm=="Oct"){mm="10";}
	else if(mm=="Nov"){mm="11";}
	else if(mm=="Dec"){mm="12";}
	else{mm="##";}

	dt = yyyy + "-" + mm + "-" + dd + " " + tm;
	return dt;
}

/**
 * change to uppercase
 * parameter:
 * obj		: object to call this function
 */
function toUpperCase(obj) {
	if ( obj.value != null && obj.value != "" ) {
		obj.value	= obj.value.toUpperCase();
	}
}

/**
 * change to uppercase
 */
function toUpperCase() {
	var obj	= event.srcElement;
	if ( obj.value != null && obj.value != "" ) {
		obj.value	= obj.value.toUpperCase();
	}
}

/**
 * change to formated number (example: ###,###,###)
 */
function toNumberFormat(data) {
	var i,j	= 0;
	var value		= data + "";
	var retValue	= "";
	
	if ( value == null ) return "";
	for ( i = value.length - 1; i >= 0; i-- ) {
		if ( j == 3 ) {
			retValue	= value.charAt(i) + "," + retValue
			j	= 1;
			continue;
		}
		retValue	= value.charAt(i) + retValue
		j++;
	}
	return retValue;
}

/**
 * trim left spaces
 * parameter:
 * value	: the value to trim left spaces
 */
function ltrim(value) {
	if ( value == null ) return "";
	if ( !value ) return "";
	try {
		while ( value.charCodeAt(0) == 32 ) {
			value = value.substring(1,value.length);
		}
	}
	catch(error) { return ""; }
	return value;
}

/**
 * trim right spaces
 * parameter:
 * value	: the value to trim right spaces
 */
function rtrim(value) {
	if ( value == null ) return "";
	if ( !value ) return "";
	try {
		while ( value.charCodeAt(value.length - 1) == 32 ) {
			value = value.substring(0, value.length - 1);
		}
	}
	catch(error) { return ""; }

	return value;
}

/**
 * trim left and right spaces
 * parameter:
 * value	: the value to trim spaces
 */
function trim(value) {
	if ( value == null ) return "";
	if ( !value ) return "";
	return (ltrim(rtrim(value)));
}

/*
 * int limit when input
 * permissible character: number and hyphen(-),(.)
 */
function intLimit() {
	var code = event.keyCode;
	
  	if ( code < 48 || code > 57 ) {
  		event.keyCode = 0;
  		return;
  	}
}

/*
 * number limit when input
 * permissible character: number and hyphen(-),(.)
 */
function numberLimit() {
	var code = event.keyCode;
	
  	if ( (code < 48 && code != 45 && code != 46) || code > 57 ) {
  		event.keyCode = 0;
  		return;
  	}
}

/*
 * decimal limit when input
 */
function decimalLimit() {
	var code = event.keyCode;
	
  	if ( (code < 48 || code > 57) && code != 46 ) {
  		event.keyCode = 0;
  		return;
  	}
}

/*
 * telephone limit when input
 * permissible character: number and hyphen(-),(#),(*)
 */
function telLimit() {
	var code = event.keyCode;
	
  	if ( (code < 48 && code != 35 && code != 42 && code != 45) || code > 57 ) {
  		event.keyCode = 0;
  		return;
  	}
}

/**
 * legal limit when input of import program
 * impermissible character: \"'~^><&
 * uppercase change
 */
function legalLimit() {
	var code = event.keyCode;
	
	if ( 	code == 92
		||	code == 34
		||	code == 39
		||	code == 126
		||	code == 94
		||	code == 62
		||	code == 60
		||	code == 38 ) {
  		event.keyCode = 0;
  		return;
  	}
  	//if (code >= 97 && code <= 122) {
  	//	event.keyCode = code - 32;
  	//}
}

/**
 * uppercase change when input
 */
function upperCaseChange() {
	var code = event.keyCode;
	
  	if (code >= 97 && code <= 122) {
  		event.keyCode = code - 32;
  	}	
}


/**
 * number check
 * parameter:
 * value	: the value to check
 */
function isNumber(value) {
	var	char;
	var	byteLeng;
	var	byteCnt;
	
	if ( value == null ) return false;
	
	value		= value + "";
	byteLeng	= value.length;
	byteCnt		= byteCount(value);

	if ( byteLeng != byteCnt ) return false;
	
	for ( i = 0; i < byteCnt; i++ ) {
		char = value.charAt(i);
		if ( char < "0" || char > "9" ) {
			return false;
		}
	}
	
	return true;
}

/**
 * decimal check
 * parameter:
 * value	: the value to check
 */
function isDecimal(value) {
	var	char;
	var	byteLeng;
	var	byteCnt;
	var cnt_dot	= 0;
	
	if ( value == null ) return false;
	
	value		= value + "";
	byteLeng	= value.length;
	byteCnt		= byteCount(value);

	if ( byteLeng != byteCnt ) return false;
	
	for ( i = 0; i < byteCnt; i++ ) {
		char = value.charAt(i);
		if ( char == "." ) {
			cnt_dot++;
			if ( cnt_dot > 1 ) return false;
		}
		if ( (char < "0" || char > "9") && char != "." ) {
			return false;
		}
	}
	return true;
}

/**
 * Chinese Format check
 *
 * parameter:
 *
 * value	: chinese string to check
 */
function isChinese(s) {
	var ret = true;
	for(var i=0; i<s.length; i++){
		ret=ret && (s.charCodeAt(i)>=10000);
	}
	return ret;
}

/**
 * ID Card check
 * parameter:
 * value	: the value to check
 */
function isIDCard(value) {
	var	char;
	var	byteLeng;
	var	byteCnt;
	
	if ( value == null ) return false;
	
	value		= value + "";
	byteLeng	= value.length;
	byteCnt		= byteCount(value);

	if ( byteLeng != byteCnt ) return false;
	
	if ( byteLeng != 15 && byteLeng != 18 ) return false;
	
	for ( i = 0; i < byteCnt; i++ ) {
		char = value.charAt(i);
		if ( char < "0" || char > "9" && char != "x" && char != "X") {
			return false;
		}
	}
	
	return true;
}

/**
 * Email Format check
 *
 * parameter:
 *
 * value	: email address to check
 */
function isEmail(value) {
	var	char;
	var	byteLeng;
	var	byteCnt;

	if ( value == null ) return false;
	
	value		= value + "";
	byteLeng	= value.length;
	byteCnt		= byteCount(value);
	
	// double character check
	if ( byteLeng != byteCnt ) return false;
	
	// length check
	if ( value.length < 3 || value.length > 80 ) return false;
	
	// character '@' middle of user ID check
	if ( value.indexOf('@') <= 0 || value.lastIndexOf('@') <= 0 ) return false;
	if ( value.indexOf('@') != value.lastIndexOf('@') ) return false;
	if ( value.lastIndexOf('@')+1 == value.length ) return false;
	
	// character '.' middle of user
	var user = value.substring(0,value.indexOf('@'));
	if ( user.indexOf('.') == 0 || user.lastIndexOf('.') == 0 ) return false;
	if ( user.lastIndexOf('.')+1 == user.length ) return false;
	
	// character '.' middle of domain
	var domain = value.substr(value.indexOf('@') + 1);
	if ( domain.indexOf('.') <= 0 || domain.lastIndexOf('.') <= 0 ) return false;
	if ( domain.lastIndexOf('.')+1 == domain.length ) return false;

	// character legal check
	for ( i = 0; i < byteCnt; i++ ) {
		char = value.charAt(i);
		
		if (!( (char >= "0" && char <= "9")
			|| (char >= "A" && char <= "Z")
			|| (char >= "a" && char <= "z")
			|| (char == "-")
			|| (char == "_")
			|| (char == ".")
			|| (char == "@")
			|| (char == "!")
			|| (char == "#")
			|| (char == "$")
			|| (char == "+")
			|| (char == "=")
			|| (char == "?")
			|| (char == "[")
			|| (char == "]")
			|| (char == "{")
			|| (char == "}")
			|| (char == "|") )) {
			return false;
		}
	}
	
	return true;
}

/**
 * password check
 *
 * parameter:
 *
 * value	: password to check
 */
function isPassword(value) {
	var	char;
	var	byteLeng;
	var	byteCnt;

	if ( value == null ) return false;
	
	value		= value + "";
	byteLeng	= value.length;
	byteCnt		= byteCount(value);
	
	// double character check
	if ( byteLeng != byteCnt ) return false;
	
	// length check
	if ( value.length < 6 || value.length > 31) return false;
	
	// character legal check
	for ( i = 0; i < byteCnt; i++ ) {
		char = value.charAt(i);
		
		if (!( (char >= "0" && char <= "9")
			|| (char >= "A" && char <= "Z")
			|| (char >= "a" && char <= "z")
			|| (char == "-")
			|| (char == "_")
			|| (char == ".")
			|| (char == "@") )) {
			return false;
		}
	}
	
	return true;
}

/**
 * Tel check
 * parameter:
 * value	: the value to check
 */
function isTel(value) {
	var	char;
	var	byteLeng;
	var	byteCnt;
	
	if ( value == null ) return false;
	
	if ( value != "" && value.length < 11 ) return false;
	
	if ( value.indexOf("--") > -1 ) return false;
	
	value		= value + "";
	byteLeng	= value.length;
	byteCnt		= byteCount(value);

	if ( byteLeng != byteCnt ) return false;
	
	for ( i = 0; i < byteCnt; i++ ) {
		char = value.charAt(i);
		if (!( (char >= "0" && char <= "9")
			|| (char == "-")
			|| (char == "*")
			|| (char == "#") )) {
			return false;
		}
	}
	
	return true;
}

/**
 * legal check ( impermissible character: \"'~^><& )
 * parameter:
 * value	: the value to check
 */
function isLegal(value) {
	var	char;
	var	byteLeng;
	var	byteCnt;

	if ( value == null ) return false;
	
	value		= value + "";
	byteLeng	= value.length;
	byteCnt		= byteCount(value);

	//if ( byteLeng != byteCnt ) return false;
	
	for ( i = 0; i < byteCnt; i++ ) {
		char = value.charAt(i);
		//if (   (char > "a" && char < "z")
		if (   (char == "\\")
			|| (char == "\"")
			|| (char == "'")
			|| (char == "~")
			|| (char == "^")
			|| (char == ">")
			|| (char == "<")
			|| (char == "&") ) {
			return false;
		}
	}
	
	return true;
}

/**
 * legal check for all input item
 */
function isLegalAll() {
	var index	= 0;
	var item;
	
	while ( document.all[index] ) {
		item	= document.all[index];
		
		if ( item.value )
		if ( (item.type == "text" || item.type == "textarea") && !isLegal(item.value) ) {
			if ( item.id != null) item_name	= item.id;
			else item_name	= item.name;
			if ( item_name ) {
				setFocus(item_name);
				return false;
			}
		}
		index++;
	}
	return true;
}

function isLegalAllExcept(set_items) {
	var index	= 0;
	var item;
	var itemsName	= new Array();
	var is_set_item = '0';
	
	if ( set_items != null && set_items != "" ) {
		itemsName	= set_items.split("^");
	}
	
	while ( document.all[index] ) {
		item	= document.all[index];
		
		if ( item.value )
		if ( (item.type == "text" || item.type == "textarea") && !isLegal(item.value) ) {
			if ( item.id != null) item_name	= item.id;
			else item_name	= item.name;
			
			is_set_item = '0';
			for ( i = 0; i < itemsName.length; i++ )
			if ( item.id == itemsName[i] || item.name == itemsName[i] ) {
				is_set_item = '1';
				break;
			}
			if (is_set_item == '0')
			if ( item_name ) {
				setFocus(item_name);
				return false;
			}
		}
		index++;
	}
	return true;
}


/**
 * status check for revise
 *
 * parameter:
 * oldStatus		: old status code
 * newStatus		: new status code
 */
function statusCheck(oldStatus, newStatus) {
	// legal status
	if (!(oldStatus == '1'
	||  oldStatus == '2'
	||  oldStatus == '3'
	||  oldStatus == '4'
	||  oldStatus == '5'
	||  oldStatus == '6'
	||  oldStatus == 'A'
	||  oldStatus == 'B'
	||  oldStatus == 'C'
	||  oldStatus == 'Z')) return false;
	
	if (!(newStatus == '1'
	||  newStatus == '2'
	||  newStatus == '3'
	||  newStatus == '4'
	||  newStatus == '5'
	||  newStatus == '6'
	||  newStatus == 'A'
	||  newStatus == 'B'
	||  newStatus == 'C'
	||  newStatus == 'Z')) return false;

	if (oldStatus == newStatus) return true;
	
	// 6:complete, Z:stop
	if ((oldStatus == '6' || oldStatus == 'Z') && newStatus != oldStatus) return false;
	
	// oldStatus > newStatus check
	if (oldStatus > newStatus && (oldStatus != 'C' || newStatus != '4')) return false;
	
	// 2:accept
	if (newStatus == '2' && oldStatus != '1') return false;
	
	// 3:in development
	if (newStatus == '3' && oldStatus != '2') return false;
	
	// 4:in waiting to check
	if (newStatus == '4' && (oldStatus != '3' && oldStatus != 'C')) return false;
	
	// 5:in checking
	if (newStatus == '5' && oldStatus != '4') return false;
	
	// 6:complete
	if (newStatus == '6' && oldStatus != '5') return false;
	
	// A:return
	if (newStatus == 'A' && oldStatus != '5') return false;
	
	// B:accept again
	if (newStatus == 'B' && oldStatus != 'A') return false;
	
	// C:in revision
	if (newStatus == 'C' && oldStatus != 'B') return false;
	
	return true;
}

/**
 * function	: Date Check
 * parameter:
 * value	: the value format is YYYYMMDD
 * return	: true/false
 */
function isDate(value) {
	
	if ( value.indexOf('.') >= 0 ) return false;
	
	if ( value.length != 10 ) {
		// length check
		if ( value.length != 8 ) return false;
	
		// number check
		if ( isNaN(value) ) return false;
		
		// space check
		if ( value.indexOf(' ') >= 0 && value.indexOf(' ') <= 7 ) return false;
	
		var year	= parseInt(value.substring(0,4), 10);
		var month	= parseInt(value.substring(4,6), 10);
		var day		= parseInt(value.substring(6,8), 10);
	} else {
		// length check
		if ( value.substring(4,5) != '-' ) return false;
		if ( value.substring(7,8) != '-' ) return false;
		
		// space check
		if ( value.indexOf(' ') >= 0 && value.indexOf(' ') <= 9 ) return false;
		
		var year	= parseInt(value.substring(0,4), 10);
		var month	= parseInt(value.substring(5,7), 10);
		var day		= parseInt(value.substring(8,10), 10);
	}
	
	

	// year check
	if ( year < 1 ) return false;

	// month check
	if ( month < 1 || month > 12 ) return false;

	// day check
	if ( day < 1 || day > 31 ) return false;

	// monthly day check
	switch ( month ) {
		case 2:
			if ( day < 1 ) return false;

			if ( (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) ) {
				if (day > 29) return false;
			} else {
				if (day > 28) return false;
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			if ( day > 30 || day < 1 ) return false;
			break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
		default:
			if ( day > 31 || day < 1 ) return false;
			break;
	}
	return true;
}


/**
 * single byte char count
 *
 * parameter:
 * value	: the value to count
 */
function byteCount(value) {
	var count	= 0;
	var currentChar;

	for ( var i = 0; i < value.length; i++ ) {
		currentChar	= escape(value.charAt(i));
		if ( (navigator.appName.indexOf("Netscape") != -1)
			&& currentChar.length > 3
			&& value.indexOf("%") != -1 ) {
			count += 2;
		}
		else if ( (navigator.appName.indexOf("Internet Explorer") != -1)
				&& currentChar.length == 6 ) {
			if(currentChar.indexOf("%uFF") == -1) {
				count += 2;
			} else if ( (currentChar == "%uFFE5")
					|| (currentChar.indexOf("%uFF") != -1 && eval(currentChar.charAt(4)) < 7) ) {
				count += 2;
			}
		} else {
			count++;
		}
	}
	return count;
}

/**
 * open dialog window and return selected value
 *
 * parameter:
 * 1.(required)	url			: dialog program id
 * 2.(optional)	width		: dialog box width
 * 3.(optional)	heigth		: dialog box height
 * 4.(optional)	set_items	: some items string to set value from dialog (dummy:'^')
 *							: exist		: fetch to set items
 *							: no exist	: return values (dummy:'^')
 * 5.(optional)	s_kbn		: 1			: automatic set items
 *							  other		: return values (dummy:'^')
 */
function openDialog(url, width, height, set_items, s_kbn) {
	var i,strPath,strReturn;
	var retValue	= new Array();
	var itemsName	= new Array();
	
	if ( width == null || width == "" )  width  = "600px";
	if ( height == null || height == "" ) height = "400px";
	
	strPath		= "fscA000_01.jsp?url=" + escape(url);
	
	//show print page
	if ( s_kbn == 2 || s_kbn == "2" ) 
	strPath		= "fscA000_01.jsp?url=" + escape(url) + "&urlprint=fscA000_02.jsp";
	
	strReturn	= window.showModalDialog(strPath, url,
				"dialogWidth:" + width + "; dialogHeight:" + height + "; help:no; resizeable:yes; status:no");
				
	

	if ( strReturn == null || strReturn == "" ) return "";
	if ( set_items == null || set_items == "" ) return strReturn;
	if ( s_kbn != 1 && s_kbn != "1" ) return strReturn;
	
	// set items
	itemsName	= set_items.split("^");
	retValue	= strReturn.split("^");

	for ( i = 0; i < retValue.length; i++ ) {
		if ( i > itemsName.length ) break;
		if ( itemsName[i] == null || itemsName[i] == "" ) continue;
		
		if ( document.all.item(itemsName[i]).value != null ) {
			document.all.item(itemsName[i]).value		= retValue[i];
		} else {
			document.all.item(itemsName[i]).innerHTML	= retValue[i];
		}
	}
	return strReturn;
}

/**
 * get message with parameter
 *
 * parameter:
 * 1.(required)	message	: message to show
 * 2.(required)	parm	: message parameter array
 */
function getMessage(message,parm) {
	for ( i = 0; i < parm.length; i++ ) {
		pos = message.indexOf("%" + (i + 1))
		if ( pos > -1 ) {
			message = message.substring(0, pos) + parm[i] + message.substring((pos + 2), message.length);
		}
	}
	return message;
}

/**
 * show messagebox with parameter
 *
 * parameter:
 * 1.(required)	s_kbn 	: 1: OK button only
 *						  2: OK and Cancel button
 * 2.(required)	message	: message to show
 * 3.(optional)	parm	: message parameter array
 */
function showMessage(s_kbn,message,parm) {
	var msg = message;
	if ( parm != null && parm != "") {
		msg = getMessage(message,parm);
	}
	if ( s_kbn == 1 ) {
		alert(msg);
		return "";
	}
	if ( s_kbn == 2 ) {
		if ( confirm(msg) == true ) {
			return true;
		} else {
			return false;
		}
	}
}


/**
 * change to formated number. format example "###,###,##0.00"
 */
function toDecimalFormat(data,format) {
	
	// no format while more than one point
	if ( format.toString().indexOf(".") != format.toString().lastIndexOf(".") ) return data;
	if ( !isDecimal(data) ) return data;
	
	var pos				= format.toString().lastIndexOf(".");
	var len_fraction	= format.length - pos - 1;
	var multiplier		= 1;
	
	for ( var i = 0; i < len_fraction; i++ ) multiplier	= multiplier * 10;
	
	var ret_value		= Math.round(parseFloat(data) * multiplier) / multiplier;
	pos					= ret_value.toString().lastIndexOf(".");
	var ret_number		= "";
	var ret_fraction	= "";
	
	if ( pos < 0 ) {
		ret_number	= ret_value.toString();
		for ( var i = 0; i < len_fraction; i++ ) ret_fraction	= ret_fraction + "0";
	} else {
		ret_number	= ret_value.toString().substring(0, pos);
		ret_fraction	= ret_value.toString().substr(pos + 1);
	}
	
	ret_value	= toNumberFormat(ret_number) + "." + ret_fraction;
	
	return ret_value;
}

/**
 * set focus
 * 
 * parameter:
 * item_name: item to focus
 */
function setFocus(item_name) {
	if ( document.readyState != "complete" ) return;
	if ( trim(item_name) == "" ) return;
	if ( document.all.item(item_name) == null ) return;
	if ( document.all.item(item_name).disabled == true ) return;
	if ( document.all.item(item_name).readonly == true ) return;
	
	// set focus
	document.all.item(item_name).focus();
	
	// select text
	if ( document.all.item(item_name).type == "text" )
	document.all.item(item_name).select();
}

/**
 * clear name while code is changed
 * 
 * parameter:
 * items_name: items name to clear (dummy:'^')
 */
function clearName(items_name) {
	var i;
	var items	= new Array();
	
	if ( trim(items_name) == "" ) return;
	items	= items_name.split("^");

	for ( i = 0; i < items.length; i++ ) {
		if ( trim(items[i]) == "" ) continue;
		
		if ( document.all.item(items[i]).value != null ) {
			document.all.item(items[i]).value		= "";
		} else {
			document.all.item(items[i]).innerHTML	= "";
		}
	}
}

/**
 * Back Space check
 */
function checkBackSpace() {
	var code = event.keyCode;
	if ( code == 8 ) {
		if ( event.srcElement.id != "datafile" && event.srcElement.id != "imagefile" ) {
			event.keyCode = 0;
			return;
		}
	}
}

// iframe content resize
function iframeresize() {
	if(parent.document.all.item("content"))
	parent.document.all.item("content").height=parent.document.all.item("content").Document.body.scrollHeight;
	
	if(parent.document.all.item("ifrm_print"))
	parent.document.all.item("ifrm_print").height=parent.document.all.item("ifrm_print").Document.body.scrollHeight;
	
}

function SetFont(size){
	document.getElementById("contentPanel").style.fontSize=size
}
