<!--
// -------------------------------------------------------------------------------------------------
// filename:	common_function.js
// description:	this file is uses to store all common used javascript function,
//				if you want to see each function in deep detail, please read the
// 				session description, which placed above each function.
// written by:	Utada P.W. SIU
// date:		2004-03-11
// -------------------------------------------------------------------------------------------------


//@@ a function used to check the user's login information
//@@ [username] as String			- username of user
//@@ [password] as String			- password of user
function login(username, password){

	if (username.replace(/ /g, "").length == 0){			//@ check username is provided or not
		alert ("Please enter your username.");
		return false;
	}

	if (password.replace(/ /g, "").length == 0){			//@ check password is provided or not
		alert ("Please enter your password.");
		return false;
	}

	return true;
}


//@@ a function uses to close current browser
function exit(){
	window.close();
}


//@@ a function uses to redirect to other page
//@@ [url] as String				- destination url
function redirect(url){
	window.location = url;
}


//@@ a function to get the length of a string, included space
//@@ [text] as string				- text which need to determine the byte length
function getWordLength(text){
	var numOfChar = 0;
	var charPosition = 0;

	for (charPosition = 0; charPosition < text.length; charPosition++){

		//-- if the char code large than 256, suppose it is not a English char, other char such
		//-- as Chinese using double bytes to represent the char
		(text.charCodeAt(charPosition) >= 256) ? numOfChar += 2 : numOfChar++;
	}

	return numOfChar;
}


//@@ a function to open new browser and placed in the central of the screen
//@@ [url] as String				- target url
//@@ [name] as String				- name of just opened browser
//@@ [width, height] as Integer		- width and height of the just opened browser
//@@ [feather] as String			- feather of the just opened browser
function openBrowser(url, name, width, height, feather){
	var isIE	= (document.all ? true : false);				//@ determine internet browser either is or not is Internet Explorer
	var isNS	= (document.layer ? true : false);				//@ determine internet browser either is or not is Netscape Navigator 4
	var isDOM	= (document.getElementById ? true : false);		//@ determine internet browser either is or not is Netscape Navigator 6+ or other
	var browser				= new Object();
	var browser_attribute	= "";

	if (!browser.win || (browser.win && browser.win.closed)){
		browser.url		= url;
		browser.name	= name;
		browser.width	= width;
		browser.height	= height;

		//-- centerized the opened broswer and initial its attribute
		if (isIE || isDOM){
			browser.left	= (screen.width - browser.width) / 2;
			browser.top		= (screen.height - browser.height) / 2;
			browser_attribute = "left=" + browser.left + ",top=" + browser.top + ",resizeable=no,width=" + browser.width + ",height=" + browser.height + "," + feather;
		} else if (isNS){
			browser.left	= window.screenX + ((window.outerWidth - browser.width) / 2);
			browser.top		= window.screenY + ((window.outerHeight - browser.height) / 2);
			browser_attribute = "screenX=" + browser.left + ",screenY=" + browser.top + ",resizeable=no,width=" + browser.width + ",height=" + browser.height + "," + feather;
		}

		//-- open new browser
		browser.win = window.open (browser.url, browser.name, browser_attribute);
	}

	browser.win.focus();
	
	return browser;
}


