var submitcount=0;
function checkfields(that, fields){
	if (submitcount>0){
		alert("This form has already been submitted. Please wait...");
		return false;
	}
	submitcount++;
	var err=0;
	var errstr='';
	len=fields.length;
	for (i=0;i<len;i++){
		var tmp=that.elements[fields[i]];
        if (tmp!==null) {
            val=trim(tmp.value);
            if ( ((tmp.type=="text"||tmp.type=="textarea")&&val=='')||(tmp.type.toString().charAt(0)=="s"&&
    (tmp.selectedIndex==-1||tmp.selectedIndex==0)) ) {
                err=1;
                var tmpName=tmp.name;
                errstr=errstr+"\n\t-"+tmpName.replace(/([A-Z][A-Z*.?|a-z]|\d)/g, " $1");
                tmp.style.backgroundColor = '#FFFF00';
            }
        }
	}
	if (err){
		submitcount=0;
		alert("The following fields are missing:\n"+errstr);
		return false;
	} else {
		return true;
	}
	
	function trim(strText) { 
	    if (strText){
			while (strText.charAt(0) == ' ') 
				strText = strText.substring(1, strText.length);
			while (strText.charAt(strText.length-1) == ' ')
				strText = strText.substring(0, strText.length-1);
		}
	   return strText;
	}	
}

// return array of selected items in a Select box element
function selItems(selBox) {
    var arr=Array(); 
    var c=0; 
    for(var i=0;i<selBox.options.length;i++) { 
        if (selBox.options[i].selected) { 
            arr[c]=selBox.options[i].value; 
            c++ 
        } 
    } 
    return arr 
}

// check to see if form is empty; return true if empty
function emptyForm(form){
    var els=form.elements;
    for (var i=0;i<els.length;i++) {
        switch(els[i].type) {
            case "text":
            case "password":
            case "file":
            case "textarea":
                if (trim(els[i].value)!=""){
                    return false;
                }
                break;
            case "radio":
            case "checkbox":
                if (els[i].checked){
                    return false;
                }
                break;
            case "select-one":
            case "select-multiple":
                var items=selItems(els[i]);
                for (var j=0; j<items.length;j++) {
                    if (items[j]!="NULL"&&items[j]!="") {
                        return false;
                    }
                }
                break;
            default:
                continue;
                break;
        }
    }
    
    function trim(strText) { 
	    if (strText){
			while (strText.charAt(0) == ' ') 
				strText = strText.substring(1, strText.length);
			while (strText.charAt(strText.length-1) == ' ')
				strText = strText.substring(0, strText.length-1);
		}
	   return strText;
	}	
    return true;
}
