
	var RULE_IS_REQUIRED		= "Required";
	var RULE_IS_INTEGER			= "Integer";
	var RULE_IS_DECIMAL			= "Decimal";
	var RULE_IS_DATE			= "Date";
	
	var RULE_LESS_THAN_EQUAL	= "LessThanEqual"; 
	var RULE_LESS_THAN			= "LessThan";
	var RULE_GREATER_THAN_EQUAL = "GreaterThanEqual"; 
	var RULE_GREATER_THAN		= "GreaterThan"; 
	var RULE_NOT_EQUAL			= "NotEqual";  
	var RULE_EQUAL				= "Equal";
	
	var RULE_DATE_GREATER_THAN_EQUAL = "DateGreaterThanEqual"; 
	var RULE_DATE_LESS_THAN_EQUAL	= "DateLessThanEqual"; 

	var RULE_RANGE				= "Range";
	var RULE_IS_CURRENCY		= "Currency";
	var RULE_MAX_LENGTH			= "MaxLength";
	var RULE_IS_EMAIL			= "Email";
	var RULE_REG_EXP			= "RegExp";

//-------------------------------------------------------------------
// getInputValue(input_object)
//   Get the value of any form input field
//   Multiple-select fields are returned as comma-separated values
//   (Doesn't support input types: button,file,password,reset,submit)
//-------------------------------------------------------------------

function getInputValue(obj) 
{
	if ((typeof obj.type != "string") && (obj.length > 0) && (obj[0] != null) && (obj[0].type=="radio")) {
		for (var i=0; i<obj.length; i++) {
			if (obj[i].checked == true) { return obj[i].value; }
			}
		return null;
		}
	if (obj.type=="text") 
		{ return obj.value; }
	if (obj.type=="hidden") 
		{ return obj.value; }
	if (obj.type=="textarea") 
		{ return obj.value; }
	if (obj.type=="checkbox") {
		if (obj.checked == true) {
			return obj.value;
			}
		return null;
		}
	if (obj.type=="select-one") { 
		if (obj.options.length > 0) {
			return obj.options[obj.selectedIndex].value; 
			}
		else {
			return null;
			}
		}
	if (obj.type=="select-multiple") { 
		var val = "";
		for (var i=0; i<obj.options.length; i++) {
			if (obj.options[i].selected) {
				val = val + "" + obj.options[i].value + ",";
				}
			}
		if (val.length > 0) {
			val = val.substring(0,val.length-1); // remove trailing comma
			}
		return val;
		}
}

//-------------------------------------------------------------------
// getInputDefaultValue(input_object)
// Generic fuction that will determine the element type (obj) and 
// determine its default value
//-------------------------------------------------------------------

function getInputDefaultValue(obj) 
{
	if ((typeof obj.type != "string") && (obj.length > 0) && (obj[0] != null) && (obj[0].type=="radio")) {
		for (var i=0; i < obj.length; i++) {
			if (obj[i].defaultChecked == true) { return obj[i].value; }
			}
		return null;
		}
	if (obj.type=="text") 
		{ return obj.defaultValue; }
	if (obj.type=="hidden") 
		{ return obj.defaultValue; }
	if (obj.type=="textarea") 
		{ return obj.defaultValue; }
	if (obj.type=="checkbox") {
		if (obj.defaultChecked == true) {
			return obj.value;
			}
		return null;
		}
	if (obj.type=="select-one") {
		if (obj.options.length > 0) {
			for (var i=0; i<obj.options.length; i++) {
				if (obj.options[i].defaultSelected) {
					return obj.options[i].value;
					}
				}
			}
		return null;
		}
	if (obj.type=="select-multiple") { 
		var val = "";
		for (var i=0; i<obj.options.length; i++) {
			if (obj.options[i].defaultSelected) {
				val = val + "" + obj.options[i].value + ",";
				}
			}
		if (val.length > 0) {
			val = val.substring(0,val.length-1); // remove trailing comma
			}
		if (isBlank(val)) { return null; }
		return val;
		}
}


