﻿/* DOLLAR FUNCTIONS JS FILE
 * CREATED: 28 Oct 2008
 * AUTHOR: Jako van Rensburg
 */

/* $ ELEMENT BY ID */
var $ = function(elem){
    if(document.getElementById && document.getElementById(elem)) {
        return document.getElementById(elem);
    }
    else {
        if(document.all && document.all(elem)) {
            return document.all(elem);
        }
        else {
            if(document.layers && document.layers[elem]) {
                return document.layers[elem];
            }
            else {
                return false;
            }
        }
    }
};

/* CREATE ELEMENT
 * IF "APPENDTO" SPECIFIED, IT WILL APPEND TO THE ELEMENT
 * ELSE, IT WILL RETURN THE OBJECT CREATED
 */
var $ce = function(elementType, propertyObj, styleObj, appendTo){
    $d('JS Function','Create Element: type: ' + elementType + '<br />' + general.objectToString(propertyObj) + '<br />' + general.objectToString(styleObj));
    var returnElem = document.createElement(elementType);
    for (var attr in propertyObj) {
        if (propertyObj.hasOwnProperty(attr)) {
            if (propertyObj[attr] != '') {
                returnElem[attr] = propertyObj[attr];
            }
        }
    }
    for (var prop in styleObj) {
        if (styleObj.hasOwnProperty(prop)) {
            if (styleObj[prop] != '') {
                returnElem.style[prop] = styleObj[prop];
            }
        }
    }
    if(appendTo) {
        appendTo.appendChild(returnElem);
    }
    return returnElem;
};

/* BEGIN DEBUGGING */
var debug = {
    debugEnabled: true,
    onlyPopupWindowOnError: false,
    silentDebugging: true,
    win: '',
    message: '',
    headersBeenSet: false,
    debugContainer: '',
    isDump: false
};

var $d = function(heading, message){
    if (debug.debugEnabled == true) {
        var headingPrefix = '';
        var headingSufix = '';
        if (heading.match('Error') == 'Error') {
            heading = '<table cellspacing="0" cellpadding="0"><tr><td style="color:#DC143C;"><strong>' + heading + ':&nbsp;</strong></td></tr></table>';
        }
        else {
            heading = '<table cellspacing="0" cellpadding="0"><tr><td><strong>' + heading + ':&nbsp;</strong></td></tr></table>';
        }
        if (debug.headersBeenSet == false) {
            debug.debugContainer = '<style>body{margin:0px; padding:0px; font-size:10px !important; background-color:#000000;}hr{border:none;border-top:dashed 1px #808080;color:#FFFFFF;background-color:#000000;height:1px;}table{color:#FFFFFF; font-size:10px;}div{color:#FFFFFF;}</style><table style="font-family: Verdana; font-size: 10px; width:100%;" cellpadding="0" cellspacing="0"><tr><td style="background-color:#333333; color:#FFFFFF; padding:2px;"><table cellspacing="0" cellpadding="0" style="color:#FFFFFF; font-size:10px; font-weight: bold;"><tr><td><img src="images/icons/application_xp_terminal.png" />&nbsp;</td><td style="padding-bottom:2px;">Debug Console:</td></tr></table></td></tr><tr><td><div id="debugMessage" style="padding:5px; height:360px; overflow:auto;"></div></td></tr></table>';
            debug.headersBeenSet = true;
        }
        debug.message += headingSufix + '<table cellpadding="0" cellspacing="0"><tr><td valign="top" style="padding-left:2px; padding-bottom:2px;">' + date.getTime() + '</td><td style="padding-left:5px; padding-bottom:2px;">' + heading + '<div style="padding-left:18px;">' + message + '</div></td></tr></table>' + headingPrefix;
        if (debug.onlyPopupWindowOnError == true) {
            if (heading.match('Error') == 'Error') {
                debug.win = window.open('', 'DebugWindow', 'menubar=0,resizable=0,width=1000,height=395,toolbar=0,scrollbars=0,status=0,directories=0,location=0,bottom=0,left=0');
                debug.win.document.write(debug.debugContainer);
                debug.win.document.getElementById('debugMessage').innerHTML = debug.message;
                debug.win.document.getElementById('debugMessage').scrollTop = debug.win.document.getElementById('debugMessage').scrollHeight;
                debug.win.focus();
            }
        }
        else {
            if (debug.silentDebugging == false) {
                debug.win = window.open('', 'DebugWindow', 'menubar=0,resizable=0,width=1000,height=395,toolbar=0,scrollbars=0,status=0,directories=0,location=0,bottom=0,left=0');
                debug.win.document.write(debug.debugContainer);
                debug.win.document.getElementById('debugMessage').innerHTML = debug.message;
                debug.win.document.getElementById('debugMessage').scrollTop = debug.win.document.getElementById('debugMessage').scrollHeight;
                debug.win.focus();
            }
        }
    }
};
/* END DEBUGGING */

/* CHECK IF USER TOKEN IS STILL VALID */
var $l = function(result){
    $d('JS Function', 'Check if user Token still valid!');
    timer.stop();
    if (result == 'Token') {
        general.alert('<font style="color:#FF6347;">Security Error: This L@W Deed session is no longer valid, please login again!<br /><br /><span style="font-size:10px;font-weight:normal;">Note: If you are inactive for 15 minutes or more,<br />you can get logged out by another user.</span></font>','Continue',function() {
                        general.logMeOut();
                    });
    }
    general.hideAnyToolsDivs();
    general.hideLoader();
};

