/*
 * $Id: global.js,v 1.8 2010-04-14 11:48:18 acollins Exp $
 * Copyright (c) 2004 Orbis Technology Ltd. All rights reserved.
 */



// trim a string
function str_trim(src, delim) {

	var r = "";
	var b = "";
	var skipsp = false;

	for(var i = 0; i < src.length; i++) {
		var c = src.charAt(i);
		if (delim.indexOf(c) >= 0) {
			b += c;
		}
		else {
			if (r.length > 0) {
				r += b;
			}
			b = "";
			r += c;
		}
	}

	return r;
}



// 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, center) {

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

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

	var left = (screen.availWidth - width) / 2;
	var top =  (screen.availHeight - height) / 2 - 40;
	
	var windowFeatures = "resizable=" + resizable +
		",scrollbars=" + scrollbars +
		",width=" + width +
		",height=" + height +
		",status=yes";

	if (center) {
		windowFeatures += ",left=" + left + ",top=" + top;
	}

	var new_window = window.open(url, name, windowFeatures);
	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.length) ? "; expires=" + expires.toGMTString() : "") +
		((path && path.length) ? "; path=" + path : "") +
		((domain && domain.length) ? "; domain=" + domain : "") +
		((secure && secure.length) ? "; 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.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){
		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;
}
function addEvent(obj, evType, fn){

  if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}

// update a 'span' value
function upd_span_value(span_id, value) {

	var elements = document.getElementsByTagName("span");

	for(var i = 0; i < elements.length; i++) {
		if(elements.item(i).id == span_id) {
			elements.item(i).style.display = value.length == 0 ? "none" : "";
			elements.item(i).innerHTML = value;
		}
	}
}

function showExtraMkts(e) {
	
	// Get event
	if(!e) e = window.event;
	
	// Find the tag which has been onmouseovered
	var theTarget = e.srcElement ? e.srcElement : e.target;
	
	// declare variables
	var div_id, live_coupon, lc_tag;
	
	// Handle Live Coupon markets
	if (theTarget.id.split("_")[0] == "lc") {
		live_coupon = true;
		div_id = theTarget.id.split("_")[2] + "_" + theTarget.id.split("_")[3];
		lc_tag = "lc_";
	} else {
		live_coupon = false;
		div_id = theTarget.id.split("_")[1] + "_" + theTarget.id.split("_")[2];
		lc_tag = "";
	}
	
	var info_holder = document.getElementById(lc_tag + "extra_mkts_info_" + div_id);
	var info_box = (info_holder.style) ? info_holder.style:info_holder;

	if(e){
		//odds_info_locked = false;
		//oinfo_box.width = "";

		if (e.pageX || e.pageY)
		{ // this doesn't work on IE6!! (works on FF,Moz,Opera7)
			//mousex = e.pageX - 160 + "px";
			mousey = e.pageY - 10 + "px";
		}
		else if (e.clientX || e.clientY)
		{ // works on IE6
			//mousex = e.clientX - e.offsetX + document.documentElement.scrollLeft - 160;
			mousey = e.clientY - e.offsetY + document.documentElement.scrollTop - 10;
		}
		
		//display box
		info_holder.style.display = "block";
		info_holder.style.top = mousey;
		//info_holder.style.left = mousex;
		
		// lock it
		document.getElementById(lc_tag + "lock_" + div_id).value = 2;
		hideExtraMkts(div_id, lc_tag);
	}
}

function hideExtraMkts(div_id, lc_tag) {
	hideExtraMktsAfter(div_id, lc_tag, 1);
}

function hideExtraMktsAfter(div_id, lc_tag, remainingTime) {
	if(document.getElementById(lc_tag + "lock_" + div_id).value == 0) {
		if(remainingTime == 0) {
			document.getElementById(lc_tag + "extra_mkts_info_" + div_id).style.display = "none";
		} else {
			setTimeout("hideExtraMktsAfter('" + div_id + "','" + lc_tag + "',0)",3000);
		}
	} else if (document.getElementById(lc_tag + "lock_" + div_id).value == 2) {
		setTimeout("hideExtraMktsAfter('" + div_id + "','" + lc_tag + "',0)",1000);
	} else {
		document.getElementById(lc_tag + "lock_" + div_id).value = 0;
		setTimeout("hideExtraMktsAfter('" + div_id + "','" + lc_tag + "',0)",1000);
	}
}

var month_days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

// check number of days in month
function chk_days_in_month(day, month, year) {

	if(month == 2 && year % 4 == 0 && day > 29) {
			return 29;
	}
	else if(day > month_days[month - 1]) {
			return month_days[month - 1];
	}

	return day;
}

//
// Set selection counter based on the number of legs in the cookie
//
function refresh_selection_counter(cookie_name) {

	var cookie = get_cookie(cookie_name);
	if (cookie == null || cookie == "") {
		set_selection_counter(0);
		return;
	}

	var selns = cookie.split("|");
	if (selns.length % 11 != 0) {
		return;
	}

	var num_legs = selns.length / 11;

	set_selection_counter(num_legs);
}

//
// Show a popup window.
//    link can be either a string or a link object
//
function show_popup(link, width, height) {

	if (! window.focus)
		return true;

	var href;
	if (typeof(link) == 'string')
		href = link;
	else
		href = link.href;

	window.open(href, 'Balkanbet', 'width=' + width + ',height=' + height + ',scrollbars=yes');
	return;
}


//
// Change the CSS class of an HTML element
//
function change_class(element, class_name) {

	// Check if an HTML DOM object has been passed or an id
	if (typeof(element) == 'string')
		element = document.getElementById(element);

	if (element != null)
		element.className = class_name;
}


// change class of price boxes
function prices_change(element, class_name) {

	// Check if an HTML DOM object has been passed or an id
	if (typeof(element) == 'string')
		element = document.getElementById(element);
	
	elems = element.childNodes;
		
	for(i = 0; i < elems.length; i++) {
		if (elems[i].id == 'price')
			elems[i].className = class_name;
	}
}


//array that checks if the image src for a particular image has been attempted before. 
var attempts= new Array()

// handler error produced loading an imagen  

function change_img_src(b, url) 
{
	if(attempts[b.src] == undefined)
	{		
		attempts[b.src]=1;	
		b.src = url;
		return true;	
	}
	else 
	{
		return true;
	}
}
