error_yet=0;
price_off=1;
$(document).ready(function() {
	$(".error").hide();
	$("#confirm_b").click(function() { go(); });
	
	$('input[type=text]').keyup(function(e) {
		if(e.keyCode == 13) {
			go();
		} else {
			if (error_yet==1) {
				update_errors();
			}
		}
	});	
	$("#creditcard_month").change(function() {
		val=$(this).val();
		if (val=="1") {
			$("#mon_len").text("month");
			price=(val*5)*price_off;
		} else {
			$("#mon_len").text("months");
			price=(val*5)*price_off;	
		}
		price=Math.round(price);
		$("#r_total").val("$" + price); 
		$("#item_name").val("Premium Membership For " + val + " Months");
		$("#r_months").val(val);
		$("#amount").val(price);
		n="f";
		if (l_in==true) { n="t"; }
		$("#return").val("http://uploadrobots.com/pay-" + val + "-" + n);
	});
	$("#r_coupon").keyup(function() {
price_off=1;
$("#discount_right").stop().hide();
		switch (SHA1($(this).val())) {
			case "5fac289d620b94fd5443e91cfeab5c642c9ad494":
				price_off=.70;
				$("#discount_wrong").hide();
				setTimeout(function() { $("#discount_right").fadeIn().find("#discount_off").text("30%"); }, 200);
				break;
case "6570eb14528d9103c53a061db1a37b54c355d39f":
price_off=.80;
				$("#discount_wrong").hide();
				setTimeout(function() { $("#discount_right").fadeIn().find("#discount_off").text("20%"); }, 200);
break;
			default:
				$("#discount_wrong").fadeIn();
				break;
		}
		$("#creditcard_month").change();
	});
});
function result(msg, style) {
	if (style=="error") {
		error_yet=1;
	}
	if (!$(".result").hasClass(style)) {
		$(".result").attr("class","result " + style);
	}
	$(".r_list").text(msg);
	$(".result").fadeIn(); 
}
function update_errors() {
	n=$("#r_name").val();
	e=$("#r_email").val();
	p1=$("#r_pass").val();
	p2=$("#r_pass2").val();
	$("input[type=text], input[type=password]").each(function() {
		$(this).css("color","#565656"); 
	});
	if (n.length<4) { result("Username too short.", "error"); $("#r_name").css("color","red"); return false; }
	if (n.length>16) { result("Username too long.", "error"); $("#r_name").css("color","red"); return false; }
	if (p1.length<4) { result("Password too short.", "error"); $("#r_pass").css("color","red"); return false; }
	if (p1!=p2) { result("Passwords must match.", "error"); $("#r_pass").css("color","red");  $("#r_pass2").css("color","red"); return false; }
	if (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(e)==false) { $("#r_email").css("color","red"); result("Email must be valid.", "error"); return false; }
	$(".result").fadeOut();
	return true;
}

function go() {
	if (l_in==false) {
		if (update_errors()==true) {
			if (update_errors()==true) {
				$.get("ajax.php",{action: "register", user:$("#r_name").val(), email:$("#r_email").val(), pass:SHA1(SHA1($("#r_pass").val()))}, function(r) {
					r=eval("(" + r + ")");
					if (r['error']==0) {
						$("#payform").submit();
					} else {	
						 result(r['message'], "error");	
					}
				});
			}
		}
	} else {
		$("#payform").submit();
	}	
}
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}