// reg_form_fax.js
//
// Ron Patterson, BPWC
//
// depends on the jQuery library

var errorMsg ="";
var ind_rate = 99;
var staff_rate = 299;
var ind_penalty = 10;
var staff_penalty = 50;
var reg = "";
var ratesArr;

// function to encode some html chars
unhtml = function (str) {
	var out = "";
	var newstr = str;
	if (newstr.replace) {
		newstr = newstr.replace(/</g,"&lt;");
		out = newstr.replace(/>/g,"&gt;");
	} else out = newstr;
	return out;
}

// validate the form values
form_validate = function ()
{
	var theForm = document.form1;
	// email validator expression
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
	var amtre = /^[0-9]+(\.[0-9]+)*$/;
	// determine the registration type
	$('#typesDiv input:checked').each(function (i) {
		reg = this.value;
	});
	theForm.school_nm.style.backgroundColor = "#ffffff";
	theForm.hdcoach_nm.style.backgroundColor = "#ffffff";
	theForm.hdcoach_email.style.backgroundColor = "#ffffff";
	theForm.contact_nm.style.backgroundColor = "#ffffff";
	theForm.email.style.backgroundColor = "#ffffff";
	theForm.school_adr.style.backgroundColor = "#ffffff";
	theForm.school_city.style.backgroundColor = "#ffffff";
	theForm.school_state.style.backgroundColor = "#ffffff";
	theForm.school_zip.style.backgroundColor = "#ffffff";
	theForm.phone_nbr.style.backgroundColor = "#ffffff";
	theForm.bill_email.style.backgroundColor = "#ffffff";
	theForm.cc_name.style.backgroundColor = "#ffffff";
	theForm.cc_number1.style.backgroundColor = "#ffffff";
	theForm.cc_number2.style.backgroundColor = "#ffffff";
	theForm.cc_number3.style.backgroundColor = "#ffffff";
	theForm.cc_number4.style.backgroundColor = "#ffffff";
	theForm.amount.style.backgroundColor = "#ffffff";
	theForm.cfname_1.style.backgroundColor = "#ffffff";
	theForm.clname_1.style.backgroundColor = "#ffffff";
	if (reg == "") {
	  errorMsg += "     - Registration Type\n";
	}
	// check for possible free promo code
	var freeit = false;
	if (comments.value.search(/NE2010|AFS2010/) >= 0) {
		freeit = true;
		theForm.amount.value = '0';
		theForm.paytype[0].checked = true;
	}
	if (theForm.comments.value.search(/199SAFCF/) >= 0) {
		staff_rate = 199;
	}
//      if ####  validate checkboxes for Clinics
	if (theForm.school_nm.value == "")
	{
	  errorMsg += "     - School Name\n";
	  theForm.school_nm.style.backgroundColor = "#ffd0d0";
	}
	if (theForm.hdcoach_nm.value == "")
	{
	  errorMsg += "     - Head Coach Name\n";
	  theForm.hdcoach_nm.style.backgroundColor = "#ffd0d0";
	}
	if (theForm.hdcoach_email.value == "" || !re.test(theForm.hdcoach_email.value))
	{
	  errorMsg += "     - Head Coach Email Address is invalid\n";
	  theForm.hdcoach_email.style.backgroundColor = "#ffd0d0";
	}
	if (theForm.contact_nm.value == "")
	{
	  errorMsg += "     - Contact Name\n";
	  theForm.contact_nm.style.backgroundColor = "#ffd0d0";
	}
	if (theForm.email.value == "" || !re.test(theForm.email.value))
	{
	  errorMsg += "     - Contact Email Address is invalid\n";
	  theForm.email.style.backgroundColor = "#ffd0d0";
	}
	if (theForm.school_adr.value == "")
	{
	  errorMsg += "     - School Address\n";
	  theForm.school_adr.style.backgroundColor = "#ffd0d0";
	}
	if (theForm.school_city.value == "")
	{
	  errorMsg += "     - School City\n";
	  theForm.school_city.style.backgroundColor = "#ffd0d0";
	}
	if (theForm.school_state.value == "")
	{
	  errorMsg += "     - School State\n";
	  theForm.school_state.style.backgroundColor = "#ffd0d0";
	}
	if (theForm.school_zip.value == "")
	{
	  errorMsg += "     - School Zip Code\n";
	  theForm.school_zip.style.backgroundColor = "#ffd0d0";
	}
	if (theForm.phone_nbr.value == "")
	{
	  errorMsg += "     - Work Phone number\n";
	  theForm.phone_nbr.style.backgroundColor = "#ffd0d0";
	}
	/*
	if (theForm.phone_nbr2.value == "")
	{
	  errorMsg += "     - Evening Phone number\n";
	  theForm.phone_nbr2.style.backgroundColor = "#ffd0d0";
	}
	*/
	if (theForm.bill_email.value == "" || !re.test(theForm.bill_email.value))
	{
	  errorMsg += "     - Billing Email Address is invalid\n";
	  theForm.bill_email.style.backgroundColor = "#ffd0d0";
	}
	var pt = "";
	//alert(typeof theForm.paytype);
	if (typeof theForm.paytype != "undefined")
	  for (var i=0; i<theForm.paytype.length; i++) {
		if (theForm.paytype[i].checked) {pt = theForm.paytype[i].value}
	  }
	if (pt == '') {
	  errorMsg += "     - Payment Type\n";
	}
	if (pt == "CC") 
	{
	  var cc = "";
	  if (typeof theForm.cc_vendor != "undefined")
		  for (var i=0; i<theForm.cc_vendor.length; i++) {
			if (theForm.cc_vendor[i].checked) {cc = theForm.cc_vendor[i].value}
		  }
	  if (cc == "")
	  {
		errorMsg += "     - Credit Card Type\n";
	  }
	  else
	  {
		if (theForm.cc_name.value == "")
		{
		  errorMsg += "     - Credit Card Name\n";
		  theForm.cc_name.style.backgroundColor = "#ffd0d0";
		}
		if ((theForm.cc_number1.value.search(/^[0-9][0-9][0-9][0-9]$/) == -1) ||
			(theForm.cc_number2.value.search(/^[0-9][0-9][0-9][0-9]$/) == -1) || 
			(theForm.cc_number3.value.search(/^[0-9][0-9][0-9][0-9]$/) == -1) || 
			(theForm.cc_number4.value.search(/^[0-9][0-9][0-9][0-9]$/) == -1))
		{
		  errorMsg += "     - Invalid Credit Card Number\n";
		  theForm.cc_number1.style.backgroundColor = "#ffd0d0";
		  theForm.cc_number2.style.backgroundColor = "#ffd0d0";
		  theForm.cc_number3.style.backgroundColor = "#ffd0d0";
		  theForm.cc_number4.style.backgroundColor = "#ffd0d0";
		}
	
		if (theForm.cc_month.value == 0)
		{
		  errorMsg += "     - Credit Card Expiration Month\n";
		}
		if (theForm.cc_year.value == 0)
		{
		  errorMsg += "     - Credit Card Expiration Year\n";
		}
		if (!amtre.test(theForm.amount.value))
		{
		  errorMsg += "     - Total Payment Amount\n";
		  theForm.amount.style.backgroundColor = "#ffd0d0";
		}
	  }
	}
	if (!theForm.chk_confirm.checked) {
	  errorMsg += "     - Confirmation checkbox must be checked\n";	  
	}
	if (theForm.clname_1.value.length == 0 || theForm.cfname_1.value.length == 0) {
	  errorMsg += "     - First Coach must be specified\n";
	  theForm.clname_1.style.backgroundColor = "#ffd0d0";
	  theForm.cfname_1.style.backgroundColor = "#ffd0d0";
	}
	var locs = '';
	eval("var loc_names = ["+$('#locsArr').val()+"];");
	$('#clinicsDiv input:checked').each(function (i) {
	  locs += loc_names[this.value] + ',';
	});
	if (locs == '') {
	  errorMsg += "     - At least one clinic must be checked\n";	  
	}
	
	if (errorMsg == "")
	{
		if (confirm('Really create a faxable page?')) {
			display_form(theForm);
			submit_data();
		}
	}
	else
	{
	  errorMsg = "Please provide or correct the following information: \n" + errorMsg;
	  errorMsg += "\nCheck the highlighted fields.\n";
	  alert(errorMsg);
	  errorMsg = "";
	  theForm.school_nm.focus();
	}
	return false;
}

