
function validateForm(form) {
	var fieldObj;
	var firstErr;
	var emailPattern = /^[\w\.\-\_]+@([\w\-]+\.)+[a-zA-Z]+$/;
	var phonePattern = /^(\(\d+\) ?)?(\d+[\- ])*\d+$/;
	var extLinkPattern = /href="?http:/;
	var alertMsg = "The Following Form Fields have errors:";
  
	var l_Msg = alertMsg.length;

	for (var i = 0; i < form.elements.length; i++) {
		fieldObj = form.elements[i];
		if ( !fieldObj || fieldObj.className == "" ) continue;

		if ( /(^| )Required( |$)/.test(fieldObj.className) && !ckRequired( fieldObj )) {
			if ( !firstErr ){	firstErr = fieldObj;}
			alertMsg += "\n\t" + fieldObj.title + " - is required";
		}

		if ( fieldObj.value != "" && /(^| )Number( |$)/.test(fieldObj.className)) {
			var val = parseFloat( fieldObj.value, 10 );
			if ( val != fieldObj.value ) {
				if ( !firstErr )	firstErr = fieldObj;
				alertMsg += "\n\t" + fieldObj.title + " - Must be a Number";
			} else {
				var ix=fieldObj.title.indexOf( " Range " );
				if ( ix > 1 ) {			// required Range
					var range = fieldObj.title.substring( ix+7, fieldObj.title.length );
					range = range.split( " to " );
					if ( val < range[0] || val > range[1] ) {
						if ( !firstErr ){	firstErr = fieldObj;}
						alertMsg += "\n\t" + fieldObj.title + " - Out of Range";
					}
				}
			}
		}

		if ( fieldObj.value != "" ) {	   // check optional formats
			if (/(^| )PhoneNumber( |$)/.test(fieldObj.className) && !phonePattern.test(fieldObj.value)) {
				if ( !firstErr )	firstErr = fieldObj;
				alertMsg += "\n\t" + fieldObj.title + " - invalid Phone Number";
			}

			if (/(^| )Email( |$)/.test(fieldObj.className) && !emailPattern.test(fieldObj.value)) {
				if ( !firstErr )	firstErr = fieldObj;
				alertMsg += "\n\t" + fieldObj.title + " - invalid e-mail address";
			}

			if (/(^| )NoExtLink( |$)/.test(fieldObj.className) && extLinkPattern.test(fieldObj.value)) {
				if ( !firstErr )	firstErr = fieldObj;
				alertMsg += "\n\t" + fieldObj.title + " - invalid External Link URL";
			}
		}
	}

	if (firstErr )	 firstErr.focus();
	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}

function ckRequired( obj ) {
	switch(obj.type){
	case "select-one":
		if (obj.selectedIndex == -1 || isEmpty(obj.options[obj.selectedIndex].text)){
			return false;
		}
		return true;
	case "select-multiple":
		if (obj.selectedIndex == -1){
			return false;
		}
		return true;
	case "text":
	case "textarea":
		return !isEmpty( obj.value);
	default:
	}
	if (obj.type == undefined){		// radio group
		var blnchecked = false;
		for (var j = 0; j < obj.length; j++){
			if (obj[j].checked){
				blnchecked = true;
			}
		}
		return blnchecked;
	}
	return true;
}

// check to see if input is empty or whitespace only
function isEmpty(val){
	if ( val == "" || val == null || val.match(/^\s+$/) ){
		return true;
	}else{
		return false;
	} 
}

function requireTrue( check, nameList ) {
	var reqPat = new RegExp( "(^| )Required( |$)" );
	var names = nameList.split(",");
	for ( var i=0; i<names.length; i++) {
		var fld=check.form[names[i]];
		if ( check.checked ) {
			if ( !reqPat.test( fld.className )) {
				if ( fld.className == "" ) {
					fld.className = "Required";
				} else {
					fld.className += " Required";
				}
			}
		} else {
			var removed = fld.className;
			removed = removed.replace( reqPat, "$1" );
			removed = removed.replace( / $/, "" );
			fld.className = removed;
		}
	}
}


function disAllow( fld, str ){
	for ( var a=1; a<arguments.length; a++ ) {
		var pat = new RegExp( arguments[a], "ig" );
		if ( pat.test( fld.value)) {
			alert ( "string " +  arguments[a] + " - is not allowed in field - " + fld.title );
			fld.value = fld.value.replace( pat, '#' );
			fld.focus();
			return false;
	}	}
	return true;
}


function addTemplate( templ, loc ){
	if ( typeof templ == "string" ) {
		templ = document.all ? document.all[templ] : document.getElementById(templ);
	}
	if ( ! templ )	{alert( "addTemplate - Invalid template");	return false;}	// invalid
	if ( ! loc )	loc = 1;
	templ.copyCount = templ.copyCount ?  templ.copyCount+1: loc;
	var nNode = templ.cloneNode( true );
	nNode.id = '';
	if ( nNode.style.display == 'none' )
		nNode.style.display = '';
	for ( var x in nNode.childNodes ) {
		var cNode = nNode.childNodes[x];
		if ( cNode.nodeType != 1 ) continue;
		if ( cNode.htmlFor )	cNode.htmlFor += templ.copyCount;
		if ( cNode.id )		cNode.id += templ.copyCount;
		if ( cNode.name && !cNode.name.match('[]') )	cNode.name += templ.copyCount;
	}
	var parent = templ.parentNode;
	parent.insertBefore( nNode, templ );
	return true;
}
function dropTemplate( templ ) {
	if ( typeof templ == "string" ) {
		templ = document.all ? document.all[templ] : document.getElementById(templ);
	}
	if ( ! templ )	{ alert("dropTemplate - invalid node Id");	return false;}	// invalid
	var parent = templ.parentNode;
	parent.removeChild( templ );
	return true;
}


/*
 * Copyright 2005 Matthew Eernisse (mde@fleegix.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Original code by Matthew Eernisse (mde@fleegix.org)
 * Additional bugfixes by Mark Pruett (mark.pruett@comcast.net)
 *
*/

// The var docForm should be a reference to a <form>

function formData2QueryString(docForm) {

  var submitContent = '';
  var formElem;
  var lastElemName = '';
  
  for (i = 0; i < docForm.elements.length; i++) {
    
    formElem = docForm.elements[i];
    switch (formElem.type) {
      // Text fields, hidden form elements
      case 'text':
      case 'hidden':
      case 'password':
      case 'textarea':
      case 'select-one':
        submitContent += formElem.name + '=' + escape(formElem.value) + '&'
        break;
        
      // Radio buttons
      case 'radio':
        if (formElem.checked) {
          submitContent += formElem.name + '=' + escape(formElem.value) + '&'
        }
        break;
        
      // Checkboxes
      case 'checkbox':
        if (formElem.checked) {
          // Continuing multiple, same-name checkboxes
          if (formElem.name == lastElemName) {
            // Strip of end ampersand if there is one
            if (submitContent.lastIndexOf('&') == submitContent.length-1) {
              submitContent = submitContent.substr(0, submitContent.length - 1);
            }
            // Append value as comma-delimited string
            submitContent += ',' + escape(formElem.value);
          }
          else {
            submitContent += formElem.name + '=' + escape(formElem.value);
          }
          submitContent += '&';
          lastElemName = formElem.name;
		}
        break;
        
    }
  }
  // Remove trailing separator
  submitContent = submitContent.substr(0, submitContent.length - 1);
  return submitContent;
}


function printit(){  
if (window.print) {
    window.print() ;  
} else {
    var WebBrowser = '<object id="WebBrowser1" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
}
}
