/*GroupSubmitIDString is a comma-separated string of id's*/
/*When the "group submit" window is opened, this string is passed to it*/
/*The window then shows the details of the items denoted by the id's contained within this string*/
var GroupSubmitIDString = "";

//JL 2008-02-20 Begin: 
//These 2 variables keep track of the mouse co-ordinates
var MouseCoordinateX;
var MouseCoordinateY;

//this variable stores the ID of the process that gets kicked off when the "mouseover" event 
// (on the item hierarchy datagrid fields) initiates a "timeout"
var MouseOverTimeoutID; 
//JL 2008-02-20 End

function HSCodeFocusLost(textbox)
{   
  document.getElementById("txtHSCodeTransfer").value = textbox.value;
  document.getElementById("btnFocusLostEventHandler").click();
}   

function ItemCECodeFocusLost()
{   
	document.getElementById("txtHSCodeTransfer").value = textbox.value;
	document.getElementById("btnFocusLostEventHandler").click();
}

function ClearHSCodeTextBoxOnQueryChange(chkQueries,txtHSCode)
{
	if (chkQueries.checked)
		txtHSCode.value = "";
}

function ClearHSCodeTextBoxOnCommentChange(txtHSCode,txtComment)
{
	if ( (txtComment.value == "") || (txtComment.value == null) )
	{
		txtHSCode.value = txtHSCode.value;
	}
	else
	{
		txtHSCode.value = "";
	}
		
}

function ClearControlsOnHSCodeChange(chkQueries,txtComment,txtHSCode)
{
	if (chkQueries.checked)
		chkQueries.checked = false;
		
	if (txtComment.value != "")	
		txtComment.value = "";	
}

function NormalRowClicked(row, itemcecodeid)
{
	var dgrdRow = document.getElementById(row.uniqueID);
	if (dgrdRow.className == "HiLite")
		{	/*Un-Highlight the row*/
			dgrdRow.className = "frDgrdBody";
			RemoveIDFromString(itemcecodeid);
		}
	else
		{	/*Highlight the row*/
			dgrdRow.className = "HiLite";
			AddToGroupSubmitString(itemcecodeid);			
		}
		
	SetGroupSubmitButtonState();
}


function AlternateRowClicked(row, itemcecodeid)
{
	var dgrdRow = document.getElementById(row.uniqueID);
	if (dgrdRow.className == "HiLite")
		{
			dgrdRow.className = "frDgrdAlternate";
			RemoveIDFromString(itemcecodeid);				
		}
	else
		{
			dgrdRow.className = "HiLite";
			AddToGroupSubmitString(itemcecodeid);			
		}
	SetGroupSubmitButtonState();
}

function SetGroupSubmitButtonState()
{
	//only enable the button if more than one item has been selected
	var SubstringToFind = ","
	var SubstringExists = GroupSubmitIDString.search(SubstringToFind);	
	if (SubstringExists >= 0) 
	{
		document.getElementById("btnGroupSubmit").disabled = false;
	}
	else
	{
		document.getElementById("btnGroupSubmit").disabled = true;
	}
}


function AddToGroupSubmitString(itemcecodeid)
{
	if (GroupSubmitIDString == "")	
		{
			/*this is the first id we're adding*/
			GroupSubmitIDString = itemcecodeid + "";	
		}
	else
		{
			GroupSubmitIDString = GroupSubmitIDString + "," + itemcecodeid;	
		}
}


function ShowGroupSubmitScreen()
{
 var ModalWindowAttributes;
 var ModalWindowHeightString;
 var ModalWindowHeight;
 var BaseHeight;
 var IncrementFactor;
 var HeightIncrement;

 /*clear any errors still visible on the main screen*/
 document.getElementById("lblErrorText").innerText = "";
 
 /*clear the groupsubmitid string*/
 var TempGroupSubmitIDString = GroupSubmitIDString;
 GroupSubmitIDString = "";
 
 /*determine how many items are going to be shown*/
 var CommaCharacter = "" + ",";
 var ItemArray = TempGroupSubmitIDString.split(CommaCharacter);
 var ItemCount = ItemArray.length;
 
 /*calculate the height of the modal window*/
 if (ItemCount >= 8)
 {
	ModalWindowHeight = 520; /*from 8 items onward, we don't want the grid to grow anymore*/
 }
 else
	{
		BaseHeight = 310; /*this is the minimum height*/
		IncrementFactor = 30; /*this is by how much pixels the height is increased, per datagrid item*/
		HeightIncrement = (ItemCount - 1) * IncrementFactor; /*this is by how much pixels we're heightening the grid*/
		ModalWindowHeight = BaseHeight + HeightIncrement;		
	}
 ModalWindowHeightString = "dialogHeight:" + ModalWindowHeight + "px;";
 
 /*show the modal window*/ 
 ModalWindowAttributes = ModalWindowHeightString + "dialogWidth:620px;dialogTop:60px;dialogLeft:10px;resizable:no;toolbar:no;scrollbars:no;center:yes;status:no"
 var l_popupUrl = "GroupSubmit.aspx?IDs=" + TempGroupSubmitIDString;
 var response = window.showModalDialog(l_popupUrl, 700, ModalWindowAttributes) + "";
 
	if ( response != null )
	{
		document.getElementById('txtReturn').innerText = response;
	}
 
 }


