// DECLARE VARIABLES
var busUnit, acct, fund, dept, prgm, grant, classif;

function getValues()	{
// GRAB THE VALUES FOR THE CHARTFIELD
	busUnit = document.theForm.business.value;
	acct    = document.theForm.Account.value;
	fund    = document.theForm.Fund.value;
	dept    = document.theForm.Dept.value;
	prgm    = document.theForm.Program.value;
	grant   = document.theForm.Grant.value;
	classif = document.theForm.classif.value;
}

function calcValue()	{
	if ( finalCheck() )	{
		var finalCode = busUnit +'-'+ acct +'-'+ fund +'-'+ dept +'-'+ prgm +'-'+ grant +'-'+ classif;

		for ( var i=0; i <2; i++ )				//  TRIMS DOUBLE HYPHENS
			if ( finalCode.indexOf(/--/) == -1 )
				finalCode = finalCode.replace(/--/, /-/);
		
		document.theForm.ChartField_Value.value = finalCode;
		document.theForm.Submit.disabled = '';
	}
	else
		document.theForm.ChartField_Value.value = 'Invalid Code.  Please check form fields.';
}


// SETS THE FORM BACK TO DEFAULT DISABLING SUBMIT

function resetForm()	{
	document.theForm.Submit.disabled = 'true';
}

//  CHECKS REQUIRED VALUES BEFORE ALLOWING SUBMIT
function finalCheck()	{
	getValues();		// GETS MOST RECENT VALUES
	
	if (busUnit == '' || fund == '' || dept == '' || dept == 'D#####' || dept.length < 6)	{
		alert('One or more fields have incorrect values.');
		return false;
	}
	else if ( dept.charAt(0) != 'D' )	{
		alert('First character must be a "D".  Remaining characters must be numeric.')
		document.theForm.Dept.value = '';
		document.theForm.Dept.focus();
		return false;
	}
	else
		return true;
}


function disableFields(objForm)	{
	document.theForm.business.disabled = 'true';
	document.theForm.Account.disabled  = 'true';
	document.theForm.Fund.disabled     = 'true';
	document.theForm.Dept.disabled     = 'true';
	document.theForm.Program.disabled  = 'true';
	document.theForm.Grant.disabled    = 'true';
	document.theForm.classif.disabled  = 'true';
	
	return true;
}


function checkLen(obj,len)	{
	if ( obj.value.length < len )	{
		alert('Insufficient value:  \"'+ obj.value +'\"\nValue must be '+ len +' characters long.' );
		obj.value = '';
		obj.focus();
	}
		
		
}