var Error_01 = "(E01) Please enter: ~~1";
var Error_02 = "(E02) Please select ~~1";
var Error_03 = "(E03) Are you sure to delete ~~1: ~~2 ?";
var Error_04 = "(E04) ~~1: Please enter the valid number.";
var Error_05 = "(E05) ~~1: Please enter a number with up to ~~2 decimal.";
var Error_06 = "(E06) ~~1 should not be greater than ~~2.";
var Error_07 = "(E07) Password does not match.";
var Error_08 = "(E08) ~~1 should at least ~~2 character.";
var Error_09 = "(E09) ~~1 is not a valid date.";

var Error_10 = "(E10) Email address seems incorrect (check @ and .'s)";
var Error_11 = "(E11) The username doesn't seem to be valid.";
var Error_12 = "(E12) Destination IP address is invalid!";
var Error_13 = "(E13) The domain name doesn't seem to be valid.";
var Error_14 = "(E14) The email address must end in a three-letter domain, or two letter country.";
var Error_15 = "(E15) This email address is missing a hostname!";

var Error_16 = "(E16) Session Timeout";
var Error_17 = "(E17) Are you sure to confirm ~~1: ~~2 ?";
var Error_18 = "(E18) Are you sure to cancel ~~1: ~~2 ?";
var Error_19 = "(E19) ~~1 should be greater than ~~2.";
var Error_20 = "(E20) Are you sure to quit without save (~~1 ~~2) ?";
var Error_21 = "(E21) Are you sure to save ~~1 ?";

var TotalErrorMessage = "";
var Flag = false;
var submitForm = "yes";

//------------------------------------------------------------------------------------------------------------------------
function chkFocus(x)
{
	if(!Flag)
	{
		x.focus();
		Flag = true;
	}
}
function processFormResult(form,url)
{
	if(TotalErrorMessage.length > 0)
		alertErrorMessage();
	else {
		form.action = url;
		form.submit();
	}
}

function processFormResult2(form,url,top_url)
{
	if(TotalErrorMessage.length > 0)
		alertErrorMessage();
	else {
		form.action = url;
		//alert("haha");
		topFrameGo(top_url);
		form.submit();
	}
}

function alertErrorMessage()
{
	if(TotalErrorMessage.length > 0)
	{
		Temp = TotalErrorMessage;
		TotalErrorMessage = "";
		Flag = false;
		submitForm = "yes";
		alert(Temp);
	}
}
function setErrorMessage(Message, obj)
{
	TotalErrorMessage+=Message + "\n";
	chkFocus(obj);
	return(false);
}
function returnErr() {
	if(TotalErrorMessage.length > 0)
		return (false);
	else
		return (true);
}
function resetErr() {
	if(TotalErrorMessage.length > 0)
		TotalErrorMessage = "";
}
//------------------------------------------------------------------------------------------------------------------------
function chkNotBlank(x, fieldDesc,Compulsory, obj) {
	if(obj.type==null){
		for(i=0;i<obj.length;i++){
			if(obj[i].checked)
			return(true);}
		if(Compulsory){
			TotalErrorMessage += Error_01.replace(/~~1/, fieldDesc) + "\n";
			chkFocus(obj[0]);
		}
		return(false);
	}
	else
	{
		if(x.length==0 || (obj.type=="checkbox" && !obj.checked) || (obj.type=="select-one")){
			if(Compulsory){
				if(obj.type == "select-one") {
					if ( (obj.value=="notselect") || (obj.value=="") || (obj.value=="NA") )
						TotalErrorMessage += Error_02.replace(/~~1/, fieldDesc) + "\n";
					else
						return(true);
				}
				else
					TotalErrorMessage += Error_01.replace(/~~1/, fieldDesc) + "\n";
				chkFocus(obj);}
			return(false);
		}
	}
	return(true);
}

function chkPW(field1, field2, obj)
{
	if (chkMoreThan(field1, 8, "Password", obj)) {
		if (field1 == field2)
			return (true) ;
		else {
			TotalErrorMessage+=Error_07 + "\n";
			chkFocus(obj);
			return (false) ;
		}
	} else
		return (false);
}

function confirmDel (fieldDesc,x) {
	ErrorMessage = Error_03.replace(/~~1/, fieldDesc);
	return (confirm(ErrorMessage.replace(/~~2/, x)));
}

function confirmCfm (fieldDesc,x) {
	ErrorMessage = Error_17.replace(/~~1/, fieldDesc);
	return (confirm(ErrorMessage.replace(/~~2/, x)));
}

function confirmClx (fieldDesc,x) {
	ErrorMessage = Error_18.replace(/~~1/, fieldDesc);
	return (confirm(ErrorMessage.replace(/~~2/, x)));
}

function confirmSave (module) {
	return (confirm(Error_21.replace(/~~1/, module)));
}

function confirmCancel (module,action,url,target) {
	ErrorMessage = Error_20.replace(/~~1/, module);
	if (confirm(ErrorMessage.replace(/~~2/, action)))
		open(url,target);
}

function confirmbox (fieldDesc,form,fieldvalue) {
	if(TotalErrorMessage.length <= 0) {
		if (fieldvalue == "notmenu")
			a = confirmDel(fieldDesc,"");
		else
			a = eval("confirmDel(fieldDesc,form."+fieldvalue+")");
		if (a)
			return (true);
		else
			return (false);
	} else
		return (true);
}

