$(document).ready(function () {
	
	setForwardButton(false);
	setBackButton(true, 'images/back.gif', function () { window.location = 'http://www.firstbasesolutions.com/fbswms.php'; });
	$('#phone1').autotab({ target: 'phone2', format: 'numeric' });
	$('#phone2').autotab({ target: 'phone3', format: 'numeric', previous: 'phone1' });
	$('#phone3').autotab({ previous: 'phone2', format: 'numeric' });

});


function setForwardButton(bEnabled, image, onclick) {
	
	if (bEnabled) {
		$('#forwardImg').attr('src',image);
		$('#forward').show();
		document.getElementById('forward').onclick = onclick;
	} else {
		$('#forward').hide();
		document.getElementById('forward').onclick = null;
	}
	
}


function setBackButton(bEnabled, image, onclick) {

	if (bEnabled) {
		$('#backImg').attr('src',image);
		$('#back').show();
		document.getElementById('back').onclick = onclick;
	} else {
		$('#back').hide();
		document.getElementById('back').onclick = null;
	}
	
}


function showSignUp() {
	$("#errorMessage").html('');
	$("#login").hide();
	$("#signup").show();
	setForwardButton(false);
	setBackButton(true, 'images/back.gif', function () { window.location = 'http://www.firstbasesolutions.com/fbswms.php'; });
}


function showLogin() {
	$("#errorMessage").html('');
	$("#login_signup").show();
	$("#signup").hide();
	$("#login").show();
	setForwardButton(false);
	setBackButton(true, 'images/back.gif', function () { window.location = 'http://www.firstbasesolutions.com/fbswms.php'; });
}

function showTerms() {
	$("#errorMessage").html('');
	$("#termsAndConditions").show();
	setForwardButton(false);
	setBackButton(true, 'images/back.gif', function () { window.location = 'http://www.firstbasesolutions.com/fbswms.php'; });
}



function checkoutLogin() {

	// get the value of the checked radio button
	var username = $("#loginUsername").val();
	var password = $("#loginPassword").val();

	$("#errorMessage").html('');

	if (username == undefined || password == undefined) {

		$('#errorMessage').append($("<div></div>").text('Please enter both your Username and Password to login.'));
		return;

	}

	$.ajax({
		type: "POST",
		url: "ajax.php",
		data: "a=clogin&username=" + username + "&password=" + password,
		dataType: "json",
		success: function (json) {
			if (json.success == true) {
				$("#login_signup").hide();
				showTerms();
			} else {
				$('#errorMessage').append($("<div></div>").text(json.feedback));
			}
		}
	});

}


function signUpUser() {

	var formValues = $("#signup_form").serialize();
	$("#errorMessage").html('');
	$("#signup_form :input").removeClass('formFieldError');

	$.ajax({
		type: "POST",
		url: "ajax.php",
		data: "a=signup&" + formValues,
		dataType: "json",
		success: function(json) {

			if (json.success) {

				$("#login_signup").hide();
				showTerms();

			} else {
				if (json.errors) {
					$.each(json.errors, function(key, value) {
						$('#errorMessage').append($("<div></div>").text(value));
						$('#' + key).addClass('formFieldError');
					});
				}

			}
		}
	});

}


function loadProvinceList(countrySelect) {
	
	$("#provinceId").empty();
	$.ajax({
		type: "POST",
		url: "ajax.php",
		data: "a=lp&country_id=" + $("#" + countrySelect).val(),
		dataType: "json",
		success: function(json) {
			if (json.data) {
			   
				$.each(json.data, function(key, value) {  
					$('#provinceId').append($("<option></option>").attr("value",value.id).text(value.name)); 
				});
					
			} 
		}
	});	
	
}


function processTrial() {

	var terms = '';
	if ($("#termsAgreement:checked").length == 1) {
		terms = "&termsAgreement=1";
	}
	
	$.ajax({
		type: "POST",
		url: "ajax.php",
		data: "a=trial" + terms,
		dataType: "json",
		success: function(json) {

			if (json.success) {

				$("#termsAndConditions").hide();
				$("#trialRequested").show();

			} else {

				alert(json.feedback);
			}
		}
	});

}
