
function xupdate_the_form (cost_array)
{
	alert("update_the_form")}

function update_the_form (cost_array)
{
	NoCamps=0;
	//alert("update_the_form");
	id=document.forms["form1"].week1first_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	//alert(cost_location);
	//alert(cost_array);
	if (id <10)
	{	document.forms["form1"].week1cost.value=cost_array.substring(cost_location+2,cost_location+5);
	}
	else
	{	document.forms["form1"].week1cost.value=cost_array.substring(cost_location+3,cost_location+6);
	}
	if (isNaN(document.forms["form1"].week1cost.value)) {document.forms["form1"].week1cost.value='0'};
	if (document.forms["form1"].week1cost.value>0) {NoCamps=NoCamps+1};
	//alert(NoCamps);
	id=document.forms["form1"].week2first_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (id <10)
	{	document.forms["form1"].week2cost.value=cost_array.substring(cost_location+2,cost_location+5);
	}
	else
	{	document.forms["form1"].week2cost.value=cost_array.substring(cost_location+3,cost_location+6);
	}
	if (isNaN(document.forms["form1"].week2cost.value)) {document.forms["form1"].week2cost.value='0'};
	if (document.forms["form1"].week2cost.value>0) {NoCamps=NoCamps+1};
	
	id=document.forms["form1"].week3first_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (id <10)
	{	document.forms["form1"].week3cost.value=cost_array.substring(cost_location+2,cost_location+5);
	}
	else
	{	document.forms["form1"].week3cost.value=cost_array.substring(cost_location+3,cost_location+6);
	}
	if (isNaN(document.forms["form1"].week3cost.value)) {document.forms["form1"].week3cost.value='0'};
	if (document.forms["form1"].week3cost.value>0) {NoCamps=NoCamps+1};
	
	id=document.forms["form1"].week4first_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (id <10)
	{	document.forms["form1"].week4cost.value=cost_array.substring(cost_location+2,cost_location+5);
	}
	else
	{	document.forms["form1"].week4cost.value=cost_array.substring(cost_location+3,cost_location+6);
	}
	if (isNaN(document.forms["form1"].week4cost.value)) {document.forms["form1"].week4cost.value='0'};
	if (document.forms["form1"].week4cost.value>0) {NoCamps=NoCamps+1};
	
	id=document.forms["form1"].week5first_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (id <10)
	{	document.forms["form1"].week5cost.value=cost_array.substring(cost_location+2,cost_location+5);
	}
	else
	{	document.forms["form1"].week5cost.value=cost_array.substring(cost_location+3,cost_location+6);
	}
	if (isNaN(document.forms["form1"].week5cost.value)) {document.forms["form1"].week5cost.value='0'};
	if (document.forms["form1"].week5cost.value>0) {NoCamps=NoCamps+1};
	
	id=document.forms["form1"].week6first_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	
	if (id <10)
	{	document.forms["form1"].week6cost.value=cost_array.substring(cost_location+2,cost_location+5);
	}
	else
	{	document.forms["form1"].week6cost.value=cost_array.substring(cost_location+3,cost_location+6);
	}
	if (isNaN(document.forms["form1"].week6cost.value)) {document.forms["form1"].week6cost.value='0'};
	if (document.forms["form1"].week6cost.value>0) {NoCamps=NoCamps+1};
	
	calculate_total();
	//alert("got this far");
	var tmp_cost_requested=Number(document.forms["form1"].cost_requested.value);
	document.forms["form1"].cost_requested.value=tmp_cost_requested.toFixed(2);
	
	var tmp_cost_donate=Number(document.forms["form1"].cost_donate.value);
	document.forms["form1"].cost_donate.value=tmp_cost_donate.toFixed(2);
	
	var tmp_cost_prepayment=Number(document.forms["form1"].cost_prepayment.value);
	document.forms["form1"].cost_prepayment.value=tmp_cost_prepayment.toFixed(2);
	
	var tmp_cost_deposit=100*NoCamps;
	document.forms["form1"].cost_deposit.value=tmp_cost_deposit.toFixed(2);
	
	var tmp_cost_total=tmp_cost_donate+tmp_cost_requested;
	document.forms["form1"].cost_total.value=tmp_cost_total.toFixed(2);
	var tmp_charged_amount = tmp_cost_deposit + tmp_cost_prepayment;
	document.forms["form1"].pay_tobe_charged.value=tmp_charged_amount.toFixed(2);
	//document.forms["form1"].amount.value=tmp_charged_amount.toFixed(2);
	
	var tmp_amount_due= tmp_cost_requested+tmp_cost_donate - tmp_cost_deposit-tmp_cost_prepayment;
	document.forms["form1"].cost_due.value=tmp_amount_due.toFixed(2);
	
	
	//document.forms["form1"].pay_charged.value=0;
	;
}
function update_the_form2 (cost_array)
{
	//alert("update_the_form2");
	//alert(cost_array);
	id=document.forms["form1"].week1first_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (document.forms["form1"].week1approved.checked==true)
	{
		if (id <10)
		{	document.forms["form1"].week1cost.value=cost_array.substring(cost_location+2,cost_location+5)}
		else
		{	document.forms["form1"].week1cost.value=cost_array.substring(cost_location+3,cost_location+6)}
	}
	else 
		{document.forms["form1"].week1cost.value='0'};
	if (isNaN(document.forms["form1"].week1cost.value)) {document.forms["form1"].week1cost.value='0'}

	id=document.forms["form1"].week2first_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (document.forms["form1"].week2approved.checked==true)
	{
		if (id <10)
		{	document.forms["form1"].week2cost.value=cost_array.substring(cost_location+2,cost_location+5)}
		else
		{	document.forms["form1"].week2cost.value=cost_array.substring(cost_location+3,cost_location+6)}
	}
	else 
		{document.forms["form1"].week2cost.value='0'};
	if (isNaN(document.forms["form1"].week2cost.value)) {document.forms["form1"].week2cost.value='0'}
	
	id=document.forms["form1"].week3first_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (document.forms["form1"].week3approved.checked==true)
	{
		if (id <10)
		{	document.forms["form1"].week3cost.value=cost_array.substring(cost_location+2,cost_location+5)}
		else
		{	document.forms["form1"].week3cost.value=cost_array.substring(cost_location+3,cost_location+6)}
	}
	else 
		{document.forms["form1"].week3cost.value='0'};
	if (isNaN(document.forms["form1"].week3cost.value)) {document.forms["form1"].week3cost.value='0'}
	
	id=document.forms["form1"].week4first_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (document.forms["form1"].week4approved.checked==true)
	{
		if (id <10)
		{	document.forms["form1"].week4cost.value=cost_array.substring(cost_location+2,cost_location+5)}
		else
		{	document.forms["form1"].week4cost.value=cost_array.substring(cost_location+3,cost_location+6)}
	}
	else 
		{document.forms["form1"].week4cost.value='0'};
	if (isNaN(document.forms["form1"].week4cost.value)) {document.forms["form1"].week4cost.value='0'}
	
	id=document.forms["form1"].week5first_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (document.forms["form1"].week5approved.checked==true)
	{
		if (id <10)
		{	document.forms["form1"].week5cost.value=cost_array.substring(cost_location+2,cost_location+5)}
		else
		{	document.forms["form1"].week5cost.value=cost_array.substring(cost_location+3,cost_location+6)}
	}
	else 
		{document.forms["form1"].week5cost.value='0'};
	if (isNaN(document.forms["form1"].week5cost.value)) {document.forms["form1"].week5cost.value='0'}
	
	id=document.forms["form1"].week6first_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (document.forms["form1"].week6approved.checked==true)
	{
		if (id <10)
		{	document.forms["form1"].week6cost.value=cost_array.substring(cost_location+2,cost_location+5)}
		else
		{	document.forms["form1"].week6cost.value=cost_array.substring(cost_location+3,cost_location+6)}
	}
	else 
		{document.forms["form1"].week6cost.value='0'};
	if (isNaN(document.forms["form1"].week6cost.value)) {document.forms["form1"].week6cost.value='0'}
	
	calculate_total2()
	;
}
function update_the_form3 (cost_array)
{
	//alert("update_the_form3");
	//alert(cost_array);
	id=document.forms["form1"].week1assigned_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (document.forms["form1"].week1approved.checked==true)
	{
		if (id <10)
		{	document.forms["form1"].week1cost.value=cost_array.substring(cost_location+2,cost_location+5)}
		else
		{	document.forms["form1"].week1cost.value=cost_array.substring(cost_location+3,cost_location+6)}
	}
	else 
		{document.forms["form1"].week1cost.value='0'};
	if (isNaN(document.forms["form1"].week1cost.value)) {document.forms["form1"].week1cost.value='0'}

	id=document.forms["form1"].week2assigned_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (document.forms["form1"].week2approved.checked==true)
	{
		if (id <10)
		{	document.forms["form1"].week2cost.value=cost_array.substring(cost_location+2,cost_location+5)}
		else
		{	document.forms["form1"].week2cost.value=cost_array.substring(cost_location+3,cost_location+6)}
	}
	else 
		{document.forms["form1"].week2cost.value='0'};
	if (isNaN(document.forms["form1"].week2cost.value)) {document.forms["form1"].week2cost.value='0'}
	
	id=document.forms["form1"].week3assigned_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (document.forms["form1"].week3approved.checked==true)
	{
		if (id <10)
		{	document.forms["form1"].week3cost.value=cost_array.substring(cost_location+2,cost_location+5)}
		else
		{	document.forms["form1"].week3cost.value=cost_array.substring(cost_location+3,cost_location+6)}
	}
	else 
		{document.forms["form1"].week3cost.value='0'};
	if (isNaN(document.forms["form1"].week3cost.value)) {document.forms["form1"].week3cost.value='0'}
	
	id=document.forms["form1"].week4assigned_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (document.forms["form1"].week4approved.checked==true)
	{
		if (id <10)
		{	document.forms["form1"].week4cost.value=cost_array.substring(cost_location+2,cost_location+5)}
		else
		{	document.forms["form1"].week4cost.value=cost_array.substring(cost_location+3,cost_location+6)}
	}
	else 
		{document.forms["form1"].week4cost.value='0'};
	if (isNaN(document.forms["form1"].week4cost.value)) {document.forms["form1"].week4cost.value='0'}
	
	id=document.forms["form1"].week5assigned_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (document.forms["form1"].week5approved.checked==true)
	{
		if (id <10)
		{	document.forms["form1"].week5cost.value=cost_array.substring(cost_location+2,cost_location+5)}
		else
		{	document.forms["form1"].week5cost.value=cost_array.substring(cost_location+3,cost_location+6)}
	}
	else 
		{document.forms["form1"].week5cost.value='0'};
	if (isNaN(document.forms["form1"].week5cost.value)) {document.forms["form1"].week5cost.value='0'}
	
	id=document.forms["form1"].week6assigned_id.value;
	cost_location=cost_array.indexOf(id+':',0);
	if (document.forms["form1"].week6approved.checked==true)
	{
		if (id <10)
		{	document.forms["form1"].week6cost.value=cost_array.substring(cost_location+2,cost_location+5)}
		else
		{	document.forms["form1"].week6cost.value=cost_array.substring(cost_location+3,cost_location+6)}
	}
	else 
		{document.forms["form1"].week6cost.value='0'};
	if (isNaN(document.forms["form1"].week6cost.value)) {document.forms["form1"].week6cost.value='0'}
	
	calculate_total2()
	;
}
function calculate_total()
{	//var total_due
	//alert("calc totals");
	total_due = parseInt(0);
	total_due = total_due + parseInt(document.forms["form1"].week1cost.value);
	total_due = total_due + parseInt(document.forms["form1"].week2cost.value);
	total_due = total_due + parseInt(document.forms["form1"].week3cost.value);
	total_due = total_due + parseInt(document.forms["form1"].week4cost.value);
	total_due = total_due + parseInt(document.forms["form1"].week5cost.value);
	total_due = total_due + parseInt(document.forms["form1"].week6cost.value);
	//alert(total_due);
	document.forms["form1"].cost_requested.value= total_due.toFixed(2);
}
function calculate_total2()
{	//var total_due
	total_camps = parseInt(0);
	total_camps = total_camps + parseInt(document.forms["form1"].week1cost.value);
	total_camps = total_camps + parseInt(document.forms["form1"].week2cost.value);
	total_camps = total_camps + parseInt(document.forms["form1"].week3cost.value);
	total_camps = total_camps + parseInt(document.forms["form1"].week4cost.value);
	total_camps = total_camps + parseInt(document.forms["form1"].week5cost.value);
	total_camps = total_camps + parseInt(document.forms["form1"].week6cost.value);
	document.forms[0].cost_approved.value= total_camps;
	document.forms[0].cost_total.value=total_camps + parseInt(document.forms["form1"].cost_donate.value);
	document.forms[0].cost_due.value=total_camps   + parseInt(document.forms["form1"].cost_donate.value)-(parseInt(document.forms["form1"].pay_charged.value)+parseInt(document.forms["form1"].pay_check.value) + parseInt(document.forms["form1"].pay_cash.value))
	;
}
function transfer_address()
{
	//alert(document.forms[0].use_same_address.checked);
	if (document.forms[0].same_address.checked == true)
	{	document.forms[0].credit_first_name.value= document.forms[0].guardian_firstname.value
		document.forms[0].credit_middle.value= document.forms[0].guardian_mi.value
		document.forms[0].credit_last_name.value= document.forms[0].guardian_lastname.value
		document.forms[0].credit_phone.value= document.forms[0].guardian_phone_home.value
		document.forms[0].credit_address.value= document.forms[0].guardian_address.value
		document.forms[0].credit_city.value= document.forms[0].guardian_city.value
		document.forms[0].credit_state.value= document.forms[0].guardian_state.value
		document.forms[0].credit_zip.value= document.forms[0].guardian_zip.value
	}
}

