﻿function share(socialMedia) {
    var urlTemplate;
    _gaq.push('_trackEvent', 'sharebuttons', socialMedia);
   
    if (socialMedia == 'Facebook') {
        urlTemplate = UrlShorteningOptions.FacebookUrl;

        postToSocialNetwork(urlTemplate, window.location.href);
    } else {
        if (socialMedia == 'Twitter') {
            urlTemplate = UrlShorteningOptions.TwitterUrl;
        } else {
            urlTemplate = UrlShorteningOptions.HyvesUrl;
        }

        var url = window.location.href;
        if (window.location.protocol == 'https:') {    
            url = url.replace('https:', 'http:');
            UrlShorteningOptions.Service = UrlShorteningOptions.Service.replace('http:', 'https:');
        }
       
        $.ajax({
            type: "POST",
            data: '{"url": "' + url + '"}',
            dataType: "json",
            url: UrlShorteningOptions.Service,
            contentType: "application/json; charset=utf-8",
            success: function(result) {
                if (result.ErrorCode != undefined && result.ErrorCode != 0) {
                    alert("An error occurred while shortening the current URL");
                } else {
                    postToSocialNetwork(urlTemplate, result);
                }
            },
            error: function(req, err, ex) {
                alert(err);
            }
        });
    }
}

function postToSocialNetwork(urlTemplate, shortUrl) {
    var url = urlTemplate.replace("{url}", shortUrl).replace("{title}", UrlShorteningOptions.Title);
    window.open(url);
}

function setVisibility(id, visibility) {
    document.getElementById(id).style.display = visibility;
}

function showBlockEl(id) {
    if (document.getElementById(id) != null)
        document.getElementById(id).style.display = 'block';
}

function hideEl(id) {
    if (document.getElementById(id) != null)
        document.getElementById(id).style.display = 'none';
}

function trim(str) {
    return str.replace(new RegExp("^[\\s]+", "g"), "");
}

function clearValue(id) {
    var item = document.getElementById(id);
    item.value = '';
}
function RemoveStyle(id) {
    var item = document.getElementById(id);
    item.style.color = '';
}

function checkEmail(elementId) {
    var email = document.getElementById(elementId);
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    return filter.test(email.value);
}