function RemoveIDFromString(itemcecodeid)
{
 /*NOTE: the "search" function returns -1 if it can't find the substring*/
 
 /*check if the id is at the beginning or middle (if there is more than 1 id in the string)*/
 var SubstringToFind = itemcecodeid + ","
 var SubstringExists = GroupSubmitIDString.search(SubstringToFind);	
 
 if (SubstringExists >= 0)
	{	
		GroupSubmitIDString = GroupSubmitIDString.replace(SubstringToFind, "")		
	}
 else
	{
		/*check if the id is at the end (of the group submit string)*/
		SubstringToFind = "," + itemcecodeid
		SubstringExists = GroupSubmitIDString.search(SubstringToFind)
		if(SubstringExists >= 0)
			{				
				GroupSubmitIDString = GroupSubmitIDString.replace(SubstringToFind, "")
			}
		else
			{
				/*check if the id is at the start of the string (if it is the only id in the sting)*/
				SubstringToFind = itemcecodeid + ""
				SubstringExists = GroupSubmitIDString.search(SubstringToFind)
				if(SubstringExists >= 0)
				{				
					GroupSubmitIDString = GroupSubmitIDString.replace(SubstringToFind, "")
				}
			}
	}
 
}

function CloseWindow()
{
	window.returnValue = "cancel";
	self.close();
}

function CloseWindowAfterSubmit()
{
	var txt = document.getElementById("txtHidden");
	if (txt.value != "")
	{
		self.close();
	}
}

function SetFocus()
{
	document.getElementById("txtUserName").focus();
}

function ForceNumericOnlyInput(control)
{   
   var keychar;
   var key=window.event.keyCode;
   keychar = String.fromCharCode(key);
   
   if (!(("0123456789").indexOf(keychar) > -1))
   {
		key=0;
   }
         
   window.event.keyCode=key;
}

/*
function HSCodeCapture()
//This function gets called each time the user presses a button while in the "hs code" field
{
   var PressedKeyCode = window.event.keyCode;
   if (PressedKeyCode == 8) //8 = backspace
   {	
		//txtHSCode.value = "";
		var x = 1977;
   }
   
   //[12.34.56.78] after 2, 4 and six, add a period to the end of the string
   
}
*/

function MakeUserNameLowerCase()
{
  var UserName = document.getElementById("txtUserName").value;
  document.getElementById("txtUserName").value = UserName.toLowerCase();
}

function DisallowSingleQuotes(control)
{   
   var keychar;
   var key=window.event.keyCode;
   keychar = String.fromCharCode(key);
   
   if ((("'").indexOf(keychar) > -1))
   {
		key=0;
   }

   window.event.keyCode=key;
}

function ValidateClipboardContents()
{   
   var clipboardtext = clipboardData.getData("Text");
   
   /*check if the clipboard contents contain any non-numeric characters*/      
   var NonNumericPattern = new RegExp("[^0-9]");
   
   var matcharray = clipboardtext.match(NonNumericPattern);
   if ( (matcharray != null) && (matcharray.length > 0) )
   {
		/*prevent the paste*/
		event.returnValue = false;
   }   
}

//JL 2008-02-19 Begin
function showTip(oEvent, HierarchyDescription)
{	
	var MouseCoordinates = new Array();
	MouseCoordinates = GetMouseCoordinates(oEvent);

	//keep track of the current mouse coordinates
	MouseCoordinateX = MouseCoordinates[0]; 
	MouseCoordinateY = MouseCoordinates[1]; 
		
	//wait a little while before actually showing the tooltip
	MouseOverTimeoutID = window.setTimeout(function() {showActualTip.call(this, HierarchyDescription);  }, 1000);	
}

function showActualTip(HierarchyDescription) 
{	
	var divToolTip = document.getElementById("divToolTip");
		
	//position the tooltip so as not to obscure the mouse pointer	
	divToolTip.style.left = MouseCoordinateX + 9;
	divToolTip.style.top = MouseCoordinateY + 2;	
		
	//replace any escape characters in the hierarchy description with the real thing
	HierarchyDescription = UnEscapeSingleQuote(HierarchyDescription);
	
	var txtToolTip = document.getElementById("txtToolTip");
	txtToolTip.value = HierarchyDescription;
	
	divToolTip.style.visibility = "visible";	
}

function UnEscapeSingleQuote(InputString)
//definition: "unescape" - the opposite of "escaping"; i.e. to translate a symbolic code to an actual value
{
	var SingleQuotePattern = /&#39;/g; //"&#39;" is the official HTML escape character for a single quote
	return InputString.replace(SingleQuotePattern, "'"); //convert the single quote code to an actual single quote
}

function hideTip() 
{
	var divToolTip = document.getElementById("divToolTip");
	divToolTip.style.visibility = "hidden";
	
	//if the user didn't hover long enough in the item hierarchy fields to trigger the tooltip, make sure the tooltip won't appear
	clearTimeout(MouseOverTimeoutID);
}

function GetMouseCoordinates(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	var ResultArray = new Array();
	ResultArray[0] = posx;
	ResultArray[1] = posy;
	
	return ResultArray;
}
//JL 2008-02-19 End
