//******************************************//
//**    Javascript Business Objects       **//
//**      (C) Copyright 2000-2002         **//
//**           By Level 2 Labs            **//
//**          All Rights Reserved         **//
//** Copying this web page, its content,  **//
//** code, images, or any other item      **//
//** in it without the expressed written  **//
//** permission from Level 2 Labs is      **//
//** strictly prohibited.                 **//
//******************************************//

//******************************************************************************************//
//** Supporting functions section                                                         **//
//******************************************************************************************//

/******************************************************************/
/* function: 	checkLength()                                     */
/* parameters:	elementPath (required) (path to the form element) */
/*              minimun (required) (can not hold less than this # */
/*              maximum (required) (can not hold more than this # */
/* returns: 	error-minimum, error-maximum, true                */
/******************************************************************/
function checkLength(elementPath, minimum, maximum) {
	// Create pointer to field element
	var fieldValue = eval("window.document." + elementPath);
	
	// Check for minimum requirement
	if (fieldValue.value.length < minimum) {
		return "error-minimum";
	}
	
	// Check for maximum requirement
	if (fieldValue.value.length > maximum) {
		return "error-maximum";
	}
	
	// Length checks ok
	return true;
}


/*****************************************************************************/
/* function: 	isEmpty()                                                    */
/* parameters:	elementPath (required) (path to the form element)            */
/*              errorMessage (required) (Actual message to display or "none" */
/* returns: 	true or false                                                */
/*****************************************************************************/
function isEmpty(elementPath, errorMessage) {

	// Create pointer to field element
	var fieldValue = eval("window.document." + elementPath);
	
	// Check for null
	if (fieldValue.value == "") {
		if (errorMessage != "none") {
			alert(errorMessage);
		}
		return true;
	} else {
		return false;
	}
}



//******************************************************************************************//
//** addDecimals(InputVal)                                                                **//
//** Arguments:                                                                           **//
//**        InputVal - The value to add decimals to                                       **//
//** Returns: 0 if value is not a number                                                  **//
//**          formatted value                                                             **//
//******************************************************************************************//
function addDecimals(InputVal) {
    temp = InputVal.indexOf(".");
    if (temp == -1) {
        newString = InputVal + ".00";
    } else {
        if(InputVal.substring(temp+1,temp+2) == "") {
            InputVal += "00";
        }
        
        if (InputVal.substring(temp+2,temp+3) == "") {
            InputVal += "0";
        }
        
        newString = InputVal.substring(0,temp+3);
    }

    return newString;
}

//******************************************************************************************//
//** addCommas(InputVal)                                                                  **//
//** Arguments:                                                                           **//
//**        InputVal - The value to add commas to                                         **//
//** Returns: 0 if value is not a number                                                  **//
//**          formatted value                                                             **//
//******************************************************************************************//
function addCommas(InputVal) {
    temp1 = InputVal.indexOf(".");
    if (temp1 == -1) {
        InputVal1 = InputVal;
        decimal = 0;
    } else {
        InputVal1 = InputVal.substring(0, temp1);
        decimal = InputVal.substring(temp1, InputVal.toString().length)
    }
    
    temp = InputVal1.toString().length;
    temp_string = "";
    for (var i = 0; i < temp; i++) {
        temp_string += InputVal1.toString().substring(temp-i-1, temp-i);
        if ((i+1) % 3 == 0 && temp > 3 && i+1 != temp) {
            temp_string += ",";
        }
    }

    new_string = "";
    for (var i = 0; i < temp_string.length; i++) {
        new_string += temp_string.substring(temp_string.length-i-1, temp_string.length-i);
    }
    if (decimal) {
        new_string += decimal;
    }
    
    return new_string;
}

//******************************************************************************************//
//** cleanInput(InputVal, TrimVal)                                                        **//
//** Arguments:                                                                           **//
//**        InputVal - The value to be cleaned of the TrimVal Character                   **//
//**        TrimVal - Value to be removed from the string                                 **//
//** Returns: 0 if value is not a number                                                  **//
//**          1 if value is a number                                                      **//
//******************************************************************************************//
function cleanInput (InputVal, TrimVal){
    var outputVal='';
    for ( var i=0; i< InputVal.length; i++){
       var ch = InputVal.charAt(i);
       if ( ch != TrimVal ){
          outputVal=outputVal + ch;
       }
    }
    return(outputVal);
}

