/**
	cookie manipulation functions
*/
function getCookie(name)
{
	var dc = document.cookie;
	//alert (dc);
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} 
	else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	var returnCookie = unescape(dc.substring(begin + prefix.length, end));
	if((returnCookie == null) || (returnCookie == 'null'))
	{
		returnCookie = "";
	}

	return returnCookie;
	
}

function setCookie(name, value, days) {
	var expire = new Date();
	expire.setTime(expire.getTime() + (24*60*60*1000)* days);
	document.cookie = name + "=" + escape(value) + "; expires = " + expire.toGMTString();
}

function deleteCookie(name) {
	var expire = new Date();
	expire.setTime(expire.getTime() - (24*60*60*1000));
	document.cookie = name + "=; expires=" + expire.toGMTString();
}

function show(obj)
{
	if(obj)
		obj.style.display = 'inline';
}
function hide(obj)
{
	if(obj)
		obj.style.display = 'none';
}

function to3Digits(num) {  // round to 3 decimal places (used for svg floating point data)
	return Math.round(num*1000)/1000;
}

function formatBucks(inVal, doRound) {  // add thousands-commas  i.e:  3,104,005.3
	var val = new Number(inVal)/1000;
	if(doRound) val = Math.round(val);
	var rtn = "";
	if(isNaN(val)) return val;
	if(val == 0) return val;
	var tmp = to3Digits(val) + "";             // this rounds as well
	var numD = tmp.indexOf(".");               // find the 1000's
	//if(numD >= 0) rtn = tmp.substring(numD);  // the decimal point and everything to the right
	var sign = "";
	if(val < 0) {
		sign= "-";
		val = -val;
	}
	tmpI = parseInt(Math.floor(val));
	while (tmpI != 0) {
		partI = tmpI % 1000;
		if(tmpI > 999) {
			zeroPad = "";
			if(partI < 10) zeroPad = "0";
			if(partI < 100) zeroPad = zeroPad + "0";
			partI = zeroPad + partI;
			rtn = "," + partI + rtn;
		} else {
			rtn = partI + rtn;
		}
		tmpI = Math.floor(tmpI/1000)
	}
	rtn = sign + rtn;
	return rtn + "K"
}

function urlOpt(name, value) {
	return name + "=" + escape(value);
}

function getUrlOpts(url) {
	var tmp = new Array();
	var opts
	indx = url.indexOf("?");
	opts = url.substring(indx+1,url.length).split("&");
	for (var i=0; i< opts.length; i++) {
		vals=opts[i].split("=");
		tmp[vals[0]] = vals[1];
	}
	return tmp
}

function getBrokerDetails(url)
{
    var urlParts = url.split("/");
    var curDomain =  urlParts[2];
    return (brokersObj[curDomain]);
}

function num2moneyWrapper(num)
{
	if(num=="0" || num=="0.0" || num=="0.00" || num == "00000") 
		return "0";
	else 
		return num2money(num);
}

function num2money(n_value,prefix,elementId,dec,retval)
{
	
	dec = (dec)?dec:false;
	retval = (retval)?true:false;
	oNval = n_value.toString();
	if(typeof(n_value) == "string")
	{
		n_value = money2num(n_value);
	}
	if(n_value=="")
	{
		if(elementId!=null)
		{
			if($(elementId).type == "text")
			{
				if(retval)
				{
					$(elementId).value = "";
				}
				else
				{
					$(elementId).value = prefix+"0";
				}
			}
		else
			$(elementId).innerHTML = prefix+"0";
		}
		return;
	}
	var pre = (!prefix)?"$":prefix;
	
	if (isNaN(Number(n_value)))
	return 'ERROR';

	var b_negative = Boolean(n_value < 0);
	n_value = Math.abs(n_value);
	
	// round to 1/100 precision, add ending zeroes if needed
	if(dec && dec>2)
	{
		dec = parseInt(oNval.substr(oNval.indexOf('.')).length-1);
		divd = parseInt(eval('1e'+dec));
		var roundPt = (Math.round(n_value*divd)%divd>9)?(Math.round(n_value*divd)%divd):('0'+Math.round(n_value*divd)%divd);
	}
	else
		var roundPt = (Math.round(n_value*1e2)%1e2>9)?(Math.round(n_value*1e2)%1e2):('0'+Math.round(n_value*1e2)%1e2);
	var s_result = String(roundPt + '00').substring(0,dec);
	// separate all orders
	var b_first = true;
	var s_subresult;
	while (n_value >= 1) 
	{
		s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value%1e3);
		s_result = s_subresult.slice(-3) + (b_first ? '.' : ',') + s_result;
		b_first = false;
		n_value = n_value/1e3;
	}
	
	// add at least one integer digit
	if (b_first)
	s_result = '0.' + s_result;

	// apply formatting and return
	if(!dec)
	{
		s_result = s_result.substring(0,s_result.indexOf("."));
	}
	if(elementId!=null)
	{
		if($(elementId).type == "text")
			$(elementId).value = b_negative ? '-'+pre + s_result + '' : pre + s_result;
		else
			$(elementId).innerHTML = b_negative ? '-'+pre + s_result + '' : pre + s_result;
	}
	
	return b_negative
	? '-'+pre + s_result + ''
	: pre + s_result;
}
