/**************************************************************************************
Author:			Vuthy Pen
Date:			10/05/2005
Description:	custom Javascript functions
**************************************************************************************/
<!--

/* ***********************************************************************************
Function:	IsNumeric 
Input:      str (string): the string to be evaluated
Return:     A Boolean value indicating whether an expression can be evaluated as a number.
Desc:  		This function returns a Boolean value indicating whether an expression 
			can be evaluated as a number.
**************************************************************************************/
function IsNumeric(str)
{
   	var ValidChars = "0123456789.";
   	var IsNumber = true;
   	var Char;
 
   	for (i = 0; i < str.length && IsNumber == true; i++) 
	{ 
      Char = str.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         	IsNumber = false;
         }
  	}
   	return IsNumber;   
} //End IsNumeric Function
   
function Plural(num)
{
	if (num > 1){
		return "s";
	} else {
		return "";
	}
} //End Plural Function

function MaskStatus(status)
{
	window.status = status;
	return true;
} //End MaskStatus Function


/* ***********************************************************************************
Function:	Trim 
Input:      str (string): the string to be altered
Return:     A string with no leading or trailing spaces;
            returns null if invalid arguments were passed
Desc:  		This function removes all leading and tralining spaces from a string.
*************************************************************************************/
function Trim(str)
{
	if(str.length < 1){
		return "";
	}
	
	str = RTrim(str);
	str = LTrim(str);
	
	if(str == ""){
		return "";
	}
	else{
		return str;
	}
} //End Trim Function


/* *******************************************************************************
Function:	RTrim 
Input:      str (string): the string to be altered
Return:     A string with no leading spaces;
            returns null if invalid arguments were passed
Desc:  		This function removes all tralining spaces from a string.
********************************************************************************/
function RTrim(str)
{
	var w_space = String.fromCharCode(32);
	var v_length = str.length;
	var strTemp = "";
	if(v_length < 0){
		return "";
	}
	var iTemp = v_length -1;
	
	while(iTemp > -1){
		if(str.charAt(iTemp) == w_space){
	}
	else{
		strTemp = str.substring(0,iTemp +1);
		break;
	}
	iTemp = iTemp-1;
	
	} //End While
	return strTemp;

} //End RTrim Function


