// Global javascript functions.

function goMobilePopup(url) {
    // Pop up a new browser window to display an external site.
    if(url != "") {
        ext_win = window.open(url, 'ext_popup', 'width=680,height=650,resizable=yes,scrollbars=no,toolbar=no,status=no,top=50,left=50');
        ext_win.focus();
    }
}

function goBackgammon(url) {
    // Pop up a new browser window to display an external site.
    if(url != "") {
		var w = 755;
		var h = 550;
        var ext_win = window.open(url, 'bg_popup', 'width='+w+',height='+h+',resizable=yes,scrollbars=yes,toolbar=no,status=no,top=50,left=50');
        ext_win.focus();
    }
}

function open_window(url) {
	// Pop up an info. window.
	info_win = window.open(url, 'info_window', 'width=440,height=460,resizable=no,scrollbars=yes,toolbar=no,status=no');
	info_win.focus();
}

function fullScreen () {
	window.moveTo(0,0);
	window.resizeTo(screen.width,screen.height);
}

function goLogin() {
	// Attempt to log user in.
	frm = document.forms['ukb_login'];

	//val = location.href;
	val = location.href.replace(/DoLogout/, "GoHome");

	if(frm.curloc.value == "")
		frm.curloc.value = val;

	// submit form - this should be via SSL
	frm.submit();
}

function login_on_enter(e) {

	if (e && e.which) {
		if (e.which == 13) {
			goLogin()
		}
	} else {
		if (e && e.keyCode == 13) {
			goLogin()
		}
	}
	return true
}

function goLogout(url) {
	//logoutwin = window.open(url, 'logoutwin','width=150,height=150,resizeable=no,scrollbars=no,toolbar=no,status=no');
	//logoutwin.focus();
	location.href = url;
}

function goBalance(url) {
	// Pop up users current balance.
	bal_win = window.open(url,'bal_window','width=150,height=150,resizable=no,scrollbars=no,status=no,toolbar=no,location=no');
	bal_win.focus();
}

function goStatsPopup(url) {
	stats_win =  window.open(url,'UKBetting','Height=475,Width=800,scrollbars=yes,status=no,left=25,top=25');
	stats_win.focus();
}


function goFastBet() {
	// Handle a fast bet select box link
	frm = document.forms['fmFastBet'];

	url = frm.fastbet.options[frm.fastbet.selectedIndex].value;

	if(url)
		location.href = url;
}

function goOrderBy() {
	// Handle the order by drop-down
	frm = document.forms['fmFastBet'];
	url = frm.order_by_list.options[frm.order_by_list.selectedIndex].value;
	if(url)
		location.href = url;
}

function checkUsername(uname) {
	// Check given username is a valid one.
	regex = "^[A-Za-z0-9\._-]*$";

	if(uname.length < 6)
		return 2;

	if(uname.match(new RegExp(regex)))
		return 0;
	else
		return 1;
}

function checkPassword(passwd1, passwd2) {
	// Check password is valid and is verified
	regex = "^[A-Za-z0-9\._-]*$";

	if(passwd1 != passwd2)
		return 3;

	if(passwd1.length < 6)
		return 2;

	if(passwd1.match(new RegExp(regex)))
		return 0;
	else
		return 1;
}

function checkEmail(email) {
	// Check email address looks like a valid one.
	regex = "^[^@ ]+@[^@ ]+\\.[^@ ]+$";

	if(email.match(new RegExp(regex)))
		return 0;
	else
		return 1;
}

function update_expiry_and_start(start_date, exp_date) {
	// Splits dates into component month and year parts - assumes MM/YY format
	frm = document.forms['fm_acct_txn'];

	if (start_date.length == 5) {
		frm.start_month.value = start_date.substring(0,2);
		frm.start_year.value  = start_date.substring(3,5);
	} else {
		//frm.start_month.value = "MM";
		//frm.start_year.value  = "YY";
	}

	if (exp_date.length == 5) {
		frm.expiry_month.value = exp_date.substring(0,2);
		frm.expiry_year.value  = exp_date.substring(3,5);
	} else {
		//frm.expiry_month.value = "MM";
		//frm.expiry_year.value  = "YY";
    }
}

