﻿function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

function init() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];

        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {
            /* Add event handlers */
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);

            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;

    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;

    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}

function hideControl(controlId) {
    document.getElementById(controlId).style.display = "none";
}

function showControl(controlId) {
    document.getElementById(controlId).style.display = "";
}


String.prototype.beginsWith = function (t, i) {
    if (i == false) {
        return (t == this.substring(0, t.length));
    } else {
        return (t.toLowerCase() == this.substring(0, t.length).toLowerCase());
    }
}

String.prototype.endsWith = function (t, i) {
    if (i == false) {
        return (t == this.substring(this.length - t.length));
    } else {
        return
        (t.toLowerCase() == this.substring(this.length - t.length).toLowerCase());
    }
}

var util = {
    formatCurrency: function (num) {
        num = isNaN(num) || num === '' || num === null ? 0.00 : num;
        return parseFloat(num).toFixed(2);
    }
}

Array.prototype.exists = function (o) {
    for (var i = 0; i < this.length; i++)
        if (this[i] === o)
            return true;
    return false;
}

function debug() {

    if (!window.location.host.toLowerCase().beginsWith('localhost') || window.location.host.toLowerCase().beginsWith('staging')) {
        return;
    }

    try {
        for (i = 0; i < arguments.length; i++) {
            if (typeof arguments[i] == 'object') {
                if (console != null)
                    console.dir(arguments[i]);

            }
            else {
                if (console != null)
                    console.log(arguments[i]);
            }
        }
    }
    catch (e) { }
}

$.urlParam = function (name) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) {
        return 0;
    }
    return results[1] || 0;
}


function showError(ctl, errorText) {
    errorTag = $("<span class='errorMessage validateerror'>" + errorText + "</span>");
    $(ctl).after(errorTag);
}