//-------------------------------------------------------------------
// setInputValue()
// Generic fuction that will determine the element type (obj) and set it
// to the supplied value (val)
//-------------------------------------------------------------------
function setInputValue(obj,val) 
{
	if ((typeof obj.type != "string") && (obj.length > 0) && (obj[0] != null) && (obj[0].type=="radio")) {
		for (var i=0; i<obj.length; i++) {
			if (obj[i].value == val) { 
				obj[i].checked = true;
				}
			else {
				obj[i].checked = false;
				}
			}
		}
	if (obj.type=="text") 
		{ obj.value = val; }
	if (obj.type=="hidden") 
		{ obj.value = val; }
	if (obj.type=="textarea") 
		{ obj.value = val; }
	if (obj.type=="checkbox") {
		if (obj.value == val) { obj.checked = true; }
		else { obj.checked = false; }
		}
	if ((obj.type=="select-one") || (obj.type=="select-multiple")) {
		for (var i=0; i<obj.options.length; i++) {
			if (obj.options[i].value == val) {
				obj.options[i].selected = true;
				}
			else {
				obj.options[i].selected = false;
				}
			}
		}
}


//-------------------------------------------------------------------
// isNull(value)
//   Returns true if value is null
//-------------------------------------------------------------------
function isNull(val) {
	if (val == null) { return true; }
	return false;
	}
	
	
//***********************************************************
// This function checks to see if a passed string is either
// NULL or empty.  If it is, the function will return TRUE, 
// otherwise it will return FALSE.
//***********************************************************
function isEmpty(s)
{   
   return ((s == null) || (s.length == 0))
}

//-------------------------------------------------------------------
// isBlank(value)
//   Returns true if value only contains spaces
//-------------------------------------------------------------------
function isBlank(val) {
	if (val == null) { return true; }
	for (var i=0; i < val.length; i++) {
		if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t") && (val.charAt(i) != "\n")) { return false; }
		}
	return true;
	}

//-------------------------------------------------------------------
// isInteger(value)
//   Returns true if value contains all digits
//-------------------------------------------------------------------
function isInteger(val) {
	
	for (var i=0; i < val.length; i++) 
	{
		if (!isDigit(val.charAt(i))) 
			 return false; 
	}
	
	return true;
}


function isInteger2(val) {
	
	for (var i=0; i < val.length; i++) 
	{
		if (!isDigit(val.charAt(i))) 
			 return false; 
		else 	if (val.charAt(i) == '.')
			return false;
	}

	return true;
}
//-------------------------------------------------------------------
// isNumeric(value)
//   Returns true if value contains a positive float value
//-------------------------------------------------------------------
function isNumeric(val) {
	var dp = false;
	for (var i=0; i < val.length; i++) {
		if (!isDigit(val.charAt(i))) { 
			if (val.charAt(i) == '.') {
				if (dp == true) { return false; } // already saw a decimal point
				else { dp = true; }
				}
			else {
			    if (i==0)
			    { 
			      if (val.charAt(i) != '-') 
			      {
			        return false;
			      }
			    }
			    else
			    {
					return false;
			    }
			}
			}
		}
	return true;
	}
	
//-------------------------------------------------------------------
// isDigit(value)
//   Returns true if value is a 1-character digit
//-------------------------------------------------------------------
function isDigit(num) {
	var string="1234567890";
	if (string.indexOf(num) != -1) {
		return true;
		}
	return false;
	}


//-------------------------------------------------------------------
// CSGValidate(poObj)
//	 Validates poObj base on rules supplied via a "CSGValidate" custom attribute
//   (i.e. <INPUT id=LastName type=text size=12 name=IsRequired 
//				CSGValidate="Required" CSGLabel="Last Name">)
//-------------------------------------------------------------------