function goAcctQry(f) {
	frm = document.forms['fmAcctQry'];

	switch(f) {
		case 0:
			frm.show.value = "BA";
			break;

		case 1:
			frm.show.value = "TX";
			break;

		default:
			frm.show.value = "XG";
			break;
	}

	frm.submit();
}

function goDeposit() {
	// Validate and submit deposit form
	frm = document.forms['fm_acct_txn'];

	//ccode = frm.tbCountryCode.options[frm.tbCountryCode.selectedIndex].value;
	//frm.country_code.value = ccode;

	//if(ccode == "US" || ccode == "UM" || ccode == "VI")
	//	alert("Betting is illegal in your country.");
	//else
	frm.submit();
}

function goWithdrawal(betting_illegal_msg) {
	// Validate and submit withdrawal form
	frm = document.forms['fm_acct_txn'];

	//ccode = frm.tbCountryCode.options[frm.tbCountryCode.selectedIndex].value;
	//frm.country_code.value = ccode;

	//if(ccode == "US" || ccode == "UM" || ccode == "VI")
		//alert(betting_illegal_msg);
	//else
	frm.submit();
}

function goAcctBetRcpt(url) {
	betrcpt = window.open(url, 'betrcpt', 'width=380,height=450,resizable=no,scrollbars=yes,toolbar=no,status=no');
	betrcpt.focus();
}

function goBetslip(url) {
	// Popup window for betslip.
	betslip = window.open(url, 'betslip', 'width=380,height=450,resizable=no,scrollbars=yes,toolbar=no,status=no');
	betslip.focus();
}

function goCoupon() {
	// Popup betslip
	betslip = window.open('', 'betslip', 'width=380,height=450,resizable=no,scrollbars=yes,toolbar=no,status=no');
	betslip.focus();

	// Get coupon details
	frm = document.forms['fmCoupon'];
	frm.target = "betslip";
	frm.submit();
}

function goFCTCBet(url, mkt_id, err_msg_missing_selns) {
	// Popup window for betslip forecast/tricast bet.
	frm = document.forms['fmForecast'];

	// Generate the correct full url.
	fullurl = url + "?action=GoFCTCBet";
	numcast = Number(eval("frm.numcast_" + mkt_id + ".value"));
	numsel = Number(eval("frm.numsel_" + mkt_id + ".value"));

	var id1 = -1;
	var id2 = -1;
	var id3 = -1;

	for(f=0;f<numsel;f++) {
		if(eval("frm.c_" + mkt_id + "_cast1[f]") && eval("frm.c_" + mkt_id + "_cast1[f].checked")) {
			id1 = eval("frm.c_" + mkt_id + "_cast1[f].value");
			fullurl += "&ev_oc_id_1=" + id1;
		}
	}

	for(f=0;f<numsel;f++) {
		if(eval("frm.c_" + mkt_id + "_cast2[f]") && eval("frm.c_" + mkt_id + "_cast2[f].checked")) {
			id2 = eval("frm.c_" + mkt_id + "_cast2[f].value");
			fullurl += "&ev_oc_id_2=" + id2;
		}
	}

	if(eval("frm.forecast_" + mkt_id) && eval("frm.forecast_" + mkt_id + ".checked"))
		fullurl += "&forecast=Y";

	if(eval("frm.fcrev_" + mkt_id) && eval("frm.fcrev_" + mkt_id + ".checked"))
		fullurl += "&fcrev=Y";

	var have_tc = false;

	// If we got tricasts available then add extra bits to query string.
	if(numcast == 3) {
		for(f=0;f<numsel;f++) {
			if(eval("frm.c_" + mkt_id + "_cast3[f].checked")) {
				id3 = eval("frm.c_" + mkt_id + "_cast3[f].value");
				fullurl += "&ev_oc_id_3=" + id3;
			}
		}

		if(eval("frm.tricast_" + mkt_id + ".checked")) {
			fullurl += "&tricast=Y";
			have_tc = true;
		}

		if(eval("frm.tcrev_" + mkt_id + ".checked"))
			fullurl += "&tcrev=Y";
	}

	if (id1==-1 || id2==-1 || (have_tc && id3==-1)) {
		alert(err_msg_missing_selns);
	} else {
		// Generate betslip window, set it to display the betslip and focus it.
		betslip = window.open('', 'betslip', 'width=380,height=450,resizable=no,scrollbars=yes,toolbar=no,status=no');
		betslip.location.href = fullurl;
		betslip.focus();
	}
}

