//--------------------------------------------------------------------------------------
// Copyright 2000 McBride Design All rights reserved 
//--------------------------------------------------------------------------------------
//
// NAME:          MD.js
//
// DESCRIPTION:   McBride Design JavaScript functions
//
// HISTORY:
// 03-MAR-1999    C. McBride         Created
// 08-OCT-2000    C. McBride         Enhanced MDdumpForm() to display like MD.pm:Dump()
//
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Name:        MDcheckForm(form)       
// Usage:       <FORM onSubmit="return MDcheckForm(this)">
//
// Description: Form validation. 
//              MDcheckList  is a comma separated list of form element names to check.  
//              MDcheckListType is either 'CHECK' or 'SKIP'.  If ommitted, the default 
//              it CHECK.  If neither variable is set, all elements are checked.
//              MDcheckList and MDcheckListType should be set in the HTML document in 
//              their own <SCRIPT> tags after the iCOM.js is loaded:
//                  <SCRIPT SRC="iCOM.js"></SCRIPT>
//                  <SCRIPT>
//                  MDcheckList = 'Zip,Telephone,City';
//                  MDcheckListType = 'CHECK';
//                  </SCRIPT>
//--------------------------------------------------------------------------------------
var MDcheckList = "", MDcheckListType = "";
function MDcheckForm(f){
    var inValid = "";
    var checkIt = 0;
    var exprCode = "";
    var foundOne = 0;
    var lst;

    // 1) Find out what we're checking
    // Since we don't have hash tables, build up 
    // an expression and eval it.
    if(MDcheckList.length > 0){
        lst = MDcheckList.split(",");         
        exprCode += 'if( ';
        for(i=0; i < lst.length; i++){
            if(i > 0){
                exprCode += ' || ';
            }
            exprCode += '(f.elements[i].name == "' + lst[i] + '" ) ';
        }
        exprCode += ') { foundOne = 1;} ';
    }


    //
    // 2) Do the check
    for(i=0; i< f.elements.length;i++){
        // Check everthing by default
        checkIt = 1;
        // If there is a check list...
        if(lst.length > 0){
            // Evaluate the expression code and set the
            // check flag accordingly
            // If this is a CHECK list, only check the element if it is found
            // If this is a SKIP list, skip the element if it is found, otherwise
            // check it
            checkIt = 0;
            foundOne = 0;
            eval(exprCode);
            if(foundOne == 1){
                // This element is in the checklist...
                if( (MDcheckListType.indexOf("SKIP") > -1) || (MDcheckListType.indexOf("skip") > -1)){
                    // the check list type is SKIP, so don't check this 
                    checkIt = 0;
                }
                else{
                    // this is a CHECK type so definitely check it
                    checkIt = 1;
                }
            }
            else{
                // This element was NOT in the checklist...
                if( (MDcheckListType.indexOf("SKIP") > -1) || (MDcheckListType.indexOf("skip") > -1)){ 
                    // ... so if the list type is SKIP,  we DON'T want to skip this one
                    checkIt = 1;
                }
                else{
                    // The list type was CHECK and this element was NOT in it, therefore,
                    // do NOT check it 
                    checkIt = 0;
                } 
            }
        }

        if((checkIt == 1) && (f.elements[i].value.length == 0) ){
            inValid += f.elements[i].name + "\n";                    
        } 
    }

    //
    // 4) Report what's missing or proceed with submission 
    if(inValid.length > 0){
        alert("Missing required information:\n" + inValid);
        return false;
    }
    return true;

}

//---------------------------------------------------------
// Name:        MDdumpForm(form)
// Usage:       <FORM onSubmit="return MDdumpForm(this)">
//
// Description: Utility function to dump the form elements
//              into a popup window for debugging etc.
//---------------------------------------------------------
function MDdumpForm(f){
    var m = '<HEAD><TITLE>Form Dump</TITLE></HEAD><BODY><H2>Form Element Dump:</H2><BR>'; 
    var bg1 = 'BGCOLOR="#FEFEFE"';
    var bg2 = 'BGCOLOR="#EEEEEE"';
    var bg = bg1;
    m += '<HTML><TABLE BORDER=0 COLS=3>';
    m += '<TR><TD>Name</TD><TD>Type</TD><TD>Value</TD></TR>';  
    for(i=0;i<f.elements.length;i++){
        m += '<TR><TD '+ bg + '>' + f.elements[i].name +  
             '</TD><TD '+ bg + '>' + f.elements[i].type +  
             '</TD><TD '+ bg + '>' + f.elements[i].value + '</TD><TR>'; 
        if(bg == bg1){bg = bg2;}else{bg = bg1;}
    }
    m += '</TABLE></BODY></HTML>';

    formDump = window.open(' ', 'FormDump', 'scrollbars,resizable,width=500,height=300,left=10,top=10')
    formDump.document.open();
    formDump.document.write(m);
    formDump.document.close();
    return false;
}









