/**
 * Browser and DOM independent HTML element class setter
 * @author Max Bobak
 * @param id - (string) id of the element attribute is set to
 * @param value - (string) new class name
 * @return void
 */
function XB_setClass(id, value){
	var t = document.getElementById(id);
	if(t){
		if(t.getAttribute('className') != null) { // IE bullshit
			t.setAttribute('className', value);
		} else { // real browsers
			t.setAttribute('class', value);
		}
	}
}

/**
 * Formats the product price according to currency used and prints it.
 * @author Max Bobak
 * @param currencyId - (integer) currency id, used to determine the proper format 
 * @param productPrice - (float) product price, numerical amount 
 * @param currencySymbol - (string) currency symbol, symbolic representation of currency 
 * @param currencyName - (string) currency name, representation of currency
 * @return void  
 */
function displayPrice(currencyId, productPrice, currencySymbol, currencyName){
	var p = currencyFormat(productPrice);
	document.write(currencySymbol+p+" <span>"+currencyName+"</span>");
}

/**
 * Formats the product price according to currency used and returns formatted string.
 * @author Max Bobak
 * @param currencyId - (integer) currency id, used to determine the proper format 
 * @param productPrice - (float) product price, numerical amount 
 * @param currencySymbol - (string) currency symbol, symbolic representation of currency 
 * @param currencyName - (string) currency name, representation of currency
 * @return void  
 */
function returnPrice(currencyId, productPrice, currencySymbol, currencyName){
	var p = currencyFormat(productPrice);
	var s;
	s = currencySymbol+p+" "+currencyName;
	return s;
}

/**
 * If needed adds .00 or 0 to number to properly output it
 * @author Max Bobak
 * @param amount - (string) amount to be formatted
 * @return formatted string
 */
function currencyFormat(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;
}

/**
 * @author Max Bobak
 * @param field - (object) object to operate on, in most cases this
 * @param defaultMessage - (string) the message that needs to be toggled
 */
function fieldToggle(field, defaultMessage){
	if(field.value == ""){
		field.value = defaultMessage;
		return;
	}
	if(field.value == defaultMessage){
		field.value = "";
		return;
	}
}

/**
 * @author Max Bobak
 * @param arrayName - (array) object to operate on
 */
function unique(arrayName){
    var newArray=new Array();
    label:for(var i=0; i<arrayName.length;i++ )
    {  
        for(var j=0; j<newArray.length;j++ )
        {
            if(newArray[j]==arrayName[i]) 
                continue label;
        }
        newArray[newArray.length] = arrayName[i];
    }
    return newArray;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

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++;
		}
	}
   
	this.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;
	}

	this.getItem = function(in_key) {
		return this.items[in_key];
	}

	this.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;
	}

	this.hasItem = function(in_key)
	{
		return typeof(this.items[in_key]) != 'undefined';
	}

	this.clear = function()
	{
		for (var i in this.items) {
			delete this.items[i];
		}

		this.length = 0;
	}
}

function GetChildCount(id){
    var container = document.getElementById(id);

    var childCount = 0;
    if (container.childElementCount === undefined) {
        if (container.children) {
            childCount = container.children.length;
        }
        else {  // Firefox before version 3.5
            var child = container.firstChild;
            while (child) {
                if (child.nodeType == 1 /*Node.ELEMENT_NODE*/) {
                    childCount++;
                }
                child = child.nextSibling;
            }
        }
    }
    else {
        childCount = container.childElementCount;
    }
    return childCount;
}
