//	routines to do page-level validation for edit prices
//		depends on routines in CCEValidPrice.js
//	MODIFIED 12/2004 to accommodate new grades
//	MODIFIED 01/2010 to accommodate plus grades

//	Form variables to be validated are expected to be in this format:
//		[prefix]cccccggpvbs[suffix]
//	where 
//		[prefix] is "txt" by default,
//		ccccc=coin#, gg=grade, p=plusflag, v=grading service, b=bid/ask, s=seen/unseen, and
//		[suffix] is one of "Orig", "Prce", "Qty", "Status"

// set this if your control names have a prefix other than "txt"
var sPrefix = "txt";

// set this if your control names have the coin number after the prefix... no default value!
var sCoin = ""; 

// WAS: var aryGrades = new Array("45", "50", "55", "58", "60", "61", "62", "63", "64", "65", "66", "67", "68");
var aryGrades = new Array("02", "03", "04", "06", "08", "10", "12", "15", "20", "25", "30", "35", "40", "45", "50", "53", "55", "58", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70");
var nGrades = aryGrades.length;

// Plus grades; note 60,61 are absent from list
var aryPlusGrades = new Array("45", "50", "53", "55", "58", "62", "63", "64", "65", "66", "67", "68");
var nPlusGrades = aryPlusGrades.length;
// Plus coin exceptions
var sPlusCoinExceptionList = "";

// set this true if your control names have grade numbers in them,
// e.g., when editing all grades at once, txtMyControl becomes txt45MyControl, txt50MyControl etc.
// NOTE! bAllGrades=true means all grades BETWEEN sMinGrade AND sMaxGrade, inclusive!
var bAllGrades = false;

// set these to restrict 'ALL' validation to a certain grade range
var sMinGrade = "02";
var sMaxGrade = "70";

// set this to something else to cause validation to be limited to specific grading services
var sGsvcIDs = "PCNGAIR";
var sPlusGsvcIDs = "PCNG";

// set this to restrict validation to certain sight-codes
var sSightCdes = "US";

function ValidTriple(thePrice, theLotQty, theStatus)
	{	// used by both single-price edit and grade edit
	var t;
	if(CanonicalPrice(thePrice.value,false) == "invalid") return false;
	if(CanonicalQty(theLotQty.value,false) == "invalid") return false;
	theStatus.value = CanonicalStatus(theStatus.value);
	return true;
	}
	
function ValidGradePricePage(theForm)
	{	// wrapper to accommodate status bar changes and prefix-coin# parts of controls
	var sMultipleSpec, aryMultipleSpec, iSpec;
	var sOneSpec, sOneCoin, sOneGrade, sOnePlusFlg, sOneGsvc;

	window.status = "Validating prices, please wait...";

	sMultipleSpec = theForm.elements("txtMultipleSpec").value;
	if (sMultipleSpec != "")
		{	// got here from a page with multiple coin specs
		aryMultipleSpec = sMultipleSpec.split("_");
		for (iSpec=0; iSpec<aryMultipleSpec.length; iSpec++)
			{
				sOneSpec = aryMultipleSpec[iSpec];
				sOneGsvc = sOneSpec.substring(0,1);
				sOneCoin = sOneSpec.substring(1,7);	// substring args are weird in JavaScript!
				sOneGrade = sOneSpec.substring(8,10);
				if (!ValidCoinGradeGsvc(theForm, sPrefix, sOneCoin, sOneGrade, "0", sOneGsvc))
					{
					window.status = "";
					return false;
					}
				if (IsPlusService(sOneGsvc) && IsPlusCoinGrade(sOneCoin, sOneGrade))
				    {   // check plus side of market
				    if (!ValidCoinGradeGsvc(theForm, sPrefix, sOneCoin, sOneGrade, "1", sOneGsvc))
					    {
					    window.status = "";
					    return false;
					    }
				    }
			}
			window.status = "Updating prices, please wait...";
			return true;
		}
	else
		{	// got here from a page with a single coin
		if (ValidGradePricePageImplementation(theForm, sPrefix, sCoin))
			{
			window.status = "Updating prices, please wait...";
			return true;
			}
		else
			{
			window.status = "";
			return false;
			}
		}
	}

function ValidGradePricePageImplementation(theForm, thePrefix, theCoin)
	{

	var iGrade;
	if (bAllGrades)
		{
		for (iGrade=0; iGrade<nGrades; iGrade++)
			{
			if (sMinGrade <= aryGrades[iGrade] && aryGrades[iGrade] <= sMaxGrade)
				{
				if (sGsvcIDs.indexOf("P") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, aryGrades[iGrade], "0", "P")) return false; }
				if (sGsvcIDs.indexOf("C") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, aryGrades[iGrade], "0", "C")) return false; }
				if (sGsvcIDs.indexOf("N") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, aryGrades[iGrade], "0", "N")) return false; }
				if (sGsvcIDs.indexOf("G") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, aryGrades[iGrade], "0", "G")) return false; }
				if (sGsvcIDs.indexOf("A") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, aryGrades[iGrade], "0", "A")) return false; }
				if (sGsvcIDs.indexOf("I") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, aryGrades[iGrade], "0", "I")) return false; }
				if (sGsvcIDs.indexOf("R") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, aryGrades[iGrade], "0", "R")) return false; }

				if (IsPlusCoinGrade(theCoin, aryGrades[iGrade]))
				    {
				    if (IsPlusService("P")) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, aryGrades[iGrade], "1", "P")) return false; }
				    if (IsPlusService("C")) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, aryGrades[iGrade], "1", "C")) return false; }
				    if (IsPlusService("N")) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, aryGrades[iGrade], "1", "N")) return false; }
				    if (IsPlusService("G")) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, aryGrades[iGrade], "1", "G")) return false; }
				    if (IsPlusService("A")) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, aryGrades[iGrade], "1", "A")) return false; }
				    if (IsPlusService("I")) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, aryGrades[iGrade], "1", "I")) return false; }
				    }
				}
			}
		}
	else
		{	// note: sMinGrade = sMaxGrade in this case
		if (sGsvcIDs.indexOf("P") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, sMinGrade, "0", "P")) return false; }
		if (sGsvcIDs.indexOf("C") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, sMinGrade, "0", "C")) return false; }
		if (sGsvcIDs.indexOf("N") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, sMinGrade, "0", "N")) return false; }
		if (sGsvcIDs.indexOf("G") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, sMinGrade, "0", "G")) return false; }
		if (sGsvcIDs.indexOf("A") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, sMinGrade, "0", "A")) return false; }
		if (sGsvcIDs.indexOf("I") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, sMinGrade, "0", "I")) return false; }
		if (sGsvcIDs.indexOf("R") >= 0) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, sMinGrade, "0", "R")) return false; }

		if (IsPlusCoinGrade(theCoin, sMinGrade)) 
		    {
		    if (IsPlusService("P")) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, sMinGrade, "1", "P")) return false; }
		    if (IsPlusService("C")) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, sMinGrade, "1", "C")) return false; }
		    if (IsPlusService("N")) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, sMinGrade, "1", "N")) return false; }
		    if (IsPlusService("G")) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, sMinGrade, "1", "G")) return false; }
		    if (IsPlusService("A")) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, sMinGrade, "1", "A")) return false; }
		    if (IsPlusService("I")) { if (!ValidCoinGradeGsvc(theForm, thePrefix, theCoin, sMinGrade, "1", "I")) return false; }
		    }
		}
	return true;
	}
	
