﻿ $(function () {

    $(document).ajaxStart($.blockUI).ajaxStop($.unblockUI);
 
});

function NewAccount() {

	$("#RegistrationForm input").val("");
	$("#RegistrationForm select").val("");
	$("#RegistrationForm .RegFormMessage").html("");
	$("#RegistrationForm tbody[id='form']").css("display", "block");
	$("#RegistrationForm tbody[id='message']").css("display", "none");
	$("#RegistrationForm").dialog({
		width: 400,
		height: 500,
		resizable: false,
		autoOpen: false,
		modal: true,
		closeOnEscape: true,
		buttons: [{ text: "Register Account", click: function () {

			var Error = false;

			// Check all fields are entered that we need

			if ($("#txtEmail").val().length == 0) {
				$('#txtEmail').attr('class', 'RegFormError');
				Error = true;
			}
			if ($("#txtConfirmEmail").val().length == 0) {
				$('#txtConfirmEmail').attr('class', 'RegFormError');
				Error = true;
			}
			if ($("#txtFirstname").val().length == 0) {
				$('#txtFirstname').attr('class', 'RegFormError');
				Error = true;
			}
			if ($("#txtSurname").val().length == 0) {
				$('#txtSurname').attr('class', 'RegFormError');
				Error = true;
			}
			if ($("#txtCompany").val().length == 0) {
				$('#txtCompany').attr('class', 'RegFormError');
				Error = true;
			}
			if ($("#txtAdd1").val().length == 0) {
				$('#txtAdd1').attr('class', 'RegFormError');
				Error = true;
			}
			if ($("#txtTown").val().length == 0) {
				$('#txtTown').attr('class', 'RegFormError');
				Error = true;
			}
			if ($("#txtPostCode").val().length == 0) {
				$('#txtPostCode').attr('class', 'RegFormError');
				Error = true;
			}
			if ($("#txtTel").val().length == 0) {
				$('#txtTel').attr('class', 'RegFormError');
				Error = true;
			}

			if (!validateEmail($("#txtEmail").val())) {
				$('#spnEmailError').attr('Style', 'color: Red; visibility: visible;');
				$('#spnEmailError').html('Enter a valid email address');
				Error = true;
			}

			if ($("#txtEmail").val() != $("#txtConfirmEmail").val()) {
				$('#spnEmailError').attr('Style', 'color: Red; visibility: visible;');
				$('#spnEmailError').html('Your email addresses do not match');
				Error = true;
			}

			if (Error) return;

			var siteAccount = {
				Forename: $("#txtFirstname").val(),
				Surname: $("#txtSurname").val(),
				Email: $("#txtEmail").val(),
				CompanyName: $("#txtCompany").val(),
				TradingName: $("#txtTradingAs").val(),
				Telephone: $("#txtTel").val(),
				Fax: $("#txtFax").val(),
				Address1: $("#txtAdd1").val(),
				Address2: $("#txtAdd2").val(),
				Address3: $("#txtAdd3").val(),
				Town: $("#txtTown").val(),
				County: $("#txtCounty").val(),
				Country: $("#drpCountry").val(),
				Postcode: $("#txtPostCode").val()
			}

			$.ajax({
				type: "POST"
				, url: "/TaxShieldService.svc/RegisterAccount"
				, contentType: "application/json; charset=utf-8"
				, dataType: "json"
				, data: JSON.stringify({ siteAccount: siteAccount })
				, success: function (data) {

					$("#RegistrationForm").dialog("close").dialog("destroy");
					$("#RegistrationForm .RegFormMessage").html("<center>Success</center><p>" + data.d + "</p>");
					$("#RegistrationForm tbody[id='form']").css("display", "none");
					$("#RegistrationForm tbody[id='message']").css("display", "block");

					$("#RegistrationForm").dialog({
						width: 500,
						height: 200,
						resizable: false,
						autoOpen: false,
						modal: true,
						closeOnEscape: true,
						burrons: [{ text: "Close", click: function () {
							$("#RegistrationForm").dialog("close");
							$("#RegistrationForm").dialog("destroy");
						}
						}]
					}).dialog("open");
				}
				, error: function (data) {

					$("#RegistrationForm").dialog("close").dialog("destroy");

					$("#RegistrationForm .RegFormMessage").html("<center>Error</center><p>" + data.responseText + "</p>");
					$("#RegistrationForm tbody[id='form']").css("display", "none");
					$("#RegistrationForm tbody[id='message']").css("display", "block");

					$("#RegistrationForm").dialog({
						width: 500,
						height: 200,
						resizable: false,
						autoOpen: false,
						modal: true,
						closeOnEscape: true,
						burrons: [{ text: "Close", click: function () {
							$("#RegistrationForm").dialog("close");
							$("#RegistrationForm").dialog("destroy");
						}
						}]
					}).dialog("open");
				}
			});

		}
		}]
	});
	$("#RegistrationForm").dialog('open');
}

function validateEmail(elementValue) {
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

	return emailPattern.test(elementValue);
}

// /Date(-2208988800000+0000)/ Represents 01/01/1900 00:00:00, Minimum Date
function ConvertFromWCFDateTime(value) {
	if (IsNullOrEmpty(value)) return "";
	if (value.toString().indexOf("/Date(") == -1) return "";
	if (value == "/Date(-2208988800000+0000)/") return "";

	var string = new String(value.toString());

	string = string.replace(/\//g, "");

	string = string.replace(/\+\d{0,4}/g, "+0000");

	return eval("new " + string);
}

function ConvertToWCFDateTime(value) {
	if (IsNullOrEmpty(value)) return "/Date(-2208988800000+0000)/";

	//return "/Date(" + value.getTime().toString() + "+" + value.getTimezoneOffset().toString() + ")/";
	return "/Date(" + value.getTime().toString() + "+0000)/";
}

