// ------------------------------------   Modifications  --------------------------------------
//  who   date   description
//  sjf  111805  111804 - added logic to skip any fields returned that do not exist on the screen.
//				 Any field that is on the screen will be updated with the returned value
//  sjf  022105  Fix problem with js_get_data clearing the value if the query does not
//               return a valid value
//  sjf  031405  Fix problem with format_gl_account function
//

function IsEmailValid(EMail) 
{

var EmailOk  = true;
//var Temp     = document.forms[FormName].elements[ElemName]
var AtSym    = EMail.indexOf('@');
var Period   = EMail.lastIndexOf('.');
var Space    = EMail.indexOf(' ');
var Length   = EMail.length - 1;   // Array is from 0 to length-1

if ((AtSym < 1) ||                     // '@' cannot be in first position
    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
    (Period == Length ) ||             // Must be atleast one valid char after '.'
    (Space  != -1))                    // No empty spaces permitted
   {  
      EmailOk = false;
   }
return EmailOk;
}

function UCase(string)
{
	string = string.toUpperCase();
	return string
}

function replaceString(mainString,searchString,replaceString)
{
	var front = getFront(mainString,searchString);
	var back  = getBack(mainString,searchString);
	if ((front != null) && (back != null))
	{
		return front + replaceString + back;
	}
	return null;
}

function getFront(mainString, searchString)
{
	// Extract front part of string prior to search string.
	var foundOffset = mainString.indexOf(searchString);
	if (foundOffset == -1)
	{
		return null;
	}
	return mainString.substring(0, foundOffset);
}

function getBack(mainString, searchString)
{
	// Extract the back end of string following search string.
	var foundOffset = mainString.indexOf(searchString);
	if (foundOffset == -1)
	{
		return null;
	}
	return mainString.substring(foundOffset+searchString.length,mainString.length);
}

function getBackRight(mainString, searchString)
{
	// Extract the back end of string following search string.
	var foundOffset = mainString.lastIndexOf(searchString);
	if (foundOffset == -1)
	{
		return null;
	}
	return mainString.substring(foundOffset+searchString.length,mainString.length);
}

function Rtrim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "Rtrim" function

function Trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "Trim" function