function FCTCChange(idx, mkt_id) {
	// Validate Forcast/Tricast form so we only allow legal bets.
	frm = document.forms['fmForecast'];

	numcast = Number(eval("frm.numcast_" + mkt_id + ".value"));
	numsel = Number(eval("frm.numsel_" + mkt_id + ".value"));

	val1 = -1;
	val2 = -2;
	val3 = -3;
	for(f=0;f<numsel;f++) {
		if(eval("frm.c_" + mkt_id + "_cast1[f]") && eval("frm.c_" + mkt_id + "_cast1[f].checked"))
			val1 = f;

		if(eval("frm.c_" + mkt_id + "_cast2[f]") && eval("frm.c_" + mkt_id + "_cast2[f].checked"))
			val2 = f;

		if(numcast == 3) {
			if(eval("frm.c_" + mkt_id + "_cast3[f]") && eval("frm.c_" + mkt_id + "_cast3[f].checked"))
				val3 = f;
		}
	}

	if(idx == 1) {
		if(val1 == val2 || val1 == val3) {
			eval("frm.c_" + mkt_id + "_cast2[val1].checked = false");
			if(numcast == 3)
				eval("frm.c_" + mkt_id + "_cast3[val1].checked = false");
		}
	} else if(idx == 2) {
		if(val1 == val2 || val2 == val3) {
			eval("frm.c_" + mkt_id + "_cast1[val2].checked = false");
			if(numcast == 3)
				eval("frm.c_" + mkt_id + "_cast3[val2].checked = false");
		}
	} else if(idx == 3) {
		if(val3 == val1 || val2 == val3) {
			eval("frm.c_" + mkt_id + "_cast1[val3].checked = false");
			eval("frm.c_" + mkt_id + "_cast2[val3].checked = false");
		}
	}
}

function checkMoney(str) {
	money = new RegExp("^([0-9]*)(\.([0-9]([0-9])?))?$");

	if(str.search(money) == -1)
		return false;

	return true;
}

// Function to display a prompt or similar once a week
function wprompt(url) {
	if(document.cookie.indexOf("wprompt") == -1) {
		time = new Date();
		prompt_win = window.open(url, 'prompt_win', 'width=800,height=500,resizable=yes,scrollbars=yes,toolbar=no,status=no');
		if(prompt_win)
			prompt_win.blur();

		exp = new Date();
		nextweek = exp.getTime() + (7*24*60*60*1000);
		exp.setTime(nextweek);

		document.cookie = "wprompt=yes; expires=" + exp.toGMTString();
	}

	self.focus();
}

function langNav() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

function openerFocus() {
	var win = window.opener;
	win.focus();
	return;
}

function setCookie(cookieName, cookieValue) {
	document.cookie = cookieName + "=" + cookieValue;
}

function getCookie(whichCookie) {

	var allCookies = document.cookie;

	var cookieString = whichCookie + "=";

	var pos = allCookies.indexOf(cookieString);

	if (pos != -1) {
		var start = pos + (cookieString.length);
		var end = allCookies.indexOf(";",start);
		if (end == -1) end = allCookies.length;
		var val = allCookies.substring(start,end);
		return val;
	}

	return 0
}
