// JavaScript Document

function FormatCurrency(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.										
 
	RETVAL:
		The formatted number!		
 **********************************************************************/
{
	var tmpStr = new String(FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas));

	if (tmpStr.indexOf("(") != -1 || tmpStr.indexOf("-") != -1) {
		// We know we have a negative number, so place '$' inside of '(' / after '-'
		if (tmpStr.charAt(0) == "(")
			tmpStr = "($"  + tmpStr.substring(1,tmpStr.length);
		else if (tmpStr.charAt(0) == "-")
			tmpStr = "-$" + tmpStr.substring(1,tmpStr.length);
			
		return tmpStr;
	}
	else
		return "$" + tmpStr;		// Return formatted string!
}




function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}





    function Left(str, n)
    /***
            IN: str - the string we are LEFTing
                n - the number of characters we want to return

            RETVAL: n characters from the left side of the string
    ***/
    {
            if (n <= 0)     // Invalid bound, return blank string
                    return "";
            else if (n > String(str).length)   // Invalid bound, return
                    return str;                // entire string
            else // Valid bound, return appropriate substring
                    return String(str).substring(0,n);
    }


    function Right(str, n)
    /***
            IN: str - the string we are RIGHTing
                n - the number of characters we want to return

            RETVAL: n characters from the right side of the string
    ***/
    {
            if (n <= 0)     // Invalid bound, return blank string
               return "";
            else if (n > String(str).length)   // Invalid bound, return
               return str;                     // entire string
            else { // Valid bound, return appropriate substring
               var iLen = String(str).length;
               return String(str).substring(iLen, iLen - n);
            }
    }


    function Mid(str, start, len)
    /***
            IN: str - the string we are LEFTing
                start - our string's starting position (0 based!!)
                len - how many characters from start we want to get

            RETVAL: The substring from start to start+len
    ***/
    {
            // Make sure start and len are within proper bounds
            if (start < 0 || len < 0) return "";

            var iEnd, iLen = String(str).length;
            if (start + len > iLen)
                    iEnd = iLen;
            else
                    iEnd = start + len;

            return String(str).substring(start,iEnd);
    }



function InStr(strSearch, charSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is not
                           found, -1 is returned.)
                           
Requires use of:
	Mid function
	Len function
*/
{


	for (i=0; i < Len(strSearch); i++)
	{
	    if (UCase(Trim(charSearchFor)) == UCase(Mid(Trim(strSearch), i, Len(Trim(strSearch)))))
	    {
			return i;
	    }
	}
	return -1;
}

function Len(str)
/***
        IN: str - the string whose length we are interested in

        RETVAL: The number of characters in the string
***/
{  return String(str).length;  }

function option_numbers(){
	var ptr = 1;
	var ch;
	var i = 0;
	//on error resume next
	sSTR = top.frames["fraDetails"].document.getElementById('txtOptions').value;
	//on error goto 0
	if(sSTR.length == 0)
	{
		return;
	}
	if (Right(sSTR,1) != ' ')
	{
		sSTR = sSTR + ' ';
	}
	if(sSTR == ' ')
	{
		sSTR = '';
	} 
	
	while(sSTR.length>0)
	{
		ch = Left(sSTR,1);
		if(ch==' ')
		{
			i = i + 1;
		}
		sSTR = Right(sSTR, sSTR.length - 1);
	}
	
	//alert(i);	
	top.frames["fraImages"].document.getElementById('opt_href').innerHTML = "Options(" + i + ")";
}

function LCase(Value) {
  return Value.toString().toLowerCase();
}

function UCase(Value) {
  return Value.toString().toUpperCase();
}

        function Trim(str)
        /***
                PURPOSE: Remove trailing and leading blanks from our string.
                IN: str - the string we want to Trim

                RETVAL: A Trimmed string!
        ***/
        {
                return RTrim(LTrim(str));
        }


        function LTrim(str)
        /***
                PURPOSE: Remove leading blanks from our string.
                IN: str - the string we want to LTrim

                RETVAL: An LTrimmed string!
        ***/
        {
                var whitespace = new String(" \t\n\r");

                var s = new String(str);

                if (whitespace.indexOf(s.charAt(0)) != -1) {
                    // We have a string with leading blank(s)...

                    var j=0, i = s.length;

                    // Iterate from the far left of string until we
                    // don't have any more whitespace...
                    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                        j++;


                    // Get the substring from the first non-whitespace
                    // character to the end of the string...
                    s = s.substring(j, i);
                }

                return s;
        }


        function RTrim(str)
        /***
                PURPOSE: Remove trailing blanks from our string.
                IN: str - the string we want to RTrim

                RETVAL: An RTrimmed string!
        ***/
        {
                // We don't want to trip JUST spaces, but also tabs,
                // line feeds, etc.  Add anything else you want to
                // "trim" here in Whitespace
                var whitespace = new String(" \t\n\r");

                var s = new String(str);

                if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
                    // We have a string with trailing blank(s)...

                    var i = s.length - 1;       // Get length of string

                    // Iterate from the far right of string until we
                    // don't have any more whitespace...
                    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                        i--;


                    // Get the substring from the front of the string to
                    // where the last non-whitespace character is...
                    s = s.substring(0, i+1);
                }

                return s;
        }

//Replace :: String Replace(String str, String find, String rep)

//Find the text in the string and replace it, text that has already been replaced once will not be replaced again. 
function Replace(str, find, rep)
{
    var res = CStr(str);
    var i = 0;
    while(true)
    {
        i = res.indexOf(find, i);
        if (i == -1) break;
        res = res.substr(0, i) + rep + res.substr(i + find.length);
        i += rep.length;
    }
    return res;
}

//CStr :: String CStr(Object)

//Force a value to be a string, asserting its type. 

function CStr(value)
{
    return (value + "");
}


function PCase(STRING){
var strReturn_Value = "";
var iTemp = STRING.length;
if(iTemp==0){
return"";
}
var UcaseNext = false;
strReturn_Value += STRING.charAt(0).toUpperCase();
for(var iCounter=1;iCounter < iTemp;iCounter++){
if(UcaseNext == true){
strReturn_Value += STRING.charAt(iCounter).toUpperCase();
}
else{
strReturn_Value += STRING.charAt(iCounter).toLowerCase();
}
var iChar = STRING.charCodeAt(iCounter);
if(iChar == 32 || iChar == 45 || iChar == 46){
UcaseNext = true;
}
else{
UcaseNext = false
}
if(iChar == 99 || iChar == 67){
if(STRING.charCodeAt(iCounter-1)==77 || STRING.charCodeAt(iCounter-1)==109){
UcaseNext = true;
}
}


} //End For

return strReturn_Value;
} //End Function

