// JQuery Document
// Author: Wayne Southey - Planethome Designs

$(document).ready(function(){


 $("#phone").mask("(99)9999 9999"); 
 $("#fax").mask("(99)9999 9999"); 						   
 $("#postcode").mask("9999");	
 
 $("#submit").attr("disabled", true)
 
 if ($("#chk_agree:checked")==true) { $("#submit").attr("disabled", false);}
 
 $("#chk_agree").click(function() {
	var checked =$(this).is(':checked');						
	if (checked) 
	{ 
		$("#submit").attr("disabled", false);
	} else {
		$("#submit").attr("disabled", true);
		alert("You must agree to the terms and conditions to submit this form"); 
	}
 });
 
 var pkg0 = fixnum($("#pkg0").html()); 
 var pkg1 = fixnum($("#pkg1").html()); 
 var pkg2 = fixnum($("#pkg2").html()); 
 var pkg3 = fixnum($("#pkg3").html()); 
 var opt1 = fixnum($("#popt1").html());
 var opt2 = fixnum($("#popt2").html());
 var opt3 = fixnum($("#popt3").html());
 
 $(".chkpackage").click(function() {
						
	var chkid = $(this).attr("ID"); 
	
	$(".chkpackage").attr("checked", false);
	$("#"+chkid).attr("checked", true);
 });
 
  /*$(".chkoption").click(function() {
						
	var chkid = $(this).attr("ID"); 
	
	$(".chkoption").attr("checked", false);
	$("#"+chkid).attr("checked", true);
 })*/
 
 check_package(); 

 $("#appForm").validate({
	   rules: {
    	password: {
      		minlength: 6
    		}, 
		e_mail: {
			required: true,
			email: true,
			remote: "members/includes/chk_email.php"
			}	
	   },
	   messages: {
		 e_mail: {
			required: "Please enter a valid email address, example: you@yourdomain.com",
			remote: jQuery.format("{0} is already taken, please enter a different address.")
		 }
	  },
	  debug:false
	  
	});
 
 $("#package-name, #package-gold, #package-silver, #package-platinum, #marketoption1, #marketoption2, #marketoption3").click(function() { 
	//alert($(this).attr("id") + " clicked");																	
	check_package(); 
 });

$("#mkt_opt1_months, #mkt_opt2_months, #mkt_opt3_months").click(function() { 
	//alert($(this).attr("id") + " clicked");																	
	check_package(); 
 });
 
function check_package() { 
 
 	var val_name = ($("#package-name").is(':checked'))?pkg0:0;
	var val_silver = ($("#package-silver").is(':checked'))?pkg1:0;
 	var val_gold = ($("#package-gold").is(':checked'))?pkg2:0;
	var val_plat = ($("#package-platinum").is(':checked'))?pkg3:0;
	
	var name_pkg_only =  ($("#package-name").is(':checked'));
	//Resets the market options to false if name package only 
	reset_checked("marketoption1", name_pkg_only);
	reset_checked("marketoption2", name_pkg_only);
	reset_checked("marketoption3", name_pkg_only)
		
	
	reset_select("mkt_opt1_months", $("#marketoption1").is(':checked'));
	
	var val_opt1 = ($("#marketoption1").is(':checked'))?opt1*$("#mkt_opt1_months").val():0;
	$("#mkt_opt1_total").html(val_opt1);
	
	reset_select("mkt_opt2_months", $("#marketoption2").is(':checked'));
	var val_opt2 = ($("#marketoption2").is(':checked'))?opt2*$("#mkt_opt2_months").val():0;
	$("#mkt_opt2_total").html(val_opt2);
	
	reset_select("mkt_opt3_months", $("#marketoption3").is(':checked'));
	var val_opt3 = ($("#marketoption3").is(':checked'))?opt3*$("#mkt_opt3_months").val():0;
	$("#mkt_opt3_total").html(val_opt3);
	//alert(val_opt1); 
	
	var total_pkg = val_name*1+val_gold*1+val_silver*1+val_plat*1;
	
	var total_opt = val_opt1*1+val_opt2*1+val_opt3*1;
	//alert(total_opt); 
	var total_all = total_pkg+total_opt; 
	//alert("total " + total_pkg); 
	$("#package_total").html(total_pkg);
	
	$("#total_all").html(total_all);
	
}
function reset_select(sid, boolval) {	
	
	if (boolval) {	
		$("#"+sid).attr("disabled", false);
	} else { 
		$("#"+sid).attr("disabled", true);
	}
}
	
function reset_checked(sid, boolval) {	
	
	if (boolval) {	
		$("#"+sid).attr("disabled", true);
		$("#"+sid).attr("checked", false);
	} else { 
		$("#"+sid).attr("disabled", false);
		
	}
}		



function fixnum(aNum) {

	//remove any commas
	
	aNum=aNum.replace(/,/g,"");
	
	//remove any spaces
	aNum=aNum.replace(/\s/g,"");
	aNum=aNum.replace(/\$/g,"");
	
	return aNum;

}//end of removeCommas(aNum)


 
});