// Lifehouse members page
// by Raina Mason from Coffs Design
// http://www.coffsdesign.com.au

// show hide function
function toggleBlock(elementId) {
   var element = document.getElementById(elementId);
   
   if (element.style.display == '')
     element.style.display = 'none';
   else
     element.style.display = '';
}

function clear(elementId) {
	var element = document.getElementById(elementId);
	element.value='';
}
	
// calculate and display the total
function calculateTotal() {
	
	// get tithe & missions
	var tithe = document.getElementById('amount_1').value;
	var missions = document.getElementById('amount_2').value;
	// check for null values
	if (tithe == '') {
		tithe = 0.0;
	}
	if (missions == '') {
		missions = 0.0;
	}
	// change values to float if necessary
	tithe = parseFloat(tithe);
	// check if reasonable number
	if (isNaN(tithe) || (tithe < 0.0)) {
		// not a valid number, warn and exit here
		alert ("Please check the number you entered for a tithe");
		tithe = 0.0;
		return;
	}
	missions = parseFloat(missions);
	// check if real amount
	if (isNaN(missions) || (missions < 0.0)) {
		// not a valid number, warn and exit here
		alert ("Please check the number you entered for missions giving");
		missions = 0.0;
		return;
	}
	
	// all ok, check total amount
	var totalAmt = tithe + missions;	
	document.getElementById('displaycost').innerHTML=''+totalAmt.toFixed(2);
}

function checknsend() {
	var tithe = document.getElementById('amount_1').value;
	var missions = document.getElementById('amount_2').value;
	
	// check that form has some amount entered
	if ((tithe == '') && (missions == '')) {
		alert("You haven't entered any amount");
		return false;
	}
	
	calculateTotal();
	
	// send the form		  
    document.forms[0].elements['hiddensubmitbutton'].click();
}
	
/**********************************************************************
   REGULAR PAYMENTS HANDLING
***********************************************************************/

function checkregular() {
	// check if radio buttons have been chosen
	if (radioChecked(1,'itemType')=='') {
		alert("please choose either tithes or missions giving");
		return 0;
	}
	if (radioChecked(1,'time')=='') {
		alert("please choose how regularly you would like to give");
		return 0;
	}
	// make sure an amount have been entered
	var amt = getValue(1,'a3');
	if (amt == '') {
		alert('No amount entered.');
		return 0;
	}
	amt = parseFloat(amt);
	if (isNaN(amt) || (amt <= 0.0)) {
		// not a valid number, warn and exit here
		alert('The amount you entered isn\'t valid, \nplease fix and resubmit.');
		return 0;
	}
	
	// case statement here to take care of different time periods
	var timeCode = getRadioValue(1,'time');
	switch (timeCode) {
		case 'W' :
			// weekly
			setValue('p3','1');
			setValue('t3','W');
			break;
		case 'F' :
			// fortnightly
			setValue('p3','2');
			setValue('t3','W');
			break;
		case 'M' :
			//monthly
			setValue('p3','1');
			setValue('t3','M');
			break;
		case 'Q' :
			//quarterly
			setValue('p3','3');
			setValue('t3','M');
			break;
	}
	
	var itemType = getRadioValue(1,'itemType');
	if (itemType == 'Tithes') {
		setValue('item_name','Tithes');
	} else {
		setValue('item_name','Missions Giving');
	}
	
	// everything in place, submit the form
	document.forms[1].elements['hiddensubmitbutton2'].click();
	
}

// get the value that is checked in a radio group

function getRadioValue(targetForm,radioGroup) {
  var group=document.forms[targetForm].elements[radioGroup];
  for (var i=0;i<=group.length;i++) {
	  try {
		  if (group[i].checked) {
			  return group[i].value;
		  }
	  } catch(err) {
	  }
  }
}
  
// set a value for a field
function setValue(theField, theValue) {
  document.forms[1].elements[theField].value=theValue;
}
  
// check if at least one option is chosen in a radio button group
function radioChecked(theForm,radioGroup) {
  var group=document.forms[theForm].elements[radioGroup];
  for (var i=0;i<=group.length;i++) {
	try {
		if (group[i].checked) {
			return true;
		}
	} catch(err) {
	}
  }
  return false;
}
	
function fillinfield(the_name, the_value) {
  document.forms[0].elements[the_name].value = the_value;	  
}

function getValue(theForm,theField) {
  theValue = document.forms[theForm].elements[theField].value;
  return theValue;
}

// refresh the page (turn off all checked boxes etc)

function resetForms() {
	ResetForm(0);
	ResetForm(1);
}

function ResetForm(xFormName){
    var arrElems = document.forms[xFormName].elements;
    document.forms[xFormName].reset();
    for(xi=0;xi<arrElems.length-1;xi++){
      if(arrElems[xi].type == "text")
        arrElems[xi].value="";
      else if(arrElems[xi].type == "radio" ||
              arrElems[xi].type == "checkbox")
        arrElems[xi].selected = false;
	}
}
