﻿/// <reference path="jQuery/jquery-1.4.1.min-vsdoc.js" />

$().ready(function() {
    FormValidate();
    $(".btnEmptyForm").click(function() {
        $("#SName").val("");
        $("#EName").val("");
        $("#Email").val("");
        $("#Phone").val("");
        $("#Address").val("");
        $("#ZipCode").val("");
        $("#PostalAddress").val("");
        var validator = $("#NewsLetterForm").validate();
        validator.resetForm();
    });
    $("#btnSend").click(function() {
        ValidateForm();
        var isValid = $("#NewsLetterForm").valid();
        if (isValid == false) {
            $("#boxError").show();
            window.setTimeout(function() {
                $("#boxError").fadeOut(500);
            }, 1500);
        }
        return isValid;
    });
    $(".spanHelpTextPhone").click(function() {
        if ($(".FormHelpTextPlate").is(":visible")) {
            $(".FormHelpTextPlate").fadeOut();
        } else {
            var left = $(this).position().left;
            var top = $(this).position().top;
            var height = $(".FormHelpTextPlate").height() / 2;
            $(".FormHelpTextPlate").css("top", top - height);
            $(".FormHelpTextPlate").css("left", left + 15);
            $(".FormHelpTextPlate").fadeIn();
        }
    });
    $(".FormHelpTextPlateClose").click(function() {
        $(".FormHelpTextPlate").fadeOut();
    });
});

function NewsLetterSuccess(content) {
    if (content.get_data().toString().toLowerCase() == 'true') {
        $("#boxSuccess").fadeIn(200);
        $("#SName").val("");
        $("#EName").val("");
        $("#Email").val("");
        $("#Phone").val("");
        $("#Address").val("");
        $("#ZipCode").val("");
        $("#PostalAddress").val("");
        window.setTimeout(function() {
            $("#boxSuccess").fadeOut(500);
        }, 5500);

    }
    else {
        $("#boxError").fadeIn(400);
        window.setTimeout(function() {
            $("#boxError").fadeOut(500);
        }, 1500);
    }
    $(".FormProgressPlate").hide();
    $(".btnSend").removeAttr('disabled');
}

function NewsLetterValidate() {
    FormValidate();
    $(".FormProgressPlate").show();
    $(".btnSend").attr('disabled', 'disabled');
}

function FormValidate() {
    $("#NewsLetterForm").validate({
        rules: {
            SName: "required",
            EName: "required",
            Email:
                {
                    required: true,
                    email: true
                },
            Phone: { required: false, regex: '^(([+]\\d{2}[ ][1-9]\\d{0,2}[ ])|([0]\\d{1,3}[-]))((\\d{2}([ ]\\d{2}){2})|(\\d{3}([ ]\\d{3})*([ ]\\d{2})+)|(\\d{5,8})+)$' },
            Message: "required",
            ZipCode: "required",
            PostalAddress: "required",
            Address: "required"
        },
        messages: {
            EName: "",
            SName: "",
            Email: "",
            Phone: "",
            Address: "",
            ZipCode: "",
            PostalAddress: "",
            Address: ""
        }
    });
}
