// Custom development Robert Mech Software rob@mechsoftware.com
var discount=0.00;
var discountType="";
var total=0;
function submitPayment(eid){
	if(checkForm()==true){
		processRegistration(eid);
	}
}
function updateTotal(){
	total=0;
	total+=parseFloat($('.fPackage:radio:checked').attr('title'));

	// Apply discount before the final total since it's only on packages.
	discountAmt=0;
	if(discountType!=""){
		// Apply Valid Discount
		if(discountType=="dollar"){
			total=total-discount;
			discountAmt=discount;
		}else{
			discountAmt=total*discount;
			total=total-discountAmt;
		}
		if(total<0){
			total=0;
		}
	}

	$('input:checkbox:checked').each(function(){
		total+=parseFloat($(this).attr('title'));
	});
	if (discountAmt == 0) {
		$('#total').html('Total: $' + total);
	}else{
		$('#total').html('<span style="color:red">Discount (on pacakge price only): $'+discountAmt+'</span><br />Total: $'+total);
	}
	
}
function buildFormData(eid){
	var buffer='';
	var classList='';
	buffer+="action: 'register',";
	buffer+="eid: '"+eid+"',";
	buffer+="packageid: '"+$('.fPackage:radio:checked').val()+"',"; // Single Selection
	$('input:checkbox:checked').each(function(){
		classList+=$(this).val()+",";
	});
	buffer+="classids: '"+classList.substring(0, classList.length - 1)+"',"; // multiple Selection list
	$('#paymentForm input[type!="button"]').each(function(){
		buffer+=$(this).attr('name')+": '"+addslashes($(this).val())+"', ";
	});
	buffer+="paymentmethod: '"+$('#paymentMethod option:selected').val()+"', ";
	buffer+="total: '"+total+"', ";
	buffer+="comments: '"+addslashes($('#comments').val())+"'"; // Single Selection
	json=eval("[{"+buffer+"}]");
	return json[0];
}
function processRegistration(eid){
	var registrationid=0;
	$('#actionButton').attr('disabled','disabled');
	$('#actionButton').val('Processing...');
	$.post("registrationService.php", buildFormData(eid),
	  function(data){
		$('#actionButton').removeAttr('disabled');
		$('#actionButton').val('Submit Registration');
	    // alert(data.status); // John
	    registrationid=data.id;
		if(!data.id){
			alert('There was an error processing your registration. ('+data.status+')');	
		}else{
			alert('Your registration has been processed, you will now be forwarded to make payment.  Your Registration id is '+registrationid);
			if($('#paymentMethod option:selected').val()=='check'){
				document.location='http://createncropevents.com/typaymentneeded.html';
			}else{
				// paypal form post.
				$('#item_number').val('#'+registrationid);
				$('#item_name').val('Web Registration #'+registrationid);
				$('#paypal_amount').val(total);
				$('#paypal_button').click();
			}	
		}
	  }, "json");
}
function checkForm(){
	var validation=true;

	// Make sure we selected a package
	if(!$('.fPackage:radio:checked').val()){
			alert("You did not choose a package.  Please go back to the first section and choose a package.");
			validation=false;
			return;
	}

	// Make They didnt want any classes
	if(!$('input:checkbox:checked').val()){
			if (!confirm("Are you sure you did not want to take any classes at this event?")) {
				validation = false;
				return;
			}
	}


	// Validate Payment Form from customer input
	$('#paymentForm input').each(function(){
		if($(this).val()=='' && $(this).attr('title')!=''){
			alert($(this).attr('title')+" is required");
			validation=false;
			return false; // Exit each loop
		}
	});
	
	return validation; // Return true unless previous validation failed.
}
function checkCoupon(couponcode){
	if ($.trim(couponcode) == '') {
		$('#couponStatus').html('');
		return;
	}
	$('#couponStatus').html('Checking...');
	$.getJSON("registrationService.php?action=verifycoupon&coupon="+couponcode,function(data){
		if (data[0].status == null) {
			$('#couponStatus').html('Unable to process coupon.');
		}
		else {
			$('#couponStatus').html(data[0].status);
			if(data[0].status!='Invalid Coupon'){
				$('#couponStatus').css('color','green');
				discount=parseFloat(data[0].discount);
				discountType=data[0].discountType;
			}else{
				$('#couponStatus').css('color','red');
				discount="";
				discountType="";
			}
		}
		updateTotal();
	});

}

function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}
