// General
// --------------------------------------------------------------------------
window.shopster = window.shopster || {};

shopster.namespace = function(ns) 
{
    if (!ns || !ns.length) 
    {
        return null;
    }

    var levels = ns.split(".");
    var nsobj = shopster;

    // Shopster is implied, so it is ignored if it is included
    for (var i=(levels[0] == "shopster") ? 1 : 0; i<levels.length; ++i) 
    {
        nsobj[levels[i]] = nsobj[levels[i]] || {};
        nsobj = nsobj[levels[i]];
    }

    return nsobj;
};

// Utility
// --------------------------------------------------------------------------

shopster.namespace("util");

var clock = 0;

shopster.util.toggleDivExpansion = function(divId)
{
	if (!clock)
	{
		clock = 1;
		var div = $(divId);
		
		if (div.getStyle('display') == 'none')
		{
			shopster.util.expandDiv(divId, Object.extend({afterFinish: afterFinish}, arguments[1] || {}));
		}
		else
		{
			shopster.util.collapseDiv(divId, Object.extend({afterFinish: afterFinish}, arguments[1] || {}));
		}
	}
}

function afterFinish()
{
	clock = 0;
}
		
shopster.util.expandDiv = function(divId)
{
	var options = arguments[1] || {};
	if (options.rate)
	{
		var dimensions = $(divId).getDimensions();
		var duration = dimensions.height / options.rate;
		options.duration = duration;
	}
	
	Effect.BlindDown(divId, options);
}

shopster.util.collapseDiv = function(divId)
{
	var options = arguments[1] || {};
	if (options.rate)
	{
		var dimensions = $(divId).getDimensions();
		var duration = dimensions.height / options.rate;
		options.duration = duration;
	}
	
	Effect.BlindUp(divId, options);
}

shopster.util.highlight = function(obj, highlightClass)
{
	obj.className = highlightClass;
}

shopster.util.popupSelectedSite = function(array, ddlId)
{
	var ddl = $(ddlId);
	shopster.util.popupSite(array[ddl.selectedIndex], 'Site', 500, 780,'yes','yes','yes','yes','yes','yes','yes');
}

shopster.util.highlightParentRow = function(objId, highlightClass)
{
	var obj = $(objId);
	
	while (obj.tagName != 'tr' && obj.tagName != 'TR')
	{
		if (!obj.parentNode) return;
		obj = obj.parentNode;
	}
	
	if (Element.hasClassName(obj, highlightClass))
	{
		Element.removeClassName(obj, highlightClass);
	}
	else
	{
		Element.addClassName(obj, highlightClass);
	}
}

shopster.util.popupSite = function(url, name, height, width, resizable, toolbar, location, directories, status, menubar, scrollbars)
{
	window.open(url, name, 
		'height=' + height +
		',width=' + width +
		',resizable=' + resizable +
		',toolbar=' + toolbar +
		',location=' + location +
		',directories=' + directories +
		',status=' + status +
		',menubar=' + menubar +
		',scrollbars=' + scrollbars);
}

shopster.util.popupHelp = function(url)
{
	shopster.util.popupSite(url, 'Help', 500, 780, 'no', 'no', 'no', 'no', 'no', 'no', 'yes');
}

shopster.util.getFirstForm = function()
{
	var elements = document.body.getElementsByTagName('form');
	return elements[0];
}

shopster.util.toggleDivDisplay = function (divId)
{
	var div = $(divId);
	
	if (div.style.display == '')
		shopster.util.hideDiv(div);
	else
		shopster.util.showDiv(div);
}

shopster.util.showDiv = function(div)
{
	div.style.display = '';
}

shopster.util.hideDiv = function(div)
{
	div.style.display = 'none';
}

shopster.util.toggleCheckAll = function(name)
{
	var frm = shopster.util.getFirstForm();
	
	var allSelected = true;
	for (i = 0; i < frm.length; i++) 
	{
		e = frm.elements[i];
		if (e.type == 'checkbox' && e.name.indexOf(name) != -1) 
		{
			if (e.checked == false)
			{
				allSelected = false;
				break;
			}
		}
	}
	
	if (allSelected == true)
		shopster.util.uncheckAll(name);
	else
		shopster.util.checkAll(name);
}

shopster.util.checkAll = function(name)
{
	var frm = shopster.util.getFirstForm();
	for (i = 0; i < frm.length; i++) 
	{
		e = frm.elements[i];
		if (e.type == 'checkbox' && e.name.indexOf(name) != -1) 
		{
			e.checked = true;
		}
	}
}

shopster.util.uncheckAll = function(name)
{
	var frm = shopster.util.getFirstForm();
	for (i = 0; i < frm.length; i++)
	{
		e = frm.elements[i];
		if (e.type == 'checkbox' && e.name.indexOf(name) != -1)
		{
			e.checked = false;
		}
	}
}

shopster.util.getValuesAsDelimitedList = function(cbField, fieldSuffix)
{
	var frm = shopster.util.getFirstForm();
	var list = '';
	for (i = 0; i < frm.length; i++)
	{
		e = frm.elements[i];
		if (e.type == 'checkbox' && e.id.indexOf(cbField) != -1 && e.checked)
		{
			pos = e.id.indexOf(cbField);
			prefix = e.id.substring(0, pos);
			e2 = document.getElementById(prefix + fieldSuffix);
			if (e2 != null && e2.type == 'hidden')
			{
				if (list.length > 0)
				{
					list = list + ',';
				}
				list = list + escape(e2.value);
			}
		}
	}
	return list;
}

shopster.util.getNumberCheckboxesChecked = function(cbName)
{
	var frm = shopster.util.getFirstForm();
	var count = 0;
	for (i = 0; i < frm.length; i++)
	{
		e = frm.elements[i];
		if (e.type == 'checkbox' && e.name.indexOf(cbName) != -1 && e.checked == true)
		{
			count = count + 1;
		}
	}
	return count;
}

shopster.util.getIFrameContents = function(iframe)
{
	var content = '';
	if (iframe.contentDocument) {
		content = iframe.contentDocument; 
	} else if (iframe.contentWindow) {
		content = iframe.contentWindow.document;
	} else if (iframe.document) {
		content = iframe.document;
	}
	return content;
}
shopster.util.dummyCallback = function(res) {}

shopster.util.checkDefaultButton = function(button, event) {
	event = window.event || event;
	button = $(button);
	
	if (event.keyCode == 13) {
		if (Prototype.Browser.IE) {
			button.click();
			return false;
		}
	}
	
	return true;
}

shopster.util.redirect = function(url) {
	window.location = url;
}

shopster.util.redirectParent = function(url) {
	parent.window.location = url;
}

shopster.util.activateControl = function(id) {
	$(id).activate();
}
