var sErrorMsg = ''; //Error message buffer
//var myFloater;
var partWindow;

function errInteger(Field,FieldName,bZeroAllowed)
{
    // strip off leading zeros
    if (Trim(Field.value) > '')
    {
        var x = Field.value;
        if (!bZeroAllowed && IsZero(x))
        {
	        addToErrorMsg(FieldName + ' cannot be zero.');
		    Field.select();  //Select field in error
        }
        else
        {
            while (x.substring(0,1) == '0') x = x.substring(1);
            Field.value = x;
            if (("" + parseInt(Field.value)) != Field.value)
            {
	            addToErrorMsg(FieldName + ' must be a numeric integer.');  //Write error message to buffer
	            Field.select();        //Select field
	        }
	    }
	}
}

function errTextRequired(Field,FieldName)
{
	if (Trim(Field.value) == '')           //If required field not present
	{
		Field.focus();                     //Set focus to missing field
		addToErrorMsg(FieldName + ' is required.');  //Write error message to buffer
	}
}

function errFieldsDoNotMatch(Field1,Field2,Field1Name,Field2Name)
{
	var sField1 = Field1.value;
	var sField2 = Field2.value;
	if (sField1 != sField2)  //If fields do not match
	{
		Field1.select();    //Set focus to first field
		addToErrorMsg(Field1Name + ' and ' + Field2Name + ' do not match.  Please reenter.');  //Write error message to buffer
	}
}

function errSelectRequired(Field,FieldName)
{
	if (Field.selectedIndex == 0)
	{
		addToErrorMsg('Please make a selection from the ' + FieldName + ' list.');
		Field.focus();                   //Set focus to missing field
	}
}

function errLengthCheck(Field,FieldName,maxLength)
{
	if (Field.value.length > maxLength)
	{
		addToErrorMsg('The ' + FieldName + ' field exceeds the maximum length of ' + maxLength + ' characters.');
		Field.focus();      //Set focus to field in error
	}
}

//================================================================
// function crossFieldCheck(sRequiredField,sDependentField,sRequiredLabel,sDependentLabel)
// If sDependent field is non-blank, then sRequired field is required.
// If sRequired field is non-blank, then sDependent field is required.
//================================================================
function crossFieldCheck(sRequiredField,sDependentField,sRequiredLabel,sDependentLabel)
{
	if (Trim(sDependentField.value) == '' && Trim(sRequiredField.value) != '')
	{
		addToErrorMsg(sDependentLabel + ' is required when ' + sRequiredLabel + ' is non-blank.');
		sDependentField.focus();                   //Set focus to missing field
	}
	else
	{
		if (Trim(sDependentField.value) != '' && Trim(sRequiredField.value) == '')
		{
			addToErrorMsg(sRequiredLabel + ' is required when ' + sDependentLabel + ' is non-blank.');
			sRequiredField.focus();                   //Set focus to missing field
		}
	}
}


//================================================================
// function errRadioRequired(Field,Fieldname)
// Returns true and formats error msg if no radio button is selected.
// Field must be in the format: 'document.thisForm.elements[i]' 
// where i is the element number in the field collection
//================================================================
function errRadioRequired(Field,FieldName)
{
	if (!getRadioValue(Field.name) > '')
	{
	    addToErrorMsg('Please make a selection from the ' + FieldName + ' buttons.');
		Field.focus();                   //Set focus to missing field
	}
}

function errCheckboxRequired(theForm,ErrorMsg)
{
	var iCount = 0; //Stores number of checked fields found
    for (i=0,n=theForm.elements.length;i<n;i++)
    {
        if (theForm.elements[i].checked)
        {
            iCount++;
        }
    }
    
	if (iCount == 0)
	{
	    addToErrorMsg(ErrorMsg);
	}
}

function errInvalidEmail(Field,FieldName)
{
    if (Field.value > '')
    {
	if (!isEmail(Field.value))
	{
	    addToErrorMsg('Please enter a valid ' + FieldName + '.');
		Field.focus();                   //Set focus to missing field
	}
    }
}
function IsZero(num)
{
    if (num == 0)
        return true;
    else
        return false;
}

