/// <reference path="jquery-1.5.1.js" />
// Will set the focus to an  input so the first element of an item is selected.
$(document).ready(function () {
    $("#focusMe").focus();
});


/*
 * Will check is an email is valid.
*/
function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}
/*
 * Will trim a value
*/
function trim(value) {
    value = value.replace(/^\s+/, '');
    value = value.replace(/\s+$/, '');
    return value;
}

/*
 * Will Replace an element by an loader. And back.
*/
function ReplaceWithLoader(elementToBeHidden) {
    var loadImage = $("#loader");
    if (loadImage.length > 0) {
        loadImage.remove();

        if (!$(elementToBeHidden).lenght > 0)
            $(elementToBeHidden).fadeIn();
    } else {
        $(elementToBeHidden).before('<div id="loader"><img src="/Content/Image/loading.gif"/></div>');
        
        if (!$(elementToBeHidden).lenght > 0)
            $(elementToBeHidden).fadeOut();
    }
}

function FormatCurrency(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0, 00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += ',00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}

/*
* Will search for "http://, https://, mailto://" in the text and make links of them.
*/
function Linkify(inputText) {
    //URLs starting with http://, https://, or ftp://
    var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    var replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

    //URLs starting with www. (without // before it, or it'd re-link the ones done above)
    var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

    //Change email addresses to mailto:: links
    var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
    var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

    return replacedText
}


/*
* Mobile contact form Validation & Send
*/
function send() {
    if ($("#Name").val() == "") {
        alert("U bent vergeten een naam in te vullen!");
        return false;
    }
    if ($("#Email").val() == "" || !isValidEmailAddress($("#Email").val())) {
        alert("Geen geldige email ingevuld");
        return false;
    }
    if ($("#Text").val() == "") {
        alert("U heeft geen tekst in gevuld om te versturen!");
        return false;
    }

    $("#ContactForm").submit();
    return true;
}


