/************************************************************************** 
*************************************************************************** 
*  Program Name: $Id: common.js,v 1.1.1.1 2007/12/10 00:51:16 neo Exp $
*  Program Author:  Michael T. Schock
*  Creation Date: 05-15-2006
*  CVS Revision: $Revision: 1.1.1.1 $
*  Copyright (c) 2006
*************************************************************************** 
*************************************************************************** 
*  Program Summary:
*  Javascript functions common to all pages
*
*
*************************************************************************** 
**************************************************************************/

//!  Convert Password for encryption
/**  Use the MD5 Hash Algorithm.  */
function Convert_Password(passName)
{
	//  Convert and restore the password
	document.getElementById(passName).value = calcMD5(document.getElementById(passName).value);
}

//!  IsNumeric function
/**  This function will accept a value and check that it is a number.  */
function Is_Numeric(passedValue)
{
	//  Variables
	var validChars = "0123456789.";
	var loop;
	var charValue;

	//  Loop through the passed value character and look for anything not a number.
	for(loop = 0; loop < passedValue.length; loop++)
	{
		//  Get the character
		charValue = passedValue.charAt(loop);

		//  Character is not a number
		if(validChars.indexOf(charValue) == -1)
		{
			//  Failure
			return(1);
		}
	}

	//  Success
	return(0);
}


//!  Is valid secure
/**  This function will check to see if the passed value is valid for a password.  */
function Is_Valid_Secure(passedValue)
{
	//  Variables
	var validChars;
	var validChars1 = " ";
	var validChars2 = "!\"#$%&'()*+`,./";
	var validChars3 = "0123456789";
	var validChars4 = ":;<=>?@";
	var validChars5 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var validChars6 = "{\\]^_"
	var loop;
	var charValue;

	//  Set the data string
	validChars = validChars1 + validChars2 + validChars3 + validChars4 + validChars5 + validChars6;

	//  Loop through the passed value character and look for anything not a valid character.
	for(loop = 0; loop < passedValue.length; loop++)
	{
		//  Get the character
		charValue = passedValue.charAt(loop);

		//  Character is not a number
		if(validChars.indexOf(charValue) == -1)
		{
			//  Failure
			return(1);
		}
	}

	//  Success
	return(0);
}

//!  Is valid text
/**  This function will check to see if the passed value is valid for general text.  */
function Is_Valid_Text(passedValue)
{
	//  Variables
	var validChars;
	var validChars1 = "0123456789";
	var validChars2 = "abcdefghijklmnopqrstuvwxyz ";
	var validChars3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var loop;
	var charValue;

	//  Set the data string
	validChars = validChars1 + validChars2 + validChars3;

	//  Loop through the passed value character and look for anything not a valid character.
	for(loop = 0; loop < passedValue.length; loop++)
	{
		//  Get the character
		charValue = passedValue.charAt(loop);

		//  Character is not a number
		if(validChars.indexOf(charValue) == -1)
		{
			//  Failure
			return(1);
		}
	}

	//  Success
	return(0);
}

//!  String Pad
/**  This function will pad a variable.  */
function String_Pad(dataString, pad, size, orientation)
{
	//  Variables
	var loop;
	var padData = '';

	//  Negative check
	if(size < 0)
		size = 0;

	//  Size check
	if(String(dataString).length >= size)
		return(dataString);

	//  Loop through and create the pad
	for(loop = (String(dataString).length); loop < size; loop++)
	{
		if(padData == '')
			padData = String(pad);
		else
			padData = String(padData) + String(pad);
	}

	//  Append the string to the pad
	if(orientation == 'right')
		dataString = String(padData) + String(dataString);
	else
		dataString = String(dataString) + String(padData);

	//  Return the data
	return(dataString);
}

//!  Trim function
/**  This function will trim left and right spaces.  */
function Trim(dataString)
{
	//  Variables
	var start = 0;
	var stop = 0;
	var loop;
	var trimString = '';

	//  Check the length 
	if(dataString.length == 0)
		return(0);
		
	//  Get the start
	for(loop = 0; loop < dataString.length; loop++)
	{
		if(dataString.substring(loop, (loop + 1)) != ' ')
		{
			start = loop;
			break;
		}
	}

	//  Get the end
	for(loop = dataString.length - 1; loop >= 0; loop--)
	{
		if(dataString.substring(loop, loop + 1) != ' ')
		{
			stop = loop + 1;
			break;
		}
	}

	//  Error
	if(stop < start || stop == start || (stop == 0 && start == 0))
		return(trimString);

	//  Get the trimmed substring
	trimString = dataString.substring(start, stop);

	//  Return the trimmed string
	return(trimString);
}

