var psGROUPING_STYLE_ONE = '444444444444';
var psGROUPING_STYLE_TWO = '433333333333';
/*
Description of the above formatting:
Using 433333 as an example format, the first number on the left (4), describes how
far from the decimal point the next separator is. The second number (3), describes
how far from the previous separator the next separator is.

Using Value: 235432879.00
A 43233 format would be: 2,35,4,32,879.00 
A 33444 format would be: 23,543,28,79.00
Using the examples above, the values will be:
A 444444444444 format would be: 235,432,879.00
A 433333333333 format would be: 23,54,32,879.00
*/

var psCurrFromDec = '';
var pbHasDecimal = false;
var piDecPos = 0;
var psDecimal = '';
var psCurUsedDec = '';
var psData = '';
var piDecPlaces = 0;
var piCurFormat = 0;
var piLenOfData = 0;
var psSep = '';
var psPreparedVal = '';
var piPreparedDecPos = 0;
var pbInitialized = false;
var	psDirection = '';
var	pbCompareDataSet = false;
var	pbDiffPercentSet = false;

//added by john nelson
//for iZeus
//
var psAltSep = '';
var psAltDec = '';

//set external methods
//
function validateFormatCurrency()
{
	this.Control;
	
	this.initialize = initMNCurrency;
	this.validate = validateMNCurrency;
	this.proper = displayProper;
	this.clear = clearVariables;
	this.setPercent = setDifferencePercentage;
	this.setCompare = setCompareData;
	this.bidDiffWarn = bidDifferenceWarning;
	this.setDirection = setAuctionDirection;
	this.direction = auctionDirection;
	this.diffMessage = differenceWarningMessage;
	this.prepareCurrency = prepare_currency;
//Modified by Anuradha Vemuri as per John's instructions on 4/4/01 for Issue 36727	
	this.displayProp = returnFormattedCurrency;
	//this.displayProp = displayProper;
	
}

//initialize the general variables
//
function clearVariables()
{
	psCurrFromDec = '';
	pbHasDecimal = false;
	piDecPos = 0;
	psDecimal = '';
	psData = '';
	piDecPlaces = 0;
	piCurFormat = 0;
	piLenOfData = 0;
	psSep = '';
	psPreparedVal = '';
	piPreparedDecPos = 0;
	pbInitialized = false;
	psCurUsedDec = '';
	psDirection = '';
	bDirectionSet = false;
	pbCompareDataSet = false;
	pbDiffPercentSet = false;
}

//this function is set to return a properly formatted
//currency value from a double formatted value
//
function returnFormattedCurrency()
{

	//if not initialized, 
	//return false
	//
	if(!pbInitialized)
		return false;
	
	var sTempDecHolder = psDecimal;
	psDecimal = '.';
	var sReturnData = '';
	piLenOfData = psData.length;
	piDecPos = getDecimalPosition();
	if((piLenOfData - piDecPos - 1) > piDecPlaces)
		return '-2';
	
	psPreparedVal = prepare_currency();
	psDecimal = sTempDecHolder;
	sReturnData = displayProper();
	return sReturnData;

}

//this function will strip preceding and following 
//spaces
//
function trimSpaces(sDataToTrim)
{
	var sReturnDataF = '';
	var sReturnDataE = '';
	var bFrontDone = false;
	var bEndDone = false;

	//strip leading spaces
	//
	for(x=0;x<sDataToTrim.length;x++)
	{
		//if not a space, append to the return
		//value
		//
		if(sDataToTrim.charAt(x)!=' ')
		{
			sReturnDataF = sReturnDataF + sDataToTrim.charAt(x);
			bFrontDone = true;
		}
		else
		{
			if(bFrontDone)
				sReturnDataF = sReturnDataF + sDataToTrim.charAt(x);
		}	
	}

	//strip trailing spaces
	//
	for(x=sReturnDataF.length-1;x>=0;x--)
	{
		//if not a space, append to the return
		//value
		//
		if(sReturnDataF.charAt(x)!=' ')
		{
			sReturnDataE = sReturnDataF.charAt(x) + sReturnDataE;
			bEndDone = true;
		}
		else
		{
			if(bEndDone)
				sReturnDataE = sReturnDataF.charAt(x) + sReturnDataE;
		}	
	}
	return sReturnDataE;
}

