﻿function SetStartValue(i_fieldName, i_startVal)
{
    $("#" + i_fieldName).bind("focus", function() {
        if ($(this).attr("value") == i_startVal) {
            $(this).attr("value", "");
        }
    });
    $("#" + i_fieldName).bind("blur", function() {
        if ($(this).attr("value") == "") {
            $(this).attr("value", i_startVal);
        }
    });
}

function Search(i_def) {
    cQuery = document.getElementById("search-string");
    if (chkI(cQuery, i_def)) {
        window.location = '/content/search.aspx?query=' + encodeURI(cQuery.value);
    }
}

function SaveComment(i_postId, i_defTitle, i_defName, i_defEmail, i_defText, i_wrongText)
{
    //cTitle = document.getElementById("txtCommentTitle");
    cName = document.getElementById("txtCommentName");
    cEmail = document.getElementById("txtCommentEmail");
    //cWebsite = document.getElementById("txtCommentWebSite");
    cText = document.getElementById("txtCommentText");
    //website:encodeURI(cWebsite.value),
    // check if valid
    if (chkI(cName, i_defName) && chkM(cEmail, i_defEmail) && chkI(cText, i_defText)) // && chkI(cTitle, i_defTitle)
    {
        //title: encodeURI(cTitle.value), 
        $.post("/ajax/blogAddComment.aspx", { name: encodeURI(cName.value), email: encodeURI(cEmail.value), text: encodeURI(cText.value), postId: i_postId }, function(data) {
        // write over the comment form the newly made comment
            if ($(".comments ol li").length == 0)
                $(".comments ol").html = data;
            else
                $(".comments ol").append(data);
            
            $("#divAddComment").hide();
        });
    }
    else
    {
        alert(i_wrongText);
    }
    return false;
}

function NewsletterSignUp(i_success, i_wrongInput, i_defName, i_defEmail) {
    cName = document.getElementById("nlName");
    cEmail = document.getElementById("nlEmail");
    if (chkI(cName, i_defName) && chkM(cEmail, i_defEmail)) {
        $.post("/ajax/nsNewsletter.aspx", { f_name: encodeURI(cName.value), f_email: encodeURI(cEmail.value) }, function() {
            alert(i_success);
        });
    }
    else {
        alert(i_wrongInput);
    }
    return false;
}

// check the dropdowns
function chkI(i_input, i_defaultValue) {
    if (i_defaultValue != null && i_defaultValue != "" && i_defaultValue.length > 0)
        if (i_input.value == i_defaultValue) {
            $(i_input).addClass("error2");
            return false;
        }
    else if (i_input.value.length > 0)
    {
        $(i_input).removeClass("error2");
        return true;
    }
    else
    {
        $(i_input).addClass("error2");
        return false;
    }
}

// check the dropdowns
function chkM(i_input, i_defaultValue)
{
    if (chkI(i_input, i_defaultValue))
    {
        if (!i_input.value.match(/^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/i))
            $(i_input).addClass("error2");
        else
            return true;
    }
    return false;
}

function DoMail(i_afterDot, i_afterAt, i_beforeAt) {
    window.location = 'mailto:' + i_beforeAt + '@' + i_afterAt + "." + i_afterDot;
}

function ShowEmail(i_where, i_afterDot, i_afterAt, i_beforeAt, i_additional) {
    alert("eome");
    builtStr = i_beforeAt + "@" + i_afterAt + "." + i_afterDot;
    if (i_additional == null)
        i_additional = builtStr;
    $(i_where).append('<a href="javascript:DoMail(' + i_afterDot + ', ' + i_afterAt + ', ' + i_beforeAt + ')">' + i_additional + '</a>');
}