//************************************************************************
//	Function:	errCurrency
//	Purpose:	Formats a textfield from numeric to currency ($xx.xx)
//	Developer:	John malone
//	Updated:	03/04/2002 by John Malone
//	Input:		Field        (Field object)
//              FieldName    (string representing the Field Caption)
//              bZeroAllowed (boolean: true if zero is allowed)
//	Output:		Formatted currency field
//  Error    :  Alert user if field cannot be formatted
//	Notes:		
//************************************************************************
function errCurrency(Field,FieldName,bZeroAllowed)
{
    var num = Field.value;
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
	{
	    addToErrorMsg(FieldName + ' must be a valid currency amount.');
		Field.select();  //Select field in error
	}
	else
	{
	    if (!bZeroAllowed && IsZero(num))
	    {
    	    addToErrorMsg(FieldName + ' cannot be zero.');
    		Field.select();  //Select field in error
	    }
	    else
	    {
	        Field.value = formatCurrency(num);
        }
    }
}

function formatCurrency(num)
{
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
	{
        cents = "0" + cents;
	}
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	{
        num = num.substring(0,num.length-(4*i+3))+','+
        num.substring(num.length-(4*i+3));
	}
    return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function addToErrorMsg(sMsg)
{
	sErrorMsg = sMsg + '\n' + sErrorMsg
}

function editForm(sPage,sOption)
{
	var sFunction = sPage + '(' + sOption + ')';
	return eval(sFunction);
}

function editResults()
{
	if (sErrorMsg > '')	
	{
		alert(sErrorMsg);
		sErrorMsg = '';
		return false;
	}
	else
	{
		return true;
	}
}

//Check if string is a valid email address
function isEmail(string)
{
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}

//*******************************************
// Gets the value of passed radiobutton     *
//*******************************************
function getRadioValue(radioName)
{
	var collection;
	collection = document.all[radioName];
//debug alert(radioName)
	for (i=0; i<collection.length; i++)
	{
//debug alert(collection[i].name + ' : ' + collection[i].value + ' : ' + collection[i].checked);
		if (collection[i].checked) return(collection[i].value);
	}
}

//****************************************************************
function Trim(str)
/****************************************************************
	PURPOSE: Remove trailing and leading blanks from our string.
	     IN: str - the string we want to Trim
     RETVAL: A Trimmed string!
       NOTE: Requires LTrim and RTrim
****************************************************************/
	{
		return RTrim(LTrim(str));
	}

//***************************************************************
function LTrim(str)
/****************************************************************
    PURPOSE: Remove leading blanks from our string.
         IN: str - the string we want to LTrim
     RETVAL: An LTrimmed string!
****************************************************************/
    {
        var whitespace = new String(" \t\n\r");

        var s = new String(str);

        if (whitespace.indexOf(s.charAt(0)) != -1)
		{
            // We have a string with leading blank(s)...

            var j=0, i = s.length;

            // Iterate from the far left of string until we
            // don't have any more whitespace...
            while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                j++;


            // Get the substring from the first non-whitespace
            // character to the end of the string...
            s = s.substring(j, i);
        }

        return s;
    }
    
//****************************************************************
function RTrim(str)
/****************************************************************
    PURPOSE: Remove trailing blanks from our string.
         IN: str - the string we want to RTrim
     RETVAL: An RTrimmed string!
****************************************************************/
    {
        /****************************************************
		// We don't want to trip JUST spaces, but also tabs,
        // line feeds, etc.  Add anything else you want to
        // "trim" here in Whitespace
        ****************************************************/
           var whitespace = new String(" \t\n\r");

           var s = new String(str);

           if (whitespace.indexOf(s.charAt(s.length-1)) != -1)
		   {
               // We have a string with trailing blank(s)...

               var i = s.length - 1;       // Get length of string

               // Iterate from the far right of string until we
               // don't have any more whitespace...
               while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                   i--;


               // Get the substring from the front of the string to
               // where the last non-whitespace character is...
               s = s.substring(0, i+1);
           }

           return s;
    }

function openWindow(fileName,options)
{
     myFloater = window.open('','myWindow',options)
     myFloater.location.href = fileName;
}

//************************************************************************
//	Function:	POPUP Calendar Functions
//	Purpose:	Pops up a new window to select a date
//	Notes:		
//************************************************************************
//popup calendar functions
function y2k(number)    { return (number < 1000) ? number + 1900 : number; }
function padout(number) { return (number < 10) ? '0' + number : number; }

var today = new Date();
var day = today.getDate(), month = today.getMonth(), year = y2k(today.getYear()), whichOne = 'date1';

function calrestart()
{
    window.document.thisForm.elements[whichOne].value = '' + padout(month - 0 + 1) + '/' + padout(day) + '/' + year;
    mywindow.close();
}

function newCalWindow(number)
{
    whichOne = number;
    day = today.getDate(), month = today.getMonth(), year = y2k(today.getYear());
    mywindow=open('include/cal.htm','Calendar','resizable=no,width=350,height=270');
}
//************************************************************************
//End of popup calendar functions
//************************************************************************

    
//************************************************************************
//	Function:	newWindow
//	Purpose:	Creates a new browser window
//	Developer:	Matt Kaiser
//	Updated:	05/07/2001 by John Malone
//	Input:		target (page), type (new or self), options (browser window parameters)
//	Output:		none
//	Notes:		
//************************************************************************
var sWindowName;
var plReport;
var ncsReport;
function newWindow(target, type, options) {
	if (type == 'self' && opener.sWindowName > '')
	{
		wID = opener.sWindowName;
	}
	else
	{
		var iRand = Math.round(Math.random()*100000);
		var wID = "rmVB" + iRand;
	}
	
	//save the name of this window so if this function is called with self,
	//we can open the new target in the same window
	sWindowName = wID;
	
	var pointer;
	if (options == null)
	{
		//defaults:
		options = 'channelmode=no,directories=no,fullscreen=no,height=400,left=100,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=no,top=100,width=400';
	}
	pointer = self.open(target, wID, options);
	//pointer.focus();
}
//************************************************************************
//	End Function:	newWindow
//************************************************************************

//************************************************************************
//	Function:	SetChecked
//	Purpose:	Checks or unchecks all checkboxes with the passed name
//	Developer:	John Malone
//	Updated:	02/21/2002 by John Malone
//	Input:		val: 0 = Uncheck, 1 = check
//              chkboxName: the name of the checkbox(es)
//	Output:		none
//	Notes:		
//************************************************************************    
function SetChecked(val,chkboxName)
{
	theForm = document.thisForm;
    for (i=0,n=theForm.elements.length;i<n;i++)
        if (theForm.elements[i].name.indexOf(chkboxName) !=-1)
            theForm.elements[i].checked = val;
}    


//************************************************************************
//	Function:	doDateCheck(from, to)
//	Purpose:	Checks that from date <= to date
//	Developer:	John Malone
//	Updated:	02/26/2002 by John Malone
//	Input:		from: date
//              to  : date
//	Output:		none
//	Notes:		
//************************************************************************    
function doDateCheck(from, to) {
   if (Date.parse(from.value) <= Date.parse(to.value)) {
      return true;
   }
   else {
      if (from.value == "" || to.value == "") {
         alert("If one date is entered the other must also be entered.");
         return false;
      }
      else {
         alert("To date must occur after the from date.");
         return false;
      }
   }
}

function colorChange(thisObject,sColor,sCursor,sMsg)
{
/*
    Example Cursors: default,hand
*/    
	thisObject.style.color=sColor;
	thisObject.style.cursor=sCursor;
	window.status = sMsg;
}

function statusChange(sMsg)
{
    window.status = sMsg;
}