//This is the main function that calls all the other
//functions in terms of validation. This function
//returns either -1 for failure - 2 appended to the
//formatted data for format failure, 
//or the formatted value for acceptance...
//
function validateMNCurrency(sInputData)
{

	if(!pbInitialized)
		return false;
	
	var bAbnormalDigitPositioning = false;
	var bAbnormalDigitsByFormat = false;
	var sMsg = '';
	
	psData = trimSpaces(sInputData);
	piLenOfData = psData.length;
	piDecPos = getDecimalPosition();
	if((piLenOfData - piDecPos - 1) > piDecPlaces)
		return '-2';

	if(invalidDigits())
		return '-1';

	if(digitPositioningBad())
		return '-2';
		
	if(incorrectDigitsByFormat())
		return '-2';
		
	psPreparedVal = prepare_currency();
	
	return psPreparedVal;
}


//set initial variable settings
//modified by john nelson for iZeus
//
function initMNCurrency(iInputCurFormat,iInputDecPlaces,sDecSep,sThouSep)
{
	clearVariables();

	piDecPlaces = Number(iInputDecPlaces);
	piCurFormat = Number(iInputCurFormat);
	psCurrFromDec = assignGroupingFormat();
	
	//set separator and determine
	//the separators other than
	//specified : alt-separators
	//
	psSep = sThouSep; //getSepByFormat(); modified by john nelson for iZeus
	switch(psSep)
	{
		case ",":
			psAltSep = '.';
			break;
		case ".":
			psAltSep = ',';
			break;
		default:
			psAltSep = '.';
	}
	
	//set decimal and determine
	//the decimal other than
	//specified : alt-decimal
	//
	psDecimal = sDecSep; //getDecimalByFormat(); modified by john nelson for iZeus
	switch(psDecimal)
	{
		case ".":
			psAltDec = ',';
			break;
		case ",":
			psAltDec = '.';
			break;
		default:
			psAltDec = ',';
	}
	
	pbInitialized = true;
	return true;
}

//get and return the position
//of the decimal
//
function getDecimalPosition() //position of decimal
{
	for(var j = piLenOfData - 1;j >= 0;j--)
	{
		
		if (psData.charAt(j) == psDecimal)
		{
			pbHasDecimal = true;
			psCurUsedDec = psData.charAt(j);
			return j;
		}	
		
		//if(piLenOfData - j -1 > piDecPlaces)
		//{
		//	pbHasDecimal = false;
		//	break;
		//}
		
	}
	if (pbHasDecimal == false)
	{
		return piLenOfData;
	}
}

//based off grouping style id passed
//passed, assign the grouping style
//format
//
function assignGroupingFormat()	
{
	//a new case would need to be created
	//for each new grouping style
	//
	switch(piCurFormat)
	{
		case 1:
			return psGROUPING_STYLE_ONE;
		case 2:
			return psGROUPING_STYLE_TWO;
		default:
			return psGROUPING_STYLE_ONE;
	}
}



//This function checks the input string
//for invalid digits. 
//
function invalidDigits()
{
	var VALIDDECIMALS = '1234567890';
	var NOTVALID = -1;
	var iCurPos = 0;
	var sCurChar;

	for( iCurPos = 0 ; iCurPos < piLenOfData ; iCurPos++ )
	{
		sCurChar = psData.charAt(iCurPos);
		if (VALIDDECIMALS.indexOf(sCurChar) == NOTVALID)
		{
			if(sCurChar != psSep && sCurChar != psDecimal)
				return true;
		}
	}
	return false;
}