function xupdate_the_form (){
	
	
	calculate_total();
}

function contribmonthly(){
	if (document.forms[0].monthly.checked) {
		document.forms[0].months.disabled=false; 
		document.forms[0].months.focus(); 
	}
	else {
		document.forms[0].monthly.focus(); 	
		document.forms[0].months.disabled=true; 		
	}
}
function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=170,left = 200,top = 412');");
}
/* check blank fields - list field names you want to check in this function */
function checkForm(form){
	//alert("checking form 1");
	for (var i =0; i < form.elements.length; i++) {
		var fldname = form.elements[i].name;
		//alert(form.elements[i].name);

		if ((form.elements[i].value=="") || (form.elements[i].value == null)) {	
	
		if ((fldname == 'camper_firstname')){
		alert("Please fill out the Camper's first name.")
		document.forms["form1"].camper_firstname.focus();
				return false
			}	
		if ((fldname == 'camper_lastname')){
		alert("Please fill out the Camper's last name.")
		document.forms["form1"].camper_lastname.focus();	
				return false
			}	
		if ((fldname == 'grade_finished')){
		alert("Please fill out the Camper's grade finished before start of camp.")
		document.forms["form1"].grade_finished.focus();
				return false
			}	
		if (fldname == 'guardian_firstname') {
		alert("Please fill out the Guardian's first name.")	
		document.forms["form1"].guardian_firstname.focus();		
				return false
			}
		if (fldname == 'guardian_lastname') {
		alert("Please fill out the Guardian's last name.")	
		document.forms["form1"].guardian_lastname.focus();			
				return false
			}
		if (fldname == 'guardian_address') {
		alert("Please fill out the Guardian's address.")	
		document.forms["form1"].guardian_address.focus();			
				return false
			}
		if (fldname == 'guardian_city') {
		alert("Please fill out the Guardian's city.")	
		document.forms["form1"].guardian_city.focus();			
				return false
			}
		if (fldname == 'guardian_state') {
		alert("Please fill out the Guardian's state.")	
		document.forms["form1"].guardian_state.focus();			
				return false
			}
		if (fldname == 'guardian_zip') {
		alert("Please fill out the Guardian's zip.")	
		document.forms["form1"].guardian_zip.focus();			
				return false
			}				
		if ((fldname == 'guardian_phone_home')){
		alert("Please enter the Guardian's home phone.")
		document.forms["form1"].guardian_phone_home.focus();				
				return false
			}
		if ((fldname == 'credit_email')){
		alert("Please enter the guardian's email.")	
		document.forms["form1"].credit_email.focus();			
				return false
			}		
		if (fldname == 'credit_first_name') {
		alert("Please fill out the credit first name.")	
		document.forms["form1"].credit_first_name.focus();			
				return false
			}
		if (fldname == 'credit_last_name') {
		alert("Please fill out the credit last name.")	
		document.forms["form1"].credit_last_name.focus();			
				return false
			}
		if (fldname == 'credit_address') {
		alert("Please fill out the credit card address.")	
		document.forms["form1"].credit_address.focus();			
				return false
			}
		if (fldname == 'credit_state') {
		alert("Please fill out the credit card state.")	
		document.forms["form1"].credit_state.focus();			
				return false
			}
		if (fldname == 'credit_zip') {
		alert("Please fill out the credit card zip.")	
		document.forms["form1"].credit_zip.focus();			
				return false
			}						
		if (fldname == 'credit_phone') {
		alert("Please fill out the credit card phone.")	
		document.forms["form1"].credit_phone.focus();			
				return false
			}
		}
	}
	return true
}
function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}	
	return result;
}
function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}
function inValidCharSet(str,charset)
{
	var result = true;
	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	return result;
}
function isValidExpDate()
{
	alert("is this routine used - apparently not");
	var result = true;
	var expired = false;
 	if (result)
 	{
		var month = this.form.expire_month; 			
		var year = this.form.expire_year;
		var now = new Date();
		var nowMonth = now.getMonth() + 1;
		var nowYear = now.getFullYear();
		expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));
 	}
 	if (expired)
	{
	result = false;
	alert('The date for this card has expired.');
	this.form.expm.focus();		
	} 
	return result;
}
function isValidCreditCardNumber(formField,ccType,fieldLabel,required)
{
	var result = true;
 	var ccNum = formField.value;

	if (required && !validRequired(formField,fieldLabel))
		result = false; 
  	if (result && (formField.value.length>0))
 	{ 
 		if (!allDigits(ccNum))
 		{
 			alert('Please enter only numbers (no dashes or spaces) for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}	
		if (result)
 		{ 			
 			if (!LuhnCheck(ccNum) || !validateCCNum(ccType,ccNum))
 			{
 				alert('The car number you entered is not valid. Check the number and the card type');
				formField.focus();
				result = false;
			}	
		} 
	} 

	return result;
}
function LuhnCheck(str) 
{
  var result = true;
  var sum = 0; 
  var mul = 1; 
  var strLen = str.length;
  
  for (i = 0; i < strLen; i++) 
  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) != 0)
    result = false;   
  return result;
}
function GetRadioValue(rArray)
{
	for (var i=0;i<rArray.length;i++)
	{
		if (rArray[i].checked)
			return rArray[i].value;
	}
	
	return null;
}
function validateCCNum(cardType,cardNum)
{
	var result = false;
	cardType = cardType.toUpperCase();
	
	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);

	switch (cardType)
	{
		case "VISA":
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "AMEX":
			var validNums = "47";
			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "MASTER":
			var validNums = "12345";
			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
			break;
		case "DISCOVER":
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		case "DINERS":
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
	}
	return result;
}
function chkmindonation(amt) {
		if ((amt.value <5) || (amt == null)){
			alert('You must select at least one first choice EcoCamp.')
			document.forms["form1"].week1first_id.focus();
			return false
	}
else {
	return true
	}	
}
function chkExpDate(expyselect,expmselect) {
var chosenyear = expyselect.options[expyselect.selectedIndex].text
var chosenmonth = expmselect.options[expmselect.selectedIndex].text

if (expmselect.selectedIndex < 1){
		alert('Please select a month.')
		expmselect.focus();
		return false
	}
	else {
		if (expyselect.selectedIndex < 1){
		alert('Please select a year.')
		expyselect.focus();
		return false
		}
		else {
			
			var testdate = new Date(chosenyear,chosenmonth - 1,"01")
			var today = new Date()
			if (testdate < today) {
				alert ("This card's date has expired. Please correct the date or use another card.")
				expmselect.focus();
				return false
			}
			else { 
				return true
				}
		}
	}
}
function validateCCForm(miform){	
//alert("validate");
//alert("Testing");
	//ccTypeValue = GetRadioValue(miform.creditcard_type);
	//ccNumField = miform.card_number;
	//var result = isValidCreditCardNumber(ccNumField,ccTypeValue,"Card Number",true);
	//if (result == true){
		var result = checkForm(miform);
			if (result == true){
				var result = chkmindonation(miform.cost_requested);
				//if (result == true){
				//	var result = chkExpDate(miform.expire_year,miform.expire_month);	
				//	if(result == true){
						
				//	}
				//}
		//	}
	}	
	return result;
}