<!-- 
// ##########################################################
//
// Functions that enhance the browsers handling of HTML forms.  Should
// be included on all pages that have HTML forms.  The standard way to
// include this file is to assign request$formHandling the value "1"
// before including the header file.
// 
// The various header files (header.tml, admin_header.tml, etc.) will
// check request$formHandling and include this file if it equals "1".
//
// ##########################################################

// initialize variables
// global flag, whether or not a form has been submitted
var formSubmittedFlag=false;

// add argument(s) to the URL in the parent form's action property
// useful when a single form has multiple submit buttons
function formAction(theForm, argString) {
	if ( theForm.action.indexOf("?") > 0 ) {
		theForm.action = theForm.action + '&' + argString;
	} else {
		theForm.action = theForm.action + '?' + argString;
	} //end if
} //end function

// on first run, return false, disable buttons and raise formSubmittedFlag
// additional runs only return false
// intended to be called by each form's onSubmit handler
function disableSubmit() {
	if (formSubmittedFlag == false) {
		formSubmittedFlag=true;
		
		// loop over all forms and disable all buttons on each form
		for (form_id=0; form_id < document.forms.length; form_id++) {
			for (element_id=0; element_id < document.forms[form_id].elements.length; element_id++) {
				if ( (document.forms[form_id].elements[element_id].type == 'submit')
					|| (document.forms[form_id].elements[element_id].type == 'reset')
					|| (document.forms[form_id].elements[element_id].type == 'button')
				) {
					document.forms[form_id].elements[element_id].disabled=true;
				} //end if
			} //end for
		} //end for

		return true;
		
	} else {
		return false;
		
	} //end if
} //end function


// this function copies the physical address fields
//    on the agency form
function agencyAddrCopy() {

	if (document.getElementById('mailing_addr_same').checked) {

		// copy physical address values to mailing address fields
		document.getElementById('mail_add1').value  = document.getElementById('phys_add1').value;
		document.getElementById('mail_add2').value  = document.getElementById('phys_add2').value;
		document.getElementById('mail_city').value  = document.getElementById('phys_city').value;
		document.getElementById('mail_state').selectedIndex = document.getElementById('phys_state').selectedIndex;
		document.getElementById('mail_zip').value   = document.getElementById('phys_zip').value;

		// disable mailing address fields
		document.getElementById('mail_add1').disabled  = true;
		document.getElementById('mail_add2').disabled  = true;
		document.getElementById('mail_city').disabled  = true;
		document.getElementById('mail_state').disabled = true;
		document.getElementById('mail_zip').disabled   = true;
	}
	else
	{
		// enable mailing address fields
		document.getElementById('mail_add1').disabled  = false;
		document.getElementById('mail_add2').disabled  = false;
		document.getElementById('mail_city').disabled  = false;
		document.getElementById('mail_state').disabled = false;
		document.getElementById('mail_zip').disabled   = false;
	}

} //end function

// this function copies the physical address fields
//    on the agency form
function shelFeedAreaDisable() {

	if (document.getElementById('feeding_none').checked) {

		// disable feeding area fields
		document.getElementById('feeding_snackbar').disabled        = true;
		document.getElementById('feeding_snackbar_seats').disabled  = true;
		document.getElementById('feeding_cafeteria').disabled       = true;
		document.getElementById('feeding_cafeteria_seats').disabled = true;
		document.getElementById('feeding_other').disabled           = true;
		document.getElementById('feeding_other_seats').disabled     = true;
		document.getElementById('seating_capacity').disabled     = true;
	}
	else
	{
		// enable feeding area fields
		document.getElementById('feeding_snackbar').disabled        = false;
		document.getElementById('feeding_snackbar_seats').disabled  = false;
		document.getElementById('feeding_cafeteria').disabled       = false;
		document.getElementById('feeding_cafeteria_seats').disabled = false;
		document.getElementById('feeding_other').disabled           = false;
		document.getElementById('feeding_other_seats').disabled     = false;
		document.getElementById('seating_capacity').disabled     = false;
	}
}

// this function copies the physical address fields
//    on the agency form
function shelRampCheck(caller) {

	// if any ramp types are checked, check the ramp box
	if ((document.getElementById('handicap_fixed_ramp').checked ||
	    document.getElementById('handicap_portable_ramp').checked ||
	    document.getElementById('handicap_level_landings').checked ) &&
	     (caller.id != 'handicap_ramps' )) {

		document.getElementById('handicap_ramps').checked = true;
	}

	// if the rampbox is unchecked, so are any ramp types
	if ( document.getElementById('handicap_ramps').checked == false && 
	     caller.id == 'handicap_ramps' ) {

		// uncheck the ramp types
		document.getElementById('handicap_fixed_ramp').checked     = false;
	    document.getElementById('handicap_portable_ramp').checked  = false;
	    document.getElementById('handicap_level_landings').checked = false;
	}

} //end function