//This function checks the input string and 
//checks to be sure the positioning is ok.
//
function digitPositioningBad()
{
	var sEvalNum = '';
	var sSubStr = '';
	var iCurPos = 0;
	var sCurChar = '';
	var iCurLenCount = 1;
	
	//create the positioning format of the submitted
	//amount
	//
	for (iCurPos = piDecPos - 1;iCurPos >= 0;iCurPos--)
	{
		sCurChar = psData.charAt(iCurPos);
		
		//if the character is a decimal!
		//
		if(sCurChar==psDecimal)
			return true;
			
		if (psSep.indexOf(sCurChar) != -1)
		{
			sEvalNum = sEvalNum + String(iCurLenCount);
			iCurLenCount = 1;
		}
		else
		{
			iCurLenCount++;
		}
	}
	
	//make sure the thousand separator is not in the decimal section
	//
	for(iCurPos = piLenOfData - (piLenOfData - piDecPos - 1); iCurPos < piLenOfData; iCurPos++)
	{
		sCurChar = psData.charAt(iCurPos);
		
		//if the character is a separator!
		//
		if(sCurChar==psSep)
			return true;
	}

	//grab the substring for the defined format
	//based off the length of the currently created
	//positioning format
	//
	sSubStr = psCurrFromDec.substr(0,sEvalNum.length);
	
	//if they match
	//
	if (sSubStr == sEvalNum)		
		return false;
	else
		return true;
}



//This function checks the input string and 
//checks to be sure the correct seperator
//characters are being used...
//
function incorrectDigitsByFormat()
{

	var sSubStr = '';
	var iCurPos = 0;
	var sCurChar = '';
	
	//grab the actual number minus
	//what is to the right of the decimal
	//
	if(pbHasDecimal)
		sSubStr = psData.substr(0,piLenOfData - (piLenOfData - piDecPos));
	else
		sSubStr = psData;

	//determine if the correct values are being used or not
	//if incorrect values, return true
	//
	if (sSubStr.indexOf(psAltSep) != -1)
		return true;
	if (psCurUsedDec == psAltDec)
		return true;

	return false;
}

//The purpose of this function is to
//format incoming "currency" inputs to a
//fixed float number value. The only case 
//a float format will not be returned
//is when no valid decimal place is
//set on the incoming data.
//
function prepare_currency()
{

	var sValue = psData.substr(0,piDecPos);
	var sDecVal = '';

	if(pbHasDecimal)
		sDecVal = psData.substr(piDecPos + 1,piLenOfData - piDecPos - 1);	
	
	var sNewData = '';
	var iCurPos = 0;
	var sCurChar = '';
	
	//for all the items in the string
	//
	for( iCurPos = 0 ; iCurPos < sValue.length ; iCurPos++ )
	{
		sCurChar = sValue.charAt(iCurPos);
		//if (sCurChar == ',' || sCurChar == '.')
		if (sCurChar == psSep)
			sCurChar = sCurChar;
		else
			sNewData = sNewData + sCurChar;
	}
	piPreparedDecPos = sNewData.length + 1;
	
	if(pbHasDecimal)
		return sNewData + "." + sDecVal;
	else
		return sNewData;
}