// display for form results
display_form = function (theForm2)
{
	var types = ["Single Clinic-Individual","Single Clinic-Staff","Season Pass-Individual","Season Pass-Staff"];
	// generate printable report
	var w = window.open("","RegPrint","resizable,status,menubar,scrollbars,width=640,height=600");
	var d = w.document;
	//      var theForm2 = document.form1;
	d.writeln('<b>Use the File/Print menu to print this report to fax your registration');
	d.writeln('and payment to (719) 536-0073.</b>');
	d.writeln('<p><b>Frank Glazier Clinics Registration '+$('#cyear').val()+'</b>');
	d.writeln('<br>');
	
	// determine number of coaches
	var ccount = 0;
	var len = 0;
	$('#coachesDiv input').each(function (i) {
	  if ($.trim(this.value) != "") ccount++;
	});
	//      print Clinics selected
	var locs = "";
	var ttl = 0;
	var x1 = 0;
	eval("var loc_names = ["+$('#locsArr').val()+"];");
	
	// determine the clinics and total amount
	$('#clinicsDiv input:checked').each(function (i) {
	  locs += loc_names[this.value] + ',';
	});
	$('#coachesDiv input').each(function (i) {
	  //if ("$r->loc".match(/Season Pass/)) reg = 2;
	  //alert(this.name); return false;
	  if ($.trim(this.value) == '') return true; // continue
	  if (this.name.search(/^clname/) == -1) return true; // continue
	  if (reg == 2)
		  ttl += ind_rate; //arr[x1][reg];
	  else
		  ttl = staff_rate; //arr[x1][reg];
	});
	//alert(locs);
	
	if (locs == "") {
	alert("No clinics were selected");
	return false;
	}
	//if (reg == 0 || reg == 2) {ttl = ttl * ccount;}
	d.writeln('<p><b>Registering as</b> ' + types[reg] + ' <b>for</b> ' + locs);
	d.writeln('<br><b>School Name:</b> ' + unhtml(theForm2.school_nm.value));
	d.writeln('<br><b>Head Coach Name:</b> ' + unhtml(theForm2.hdcoach_nm.value));
	d.writeln('<br><b>Head Coach Email Address:</b> ' + unhtml(theForm2.hdcoach_email.value));
	d.writeln('<br><b>Contact Name:</b> ' + unhtml(theForm2.contact_nm.value));
	d.writeln('<br><b>Email Address:</b> ' + unhtml(theForm2.email.value));
	d.writeln('<br><b>Address:</b> ' + unhtml(theForm2.school_adr.value));
	d.writeln('<br><b>City:</b> ' + unhtml(theForm2.school_city.value));
	d.writeln('<br><b>State:</b> ' + unhtml(theForm2.school_state.value));
	d.writeln('<br><b>Zip Code:</b> ' + unhtml(theForm2.school_zip.value));
	d.writeln('<br><b>Work Phone:</b> ' + unhtml(theForm2.phone_nbr.value));
	d.writeln('<br><b>Extension:</b> ' + unhtml(theForm2.phone_ext.value));
	d.writeln('<br><b>Cell Phone:</b> ' + unhtml(theForm2.phone_nbr2.value));
	d.writeln('<br><b>Fax:</b> ' + unhtml(theForm2.fax_nbr.value));
	d.writeln('<br><b>Billing Email Address:</b> ' + unhtml(theForm2.bill_email.value));
	//if (theForm2.confirm.checked) {
	//   d.writeln('<br><b>Confirmation requested</b>');
	//}

	// determine the payment type
	var pt = "";
	$('#paytypesDiv input:checked').each(function (i) {
		pt = this.value;
	});
	d.writeln('<br><b>Payment type:</b> ' + unhtml(pt));
	
	// if credit card then get card billing info
	if (pt == 'CC') {
		var cc = "";
		$('#cardsDiv input:checked').each(function (i) {
			cc = this.value;
		});
		d.writeln('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>********************************</b>');
		d.writeln('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Card Vendor:</b> ' + unhtml(cc));
		d.writeln('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Name:</b> ' + unhtml(theForm2.cc_name.value));
		d.writeln('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Billing info:</b> ' + unhtml(theForm2.bill_adr.value) + ", " + unhtml(theForm2.bill_city.value) + ", " + unhtml(theForm2.bill_state.value) + ", " + unhtml(theForm2.bill_zip.value));
		d.writeln('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Number:</b> ' + unhtml(theForm2.cc_number1.value + ' ' + theForm2.cc_number2.value + ' ' + theForm2.cc_number3.value + ' ' + theForm2.cc_number4.value) + "  <b>Sec. code:</b> " + unhtml(theForm2.bill_code.value));
		d.writeln('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Exp month/year:</b> ' + unhtml(theForm2.cc_month.options[theForm2.cc_month.selectedIndex].text + ', ' + theForm2.cc_year.options[theForm2.cc_year.selectedIndex].text));
		d.writeln('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Amount:</b> ' + unhtml(theForm2.amount.value));
		d.writeln('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Signature:</b> ____________________________________________');
		d.writeln('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Signature required for Credit Card purchase');
		d.writeln('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>********************************</b>');
	}
	if (pt == 'PO') {
		d.writeln('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>PO #:</b> ' + unhtml(theForm2.ponbr.value));
	}
	d.writeln('<br><b>Total cost:</b> $' + ttl);
	// show coach names
	var name;
	var count = 1;
	$('#coachesDiv input').each(function (i) {
		if (this.name.match(/^cfname/)) name = this.value;
		if (this.name.match(/^clname/)) {
			name = name + ' ' + this.value;
			if ($.trim(name) != "") d.writeln('<br><b>Coach '+(count++)+':</b> ' + unhtml(name));
		}
	});
	d.writeln('<br>Athletic Director\'s Name: ' + unhtml(theForm2.ath_dir_nm.value + ', Phone: ' + theForm2.ath_dir_phn.value));
	d.writeln('<br>President\'s Name: ' + unhtml(theForm2.league_pres.value + ', Phone: ' + theForm2.league_phone.value));
	//d.writeln('<p>Topic 1: ' + unhtml(theForm2.top_1.value));
	//d.writeln('<br>Topic 2: ' + unhtml(theForm2.top_2.value));
	//d.writeln('<br>Topic 3: ' + unhtml(theForm2.top_3.value));
	d.writeln('<br>Comments: ' + unhtml(theForm2.comments.value));
	d.writeln('<p>I understand that all the coaches listed above must coach at the named school or affiliated junior high school. Each coach must present a photo ID at each clinic.');
	d.writeln('<p>Signed by: ____________________________________________');
	d.writeln('<br>&nbsp;&nbsp;&nbsp;&nbsp;Signature of Head Coach or Athletic Director');
	d.writeln('<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(For Staff registrations only)</p>');
	d.writeln('<p><b>TO COMPLETE YOUR REGISTRATION PLEASE PRINT THIS FORM AND FAX TO 719-536-0073.</b></p>');
	d.writeln('<p>Or mail to:<br>Frank Glazier Clinics / MEGA CLINIC<BR>P.O. Box 63673<br>Colorado Springs, CO 80962-3673</p>');
	d.close();
}