function Pack(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "Pack" function


function IsAlphanumeric(string)
{
	
	string = Trim(string);
	if (string.length > 0)
	{
		var i;
		var character;
		var length = string.length;
		for (i = 0; i < length; i++)
		{
			character = string.charAt(i).toUpperCase();
			if ((character < "0") || (character > "Z"))
			{
				return false;
			}
		}
		return true;
	}
	return false;
}

function ContainsSpaces(string)
{
	if (string.length > 0)
	{
		var i;
		var character;
		var length = string.length;
		for (i = 0; i < length; i++)
		{
			character = string.charAt(i).toUpperCase();
			if (character == " ")
			{
				return true;
			}
		}
		return false;
	}
	return false;
}

function IsNumeric(string)
{
	
	string = Trim(string);
	if (string.length > 0)
	{
		var i;
		var character;
		var length = string.length;
		for (i = 0; i < length; i++)
		{
			character = string.charAt(i).toUpperCase();
			if ((character < "0") || (character > "9"))
			{
				return false;
			}
		}
		return true;
	}
	return false;
}

function IsMoney(string)
{
	
	string = Trim(string);
	if (string.length > 0)
	{
		var i;
		var character;
		var length = string.length;
				
		for (i = 0; i < length; i++)
		{
			character = string.charAt(i);
			if ((character.toUpperCase() < "0") || (character.toUpperCase() > "9"))
			{
				if ((character != ".") && (character != ",")) {
					return false;
				}
			}
		}
		
		//If the first character is a "," then error
		if (string.charAt(0) == ",") {
			return false;
		}
	
				
		//validate decimals (no more than 2)
		value = ((Math.round(string * 1000)/10));
		string = value.toString();
		length = string.length;
		
		for (i = 0; i < length; i++)
		{
			character = string.charAt(i);
			if (character.toUpperCase() == ".")
			{
				return false;
			}
		}
		
		return true;
	}
	return false;
}

function IsRate(string)
{
	
	string = Trim(string);
	if (string.length > 0)
	{
		var i;
		var character;
		var length = string.length;
		var intDec
		
		intDec = 0;
				
		for (i = 0; i < length; i++)
		{
			character = string.charAt(i);
			if ((character.toUpperCase() < "0") || (character.toUpperCase() > "9"))
			{
				if (character != ".") {
					return false;
				}
				else
				{
					intDec = intDec + 1
				}
			}
		}
		
		if (intDec > 1) {
			return false;
		}
		
		//validate decimals (no more than 6)
		//Rounding and then dividing fixes the error with 0.03255 and 0.03251
		var value = new Number;
		value = ((Math.round(string * 10000000)/10));
		string = value.toString();
		length = string.length;

		for (i = 0; i < length; i++)
		{
			character = string.charAt(i);
			if (character.toUpperCase() == ".")
			{
				return false;
			}
		}
		
		//the rate must be less than or equal to 1
		if (value > 1000000) {
			return false;
		}
		
		return true;
	}
	return false;
}


function IsCommission(string)
{
	
	string = Trim(string);
	if (string.length > 0)
	{
		var i;
		var character;
		var length = string.length;
		var intDec
		
		intDec = 0;
				
		for (i = 0; i < length; i++)
		{
			character = string.charAt(i);
			if ((character.toUpperCase() < "0") || (character.toUpperCase() > "9"))
			{
				if (character != ".") {
					return false;
				}
				else
				{
					intDec = intDec + 1
				}
			}
		}
		
		if (intDec > 1) {
			return false;
		}
		
		//validate decimals (no more than 2)
		//Rounding and then dividing fixes the error with 0.03255 and 0.03251
		var value = new Number;
		value = ((Math.round(string * 1000)/10));
		string = value.toString();
		length = string.length;

		for (i = 0; i < length; i++)
		{
			character = string.charAt(i);
			if (character.toUpperCase() == ".")
			{
				return false;
			}
		}
		
		//commission cannot be greater than 100%
		if (value > 10000) {
			{
				return false;
			}
		}			
				
		return true;
	}
	return false;
}

function IsDate(field)
{
	Trim(field);
	var inDate = field;
	
	// Parse the date string to create a new Date object.
	var chkDate=new Date(Date.parse(inDate));
	if (chkDate == null)
	{
		return false;
	}
	
	// Check the gross format of the date; it must be one of the
	// following formats:
	//		mm/dd/yyyy
	//		mm-dd-yyyy
	//		mmddyyyy
	var slash1 = inDate.indexOf("/");
	var slash2 = inDate.lastIndexOf("/");
	
	if (slash1 == -1)
	{
		// There is only one delimiter in the string.
		return false;
	}

	
	if ((slash1 != -1) && (slash1 == slash2))
	{
		// There is only one delimiter in the string.
		return false;
	}
	
	var mm;
	var dd;
	var dateString;
	var yearString;
	var ccyy;
	var yy;
	var cc;
	// Tear apart the date into components for month, day, and year.
	// Use base 10 conversions in the parseInt() functions, because
	// the components may begin with leading zeroes.

	// There are slashes; extract component values accordingly.
	mm			= parseInt(inDate.substring(0,slash1),10);
	dd			= parseInt(inDate.substring(slash1+1,slash2),10);
	dateString	= inDate.substring(0,slash1) + 
				  inDate.substring(slash1+1,slash2) + 
				  inDate.substring(slash2+1,inDate.length);
	yearString =  inDate.substring(slash2+1,inDate.length);				  
	ccyy		= parseInt(inDate.substring(slash2+1,inDate.length),10);
	cc			= parseInt(inDate.substring(slash2+1,slash2+3),10);
	yy			= parseInt(inDate.substring(slash2+3,inDate.length),10);
	
	// Check if any of the date components are not a number (NaN).
	
	if (isNaN(dateString))
	{
		return false;
	}
		
	// Check that a four-digit year has been entered.
	if (yearString.length != 4)
	{
		return false;
	}
	
	// Check if the month is valid.
	if (mm < 1 || mm > 12)
	{
		return false;
	}
	
	if (ccyy < 1800) 
	{
		return false;
	}
	
	// Check if the day of the month is valid.
	if (dd.toString() == "NaN") {
		return false;
	}
	
	if (dd < 1 || dd > 31)
	{
		return false;
	}
	else
	{
		if (((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11)) && (dd > 30))
		{
			return false;
		}
		else
		{
			if (mm == 2)
			{
				if (dd > 29)
				{
					return false;
				}
				else
				{
					// For years like 1800, 1900, 2000, etc. to be leap
					// years, the century must be divisible by 400, i.e.,
					// the years 1600 and 2000 are leap years, but the
					// years 1700, 1800, and 1900 are not.
					if ((yy == 0) && (cc % 4 > 0) && (dd > 28))
					{
						return false;
					}
					else
					{
						// Other years must be divisible by 4 to be leap
						// years.
						if ((yy % 4 > 0) && (dd > 28))
						{
							return false;
						}
					}
				}
			}
		}
	}
	//return true;
	
	var strMM;
	var strDD;
	
	if (mm < 10) {
		strMM = "0" + mm.toString();
	}
	else {
		strMM = mm.toString();
	}
		
	if (dd < 10) {
		strDD = "0" + dd.toString();
	}
	else {
		strDD = dd.toString();
	}

	return ccyy + "/" + strMM + "/" + strDD;
	
}


function IsMilitaryTime(timeStr) {
	// Checks if time is in HH:MM:SS format.
	
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	
	var matchArray = timeStr.match(timePat);
	if (matchArray == null) 
	{
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];
	
	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }
	
	if (hour < 0  || hour > 23) 
	{
		alert("Hour must be between 0 and 23 for military time");
		return false;
	}
	if (minute<0 || minute > 59) 
	{
		alert ("Minute must be between 0 and 59.");
		return false;
	}
	if (second < 0 || second > 59) 
	{
		alert ("Second must be between 0 and 59.");
		return false;
	}
	return true;
}
//  End -->