//!  Right Trim function
/**  This function will trim the right.  */
function R_Trim(dataString)
{
	//  Variables
	var start = 0;
	var stop = 0;
	var loop;
	var trimString = '';

	//  Check the length 
	if(dataString.length == 0)
		return(0);
		
	//  Get the end
	for(loop = dataString.length - 1; loop >= 0; loop--)
	{
		if(dataString.substring(loop, loop + 1) != ' ')
		{
			stop = loop + 1;
			break;
		}
	}

	//  Error
	if(stop < start || stop == start || (stop == 0 && start == 0))
		return(trimString);

	//  Get the trimmed substring
	trimString = dataString.substring(start, stop);

	//  Return the trimmed string
	return(trimString);
}

//!  Left Trim function
/**  This function will trim the left.  */
function L_Trim(dataString)
{
	//  Variables
	var start = 0;
	var stop = dataString.length;
	var loop;
	var trimString = '';

	//  Check the length 
	if(dataString.length == 0)
		return(0);
		
	//  Get the start
	for(loop = 0; loop < dataString.length; loop++)
	{
		if(dataString.substring(loop, loop + 1) != ' ')
		{
			start = loop;
			break;
		}
	}

	//  Error
	if(stop < start || stop == start || (stop == 0 && start == 0))
		return(trimString);

	//  Get the trimmed substring
	trimString = dataString.substring(start, stop);

	//  Return the trimmed string
	return(trimString);
}

//!  Help Image Display
/**  This function will display the help button image.  */
function Set_Help_Image()
{
	//  Variables
	var helpObj = '';
	
	//  Get the object reference for the help table spot
	helpObj = document.getElementById('Help_Button');
	
	//  Set the data for the table spot
	helpObj.innerHTML = "<img src=\"../Graphics/help_ico.png\" width=\"48px\" height=\"48px\" align=\"middle\" border=\"0\" onclick=\"Show_Popup('Help_Div', 'Help_Iframe', '25%', '25%', '45%', '45%', 'N')\">";
}

//!  Start the drag and move code
//! Drag Object (One for div, one for Iframe)
var drag1Object = null;
var drag2Object = null;
var dragx = 0;
var dragy = 0;
var posx = 0;
var posy = 0;

//!  Set document event
document.onmousemove = Drag;
document.onmouseup = Drag_Stop;


//!  Start the drag event
function Drag_Start(element1, element2)
{
	if(element1.id == "Help_Div")
	{
		drag1Object = element1;
		drag2Object = document.getElementById(element2);

		dragx = posx - drag1Object.offsetLeft;
		dragy = posy - drag1Object.offsetTop;
	}
}

//!  Stop the drag event
function Drag_Stop()
{
	drag1Object=null;
	drag2Object=null;
}

//!  On drag event
function Drag(event)
{
	posx = document.all ? window.event.clientX : event.pageX;
	posy = document.all ? window.event.clientY : event.pageY;

	if(drag1Object != null && drag2Object != null)
	{
		drag1Object.style.left = (posx - dragx) + "px";
		drag1Object.style.top = (posy - dragy) + "px";
		drag2Object.style.left = (posx - dragx) + "px";
		drag2Object.style.top = (posy - dragy) + "px";
	}
}

//!  Extract variable
/**  This function will extract a single or multiple variables from the table class hidden
	variables.  */
function Extract_Variable(hiddenVar)
{
	//  Variables
	var start;
	var stop;
	var extractedString;
	var varNumber = 0;
	var loop;

	//  Find the number of elements in the array
	for(loop = 0; loop < hiddenVar.length; loop++)
	{
		//  Count the number of commas
		if(hiddenVar.substring(loop, loop + 1) == ',')
			varNumber++;
	}
	
	arraySize = (varNumber + 1)/2;
	
	//  Create an array for the data
	var dataArray = new Array(Math.ceil(arraySize));
		
	//  Loop through the string and get the data
	for(loop = 0; loop < dataArray.length; loop++)
	{ 
		//  Skip over the table index
		if(loop == 0)
			start = 0;
		else
			start = stop + 1;

		stop = hiddenVar.indexOf(",", start);
		
		//  Get the data position
		start = stop + 1;
		stop = hiddenVar.indexOf(",", start);
		
		if(stop < start)
			stop = hiddenVar.length;
			
		//  Parse the data 
		dataArray[loop] = hiddenVar.substring(start, stop);
	}

	//  Return the data array
	return(dataArray);
}

//!  Print the page
/**  This function will open the print page function.  */
function Print_Page()
{
	// Print the page
	window.print();
}


//!  Delay function (milliseconds)
/**  This function will create a delay in milliseconds.  */
function Delay(timeInterval)
{
	//  Variables
	var then;
	var now;

	//  Get current time
	then = new Date().getTime();

	//  Set the current time to now
	now = then;

	//  Loop until gap is met
	while((now-then) < timeInterval)
	{
        	now = new Date().getTime();
	}
}