function chkDecimals(obj, x, fieldDesc, limit, digit) {

	decallowed = digit;  // how many decimals are allowed?

	a = chkNotGreaterThan(x,limit,fieldDesc,obj);
	if (a) {
		if (isNaN(x) || x == "") {
			TotalErrorMessage += Error_04.replace(/~~1/, fieldDesc) + "\n";
			chkFocus(obj);
			return (false);
		}
		else {
			if (x.indexOf('.') == -1) x += ".";
				dectext = x.substring(x.indexOf('.')+1, x.length);
			if (dectext.length > decallowed) {
				ErrorMessage = Error_05.replace(/~~1/, fieldDesc);
				return(setErrorMessage(ErrorMessage.replace(/~~2/, digit),obj));
	      		}
			else
				return (true);
	   	}
	} else
		return (false);
}

function chkNotGreaterThan(x,y,fieldDesc, obj)
{
	tempx = x;
	tempy = y;

	if(parseFloat(tempx) >= parseFloat(tempy)){
		ErrorMessage = Error_06.replace(/~~1/, fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/, y), obj));
	}
	else
		return(true);
}

function chkLessThan(x,y,fieldDesc, obj)
{
	tempx = x;
	tempy = y;

	if(parseFloat(tempx) > parseFloat(tempy)){
		ErrorMessage = Error_06.replace(/~~1/, fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/, y), obj));
	}
	else
		return(true);
}

function chkGreaterThan(x,y,fieldDesc, obj)
{
	tempx = x;
	tempy = y;

	if(parseFloat(tempx) <= parseFloat(tempy)){
		ErrorMessage = Error_19.replace(/~~1/, fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/, y), obj));
	}
	else
		return(true);
}

function chkNotLessThan(x,y,fieldDesc, obj)
{
	tempx = x;
	tempy = y;

	if(parseFloat(tempx) < parseFloat(tempy)){
		ErrorMessage = Error_19.replace(/~~1/, fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/, y), obj));
	}
	else
		return(true);
}

function chkMoreThan(x, y, fieldDesc, obj)
{
	if(x.length < y)
	{
		ErrorMessage = Error_08.replace(/~~1/ , fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/ , y), obj));
	}
	else
		return(true);
}
					   // true = Must fill
function chkDateString(datestring,obj,desc,forcenull) {
	var year;
	var month;
	var day;

	if (forcenull) {
		if (chkMoreThan (datestring,8,desc,obj)) {

			year = datestring.substring(0, 4);
			month = datestring.substring(4, 6);
			day = datestring.substring(6, 8);

			return chkDate(day, month, year, obj);
		} else
			return false;
	} else {
		if (datestring != "") {
			if (chkMoreThan (datestring,8,desc,obj)) {
				year = datestring.substring(0, 4);
				month = datestring.substring(4, 6);
				day = datestring.substring(6, 8);

				return chkDate(day, month, year, obj);
			}
			else
				return false;
		} else
			return true;
	}
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }
function chkDate (day,month,year,obj) {
    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return (true);
    else {
	TotalErrorMessage += Error_09.replace(/~~1/, year+" / "+(month+1)+" / "+day) + "\n";
	chkFocus(obj);
        return (false);
    }
}

function chkEmail (emailStr) {
	/* The following pattern is used to check if the entered e-mail address
	   fits the user@domain format.  It also is used to separate the username
	   from the domain. */
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
	   characters.  We don't want to allow special characters in the address.
	   These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a
	   username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	   which case, there are no rules about which characters are allowed
	   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	   is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	   e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	   non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	   For example, in john.doe@somewhere.com, john and doe are words.
	   Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	   domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


	/* Finally, let's start trying to figure out if the supplied address is
	   valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
	   different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	  /* Too many/few @'s or something; basically, this address doesn't
	     even fit the general mould of a valid e-mail address. */
		TotalErrorMessage += Error_10 + "\n";
		return (false);
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	// See if "user" is valid
	if (user.match(userPat)==null) {
	    // user is not valid
	    TotalErrorMessage += Error_11 + "\n";
	    return (false);
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    // this is an IP address
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		    	TotalErrorMessage += Error_12 + "\n";
			return (false);
		    }
	    }
	    return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		TotalErrorMessage += Error_13 + "\n";
		return (false);
	}

	/* domain name seems valid, but now make sure that it ends in a
	   three-letter word (like com, edu, gov) or a two-letter word,
	   representing country (uk, nl), and that there's a hostname preceding
	   the domain or country. */

	/* Now we need to break up the domain to get a count of how many atoms
	   it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 ||
	    domArr[domArr.length-1].length>3) {
	   // the email address must end in a two letter or three letter word.
	   TotalErrorMessage += Error_14 + "\n";
	   return (false);
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   TotalErrorMessage += Error_15 + "\n";
	   return (false);
	}

	// If we've gotten this far, everything's valid!
	return true;
}

function changeText(formName,textName,selectName) {
	if (eval(formName+"."+selectName+".selectedIndex != -1") )
		if (eval(formName+"."+selectName+".options["+formName+"."+selectName+".selectedIndex].value != \"NA\""))
			eval(formName+"."+textName+".value = "+formName+"."+selectName+".options["+formName+"."+selectName+".selectedIndex].text");
}
