//-----------------------------------------------------------------------

//need to encode our own cookies to get around IE (all versions) 20 cookie limit

function cookie_set(cookie_name, value_name, new_value, expire_days)
{
	//sets new value in cookie
	var existing = cookie_get_all(cookie_name);
	var new_cookie = "";
	var i;
	var pos;
	var name;
	var value;
	var found_old = false;

	//create new cookie
	for (i = 0; i < existing.length; i++)
	{
		pos = existing[i].indexOf("=");
		if (pos == -1)
			continue;
		name = existing[i].substring(0, pos);
		value = existing[i].substring(pos+1, existing[i].length);
		if (name == value_name)
		{
			//replace old value
			new_cookie += value_name+"="+new_value+"|";
			found_old = true;
		}
		else
			//keep old value
			new_cookie += name+"="+value+"|";
	}
	if (!found_old)
		//add new value
		new_cookie += value_name+"="+new_value+"|";

	cookie_write(cookie_name, new_cookie, expire_days);
}

function cookie_get(cookie_name, value_name)
{
	//returns value in cookie
	var all = cookie_get_all(cookie_name);
	var i;
	var pos;
	var name;
	var value;
	for (i = 0; i < all.length; i++)
	{
		pos = all[i].indexOf("=");
		if (pos == -1)
			continue;
		name = all[i].substring(0, pos);
		value = all[i].substring(pos+1, all[i].length);
		if (name == value_name)
			return value;
	}
	return "";
}

function cookie_get_all(cookie_name)
{
	//returns array of value_name=value
	var ret = [];
	var cookie = cookie_read(cookie_name);
	var start = 0;
	var end = 0;
	var count = 0;
	var val;

	while (end != cookie.length)
	{
		end = cookie.indexOf("|", start);
		if (end == -1)
			end = cookie.length;
		val = cookie.substring(start, end);
		start = end+1;
		if (val.indexOf("=") != -1)
		{
			//save a good value
			ret[count] = val;
			count++;
		}

	}
	return ret;
}

function cookie_write(name, value, expire_days)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expire_days);
	var cookie = name + "=" + escape(value) +
	 ((expire_days == null) ? "" : ";expires=" + exdate.toGMTString()) +
	 ";path=/";
	document.cookie = cookie;
}

function cookie_read(name)
{
	var c_start;
	var c_end;
	if (document.cookie.length > 0)
	{
		c_start = document.cookie.indexOf(name + "=")
		if (c_start != -1)
		{ 
			c_start = c_start + name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1)
				c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		} 
	}
	return "";
}

//-----------------------------------------------------------------------

function getElementsByClassName(className, tag, elm)
{
	var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i = 0; i < length; i++)
	{
		current = elements[i];
		if(testClass.test(current.className))
		{
			returnElements.push(current);
		}
	}
	return returnElements;
}

//-----------------------------------------------------------------------

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			if (oldonload)
			{
				oldonload();
			}
			func();
		}
	}
}

//-----------------------------------------------------------------------

function ajax_req(url, callback)
{
	var req = init();
	req.onreadystatechange = process_request;

	function init()
	{
		var xml_http = null;
		try
		{
			// firefox, opera, safari
			xml_http = new XMLHttpRequest();
		}
		catch (e)
		{
			//internet explorer
			try
			{ xml_http = new ActiveXObject("Msxml2.XMLHTTP"); }
			catch (e)
			{ xml_http = new ActiveXObject("Microsoft.XMLHTTP"); }
		}
		return xml_http;
	}

	function process_request()
	{
		if ((req.readyState == 4) && (req.status == 200) && (callback))
			callback(req.responseText);
	}

	this.do_get = function()
	{
		req.open("GET", url, true);
		req.send(null);
	}

	this.do_post = function(body)
	{
		req.open("POST", url, true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(body);
	}
}

//-----------------------------------------------------------------------

function get_int(value)
{
	return parseInt(parseFloat(value));
}

function to_dollars(value)
{
	var total_price = Math.round(value*100)/100;
	var tpstr = total_price.toString();
	var di = tpstr.indexOf(".");
	if (di == -1)
		tpstr += ".00";
	else
		if (di == (tpstr.length-2))
			tpstr += "0";
	return '$'+tpstr;
}

//-----------------------------------------------------------------------

function get_browser()
{
	var ua, i;
	this.isIE    = false;
	this.isNS    = false;
	this.isOpera = false;
	this.isiPhone = false;
	this.isSafari = false;
	this.version = null;

	ua = navigator.userAgent;

	//iPhone/iPod
	if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
	{
		this.isiPhone = true;
		this.version = "1.00";
		return;
	}

	//Safari
	s = "Safari";
	if ((i = ua.indexOf(s)) >= 0)
	{
		this.isSafari = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

	//IE
	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0)
	{
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

	//NS
	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0)
	{
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

	// Treat any other "Gecko" browser as NS 6.1.
	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0)
	{
		this.isNS = true;
		this.version = 6.1;
		return;
	}

	//Opera
	s = "Opera/";
	if ((i = ua.indexOf(s)) >= 0)
	{
		this.isOpera = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
}

//-----------------------------------------------------------------------

function find_offset_from_parents(obj)
{
	var left;
	var top;
	var orig_obj = obj;

	left = 0;
	if(obj.offsetParent)
	{
		while (1) 
		{
			left += obj.offsetLeft;
			if (!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	}
	else
		if (obj.x)
			left += obj.x;

	obj = orig_obj;
	top = 0;
	if(obj.offsetParent)
	{
		while (1) 
		{
			top += obj.offsetTop;
			if (!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	}
	else
		if (obj.x)
			top += obj.x;

	return {left: left, top: top};
}

function find_offset(obj)
{
	var left, top;
	if (obj.getBoundingClientRect)
	{
		var pos = obj.getBoundingClientRect();
		//left = pos.left + Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
		//top = pos.top + Math.max(document.documentElement.scrollTop,  document.body.scrollTop);
		left = Math.round(pos.left);
		top = Math.round(pos.top);
	}
	else
	{
		if (obj.offsetLeft)
		{
			left = obj.offsetLeft;
			top = obj.offsetTop;
		}
		else
		{
			var ret = find_offset_from_parents(obj);
			left = ret.left;
			top = ret.top;
		}
	}

	return {left: left, top: top};
}
		
function find_pos_x(obj)
{
/*	var curleft = 0;
	if(obj.offsetParent)
		while (1) 
		{
			curleft += obj.offsetLeft;
			if (!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
		else
			if (obj.x)
				curleft += obj.x;
	return curleft; */

	var pos = find_offset(obj);
	return pos.left;
}

function find_pos_y(obj)
{
/*	var curtop = 0;
	if (obj.offsetParent)
		while (1)
		{
			curtop += obj.offsetTop;
			if (!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
		else
			if (obj.y)
				curtop += obj.y;
	return curtop; */

	var pos = find_offset(obj);
	return pos.top;
}

//-----------------------------------------------------------------------

function html_color(r,g,b)
{
	var sr = r.toString(16);
	if (sr.length <= 1)
		sr = "0" + sr;
	var sg = g.toString(16);
	if (sg.length <= 1)
		sg = "0" + sg;
	var sb = b.toString(16);
	if (sb.length <= 1)
		sb = "0" + sb;
	
	return "#"+sr+sg+sb;
}
