window.onload = function(){
	if (location.href == "http://www.askthecommish.com/forums/index.php" || location.href == "http://www.askthecommish.com/forums/"){
		//Get the <strong> tags -- that's what holds the "New messages (xxx)" text
		strongTagArray = document.getElementsByTagName("strong");
	
		for (i=0; i < strongTagArray.length; i++){
			var pattern = new RegExp("New messages");
	
			if (pattern.test(strongTagArray[i].innerHTML)){
				var messageTotalRegex = /\d+/g;
				messageTotal = messageTotalRegex.exec(strongTagArray[i].innerHTML);
				
				try{
					notificationMessage = "messages";
					itOrThemMessage = "them";
					
					if (parseInt(messageTotal) == 1){
						notificationMessage = "message";
						itOrThemMessage	= "it";
					}
					
					if (readCookie("readNewMessages") != 0){
						var notification = confirm("You have " + messageTotal + " new private " + notificationMessage + ".\n\nWould you like to read " + itOrThemMessage + " now?");
						
						if (notification){
							window.location = "http://www.askthecommish.com/forums/misc.php?section=pun_pm&pmpage=inbox";
						}
						else{
							createCookie("readNewMessages", "0", 1);
						}
					}
				}
				catch (err){
					//die silently
				}
			}
		}
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