//@@ a function to validate user provided email
//@@ [email] as String				- email address which need to validate
function isValidEmail(email){
	var emailInfo = email.split("@");

	if (emailInfo.length > 2){									//@ check at most one @ is exist
		//alert("check email error index: 1");
		return false;
	} else if (emailInfo.length < 2){
		//alert("check email error index: 2");
		return false
	} else{
		var user = emailInfo[0];
		var domain = emailInfo[1];
		var userFormat = /[\s\/\\\*\+\?\|\(\)\[\]\{\}~!@#$%^&=;\'\"<>:]/;
		var domainFormat = /[\s\/\\\*\+\?\|\(\)\[\]\{\}~!@#$%^&=;\'\"<>:_]/;

		if (user.match(userFormat) != null){					//@ check username haven't any illegal character
			//alert("check email error index: 3");
			return false;
		}

		if (domain.indexOf(".") == -1){							//@ check at least the format of domain be XXXX.XXX
			//alert("check email error index: 4");
			return false;
		}

																//@ check the top domain at least have 1 char and not more than 4 char
		if ((domain.substring(domain.lastIndexOf(".") + 1, domain.length).length > 4) || (domain.substring(domain.lastIndexOf(".") + 1, domain.length).length < 2)){
			alert("check email error index: 5");
			return false;
		}
																//@ check the top level domain MUST be English character
		if (domain.substring(domain.lastIndexOf(".") + 1, domain.length).match(/[^A-Za-z]/) != null){
			alert("check email error index: 6");
			alert("X"+ domain.substring(domain.lastIndexOf(".") + 1, domain.length) +"X");
			return false;
		}

		if (domain.match(domainFormat) != null){				//@ check the domain havent any illegal character
			alert("check email error index: 7");
			return false;
		}
	}
	return true;
}


//@@ a function to disable the form item
//@@ [form] as formObject				- such as text object, radio object, checkbox object etc.
//@@ [object_type] as String			- txt, chk, rdo etc
//@@ [item_prefix] as String			- Prefix of item, Exhib for exhibition, Mail for mailing
function disableFormItems(form, object_type, item_prefix){
	var i = 0;
	var type = object_type.split(", ");

	for (i = 0; i < form.length; i++){
		for (var j = 0; j < type.length; j++){
			if (form[i].name.indexOf(type[j] + item_prefix) > -1){
//				form[i].disabled = true;
				form[i].readOnly = true;
				form[i].className = "readonly";
				break;
			}
		}
	}
}


//@@ a function to enable the form item
//@@ [form] as formObject				- such as text object, radio object, checkbox object etc.
//@@ [object_type] as String			- txt, chk, rdo etc
//@@ [item_prefix] as String			- Prefix of item, Exhib for exhibition, Mail for mailing
function enableFormItems(form, object_type, item_prefix){
	var i = 0;
	var type = object_type.split(", ");

	for (i = 0; i < form.length; i++){
		for (var j = 0; j < type.length; j++){
			if (form[i].name.indexOf(type[j] + item_prefix) > -1){
//				form[i].disabled = false;
				form[i].readOnly = false;
				form[i].className = "input_box";
				break;
			}
		}
	}
}

//@@ round the number
//@@ [number] as Integer			- number need to round
//@@ [number_of_decimal] as Integer	- number of decimal
function round(number, number_of_decimal){
	var roundedNumber = 0;

	roundedNumber = number;
	roundedNumber = Math.round(number * Math.pow(10, number_of_decimal)) / Math.pow(10, number_of_decimal);

	//-- rounds number to number_of_decimal places
	return (roundedNumber);
}


//@@ trim the text
String.prototype.trim = function(){
	return this.replace(/^\s*/,"").replace(/\s*$/,"");
}

//@@ valid the provide password format is correct
//@@ [password] as String				- user provided password
//@@ [min_length] as Integer			- minimun length of password
//@@ [max_length] as Integer			- maximun length of password
function isValidPassword(password, min_length, max_length){
	if ((password.length < min_length) || (password.length > max_length)){
		return false;
	} else{
		var passwordFormat = /[\W]/;

		if (password.match(passwordFormat) != null){				/* @ check username haven't any illegal character */
			return false;
		}
	}
	return true;
}

//@@ replace special symbol
//@@ [query] as String					- string
function encode(query){
	query = query.replace(/\"/g, "");
	query = query.replace(/\\/g, "");
	query = query.replace(/[&]/g, "%26");
	query = query.replace(/\#/g, "");
	return (query);
}
// ==========================================================================================
// [ amendment history ]
// 2004-03-19	Utada P.W.SIU	add new function, "round" to round the number.
//								add enw function, "trim" to the String object
// 2004-03-11	Utada P.W.SIU	combined the function "disableFormItems" and "enableFormItems"
//								in this file, for more details information, please refer to
//								that two function.
// ==========================================================================================

//-->