function ValidCoinGradeGsvc(theForm, thePrefix, theCoin, theGrade, thePlusFlg, theGsvc)
	{
	if (thePlusFlg == "1")
	    {
	    if (sSightCdes.indexOf("U") >= 0) { if (!ValidPricebyName(theForm, thePrefix, theCoin, theGrade, "1", theGsvc, "B", "U")) return false; }
	    if (sSightCdes.indexOf("U") >= 0) { if (!ValidPricebyName(theForm, thePrefix, theCoin, theGrade, "1", theGsvc, "A", "U")) return false; }
	    }
	else
	    {
	    if (sSightCdes.indexOf("U") >= 0) { if (!ValidPricebyName(theForm, thePrefix, theCoin, theGrade, "0", theGsvc, "B", "U")) return false; }
	    if (sSightCdes.indexOf("S") >= 0) { if (!ValidPricebyName(theForm, thePrefix, theCoin, theGrade, "0", theGsvc, "B", "S")) return false; }
	    if (sSightCdes.indexOf("U") >= 0) { if (!ValidPricebyName(theForm, thePrefix, theCoin, theGrade, "0", theGsvc, "A", "U")) return false; }
	    if (sSightCdes.indexOf("S") >= 0) { if (!ValidPricebyName(theForm, thePrefix, theCoin, theGrade, "0", theGsvc, "A", "S")) return false; }
	    }
	return true;
	}
	