//
// setBoxGroup: if control rstRadioId is selected,
//	enable all divs starting with divPrefix 
// and checkboxes starting with boxPrefix
//
// inconsistent naming of DHTML property for class/className requires
// two different statements
//
//   Safari: class
//   IE 5.2 mac: className
//   IE 7: either
//   FF: either

function setBoxGroup(rstRadioId, boxPrefix, divPrefix, maxBoxId) 
{
	var i=0;

	for (i = 1; i <= maxBoxId; i++)  	{
		if (document.getElementById(boxPrefix+i) && document.getElementById(divPrefix+i)) {
			if (document.getElementById(rstRadioId).checked) 	{
				document.getElementById(boxPrefix+i).disabled = false;
				document.getElementById(divPrefix+i).setAttribute("class","text_enabled",0);
				document.getElementById(divPrefix+i).setAttribute("className","text_enabled",0);
			} else {
				document.getElementById(boxPrefix+i).disabled = true;
				document.getElementById(divPrefix+i).setAttribute("class","text_disabled",0);
				document.getElementById(divPrefix+i).setAttribute("className","text_disabled",0);
			}
		}
	}
} // end function


//
// keepOneCheck: if box being unchecked is the last checked box,
//	don't allow the user to uncheck it 
//

function keepOneCheck(thisId, boxPrefix, maxBoxId) 
{
	var i=0;
	var safe=false;

	for (i = 1; i <= maxBoxId; i++) {
		if (document.getElementById(boxPrefix+i)) {
			if ((document.getElementById(boxPrefix+i).disabled == false) &&
					(document.getElementById(boxPrefix+i).checked == true) &&
					(boxPrefix+i != thisId)) {
				safe = true;
			} 
		}
	}
	
	if  (!safe) {
		document.getElementById(thisId).checked = true;
	}
} // end function


// functions for managing disaster association checkboxes on a search form
var ogUnassocVal;
		
function setDisasterBoxes(count)
{
	if (document.getElementById('disaster_checkall').checked)	{
		for (i=1; i<=count; i++)	{
			document.getElementById('disaster_em'+i).checked = true;
		}
		if (document.getElementById('disaster_and').checked == false)		{
			document.getElementById('disaster_unassociated').checked = true;
		}
	}
	else	{
		for (i=1; i<=count; i++)	{
			document.getElementById('disaster_em'+i).checked = false;
		}
		document.getElementById('disaster_unassociated').checked = false;
	}

}

function disableUnassoc()
{
	// save value of 'unassociated disasters' checkbox
	ogUnassocVal = document.getElementById('disaster_unassociated').checked;

	document.getElementById('disaster_unassociated').disabled = true;
	document.getElementById('disaster_unassociated').checked = false;
	document.getElementById('dis_un_text').style.color = "#B0B0B0";
}

function enableUnassoc()
{
	document.getElementById('disaster_unassociated').disabled = false;
	document.getElementById('dis_un_text').style.color = "#000000";

	// restore value of 'unassociated disasters' checkbox
	document.getElementById('disaster_unassociated').checked = ogUnassocVal;
}
// sets the 'disabled' property for a given element, and all of its child nodes
//   elem: element object to enable/disable
//   dis:  boolean value (true = disable, false = don't disable)
function setChildNodeDis(elem,dis) {
    try {
        elem.disabled = dis;
    }
    catch(E) {}

    if (elem.childNodes && elem.childNodes.length > 0) {
        for (var x = 0; x < elem.childNodes.length; x++) {
            setChildNodeDis(elem.childNodes[x],dis);
        }
    }
}
// =============================================================================
//
// The confirm_delete() function presents an alert confirmation box, prior to 
// deleting a record.
//
// =============================================================================
// .............................................................................
function confirm_delete(dialog, form_name) {
	var confirm_box = confirm(dialog);
	var form_obj = document.getElementById(form_name);
	if (confirm_box==true) { form_obj.submit(); }
	else { return; }
}
// .............................................................................
// =============================================================================
//
// The confirm_delete() function presents an alert confirmation box, prior to 
// deleting a record.
//
// =============================================================================
// .............................................................................

function ccmExpenseTotal(form_obj) {
	var mi = filterNum(document.getElementById("housing_expenses").value);
	var gi = filterNum(document.getElementById("general_expenses").value);
	var ci = filterNum(document.getElementById("comm_expenses").value);
	var te = filterNum(document.getElementById("transportation_expenses").value);
	var ui = filterNum(document.getElementById("utility_expenses").value);
	var me = filterNum(document.getElementById("medical_expenses").value);
	var ee = filterNum(document.getElementById("edu_expenses").value);
	var oe = filterNum(document.getElementById("other_expenses").value);
	var expense_total = mi+gi+ci+te+ui+me+ee+oe;
	
	document.getElementById("total_expenses").value = expense_total.toFixed(2);
}

