/*
 Author 		: Chairat Panpun
 Date			: 8/11/00
 Description 	: This module consists of a collection of functions concerning to
	string input from clients such as format validation.
 Module 		: String Functions
 Supervisor 	: Mark Able 
 Version 		: 1.0  
 Compatability	: IE 4.0+/Netscape 4.0+ 
*/
// isAlphaNumeric(inpVal) determines whether InpVal is alphabetic and/or number (A-z, 0-9).
function isAlphaNumeric(inpVal) {
	var strValid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	var blnValid = true;
	var strTemp;
	
	if(inpVal.length==0)
		return false
		
	for (var i = 0; i < inpVal.length; i++) {
		strTemp = "" + inpVal.substring(i, i+1);
		if (strValid.indexOf(strTemp) == "-1") blnValid = false;
	}
	if (blnValid) {
		return true		// e.g. Sydney2000; 007; 750iL
	} else {
		return false	// e.g. Sydney 2000; -007; 750iL*
	}		
}

function isAlphaNumericThOrBlank(inpVal) {
var strValid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ"+String.fromCharCode(13,10);
	var blnValid = true;
	var strTemp;
	var chrcode
	strTemp = Trim(inpVal);
	if(strTemp.length==0)
	{
		return true
	}
	for (var i = 0; i < inpVal.length; i++) {
		chrcode =inpVal.charCodeAt(i);
		if(chrcode >1000)				  // unicode support
		{
			for (var i = 0; i < inpVal.length; i++) {
				strTemp = "" + inpVal.substring(i, i+1);
				if (strValid.indexOf(strTemp) == -1) 
					blnValid = false;
			}	
		}
		else
		{ 
			if((chrcode >= 65 && chrcode <= 90 ) || (chrcode >= 97 && chrcode <= 122) || (chrcode >= 161 && chrcode <= 249) || (chrcode >= 47 && chrcode <= 57)  || (chrcode == 32)  || (chrcode == 46)  || (chrcode == 40) || (chrcode == 41)  || (chrcode == 44) || (chrcode == 45) || (chrcode == 10) || (chrcode == 13) || (chrcode == 63))
			{
				blnValid = true;
			}
			else
			{
				blnValid = false;
			}
		}
	}
	
	if (blnValid) {
		return true		// e.g. Sydney2000; 007; 750iL
	} else {
		return false	// e.g. Sydney 2000; -007; 750iL*
	}
}

function isAlphaNumericTh(inpVal) {
	var strValid = ".À¶¤Éµ¨¢ª£¥¾Ã¹ÂºÅ®±¸³­°Ä¦¯âÈ¬«×¿ÕË¡ë´ÊçÇ§è¼é»ÍÔ·ØÁÙ½©ìÎ²êÌÑ  æäÓÐÑÑÕÕÏÖØØÄÆã×ìÒâàá0123456789ñòóôõö÷øù'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ/!(),-?:;"+String.fromCharCode(13,10);
	var blnValid = true;
	var strTemp;
	var chrcode
	strTemp = Trim(inpVal);
	if(strTemp.length==0)
	{
		return false
	}
	for (var i = 0; i < inpVal.length; i++) {
		chrcode =inpVal.charCodeAt(i);
		if(chrcode >1000)				  // unicode support
		{
			for (var i = 0; i < inpVal.length; i++) {
				strTemp = "" + inpVal.substring(i, i+1);
		
				if (strValid.indexOf(strTemp) == -1) 
					blnValid = false;
			}	
		}
		else
		{ 
			if((chrcode >= 65 && chrcode <= 90 ) || (chrcode >= 97 && chrcode <= 122) || (chrcode >= 161 && chrcode <= 249) || (chrcode >= 47 && chrcode <= 57)  || (chrcode == 32)  || (chrcode == 46)  || (chrcode == 39) || (chrcode == 40) || (chrcode == 41)  || (chrcode == 44) || (chrcode == 45) || (chrcode == 10) || (chrcode == 13) || (chrcode == 63))
			{
				blnValid = true;
			}
			else
			{
				blnValid = false;
			}
		}
	}
	
	if (blnValid) {
		return true		// e.g. Sydney2000; 007; 750iL
	} else {
		return false	// e.g. Sydney 2000; -007; 750iL*
	}
}

