/* ====================================================
//
// Client side script utilities.
//
=====================================================*/
// Page version: v1.27, released 13 January 2004

function cloneObject() {

    var oNew = new Object();
    
    // iterate through the objects properties
    for (var i in this) {
        // don't process clone() method, which is part of every Object
        if (!(typeof(this[i]) == "function" && i == "clone")) {
        
            // if the property is an Object, clone it
            if (typeof(this[i]) == "object") {
                                oNew[i] = this[i].clone();
                        } else {
                                oNew[i] = this[i].valueOf();
                        }
        }
    }
    
    // return the new Object
    return oNew;
}

function goToNextElement(element) {
        var elements = element.form.elements;
        var i;
        var nextElement;
        
        //get element index
        for (i = 0; i < elements.length; i++) { 
                if (element == elements[i]) {
                        break;
                }
        }

        //get next available element
        for (var j = 0; j < elements.length; j++) {
                i++;
                if (i == elements.length) {
                        i = 0;
                }
                
                nextElement = elements[i];
                if (nextElement.type != "hidden" && nextElement.style.display != "none" && !nextElement.disabled) {
                        try {
                                nextElement.focus();
                                if (nextElement.type == "text") {
                                        nextElement.select();
                                }
                                break;
                        } catch(e) {}
                }
        }
}

function setFocus(element) {
        if (!element.disabled) {
                try {
                        element.focus();
                        if (element.type == "text") {
                                element.select();
                        }
                } catch (e) {}
        }
}

function getRadioGroupValue(radio) {
        if (typeof(radio.length) == "undefined") {
                if (radio.checked)
                        return radio.value;
        }else{
                for (var i = 0; i < radio.length; i++) {
                        if (radio[i].checked)
                                return radio[i].value;
                }
        }
}

function isRadioGroupSelected(radio) {
        if (typeof(radio.length) == "undefined") {
                if (radio.checked)
                        return true;
        }else{
                for (var i = 0; i < radio.length; i++) {
                        if (radio[i].checked)
                                return true;
                }
        }
        return false;
}

function addSeparator(str, strSeparator) {
        if (str.length > 0)
                str += strSeparator;

        return str;
}