function filterNum(str) {
	re = /^\$|,/g;	// remove "$" and ","

	var x = parseFloat(str.replace(re, ""));
	    x = parseFloat(x.toFixed(2));

	if (isNaN(x)) {
		x = parseFloat(0);
	}	
	return x;
}

function OLDcalcSum(form_obj, group_hdr, n, total_target) {
	var sum = 0;
	var section_array = new Array();
	for (i=0; i < n; i++) {
		var ref = group_hdr + "_" + (i+1);
		section_array[i] = filterNum(form_obj.elements[ref].value);
		form_obj.elements[ref].value = section_array[i];
	}
	for (i=0; i < n; i++) {
		sum += section_array[i];
	}
	form_obj.elements[total_target].value = sum.toFixed(2);
}


function calcSum(form_obj, group_hdr, n, total_target) {
	var sum = 0;
	var section_array = new Array();
	for (i=0; i < n; i++) {
		var ref = group_hdr + "_" + (i+1);
		section_array[i] = filterNum(document.getElementById(ref).value);
		document.getElementById(ref).value = section_array[i].toFixed(2);
	}
	for (i=0; i < n; i++) {
		sum += section_array[i];
	}
	document.getElementById(total_target).value = sum.toFixed(2);
}

// .............................................................................
// removes leading/trailing whitespace from a string
function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
} //end function

// .............................................................................
// checks all text boxes in a form and submits only if at least one is filled in
function val_submit(form_name,message,num) {
    var valid = 0;
    if(num){
      valid -= num;
    }
    var cur_tagname = '';

    for (i=0;i<document.forms[form_name].elements.length;i++) {
        cur_tagname = document.forms[form_name].elements[i].tagName.toLowerCase();
        if (cur_tagname == 'select' || (cur_tagname == 'input' && document.forms[form_name].elements[i].attributes.getNamedItem('type').value.toLowerCase() == 'text')) {
            if (trim(document.forms[form_name].elements[i].value) != '') { valid += 1; }
        }
    }

    if(valid <= 0){
      alert(message);
      return false;
    }else{
      return true;
    }

} //end function

// .............................................................................
// =============================================================================
//
// Functions that dynamicly add a sequence to a form.  All children of the 
// parent object will be added to the form with the next sequence number.
// 
// To include this file in the page HEAD assign
// <@VAR request$js_propSwap> = 1
// prior to including the header file.
// 
// =============================================================================
// .............................................................................
function cloneSequence(id_parent,id_template,id_count) {
    if (document.getElementById) {obj_parent = document.getElementById(id_parent);}
    else if (document.all) {obj_parent = document.all[id_parent];}
    else if (document.layers) {obj_parent = document.layers[id_parent];}

    if (document.getElementById) {obj_templ = document.getElementById(id_template);}
    else if (document.all) {obj_templ = document.all[id_template];}
    else if (document.layers) {obj_templ = document.layers[id_template];}

    if (document.getElementById) {obj_count = document.getElementById(id_count);}
    else if (document.all) {obj_count = document.all[id_count];}
    else if (document.layers) {obj_count = document.layers[id_count];}

    obj_count.value = parseInt(obj_count.value)+1;
    var seq = obj_count.value;

    obj_new = obj_templ.cloneNode(true);
    if (obj_new.hasAttribute('name')) {
        obj_new.setAttribute('name',obj_new.getAttribute('name')+seq);
    }
    if (obj_new.hasAttribute('id')) {
        obj_new.setAttribute('id',obj_new.getAttribute('id')+seq);
    }

    setChildrenAttribSeq(obj_new,'name',seq);

    obj_parent.appendChild(obj_new);
    obj_new.style.display='block';

} // end function
function setChildrenAttribSeq(parent,attrib_name,seq) {
  for (var i=0; i<parent.childNodes.length; i++) {
    with (parent.childNodes[i]) {
      var childNode = parent.childNodes[i];
      if (childNode.nodeName == '#text') { continue; }
      setAttribSeq(childNode,seq);

        // check on the kids
      if (childNode.hasChildNodes()) {
        setChildrenAttribSeq(childNode,id,seq);
      }

    }
  }
} // end function
function setAttribSeq(obj,seq) {
  if (obj.hasAttribute('name')) {
    var attrib_val = obj.getAttribute('name');
    if (attrib_val.charAt(attrib_val.length-1) == '_') {
      obj.setAttribute('name',attrib_val+seq);
    }
  }
  if (obj.hasAttribute('id')) {
    var attrib_val = obj.getAttribute('id');
    obj.setAttribute('id',attrib_val+seq);
  }
  if (obj.hasAttribute('value')) {
    var attrib_val = obj.getAttribute('value');
    if (attrib_val.charAt(attrib_val.length-1) == '_') {
      obj.setAttribute('value',attrib_val+seq);
    }
  }
} // end function
// .............................................................................
// -->
