//
// we use this function to lookup objects by name
// we need this because ActionForm properties are
// often dotted (state.abbr, eg.) which confuses 
// the javascript language.  so, we pass the form
// and the name of the object and look it up iteratively
//
function lookup(_form, _objname) {
    var i=0;
    if (_form.elements != null) {
        for(i=0; i<_form.elements.length; i++) {
            if (_form.elements[i].name == _objname) {
                return _form.elements[i];
            }
        }
    }
    return null;
}

//
// called as a result of a __check field,
// verifies that the value matches a regular expression
//
function regexpCheck(_testobj,_regexp,_error,_obj)
{
    //create a regular expression object
    var re = new RegExp(_regexp);

    //if the object doesnt exist, return 'success'
    if (_testobj == null) return "";

    //do regular expression matching on the text
    var matchArray = re.exec(_testobj.value);

    //if there were no matches, return the error string
    if (!matchArray) return _error;

    //else return 'success'
    return "";
}

//
// checks for confirmation from the user
// also checks all fields with name__check hidden fields
// if the field exists, a call to its corresponding "check()"
// function is made to verify it is appropriately populated
//
function checkValidity(_formname, _confirm) {
   // an accumulation of error strings
   var errorText = "";

   // if a  confirm string is not specified, then dont bother the user
   if (_confirm!="") doit=false;
   else doit=true;
   // check with the user to see if we can proceed.
   if (_confirm && confirm(_confirm)) {
    doit=true;
   }

   // if all is well, check each field for its assiciated __check hidden field
   if (doit==true)
   {
        // check each field in the form to see if it has a __check hidden field
        for(var i=0; i<document.forms[_formname].elements.length; i++)
        {
            var checkobj = document.forms[_formname].elements[i];  // the current object to check
            var objname = checkobj.name; // the current object's name
            var s = objname.substring(objname.length-7,objname.length);

            // does the name end in __check? if so, persue further
            if (s=="__check")
            {
                var testObjName = objname.substring(0,objname.length-7);
                var testObj = lookup(document.forms[_formname], testObjName);
                // the value of the __check object is parameter to validate the object
                var cmd = "regexpCheck(testObj,"+checkobj.value+",checkobj)";
                var errorString = eval(cmd);
                if (errorString != "") errorText = errorText + errorString + "\n";
             }
        }

        if (errorText != "")
        {
            alert("Some problems were found in the form\n\n"+errorText);
            return false;
        }

        // submit the form and perform the action
        document.forms[_formname].submit();
        return false;
   }
   return false;
}

//
// find a form that has a particular object in it
//
function findForm(_formname, _objname) {
    var i=0;
    for(i=0; i<document.forms.length; i++) {
        if (document.forms[i].name == _formname) {
            var obj = lookup(document.forms[i], _objname);
            if (obj != null) {
                return document.forms[i];
            }
        }
    }
    return null;
}

//
// checks for confirmation from the user
// also checks all fields with name__check hidden fields
// if the field exists, a call to its corresponding "check()"
// function is made to verify it is appropriately populated
//
function checkValidity2(_formname, _objname, _confirm) {
   // an accumulation of error strings
   var errorText = "";

   // if a  confirm string is not specified, then dont bother the user
   if (_confirm!="") doit=false;
   else doit=true;
   // check with the user to see if we can proceed.
   if (_confirm && confirm(_confirm)) {
    doit=true;
   }

   //because there may be more than one form with this name
   var formobj = findForm(_formname, _objname);

   // if all is well, check each field for its assiciated __check hidden field
   if (doit==true)
   {
        // check each field in the form to see if it has a __check hidden field
        for(var i=0; i<formobj.elements.length; i++)
        {
            var checkobj = formobj.elements[i];  // the current object to check
            var objname = checkobj.name; // the current object's name
            var s = objname.substring(objname.length-7,objname.length);

            // does the name end in __check? if so, persue further
            if (s=="__check")
            {
                var testObjName = objname.substring(0,objname.length-7);
                var testObj = lookup(formobj, testObjName);
                // the value of the __check object is parameter to validate the object
                var cmd = "regexpCheck(testObj,"+checkobj.value+",checkobj)";
                var errorString = eval(cmd);
                if (errorString != "") errorText = errorText + errorString + "\n";
             }
        }

        if (errorText != "")
        {
            alert("Some problems were found in the form\n\n"+errorText);
            return false;
        }
        // success, the form is fine
        return true;
   }
   return false;
}

function submitWithMethod(_formName, _method) {
    document.forms[_formName].method.value=_method;
    document.forms[_formName].submit();
    return false;
}

function submitWithMethodValue(_formName, _method, _objname, _value) {
    var formobj = findForm(_formName, _objname);
    formobj.method.value=_method;
    formobj.elements[_objname].value=_value;
    formobj.submit();
    return false;
}

function viewImageFixedSize(_filename, _w, _h){
    var location = _filename;
    var params = "resizable=no,menubar=no,scrollbars=no,width="+_w+",height="+_h;
    var newwin = window.open(location, null, params);
    newwin.focus();
    return false;
}

function viewImage(_filename){
    var location = _filename;
    var params = "resizable=yes,menubar=no,scrollbars=yes,width=200,height=200";
    var newwin = window.open(location, null, params);
    newwin.focus();
    return false;
}

function newPage(_location) {
    var params = "resizable=yes,menubar=yes,scrollbars=yes";
    var newwin = window.open(_location, "", params);
    newwin.focus();
    return false;
}
function newPageSize(_location,_name,_width,_height) {
    var params = "resizable=yes,menubar=yes,scrollbars=yes,width="+_width+",height="+_height;
    var newwin = window.open(_location, _name, params);
    newwin.focus();
    return false;
}
function newPageSizeNoMenu(_location,_name,_width,_height) {
    var params = "resizable=yes,menubar=no,scrollbars=yes,width="+_width+",height="+_height;
    var newwin = window.open(_location, _name, params);
    newwin.focus();
    return false;
}