submit_data = function () {
	//alert('submit_data');
	var data = $('#form1').serializeArray();
	// remove the unwanted CC fields
	for (var x in data) {
		if (data[x].name == 'cc_number1') delete data[x];
		else if (data[x].name == 'cc_number2') delete data[x];
		else if (data[x].name == 'cc_number3') delete data[x];
		else if (data[x].name == 'cc_number4') delete data[x];
		else if (data[x].name == 'bill_code') delete data[x];
	}
	//alert('x='+data[1].name);
	$.post("../megaclinic/regsubmitfaxAjax.php",data,function (msg) {
		//alert(msg);
		//$('#msg_area').html('<pre>'+msg+'</pre>');
		$('#msg_area').text(msg);
	});
}

handle_paytype = function (event) {
	var obj = event.target;
	if (obj.id == 'ponbr') return true;
	if (obj.value == 'CC') $('#ccinfoDiv').show(); else $('#ccinfoDiv').hide();
	if (obj.value == 'PO') $('#ponbrDiv').show(); else $('#ponbrDiv').hide();
	return true;
}

// calculate the amount due
calc_amt = function (event) {
	var ttlamt = 0;
	// determine the registration type
	$('#typesDiv input:checked').each(function (i) {
		reg = this.value;
	});
	if (reg == "") {
	  alert("ERROR: No registration type selected!");
	  return false;
	}
	// check for possible free promo code
	var freeit = false;
	if ($('#comments').val().search(/NE2010|AFS2010/) >= 0)
		freeit = true;
	if (freeit) {
		$('#amount').val(0);
		return true;
	}
	// determine the clinics penalty
	var gbl_penalty = false;
	$('#clinicsDiv input').each(function (i) {
		if (this.checked && ratesArr[i][4] == 1) gbl_penalty = true;
	});
	var coaches = 0;
	$('#coachesDiv input').each(function (i) {
	  if ($.trim(this.value) == '') return true; // continue
	  if (this.name.search(/^clname/) == -1) return true; // continue
	  if (reg == 2)
		  ttlamt += ind_rate;
	  else
		  ttlamt = staff_rate;
	  coaches++;
	});
	if (coaches == 0) {
	  alert("ERROR: No coaches specified!");
	  return false;
	}
	if (gbl_penalty) {
		if (reg == 2) {
			ttlamt += ind_penalty * coaches;
		}
		if (reg == 3) {
			ttlamt += staff_penalty;
		}
	}
	if (ttlamt < 0) ttlamt = 0;
	$('#amount').val(ttlamt);
	return true;
}

$(document).ready(function () {
	$('#paytypesDiv input').click(handle_paytype);
	$('#calc').click(calc_amt);
	eval("ratesArr = ["+$('#ratesArr').val()+"];");
});