/* INPUT VALIDATION */
var $v = function(obj){
    $d('JS Function', 'Validating Input: '+obj.id);
    if (obj.tagName.toString() == 'INPUT' || obj.tagName.toString() == 'input') {
        if (obj.type.toString() == 'TEXT' || obj.type.toString() == 'text') {
            if (obj.value.toString() == '') {
                obj.style.backgroundColor = '#FFA595';
                help.showTooltip(obj.id,obj,help.helpWording.requiredField,{width:150,offsetX:(obj.offsetWidth+16),offsetY:5,enableHeading:false,arrowPos:'left',canShowAgain:true,hideClose:true});
                throw new Error('blank field exception');
            }
            else {
                if (obj.id.toString().match('mail') == 'mail' || obj.id.toString().match('email') == 'email' || obj.id.toString().match('Email') == 'Email') {
                    if (general.emailCheck(obj, obj.value)) {
                        obj.style.backgroundColor = '#FFFFFF';
                        return obj.value;
                    }
                    else {
                        obj.style.backgroundColor = '#FFA595';
                        throw new Error('invalid email address!');
                    }
                }
                else {
                    obj.style.backgroundColor = '#FFFFFF';
                    return obj.value;
                }
            }
        }
        else {
            if (obj.type.toString() == 'PASSWORD' || obj.type.toString() == 'password') {
                if (obj.value.toString() == '') {
                    obj.style.backgroundColor = '#FFA595';
                    help.showTooltip(obj.id,obj,help.helpWording.requiredField,{width:150,offsetX:(obj.offsetWidth+16),offsetY:5,enableHeading:false,arrowPos:'left',canShowAgain:true,hideClose:true});
                    throw new Error('blank field exception');
                }
                else {
                    obj.style.backgroundColor = '#FFFFFF';
                    return obj.value;
                }
            }
        }
    }
    else {
        if (obj.tagName.toString() == 'SELECT' || obj.tagName.toString() == 'select') {
            if (obj.options[obj.selectedIndex].value.toString() == '' || obj.options[obj.selectedIndex].value.toString() == '0') {
                obj.style.backgroundColor = '#FFA595';
                help.showTooltip(obj.id,obj,help.helpWording.requiredField,{width:150,offsetX:(obj.offsetWidth+16),offsetY:5,enableHeading:false,arrowPos:'left',canShowAgain:true,hideClose:true});
                throw new Error('blank field exception');
            }
            else {
                obj.style.backgroundColor = '#FFFFFF';
                return obj.options[obj.selectedIndex].value;
            }
        }
        else {
            if (obj.tagName.toString() == 'TEXTAREA' || obj.tagName.toString() == 'textarea') {
                if (obj.innerText) {
                    if (obj.innerText.toString() == '') {
                        obj.style.backgroundColor = '#FFA595';
                        help.showTooltip(obj.id,obj,help.helpWording.requiredField,{width:150,offsetX:(obj.offsetWidth+16),offsetY:5,enableHeading:false,arrowPos:'left',canShowAgain:true,hideClose:true});
                        throw new Error('blank field exception');
                    }
                    else {
                        obj.style.backgroundColor = '#FFFFFF';
                        return obj.innerText;
                    }
                }
                else {
                    if (obj.value.toString() == '') {
                        obj.style.backgroundColor = '#FFA595';
                        help.showTooltip(obj.id,obj,help.helpWording.requiredField,{width:150,offsetX:(obj.offsetWidth+16),offsetY:5,enableHeading:false,arrowPos:'left',canShowAgain:true,hideClose:true});
                        throw new Error('blank field exception');
                    }
                    else {
                        obj.style.backgroundColor = '#FFFFFF';
                        return obj.value;
                    }
                }
            }
        }
    }
};

/* COMPARE */
var $c = function(obj1, obj2){
    $d('JS Function', 'Comparing Inputs: Input 1: '+obj1.id+'<br />Input 2: '+obj2.id);
    if (obj1.tagName.toString() == 'INPUT') {
        if (obj1.type.toString() == 'text' && obj2.type.toString() == 'text') {
            if (obj1.value != obj2.value) {
                obj1.style.backgroundColor = '#FF6347';
                obj2.style.backgroundColor = '#FF6347';
                return false;
            }
            else {
                return true;
            }
        }
        else {
            if (obj1.type.toString() == 'password' && obj2.type.toString() == 'password') {
                if (obj1.value.toString() != obj2.value.toString()) {
                    obj1.style.backgroundColor = '#FF6347';
                    obj2.style.backgroundColor = '#FF6347';
                    return false;
                }
                else {
                    return true;
                }
            }
        }
    }
};

/* SET DIV INNERHTML */
var $hd = function(divID, contents) {
    $d('JS Function', 'Setting innerHTML: divID: '+divID);
    if ($(divID)) {
        $(divID).innerHTML = contents;
    }
};

/* APPEND CONTENTS TO DIV AND SCROLL TO BOTTOM */
var $ad = function(divID, contents) {
    var divContent = '';
    divContent = $(divID).innerHTML;
    $(divID).innerHTML = '';
    divContent = divContent + contents;
    $(divID).innerHTML = divContent;
    $(divID).innerHTML = $(divID).innerHTML;
    $(divID).scrollTop = $(divID).scrollHeight;
};
