// JavaScript Document

<!-- javascript to add more fields -->
function jfunc_more_fields(var_field, var_write)
{
	var counter = 0;
	counter++;
	var newFields = document.getElementById(var_field).cloneNode(true);
	newFields.id = '';
	newFields.style.display = '';
	var newField = newFields.childNodes;
	for (var i=0;i<newField.length;i++)
	{
		var theName = newField[i].name
		if (theName)
			newField[i].name = theName + counter;
	}
	var insertHere = document.getElementById(var_write);
	insertHere.parentNode.insertBefore(newFields,insertHere);
}

<!-- javascript to show/hide row upload photos -->
function jfunc_show_hide_row_upload(var_noofpic){
	for(i=1;i<=var_noofpic;i++){
		var_trid = 'tr_id'+i;
		var_doc_trid = document.getElementById(var_trid);
		var_doc_trid.style.display = "";
	}
	
	for(i=i;i<=20;i++){
		var_trid = 'tr_id'+i;
		var_doc_trid = document.getElementById(var_trid);
		var_doc_trid.style.display = "none";
	}
	
}

<!-- javascript to show/hide table -->
function jfunc_show_hide_table(var_tableid, thisItem){
	f = document.getElementById(var_tableid);
	if(f.style.display == "none"){
		f.style.display = "block";
		thisItem.src = "images/button_minus.gif";
	}else{
		f.style.display = "none";
		thisItem.src = "images/button_plus.gif";
	}
}

<!-- javascript to show/hide row description -->
function jfunc_show_hide_row(var_trid, var_trtype, var_trcount, var_button){
	
	f1 = document.getElementById(var_trid);	
	for(i=0;i<=var_trcount;i++){
		f0 = var_trtype+i;
		fbutton0 = 'button_plus'+i;
		f = document.getElementById(f0);
		fbutton = document.getElementById(fbutton0);
		
		if(f == f1){
			if(f1.style.display == "none"){
				f1.style.display = "";
				document.getElementById(var_button).src = "images/button_minus.gif";
			}else{
				f1.style.display = "none";
				document.getElementById(var_button).src = "images/button_plus.gif";	
			}
			
		}else if(f){
			f.style.display = "none";
			fbutton.src = "images/button_plus.gif";
		}
	}

}

<!-- javascript for mouseover menu tr -->
function rollIn(thisItem) { 
	thisItem.style.background="#f0f0f0";
} 

function rollOut(thisItem) { 
	thisItem.style.background=""; 
} 

<!-- admin logout -->
function jfunc_admin_logout ()
{
    var message = "Are you sure you want to Logout?";
    var return_value = confirm(message);
    if ( return_value == true )
        {
        document.location='index.php?var_action_admin_logout=true';
        }
}

<!-- delete admin -->
function jfunc_admin_delete(var_admin_id)
{
    var message = "Are you sure you want to delete this admin?";
    var return_value = confirm(message);
    if ( return_value == true )
        {
        document.location='?fn=admin_index&var_action=delete&var_admin_id='+var_admin_id;
        }
}


<!-- delete stuff -->
function jfunc_delete_stuff_link(var_url)
{
    var message = "Are you sure you want to permanently delete the selected items?\n\nTHIS ACTION IS NOT REVERSEABLE.\n\nAre you sure you want to continue?";
    var return_value = confirm(message);
    if ( return_value == true )
        {
        document.location=var_url;
        }
}

<!-- javascript to update members -->
function jfunc_update_members(var_message){
	var thispagefrm = document.frm_MemberList;
		msg = "Are you sure you want to "+var_message+"?";
		return confirm(msg);
}

<!-- javascript to delete stuff -->
function jfunc_delete_stuff(var_form){
	var thispagefrm = document.forms[var_form];
		msg = "Are you sure you want to permanently delete the selected items?\n\nTHIS ACTION IS NOT REVERSEABLE.\n\nAre you sure you want to continue?";
		return confirm(msg);
}

<!-- javascript to confirm form -->
function jfunc_confirm_form(var_form){
	var thispagefrm = document.forms[var_form];
		msg = "Continue?";
		return confirm(msg);
}

<!-- javascript to check all check boxes -->
function jfunc_check_checkbox_all(checkobj, value)
{
	formobj = checkobj.form;
	for (var i = 0; i < formobj.elements.length; i++)
	{
		elm = formobj.elements[i];
		if (elm.type == "checkbox" && elm.id == value)
		{
			elm.checked = checkobj.checked;
		}
	}
	jfunc_check_checkbox_checked(checkobj, value)
}

