﻿$(document).ready(function () {
    if (!cookieExists()) {
        $.blockUI({
            message: $("#email_frame"),
            overlayCSS: {
                opacity: 0.4,
                cursor: 'default'
            },
            css: {
                padding: 0,
                margin: 0,
                width: '400',
                top: '30%',
                left: '35%',
                textAlign: 'center',
                color: '#000',
                border: '0px',
                backgroundColor: '#fff',
                cursor: 'default'
            },
            centerY: false,
            centerX: false
        });
        $('.blockOverlay').attr('title', 'Click to unblock').click($.unblockUI);
    }

    $("#close-email").css("cursor", "pointer").click(function () {
        createCookie("has-seen-popup", true, 365);
        $.unblockUI();
    });

    $("#email-signup").css("cursor", "pointer").click(function () {
        if (validateForm()) { // validate that all fields are set
            var fname = $("#firstname").val();
            var lname = $("#lastname").val();
            var email = $("#email").val();
            var zip = $("#zip").val();

            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "ws/service.asmx/SaveEmail",
                data: "{'firstName':'" + fname + "','lastName':'" + lname + "','emailAddress':'" + email + "','zip':'" + zip + "'}",
                dataType: "json",
                success: emailsuccess,
                error: emailsuccess
            });
            createCookie("has-seen-popup", true, 365);
        }
    });

    FB.Event.subscribe('edge.create', function (response) {
        // do something with response.session
        window.location ="http://www.facebook.com/pages/Ross-Dress-for-Less/97935209700?ref=nf";
    });
});

function emailsuccess() {
    $.unblockUI();
}

function cookieExists() {
    return readCookie("has-seen-popup") != null;
}

function validateForm() {
    var fname = $("#firstname").val();
    var lname = $("#lastname").val();
    var email = $("#email").val();
    var zip = $("#zip").val();

    var isValid = fname.length > 0 && lname.length > 0 && email.length > 0 && zip.length > 0;
    if (!isValid) {
        $("#error").text("Please fill in each of the four fields to sign up.");
        $("#error").show();
    }
    else {
        $("#error").hide();
    }

    return isValid;
    //return $('form').valid();
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}