function CSGValidateForm(poObj)
{
	var sRule = new Array;

	// Determine if validating an entire form or single input
	//if (poObj.elements == "[object]")
	if (poObj.tagName.toUpperCase() == "FORM")
	{
		//alert(poObj.name);
		// Loop Thru form collection and process input tags with Validation Rules
		for(var i=0; i < poObj.elements.length; i++)
		{
		    //alert(poObj.elements.elements[i].name);
		    var csgValidate = poObj.elements[i].getAttribute("CSGValidate");

		    if (csgValidate != null)
			{
				//alert("hi 2");
			    sRule = csgValidate.split("!");
				for(i1=0; i1 < sRule.length; i1++)
				{
					if(!ProcessRule(Trim(sRule[i1]), poObj.elements[i]))
					{
						// Reformat Form
						this.CSGFormatElement(poObj);	
						this.SetFocus(poObj.elements[i]);				
						return false;
					}
				}			
			}		
		}		
	}
	else // Single Input Object
	{
	    var csgValidate = poObj.getAttribute("CSGValidate");
		if(csgValidate != null)
		{
		    sRule = csgValidate.split("!");
			for(var i1=0; i1 < sRule.length; i1++)
			{
				if(!ProcessRule(Trim(sRule[i1]), poObj))
				{
					this.CSGFormatElement(poObj);
					this.SetFocus(poObj);	
					return false;
				}
				
			}	
		}	
	}
    return true;
}

//-------------------------------------------------------------------
// Parses rule parametes and calls the appropriate validation function
//-------------------------------------------------------------------
function ProcessRule(psRule, poElement)
{
	var sRule;
	var sParams;
	
	// Check if Rule has parameters by 
	// checking for the existance of a "("
	var iHasParams = 0;	
	iHasParams = parseInt(psRule.indexOf("("));
	if(iHasParams > 0)
	{
		sRule   = psRule.substr(0, iHasParams);
		sParams = psRule.substring(iHasParams + 1, psRule.indexOf(")"));
	}
	else 
	{
		sRule = psRule;
	}

		
	switch (sRule)
	{
		case RULE_IS_REQUIRED:
			return IsRequiredRule(poElement);
			break; 
			
		case RULE_IS_INTEGER:
			return IsIntegerRule2(poElement);
			break; 
			
		case RULE_IS_DECIMAL:
			return IsDecimalRule(poElement);
			break; 

		case RULE_LESS_THAN_EQUAL:
		case RULE_LESS_THAN:
		case RULE_GREATER_THAN_EQUAL:
		case RULE_DATE_GREATER_THAN_EQUAL:
		case RULE_DATE_LESS_THAN_EQUAL:
		case RULE_GREATER_THAN:
		case RULE_NOT_EQUAL:
		case RULE_EQUAL:
			// DEBUG alert("CompareRule(poElement,'" + sRule  + "', " + sParams + ")");
			return eval("CompareRule(poElement,'" + sRule  + "', " + sParams + ")");
			break; 				

		case RULE_IS_CURRENCY:		
			return IsCurrencyRule(poElement);
			break; 
		case RULE_MAX_LENGTH:
			return eval("MaxLengthRule(poElement," + sParams + ")");
			break; 	 
			
		case RULE_RANGE:
			return eval("RangeRule(poElement," + sParams + ")");
			break; 	
			
		case RULE_IS_EMAIL:		
			return EmailRule(poElement);
			break; 	

		case RULE_IS_DATE:		
			return DateRule(poElement);
			break; 	
			
		case RULE_REG_EXP:
			return eval("RegExpRule(poElement," + sParams + ")");
			break; 	
			
		default:
			break; 
	}
	
	return true;

}

//-------------------------------------------------------------------
// Check for all blank or all whitespace condition
//-------------------------------------------------------------------
function IsRequiredRule(poElement)
{
	var val;
	val = this.getInputValue(poElement);
	val = this.Trim(val);

	if (isBlank(val)) 
	{
		DisplayErrorMessage(poElement, "@LABEL is required!")	
		return false;
	}
	else
	{
		return true;
	}
}