//******************************************************************************************//
//** trimField(Value)                                                                     **//
//** Arguments:                                                                           **//
//**        Value - The value to be checked as number                                     **//
//** Returns: 0 if value is not a number                                                  **//
//**          1 if value is a number                                                      **//
//******************************************************************************************//
function trimField (Value){
    //** Trim spaces from the start **//
    var foundStart = false;
    var ch;
    if ( Value.length == 0){
       return(Value);
    }
    for (var i=0; i<Value.length; i++ ){
        ch = Value.charAt(i);
        if ( ch != " " ){
            Value = Value.substring (i);
            foundStart=true;
			break;
		}
    }
    if ( foundStart ){
		//** Trim spaces from the end **//
		for (i=Value.length-1; i>=0; i--){
			ch = Value.charAt(i);
		    if ( ch != " " ){
				Value = Value.substring (0,i+1);
				break;
			}
		}
    }else{
	   Value='';
	}

    return ( Value );
    
}

//******************************************************************************************//
//** checkNumber(Value)                                                                   **//
//** Arguments:                                                                           **//
//**        Value - The value to be checked as number                                     **//
//** Returns: 0 if value is not a number                                                  **//
//**          1 if value is a number                                                      **//
//******************************************************************************************//
function checkNumber (Value){

    var found_period = false;
    var found_dash = false;
    Value = trimField (Value);
    if (Value.length == 0){
       return (0);
    }
    
    for ( var i=0; i< Value.length; i++){
        var ch = Value.charAt(i);
        if (ch == ".") {
            if (found_period) {
                return(0);
            }
            found_period = true;
        }
        
        if (ch == "-") {
            if (found_dash) {
                return(0);
            }
            found_dash = true;
        }
                
        if ( (ch < "0" || ch > "9") && ch != "." && ch != "-"){
            return(0);
        }
    }
    return(1);
}

//******************************************************************************************//
//** checkFormat(Value, FormatString)                                                     **//
//** Arguments:                                                                           **//
//**        Value - The value to be formatted according to format string                  **//
//**        FormatStr - The format string in the form xxx-xxx-xxx                         **//
//** Returns: 0 if value cannot be formatted                                              **//
//**          formatted value                                                             **//
//******************************************************************************************//
function checkFormat (Value, FormatString){

    var xString='';
    var xValue='';
    var returnValue='';
    Value = trimField (Value);
    FormatString = trimField (FormatString);
    
    if (Value.length == 0 || FormatString.length == 0){
       return (0);
    }
    
    if ( FormatString == "NONE" ){
       return ( Value );
    }
    
    for ( i=0; i<FormatString.length; i++){
		if ( FormatString.charAt(i) == "x" || FormatString.charAt(i) == "X"){
			xString = xString + FormatString.charAt(i);
		}
	}
	
	for ( i=0; i< Value.length; i++){
		if ( Value.charAt(i) >= "0" && Value.charAt(i) <= "9"){
			xValue = xValue + Value.charAt(i);
		}
	}
	
    if ( xValue.length != xString.length ){
       return (0);
    }else{
        var valueCounter=0;
		for ( var i=0; i< FormatString.length; i++){
		   if ( FormatString.charAt(i) == "x" || FormatString.charAt(i) == "X"){
		      returnValue = returnValue + Value.charAt(valueCounter);
		      valueCounter++;
		   }else{
		      returnValue = returnValue + FormatString.charAt(i);
		   }
		}
	}
    return(returnValue);
}


//******************************************************************************************//
//** checkAlpha(Value)                                                                    **//
//** Arguments:                                                                           **//
//******************************************************************************************//
//** checkAlpha(Value)                                                                    **//
//** Arguments:                                                                           **//
//**        Value - The value to be checked as number                                     **//
//** Returns: 0 if value is not a number                                                  **//
//**          1 if value is a number                                                      **//
//******************************************************************************************//
function checkAlpha (Value){
    //alert ("alpha value is:" + Value + " of length: " + Value.length);
    for ( var i=0; i< Value.length; i++){
       var ch = Value.charAt(i);
       if ( (ch >= "0" && ch <= "9") || ch < " " || ch > "~" ){
          return (0);
       }
    }
    return(1);
}

//******************************************************************************************//
//** checkAlphanum(Value)                                                                 **//
//** Arguments:                                                                           **//
//**        Value - The value to be checked as number                                     **//
//** Returns: 0 if value is not a number                                                  **//
//**          1 if value is a number                                                      **//
//******************************************************************************************//
function checkAlphanum (Value){
    for ( var i=0; i< Value.length; i++){
       var ch = Value.charAt(i);
       if ( ch < " " || ch > "~" ){
          return(0);
       }
    }
    return(1);
}

//******************************************************************************************//
//** checkSpaces(Value)                                                                 **//
//** Arguments:                                                                           **//
//**        Value - The value to be checked as number                                     **//
//** Returns: 0 if value contains spaces                                                  **//
//**          1 if value doesn't contain spaces                                                      **//
//******************************************************************************************//
function checkSpaces (Value){
    for ( var i=0; i< Value.length; i++){
       var ch = Value.charAt(i);
       if ( ch == " " ){
          return(0);
       }
    }
    return(1);
}

