//image rollovers

function preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


//popup window

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function viewImage(_filename){
    var location = _filename;
    var params = "resizable=yes,menubar=no,scrollbars=yes,width=100,height=75";
    var newwin = window.open(location, null, params);
    newwin.focus();
    return false;
}

//
// 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;
}

//
// 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;
}
//
// 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
                if (testObj.type == 'checkbox') testObj.value = testObj.checked ? "true" : "false";
                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;
        }

        //all is well, return true
        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 addCountyState(themethod) {	
	alert('If you would like to make another state/county selection, please do so now and click the "add" button.\nOtherwise, click "continue" and proceed to the next step.');
	document.orderForm.method.value=themethod;
	document.orderForm.submit();
	return false;
}
