﻿function OnTextChange(field, maxLength, counter, text) {
    var counterSpan = document.getElementById(counter);
    var textBox = document.getElementById(field);
    var length = Trim(textBox.value).length;
    var charsRemaining = maxLength - length;
    if (charsRemaining < 0) {
        textBox.value = textBox.value.substring(0, maxLength);

        charsRemaining = 0;
    }
    counterSpan.value = charsRemaining + ' ' + text;
}

function LTrim(value) {
    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}

function RTrim(value) {
    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");
}

function Trim(value) {
    return LTrim(RTrim(value));
}

function OpenNewBrowserWindow(title, contentControlID) {
    var content = document.getElementById(contentControlID).value;
    OpenWindow = window.open("", "newwin", "height=250, width=250,toolbar=no,scrollbars=yes,menubar=yes, resizable=yes");
    //TODO: Cleanup following commented lines. The issue was, the "content" passed to this function was already having the 
    //html page level markup ex, head, body etc.-------------
    //OpenWindow.document.write("<head>")
    //OpenWindow.document.write("<title>" + title + "</title>")
    //OpenWindow.document.write("<link rel='stylesheet' type='text/css' href='~/CSS/PrintInvoice.css' media='print' />")
    //OpenWindow.document.write("</head>")
    //OpenWindow.document.write("<body>")
    //--------------------------------------------------------
    OpenWindow.document.write(content)
    //OpenWindow.document.write("</body>")
    //OpenWindow.document.write("</html>")
    OpenWindow.document.close()
    self.name = "main"
}

function browserDetection() {

    if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
        return "FireFox";
    }    
    else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
    {
        return "IE";
    }
    else
        return "N/A";
}
