/*
trimTrailingWhiteSpace : function ( myString ) {
	return myString.replace(/\s*$/, '');
}
*/
$(document).ready(function() {
	
	var mtotal = 0; // mooncake total price
	$("div.tips").cluetip({
		local:true,
		cursor: 'pointer',
		cluetipClass: 'jtip',
		positionBy: 'bottomTop',
		arrows: true,
		closePosition: 'title',
		closeText: '<img src="/brand/images/close.png" alt="close" />',
		sticky: true,
		hideLocal: true,
		hoverIntent: false
	});
	
	$("a.tips").cluetip({
		local:true,
		cursor: 'pointer',
		cluetipClass: 'jtip',
		positionBy: 'bottomTop',
		arrows: true,
		closePosition: 'title',
		closeText: '<img src="/brand/images/close.png" alt="close" />',
		sticky: true,
		hideLocal: true,
		hoverIntent: false
	});
	
	$("input[name='dontype']").click(
		function() {
			switch($(this).val()) {
				case "1":
					$(".type2").hide();
					$(".type3").hide();
					$(".type4").hide();
					$(".type1").slideDown("slow");
					break;
				case "2":
					$(".type1").hide();
					$(".type3").hide();
					$(".type4").hide();
					$(".type2").slideDown("slow");
					break;
				case "3":
					$(".type1").hide();
					$(".type2").hide();
					$(".type4").hide();
					$(".type3").slideDown("slow");
					break;
				case "4":
					$(".type1").hide();
					$(".type2").hide();
					$(".type3").hide();
					$(".type4").slideDown("slow");
					break;
			}
		}
	);
	
	$(".multidoncont").ajaxStart(function() {
			$(".multidoncont").html('<div class="loading1"><img src="/images/tw/loading.gif" width="126" height="22" /></div>');
	});
	
	$("#type11t").bind("keyup", function() {
		var tmpamount = $(this).val();
		var count = 1;
		//console.log(tmpamount);
		if (tmpamount >= 3900) {
			count = parseInt(tmpamount/1950);
			//console.log(count);
			if (count > 1) {
				$(".multidoncont").load('/template/community/tw/multi_donor.php',{count: count});
			}
			$("input[name='reg[donor_number]']").val(count);
		} else {
			$(".multidoncont").text('');
		}
		// add multiple donor input fields
		
	});
	
	$("input[name='type11r']").click(
		function() {
			$(this).val() == 'other'?$(this).next().removeAttr("disabled").focus():$("#type11t").attr("disabled","disabled");
		}
	);
	
	$("input[name='type12r']").click(
		function() {
			$(this).val() == 'other'?$(this).next().removeAttr("disabled").focus():$("#type12t").attr("disabled","disabled");
		}
	);
	
	$("input[name='type21r']").click(
		function() {
			$(this).val() == 'other'?$(this).next().removeAttr("disabled").focus():$("#type21t").attr("disabled","disabled");
		}
	);
	
	$("input[name='type22r']").click(
		function() {
			$(this).val() == 'other'?$(this).next().removeAttr("disabled").focus():$("#type22t").attr("disabled","disabled");
		}
	);
	
	$("#mooncaketext").bind('keyup',function() {
											 mtotal = $(this).val();
											 mtotal = mtotal * 25;
											 $("#mooncaketotal").html(mtotal);
											 });
	
	$("input[name='type31r']").click(
		function() {
			$(this).val() == 'other'?$(this).next().removeAttr("disabled").focus():$("#type31t").attr("disabled","disabled");
		}
	);
	
	$("input[name='type32r']").click(
		function() {
			$(this).val() == 'other'?$(this).next().removeAttr("disabled").focus():$("#type32t").attr("disabled","disabled");
		}
	);
	
	$("input[name^='donorname']").click(function() {
		//$(".donornametext").val("");
		if ($(this).val() == 'chi') {
			$(this).next().next().attr("maxlength","6").removeAttr("disabled").focus();
		} else {
			$(this).next().attr("maxlength","15").removeAttr("disabled").focus();
		}
	});
	
	$("#form2submit").click(
		function() {
			//console.log("QQ");
			//console.log($("#mooncaketext").val());
			//alert($("input[name='type1r']:checked").val());
			var total = 0;
			var oneoff = 0;
			var monthly = 0;
			var error = 0;
			var error2 = 0;
			var error3 = 0; // for detect if amount is entered when others is selected
			var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
			switch($("input[name='dontype']:checked").val()) { // check for which type of donation
				case "1":
					$("input[name='type1c[]']:checked").each(function() { // selected first option of radion button group
						if ($(this).val() == '1co1') { // one off payment
							if ($("input[name='type11r']:checked").val() == 'other') { // type amount by user
								//console.log(isNaN($("#type11t").val()));
								//console.log($("#type11t").val());
								if (isNaN($("#type11t").val()) == false && $("#type11t").val() != '') { // check whether the input is numeric
									oneoff = parseInt($("#type11t").val()); // save varbiable for later form post
									total += parseInt($("#type11t").val());
								} else { error3++; }
							} else { // select perdefined amount from radio button
								oneoff = parseInt(($("input[name='type11r']:checked").val()));
								total += parseInt(($("input[name='type11r']:checked").val()));
							}
						}
						if ($(this).val() == '1co2') { // monthly
							//alert($("input[name='type12r']:checked").val());
							if ($("input[name='type12r']:checked").val() == 'other') {
								if (isNaN($("#type12t").val()) == false && $("#type12t").val() != '') {
									monthly = parseInt($("#type12t").val()); // save variable for later form post
									total += parseInt($("#type12t").val());
									//console.log($("#type12t").val());
								} else { error3++; }
							} else {
								monthly = parseInt(($("input[name='type12r']:checked").val()));
								total += parseInt(($("input[name='type12r']:checked").val()));
							}
						}
					});
					break;
					
				case "2":
					//alert($("input[name='type2c']:checked").val());
					$("input[name='type2c[]']:checked").each(function() {
						if ($(this).val() == '2co1') {
							if ($("input[name='type21r']:checked").val() == 'other') {
								if (isNaN($("#type21t").val()) == false && $("#type21t").val() != '') {
									oneoff = parseInt($("#type21t").val()); // save varbiable for later form post
									total += parseInt($("#type21t").val());
								} else { error3++; }
							} else {
								oneoff = parseInt(($("input[name='type21r']:checked").val()));
								total += parseInt(($("input[name='type21r']:checked").val()));
							}
						}
						if ($(this).val() == '2co2') {
							if ($("input[name='type22r']:checked").val() == 'other') {
								if (isNaN($("#type22t").val()) == false && $("#type22t").val() != '') {
									monthly = parseInt($("#type22t").val());
									total += parseInt($("#type22t").val());
								} else { error3++; }
							} else {
								monthly = parseInt(($("input[name='type22r']:checked").val()));
								total += parseInt(($("input[name='type22r']:checked").val()));
							}
						}
						if ($(this).val() == '2co3') {
								oneoff = parseInt(mtotal);
								total += parseInt(mtotal);
						}
					});
					break;
					
				case "3":
					$("input[name='type3c[]']:checked").each(function() {
						if ($(this).val() == '3co1') {
							if ($("input[name='type31r']:checked").val() == 'other') {
								if (isNaN($("#type31t").val()) == false && $("#type31t").val() != '') {
									oneoff = parseInt($("#type31t").val());
									total += parseInt($("#type31t").val());
								} else { error3++; }
							} else {
								oneoff = parseInt(($("input[name='type31r']:checked").val()));
								total += parseInt(($("input[name='type31r']:checked").val()));
							}
						}
						if ($(this).val() == '3co2') {
							if ($("input[name='type32r']:checked").val() == 'other') {
								if (isNaN($("#type32t").val()) == false && $("#type32t").val() != '') {
									monthly = parseInt($("#type32t").val());
									total += parseInt($("#type32t").val());
								} else { error3++; }
							} else {
								monthly = parseInt(($("input[name='type32r']:checked").val()));
								total += parseInt(($("input[name='type32r']:checked").val()));
							}
						}
					});
					break;
				case "4":
					if (isNaN($("#type41t").val()) == false && $("#type41t").val() != '') {
						oneoff = parseInt($("#type41t").val());
						total += parseInt($("#type41t").val());
					} else { error3++; }
					break;
			}
			$("input[name='reg[pay_amt]']").val(total);
			$("input[name='reg[donation_type]']").val($("input[name='dontype']:checked").val());
			$("input[name='reg[oneoff]']").val(oneoff);
			$("input[name='reg[monthly]']").val(monthly);
			//console.log($("input[name='reg[pay_amt]']").val());
			if ($("input[name='reg[fullname_tw]']").val() == '') { // form validation
				$("#formchiname").addClass("error");
				error2++;
			} else {
				$("#formchiname").removeClass("error");
			}
			if ($("input[name='reg[fullname]']").val() == '') {
				$("#formengname").addClass("error");
				error2++;
			} else {
				$("#formengname").removeClass("error");
			}
			//alert($("input[name='reg[gender]']").val());
			if ($("input[name='reg[gender]']").val() == '') {
				$("#fromgender").addClass("error");
				error2++;
			} else {
				$("#fromgender").removeClass("error");
			}
			//console.log($("#textarea1").val());
			if ($("#textarea1").val() == '') {
				$("#formaddr").addClass("error");
				error2++;
			} else {
				$("#formaddr").removeClass("error");
			}

			if ($("input[name='reg[tel]']").val() == '') {
				$("#formtel").addClass("error");
				error2++;
			} else {
				$("#formtel").removeClass("error");
			}
			
			if (emailfilter.test($("input[name='reg[email]']").val()) == false) {
				$("#formemail").addClass("error");
				error2++;
			} else {
				$("#formemail").removeClass("error");
			}
			//console.log(total);
			if (total == 0 || isNaN(total) == true) {
				$("#formdontype").addClass("error");
				error++;
			} else {
				$("#formdontype").removeClass("error");
			}
			if (error > 0) {
				$(".warningmsg").show();
			} else { $(".warningmsg").hide(); }
			if (error3 > 0) {
				$(".warningmsg3").show();
			} else { $(".warningmsg3").hide(); }
			if (error2 > 0) {
				$(".warningmsg2").show();
			} else { $(".warningmsg2").hide(); }
			if (error == 0 && error2 == 0 && error3 == 0) {
				$("#form2").submit();
			}
		}
	);
	
	
	if ($("input[name='dontype']:checked").val() != '') {
		if ($("input[name='dontype']:checked").val() == 1) {
			$(".type2").hide();
			$(".type3").hide();
			$(".type4").hide();
			$(".type1").slideDown("slow");
		} else if ($("input[name='dontype']:checked").val() == 2) {
			$(".type1").hide();
			$(".type3").hide();
			$(".type4").hide();
			$(".type2").slideDown("slow");
		} else if ($("input[name='dontype']:checked").val() == 3) {
			$(".type1").hide();
			$(".type2").hide();
			$(".type4").hide();
			$(".type3").slideDown("slow");
		} else if ($("input[name='dontype']:checked").val() == 4) {
			$(".type1").hide();
			$(".type2").hide();
			$(".type3").hide();
			$(".type4").slideDown("slow");
		}
	}
	
});
