$.validator.setDefaults({
     submitHandler: function() {  

	//if(verify_dob())
	//{
		$.ajax({
			type: "POST",
			url: "/signup/ajax/account/",
			/*dataType: "xml",*/
			data: {
				email: $("#email").val(),
				username: $("#username").val(),
				password: $("#password").val(),
				gender: $("input[@name='gender']:checked").val(),
				dob_year: $("#dob_year").val(),
				dob_month: $("#dob_month").val(),
				dob_day: $("#dob_day").val(),
				country_code: $("#country").val(),
				firstname: $("#firstname").val(),
				lastname: $("#lastname").val(),
				optin_newsletter: $("#optin_newsletter:checked").val(),
				optin_partner: $("#optin_partner:checked").val(),
				src: $("#rego_src").val(),
				promo_code: $("#promo_code").val(),
				request_type: 'signup'
			},
                        beforeSend: function(){
                                $("#signup_form_msg").html('<img src="/images/common/loading.gif" /> saving...');
                        },
			success: function(xml){

				$("#signup_form_msg").html('');
				if($("status",xml).text() == "0")
				{
					$("error",xml).each(function(id){
						error = $("error",xml).get(id);
						error_field = $("field", error).text();
						error_msg = $("message", error).text();
						$("#" + error_field + "_error").html('<em class="signup_error">'+error_msg+'</em>');
					});
				} else
				{
					if($("#callback_func").length>0)
					{
						var callback_func = $("#callback_func").val();
						eval(callback_func + '()');

						reload_login_box();
						$("#dialog_box").remove();
						$("#dim_overlay").remove();
					} else
					{
						$("#signup_form *").attr("disabled", "disabled");
				                $("#dim_overlay").show();
						$("#signup_result").html(xml);
				                $("#dialog_box_1").css("z-index", 102).show();
						$("#dialog_box_1_content").show();
				                $("#dialog_box_2").show();
			        	        $("#dialog_box_3").show();
					}
				}
			},
			error: function() {
				$("#signup_form_msg").html('Sorry. Please try again.');
			}
		});
	//} else
	//{
	//	return false;
	//}
    }
});