function IsOptionSelected(objElement) {

	for (var i=0; i < objElement.length; i++) {
		if (objElement.options[i].selected) {
			var intIndex = i;
			break;
		}
	}
	if (objElement.options[intIndex].value == "") {
		return false;
	}
	
	return true;

}

// This function will allow only numbers, periods and the enter key
function numeric_only(myfield,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	
	if (((keycode>44) && (keycode<58) )  || (keycode==8) ||(keycode==13))  { return true; }
	else 
	{
//		alert("keycode = " + keycode);
		alert("Only numeric data is allowed");
		return false;
	}
}

function upshift(value)
{
	value = value.toUpperCase();
	return value;
	
}

function textarea_size(myfield, size)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13)
		return;

	var field_value = myfield.value;
	
	if (field_value.length > size - 1)
	{
		alert("The maximum size of this field is " + size + ".");
		myfield.value = field_value.slice(0,size);
		return(false);
	}
}

// auto advance to the next field when the maximum number of characters have been entered
  var field_length=0;

function auto_advance(myfield, event, size, focus_field)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13)
		return;
	if (keycode == 9) // tab
		return;
  if (keycode == 16) // shift-tab was causing a problem
		return;

	if (event == "down")
  {
		field_length=myfield.value.length;
	}
	else if (event == "up")
 {
		if (myfield.value.length != field_length)
    {
			field_length=myfield.value.length;
			if (field_length == size) {
				focus_field.focus();
		  }
		}
	}
}