//-------------------------------------------------------------------
// Allows only integers to be inputed
//-------------------------------------------------------------------
function IsIntegerRule2(poElement, psPreferLabel)
{
	//ATT 05072004 Add optional psPreferLabel to display readable field name
	var val;
	val = this.getInputValue(poElement);
	val = this.Trim(val);
	
	if(isBlank(val)) return true; 
	
	if (!isInteger2(val)) 
	{
		if (psPreferLabel != null) 
			DisplayErrorMessage(poElement, psPreferLabel + " is not a valid integer value!");
		else
			DisplayErrorMessage(poElement, "Only Integers are allowed in the @LABEL field!");
		return false;
	}
	else
	{
		this.setInputValue(poElement, val)	;
		return true;
	}
}


//-------------------------------------------------------------------
// Used to support the is Decimal Function
//-------------------------------------------------------------------
function NumDecimalRule(poElement, piMaxNumDecimals)
{
	var val;
	val = this.getInputValue(poElement);
	val = this.Trim(val);
	val = this.stripCharString(val, ","); 
	
	if (val.indexOf('.') == -1) 
	{
		val += ".";
	}
	
	sDecText = val.substring(val.indexOf('.')+1, val.length);
	
	if (sDecText.length > piMaxNumDecimals)
	{
		return false;
	}
	else
	{
		setInputValue(poElement, val)	
		return true;
	}
}


//-------------------------------------------------------------------
// Numeric values with 2 decimal places.
//-------------------------------------------------------------------
function IsCurrencyRule(poElement, psPreferLabel)
{ 
	//ATT 03242004 Add optional psPreferLabel to display readable field name
	var val;
	val = this.getInputValue(poElement);
	val = this.Trim(val);
	val = this.stripCharString(val, ",$"); 
	
	if(isBlank(val)) return true; 
	
	if (!isNumeric(val) || !NumDecimalRule(poElement, 2)) 
	{
		if (psPreferLabel != null) 
			DisplayErrorMessage(poElement, psPreferLabel + " is not a valid currency value!")	;
		else
			DisplayErrorMessage(poElement, "@LABEL is not a valid currency value!")	;
		return false;
	}
	else
	{
		setInputValue(poElement, val)	
		return true;
	}
}


//-------------------------------------------------------------------
// Checks for numeric values
//-------------------------------------------------------------------
function IsDecimalRule(poElement, psPreferLabel)
{
	//ATT 03242004 Add optional psPreferLabel to display readable field name
	var val;
	val = this.getInputValue(poElement);
	val = this.Trim(val);
	val = this.stripCharString(val, ","); 
	
	if (!isNumeric(val)) 
	{
		if (psPreferLabel != null) 
			DisplayErrorMessage(poElement,  "Only Decimal values are allowed in " + psPreferLabel);
		else	
			DisplayErrorMessage(poElement, "Only Decimal values are allowed in @LABEL Field!")	
		
		return false;
	}
	else
	{
		setInputValue(poElement, val)	
		return true;
	}
}

//-------------------------------------------------------------------
// Determines if values lies within the provided range
//-------------------------------------------------------------------
function RangeRule(poElement, pMinVal, pMaxVal, psPreferLabel)
{
	//ATT 03242004 Add optional psPreferLabel to display readable field name
	var val;
	val = this.getInputValue(poElement);	
	
	if(isBlank(val)) return true;
	
	val = this.Trim(val);
	val = this.stripCharString(val, ","); 
	  
	if (!(val >= pMinVal) || !(val <= pMaxVal)) 
	{
		if (psPreferLabel != null) 
			DisplayErrorMessage(poElement, psPreferLabel + " must be between " + pMinVal + " and " + pMaxVal)	
		else
			DisplayErrorMessage(poElement, "@LABEL must be between " + pMinVal + " and " + pMaxVal)	
		return false;
	}
	else
	{
		setInputValue(poElement, val)	
		return true;
	}
}

