/**********************************************************************************
*  (C) COPYRIGHT Boston Data Technologies, Inc., 2006-2008.  All rights reserved. *
*  No part of this material may be used, photocopied, reproduced, or committed    *
*  to magnetic, electronic or any other form of media without the prior written   *
*  consent of Boston Data Technologies.                                           *
**********************************************************************************/

var formFlds=null;
function formFldData (fld) {
   if (fld.type == "radio" || fld.type == "checkbox") {
	   	if (fld.type == "checkbox") {
   			this.name = fld.name;
		} else {
   			this.name = fld.id;
		}
	   	if (fld.checked) {
   			this.value = 'checked';
		} else {
   			this.value = '';
		}
   } else {
   		this.name = fld.name;
   		this.value = fld.value;
   }
}
function SaveFormData(theForm) {
   formFlds = new Array();
   if (!theForm) { return false; }
   for (var ix=0; ix < theForm.elements.length; ix++) {
         formFlds[ix] = new formFldData(theForm.elements[ix]);
   }
}

function FindFld (fld) {
   var str = null;
   if (!fld) { return; }
   for (var ix=0; ix < formFlds.length; ix++) {
       if (!fld.name) { continue; }
       if (!formFlds[ix].name) { continue; }
	   if (fld.type == "radio" || fld.type == "checkbox") {
		    if (fld.type == "checkbox") {
				chkName = fld.name;
			} else {
				chkName = fld.id;
			}
       		if (exactMatch(chkName,formFlds[ix].name)) {
				if (fld.checked) {
   					tmp = 'checked';
				} else {
   					tmp = '';
				}
            	if (!exactMatch(tmp,formFlds[ix].value)) {
                	str = '\n Field Name:' + chkName + '\n    New Value:"' + tmp + '"\n    Old Value:"' + formFlds[ix].value + '"';
            	}
	            break;
			}
	   } else {
       		if (exactMatch(fld.name,formFlds[ix].name)) {
            	if (!exactMatch(fld.value,formFlds[ix].value)) {
   			//	alert ('fld.value length = ' + fld.value.length + ' formFlds[ix].value length = ' + formFlds[ix].value.length);
                	str = '\n Field Name:' + fld.name + '\n    New Value:"' + fld.value + '"\n    Old Value:"' + formFlds[ix].value + '"';
            	}
            	break;
			}
       }
   }
   return str;
}
function exactMatch(s1, s2) {
  /* if (!s1) { return false; }
   if (!s2) { return false; } */
   if (s1.length == 0 && s2.length == 0) {
   		return true;
   }
   if (s1.length == s2.length) {
      if (s1.match(s2) && s2.match(s1)) { return true; }
   }
   return false;
}
function CheckChanged(theForm) {
      var changed = false;
    if (!theForm) { return changed; }
//    var msgStr = '';
    for (var ex=0; ex < theForm.elements.length; ex++) {
       var str = FindFld (theForm.elements[ex]);
       if (str) {
               changed = true;
//                  msgStr += str;
            }
    }
/*    if (changed) {
            msgStr = 'Following fields are changed\n' + msgStr;
    }
    else {
            msgStr = 'Nothing is changed\n';
    }
      alert (msgStr);*/
    return changed;    
}
function submitForm(theForm) {
	var conf = true;
	if (CheckChanged(theForm)) {
    	conf = confirm ('You have made changes,\n press OK to save or Cancel to discard changes.');
    } else {
		conf = false;
	}
	return conf;
}
function dosubmit(theForm, tgtStr) {
	
	if (submitForm(theForm)) {
		theForm.sec.value=tgtStr;
	} else {
		theForm.sec.value=tgtStr + ' nosave';
	}
	theForm.unl.value='dont';
//	alert('tgtStr = ' + theForm.sec.value);
	return false;
}
function unltest(theForm) {
	if (theForm.unl.value=="do") {
		if (submitForm(theForm)) {
			theForm.submit();
		}
	}
	return false;
}

// Find any checked boxes on records that are marked for deletion
function findCheckedBox(theForm) {
	var checked = false;
	if (theForm) {
		for (var ix=0; ix < theForm.elements.length; ix++) {
			if (theForm.elements[ix].type == "checkbox") {
				if (theForm.elements[ix].checked) {		// There is at least one record selected for deletion
					checked = true;
					break;
				}
			}
		}
	}
	return checked;
}

// Check with the user and perform the delete
function dodelete(theForm) {
	var submitRtn = true;
	if (findCheckedBox(theForm)) {
    	submitRtn = confirm ('Are you sure you want to delete these records?\n Press OK to delete or Cancel to disregard.');
	}
	if (submitRtn) {
		theForm.mode.value='del';	// Setup for delete
		theForm.submit();			// Submit the delete
	}
	return submitRtn;
}

