﻿$(function() {

    $('.error').hide();
	$('#form2').hide();
    $('#GroupSelection').hide();
    $('#GroupView').hide();

	 $("input#next").click(function() {
        var error = false;
        var clientidprefix = "ctl00_LeftContent_";
        //Validation
        $('.error').hide();
        var email_address = $("input#email_address").val();
        var password = $("input#password").val();
        var password_confirm = $("input#password_confirm").val();
        var first_name = $("input#first_name").val();
        var last_name = $("input#last_name").val();
        
        if (last_name == "") {
            //$("label#last_name_error").show();
            $("input#last_name").addClass("errorBox");
            $("input#last_name").focus();
            error = true;
        }else{
            $("input#last_name").removeClass("errorBox");
        }

        if (first_name == "") {
            //$("label#first_name_error").show();
            $("input#first_name").focus();
            $("input#first_name").addClass("errorBox");
            error = true;
        }else{
            $("input#first_name").removeClass("errorBox");
            //$("input#first_name").addClass("text");
        }

        if (password_confirm == "") {
            //$("label#password_confirm_error").show();
            $("input#password_confirm").focus();
            $("input#password_confirm").addClass("errorBox");
            error = true;
        }else{
            $("input#password_confirm").removeClass("errorBox");
        }

        if (password == "") {
            //$("label#password_error").show();
            $("input#password").focus();
            $("input#password").addClass("errorBox");
            error = true;
        }else{
            $("input#password").removeClass("errorBox");
            if (password != password_confirm) {
                $("label#password_match_error").show();
                $("input#password").focus();
                $("input#password").addClass("errorBox");
                error = true;
            }else{
                $("input#password").removeClass("errorBox");
            }
        }

        

        if (email_address == "") {
            //$("label#email_address_error").show();
            $("input#email_address").focus();
            $("input#email_address").addClass("errorBox");
            error = true;
        }else{
            $("input#email_address").removeClass("errorBox");
        }

        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        if (!emailPattern.test(email_address)) {
            $("label#email_address_error_format").show();
            $("input#email_address").focus();
            $("input#email_address").addClass("errorBox");
            error = true;
        }else{
            $("input#email_address").removeClass("errorBox");
        }

        if (error) {
			
            return false;
        }
		else{
			$('#form2').show();
			$("#form_holder").animate({marginLeft:"-500px"}, 1000);
			
		}
    });
	 
	 $("input#Previous").click(function() {
		$("#form_holder").animate({marginLeft:"0"}, 1000);								
	});




    $("input#Register").click(function() {
        var error = false;
        var clientidprefix = "ctl00_LeftContent_";
        //Validation
        $('.error').hide();
        var email_address = $("input#email_address").val();
        var password = $("input#password").val();
        var password_confirm = $("input#password_confirm").val();
        var first_name = $("input#first_name").val();
        var last_name = $("input#last_name").val();
        var company = $("input#company").val();
        var address = $("input#address").val();
        var address2 = $("input#address2").val();
        var suburb = $("input#suburb").val();
        var state = $("select#" + clientidprefix + "state").val();
        if (state == "") {
            state = $("input#" + clientidprefix + "StateTextBox").val();
        }
        //alert(state);
        var postcode = $("input#postcode").val();
			var country = $("select#" + clientidprefix + "country").val();
        var newsletter = "false";
        if ($("input#newsletter").attr('checked')) {
            newsletter = "true";
        }
		var terms = "false";
		if ($("input#terms").attr('checked')) {
            terms = "true";
        }

        var phone = $("input#phone").val();

        if (last_name == "") {
            $("label#last_name_error").show();
            $("input#last_name").focus();
            error = true;
        }

        if (first_name == "") {
            $("label#first_name_error").show();
            $("input#first_name").focus();
            error = true;
        }

        if (password_confirm == "") {
            $("label#password_confirm_error").show();
            $("input#password_confirm").focus();
            error = true;
        }

        if (password == "") {
            $("label#password_error").show();
            $("input#password").focus();
            error = true;
        }

        if (password != password_confirm) {
            $("label#password_match_error").show();
            $("input#password").focus();
            error = true;
        }

        if (email_address == "") {
            $("label#email_address_error").show();
            $("input#email_address").focus();
            error = true;
        }

        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        if (!emailPattern.test(email_address)) {
            $("label#email_address_error_format").show();
            $("input#email_address").focus();
            error = true;
        }
		if (terms == "false") {
            $("label#terms_error").show();
            $("input#terms").focus();
            error = true;
        }
		else(error='');
		
        if (error) {
            return false;
        }

        //Post Data
        var dataString = 'email_address=' + email_address +
            '&password=' + password +
            '&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: "ProcessRegistration.aspx",
            data: dataString,
            success: function(response) {
                // Replace the div's content with the page method's return.
                if (response == "Success") {
                    $('#GroupSelection').show();
					$("#form_holder").animate({marginLeft:"-1000px"}, 1000);

                } else if (response == "Existing") {
                    $("label#email_address_existing_error").show();
                    $("input#email_address").focus();
                } else {
                    $("label#registration_error").show();
                }
            }
        });

    });

    $("input#NewGroup").click(function() {
        //alert("New Group");        
        var email_address = $("input#email_address").val();
        var dataString = 'email_address=' + email_address;
        $.ajax({
            type: "POST",
            url: "ProcessNewGroup.aspx",
            data: dataString,
            success: function(response) {
                // Replace the div's content with the page method's return.
                if (response == "Success") {
                    //$('#GroupView').show();
                    //$("label#new_group_success").show();
                    //$("label#join_group_success").hide();
					//$("#form_holder").animate({marginLeft:"-1500px"}, 1500);
                    //$('#GroupSelection').hide();
                    document.location = "ViewTable.aspx";
                } else {
                    $("label#new_group_error").show();
                    $("label#join_group_error").hide();
                }
            }
        });
    });
    
    $("input#continue").click(function() {
        //alert("New Group");        
        document.location = "Default.aspx";
        /*var email_address = $("input#email_address").val();
        var dataString = 'email_address=' + email_address;
        $.ajax({
            type: "POST",
            url: "ProcessNewGroup.aspx",
            data: dataString,
            success: function(response) {
                // Replace the div's content with the page method's return.
                if (response == "Success") {
                    //$('#GroupView').show();
                    //$("label#new_group_success").show();
                    //$("label#join_group_success").hide();
					//$("#form_holder").animate({marginLeft:"-1500px"}, 1500);
                    //$('#GroupSelection').hide();
                    document.location = "Default.aspx";
                } else {
                    $("label#new_group_error").show();
                    $("label#join_group_error").hide();
                }
            }
        });*/
    });

    $("input#JoinGroup").click(function() {
        //alert("Join");
        var error = false;
        //Validation
        $('.error').hide();
        var clientidprefix = "ctl00_LeftContent_";
        var email_address = $("input#email_address").val();
        var group_code = $("input#" + clientidprefix + "GroupCode").val();

        if (group_code == "") {
            //$("label#join_group_error").show();
            $("input#" + clientidprefix + "GroupCode").focus();
            $("input#" + clientidprefix + "GroupCode").addClass("errorBox");
            error = true;
        }

        if (error) {
            return false;
        }

        var dataString = 'email_address=' + email_address +
            '&group_code=' + group_code;

        $.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") {
                    //$('#GroupView').show();
                    //$("label#join_group_success").show();
                    //$("label#new_group_success").hide();
                   // $('#GroupSelection').hide();
				   //$("#form_holder").animate({marginLeft:"-1500px"}, 1500);
				   document.location = "ViewTable.aspx";
                } else {
                    $("label#join_group_error").show();
                    //$("label#new_group_error").hide();
                }
            }
        });
    });

    $("input#ProceedButton").click(function() {
        document.location = "ViewTable.aspx";
    });
});
  