function ValidPricebyName(theForm, thePrefix, theCoin, theGrade, thePlusFlg, theGsvc, theBidAsk, theSightCde)
	{
	var rc, txt, front;
	front = thePrefix + theCoin + theGrade + thePlusFlg + theGsvc + theBidAsk + theSightCde;
	rc = ValidTriple(
	    theForm.elements(front + "Prce"), 
	    theForm.elements(front + "Qty"), 
	    theForm.elements(front + "Status")
	    )
	if (rc == false)
		{
		txt = "Price or Lot size is invalid for" 
		if (theCoin != "") txt = txt + " " + theCoin;
		if (theGrade != "") txt = txt + (theCoin != "" ? "." : " ") + theGrade;
		if (thePlusFlg != "") txt = txt + (thePlusFlg == "1" ? "+" : "");
		if (theGsvc != "") txt = txt + " " + GsvcNameForGsvc(theGsvc);
		txt = txt + (theBidAsk == "B" ? " BID" : " ASK");
		txt = txt + (theSightCde == "U"  ? " SIGHT-UNSEEN" : " sight-seen");
		alert(txt);
		}
	return rc; 
	}

function GsvcNameForGsvc(theGsvc)
	{
	return (theGsvc == "P" ? "PCGS" : theGsvc == "C" ? "PCGS-CAC" : theGsvc == "N" ? "NGC" : theGsvc == "G" ? "NGC-CAC" : theGsvc == "A" ? "ANACS" : theGsvc == "I" ? "ICG" : theGsvc == "R" ? "RAW" : "?");
	}

function IsPlusCoinGrade(theCoin, theGrade)
    {
	var iPlusGrade;

	if (IsPlusCoin(theCoin) == false) { return false; }

	if (theCoin == "9146")
		{
		if (theGrade == "66") { return true; }
		if (theGrade == "67") { return true; }
		return false;
		}
	if (theCoin == "9140")
		{
		if (theGrade == "66") { return true; }
		if (theGrade == "67") { return true; }
		if (theGrade == "68") { return true; }
		return false;
		}
	if (theCoin == "8981")
		{
		if (theGrade == "66") { return true; }
		if (theGrade == "67") { return true; }
		return false;
		}
	if (theCoin == "8858")
		{
		if (theGrade == "66") { return true; }
		if (theGrade == "67") { return true; }
		if (theGrade == "68") { return true; }
		return false;
		}
	if (theCoin == "8648")
		{
		if (theGrade == "66") { return true; }
		if (theGrade == "67") { return true; }
		if (theGrade == "68") { return true; }
		return false;
		}
	if (theCoin == "8310")
		{
		if (theGrade == "66") { return true; }
		if (theGrade == "67") { return true; }
		if (theGrade == "68") { return true; }
		return false;
		}
	
	for (iPlusGrade=0; iPlusGrade<nPlusGrades; iPlusGrade++)
		{
		if (theGrade == aryPlusGrades[iPlusGrade]) { return true; }
		}
	return false;
    }

function IsPlusService(theGsvc)
    {
	return (sPlusGsvcIDs.indexOf(theGsvc) >= 0 ? true : false);
    }

function IsPlusCoin(theCoin)
    {
	return (sPlusCoinExceptionList.indexOf(theCoin) >= 0 ? false : true);
    }