function formatHTML(str) {
        if (str == null)
                return str;
                
        str = new String(str);
                
        str = str.replace(/"/g, "&quot;");
        //str = str.replace(/ /g, "&nbsp;");
        str = str.replace(/</g, "&lt;");
        str = str.replace(/>/g, "&gt;");
        str = str.replace(/\r\n/g, "<br>");
        
        return str;
}

function formatJS(str) {
        if (str == null)
                return str;
                
        str = new String(str);
                
        str = str.replace(/\\/g, "\\\\");
        str = str.replace(/"/g, "\\\"");
        str = str.replace(/'/g, "\\\'");
        str = str.replace(/\r/g, "\\r");
        str = str.replace(/\n/g, "\\n");
        
        return str;
}

function formatSQL(str) {
        if (str == null)
                return str;
                
        str = new String(str);
                
        str = str.replace(/'/g,"\''");
        
        return str;
}

function openWindow(page, pagename, width, height, windowfeatures) {
        if (windowfeatures != "") {
                windowfeatures = "," + windowfeatures;
        }
        
        var left = screen.availWidth/2 - width/2;       
        var top = screen.availHeight/2 - height/2;
        
        pagename = pagename.toString().replace(/[\s-&.]/g, "");

        var w  = window.open(page, pagename,"LEFT=" + left + ",TOP=" + top + ",HEIGHT=" + height + ",WIDTH=" + width + windowfeatures);
        var browser = browserSniff();
        
        if (browser != "NS" && browser != "OPR") {
                eval("try {w.focus();} catch(e) {}");
        }
        
/*      return w;*/
}

function browserSniff() {
        if (document.layers) {
                return "NS";
        }
        if (document.all) {
                var agt = navigator.userAgent.toLowerCase();
                var is_opera = (agt.indexOf("opera") != -1);
                
                if(is_opera) {
                        return "OPR";
                } else {
                        return "IE";
                }
        }
        if (document.getElementById) {
                return "MOZ";
        }
        return "OTHER";
}

function centreWindow(w, width, height) {
        w.resizeTo(width, height);
        w.moveTo(screen.availWidth/2 - width/2, screen.availHeight/2 - height/2);
}

function setRowEvents(table, evt, func, start) {
        for (var i = start; i < table.rows.length; i++)
                eval("table.rows[" + i + "]." + evt + " = " + func +";");
}

function trimNonNumericChars(varStr) {
        return varStr.replace(/\D/g,"");
}

function trimWhiteSpace(varStr) {
        return varStr.replace(/\s/g,"");
}

function lTrim(varStr) {
        return varStr.replace(/^\s+/,"");
}

function rTrim(varStr) {
        return varStr.replace(/\s+$/,"");
}

function trim(varStr) {
        return varStr.replace(/^\s+|\s+$/g,"");
}

function maxLength(field, length) {
        if (field.value.length > length){
                alert("You have exceeded the maximum number of characters (" + length + ") for this field by " + (field.value.length - length) + " character(s).");
                field.focus();
                return false;
        }else{
                return true;
        }
}

function hide(x){
        x.style.display = "none";
}

function show(x){
        x.style.display = "block";
}

//used for moving the selected item in fromSelect to toSelect - where they are exclusive from each other.
function moveSelection (fromSelect, toSelect) {
        var index = fromSelect.selectedIndex;

        for (var i = 0; i< fromSelect.length; i++)
                if (fromSelect.options[i].selected){
                
                        var newOption = document.createElement("OPTION");
                        newOption.value = fromSelect.options(i).value;
                        newOption.text =  fromSelect.options(i).text;

                        toSelect.add(newOption);                
                        fromSelect.remove(i);
                        i--;
                }
                
        if (index != fromSelect.length)
                fromSelect.selectedIndex = index;
        else if (fromSelect.length != 0)
                fromSelect.selectedIndex = index - 1;
}

function selectAdd (toSelect, value, text) {
        var newOption = document.createElement("OPTION");
        newOption.value = value;
        newOption.text =  text;

        toSelect.add(newOption);                
}

function selectClear (list) {
        while (list.length > 0){
                list.options.remove(0);
        }
}

function selectReset (list){
    var options = list.options;
 
    for (var i = 0; i < options.length; i++) {
        options[i].selected = false;
    } 
}

function setSelectTextValue (list, text){
        var options = list.options;
        
        for (var i=0; i < options.length; i++)
                if (options[i].text == text){
                        list.selectedIndex = i;
                        return;
                }       
}

function setSelectValue (list, value){
        var options = list.options;
        
        for (var i=0; i < options.length; i++)
                if (options[i].value == value){
                        list.selectedIndex = i;
                        return;
                }       
}

function getSelectTextValue(list) {
        return list.options[list.selectedIndex].text;
}

function getSelectValue(list) {
        return list.options[list.selectedIndex].value;
}

function getMultiSelectValue(list) { 
        var options = list.options; 
        var values = new Array(); 
        var x = 0;  
        
        for (var i = 0; i < options.length; i++) {
                if (options[i].selected) {   
                        values[x++] = options[i].value;   
                } 
        }
 
        return values;
}

function getMultiSelectTextValue(list) { 
        var options = list.options; 
        var values = new Array(); 
        var x = 0;  
        
        for (var i = 0; i < options.length; i++) {
                if (options[i].selected) {   
                        values[x++] = options[i].text;   
                } 
        }
 
        return values;
}

function phoneOnly() {
        if ((window.event.keyCode < 48 || window.event.keyCode > 57) && window.event.keyCode != 32) {
                window.event.returnValue = false;
        }
}

function numericOnly(point) {
        if (!point && window.event.keyCode == 46) {
                window.event.returnValue = false;
        }
        if ((window.event.keyCode < 45 || window.event.keyCode > 57) && window.event.keyCode != 47) {
                window.event.returnValue = false;
        }
}

function checkBounds(field, minNumber, maxNumber) {
        if (field.value.length == 0) {
                return true;
        }
        
        if (parseFloat(field.value) < minNumber) {
                alert("The minimum valued allowed for this field is " + minNumber + ".");
                field.focus();
                field.select();
                return false;
        }
        if (parseFloat(field.value) > maxNumber) {
                alert("The maximum valued allowed for this field is " + maxNumber + ".");
                field.focus();
                field.select();
                return false;
        }

        return true;
}

function decimalPlaces(field, dp)
{

        if(window.event)//IE
        {
        var endRange;

        if (dp == 0 && window.event.keyCode == 46){
                window.event.returnValue = false;
                return;
        }
        
        // CATCH INVALID CHARACTERS
        if ((window.event.keyCode < 47 || window.event.keyCode > 57) && window.event.keyCode != 45 && window.event.keyCode != 46) {
                window.event.returnValue = false;
                return;
        }       
        
        if (document.selection.type == "None") {
                // WHEN TEXT NOT SELECTED
                
                endRange = document.selection.createRange();
                endRange.moveEnd("textedit");

                // CATCH MULTIPLE -'S
                if (window.event.keyCode == 45 && (field.value.indexOf("-") != -1 || (field.value.length - endRange.text.length) != 0)) {
                        window.event.returnValue = false;
                        return;
                }
                
                if (dp != 0) {  
                
                        // CATCH DECIMAL POINTS 
                        if (window.event.keyCode == 46 && (field.value.indexOf(".") != -1 || endRange.text.length > dp)) {
                                window.event.returnValue = false;
                                return;
                        }       

                        // CATCH DECIMAL PLACES 
                        if (window.event.keyCode != 45 && window.event.keyCode != 46 && endRange.text.indexOf(".") == -1 && getDecimalPlaces(field.value) >= dp) {
                                window.event.returnValue = false;
                                return;
                        }
                }
                
        } else if (document.selection.type == "Text"){
                // WHEN TEXT SELECTED
        
                endRange = document.selection.createRange();    

                // CATCH MULTIPLE -'S
                if (window.event.keyCode == 45 && (field.value.indexOf(endRange.text) > 0) || (endRange.text.indexOf("-") == -1 && field.value.indexOf("-") != -1)) {
                        window.event.returnValue = false;
                        return;
                }
                
                if (dp != 0) {
                
                        // CATCH DECIMAL POINTS 
                        if (window.event.keyCode == 46 && endRange.text.indexOf(".") == -1 && field.value.indexOf(".") != -1) {
                                window.event.returnValue = false;
                                return;
                        }       
                                        
                }
        }
        }
}

function formatCurrency(field, dp) {
        var start = 0;
        
        if (field.lastvalue != field.value && field.value.length > 0) {
        
                if (field.value.substr(0, 1) == "-") {
                        start = 1;
                }
                        
                if (dp > 0 && field.value.indexOf(".") == -1) {
                        field.value += ".";
                }
                
                if (field.value.substr(start, 1) == ".") {
                        field.value = field.value.substr(0, start) + "0" + field.value.substr(start, field.value.length - start);
                }
                
                while (field.value.length - field.value.indexOf(".") - 1 < dp) {
                        field.value += "0";
                }
                
                field.lastvalue = field.value;
        }
}

function excludeCharacters(chars) {
        for (var i = 0; i < chars.length; i++) {
                if (chars.charCodeAt(i) == window.event.keyCode) {
                        window.event.returnValue = false;
                        return;
                }
        }
}

function getDecimalPlaces(num) {
        if (num.indexOf(".") == -1) {
                return 0;
        } else {
                return num.length - num.indexOf(".") - 1;
        }
}

function nz(val, rtn) {
        return cnull(val, rtn);
}

function MT(val, rtn) {
        if (val.length == 0) {
                return rtn;     
        } else {
                return val;     
        }
}

function cnull(val, rtn){
        if (val == null  || val == 'null' || val == 'undefined' || typeof(val) == 'undefined')
                return rtn;     
        else
                return val;             
}

function charToUpper(){
        var k = window.event.keyCode;
        
        if (k > 96 && k < 123){
                window.event.keyCode = k - 32;
        }
        return;
}

function format(n, d) {
 
  with (Math) {
 
    // Separate the sign from the number
    var sign  = (n < 0) ? "-" : "";
    var num  = abs(n);

         num = floor((num * pow(10, d + 1) + 5) / 10) / pow(10, d);
         var whole = floor(num).toString();
         var frac  = round((num - whole) * pow(10, d)).toString();
  }

  // Zero-fill decimal
  for ( ; frac.length < d ; ) frac = "0" + frac;

  // Put it all back together
  return sign + whole + "." + frac;
}

function rot13(value) {
        var coding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMabcdefghijklmnopqrstuvwxyzabcdefghijklm0123456789@.-{}~!$?=[]_*+#0123456789@.-';
        var text = '';
        var i = 0;

        for (i = 0; i < value.length; i++) {
                character = value.charAt(i);
                position = coding.indexOf(character);
                if (position > -1) {
                        character = coding.charAt(position + 13);
                }
                text += character;
        }

return text;
}

function decodeEmail(encodedAddress, text, subject, attribs) {
        var decodedAddress = rot13(encodedAddress);
        var subjectText = "";
        var attribsText = "";

        if (text == "" || typeof(text) == 'undefined') {
                text = decodedAddress.replace("-", "&#8209");
        }

        if (subject != "" && typeof(subject) != 'undefined') {
                subjectText = "?subject=" + subject;
        }

        if (attribs != "" && typeof(attribs) != 'undefined') {
                attribsText = " " + attribs;
        }

        document.write("<a href='mailto:" + decodedAddress + subjectText + "'" + attribsText + ">" + text + "</a>");
}

function showStatus(text) {
        status = text;
        return true;
}

function clickButton(e, buttonId) {
        if (e.keyCode == 13) {
                sendButtonClick(buttonId);
                return false; 
        } 
}

function sendButtonClick(buttonId) {
        btn = document.getElementById(buttonId);
        if (btn != null) {
                if (btn.click) {
                        btn.click();
                } else {
                        if (btn.onclick) {
                                btn.onclick();
                        } else {
                                eval(btn.href);
                        }
                }
        }
}

function isValidEmail(address) {
        var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
        var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid

        address = address.replace(/(^\s+)|(\s+$)/g, ""); // trim

        if (!reg1.test(address) && reg2.test(address)) { // if syntax is valid
                return true;
        }
}

function getFirstTextNode(x) {
        if (x != null && x.nodeType != 3) {
                if (x.hasChildNodes) {
                        x = getFirstTextNode(x.firstChild)
                } else {
                        x = null;
                }
        }
        
        return x
}