/* *******************************************************************************
Function:	LTrim 
Input:      str (string): the string to be altered
Return:     A string with no trailing spaces;
            returns null if invalid arguments were passed
Desc:  		This function removes all leading spaces from a string.
********************************************************************************/
function LTrim(str)
{
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return "";
	}
	
	var v_length = str.length;
	var strTemp = "";
	
	var iTemp = 0;
	
	while(iTemp < v_length){
		if(str.charAt(iTemp) == w_space){
		}
		else{
			strTemp = str.substring(iTemp, v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
	
} //End LTrim unction


/* *******************************************************************************
Function:	Left 
Input:      str (string): the string from which the leftmost characters are returned
			n (integer): the number of characters to return 
Return:     Returns a specified number of characters from the left side of a string.
Desc:  		This function takes a string and a length and returns a specified 
			number of characters from the left side of a string.
********************************************************************************/
function Left(str, n)
{
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return String(str).substring(0, n);
} //End Left Function
	
	
/* *******************************************************************************
Function:	Right 
Input:      str (string): the string from which the rightmost characters are returned
			n (integer): the number of characters to return 
Return:     Returns a specified number of characters from the right side of a string.
Desc:  		This function takes a string and a length and returns a specified 
			number of characters from the right side of a string.
********************************************************************************/
function Right(str, n)
{
	if (n <= 0)
	   return "";
	else if (n > String(str).length)
	   return str;
	else {
	   var iLen = String(str).length;
	   return String(str).substring(iLen, iLen - n);
	}
} //End Right Function
	
	
/* *******************************************************************************
Function:	stripCtrlCodes 
Input:      str (string): the string to be stripped
			n (integer): the number of characters to return
Return:     A string with no control codes, ie \n\r\t etc.
Desc:  		This function removes all control codes from a string.
			This function should be used when trying to write a string to a 
			database that has been entered into a text area.
********************************************************************************/
function stripCtrlCodes(str)
{
     var strBuffer = new String(str);
     var strOutputBuffer = new String("");
     
     for( iIndex = 0; iIndex < strBuffer.length; iIndex ++ )
     {
          if( strBuffer.charCodeAt( iIndex ) < 33 ) 
          {
               if( iIndex > 0 )
               {
                    if( strOutputBuffer.length > 0 )
                    {
                         if( strOutputBuffer.charCodeAt( strOutputBuffer.length - 1 ) != 32 )
                              strOutputBuffer += " ";
                    }
               }
          }
          else
               strOutputBuffer += strBuffer.charAt( iIndex );
     }
     
     return strOutputBuffer;
} // End stripCtrlCodes Function

function RemoveQueryString()
{
	//Show queryString
	alert('Query string: '+self.location.search)
	self.location = self.location.protocol + '//' + self.location.host + self.location.pathname;
}


/* *******************************************************************************
Function:	IsValidEMail 
Input:      str (string): the email string to validate
Return:     boolean
Desc:  		This function checks for a valid email address
********************************************************************************/
function IsValidEMail2(crtl, required, error_msg) 
{
	if (required == "undefined") required = false;
	if (error_msg == "undefined") error_msg = "";
	
	var emailStr = crtl.value;
	
	//alert(emailStr)
	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 represents the range of characters allowed as
   the first character in a valid username or domain.  I just made it
   the same as above, but if you want to add a different constraint,
   you would change it here. */
	var firstChars=validChars
	/* 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 at atom (basically a series of
   non-special characters.) */
	var atom="(" + firstChars + 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 course 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. */
		if (error_msg != ""){	 
			alert(error_msg);
		}
		crtl.focus();
		return false;	
	}
	
	var user=matchArray[1]
	var domain=matchArray[2]

	// See if "user" is valid 
	if (user.match(userPat)==null) {
		// user is not valid
		//alert("The username doesn't seem to be valid.");
		alert(error_msg);
		crtl.focus();
		return false;
	}
	return true;
}

/* *******************************************************************************
Function:	IsValidEMail 
Input:      str (string): the email string to validate
Return:     boolean
Desc:  		This function checks for a valid email address
********************************************************************************/
function IsValidEMail(str) 
{
	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	var ret = true;
	
	if (str.indexOf(at) == -1){
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr){
	   return false;
	}

	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr){
		return false;
	}

	 if (str.indexOf(at, (lat+1)) != -1){
		return false;
	 }

	 if (str.substring(lat-1,lat) == dot || str.substring(lat+1, lat+2) == dot){
		return false;
	 }

	 if (str.indexOf(dot, (lat+2)) == -1){
		return false;
	 }
	
	 if (str.indexOf(" ") != -1){
		return false;
	 }

	return true;
}


function GetRadioButtonValue(ctrl_radio_button, required, error_msg)
{
	// This function returns the selected radio_button  value if selected
	
	// set var required and error_msg to default
	if (required == "undefined") required = false;
	if (error_msg == "undefined") error_msg = "";
	
	// set var radio_choice to false
	var radio_choice = false;
	var radio_choice_value;
	
	// Loop from zero to the one minus the number of radio button selections
	for (counter = 0; counter < ctrl_radio_button.length; counter++){
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (ctrl_radio_button[counter].checked){
			radio_choice = true; 
			radio_choice_value = ctrl_radio_button[counter].value;
		}
	}

	if (required == true){
		if (!radio_choice){
			// If there were no selections made display an alert box 
			alert(error_msg)
			return false;
		}	
	}
	
	if (radio_choice){
		return radio_choice_value;
	} else {
		return 0;
	}

}

/* *******************************************************************************
Function:	ClearButtons 
Input:      buttonGroup (radio control): the radio button control to clear
Return:     
Desc:  		Clear radio buttons
********************************************************************************/
function ClearButtons(buttonGroup){ 
   	for (i=0; i < buttonGroup.length; i++) {
    	if (buttonGroup[i].checked == true){ 
    		buttonGroup[i].checked = false 
    	}
	} 
}


/* *******************************************************************************
Function:	getCheckedValue 
Input:      radioObj (radio control): the checkbox control to get value
Return:     string
Desc:  		return the value of the radio button that is checked;
			return an empty string if none are checked, or
			there are no radio buttons
********************************************************************************/
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
//-->