// This function will call the cmtgetdata program and which returns an XML string
//  The string returned contains one or more fields and values.  For each pair found
// the value is displayed on the screen.
// The function accepts four parameters as follows:
// query - the query to be executed.  Note: the query must return only one record or it will not work correctly.
// database - the name of the database the query is to be excuted against.
// error_focus_field - this is where the cursor will be placed if an error occurs.  If the value is blank
//                     then the cursor is placed in the first field returned by the query.  
// override_msg - this is the error message that displays in the alert message.  If this is blank then a
//                message containing the name of the first field in the query is displayed.
//
//
function js_get_data(query, database, error_focus_field, override_msg, return_the_message)
{
//	offsetInfo = "The srcElement name is: " + window.event.srcElement.name + "\r";
//	offsetInfo += "The srcElement type is: " + window.event.srcElement.type + "\r";
//	offsetInfo += "The srcElement tagName is: " + window.event.srcElement.tagName + "\r";
//	offsetInfo += "The type is: " + window.event.type + "\r";
//    alert(offsetInfo);

//	if (window.event.toElement)
//	{
//		offsetInfo = "The toElement name is: " + window.event.toElement.name + "\r";
//		offsetInfo += "The toElement type is: " + window.event.toElement.type + "\r";
//		offsetInfo += "The toElement tagName is: " + window.event.toElement.tagName + "\r";
//		offsetInfo += "The type is: " + window.event.type + "\r";
//	    alert(offsetInfo);
//	}
	
	// Since the variable return_the_message is optional, it may not have been defined by the user
	if (typeof return_the_message == "undefined") 
	{
		var return_the_message = "N";
	}
	
	// If the cursor is currently in one of the following areas then we do not need to run this
	// function.  Since the function is run from a ondeactivate event, any time the cursor is moved
	// from the field this function is triggered.  The following allows us to still do right click or
	// left click on the toolbar.
	if (window.event.toElement)
	{
		if (window.event.toElement.tagName == "BODY" || 
		    window.event.toElement == "javascript:void();" ||
//			window.event.toElement.tagName == "IMG" ||
			window.event.toElement.tagName == "DIV" || 
//			window.event.toElement.tagName == "A" ||
			window.event.toElement.tagName == "TD")
		{
	//		alert ("js_get_data: leaving");
			return null;
		}
		if (window.event.toElement.tagName == "A" ||
		    window.event.toElement.tagName == "IMG")
		{
			switch  (window.event.toElement.name)
			{
				case "magnifying_glass":   	        //022105 - sjf
				    return true;                    //022105 - sjf
				    break;                          //022105 - sjf
				case "ok_icon":   	
				case "add_icon":   	
				case "update_icon":   	
				case "cancel_icon":   	
				case "batch_icon":   	
				case "spooler_icon":   	
				case "calculator_icon":   	
					break;		//continue processing		
				default:
					return null;
			}
		}
	}
	
	// Change the cursor to an hourglass
//	document.body.style.cursor = 'wait';
	
	//Create an instance of XML HTTP Request Object
	var oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");

	
	//Prepare the XMLHTTP object for a HTTP Post to our validation page
	var sURL = "/stw_php/stwcm/cmtgetdata.php?url_query=" + query + "&url_database=" + database;

//	alert("validate:sURL = " + sURL);
	
	oXMLHTTP.open("POST",sURL,false);
	
	//Execute the request
	oXMLHTTP.send();
	
	// What is returned from the screen cmtgetdata will be XML.  To process the XML we have
	// to the XMLDOM activeX.
	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
	xmlDoc.async="false"; 
	
//	alert("validate:response = " + oXMLHTTP.responseText);

	// The value of the XML will be in the oXMLHTTP.responseText variable.  We are loading that into
	// the XMLDOC so we can process all the XML nodes
	xmlDoc.loadXML(oXMLHTTP.responseText); 
	
	// Start at the beginning of the XML	
 	xmlObj=xmlDoc.documentElement; 
	
//	alert("Validation:Root is " + xmlObj.tagName);

	// if the value of the root node is "error" then the SQL query generated an error condition
	
	
	
	if (xmlObj.tagName == "error")
	{
		alert(xmlObj.xml);
//		document.body.style.cursor='auto';

		if (error_focus_field > '')
		{
			var field = document.getElementById(error_focus_field);
		}
		return null;
	}

	// if the value of the root node is "not_found" then the SQL query did not find a
	// record.  We need to display the error message.  If there is an override_msg
	// then use that message, otherwise create an error using the first field in
	// the select statement.
	if (xmlObj.tagName == "not_found")
	{

		// Add a field on the screen to specify the error message has been cleared
		
		if (!document.all.item("skip_msg"))
		{
			var input = document.createElement("INPUT");
			input.name = "skip_msg";
			input.type = "hidden";
			input.size = "1";
			input.id = "skip_msg";
			var form = document.getElementById(document.forms[0].name);
			form.appendChild(input);
			document.forms[0].skip_msg.value = "N";
		}
		
//		alert ("skip_msg = " + document.forms[0].skip_msg.value);

		// If we have already displayed the message, don't display it again
		if (document.forms[0].skip_msg.value == "N")
		{	
			if (return_the_message == "Y")
			{
				if (override_msg > '')
				{
					return override_msg;
				}
				else
				{
					var msg = "The " + xmlObj.childNodes(0).tagName + " was not found.";
					return msg;
				}
			}
			else
			{
				if (override_msg > '')
				{
					alert(override_msg);
				}
				else
				{
					alert("The " + xmlObj.childNodes(0).tagName + " was not found.")
				}
			}
			
	
			// If there is a value for error_focus_field, put the cursor in that field
			// otherwise use the first field in the select statement		
			
//			alert("validation: error_focus_field = " + error_focus_field);
			if (error_focus_field > '')
			{
				var field = document.getElementById(error_focus_field);
				var field_name = error_focus_field;                             //022105
			}
			else
			{
				var field = document.getElementById(xmlObj.childNodes(0).tagName);
				var field_name = xmlObj.childNodes(0).tagName                   //022105
			}
			
			
			// --- added 102004 - sjf
			// Even if the record is not returned, we have all the fields requested, so display them so if
			// anything is on the screen, the fields will be cleared.
			for (i = 0; i < xmlObj.childNodes.length; ++i)
			{
					var field_exists = document.getElementById(xmlObj.childNodes(i).tagName);		//111804	
					if (field_exists )   															//111804
					{
						if (xmlObj.childNodes(i).tagName != field_name)                             //022105
						{
							var field_value = "document.forms[0]." + xmlObj.childNodes(i).tagName + ".value='" + xmlObj.childNodes(i).text + "'";
							eval(field_value);
						}
					}
			}

			document.forms[0].skip_msg.value = "Y";
			field.focus();
		}
	}
	else
	{
		// If we get here then the select returned valid data.  We will loop through each field (childNodes)
		// and for each field we will set the screen field to that value.

		//set each variable
		for (i = 0; i < xmlObj.childNodes.length; ++i)
		{
			// 112904 - have to check if the field value has a single quote in it.  If it does then "escape" it.
			
			 var field_value = xmlObj.childNodes(i).text;
			 var single_quote_pos = field_value.indexOf("'");
			 if (single_quote_pos > 0)
             {
             	var re = new RegExp ("\'", "gi") ; // string to search for, 2nd is always "gi"???
                var new_field_value = field_value.replace(re, "\\'") ;
             }
             else
            {
            	var new_field_value = field_value;
            }

			var field_exists = document.getElementById(xmlObj.childNodes(i).tagName);		//111804	
			if (field_exists)   															//111804
			{
				var eval_field_value = "document.forms[0]." + xmlObj.childNodes(i).tagName + ".value='" + new_field_value + "'";
				eval(eval_field_value);		                     
			}
		}
	}	
	return null;
}


