﻿
jQuery.noConflict();
var $j = jQuery;

function IsEmail(text) {
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    return filter.test(text);
}

function IsCell(text) {
    var filter = /^\s*\d+\s*$/;
    return (filter.test(text) && text.indexOf("07") == 0 && text.length == 10);
}

function ClearValidation() {
    var i = 1;
    while ($("val" + i)) {
        $("val" + i).style.display = "none";
        i++;
    }
}

function GetBirthDate() {
    var d = $("year").options[$("year").selectedIndex].value + "-" +
            $("month").options[$("month").selectedIndex].value + "-" +
            $("day").options[$("day").selectedIndex].value;
    return d;
}

function IsValidDate(d) {
    return d.match(/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/);
}

function GetResult(res) {
    ClearValidation();
    if (res == "") {
        $("firstNames").value = "";
        $("firstNamesPrint").innerHTML = "";
        $("lastName").value = "";
        $("email").value = "";
        $("cellPhone").value = "";
        $("year").selectedIndex = 0;
        $("month").selectedIndex = 0;
        $("day").selectedIndex = 0;
        $j(function () { $j("#success").dialog(); });
    } else {
        $("val" + res).style.display = "list-item";
        $j(function () { $j("#validation").dialog(); });
    }
}

function SaveSMSReceiver() {
    var firstNames = $("firstNames").value;
    var lastName = $("lastName").value;
    var email = $("email").value;
    var cellPhone = $("cellPhone").value;
    var birthDate = GetBirthDate();
    var postForm = true;

    ClearValidation();

    if (firstNames.length == 0) {
        $("val9").style.display = "list-item";
        $j(function () { $j("#validation").dialog(); });
        postForm = false;
    }
    if (lastName.length == 0) {
        $("val10").style.display = "list-item";
        $j(function () { $j("#validation").dialog(); });
        postForm = false;
    }
    if (!IsEmail(email)) {
        $("val2").style.display = "list-item";
        $j(function () { $j("#validation").dialog(); });
        postForm = false;
    }
    if (!IsCell(cellPhone)) {
        $("val3").style.display = "list-item";
        $j(function () { $j("#validation").dialog(); });
        postForm = false;
    }
    if (!IsValidDate(birthDate)) {
        $("val7").style.display = "list-item";
        $j(function () { $j("#validation").dialog(); });
        postForm = false;
    }
    if (!$("accept").checked) {
        $("val6").style.display = "list-item";
        $j(function () { $j("#validation").dialog(); });
        postForm = false;
    }

    if (postForm) {
        new Ajax.Request("/inc/Events.aspx?func=Events/SaveSMSReceiver", {
            method: "post",
            parameters: {
                firstNames: firstNames,
                lastName: lastName,
                email: email,
                cellPhone: cellPhone,
                birthDate: birthDate
            },
            onSuccess: function (transport) { GetResult(transport.responseText); },
            onFailure: function () { alert("Det blev nåt fel."); },
            asynchronous: false
        });
    }
}

function ShowFirstNameList() {
    $j(function () { $j("#firstNameList").dialog({ position: ['center', 375] }); });
}

function AddFirstName(name) {
    if ($("firstNames").value.indexOf(" " + name) < 0) {
        $("firstNames").value += " " + name;
        $("firstNamesPrint").innerHTML += "<span class=\"name\" onclick=\"javascript:RemoveFirstName(this);\" title=\"Ta bort " + name + "\">" + name + "</span> ";
        //$j(function () { $j("#firstNameList").dialog("close"); });
        //alert($("firstNames").value);
    } else
        alert("Du har redan lagt till namnet " + name + ".");
}

function RemoveFirstName(el) {
    var name = el.innerHTML;
    $("firstNames").value = $("firstNames").value.replace(" " + name, "");
    $("firstNamesPrint").removeChild(el);
    //alert($("firstNames").value);
}