//!  Disable a page control
/**  This function will disable a control.  */
function Disable_Control(varObj)
{
	if(typeof(varObj) == 'string')
		varObj = document.getElementById(varObj);

	varObj.disabled = true;
}

//!  Enable a page control
/**  This function will enable a control.  */
function Enable_Control(varObj)
{
	if(typeof(varObj) == 'string')
		varObj = document.getElementById(varObj);

	varObj.disabled = false;
}

//!  Function to set a class for an object
/**  This function will set a class for a given object.  */
function Set_Class(objRef, classTitle)
{
	//  Variables
	var varObj;
	
	if(typeof(objRef) == 'string')
	{
		varObj = document.getElementById(objRef);
		varObj.className = classTitle;
	}
	else	
		objRef.className = classTitle;
}

//!  Function to refresh the status frame
/**  This function will refresh the frame for the status bar.  */
function Refresh_Status()
{
	window.top.Status_Frame.document.Status_Form.submit();
}

//!  Page Submit
/**  This function will submit the page and set the page action.  */
function Submit_Page(pageAction, formId)
{
	//  Set the page action
	document.getElementById('Page_Action').value = pageAction;

	//  Submit the page
	document.getElementById(formId).submit();
}

//!  Form Submit
/**  This function will submit the page and set the page action.  */
function Submit_Form(actionName, pageAction, formId)
{
	//  Set the page action
	document.getElementById(actionName).value = pageAction;

	//  Submit the page
	document.getElementById(formId).submit();
}

//!  Table Submit
/**  This function will submit a page if a table item is selected.  */
function Table_Submit(tableNumber, selectType, pageAction, formId)
{
	//  Check the table selection
	if(Check_Table(tableNumber, selectType))
		return(1);

	//  Set the page action
	document.getElementById('Page_Action').value = pageAction;

	//  Submit the page
	document.getElementById(formId).submit();
}

//!  Table Data Submit
/**  This function will verify at a single or multiple selection in the table.  */
function Check_Table(tableNumber, selectType)
{
	//  Variables
	var dataObj;
	var dataValue;
	var dataFlag;
	var loop = 0;
	var count = 0;
	
	//  Verify table object exists
	if(!document.getElementById('Table_Control_Hidden_' + tableNumber + '_0'))
		return(0);
	
	//  Get the table object
	dataObj = document.getElementById('Table_Control_Hidden_' + tableNumber + '_0');
	dataValue = dataObj.value;
	
	//  If nothing is selected, exit
	if(dataValue.length == 0)
	{
		alert('Please select a table item(s)');
		return(1);
	}

	//  Count the number of selections (Count the comma's)
	for(loop = 0; loop < dataValue.length;loop++)
	{
		//  Search for the comma
		dataFlag = dataValue.indexOf(',', loop);
		if(dataFlag == -1)
			break;
			
		//  Increment the loop to the comma index
		loop = dataFlag;
		
		//  Keep track of the count
		count++;
	}
	
	if(selectType == 'Single')
	{
		if(count == 1)
		{
			return(0);
		}
		else
		{
			alert('Only a single item can be selected in the table');
			return(1);
		}
	}
	
	if(selectType == 'Multiple' && count > 1)
	{
		return(0);
	}
}

//!  Create Input Row
/**  Function to create a new row in a table with a label and and input element.  */
function Create_Input_Row(tableObj, elementName, labelHtml, rowIndex)
{
	//  Variables
	var newRow;
	var labelCell;
	var inputCell;
	var newLabel;
	var newInput;

	//  Add a new row to the table
	newRow = tableObj.insertRow(rowIndex);

	//  Add a label cell
	labelCell = newRow.insertCell(0);

	//  Add a input cell
	inputCell = newRow.insertCell(1);

	//  Add an input to the input cell
	newInput = document.createElement("input");

	//  Set the attributes for the input
	newInput.type = "text";
	newInput.id = elementName;
	newInput.name = elementName;

	//  Append the input to the cell
	inputCell.appendChild(newInput);

	//  Add a label to the label cell for the input
	newLabel = document.createElement("label");

	//  Set the attributes for the label
	newLabel.innerHTML = labelHtml;

	//  Append the label to the cell
	labelCell.appendChild(newLabel);
}

//!  Remove HTML
/**  This is a function to remove all html tags from the string.  */
function RemoveHTML( strText )
{
	//  This will remove all tags, but the data should not contain "<" or ">"
	var regEx = /<[^>]*>/g;
	return strText.replace(regEx, "");
}

//!  Show Update
/**  This function will show the provided popup and set the parameters.  
	tableSelect should be 'N', 'S<table number>', 'M<table number>'.  */