function format_gl_account(account_number)
{
	var acct = Trim(account_number);
	
	if (acct.length == 0)
	{
		return account_number;
	}
	
	if (window.event.toElement)
	{
		if (window.event.toElement.tagName == "BODY" || 
		    window.event.toElement == "javascript:void();" ||
			window.event.toElement.tagName == "DIV" || 
			window.event.toElement.tagName == "TD")
		{
//			return null;
			return account_number;
		}
		if (window.event.toElement.tagName == "A" ||
		    window.event.toElement.tagName == "IMG")
		{
			switch  (window.event.toElement.name)
			{
				case "magnifying_glass":   	
				case "ok_icon":   	
				case "add_icon":   	
				case "update_icon":   	
				case "cancel_icon":   	
				case "batch_icon":   	
				case "spooler_icon":   	
				case "calculator_icon":   	
					break;		//continue processing		
				default:
//                  return null;
					return account_number;
			}
		}
	}
	
	//Create an instance of XML HTTP Request Object
	var oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	
	// Get data from the gl_constants file
	query = "select fund_no_size from gl_constants where gl_key = 'GL'";
	database = "stwgldb";

	//Prepare the XMLHTTP object for a HTTP Post to our validation page
	var sURL = "/stw_php/stwcm/cmtgetdata.php?url_query=" + query + "&url_database=" + database;

	oXMLHTTP.open("POST",sURL,false);
	
	//Execute the request
	oXMLHTTP.send();
	
	// What is returned from the screen cmtgetdata will be XML.  To process the XML we have
	// to the XMLDOM activeX.
	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
	xmlDoc.async="false"; 
	
	// The value of the XML will be in the oXMLHTTP.responseText variable.  We are loading that into
	// the XMLDOC so we can process all the XML nodes
	xmlDoc.loadXML(oXMLHTTP.responseText); 
	
	// Start at the beginning of the XML	
 	xmlObj=xmlDoc.documentElement; 
	
	var fund_no_size = xmlObj.childNodes(0).text
	
	// Get the data from the related fund	

	var fund_no = account_number.substring(0, fund_no_size);

	query = "select account_object_size, account_dept_size, account_prog_size, " +
			"account_proj_size, account_proj_cat_size, account_user1_size, account_user2_size, account_user3_size " +
		    " from gl_funds where fund_no = " + fund_no;
	database = "stwgldb";
	
	//Prepare the XMLHTTP object for a HTTP Post to our validation page
	var sURL = "/stw_php/stwcm/cmtgetdata.php?url_query=" + query + "&url_database=" + database;

	oXMLHTTP.open("POST",sURL,false);
	
	//Execute the request
	oXMLHTTP.send();
	
	xmlDoc.loadXML(oXMLHTTP.responseText); 
	
	// Start at the beginning of the XML	
 	xmlObj=xmlDoc.documentElement; 
	
	// if the value of the root node is "error" then the SQL query generated an error condition
	
	if (xmlObj.tagName == "error")
	{
		alert(xmlObj.xml);
		if (error_focus_field > '')
		{
			var field = document.getElementById(error_focus_field);
		}
		return null;
	}

	// if the value of the root node is "not_found" then the SQL query did not find a
	// record.  We need to display the error message.  If there is an override_msg
	// then use that message, otherwise create an error using the first field in
	// the select statement.
	if (xmlObj.tagName == "not_found")
	{
		alert("The fund associated with this account was not found. - " + fund_no)
		return account_number;
//		field.focus();
	}
	else
	{
		// If we get here then the select returned valid data.  We will loop through each field (childNodes)
		// and for each field we will set the screen field to that value.

		//set each variable
		for (i = 0; i < xmlObj.childNodes.length; ++i)
		{
			var field_value = xmlObj.childNodes(i).text;
			var field_name = xmlObj.childNodes(i).tagName;
			
			// Create a variable for each field returned
			var new_var = "var " + field_name.toLowerCase() + " = " + field_value;
			eval(new_var);
		}
	}	
	
	var dummy_dept = "000";
	var dummy_prog = "000";

	var account_size_without_prog = parseInt(fund_no_size) +
	                   				parseInt(account_object_size) +
					   				parseInt(account_dept_size);
									
	var fund_object_size = parseInt(fund_no_size) +
	                   	   parseInt(account_object_size);
						   

	var fund_obj_dept_prog_size = parseInt(fund_no_size) + 
							      parseInt(account_object_size) +
							      parseInt(account_dept_size) +
							      parseInt(account_prog_size);

	var account_number_size = account_number.length;						   
	var new_account;
	
	if (fund_obj_dept_prog_size > account_number_size)
	{
		if (account_dept_size > 0)
		{
			new_account = Trim(account_number) + dummy_dept.substring(0, account_dept_size);
		}
		
		account_number_size = new_account.length;   //031405 - sjf
		if (account_prog_size > 0 && account_size_without_prog == account_number_size)
		{
			new_account = new_account + dummy_prog.substring(0, account_prog_size);
		}
	}
	else
	{
		new_account = account_number.toUpperCase();
    }
    return new_account;
} // end of function format_gl_account
