﻿var fb = fb || {};

$(function() {

	fb.init();
	

});

fb = {

	init: function () {
		fb.misc();
		fb.startAnim();
		fb.showFields();
		fb.validate();
		fb.isCampaignRunning = false;
	},
	misc: function () {

		// When pressing on the close button
		$("a.closeCurtain").click(function () {
			$(".campaignCurtain").stop().animate({
				height: '0px'
			}, {
				queue: false,
				duration: 1200,
				easing: 'easeOutBounce',
				complete: function () {
					$(this).hide();
				}
			});
		});

	},
	isCampaignRunning: false,
	showFields: function () {
		// $(".campaignCurtain #firstnameForCampaign, .campaignCurtain #lastnameForCampaign").fadeIn(1200, function () {

		//});



	}, // startAnim
	startAnim: function () {

		if ($(".campaignCurtain").hasClass("small")) {
			$(".campaignCurtain.small").delay(1200).animate({
				height: "300px"
			}, {
				queue: true,
				duration: 800,
				easing: 'easeInBack',
				complete: function () {
					$(".campaignCurtain.small").animate({
						height: "400px"
					}, {
						queue: true,
						duration: 800,
						easing: 'easeOutBounce',
						complete: function () {
							$(".campaignCurtain #firstnameForCampaign, .campaignCurtain #lastnameForCampaign").show();
						}
					});
				}
			});
		}
		else {
			$(".campaignCurtain.start.big").delay(1000).animate({
				height: "1100px"
			}, {
				queue: true,
				duration: 800,
				easing: 'easeOutExpo',
				complete: function () {
					$(".campaignCurtain.start.big").animate({
						height: "1020px"
					}, {
						queue: true,
						duration: 800,
						start_value: 0,
						easing: 'easeOutBounce',
						complete: function () {
							$(".campaignCurtain #firstnameForCampaign, .campaignCurtain #lastnameForCampaign").show();
						}
					});
				}
			});
		}
	}, // startAnim
	validate: function () {
		// Make the check-image visible when user clicks on the yellow checkbox.
		$("a.agreeForCampaign").click(function (event) {
			event.preventDefault();

			if ($(this).hasClass("selected")) {
				$(this).removeClass("selected");
				$("#agreeForCampaign").removeAttr("checked");
			} else {
				$("#agreeForCampaign").attr("checked", "checked");
				$(this).addClass("selected");
			}
		});
		// Make the check-image visible when user clicks on the condition-field.
		$(".campaignConditions").click(function (event) {
			event.preventDefault();

			if ($("a.agreeForCampaign").hasClass("selected")) {
				$("a.agreeForCampaign").removeClass("selected");
				$("#agreeForCampaign").removeAttr("checked");
			} else {
				$("#agreeForCampaign").attr("checked", "checked");
				$("a.agreeForCampaign").addClass("selected");
			}
		});



		// When submitting on the big campaign curtain
		$("#submitForCampaign, #submitForSmallCampaign").click(function (event) {

			event.preventDefault();

			if (fb.isCampaignRunning) {
				alert('Dina uppgifter skickas. Var god vänta...');
				return;
			}
			fb.isCampaignRunning = true;

			// Check every single inputfield. if the length of the field is 0, add the class error to the input field
			$(".campaignCurtain input[type='text']").each(function () {

				if ($(this).val().length == 0) { $(this).addClass("error"); }

				$(this).focusout(function () {
					if ($(this).val().length != 0) {
						$(this).removeClass("error");
					}
				});

			});

			if ($(".campaignCurtain input[type='text']").val().length == 0) {
				$(".campaignCurtain	").append("<div class='errorMess'></div>");
				$("div.errorMess").show();
			} else {
				$("div.errorMess").hide();
			}

			var agreeChecked = $(".agreeForCampaign").hasClass("selected");
			if (!agreeChecked) {
				$(".campaignCurtain	").append("<div class='errorMess'></div>");
				$("div.errorMess").show();
				alert("Du måste godkänna användarvillkoren.");
				fb.isCampaignRunning = false;
			} else {
				$("div.errorMess").hide();
			}

			var phone = $("input[type='text']#phoneForCampaign").val().replace("-", "").replace(" ", "");

			if (phone.length != 0) {
				if (phone.substr(0, 1) == '0' || phone.substr(0, 1) == '+') {

					var zip = $("#zipForCampaign").val().replace("-", "").replace(" ", "");

					if ($("input[type='text']#firstnameForCampaign").val().length != 0 && $("input[type='text']#lastnameForCampaign").val().length != 0 && $("input[type='text']#phoneForCampaign").val().length != 0 && $(".agreeForCampaign").hasClass("selected")) {
						$("div.errorMess").hide();
						$(".campaignCurtain .ajaxLoadingCampaignCurtain").show();

						fb.checkZip(zip);
					}
					else {
						fb.isCampaignRunning = false;
						$("div.errorMess").show();
					}
				}
				else {
					alert('Telefonnumret måste börja med en nolla (0) eller ett plustecken (+)');
					fb.isCampaignRunning = false;
				}
			}
			else {
				$("div.errorMess").show();
				fb.isCampaignRunning = false;
			}

		});

	}, // validate
	checkZip: function (value) {
		var zvalue = value.replace("-", "").replace(" ", "");
		var pattern = /^\d{5}$/;
		if (pattern.test(zvalue)) {
			//return true;
			$.ajax({
				type: 'POST',
				url: '/templates/services/internal/ajaxservice.svc/ValidateZip',
				data: JSON.stringify({ zipCode: zvalue
				}),
				async: true,
				datatype: 'json',
				contentType: "application/json; charset=utf-8",
				success: function (data) {
					if (data.d) {
						fb.sendForm(zvalue);
					} else {
						$(".campaignCurtain .ajaxLoadingCampaignCurtain").hide();
						fb.isCampaignRunning = false;
						alert("Fastighetsbyrån har ingen verksamhet i angivet område")
					}

				},
				error: function () {
					$(".campaignCurtain .ajaxLoadingCampaignCurtain").hide();
					alert("Fastighetsbyrån har ingen verksamhet i angivet område");
					fb.isCampaignRunning = false;
					return false;

				}
			});
			return true;

		} else {
			$(".campaignCurtain .ajaxLoadingCampaignCurtain").hide();
			$("#zipForCampaign").addClass("error");
			alert("Var god kontrollera postnumret.");
			fb.isCampaignRunning = false;
			return false;
		}
	},
	sendForm: function (value) {
		zip = value;
		$.ajax({
			type: 'POST',
			url: '/templates/services/internal/ajaxservice.svc/SendFriVarderingForm',
			data: JSON.stringify({
				firstName: $('#firstnameForCampaign').val(),
				lastName: $('#lastnameForCampaign').val(),
				address: '',
				zipCode: zip,
				phoneNumber: $('#phoneForCampaign').val().replace("-", "").replace(" ", ""),
				email: '',
				source: $('#sourceCampaign').val(),
				sellNow: false,
				ip: '255.255.255.255'
			}),
			datatype: 'json',
			contentType: "application/json; charset=utf-8",
			success: function () {
				if ($('.campaignCurtain').hasClass('small')) {
					fb.smallCurtainSuccess();
				}
				else {
					fb.bigCurtainSuccess();
				}
				$.cookie("ProvSalj", "FormSent", { expires: 365, path: '/' });
				fb.isCampaignRunning = false;
			},
			error: function () {
				if ($('.campaignCurtain').hasClass('small')) {
					fb.smallCurtainFail();
				}
				else {
					fb.bigCurtainFail();
				}
				fb.isCampaignRunning = false;
			}
		});
	},
	bigCurtainSuccess: function () {
		//Run animation if submitting failed
		$(".campaignCurtain").stop().animate({
			height: '0px'
		}, {
			queue: false,
			duration: 1200,
			easing: 'easeOutBounce',
			complete: function () {
				$(".campaignCurtain").hide();
				$(".campaignCurtain.success").show();
			}
		});
		// 2. Bring the "thank you" curtain in front
		$(".campaignCurtain.success").delay(1000).animate({
			height: "1100px"
		}, {
			queue: true,
			duration: 800,
			easing: 'easeOutExpo',
			complete: function () {
				$(".campaignCurtain.success").animate({
					height: "1020px"
				}, {
					queue: true,
					duration: 800,
					start_value: 0,
					easing: 'easeOutBounce'
				});
				$(".campaignCurtain.success").delay(5000).animate({
					height: "0px"
				}, {
					queue: true,
					duration: 800,
					start_value: 0,
					easing: 'easeOutBounce'
				});
			}
		});
	},
	smallCurtainSuccess: function () {

		$(".campaignCurtain.small").stop().animate({
			height: '0px'
		}, {
			queue: false,
			duration: 1200,
			easing: 'easeOutBounce',
			complete: function () {
				// When the animation is done, hide the curtain
				$(".campaignCurtain.small").hide();
				$(".campaignCurtain.small.success").show();
			}
		});
		// 2. Bring the "thank you" curtain in front
		$(".campaignCurtain.small.success").delay(1000).animate({
			height: "300px"
		}, {
			queue: true,
			duration: 800,
			easing: 'easeOutExpo',
			complete: function () {
				$(".campaignCurtain.small.success").animate({
					height: "400px"
				}, {
					queue: true,
					duration: 800,
					start_value: 0,
					easing: 'easeOutBounce'
				});
				$(".campaignCurtain.small.success").delay(5000).animate({
					height: "0px"
				}, {
					queue: true,
					duration: 800,
					start_value: 0,
					easing: 'easeOutBounce',
					complete: function () {
						$(this).hide();
					}
				});
			}
		});
	},
	bigCurtainFail: function () {
		//Run animation if submitting failed
		$(".campaignCurtain").stop().animate({
			height: '0px'
		}, {
			queue: false,
			duration: 1200,
			easing: 'easeOutBounce',
			complete: function () {
				$(".campaignCurtain").hide();
				$(".campaignCurtain.fail").show();
			}
		});
		// 2. Bring the "thank you" curtain in front
		$(".campaignCurtain.fail").delay(1000).animate({
			height: "1100px"
		}, {
			queue: true,
			duration: 800,
			easing: 'easeOutExpo',
			complete: function () {
				$(".campaignCurtain.fail").animate({
					height: "1020px"
				}, {
					queue: true,
					duration: 800,
					start_value: 0,
					easing: 'easeOutBounce'
				});
				$(".campaignCurtain.fail").delay(5000).animate({
					height: "0px"
				}, {
					queue: true,
					duration: 800,
					start_value: 0,
					easing: 'easeOutBounce'
				});
			}
		});
	},
	smallCurtainFail: function () {
		$(".campaignCurtain.small").stop().animate({
			height: '0px'
		}, {
			queue: false,
			duration: 1200,
			easing: 'easeOutBounce',
			complete: function () {
				// When the animation is done, hide the curtain
				$(".campaignCurtain.small").hide();
				$(".campaignCurtain.small.fail").show();
			}
		});
		// 2. Bring the "thank you" curtain in front
		$(".campaignCurtain.small.fail").delay(1000).animate({
			height: "300px"
		}, {
			queue: true,
			duration: 800,
			easing: 'easeOutExpo',
			complete: function () {
				$(".campaignCurtain.small.fail").animate({
					height: "400px"
				}, {
					queue: true,
					duration: 800,
					start_value: 0,
					easing: 'easeOutBounce'
				});
				$(".campaignCurtain.small.fail").delay(5000).animate({
					height: "0px"
				}, {
					queue: true,
					duration: 800,
					start_value: 0,
					easing: 'easeOutBounce',
					complete: function () {
						$(this).hide();
					}
				});
			}
		});
	}
}