//-------------------------------------------------------------------
//CompareRule(poElement, pCompareVal, pOperator[,poElement2])
//  can be used to compare against a supplied value or the value of 
// another field
//-------------------------------------------------------------------
function CompareRule(poElement, pOperator, pCompareVal)
{
	var val;
	var val2;
	var sMsgCompareValue 
	
	val = this.getInputValue(poElement);
		

	if(isBlank(val)) return true;

	//Check to see if it is of Date type
	if (pOperator.indexOf("Date") != -1)
	{

		val = new Date(val);
		val2= new Date(pCompareVal);

		sMsgCompareValue = pCompareVal;
	}
	else
	{

		val = this.Trim(val);
		val = parseFloat(this.stripCharString(val, ",")); 
	
		sMsgCompareValue = pCompareVal;
		val2 = pCompareVal;
	}	
	
				
	// Check if the ID of a second element has been entered
	// if yes then compare its value.
	if (arguments.length > 3) 
	{ 

		poElement2 = document.getElementById(arguments[3]); 

		val2 = this.getInputValue(poElement2);
		if(isBlank(val2) || val2 == null) return true;
		
		if (pOperator.indexOf("Date") != -1)
		{		
			val2= new Date(val2);
		}
		else
		{
			val2 = this.Trim(val2);
			val2 = parseFloat(this.stripCharString(val2, ",")); 
		}

		var csgLabel = poElement2.getAttribute("CSGLabel");
		if (!isBlank(csgLabel))
		{
		    sMsgCompareValue = csgLabel;
		}
		else
		{
			sMsgCompareValue = poElement2.name;
		}			
	}	
		
	// Make the appropriate test.
	switch (pOperator) {
        case RULE_NOT_EQUAL:
			if((val == val2))
			{
				DisplayErrorMessage(poElement, "The value for @LABEL cannot equal " + sMsgCompareValue);	
				return false;
            }
            break;
        case RULE_GREATER_THAN:
			if(!(val > val2))
			{
				DisplayErrorMessage(poElement, "The value for @LABEL is not greater than " + sMsgCompareValue);	
				return false;
            }
            break;
        case RULE_GREATER_THAN_EQUAL:        
			if(!(val >= val2))
			{
				DisplayErrorMessage(poElement, "The value for @LABEL is not greater than or equal to " + sMsgCompareValue);	
				return false;
            }
            break;
        case RULE_LESS_THAN:        
			if(!(val < val2))
			{
				DisplayErrorMessage(poElement, "The value for @LABEL is not less than " + sMsgCompareValue);
				return false;
            }
            break;
        case RULE_LESS_THAN_EQUAL:         
			if(!(val <= val2))
			{
				DisplayErrorMessage(poElement, "The value for @LABEL is not less than or equal to " + sMsgCompareValue);	
				return false;
            }
            break;
        case RULE_EQUAL:         
			if((val != val2))
			{
				DisplayErrorMessage(poElement, "The value for @LABEL is not equal to " + sMsgCompareValue);	
				return false;
            }
            break;
        case RULE_DATE_LESS_THAN_EQUAL:         
			if(!(val <= val2))
			{
				DisplayErrorMessage(poElement, "The value for @LABEL is not less than or equal to " + sMsgCompareValue);	
				return false;
            }
            break;
        case RULE_DATE_GREATER_THAN_EQUAL:         
			if(!(val >= val2))
			{
				DisplayErrorMessage(poElement, "The value for @LABEL is not less than or equal to " + sMsgCompareValue);	
				return false;
            }
            
            break;            
        default:
            break;            
    }
    
    return true;
}

