/*
 * $Id: global.js,v 1.1.2.3.2.4 2011-03-08 17:21:24 pmiller Exp $
 * Copyright (c) 2004 Orbis Technology Ltd. All rights reserved.
 */



// trim a string
function str_trim(str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}



// round a float to 2 decimal places
function round_float(n) {

	var s = "" + Math.round(n * 100) / 100
	var i = s.indexOf('.')

	if(i < 0) {
		return s + ".00"
	}

	var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
	if(i + 2 == s.length) {
		t += "0"
	}

	return t;
}



// open a window
function open_window(url, name, width, height, resizable, scrollbars) {

	var w = window.screen.width;
	var h = window.screen.height;

	if(width > w) {
		width = w / 2;
	}
	if(height > h) {
		heigth = h / 2;
	}

	// spaces in window name cause problems for IE 6-7
	name = name.replace(/ /g,"_");

	var new_window = window.open(url, name,
		"resizable=" + resizable +
		",scrollbars=" + scrollbars +
		",width=" + width +
		",height=" + height +
		",status=yes");
	new_window.focus();
	new_window.opener = window;
}



// get a cookie
function get_cookie(name) {

	var dc = document.cookie;
	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;
	}

	return unescape(dc.substring(begin + prefix.length, end));
}



// set a cookie
function set_cookie(name, value, expires, path, domain, secure) {

	var curCookie = name + "=" + value +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");

	document.cookie = curCookie;
}



// get user-agent
var at = new UserAgent();
function UserAgent() {

	var b = navigator.appName.toUpperCase();

	if(b == "NETSCAPE") {
		this.b = "ns";
	}
	else if(b == "MICROSOFT INTERNET EXPLORER") {
		this.b="ie";
	}
	else if(b == "OPERA") {
		this.b = "op";
	}
	else {
		this.b = b;
	}

	this.version = navigator.appVersion;
	this.v = parseInt(this.version);

	this.ns = (this.b=="ns" && this.v>=4);
	this.ns4 = (this.b=="ns" && this.v==4);
	this.ns5 = (this.b=="ns" && this.v==5);

	this.ie = (this.b=="ie" && this.v>=4);
	this.ie4 = (this.version.indexOf('MSIE 4')>0);
	this.ie5 = (this.version.indexOf('MSIE 5')>0);
	this.ie55 = (this.version.indexOf('MSIE 5.5')>0);
	this.ie6 = (this.version.indexOf('MSIE 6')>0);
	this.ie9 = (this.version.indexOf('MSIE 9')>0);

	this.op = (this.b=="op");
	this.op4 = (this.b=="op" && this.v==4);
	this.op5 = (this.b=="op" && this.v==5);
}



// Add a form variable
function insertInputObj(form, type, id, name, value) {

	var doc = form.ownerDocument;

	if(at.ie && !at.ie9){
		inputObj = doc.createElement("<input name='" + name + "'>");
	} else {
		inputObj      = doc.createElement("input");
		inputObj.name = name;
	}

	inputObj.type  = type;
	inputObj.id    = id;
	inputObj.value = value;

	form.appendChild(inputObj);
}



// Change the display characteristics of a document element
function alterElements(tag, id, visibility) {

	var elements     = document.getElementsByTagName(tag);
	var num_elements = elements.length;

	for(var i = 0; i < num_elements; i++) {
		if(elements.item(i).id == id) {
			elements.item(i).style.display = visibility;
		}
	}
}



// get a currency symbol
function get_ccy_symbol (code) {

	switch(code) {
		case "GBP": { return "\u00A3"; }
		case "EUR": { return "\u20AC"; }
		case "USD": { return "$"; }
		default:    { return code + " "; }
	}
}



// fraction to decimal conversion
function frac_to_dec(num, den) {

	var p = num + "/" + den;

	if(p == "13/8") {
		return 2.62;
	}
	if(p == "15/8") {
		return 2.87;
	}
	if(p == "11/8") {
		return 2.37;
	}
	if(p == "8/13") {
		return 1.61;
	}
	if(p == "2/7") {
		return 1.28;
	}
	if(p == "1/8") {
		return 1.12;
	}

	return (parseFloat(num) / parseFloat(den)) + 1.00;
}

/*
 * Used to link a customer's name in a leaderboard to their community profile.
 */
function gotoProfile (link,acctno) {
	if (acctno=='') return false;
	window.location = link+acctno;
}

/*
 *
 */
function gotoPage (link) {
	window.location = link
}



/*
 * Updates the account balance displayed on the page.
 * Makes an ajax call with the action GoBalanceUpdate.
 * Callback is handled by balanceUpdateCallback.
 */
function updateAccountBalances() {
	// If we're not logged in, the account box won't be there, so no need to
	// update.
	if (document.getElementById("AccountBalances") == null) {
		return;
	}


    url = "?action=GoBalanceUpdate";

    var r = SA_request_broker;
    r.send( {
        'priority': r.UPDATE_BALS,
        'method'  : 'GET',
        'url'     : url,
        'callback': balanceUpdateCallback,
        'debug'   : true
    } );
}



/*
 * Handles the callback from the request made in updateAccountBalances().
 * Inserts the received HTML into the page (uses a DOM fragment) and re-adds
 * the relevant jQuery-based event handlers.
 */
function balanceUpdateCallback(response) {
    printfire(response);
    var fragment  = document.createDocumentFragment();
	var container = document.createElement('div');

	// Strip out any unwanted whitespace, so we only have the <table> as a childNode
    container.innerHTML = str_trim(response.responseText);
	var newTable = container.childNodes[0];

    var oldTable = document.getElementById("AccountBalances");
	var parentDiv= oldTable.parentNode;
	// Make sure that the new table is displayed if the old one was
	if ($("a.open", parentDiv).length > 0) {
		$("a", newTable).toggleClass('open');
		$("table", newTable).css("display", "inline-block");
		$("a",newTable).text('Hide Balances');
	}
    parentDiv.parentNode.replaceChild(newTable, parentDiv);
	$("a", newTable).click(function(e) {
			$(this).parent("div").find("table").toggle();
			$(this).text($(this).text() == 'Show Balances' ? 'Hide Balances' : 'Show Balances');
			$(this).toggleClass('open');
			}); 
}




