﻿//
// FUNCTION:	show_hide
// DESCRIPTION:	Function used to show and hide sections of HTML, so that content may expand and contract
//

// Do we use DOM or not?
var dom = (document.getElementById && !document.all)? 1: 0;

function show_hide(the_id)
{
  var obj = (dom)? document.getElementById(the_id): document.all[the_id];

  if(obj.style.visibility == "hidden"){ // show
      obj.style.visibility = "visible";
	  obj.style.display = "block"; // If this looks wrong, try 'inline'
  }
  else { // hide
      obj.style.visibility = "hidden";
	  obj.style.display = "none";
  }
}

// HTML CODE
/*
<!-- Any HTML here -->

<!-- Link to show the collapsible content -->
<a href="javascript:show_hide('my_options')">My Options</a>

<!-- The collapsible content -->
<div id="my_options" style="visibility:hidden; display:none;">
   <!-- HTML to show/hide here -->
</div>

<!-- Any HTML here --> 
*/

// *****************************************************************************************************

//
// FUNCTION:	ShowLayer
// DESCRIPTION:	Function used to show section of HTML defined in a <DIV> tag
//

// Do we use DOM or not?
var dom = (document.getElementById && !document.all)? 1: 0;

function ShowLayer(the_id)
{
  var obj = (dom)? document.getElementById(the_id): document.all[the_id];
  obj.style.visibility = "visible";
  obj.style.display = "block";
}

// HTML CODE
/*
<!-- Any HTML here -->

<!-- Link to show the collapsible content -->
<a href="javascript:ShowLayer('my_options')">My Options</a>

<!-- The collapsible content -->
<div id="my_options" style="visibility:hidden; display:none;">
   <!-- HTML to show/hide here -->
</div>

<!-- Any HTML here --> 
*/


// *****************************************************************************************************

//
// FUNCTION:	HideLayer
// DESCRIPTION:	Function used to hide section of HTML defined in a <DIV> tag
//

// Do we use DOM or not?
var dom = (document.getElementById && !document.all)? 1: 0;

function HideLayer(the_id)
{
  var obj = (dom)? document.getElementById(the_id): document.all[the_id];
  obj.style.visibility = "hidden";
  obj.style.display = "none";
}

// HTML CODE
/*
<!-- Any HTML here -->

<!-- Link to show the collapsible content -->
<a href="javascript:HideLayer('my_options')">My Options</a>

<!-- The collapsible content -->
<div id="my_options" style="visibility:visible; display:block;">
   <!-- HTML to show/hide here -->
</div>

<!-- Any HTML here --> 
*/


// *****************************************************************************************************

//
// FUNCTION:	SetFormFieldValue
// DESCRIPTION:	Function used to set form element value by element ID.
// VARIABLES:	SourceID:		ID of the form field to be changed
//				SourceValue:	New value of the field
//

function SetFormFieldValue(SourceID, Value)
{
	var SourceID = (dom)? document.getElementById(SourceID): document.all[SourceID];
	SourceID.value = Value;
}

// *****************************************************************************************************

//
// FUNCTION:	SetFormFieldActiveInactive
// DESCRIPTION:	Enables or disables a form field depending on the value of a different form field.
// VARIABLES:	SourceID:		ID of the form field to be checked
//				SourceValue:	Value of the source field which will trigger the enabling or disabling of the target field
//				TargetID:		ID of the target form field (i.e. the field which is to be enabled or disabled)
//				TargetDisabled:	true or false - true to disable: false to enable
//

function SetFormFieldActiveInactive(SourceID, SourceValue, TargetID, TargetDisabled)
{
	var SourceField = (dom)? document.getElementById(SourceID): document.all[SourceID];
	var TargetField = (dom)? document.getElementById(TargetID): document.all[TargetID];

	if (SourceField.value == SourceValue)
		{
		TargetField.disabled = TargetDisabled;
		}
	else
		{
		TargetField.disabled = !TargetDisabled;
		}
}