//-------------------------------------------------------------------
// Checks the length of a given field
//-------------------------------------------------------------------
function MaxLengthRule(poElement, pMax)
{
	var val;
	val = this.getInputValue(poElement);
	val = this.Trim(val);

	if (val.length > pMax) 
	{
		DisplayErrorMessage(poElement, "@LABEL cannot be greater than "+ pMax +" characters.");	
		return false;
	}
	else
	{
		return true;
	}

}

//-------------------------------------------------------------------
// Tests for valid email address.
//-------------------------------------------------------------------
function EmailRule(poElement) {
	var val;
	val = this.getInputValue(poElement);
	val = this.Trim(val);
	
	if(isBlank(val)) return true; 
	
    var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
    var regex = new RegExp(emailReg);
    if(!regex.test(val))
    {
		DisplayErrorMessage(poElement, "@LABEL is not a valid email address.");
		return false;
    }
    
    return true;
}

//-------------------------------------------------------------------
// Applies the supplied Reg Exp
//-------------------------------------------------------------------
function RegExpRule(poElement, psRegExp) {
	var val;
	val = this.getInputValue(poElement);
	val = this.Trim(val);
	
	if(isBlank(val)) return true; 
	
    var regex = new RegExp(psRegExp);
    if(!regex.test(val))
    {
		DisplayErrorMessage(poElement, "@LABEL is not a valid pattern.");
		return false;
    }
    
    return true;
}


//-------------------------------------------------------------------
// Tests for valid date.
//-------------------------------------------------------------------
function DateRule(poElement, psPreferLabel)
{
	//ATT 04152004 Add optional psPreferLabel to display readable field name
  d1 = this.getInputValue(poElement);
  d1 = this.Trim(d1);

  if(isBlank(d1)) return true; 
  
  //get the year.

  var re =/^(1[0-2]|0?[1-9])[\-\/](0?[1-9]|[12][0-9]|3[01])[\-\/]((19|20)\d{2})/;
  var matches = re.exec( d1 );
  if ( matches )
  {
	var mm = parseInt(matches[1],10);
    var dd = parseInt(matches[2],10);
    var yy = parseInt(matches[3],10);
	var yyyy;
	
    var d2 = new Date( d1 );
    	    	
    if ( mm == d2.getMonth()+1 && dd == d2.getDate())
    {
		var rex =/^(1[0-2]|0?[1-9])[\-\/](0?[1-9]|[12][0-9]|3[01])[\-\/]((19|20)\d{3})/;
		var matches = rex.exec( d1 );
			if ( matches )
			{
				yyyy = parseInt(matches[3],10);
			}
			if ( yyyy > 2099)
			{
				if (psPreferLabel != null) 
					DisplayErrorMessage(poElement,  psPreferLabel +  " must be in the 'MM/DD/YYYY' format and between the years of 1900 - 2099");
				else
					DisplayErrorMessage(poElement,"@LABEL must be in the 'MM/DD/YYYY' format.");

				return false;
			}
			else
				return true;
    }
  }
	if (psPreferLabel != null) 
		DisplayErrorMessage(poElement,  psPreferLabel +  " must be in the 'MM/DD/YYYY' format and between the years of 1900 - 2099");
	else
		DisplayErrorMessage(poElement,"@LABEL must be in the 'MM/DD/YYYY' format.");

   return false;
}