$(document).ready(function() {
	$('#dob').datepicker({
		mandatory: true,
		yearRange: '1960:1999',
		minDate: new Date(1960, 1 - 1, 1),
		maxDate: new Date(1999, 12 - 1, 31),
		defaultDate: new Date(1980, 1 - 1, 1),
		dateFormat: 'dd-M-yy',
		highlightWeek: true,
		beforeShow: function() {
			$("#ui-datepicker-div").css("z-index", "102");
		},
		onSelect: function() { 
			//$("#dob").trigger('focus');
			$("#dob").trigger('blur');
		} 
	});

	$("#firstname").focus(function() {
		if($(this).val()=="first name") $(this).val('');
	});

        $("#lastname").focus(function() {
                if($(this).val()=="last name") $(this).val('');
        });
        // check username availablity
        $("#availability, #availability_img").css("cursor", "pointer").click(function() {
                $.ajax({
                        type: "POST",
                        url: "/signup/ajax/availability/",
                        //dataType: "xml",
                        data: {
                                username: $("#username").val(),
                                request_type: "check_availability"
                        },
			beforeSend: function(){
				$("#username_error").html("<img src='/images/common/loading.gif' />");
			},
                        success: function(xml){
				$("#username_error").html(xml);
                        }
                });
        });

	$("#signup_form input").focus(function(){
		var item_id = $(this).attr("id");
		$("#"+item_id+"_error").html('');
	});

	// validate signup form on keyup and submit
	$("#signup_form").validate({
		errorElement: "em",
		errorClass: "signup_error",
		focusInvalid: false,
		errorPlacement: function(error, element) {
			var item_id = element.attr("id");
			error.appendTo( $("#"+item_id+"_error")  );
		},
		success: function(label) {
			label.addClass("signup_success").html('&nbsp;');
		},
		rules: {
                        email: {
                                required: true,
                                email: true
                        },
			username: {
				required: true,
				alphanumeric: true,
				minlength: 3
			},
			password: {
				required: true,
				minLength: 6
			},
			confirm_password: {
				required: true,
				minlength: 6,
				equalTo: "#password"
			},
			gender: "required",
			country: "required"
		},
                messages: {

                        email: "Please enter a valid email address",

                        username: {
                                required: "Please enter a username",
                                alphanumeric: "Username must only consist of characters, numbers or dash",
                                minLength: "Your username must consist of at least 3 characters"
                        },
                        password: {
                                required: "Please provide a password",
                                minLength: "Your password must be at least 6 characters long"
                        },
                        confirm_password: {
                                required: "Please provide a password",
                                minLength: "Your password must be at least 6 characters long",
                                equalTo: "Please enter the same password as above"
                        },
                        gender: "Please select gender",
                        country: "Please select a country"
                }
	});


	$("#pre_btn_1").click(function(){
		if($("#redirect_url").val()!="")
		{
			window.location = $("#redirect_url").val();
		} else
		{
			window.location = $("#member_permalink").val();  
		}
	});

	$("#next_btn_1").click(function() {

                $.ajax({
                        type: "POST",
                        url: "/signup/ajax/profile/",
                        data: {
                                favorite_songs: $("#favorite_songs").val(),
                                favorite_albums: $("#favorite_albums").val(),
                                favorite_artists: $("#favorite_artists").val(),
                                favorite_lyric_line: $("#favorite_lyric_line").val(),
                                favorite_genres: $("#favorite_genres").val(),
                                profile_type: "music_preferences"
                        },
                        beforeSend: function(){
				$("#dialog_1_msg").show().html('<img src="/images/common/loading.gif" /> saving...');	
                        },
                        success: function(xml){
                                //if($("status",xml).text() == "0")
                                //{

                                //} else
                                //{
					$("#dialog_1_msg").hide().html('');
					$("#dialog_box_1").css("z-index", 0);
					$("#dialog_box_1_content").hide();
					$("#dialog_box_2").css("z-index", 102);
					$("#dialog_box_2_content").show();
                                //}
                        },
			error: function() {
                                        $("#dialog_1_msg").hide().html('');
                                        $("#dialog_box_1").css("z-index", 0);
                                        $("#dialog_box_1_content").hide();
                                        $("#dialog_box_2").css("z-index", 102);
                                        $("#dialog_box_2_content").show();
			}
                });
	});

	$("#pre_btn_2").click(function() {
		$("#dialog_box_1").css("z-index", 102);
		$("#dialog_box_1_content").show();
		$("#dialog_box_2").css("z-index", 0);
		$("#dialog_box_2_content").hide();
	});

	$("#next_btn_2").click(function() {

                $.ajax({
                        type: "POST",
                        url: "/signup/ajax/tag/",
                        data: {
                                user_tags: $("#user_tags").val(),
                                profile_type: "user_tags"
                        },
                        beforeSend: function(){
				$("#dialog_2_msg").show().html('<img src="/images/common/loading.gif" /> saving...');
                        },
                        success: function(xml){
                                //if($("status",xml).text() == "0")
                                //{
                                //} else
                                //{
					$("#dialog_2_msg").hide().html('');
					$("#dialog_box_2").css("z-index", 0);
					$("#dialog_box_2_content").hide();
					$("#dialog_box_3").css("z-index", 102);
					$("#dialog_box_3_content").show();
                                //}
                        },
			error: function() {
                                        $("#dialog_2_msg").hide().html('');
                                        $("#dialog_box_2").css("z-index", 0);
                                        $("#dialog_box_2_content").hide();
                                        $("#dialog_box_3").css("z-index", 102);
                                        $("#dialog_box_3_content").show();
			}
                });
	});

	$("#pre_btn_3").click(function() {
		$("#dialog_box_3").css("z-index", 0);
		$("#dialog_box_3_content").hide();
		$("#dialog_box_2").css("z-index", 102);
		$("#dialog_box_2_content").show();
	});

	$("#next_btn_3").click(function() {
		$("#dialog_box_1").hide();
		$("#dialog_box_2").hide();
		$("#dialog_box_3").css("z-index", 0).hide();
		$("#dialog_box_3_content").hide();
		$("#dim_overlay").hide();

                if($("#redirect_url").val()!="")
                {
                        window.location = $("#redirect_url").val();
                } else
                {
                        window.location = $("#member_permalink").val();
                }
	});


	$("#email_password").click(function() {
		$("#email_password").val("");
	});

	$("#get_contacts").click(function() {
                $.ajax({
                        type: "POST",
                        url: "/signup/ajax/invite/",
                        data: {
                                email_account: $("#email_account").val(),
				email_domain:  $("#email_domain").val(),
				email_password: $("#email_password").val(),
                                request_type: "get_email_contacts"
                        },
                        beforeSend: function(){
				$("#dialog_3_msg").show().html('<img src="/images/common/loading.gif" /> getting contacts...');
				$("#invite_friends").html('');
                        },
                        success: function(xml){

				$("#dialog_3_msg").hide().html('');
				$("#email_password").val('');
				$("#invite_friends").html(xml);

				$("#email_check_all").click(function() {   

			                if($("#email_check_all:checked").length == 1)
                			{
                        			$("input[name='email_list']").attr("checked", "checked");
			                } else
                			{
						$("input[name='email_list']").attr("checked", "");
                			}
				});

				var checkedEmails = new Array();

				$("#send_invitation").click(function() {
					$("input[name='email_list']:checked").each(function(i) {
						checkedEmails[i] =  this.value;
					});
					var checkedEmailsStr = checkedEmails.join("|")
			                $.ajax({
                        			type: "POST",
			                        url: "/signup/ajax/invite/",
                        			//dataType: "xml",
			                        data: {
                        			        checked_emails_str: checkedEmailsStr,
							invitation_message: $("#invitation_message").val(),
			                                request_type: "send_invitation"
                        			},
			                        beforeSend: function(){
							$("#dialog_1_msg").show().html('<img src="/images/common/loading.gif" /> sending...');
			                        },
			                        success: function(xml){
							$("#dialog_3_msg").hide().html('');
							$("#send_invitation_result").html(xml);
                        			}
                			});
					
				});
                        }
                });
	});

});