/*******************************************************************************
FILE: RegExpValidate.js

DESCRIPTION: This file contains a library of validation functions
  using javascript regular expressions.  Library also contains functions that re-
  format fields for display or for storage.
  
SOURCE: http://www.rgagnon.com/jsdetails/js-0063.html

  VALIDATION FUNCTIONS:
  
  validateEmail - checks format of email address
	validateUSPhone - checks format of US phone number
	validateNumeric - checks for valid numeric value
  validateInteger - checks for valid integer value
	validateNotEmpty - checks for blank form field
  validateUSZip - checks for valid US zip code
  validateUSDate - checks for valid date in US format
  validateValue - checks a string against supplied pattern
  
  FORMAT FUNCTIONS:
  
  rightTrim - removes trailing spaces from a string
  leftTrim - removes leading spaces from a string
  trimAll - removes leading and trailing spaces from a string
  removeCurrency - removes currency formatting characters (), $ 
  addCurrency - inserts currency formatting characters
  removeCommas - removes comma separators from a number
  addCommas - adds comma separators to a number
  removeCharacters - removes characters from a string that match passed pattern
  
  
AUTHOR: Karen Gayda

DATE: 03/24/2000
*******************************************************************************/

function validateEmail( strValue) {
/************************************************
DESCRIPTION: Validates that a string contains a 
  valid email pattern. 
  
 PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
   
REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
*************************************************/

/*
var objRegExp  = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
 
  //check for valid email
  return objRegExp.test(strValue);
*/

if ( (strValue == '') || (strValue.indexOf('@',0) == -1) || (strValue.indexOf('.',0) == -1) ) {
  return (false);
} else
  return (true);
}

function validateUSPhone( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains valid
  US phone pattern. 
  Ex. (999) 999-9999 or (999)999-9999
  
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
 
  //check for valid us phone with or without space between 
  //area code
  return objRegExp.test(strValue); 
}

function validateUKPhone( strValue ) {
/************************************************
DESCRIPTION: Validates UK telephone numbers.
	Formats taken from http://www.ofcom.org.uk/licensing_numbering/numbers/num_user_guide?a=87101#2

	Valide formats are:
	029 99999999 or 029 9999 9999
	0199 9999999 or 0199 999 9999
	01999 99999
	01999 999999
	019999 9999
	019999 99999
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.

*************************************************/
var objRegExp = /(^(02\d\s?\d{4}\s?\d{4})$|^(01\d{2}\s?\d{3}\s?\d{4})$|^(01\d{3}\s?\d{5,6})$|^(01\d{4}\s?\d{4,5})$)/;

  //check for valid UK telephone number
  return objRegExp.test(strValue);
}

function validateUKMobile( strValue ) {
/************************************************
DESCRIPTION: Validates UK mobile telephone numbers.
	Valide formats are `077', `078' or `079' followed by another 8 digits
	Formats taken from http://www.ofcom.org.uk/licensing_numbering/numbers/num_user_guide?a=87101#2
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.

*************************************************/
var objRegExp = /(^(077|078|079)\s?\d{2}\s?\d{6}$)/;

  //check for valid UK mobile telephone number
  return objRegExp.test(strValue);
}

function  validateNumeric( strValue ) {
/******************************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
 
  //check for numeric characters 
  return objRegExp.test(strValue);
}

function validateInteger( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only 
    valid integer number.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  var objRegExp  = /(^-?\d\d*$)/;
 
  //check for integer characters
  return objRegExp.test(strValue);
}

function validatePassword( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains at between
		6 and 10 alphanumeric characters.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  var objRegExp  = /(^(\w{6})(\w{1,4})?$)/;
 
  //check for integer characters
  return objRegExp.test(strValue);
}

function validatePositiveInteger( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only 
    valid positive integer number.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  var objRegExp  = /(^\d\d*$)/;
 
  //check for positive integer characters
  return objRegExp.test(strValue);
}

function validateNotEmpty( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }  
   return false;
}

function validateUSZip( strValue ) {
/************************************************
DESCRIPTION: Validates that a string a United
  States zip code in 5 digit format or zip+4
  format. 99999 or 99999-9999
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.

*************************************************/
var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
 
  //check for valid US Zipcode
  return objRegExp.test(strValue);
}

