﻿/*
Helper 1.0.0 - Javascript Helper class. Collection of general javascript properties/methods (non system specific).

Copyright (c) Aydus (www.aydus.com)

Date: 5/29/2010

ChangeLog:
*/
function Helper() {

    // Format a number as currency.
    this.FormatCurrency = function(amount) {
        var i = parseFloat(amount);
        if (isNaN(i)) { i = 0.00; }
        var minus = '';
        if (i < 0) { minus = '-'; }
        i = Math.abs(i);
        i = parseInt((i + .005) * 100);
        i = i / 100;
        s = new String(i);
        if (s.indexOf('.') < 0) { s += '.00'; }
        if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
        s = minus + s;
        return s;
    }

    // Create jquery AJAX parameter string from array of key/value pairs.
    // e.g. key1=value1&key2=value2
    this.CreateJQueryAJAXParams = function(a) {

        var params = '';

        for (var i = 0; i < a.length; i++) {
            if (i != 0) {
                params += '&';
            }
            params += a[i][0] + '=' + a[i][1];
        }
        return params;
    }

    // Create jQuery AJAX Json parameter string from array of key/value pairs.
    // e.g. {"key1": "value1", "key2": "value2"}
    this.CreateJQueryAJAXJsonParams = function(a) {

        var params = '{';

        for (var i = 0; i < a.length; i++) {
            if (i != 0) {
                params += ', ';
            }
            params += '"' + a[i][0] + '": ' + '"' + a[i][1] + '"';
        }
        return params + '}';
    }

    // Popup browser window. Optional parameters (width, height, windowFeatures).
    this.Popup = function(url, title, width, height, windowFeatures) {

        if (width == undefined) {
            width = 600;
        }
        if (height == undefined) {
            height = 375;
        }
        if (windowFeatures == undefined) {
            windowFeatures = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,left=0,top=0';
        } 

        var windowFeatures = windowFeatures + ',width=' + width + ',height=' + height;
        window.open(url, title, windowFeatures);
        return (true);
    }

    // Wrapper for the standard DOM getElementsByTagName function. IE errors on empty nodes so wrap in test.
    this.getElementsByTagName = function(data, name) {

        var returnValue = '';
        
        if (data.getElementsByTagName(name)[0].childNodes[0] != null) {
            returnValue = data.getElementsByTagName(name)[0].childNodes[0].nodeValue;
        }
        return returnValue;
    }

    this.NaNToZero = function(n) {
        if (isNaN(n)) {
            return 0;
        }
        else {
            return n;
        }
    }

    this.Test = function() {
        alert('Hello World!');
    }
}
