function $(name) { return document.getElementById(name); };

if (window.addEventListener) {
	window.addEventListener("load", __init, false);
} else {
	window.attachEvent("onload", __init);
}

var Days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var Months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];	

if (document.location.href.match(/\/es\//i)) {
	Days = ["domingo","lunes","martes","miércoles","jueves","viernes","sábado"];
	Months = ["ene","feb","mar","abr","may","jun","jul","ago","sep","oct","nov","dic"];	
} else if (document.location.href.match(/\/fr\//i)) {
	Days = ["dimanche","lundi","mardi","mercredi","jeudi","vendredi","samedi"];
	Months = ["janv.","févr.","mars","avr.","mai","juin","juil.","août","sept.","oct.","nov.","déc."];	
}

function addLeadingZero(value) {
    if (value < 10) {
        return '0' + value;
    } else {
        return value;
    }
}
function getAMPM(value) {
    return (value >= 12) ? "PM" : "AM";
}
function cutHoursToAMPM(value) {
    return (value == 12 || value == 0) ? 12 : value % 12;
}

function updateDateSpan() {
    var dateSpan = $('dateContainer');
    if (dateSpan) {
        var date = new Date();

        dateSpan.innerHTML = Days[date.getUTCDay()] + ", " +
                             Months[date.getUTCMonth()] + " " +
                             addLeadingZero(date.getUTCDate()) + ", " +
                             addLeadingZero(date.getUTCFullYear()) + " - " +
                             cutHoursToAMPM(date.getUTCHours()) + ":" +
                             addLeadingZero(date.getUTCMinutes()) + " " + getAMPM(date.getUTCHours()) + " GMT";
    }
    setTimeout("updateDateSpan();", 1000);
}

function __init() {
	updateDateSpan();
}

function launchHelp() { 
	var url = "http://faq.mansion.com/faq.html"; 
	var helpWin = window.open(url,'FAQ','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=yes,width=614,height=500,top=30,left=50'); 
	helpWin.focus(true); 
} 

function launchAffiliates() { 
	var url = "https://affiliates.mansion.com"; 
	var newWindow = window.open(url, 'Promoter', 'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=650,height=450'); 
	if(newWindow != null) newWindow.focus(true); 
} 

function launchPopup(url) {
    var popupWin = window.open(url, 'popup', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=yes,width=614,height=500,top=30,left=50');
    popupWin.focus(true);
}

function launchChat(url) {
    var chatWin = window.open(url, 'chatWindow', 'width=490,height=400,resizable=0,scrollbars=no,menubar=no,status=no');
    chatWin.focus(true);
}

function openForgotLogin() {
   var forgotWin  = window.open("http://www.mansionpoker.com/forgot-login/", "forgot", "width=750, height=550");
   forgotWin.focus();
}

function clearAllFields() {
    if ($('user').value == 'Sign In') {
        $('user').value = '';
    }
    if ($('password').value == "password") {
        $('password').value = '';
    }
}

function openForgot() {
	$('forgotPassword').style.display = '';
}

function closeForgot() {
	$('forgotPassword').style.display = 'none';
}

var emerpcomp = null;
var counter = 0;
var userGuid = "";

function EMERPCallback(comp, eventName) {
	emerpcomp = comp;
	userGuid = comp.GetUserID();
	if (typeof(document.EMERPAdditionalCallback) != "undefined") document.EMERPAdditionalCallback(comp, eventName);
}

function isAffiliate() {
	return true;
	return document.cookie.match(/banner_domainclick=[\d\D]+/i) ? true : false;
}

function getDownloadLink() {
	var downloadUrl = (isAffiliate()) ? "http://banner.mansionpoker.com/cgi-bin/SetupPoker.exe?creferer=UserGUID:"+userGuid : "/download/SetupPoker.exe";
	return downloadUrl;
}

function doDownload() {
/*		var w = null;
		try {
			w = window.open(getDownloadLink(), "popDown", "width=1,height=1,top=0,left=0");
		} catch (e) {
			w = null;
		}
		if (w == null) {
*/
			var downloadframe = document.getElementById("download_iframe");
			if (downloadframe == null) {
				downloadframe = document.createElement("iframe");
				downloadframe.width = "1px";
				downloadframe.height = "1px";
				downloadframe.position = "absolute";
				downloadframe.left = "-100px";
				document.body.appendChild(downloadframe);
			}
			downloadframe.src = getDownloadLink();
			if (document.location.href.match(/\/download\.html/i)) return;
			window.setTimeout(function() { document.location.href = "http://www.mansionpoker.com/download.html"; }, 4000); 
/*
		} else {
			if (document.location.href.match(/\/download\.html/i)) return;
			document.location.href = "/download.html";
		}
*/
}

function startDownload(src) {
	try {
		var source = (s.pageName != "" ? s.pageName : document.location.href.replace(/^.*\//, ""));
		if (typeof(src) != "undefined") source += ":"+src;

		var s1=s_gi(s_account); 
		s1.linkTrackVars='eVar3,events';
		s1.linkTrackEvents='event4';
		s1.eVar3 = source;
		s1.events='event4';
		s1.tl(this, 'd', source);
	} catch (e) {
	}
	if (emerpcomp != null) emerpcomp.SendEvent("", "download", "pd");
	doDownload();
}

function openForgot() {
	$('forgotPassword').style.display = '';
}

function closeForgot() {
	$('forgotPassword').style.display = 'none';
}

function getReferrer() {
	return extractSearch(document.referrer);
	function extractSearch(url) {
		if (!url) return;
		var sites = [
			/^http:\/\/[^\/]*?\b(google)(?:\.[^\.]+)\/.+?[\?&]q=([^&#]*)/i,
			/^http:\/\/[^\/]*?\b(google\.com)\/.+?[\?&]q=([^&#]*)/i,
			/^http:\/\/[^\/]*?\b(google\.co\.uk)\/.+?[\?&]q=([^&#]*)/i,
			/^http:\/\/[^\/]*?\b(google\.ca)\/.+?[\?&]q=([^&#]*)/i,
			/^http:\/\/[^\/]*?\b(yahoo)\.co(?:m|\.[^\.]+)\/.+?[\?&]p=([^&#]*)/i,
			/^http:\/\/search\.(yahoo\.com)\/.+?[\?&]p=([^&#]*)/i,
			/^http:\/\/(uk\.search\.yahoo\.com)\/.+?[\?&]p=([^&#]*)/i,
			/^http:\/\/(ca\.search\.yahoo\.com)\/.+?[\?&]p=([^&#]*)/i,
			/^http:\/\/search\.(live\.com)\/.+?[\?&]q=([^&#]*)/i,
		""];
		for (var m, i = sites.length-2; i >= 0; --i)  {
			if (m = sites[i].exec(url)) {
				return {site:m[1], keyword:decodeURIComponent(m[2])};
			}
		}
	}
}

var iframese = null;
var q = getReferrer();

if (!document.location.search.match(/(?:\?|\&)SID=[0-9]+/)) {
	if (q) {
		iframese = document.createElement("iframe");
		iframese.width = 1;
		iframese.height = 1;
		iframese.src = "/EMERPECp/se.php?keyword="+q.keyword+"&site="+q.site+"&r="+Math.random()*100000;
		callse();
	}
}

function callse() {
	if (document.getElementById("emerpec") == null) {
		window.setTimeout(callse, 100);
		return;
	} else {
		document.getElementById("emerpec").parentNode.appendChild(iframese);
	}
}

function findPosition(obj) {
	var pos = {x:0, y:0}
	try {
		while (obj.nodeName.toLowerCase() != "body") {
			pos.x += parseInt(obj.offsetLeft, 10);
			pos.y += parseInt(obj.offsetTop, 10);
			obj = obj.offsetParent;
		}
	} catch (e) {
	}
	return pos;
}

function findPosX(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;
}

function findPosY(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;
}

function formatNumber(value) {
	value = value.split("").reverse().join("");
	value = value.replace(/([0-9]{3})(?=[0-9])/g, "$1,");
	return value.split("").reverse().join("");
}