﻿$(function() {

    $('.error').hide();
    $('.message').hide();
		
		$('#Group').hide();
		$('#Avatar').hide();
		$('#Password').hide();

$('#group').addClass('profile_tab_inactive');
$('#password').addClass('profile_tab_inactive');
$('#image').addClass('profile_tab_inactive');
	
	$("#details").click(function() {
		$('#details').removeClass('profile_tab_inactive');

		$('#password').removeClass('profile_tab');
		$('#password').addClass('profile_tab_inactive');

		$('#group').removeClass('profile_tab');
		$('#group').addClass('profile_tab_inactive');
		
		$('#image').removeClass('profile_tab');
  		$('#image').addClass('profile_tab_inactive');
		  		
		$('#details').addClass('profile_tab');


		$('#Details').show();
		$('#Group').hide();
		$('#Avatar').hide();
		$('#Password').hide();
	});
	
	$("#group").click(function() {
		$('#details').removeClass('profile_tab');
		$('#details').addClass('profile_tab_inactive')

	
		$('#password').removeClass('profile_tab');
		$('#password').addClass('profile_tab_inactive')

		
		$('#group').removeClass('profile_tab_inactive');
		$('#image').removeClass('profile_tab');
		$('#image').addClass('profile_tab_inactive')


  		$('#group').addClass('profile_tab');

		$('#Details').hide();
		$('#Group').show();
		$('#Avatar').hide();
		$('#Password').hide();
	});
	
	$("#image").click(function() {
		$('#details').removeClass('profile_tab');
		$('#details').addClass('profile_tab_inactive')

		$('#password').removeClass('profile_tab');
		$('#password').addClass('profile_tab_inactive')

		$('#group').removeClass('profile_tab');
		$('#group').addClass('profile_tab_inactive')
		
		$('#image').removeClass('profile_tab_inactive');
  		$('#image').addClass('profile_tab');

		$('#Details').hide();
		$('#Group').hide();
		$('#Avatar').show();
		$('#Password').hide();
	});
	
	$("#password").click(function() {
								  $('#details').removeClass('profile_tab');
		$('#details').addClass('profile_tab_inactive')

		$('#password').removeClass('profile_tab_inactive');

		$('#group').removeClass('profile_tab');
		$('#group').addClass('profile_tab_inactive')
		$('#image').removeClass('profile_tab_inactive');
				$('#image').addClass('profile_tab_inactive')

		
  		$('#password').addClass('profile_tab');

		$('#Details').hide();
		$('#Group').hide();
		$('#Avatar').hide();
		$('#Password').show();
	});
	
    $("input#SaveButton").click(function() {
        var error = false;
        var clientidprefix = "ctl00_LeftContent_";
        //Validation
        $('.error').hide();
        $('.message').hide();

        var email_address = $("input#" + clientidprefix + "EmailTextBox").val();
        //var password = $("input#LastNameTextBox").val();
        //var password_confirm = $("input#CompanyTextBox").val();
        var first_name = $("input#" + clientidprefix + "FirstNameTextBox").val();
        var last_name = $("input#" + clientidprefix + "LastNameTextBox").val();
        var company = $("input#" + clientidprefix + "CompanyTextBox").val();
        var address = $("input#" + clientidprefix + "AddressTextBox").val();
        var address2 = $("input#" + clientidprefix + "Address2TextBox").val();
        var suburb = $("input#" + clientidprefix + "SuburbTextBox").val();
        var state = $("select#" + clientidprefix + "StateDropDownList").val();
        if (state == "") {
            state = $("input#" + clientidprefix + "StateTextBox").val();
        }
        var postcode = $("input#" + clientidprefix + "PostcodeTextBox").val();
        var country = $("select#" + clientidprefix + "CountryDropdown").val();
        var newsletter = "false";
        if ($("input#" + clientidprefix + "OptInCheckBox").attr('checked')) {
            newsletter = "true";
        }
        var phone = $("input#" + clientidprefix + "PhoneTextBox").val();

        if (email_address == "") {
            $("#email_error").show();
            $("input#" + clientidprefix + "EmailTextBox").focus();
            error = true;
        }
        //alert(email_address);
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        if (!emailPattern.test(email_address)) {
            //$("#email_error_format").show();
            $("input#" + clientidprefix + "EmailTextBox").focus();
            $("input#" + clientidprefix + "EmailTextBox").addClass("errorBox");
            error = true;
        }else{
            $("input#" + clientidprefix + "EmailTextBox").removeClass("errorBox");
        }

        if (last_name == "") {
            //$("#last_name_error").show();
            $("input#" + clientidprefix + "LastNameTextBox").focus();
            $("input#" + clientidprefix + "LastNameTextBox").addClass("errorBox");
            error = true;
        }else{
            $("input#" + clientidprefix + "LastNameTextBox").removeClass("errorBox");
        }

        if (first_name == "") {
            //$("#first_name_error").show();
            $("input#" + clientidprefix + "FirstNameTextBox").focus();
            $("input#" + clientidprefix + "FirstNameTextBox").addClass("errorBox");
            error = true;
        }else{
            $("input#" + clientidprefix + "FirstNameTextBox").removeClass("errorBox");
        }

        if (error) {
            return false;
        }

        //Post Data
        var dataString = 'email_address=' + email_address +
            '&first_name=' + first_name +
            '&last_name=' + last_name +
            '&company=' + company +
            '&address=' + address +
            '&address2=' + address2 +
            '&suburb=' + suburb +
            '&state=' + state +
            '&postcode=' + postcode +
            '&country=' + country +
            '&newsletter=' + newsletter +
            '&phone=' + phone;

        //alert (dataString);return false;
        $.ajax({
            type: "POST",
            url: "ProcessProfile.aspx",
            data: dataString,
            success: function(response) {
                // Replace the div's content with the page method's return.
                if (response == "Success") {
                    $("#UpdateConfirmation").show();
                } else if (response == "Existing") {
                    $("#UpdateEmailError").show();
                } else {
                    $("#UpdateError").show();
                }
            }
        });

    });

    $("input#SaveGroup").click(function() {
        var error = false;
        var clientidprefix = "ctl00_LeftContent_";
        //Validation
        $('.error').hide();
        $('.message').hide();

        var group_code = $("input#" + clientidprefix + "GroupCodeTextBox").val();
        var email_address = $("input#" + clientidprefix + "EmailTextBox").val();

        if (group_code == "") {
            $("#GroupError").show();
            $("input#" + clientidprefix + "GroupCodeTextBox").focus();
            error = true;
        }

        if (error) {
            return false;
        }

        //Post Data
        var dataString = 'email_address=' + email_address +
            '&group_code=' + group_code;

        //alert (dataString);return false;
        $.ajax({
            type: "POST",
            url: "ProcessJoinGroup.aspx",
            data: dataString,
            success: function(response) {
                // Replace the div's content with the page method's return.
                if (response == "Success") {
                    $('#GroupConfirmation').show();
                    $('#GroupDetails').hide();
                } else {
                    $("#GroupError").show();
                }
            }
        });

    });

    $("input#SavePassword").click(function() {
        var error = false;
        var clientidprefix = "ctl00_LeftContent_";
        //Validation
        $('.error').hide();
        $('.message').hide();

        var email_address = $("input#" + clientidprefix + "EmailTextBox").val();
        var old_password = $("input#" + clientidprefix + "OldPassword").val();
        var new_password = $("input#" + clientidprefix + "NewPassword").val();
        var new_password_confirm = $("input#" + clientidprefix + "NewPasswordConfirm").val();

        if (new_password_confirm == "") {
            //$("#new_password_confirm_error").text("*");
            //$("#new_password_confirm_error").show();
            $("input#" + clientidprefix + "NewPasswordConfirm").focus();
            $("input#" + clientidprefix + "NewPasswordConfirm").addClass("errorBox");
            error = true;
        }else{
            $("input#" + clientidprefix + "NewPasswordConfirm").removeClass("errorBox");
        }

        if (new_password == "") {
            //$("#new_password_error").text("*");
            //$("#new_password_error").show();
            $("input#" + clientidprefix + "NewPassword").focus();
            $("input#" + clientidprefix + "NewPassword").addClass("errorBox");
            error = true;
        }else{
            $("input#" + clientidprefix + "NewPassword").removeClass("errorBox");
        }

        if (old_password == "") {
            //$("#old_password_error").text("*");
            //$("#old_password_error").show();
            $("input#" + clientidprefix + "OldPassword").focus();
            $("input#" + clientidprefix + "OldPassword").addClass("errorBox");
            error = true;
        }else{
            $("input#" + clientidprefix + "OldPassword").removeClass("errorBox");
        }

        if (new_password != new_password_confirm) {
            $("#new_password_error").text("New password and confirm password do not match");
            $("#new_password_confirm_error").text("New password and confirm password do not match");
            $("label#new_password_error").show();
            $("label#new_password_confirm_error").show();
            $("input#" + clientidprefix + "NewPassword").focus();
            error = true;
        }

        if (error) {
            return false;
        }

        //Post Data
        var dataString = 'email_address=' + email_address +
            '&old_password=' + old_password +
            '&new_password=' + new_password;

        //alert (dataString);return false;
        $.ajax({
            type: "POST",
            url: "ProcessPasswordChange.aspx",
            data: dataString,
            success: function(response) {
                // Replace the div's content with the page method's return.
                if (response == "Success") {
                    $('#ChangeConfirmation').show();
                } else {
                    $("label#old_password_error").text("Incorrect old password.");
                    $("label#old_password_error").show();
                    $("input#" + clientidprefix + "OldPassword").focus();
                }
            }
        });
    });
});
  