﻿function StringBuffer() 
{
    this.buffer = [];
}

StringBuffer.prototype.append = function append(string) 
{
    this.buffer.push(string);
    return this;
};

StringBuffer.prototype.toString = function toString() 
{
    return this.buffer.join("");
};


function Hash()
{
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) 
	{
		if (typeof(arguments[i + 1]) != 'undefined') 
		{
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
}

	Hash.prototype = new Object();

	Hash.prototype.removeItem = function(in_key)
	{
		var tmp_previous;
		if (typeof(this.items[in_key]) != 'undefined') 
		{
			this.length--;
			var tmp_previous = this.items[in_key];
			delete this.items[in_key];
		}
	   
		return tmp_previous;
	}
	
	Hash.prototype.getItem = function(in_key)
	{
		return this.items[in_key];
	}
	
	Hash.prototype.setItem = function(in_key, in_value)
	{
		var tmp_previous;
		if (typeof(in_value) != 'undefined') 
		{
			if (typeof(this.items[in_key]) == 'undefined') 
			{
				this.length++;
			}
			else 
			{
				tmp_previous = this.items[in_key];
			}

			this.items[in_key] = in_value;
		}
	   
		return tmp_previous;
	}



	Hash.prototype.length = function()
	{
		return this.length;
	}

	Hash.prototype.clear = function()
	{
		for (var i in this.items) 
		{
			delete this.items[i];
		}

		this.length = 0;
	}

	
	Hash.prototype.getPartialJSON = function()
	{
		var result = '';
		
		for (var i in this.items) 
		{
		    if (typeof(this.items[i]) != 'undefined') 
		    {
			    result = result + i + '|||' + this.items[i] + '~~~';
		    }
		}
		
		if (result.substring(result.length - 3) == '~~~')
		    result = result.substring(0, result.length - 3);
		
		return result;
	}


//////////////////////////////////////////////////////////////////////////////////////////
//End Objects
//////////////////////////////////////////////////////////////////////////////////////////

function AjaxFailed(result) {
    alert(result.status + ' ' + result.statusText);
}

function ShowHideAdvSearch(v, s) {
    var st = document.getElementById(v);
    if (st.value == 'H') {
        //We need to show it
        st.value = 'V';
        $('#' + s).show('fast');
    }
    else {
        //We need tohide it
        st.value = 'H';
        $('#' + s).hide('fast');
    }
}

function TestForEnter(ctl, e) 
{
    var result = true;
    e = e || window.event;
    if (ctl != null && ctl.type == "text") 
    {
        var keyPressed = e.charCode || e.keyCode;

        if (keyPressed == 13 || keyPressed == 10) 
        {
            result = false;
        }
    }

    return result;
}

function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}

function DisallowChange(ctl, e) 
{
    var result = true;
    e = e || window.event;
    if (ctl != null && ctl.type == "text") 
    {
        result = false;
    }

    return result;
}

function MakeWhereClause(ctls) 
{
    var c;
    var result = '';
    for (var i = 0; i < ctls.length; i++) 
    {
        c = document.getElementById(ctls[i].name);

        if (c.nodeName == 'INPUT') 
        {
            if (c.value != '') 
            {
                if (result != '') 
                {
                    result = result + " and " + ctls[i].field + " like '%" + c.value + "%'";
                }
                else 
                {
                    result = ctls[i].field + " like '%" + c.value + "%'";
                }
            }
        }
        else if (c.nodeName == 'SELECT') 
        {
            if (c.value != '') 
            {
                if (result != '') 
                {
                    result = result + " and " + ctls[i].field + " like '%" + c.value + "%'";
                }
                else 
                {
                    result = ctls[i].field + " like '%" + c.value + "%'";
                } 
            }
        }


    }

    if (result != '')
        result = " and (" + result + ")";

    return result;
}

function ControlType(name, field) 
{
    this.name = name;
    this.field = field;
}

