/*******************************************************************************
* Name         = generalpurposescripts.js
*
* Description  = General Purpose Javascript Functions used in the Odometer
*                Capture Application.
*
* Revision History:
*
*  Date        Developer           Description
* ------------------------------------------------------------------------------
*  03/31/06    Maria Kaufmann      Initial implementation
*******************************************************************************/

/*******************************************************************************
* Validates that the field is not empty and not just spaces.
*******************************************************************************/
function isNotEmpty(field, fieldName, frm)
{
	var pfield = eval("document."+ frm +"."+ field +".value ");
	if (pfield == "")
	{
	    alert(fieldName + " field is empty. Please enter a valid value. ");
	    eval("document."+ frm +"."+ field +".focus()");
	    return false;
  	}

  	for (var i=0;i < pfield.length;i++)
	{
	    if (pfield.substring(i,i+1) != " ")
    	    {
		return true;
    	    }
  	}

      alert(fieldName + " field is empty. Please enter a valid value. ");
	eval("document."+ frm +"."+ field +".focus()");
	return false;
}

/*******************************************************************************
* Opens a scrollable, resizeable window with no menubar and tool bars.
*******************************************************************************/
function openWindow(url, urlDescription, width, height)
{
	var windowOptions = "width="  + width  + ",height=" + height + ",menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no";
  	window.open (url, urlDescription, windowOptions);
 }

/*******************************************************************************
* Opens a scrollable, resizeable window with menubar and tool bars.
*******************************************************************************/
function openWindowWithMenu(url, urlDescription, width, height)
{
	var windowOptions = "width="  + width  + ",height=" + height + ",menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes";
  	window.open (url, urlDescription, windowOptions);
 }

/*******************************************************************************
* Checks if the number passed in as argument is a whole number with no fraction.
*******************************************************************************/
function isWholeNumber(theValue)
{
    if (theValue == "") return false;
    var validDigits = "0123456789";
    for (i=0; i < theValue.length; i++ )
    {
        var thisChar = theValue.charAt(i);
        if (validDigits.indexOf(thisChar) == -1)
            return false;
    }

    return true;
}

/*******************************************************************************
* This function trims off leading and trailing spaces from text form fields.
*******************************************************************************/
function trimField(formField)
{
    var str = formField.value;
    while (str.substr(0,1) == " " || str.substr(str.length - 1) == " ")
    {
        if (str.substr(0,1) == " ")
        {
            str = str.substr(1);
        }
        else
        {
            str = str.substr(0, str.length - 1);
        }
    }

    formField.value = str;
}

/*******************************************************************************
* Returns true if the form field with the name (fieldName) specified is not
* empty.  Returns false otherwise.
*******************************************************************************/
function validateField(field, fieldName)
{
    if ( isEmpty(field) )
    {
        alert("Please enter a value in the '" + fieldName + "' field");
        field.focus();
        return false;
    }

    return true;
}

/*******************************************************************************
* Returns true if the field is empty or filled with blanks.
*******************************************************************************/
function isEmpty(field)
{
    trimField(field);
	if (field.value == "")  return true;

    return false;
}