<!-- javascript to check if check boxes are checked -->
function jfunc_check_checkbox_checked(checkobj, value){
	formobj = checkobj.form;
	var box_checked = 0;
	for( var i = 0; i < formobj.elements.length; i++){
		elm = formobj.elements[i];
		if (elm.type == "checkbox" && elm.id == value && elm.checked){
			box_checked = box_checked + 1;
		}else if(elm.type == "radio" && elm.id == value && elm.checked){
			box_checked = box_checked + 1;
		}
	}

	if(box_checked > 0){
		formobj.boxchecked.value = 1;	
	}else{
		formobj.boxchecked.value = 0;	
	}
}

<!-- javascript to set next action -->
function jfunc_set_next_action(var_form, next_action){
	var thispagefrm = document.forms[var_form];
	thispagefrm.var_next_action.value = next_action;
	thispagefrm.submit();
}

<!-- function to round decimals -->
function jfunc_round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}
function pad_with_zeros(rounded_value, decimal_places) {
    // Convert the number to a string
    var value_string = rounded_value.toString()
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")
    // Is there a decimal point?
    if (decimal_location == -1) {
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length    
    if (pad_total > 0) {        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

<!-- function to validate form fields -->
function jfunc_validate_fields(){
	error_message = '';
	var argv = jfunc_validate_fields.arguments;
	var argv_count = argv.length;
	var thispagefrm = document.forms[argv[0]];
	for(var i = 1; i < argv_count; i+=3){
		
		if(document.getElementById(argv[i]).value == ""){
			error_message += '- '+document.getElementById(argv[i]).title+' is required\n';	
		
		}else{
			if(argv[i+1] == 'isSame' && document.getElementById(argv[i]).value != document.getElementById(argv[i+2]).value){
				error_message='- '+document.getElementById(argv[i]).title + " must be the same as "+document.getElementById(argv[i+2]).title+"\n";
			}else if(argv[i+1] == 'isEmail'){
				email_contain = document.getElementById(argv[i]).value.indexOf('@');
				if(email_contain < 1 || email_contain == ((document.getElementById(argv[i]).value.length) - 1)){
					error_message='- '+document.getElementById(argv[i]).title + " must contain an email address\n";
				}
				
			}else if(argv[i+1] == 'isNum' && isNaN(document.getElementById(argv[i]).value)){
				error_message='- '+document.getElementById(argv[i]).title + " must contain a number\n";
			}
		}
	}
	
	if(error_message != ""){
		alert(error_message);	
	}else{
		thispagefrm.submit();	
	}
}

<!-- function to calculate time hours -->
function jfunc_calculate_hours(var_form, var_time1, var_time2, var_timeto1, var_timeto2){
	var vvar_form = document.forms[var_form];
	var var_drop_down_index1 = vvar_form[var_time1].selectedIndex;
	var var_drop_down_index2 = vvar_form[var_time2].selectedIndex;
	var var_drop_down_indexto1 = vvar_form[var_timeto1];
	var var_drop_down_indexto2 = vvar_form[var_timeto2];
	var vvar_time1 = parseFloat(vvar_form[var_time1].value) * 60;
	var vvar_time2 = parseFloat(vvar_form[var_time2].value);
	var vvar_timeto1 = parseFloat(vvar_form[var_timeto1].value) * 60;
	var vvar_timeto2 = parseFloat(vvar_form[var_timeto2].value);
	
	var vvar_total_time1 = parseFloat(vvar_time1 + vvar_time2);
	var vvar_total_time2 = parseFloat(vvar_timeto1 + vvar_timeto2);
	
	
	if(vvar_total_time1 > vvar_total_time2){
		//alert("Time \"from\" can not be more than time \"to\"\nPlease try again");
		//vvar_form.reset();
		//vvar_form.SubmitButton.disabled="true";
		var_drop_down_indexto1[var_drop_down_index1].selected = 'true';
		var_drop_down_indexto2[var_drop_down_index2].selected = 'true';

	}else{
		var vvar_count_difference = vvar_total_time2 - vvar_total_time1;
		//alert(vvar_count_difference);
		
		var var_hours = Math.floor(vvar_count_difference / 60);
	}
	
}

<!-- FORM JUMP MENU
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

<!-- FORM VALIDATION
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.title; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
//-->