function validateUKPostcode( strValue ) {
/************************************************
DESCRIPTION: Validates that a string a United
  Kingdom postcode.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.

*************************************************/
var objRegExp = /(^([A-P]|[R-U]|W|[Y-Z])([A-H]|[K-Y])?(\d)?(\d)?([A-H]|[J-K]|[S-U]|W)?(A|B|E|H|M|N|P|R|[V-Y])?\s?(\d)([JLNA-BD-HP-UW-Z]){2}$)|(^GIR\s0AA$)/;

  //check for valid UK Postcode
  return objRegExp.test(strValue);
}

function validateUKVehiclePlate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string a United
  Kingdom vehicle registration plate of new or old style.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.

*************************************************/
var objRegExp = /(^([A-Z]{3}\s?(\d{3}|\d{2}|d{1})\s?[A-Z])|([A-Z]\s?(\d{3}|\d{2}|\d{1})\s?[A-Z]{3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3})$)/;

  //check for valid UK vehicle registration plate
  return objRegExp.test(strValue);
}

function validateRealName( strValue ) {
/************************************************
DESCRIPTION: Validates real names.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.

*************************************************/
var objRegExp = /(^[A-Za-zÀ-ÖØ-öø-ÿ '\-\.]{1,22}$)/;

  //check for valid real name
  return objRegExp.test(strValue);
}

function validateDate( strValue ) {
/************************************************
DESCRIPTION:
Description:      This expression validates dates in the ITALIAN d/m/y format from 1/1/1600 - 31/12/9999.
                                The days are validated for the given month and year. Leap years are validated for all 4 digits
                                years from 1600-9999, and all 2 digits years except 00 since it could be any century (1900, 2000, 2100).
                                Days and months must be 1 or 2 digits and may have leading zeros. Years must be 2 or 4 digit years.
                                4 digit years must be between 1600 and 9999. Date separator may be a slash (/), dash (-), or period (.)
                                Thanks to Michael Ash for US Version
Source:                    regexlib.com
                               
Matches:                 29/02/1972|||5-9-98|||10-11-2002
Non-Matches:         29/02/2003|||12/13/2002|||1-1-1500
   
PARAMETERS:
   strValue - String to be tested for validity
  
RETURNS:
   True if valid, otherwise false.
  
REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/

    var objRegExp = /^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/;

  //check for valid date
  return objRegExp.test(strValue);
}

function validateTime( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only 
    valid 24 hour times with 2 digit hour, 2 digit
	minutes.
	Date separator must be :
    Uses combination of regular expressions and 
    string parsing to validate time.
    Ex. hh:mm
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
   
*************************************************/
  var objRegExp  = /^([01]?[0-9]|[2][0-3])(:[0-5][0-9])?$/;
 
  //check for valid time
  return objRegExp.test(strValue);
}

function validateValue( strValue, strMatchPattern ) {
/************************************************
DESCRIPTION: Validates that a string a matches
  a valid regular expression value.
    
PARAMETERS:
   strValue - String to be tested for validity
   strMatchPattern - String containing a valid
      regular expression match pattern.
      
RETURNS:
   True if valid, otherwise false.
*************************************************/
var objRegExp = new RegExp( strMatchPattern);
 
 //check if string matches pattern
 return objRegExp.test(strValue);
}


function rightTrim( strValue ) {
/************************************************
DESCRIPTION: Trims trailing whitespace chars.
    
PARAMETERS:
   strValue - String to be trimmed.  
      
RETURNS:
   Source string with right whitespaces removed.
*************************************************/
var objRegExp = /^([\w\W]*)(\b\s*)$/;
 
      if(objRegExp.test(strValue)) {
       //remove trailing a whitespace characters
       strValue = strValue.replace(objRegExp, '$1');
    }
  return strValue;
}

function leftTrim( strValue ) {
/************************************************
DESCRIPTION: Trims leading whitespace chars.
    
PARAMETERS:
   strValue - String to be trimmed
   
RETURNS:
   Source string with left whitespaces removed.
*************************************************/
var objRegExp = /^(\s*)(\b[\w\W]*)$/;
 
      if(objRegExp.test(strValue)) {
       //remove leading a whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/ 
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
    
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function removeCurrency( strValue ) {
/************************************************
DESCRIPTION: Removes currency formatting from 
  source string.
  
PARAMETERS: 
  strValue - Source string from which currency formatting
     will be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /\(/;
  var strMinus = '';
 
  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }
  
  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

function addCurrency( strValue ) {
/************************************************
DESCRIPTION: Formats a number as currency.

PARAMETERS: 
  strValue - Source string to be formatted

REMARKS: Assumes number passed is a valid 
  numeric value in the rounded to 2 decimal 
  places.  If not, returns original value.
*************************************************/
  var objRegExp = /-?[0-9]+\.[0-9]{2}$/;
   
    if( objRegExp.test(strValue)) {
      objRegExp.compile('^-');
      strValue = addCommas(strValue);
      if (objRegExp.test(strValue)){
        strValue = '(' + strValue.replace(objRegExp,'') + ')';
      }
      return '$' + strValue;
    }
    else
      return strValue;
}

function removeCommas( strValue ) {
/************************************************
DESCRIPTION: Removes commas from source string.

PARAMETERS: 
  strValue - Source string from which commas will 
    be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /,/g; //search for commas globally
 
  //replace all matches with empty strings
  return strValue.replace(objRegExp,'');
}

function addCommas( strValue ) {
/************************************************
DESCRIPTION: Inserts commas into numeric string.

PARAMETERS: 
  strValue - source string containing commas.
  
RETURNS: String modified with comma grouping if
  source was all numeric, otherwise source is 
  returned.
  
REMARKS: Used with integers or numbers with
  2 or less decimal places.
*************************************************/
  var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})'); 

    //check for match to search criteria
    while(objRegExp.test(strValue)) {
       //replace original string with first group match, 
       //a comma, then second group match
       strValue = strValue.replace(objRegExp, '$1,$2');
    }
  return strValue;
}

function removeCharacters( strValue, strMatchPattern ) {
/************************************************
DESCRIPTION: Removes characters from a source string
  based upon matches of the supplied pattern.

PARAMETERS: 
  strValue - source string containing number.
  
RETURNS: String modified with characters
  matching search pattern removed
  
USAGE:  strNoSpaces = removeCharacters( ' sfdf  dfd', 
                                '\s*')
*************************************************/
 var objRegExp =  new RegExp( strMatchPattern, 'gi' );
 
 //replace passed pattern matches with blanks
  return strValue.replace(objRegExp,'');
}

function ChangeStyle(ElementID, ClassPassed){
	/************************************************
	DESCRIPTION: Replaces class style on an HTML element with another class.
	
	PARAMETERS: 
		ElementID - ID of element to change.
		ClassPassed - CSS style class.
		
	RETURNS: Nothing
		
	USAGE:  ChangeStyle(ElementID, ClassPassed);
	*************************************************/
	document.getElementById(ElementID).className=ClassPassed;
} 

function SetClasses(node, classname) {
	if (node.className != classname)
		node.className = classname;
}

function Highlight(node) {
	window.status='';
	SetClasses(node, 'HighlightRow');
}

function LowLightOdd(node) {
	window.status='';
	SetClasses(node, 'TableRowOdd');
}

function LowLightEven(node) {
	window.status='';
	SetClasses(node, 'TableRowEven');
}

function ErrorMessage(CellRef, Message){
/************************************************
DESCRIPTION: Displays an error message and highlights a form field,

PARAMETERS: 
  CellRef - ID of form field.
  Message - Error message to be displayed.
  
USAGE: return ErrorMessage('FieldName', 'Error message.');
*************************************************/
	document.getElementById(CellRef).focus();
	if ((document.getElementById(CellRef).type != 'select-one') && (document.getElementById(CellRef).type != 'select-multiple')) {
		document.getElementById(CellRef).select();
	}
	window.alert(Message);
	return false;
}

function WriteEmailAddress(partc,partb,parta,part2,part1) {
	it1 = "mail";
	it2 = "to";
	
	document.write('<a href=\"' + it1 + it2 + ':' + part1 + part2 + '@' + parta + partb + partc + '\">');
	document.write(part1 + part2 + '@' + parta + partb + partc + '</a>');
}