function PopulateDropdownStates(ctl, cty) 
{
    ctl.options.length = 0;
    
    if (cty == 'CA') 
    {
        addOption(ctl, "", "");
        addOption(ctl, "AB", "AB");
        addOption(ctl, "BC", "BC");
        addOption(ctl, "MB", "MB");
        addOption(ctl, "NB", "NB");
        addOption(ctl, "NL", "NL");
        addOption(ctl, "NS", "NS");
        addOption(ctl, "NT", "NT");
        addOption(ctl, "NU", "NU");
        addOption(ctl, "ON", "ON");
        addOption(ctl, "PE", "PE");
        addOption(ctl, "QC", "QC");
        addOption(ctl, "SK", "SK");
        addOption(ctl, "YT", "YT");
    }
    else 
    {
        addOption(ctl, "", "");
        addOption(ctl, "AL", "AL");
        addOption(ctl, "AK", "AK");
        addOption(ctl, "AR", "AR");
        addOption(ctl, "AZ", "AZ");
        addOption(ctl, "CA", "CA");
        addOption(ctl, "CO", "CO");
        addOption(ctl, "CT", "CT");
        addOption(ctl, "DE", "DE");
        addOption(ctl, "DC", "DC");
        addOption(ctl, "FL", "FL");
        addOption(ctl, "GA", "GA");
        addOption(ctl, "GU", "GU");
        addOption(ctl, "HI", "HI");
        addOption(ctl, "IA", "IA");
        addOption(ctl, "ID", "ID");
        addOption(ctl, "IL", "IL");
        addOption(ctl, "IN", "IN");
        addOption(ctl, "KS", "KS");
        addOption(ctl, "KY", "KY");
        addOption(ctl, "LA", "LA");
        addOption(ctl, "MA", "MA");
        addOption(ctl, "MD", "MD");
        addOption(ctl, "ME", "ME");
        addOption(ctl, "MI", "MI");
        addOption(ctl, "MN", "MN");
        addOption(ctl, "MO", "MO");
        addOption(ctl, "MS", "MS");
        addOption(ctl, "MT", "MT");
        addOption(ctl, "NC", "NC");
        addOption(ctl, "ND", "ND");
        addOption(ctl, "NE", "NE");
        addOption(ctl, "NH", "NH");
        addOption(ctl, "NJ", "NJ");
        addOption(ctl, "NM", "NM");
        addOption(ctl, "NV", "NV");
        addOption(ctl, "NY", "NY");
        addOption(ctl, "OH", "OH");
        addOption(ctl, "OK", "OK");
        addOption(ctl, "OR", "OR");
        addOption(ctl, "PA", "PA");
        addOption(ctl, "PR", "PR");
        addOption(ctl, "RI", "RI");
        addOption(ctl, "SC", "SC");
        addOption(ctl, "SD", "SD");
        addOption(ctl, "TN", "TN");
        addOption(ctl, "TX", "TX");
        addOption(ctl, "UT", "UT");
        addOption(ctl, "VA", "VA");
        addOption(ctl, "VI", "VI");
        addOption(ctl, "VT", "VT");
        addOption(ctl, "WA", "WA");
        addOption(ctl, "WI", "WI");
        addOption(ctl, "WV", "WV");
        addOption(ctl, "WY", "WY");
    }
}

function addOption(ctl, text, value) 
{
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    ctl.options.add(optn);
}

function ValidateZipCode(zip, cty) 
{
    var result = true;
    var re;

    if (cty == 'US') 
    {
        re = /(^\d{5}$)/;
        result = re.test(zip);
    }
    else if (cty == 'CA') 
    {
        re = /[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ] *\d[ABCEGHJKLMNPRSTVWXYZ]\d/;
        result = re.test(zip);
    }

    return result;
}

function DetectBrowser() 
{
    var result = '';

    if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) 
    {
        result = 'FireFox';
    }
    else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) 
    {
        var ieversion = new Number(RegExp.$1)
        if (ieversion >= 9)
            result = 'NIE';
        else
            result = 'IE';
    }
    else if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) 
    {
        result = 'Opera';
    }
    else if (/Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent)) 
    {
        result = 'Safari';
    }
    else if (/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent)) 
    {
        result = 'Chrome';
    }
    else 
    {
        result = 'NotSupport';
    }

    return result;
}

function roundNumber(number, decimals) {
    var newString; // The new rounded number
    decimals = Number(decimals);
    if (decimals < 1) {
        newString = (Math.round(number)).toString();
    } else {
        var numString = number.toString();
        if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
            numString += "."; // give it one at the end
        }
        var cutoff = numString.lastIndexOf(".") + decimals; // The point at which to truncate the number
        var d1 = Number(numString.substring(cutoff, cutoff + 1)); // The value of the last decimal place that we'll end up with
        var d2 = Number(numString.substring(cutoff + 1, cutoff + 2)); // The next decimal, after the last one we want
        if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
            if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
                while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
                    if (d1 != ".") {
                        cutoff -= 1;
                        d1 = Number(numString.substring(cutoff, cutoff + 1));
                    } else {
                        cutoff -= 1;
                    }
                }
            }
            d1 += 1;
        }
        if (d1 == 10) {
            numString = numString.substring(0, numString.lastIndexOf("."));
            var roundedNum = Number(numString) + 1;
            newString = roundedNum.toString() + '.';
        } else {
            newString = numString.substring(0, cutoff) + d1.toString();
        }
    }
    if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
        newString += ".";
    }
    var decs = (newString.substring(newString.lastIndexOf(".") + 1)).length;
    for (var i = 0; i < decimals - decs; i++) newString += "0";
    var newNumber = Number(newString);// make it a number if you like
    return newNumber;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function formatWithComma(number) 
{
    var newNumber, front, zero;
    zero = '';
    if (number.length == 4)
    {                    
        zero = parseFloat(number.substring(1));
        front = parseFloat(number.substring(0,1));
    }
    else if (number.length == 5)
    {
        zero = parseFloat(number.substring(2));
        front = parseFloat(number.substring(0,2));
    }
    else if (number.length == 6)
    {
        zero = parseFloat(number.substring(3));
        front = parseFloat(number.substring(0,3));
    }
    else
    {
        newNumber = number;
    }
    
    if (zero == '0')
        zero = '000';
    
    if (zero != '')
        newNumber = front + ',' + zero;
    
    return newNumber;
}