function Show_Popup(elementName, iframeName, objTop, objLeft, objWidth, objHeight, tableSelect)
{
	//  Variables
	var elementObj;
	var iframeObj;
	var tableData;
	var tableNumber;
	var tableAction;
	var count = 0;
	var loop = 0;
	
	//  Determine if a table element needs to be selected
	if(tableSelect != 'N')
	{
		tableNumber = tableSelect.substring(1, (tableSelect.length));
		tableData = document.getElementById('Table_Control_Hidden_' + tableNumber + '_0').value;
			
		//  Verify that only one item is selected
		for(loop = 0; loop < tableData.length; loop++)
		{
			if(tableData.substring(loop, loop + 1) == ',')
				count++;
		}
		
		//  Verify an item is selected
		if(count == 0)
		{
			alert('Please select an item from the appropriate table.');
			return(1);
		}
			
		//  Determine if it is a single or multi select
		if(tableSelect.substring(0, 1) == 'S')
		{
			if(count > 1)
			{
				alert('Please select only one row from the table.');
				return(1);
			}
		}
	}
	
	//  Set the popup to the desired location
	elementObj = document.getElementById(elementName);
	iframeObj = document.getElementById(iframeName);
	
	//  Position the div in the center of the screen
	elementObj.style.position = "absolute";
	elementObj.style.top = objTop;
	elementObj.style.left = objLeft;
	elementObj.style.width = objWidth;
	elementObj.style.height = objHeight;
	elementObj.style.zIndex = "1000";
	elementObj.style.display = 'block';
	
	//  Position the iframe under the div
	iframeObj.style.position = "absolute";
	iframeObj.style.top = objTop;
	iframeObj.style.left = objLeft;
	iframeObj.style.width = objWidth;
	iframeObj.style.height = objHeight;
	iframeObj.style.zIndex = "999";
	iframeObj.style.display = 'block';
	
	//  Show the blocks
	iframeObj.style.visibility = "visible";
	elementObj.style.visibility = "visible";
	
	//  Return success
	return(0);
}


//!  Hide Update
/**  This function will hide the update window.  */
function Hide_Popup(elementName, iframeName)
{
	//  Variables
	var elementObj;
	var iframeObj;
	
	//  Get the object references
	elementObj = document.getElementById(elementName);
	iframeObj = document.getElementById(iframeName);
	
	//  Drop the div down
	elementObj.style.zIndex = "0";
	
	//  Hide the iframe in the upper left corner
	iframeObj.style.position = "absolute";
	iframeObj.style.top = "0";
	iframeObj.style.left = "0";
	iframeObj.style.width = "0";
	iframeObj.style.height = "0";
	iframeObj.style.zIndex = "0";
	
	//  Hide the blocks
	iframeObj.style.block = "none";
	iframeObj.style.visibility = "hidden";
	elementObj.style.display = 'none';
	elementObj.style.visibility = "hidden";
	
	//  Return success
	return(0);
}

//!  Page Direct
/**  Redirect the form to the specified page.  */
function Page_Direct(dataForm, pageName)
{
        //  Reset the action
        document.getElementById(dataForm).action = pageName;

        //  Submit the page
        document.getElementById(dataForm).submit();
	//  Return success
	return(0);
}

//!  Set select value
/**  Set the value of a select html element.  */
function Select_Set(elementName, selectValue)
{
	//  Variables
	var selectObj = null;
	var loop = 0;
	
	//  Set the object
	selectObj = document.getElementById(elementName);
	
	if(selectObj)
	{
		while(loop < selectObj.length)
		{
			if(selectObj.options[loop].value == selectValue)
				break;
			loop++;
		}
		selectObj.options[loop].selected = true;
	}
	//  Return success
	return(0);
}


//!  Create Select Control
/**  Create the select control utilizing input data.  */
function Create_Select_Data(elementName, selectData, selectValue, selectedData)
{
	//  Variables
	var selectObj;
	var selectLength;
	var loop;
	var optionDoc;
	
	//  Get the obj
	selectObj = document.getElementById(elementName);
	
	//  Get the length of the select
	selectLength = selectObj.options.length;
	
	//  Delete the options in the object
	for(loop = (selectLength - 1); loop >= 0; loop--)
	{
		//  Remove
		selectObj.options[loop] = null;
	}
	
	//  Add the blank select item
	optionDoc = new Option('', '');
	selectObj.options[0] = optionDoc;
	
	//  Add the remaining items
	for(loop = 0; loop < selectData.length; loop++)
	{
		//  Get Data and Value
		optionDoc = new Option(selectData[loop], selectValue[loop]);
		selectObj.options[(loop + 1)] = optionDoc;
		if(selectedData == selectValue[loop])
			selectObj.options[(loop + 1)].selected = true;
		else
			selectObj.options[(loop + 1)].selected = false;
	}
	
	//  Return success
	return(0);
}






