/**
* Combo Object Selector v 1.1
*
* © 2007 MJ Bytes Ltd
* http://www.mjbytes.com
*
* Language: Javascript
* @Last Revision: 28/May/2007
* 
*/

function ComboSelector(combo)
{
	if (combo && document.getElementById(combo))
	{
		this.combo = document.getElementById(combo);
	}
	else return false;
}

ComboSelector.prototype =
{
	setSelectedCookie: function(c)
	{
		this.setSelected(this.findCookie(c));
	},
	
	setSelected: function(i)
	{
		var idx = this.findIndex(i);

		if (idx) this.combo.selectedIndex = idx;
	},
	
	getSelected: function()
	{
		return this.combo[this.combo.selectedIndex].value;
	},

	getSelectedText: function()
	{
		return this.combo[this.combo.selectedIndex].text;
	},

	getIndex: function()
	{
		return this.combo.selectedIndex;
	},
	
	findIndex: function(v)
	{
		for (var i = 0; i < this.combo.length; i++)
			if (v == this.combo.options[i].value) return i;

		return false;
	},
	
	findCookie: function(n)
	{
		var cn = n + "=";
		var arr = document.cookie.split(';');
		
		for (var i = 0; i < arr.length; i++) 
		{
			var cr = arr[i];
			
			while (cr.charAt(0)==' ') 
				cr = cr.substring(1, cr.length);
				
			if (cr.indexOf(cn) == 0)
				if (cr.indexOf(cn) == 0)
					return cr.substring(cn.length, cr.length);
		}

		return false;		
	},
	
	clearItems: function()
	{
		this.combo.options.length = 0;
	},
	
	fillItems: function(itm, obj)
	{
		this.combo.options[0] = new Option('Sub Category', '0');

		if (obj[itm])
		{
			var len = obj[itm].length;
			var newitems = obj[itm];

			for (var i = 0; i < len; i++)
			{
				this.combo.options[i+1] = new Option((obj[itm][i]).item, newitems[i].value);
			}
		}
	},

	addEvent: function(ev, fn)
	{
		if (this.combo.addEventListener)
		{
			this.combo.addEventListener(ev, fn, false);
		}
		else if (this.combo.attachEvent)
		{
			this.combo.attachEvent('on' + ev, fn);
		}
		else { this.combo['on' + ev] = fn };
	},

	removeEvent: function(ev, fn)
	{
		if (this.combo.removeEventListener)
		{
			this.combo.removeEventListener(ev, fn, false);
		}
		else if (this.combo.detachEvent)
		{
			this.combo.detachEvent('on' + ev, fn);
		}
		else { this.combo['on' + ev] = null };
	},
	
	getPage: function()
	{
		var pURL = document.URL;
			
		return pURL.substring(pURL.lastIndexOf('/', pURL)+1, pURL.length);
	},
	
	adjustPosition: function(x, y, brw)
	{
		if (this.detectBrowser() == brw)
		{
			this.combo.style.marginLeft = this.combo.style.marginLeft + x + 'px';
			this.combo.style.marginBottom = this.combo.style.marginBottom + y + 'px';
			this.combo.style.paddingTop = '1px';
		}
	},
	
	detectBrowser: function()
	{
		var browser = navigator.userAgent;
		
		if (browser.match('Firefox')) { return 'Firefox'; }
		else if (browser.match('SeaMonkey')) { return 'SeaMonkey'; }
		else if (browser.match('Gecko')) { return 'Mozilla'; }
		else if (browser.match('Netscape')) { return 'Netscape'; }
		else if (browser.match('Opera')) { return 'Opera'; }
		else if (browser.match('Safari')) { return 'Safari'; }
		else if (browser.match('MSIE')) return 'Explorer';
	}	
}