//-------------------------------------------------------------------
// DisplayErrorMessage(input_object,[,Message[,true]]))
//   Optionally displays Costar.SharedMasterPage.UI.Message and sets focus
//-------------------------------------------------------------------
function DisplayErrorMessage(poElement)
{	
	var sMsg;
	var pMsg;
	var pbFocus;
	var regexp = /@LABEL/gi;

	if (arguments.length > 1) { pMsg    = arguments[1]; } else { pMsg="";}
	if (arguments.length > 2) { pbFocus = arguments[2]; } else { pbFocus = true;}

	// Check for custom error Costar.SharedMasterPage.UI.Message and if a custom label for UI is available then
	// add it to the Costar.SharedMasterPage.UI.Message

	var csgMessage = poElement.getAttribute("CSGMessage");
	var csgLabel = poElement.getAttribute("CSGLabel");

	if (!isBlank(csgMessage)) {
	    
	    if (!isBlank(csgLabel))
		{
		    sMsg = csgMessage.replace(regexp, csgLabel);
		}
		else 
		{
		    sMsg = csgMessage.replace(regexp, poElement.name);
		}
	}
	else // Use Parameter Costar.SharedMasterPage.UI.Message
	{
		if(!isBlank(pMsg))
		{
		    if (!isBlank(csgLabel))
			{
			    sMsg = pMsg.replace(regexp, csgLabel);
			}
			else 
			{
				sMsg = pMsg.replace(regexp, poElement.name);
			}
		}
		else  // Generic Error Costar.SharedMasterPage.UI.Message
		{
			sMsg = "Error: Invalid Form Input.";
		}		
	}
	
	alert(sMsg);
	
	if (pbFocus) 
	{
		// DEBUG: alert(poElement.type.toString().toUpperCase());
		if(poElement.type.toString().toUpperCase() == "TEXT")
		{
			this.PreFocusEvent(poElement);
			poElement.focus();
			poElement.select();
		}
		
		if(poElement.type.toString().toUpperCase() == "SELECT-ONE")
		{
			this.PreFocusEvent(poElement);
			poElement.focus();
		}
	}
}

function PreFocusEvent(poElement) {
    var csgFocusEvent = poElement.getAttribute("CSGFocusEvent");
    if (!csgFocusEvent)
	{
	    eval(csgFocusEvent);
	}
}

function SetFocus(poElement)
{
	// DEBUG: alert(poElement.type.toString().toUpperCase());
	if(poElement.type.toString().toUpperCase() == "TEXT")
	{
		this.PreFocusEvent(poElement);
		poElement.focus();
		poElement.select();
	}
	
	if(poElement.type.toString().toUpperCase() == "SELECT-ONE")
	{
		this.PreFocusEvent(poElement);
		poElement.focus();
	}
}


//-------------------------------------------------------------------
// Format Forms 
//
//-------------------------------------------------------------------

var FORMAT_ADD_COMMAS		 = "AddCommas";
var FORMAT_REMOVE_CHAR		 = "RemoveChar";
var FORMAT_TRIM				 = "Trim";
var FORMAT_REMOVE_LEAD_ZEROS = "RemoveLeadZeros"

//-------------------------------------------------------------------
// CSGFormat(poObj)
//	 Validates poObj base on rules supplied via a "CSGValidate" custom attribute
//   (i.e. <INPUT id=LastName type=text size=12 name=IsRequired onchange="CSGFormatElement(this)" 
//				CSGFormat="Trim!AddCommas" CSGLabel="Last Name">)
//-------------------------------------------------------------------

function CSGFormatElement(poObj)
{
	var sRule = new Array;

	// Determine if validating an entire form or single input
	if (poObj.tagName.toUpperCase() == "FORM")
	{
		// Loop Thru form collection and process input tags with Formatting Rules
		for(var i=0; i < poObj.elements.length; i++) {
		    var csgFormat = poObj.elements[i].getAttribute("CSGFormat");

		    if (csgFormat != null)
			{
			    sRule = csgFormat.split("!");
				for(var i1=0; i1 < sRule.length; i1++)
				{
					this.ProcessFormat(Trim(sRule[i1]), poObj.elements[i])
				}			
			}		
		}		
	}
	else // Single Input Object
	{
	    var format = poObj.getAttribute("CSGFormat");
	    if (format != null) {
	        sRule = format.split("!");
			for(var i1=0; i1 < sRule.length; i1++)
			{
				this.ProcessFormat(Trim(sRule[i1]), poObj)
			}	
		}	
	}
}

