/************************************************************************** 
*************************************************************************** 
*  Program Name: $Id: common.js,v 1.20 2007/08/25 23:21:53 neo Exp $
*  Program Author:  Michael T. Schock
*  Creation Date: 05-15-2006
*  CVS Revision: $Revision: 1.20 $
*  Copyright (c) 2006
*************************************************************************** 
*************************************************************************** 
*  Program Summary:
*  Javascript functions common to all pages
*
*
*************************************************************************** 
**************************************************************************/

//!  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 == 'left')
		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 = '';

	//  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 = '';


	//  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 = '';


	//  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);
}

//!  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();
	}
}

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

//!  Change form source
/**  Change source destination of the form.  */
function Change_Action(pageSrc, formId)
{
	document.getElementById(formId).action = pageSrc;
}

//!  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();
}

//!  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.  */
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(2, (tableSelect.length-2));
		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, 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";
	
	//  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";
	
	
	
	//  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.visibility = "hidden";
	elementObj.style.visibility = "hidden";
	
	//  Return success
	return(1);
}

//!  XML Page Variable
/**  Set the page variable for ajax requests.  */
var ajaxRequest;

//!  Load XML request
/**  Load the xml request object.  */
function Load_XML_Doc(url, processFunction)
{
	if(window.XMLHttpRequest)
	{
		ajaxRequest = new XMLHttpRequest();
		ajaxRequest.onreadystatechange = processFunction;
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null);
	}
	else if(window.ActiveXObject) //  Windows activeX request
	{
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if(ajaxRequest)
		{
			ajaxRequest.onreadystatechange = processFunction;
			ajaxRequest.open("GET", url, true);
			ajaxRequest.send(null);
		}
	}
}


//!  Ajax function
/**  Get the ajax data.  */
function Process_Request_Change()
{
	if(ajaxRequest.readyState == 4)
	{
		if(ajaxRequest.status == 200)
		{
			response = ajaxRequest.responseXML;
			Parse_Data(response);
		}
		else
		{
			alert("There was a problem retrieving the XML data:\n" + ajaxRequest.statusText);
		}
	}
}


//!  Convert Password for encryption
/**  Use the MD5 Hash Algorithm.  */
function Convert_Password(pageForm, referPage)
{
	//  Convert and restore the password
	document.getElementById('Password').value = calcMD5(document.getElementById('Password').value);
	
	//  Store the referring page
	document.getElementById('Refer_Page').value = referPage;
	
	//  Submit the page
	document.getElementById(pageForm).submit();
}

//!  Verify password fields and submit form
function Verify_Form(formName)
{
	//  Verify the form is complete
	if(document.getElementById('New_User').value == '')
	{
		alert('Username not filled in.  Please enter this value.');
		return 1;
	}
	if(document.getElementById('New_Name').value == '')
	{
		alert('Full Name not filled in.  Please enter this value.');
		return 1;
	}

	if(document.getElementById('New_Mail').value == '')
	{
		alert('Email not filled in.  Please enter this value.');
		return 1;
	}

	if(document.getElementById('New_Pass').value == '')
	{
		alert('Password not filled in.  Please enter this value.');
		return 1;
	}

	if(document.getElementById('New_Pass_Confirm').value == '')
	{
		alert('Password Confirmation not filled in.  Please enter this value.');
		return 1;
	}
	
	if(document.getElementById('New_Pass').value != document.getElementById('New_Pass_Confirm').value)
	{
		alert('Password and Password confirmation do not match.  Please try again.');
		return 1;
	}
	
	if(document.getElementById('New_Pass').value.length < 6)
	{
		alert('Password  not correct length.  Password length must be at least 6 characters.');
		document.getElementById('New_Pass').value = "";
		document.getElementById('New_Pass_Confirm').value = "";
		return 1;
	}

	//  Encrypt Password
	document.getElementById('New_Pass').value = calcMD5(document.getElementById('New_Pass').value);
	document.getElementById('New_Pass_Confirm').value = calcMD5(document.getElementById('New_Pass_Confirm').value);
	
	//  Submit the page
	Submit_Page('Add User', formName);
}


