/*=====================================================================================
'  Viterra Ltd.
'
'  $Workfile:   LatestPrices.js  $
'  $Revision:   1.13  $
'  $Modtime:   Nov 15 2004 12:29:52  $
'
'  Purpose		: Latest Prices client interaction functionality
'  Parameters		: - Input(s)
'					  - Output(s)	
'  Notes		:     
'
'  Code Review Status 	: /PARTIAL
'  Page Future 		: KEEP
'  History		:
' $Log:   P:/eCommerce/VMArchive/archives/ezigrain/Main/Web/reports/js/LatestPrices.js-ARC  $
' 
'    Rev 1.13   Nov 15 2004 12:31:24   nabbo
' Issue 578 - Latest Prices in Netscape
' Compatibility issues resolved and added code header
' =====================================================================================
*/
function ClearInputs(ele) {
	if (ele.tagName == 'INPUT') {
		ele.value='';
	}
	if (ele.childNodes) {
		for (var i=0; i<ele.childNodes.length; i++) {
			ClearInputs(ele.childNodes.item(i));
		}
	}
}

function RemoveAndClear(pstr) {
	xRemove(pstr);
}

/*
Populate hidden fields that will be used to pass test criteria to the pricereports page
pType = S is for saved reports when submitting
pType = N is for normal reports when submitting
*/
function PopulateQS(pType) {
	var strComm;
	var element = xGetElementById("qsComm");
	var elementTwo;
	var intCount;
	var arrTestID;
	var i;
	var strName;
	var blnSubmit;
	var blnFullSubmit;
	
	blnNotSubmit = false;
	blnFullSubmit = false;

	//Get the currently selected Commodity
	if (element) {
		strComm = element.value;
	} else {
		if (document.report.cv1) {
			strComm = document.report.cv1.value;
			strComm = strComm.substring(1,(strComm.length -1));
		} 
		else {
			strComm = "-";
		}
	}
	
	//Loop though array until we find the commodity that we are using
	for (i=0;i<arrResultFieldCommodities.length;i=i+2) {
		if(arrResultFieldCommodities[i]==strComm) {
			//Get the sub array that holds all test ids that need to saved into the hidden fields
			arrTestID 		= arrResultFieldCommodities[i+1];
			//Loop though sub array and populate all hidden fields
			for(intCount=0;intCount<arrTestID.length;intCount++) {
				//Skip the padded value. This padded value is used to force Javascript to create the sub array
				//instead of creating a element of the master array
				if(arrTestID[intCount]!=0) {
					element 		= xGetElementById("qs"+(intCount-1));
//--------------------------------------------------------------------------------
// Changed  12-11-2004  Nick Abbot
// Issue 578 - Latest Prices in Netscape
// Netscape compatability correction
//--------------------------------------------------------------------------------
					elementTwo 		= xGetElementById("Post" + arrTestID[intCount]);
//--------------------------------------------------------------------------------
// End Changed  12-11-2004  Nick Abbot
// Issue 578 - Latest Prices in Netscape
//--------------------------------------------------------------------------------
					strName			= elementTwo.name;
					element.value 	= strName.substring(4,strName.length);
					element 		= xGetElementById("qv"+(intCount-1));
					if (elementTwo.value.length>8) {
						alert('The entered test result values are too long. Please enter values that are eight digits or less in length');
						return;
					}					
					if (elementTwo.value.length==0) {
						//blnNotSubmit = true;
						element.value 	= elementTwo.value;
					} else {
						element.value 	= elementTwo.value;
						blnFullSubmit = true;
					}
				}
			}
		}

	}
	if(pType=='N') {
		SubmitQS()
	} else {
		RefreshReport()
	}
}

/*
When a commodity is selected in the drop down box we need to display and hide test results
accordingly
*/
//--------------------------------------------------------------------------------
// Changed  12-11-2004  Nick Abbot
// Issue 578 - Latest Prices in Netscape
// Netscape compatability correction
//--------------------------------------------------------------------------------
function ChangeCommodity(strComm) {
	var element1;
	var element2;
	var i;
	var blnDisplayMessage;
	var arrTests;
	blnDisplayMessage = false;
	
	// Initally hide messages
	xRemove("itest");
	xRemove("itestOther");
	xRemove("itestTwo");

	//Initialise the booleans
	document.report.iCommodityTest.value ="false";
	
	//Hide all other test result fields
	for(i=0;i<arrResultFields.length;i++) {
		strCommand = "itest" + arrResultFields[i];
		xRemove(strCommand);
	}
	
	//Display the correct test result fields required for this commodity if any
	for (i=0;i<arrResultFieldCommodities.length;i=i+2) {
		if(arrResultFieldCommodities[i]==strComm) {
			arrTests = arrResultFieldCommodities[i+1]
			for(i=0;i<arrTests.length;i++) {
				if (arrTests[i]!=0) {
					strCommand = "itest" + arrTests[i];
					ClearInputs(xGetElementById(strCommand));
					xDisplay(strCommand);
					document.report.iCommodityTest.value ="true"; 
				}
			}
			
			blnDisplayMessage = true;
		}
	}
	
	//Hide Display message
	if(!blnDisplayMessage)
	{
		if(strComm=='-') {
			xDisplay("itest");
		} else {
			xDisplay("itestOther");
		}
	}

	//clear hidden fields when commodity is selected
	for(i=0;i<intTestCount;i++) {
		element1 = xGetElementById("qs"+(i));		
		element2 = xGetElementById("qv"+(i));		
		element1.value = '';
		element2.value = '';
	}

	return true;
}
//--------------------------------------------------------------------------------
// End Changed  12-11-2004  Nick Abbot
// Issue 578 - Latest Prices in Netscape
//--------------------------------------------------------------------------------
	
function DisplayTestResultsCombos(pArray) {
	var i;
	var strCommand;
	//Show all
	for(i=0;i<pArray.length;i++) {
		if (pArray[i]!=0) {
			strCommand = "itest" + pArray[i];
			xDisplay(strCommand);
			ClearInputs(xGetElementById(strCommand));
			document.report.iCommodityTest.value ="true"; 
		}
	} 
}

function CheckNumber(num) {
	num.value = trim(num.value)
	if (isNaN(num.value)) {  
		num.value = '';
	}
}


/* now in global.js
function trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = /  /g;
   while (temp.match(obj)) { temp = temp.replace(obj, " "); }
   return temp;
}
*/

// Function to navigate to Price Detail drill-down
function formSubmitView(PurchaseOptionID, strTestTypeList)
{
	var arrayOfStrings = strTestTypeList.split(',')

	for (var i=0; i < arrayOfStrings.length; i++) 
	{
		if ( i % 2 == 0)
		{
			xGetElementById("qs"+(i/2)).value = arrayOfStrings[i];
		}
		else
		{
			xGetElementById("qv"+((i-1)/2)).value = arrayOfStrings[i];			
		}
	}									
	
	if (document.layers) 
	{
		document.layers['Main'].document.report.action = '/Reports/PriceDetail.asp';
		document.layers['Main'].document.report.hdnPurchaseOptionID.value = PurchaseOptionID;
		document.report.target = '_self';
		document.layers['Main'].document.report.submit();
	}
	else 
	{
		document.report.action = '/Reports/PriceDetail.asp';
		document.report.hdnPurchaseOptionID.value = PurchaseOptionID;
		document.report.target = '_self';
		document.report.submit();
	}		

	return;
}