//
//The purpose of this function is to
//format incoming "currency" inputs to a
//formate based off the currency id 
//
function displayProper()
{

	if(!pbInitialized)
		return false;
	
	var sSubStr = '';
	var iCurPos = 0;
	var sCurChar = '';
	var sDecString = '';
	var sValString = '';
	var iCounterA = 0;
	var iCounterB = 0;
	var iCounterC = Number(psCurrFromDec.charAt(iCounterA));
	var sFormatData = psPreparedVal.split('.');

	//if a decimal is specified
	//get separate the dec string 
	//from the number string
	//
	if(pbHasDecimal)
		sDecString = sFormatData[1];
	sFormatData = sFormatData[0];
	
	//format the string left of the dec
	//
	//
	for (iCurPos = piPreparedDecPos - 2;iCurPos >= 0;iCurPos--)
	{
		//grab the current position
		//
		sCurChar = sFormatData.charAt(iCurPos);
		iCounterB++;
		
		//if the current position equals the
		//expected separator location
		//
		if(iCounterB == iCounterC)
		{
			//apply the seperator
			//
			sValString = sCurChar + psSep + sValString;
			iCounterA++;  //inc pos of pos format value string
			
			//set the next position to place the next separator
			//
			iCounterC = iCounterC + Number(psCurrFromDec.charAt(iCounterA)) - 1;
		}
		else
			sValString = sCurChar + sValString;
	}
	
	//return the value
	//
	if (sDecString == '')
		return sValString;
	else
		return sValString + psDecimal + sDecString;
}
function setDifferencePercentage(iPercentage)
{
	iDiffPercent = iPercentage;
	pbDiffPercentSet = true;
}

function setCompareData(sCompData)
{
	sCompareData = sCompData;
	pbCompareDataSet = true;
}

function setAuctionDirection(sDir)
{
	psDirection = sDir;
	bDirectionSet = true;
}

function auctionDirection()
{
	if(!bDirectionSet)
		return 'no auction direction defined';
	
	return psDirection;
}

function bidDifferenceWarning()
{
	if(!pbCompareDataSet)
		return false;
		
	if(!pbDiffPercentSet)
		return false;
		
	if(!bDirectionSet)
		return false;
		
	if(psDirection == 'F')
	{
		var dPercent = iDiffPercent / 100;
		var psDataAddPercent = Number(sCompareData) + Number((sCompareData * dPercent));
	
		if(Number(psPreparedVal) >= Number(psDataAddPercent))
			return true;
		else
			return false;
	}
	else if(psDirection == 'R')
	{
		var dPercent = iDiffPercent / 100;
		var psDataAddPercent = Number(sCompareData) - Number((sCompareData * dPercent));
	
		if(Number(psPreparedVal) <= Number(psDataAddPercent))
			return true;
		else
			return false;
	}
}

//determine which message to use
//for the bid difference warning
//
function differenceWarningMessage(bHideBest,sMsg1,sMsg2,sMsg3,sMsg4)
{
	var bHideBestBid = bHideBest;
	
	if(bHideBestBid && (psDirection == 'F'))
		return sMsg1;
	if(!bHideBestBid && (psDirection == 'F'))
		return sMsg2;
	if(bHideBestBid && (psDirection == 'R'))
		return sMsg3;
	if(!bHideBestBid && (psDirection == 'R'))
		return sMsg4;
}


/********************************************************
By Anuradha Vemuri on 1/12/01 

Purpose: To check if the given string contains any characters that are blocked
			
Inputs:	strToBeValidated	-	The String against which the validation can be done
			
Output:		A boolean value specifying whether the given string is valid or not.
*********************************************************/
function validateStringForHTMLtags(strToBeValidated)
{	
//	var invalid="<%=LIST_OF_CHARS_TO_BE_BLOCKED%>";
//Commented temporarily until a solution is found. 
	var invalid = "/\\*?<>|;";
	
	for (var i=0; i<= strToBeValidated.length-1; i++) {
		if (invalid.indexOf(strToBeValidated.charAt(i)) >= 0)
			return false;
	}			
	return true;
}


/**************
dt:03/30/01

Purpose: To trim a give string

Input: String to be trimmed

Output: Trimmed string
***************/


function trimString(strValue) {
     var ichar, icount;
     ichar = strValue.length - 1;
     icount = -1;
     while (strValue.charAt(ichar)==' ' && ichar > icount)
         --ichar;
     if (ichar!=(strValue.length-1))
         strValue = strValue.slice(0,ichar+1);
     ichar = 0;
     icount = strValue.length - 1;
     while (strValue.charAt(ichar)==' ' && ichar < icount)
         ++ichar;
     if (ichar!=0)
         strValue = strValue.slice(ichar,strValue.length);
     return strValue;
 }