//-------------------------------------------------------------------
// Parses format parametes and calls the appropriate formatting function
//-------------------------------------------------------------------
function ProcessFormat(psRule, poElement)
{
	var sRule;
	var sParams;
	
	// Check if Rule has parameters by 
	// checking for the existance of a "("
	var iHasParams = 0;	
	iHasParams = parseInt(psRule.indexOf("("));
	if(iHasParams > 0)
	{
		sRule   = psRule.substr(0, iHasParams);
		sParams = psRule.substring(iHasParams + 1, psRule.indexOf(")"));
	}
	else 
	{
		sRule = psRule;
	}
		
	switch (sRule)
	{	
		case FORMAT_ADD_COMMAS:
			this.CommaSplitFormat(poElement);
			break;					
		case FORMAT_REMOVE_CHAR:
			this.eval("RemoveCharFormat(poElement," + sParams + ")");
			break;			
		case FORMAT_TRIM:
			this.TrimFormat(poElement);
			break;					
		default:
			break; 
	}	
}

//-------------------------------------------------------------------
// Adds commas to large numeric values (1000000.50 becomes 1,000,000.50
//-------------------------------------------------------------------
function CommaSplitFormat(poElement)
{	
	var val;
	val = this.getInputValue(poElement);
	val = this.stripCharString(val, ","); 
	
	if(isBlank(val)) return;
		
	val = commaSplit(val)	
	this.setInputValue(poElement, val)	
}

//-------------------------------------------------------------------
// Strips out any characters contained in psChar
//-------------------------------------------------------------------
function RemoveCharFormat(poElement, psChar)
{	
	var val;
	val = this.getInputValue(poElement);	
		
	if(isBlank(val)) return;
	
	val = stripCharString(val, psChar)	
	this.setInputValue(poElement, val)	
}

//-------------------------------------------------------------------
// Strips out any preceedin or trailing whitespace
//-------------------------------------------------------------------
function TrimFormat(poElement)
{	
	var val;
	val = this.getInputValue(poElement);
	
	if(isBlank(val)) return;
		
	val = Trim(val)	
	this.setInputValue(poElement, val)	
}

//-------------------------------------------------------------------
// Trim functions
//   Returns string with whitespace trimmed
//-------------------------------------------------------------------
function LTrim(str) {
	for (var i=0; str.charAt(i)==" "; i++);
	return str.substring(i,str.length);
	}
function RTrim(str) {
	for (var i=str.length-1; str.charAt(i)==" "; i--);
	return str.substring(0,i+1);
	}
function Trim(str) {
	return LTrim(RTrim(str));
	}

//-------------------------------------------------------------------
// stripCharString(InString, CharString) 
//	 Removes CharString from InString
//   Sample usage is the removal of commas from numeric input fields
//-------------------------------------------------------------------
function stripCharString(InString, CharString)  {
        var OutString="";
   for (var Count=0; Count < InString.length; Count++)  {
        var TempChar=InString.substring (Count, Count+1);
      var Strip = false;
      for (var Countx = 0; Countx < CharString.length; Countx++) {
        var StripThis = CharString.substring(Countx, Countx+1)
         if (TempChar == StripThis) {
                Strip = true;
            break; 
         }
      }
      if (!Strip)
        OutString=OutString+TempChar;
   }
        return (OutString);
}

//-------------------------------------------------------------------
//commaSplitFormat(srcNumber) 
//  Add commas to large numeric values (i.e. 1000000.01 ==> 1,000,000.01
//-------------------------------------------------------------------
function commaSplit(srcNumber) 
{
	var txtNumber = '' + srcNumber;
	
	if (isBlank(srcNumber)) return;

	var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
	var arrNumber = txtNumber.split('.');
	arrNumber[0] += '.';
	do {
		arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
	} while (rxSplit.test(arrNumber[0]));
	
	if (arrNumber.length > 1) {
		return arrNumber.join('');
	}
	else 
	{
		return arrNumber[0].split('.')[0];
	}
}