// isAlphabetic(inpVal) determines whether InpVal is alphabetic and/or number (A-z).
function isAlphabetic(inpVal) {
	var strValid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	var blnValid = true;
	var strTemp;
	if(inpVal.length==0)
		return false
	for (var i = 0; i < inpVal.length; i++) {
		strTemp = "" + inpVal.substring(i, i+1);
		if (strValid.indexOf(strTemp) == "-1") blnValid = false;
	}
	if (blnValid) {
		return true		// e.g. ABC; Z; sdd
	} else {
		return false	// e.g. A B C; 911; 'S*
	}	
}

/*
 isEmail(InpVal) is used to check if the entered e-mail address fits the 
 user@domain format (closely to RFC 822).
*/
function isEmail(inpVal) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+' 
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=inpVal.match(emailPat)
	var blnValid = true;
// Jil add
	for (var i = 0; i < inpVal.length; i++) {
		chrcode =inpVal.charCodeAt(i);
		if(chrcode >1000)               // unicode support
			blnValid = false;
   }
	if (!blnValid) {
		return false
	}
// Jil end

	if (matchArray==null) {	
		return false	// Email address seems incorrect (check @ and .'s)
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
		return false	// The username doesn't seem to be valid.
	}	
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
    	for (var i=1;i<=4;i++) {
			if (IPArray[i]>255)	{	
				return false	// Destination IP address is invalid!
			}
    	}
	    return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return false	// The domain name doesn't seem to be valid.
	}	
	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) {	
		return false	// The address must end in a three-letter domain, or two letter country.
	}	
	if (len<2) {	
		return false	// This address is missing a hostname!
	}	
	return true;
}

function isEmailOrBlank(inpVal) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+' 
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=inpVal.match(emailPat)
	var tempstr;
	var blnValid = true;

	tempstr = inpVal.toString();

	if(tempstr.length==0)
		return true;
//  Jil add
	for (var i = 0; i < inpVal.length; i++) {
		chrcode =inpVal.charCodeAt(i);
		if(chrcode >1000)               // unicode support
			blnValid = false;
   }

	if (!blnValid) {
		return false
	}		
//  Jil end
	if (matchArray==null) {	
		return false	// Email address seems incorrect (check @ and .'s)
	}	

	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
		return false	// The username doesn't seem to be valid.
	}	
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
    	for (var i=1;i<=4;i++) {
			if (IPArray[i]>255)	{	
				return false	// Destination IP address is invalid!
			}
    	}
	    return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return false	// The domain name doesn't seem to be valid.
	}	
	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) {	
		return false	// The address must end in a three-letter domain, or two letter country.
	}	
	if (len<2) {	
		return false	// This address is missing a hostname!
	}	
	return true;
}
function isPhone(inpVal){
	var PhoneAdd = inpVal;
	var FirstChr;
	var PhoneExp = /[0-9\)\(\-]/;
	var i;
	for(i=0 ; i<PhoneAdd.length ; i++)
	{
		FirstChr = PhoneAdd.substring(i, i+1);	
		if(!PhoneExp.test(FirstChr))
			return false;
	}
	// if there is nothing wrong, return true
	return true;
}
function isPhoneOrBlank(inpVal){
	var PhoneAdd = inpVal;
	var FirstChr;
	var PhoneExp = /[0-9\)\(\-]/;
	var i;
	if(inpVal.length==0)
		return true
	for(i=0 ; i<PhoneAdd.length ; i++)
	{
		FirstChr = PhoneAdd.substring(i, i+1);	
		if(!PhoneExp.test(FirstChr))
			return false;
	}
	// if there is nothing wrong, return true
	return true;
}

// isName(inpVal) determines whether InpVal is a valid name (A-z, -).
function isName(inpVal) {
	var strValid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-' "
	var blnValid = true;
	var strTemp;
		
	for (var i = 0; i < inpVal.length; i++) {
		strTemp = "" + inpVal.substring(i, i+1);
		if (strValid.indexOf(strTemp) == "-1") blnValid = false;
	}
	if (blnValid) {
		return true		// e.g. Abc-; Zx; SD-TH
	} else {
		return false	// e.g. A b c; 911; 'S*
	}	
}
function isAlphaEng(inpVal){
	var blnValid = true;
	var strTemp;
	if(inpVal.length==0)
		return true
		
	for (var i = 0; i < inpVal.length; i++) {
		chrcode =inpVal.charCodeAt(i);
		if(chrcode >1000)               // unicode support
			blnValid = false;
   } 
   if (blnValid) {
		return true		// e.g. Sydney2000; 007; 750iL
	} else {
		return false	// e.g. Sydney 2000; -007; 750iL*
	}
}