

function MM_showHideLayers() { //v3.0
    var i, p, v, obj, args = MM_showHideLayers.arguments;
    for (i = 0; i < (args.length - 2); i += 3) if ((obj = MM_findObj(args[i])) != null) {
        v = args[i + 2];
        if (obj.style) { obj = obj.style; v = (v == 'show') ? 'visible' : (v = 'hide') ? 'hidden' : v; }
        obj.visibility = v;
    }
}

function MM_findObj(n, d) { //v4.0
    var p, i, x;
    if (!d)
        d = document;

    if (!n) return;

    if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }

    if (!(x = d[n]) && d.all) {
        x = d.all[n];
    }

    for (i = 0; !x && i < d.forms.length; i++) {
        x = d.forms[i][n];
    }


    for (i = 0; !x && d.layers && i < d.layers.length; i++) {
        x = MM_findObj(n, d.layers[i].document);

    }

    if (!x && document.getElementById) {
        x = document.getElementById(n);
    }
    return x;
}

function textCounter(field, cntfield, maxlimit) {
    if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
    // otherwise, update 'characters left' counter
    else
        cntfield.value = maxlimit - field.value.length;
}

function openNewWindow(URLtoOpen, windowName, height, width) {
    var w, h;
    var windowFeatures = "menubar=no,scrollbars=no,location=no,favorites=no,resizable=no,status=no,toolbar=no,directories=no";
    if (document.all || document.layers) {
        w = screen.availWidth;
        h = screen.availHeight;
    }

    var test = "'";
    var leftPos = (w - width) / 2, topPos = (h - height) / 2;

    //window.open('page,html','popup','width=' + popW + ',height='+popH+',top='+topPos+','left='+leftPos);
    var myWin = window.open(URLtoOpen, windowName, "width=" + width + ",height=" + height + ",left=" + leftPos + ",top=" + topPos + test + windowFeatures + test);

    return myWin;
}

function displayImage(picTitle, picURL, picWidth, picHeight) {
    var newWindow = window.open(picURL, 'newWin', 'toolbar=no,width=' + picWidth + ',height=' + picHeight);
    newWindow.document.write('<html><head><title>' + picTitle + '<\/title><\/head><body background="' + picURL + '"><\/body><\/html>');
    newWindow.resizeBy(picWidth - newWindow.document.body.clientWidth, picHeight - newWindow.document.body.clientHeight);
    newWindow.focus();
}

// -------------------------------------------------------------------
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
    if (obj != null && obj.options != null) { return true; }
    return false;
}



// -------------------------------------------------------------------
// sortSelect(select_object)
//   Pass this function a SELECT object and the options will be sorted
//   by their text (display) values
// -------------------------------------------------------------------
function sortSelect(obj) {
    var o = new Array();
    if (!hasOptions(obj)) { return; }
    for (var i = 0; i < obj.options.length; i++) {
        o[o.length] = new Option(obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected);
    }
    if (o.length == 0) { return; }
    o = o.sort(
		function(a, b) {
		    if ((a.text + "") < (b.text + "")) { return -1; }
		    if ((a.text + "") > (b.text + "")) { return 1; }
		    return 0;
		}
		);

    for (var i = 0; i < o.length; i++) {
        obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
    }
}
// ------------------------------
// Add url to favourites
// ------------------------------
function bookmarkSite(title, url) {
    if (document.all)
        window.external.AddFavorite(url, title);
    else if (window.sidebar)
        window.sidebar.addPanel(title, url, "")
}

// ------------------------------------------
//  addLoadEvent(func)
// Adds a new function to the load event of the page
// ---------------------------------------------
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

// Function retrieved from: http://cass- hacks.com/articles/code/js_url_encode_decode/   
function URLEncode(clearString) {
    var output = '';
    var x = 0;
    clearString = clearString.toString();
    var regex = /(^[a-zA-Z0-9_.]*)/;
    while (x < clearString.length) {
        var match = regex.exec(clearString.substr(x));
        if (match != null && match.length > 1 && match[1] != '') {
            output += match[1];
            x += match[1].length;
        } else {
            if (clearString[x] == ' ')
                output += '+';
            else {
                var charCode = clearString.charCodeAt(x);
                var hexVal = charCode.toString(16);
                output += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase();
            }
            x++;
        }
    }
    return output;
}
// Retrieved from: http://www.htmlcodetutorial.com/forms/index_famsupp_157.html   
function SubmitOnEnter(myfield, e) {
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    else
        return true;
    if (keycode == 13) {
        element = document.getElementById("sa");
        if (!element.onclick || (element.onclick() != false)) location = element.href;
        return false;
    }
    else
        return true;
}

// Registers a JavaScript namespace (e.g. input string Vividium.Development.Code)
function registerNS(ns) {
    var nsParts = ns.split(".");
    var root = window;

    for (var i = 0; i < nsParts.length; i++) {
        if (typeof root[nsParts[i]] == "undefined")
            root[nsParts[i]] = new Object();

        root = root[nsParts[i]];
    }
}
// Ask all validators if they are valid or not
function isPageValid(validationGroup) {
    var isValid = true;

    for (i = 0; i < Page_Validators.length; i++) {

        var val = Page_Validators[i];

        if ((typeof (val.enabled) == "undefined" || val.enabled != false) && IsValidationGroupMatch(val, validationGroup)) {
            if (typeof (val.evaluationfunction) == "function") {
                if (val.evaluationfunction(val) == false) {
                    isValid = false;
                }
            }
        }

    }

    return isValid;
}

function insertAtCaret(areaId, text) {
    var txtarea = document.getElementById(areaId);
    var scrollPos = txtarea.scrollTop;
    var strPos = 0;
    var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
		"ff" : (document.selection ? "ie" : false));
    if (br == "ie") {
        txtarea.focus();
        var range = document.selection.createRange();
        range.moveStart('character', -txtarea.value.length);
        strPos = range.text.length;
    }
    else if (br == "ff") strPos = txtarea.selectionStart;

    var front = (txtarea.value).substring(0, strPos);
    var back = (txtarea.value).substring(strPos, txtarea.value.length);
    txtarea.value = front + text + back;
    strPos = strPos + text.length;
    if (br == "ie") {
        txtarea.focus();
        var range = document.selection.createRange();
        range.moveStart('character', -txtarea.value.length);
        range.moveStart('character', strPos);
        range.moveEnd('character', 0);
        range.select();
    }
    else if (br == "ff") {
        txtarea.selectionStart = strPos;
        txtarea.selectionEnd = strPos;
        txtarea.focus();
    }
    txtarea.scrollTop = scrollPos;
}


  


