var g_windowWidth = 800;
var g_windowHeight = 600;

/********************************************************************************/
function initPage()
{
	getWindowSize();
	getBrowserName();	
	getBrowserVersion();
}
/********************************************************************************/
function getWindowSize()
{
	if (self.innerWidth)
	{
		g_windowWidth = self.innerWidth;
		g_windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		g_windowWidth = document.documentElement.clientWidth;
		g_windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		g_windowWidth = document.body.clientWidth;
		g_windowHeight = document.body.clientHeight;
	}
}
/********************************************************************************/
function getBrowserVersion()
{
	// Test for MSIE x.x;
	g_isIE7orLater = false;
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{ 
		// capture x.x portion and store as a number
		var ieversion=new Number(RegExp.$1) 
		if (ieversion>=7)
			g_isIE7orLater = true;
	}
}
/********************************************************************************/
function getBrowserName()
{
	var browserName=navigator.appName; 
	if (browserName=="Netscape")
	{ 
		g_browserType = "nt";
	}
	else 
	{ 
		if (browserName=="Microsoft Internet Explorer")
		{
			g_browserType = "ie";
		}
		else
		{
			g_browserType = "u";
		}
	}	
}
/********************************************************************************/
function validateFeedbackForm(form)
{
	if(form.FBMessage.value.length <= 0)
	{
		alert("The message itself is blank. Please fill in your comment and try again.");
		return false;	
	}
	return true;
}
/********************************************************************************/
function validateSubscribeForm(form)
{
	if(form.FBName.value.length <= 0)
	{
		alert("Please enter a name or a nickname so we know how to address you.");
		return false;	
	}
	
	if(form.FBEmail.value.length <= 0)
	{
		alert("Please enter an email address so we can contact you.");
		return false;	
	}
/*
	if(form.FBNewsCheck.checked == false && form.FBBetaCheck.checked == false)
	{
		alert("Please select at least one checkbox to subscribe.");
		return false;	
	}
*/	
	return true;
}
/********************************************************************************/
// User system code
/********************************************************************************/
isRegDown = false;
isForgotDown = false;
isQuickLoginDown = false;

gRollDur = 0.2

function toggleQuickLogin()
{
	if(isQuickLoginDown)
	{
		isQuickLoginDown = false;
		Effect.BlindUp('qk_login', { duration: gRollDur } ); 
		
		document.images['qk_login_img'].src = '../img/qk_login_tab.png';
	}
	else
	{
		isQuickLoginDown = true;
		Effect.BlindDown('qk_login', { duration: gRollDur } ); 
		
		document.images['qk_login_img'].src = '../img/qk_login_tab_hide.png';
	}
	return false;
}


function toggleReg()
{
	if(isForgotDown)
	{ toggleForgot(); }
	if(isRegDown)
	{
		isRegDown = false;
		Effect.BlindUp('reg_form', { duration: gRollDur } ); 
	}
	else
	{
		isRegDown = true;
		Effect.BlindDown('reg_form', { duration: gRollDur } ); 
	}
	return false;
}

function toggleForgot()
{
	if(isRegDown)
	{ toggleReg(); }

	if(isForgotDown)
	{
		isForgotDown = false;
		Effect.BlindUp('forget_form', { duration: gRollDur } ); return false;

	}
	else
	{
		isForgotDown = true;
		Effect.BlindDown('forget_form', { duration: gRollDur } ); return false;
	}

	return false;
}

function validateLoginForm(thisform)
{

	if(thisform.userid.value.length <= 0)
	{
		showMessage("Please enter a user name.", true);
		return false;
	}
	
	if(!isAlphaNum(thisform.userid.value))
	{
		showMessage("Only letters and numbers are allowed in the user name.", true);
		return false;
	}
	
	if(thisform.password.value.length <= 0)
	{
		showMessage("Please enter a valid password.", true);
		return false;
	}

	if(!isAlphaNum(thisform.password.value))
	{
		showMessage("Only letters and numbers are allowed in the password.", true);
		return false;
	}
	
	return true;
}

function validateForgotForm(thisform)
{
	if(thisform.email.value.length <= 0)
	{
		showMessage("Please enter a valid email.", true);
		return false;
	}
}

function validateRegisterForm(thisform)
{
	if(thisform.newid.value.length <= 2)
	{
		showMessage("Please enter a valid user name. It must be at least three characters long.", true);
		return false;
	}
	
	if(!isAlphaNum(thisform.newid.value))
	{
		showMessage("Only letters and numbers are allowed in the user name.", true);
		return false;
	}

	if(thisform.pwd1.value.length <= 5)
	{
		showMessage("Please enter a valid password. Passwords must be at least six characters long.", true);
		return false;
	}

	if(thisform.pwd2.value != thisform.pwd1.value)
	{
		showMessage("The passwords do not match.", true);
		return false;
	}
	
	if(!isAlphaNum(thisform.pwd1.value))
	{
		showMessage("Only letters and numbers are allowed in the password.", true);
		return false;
	}
	
	if(thisform.email.value.length <= 0 || thisform.email.value.indexOf("@") == -1)
	{
		showMessage("Please enter a valid email. We will not send you junk - this is mostly in case you forget your password.");
		return false;
	}
	
	if(thisform.email.value.indexOf("@mailinator") != -1)
	{
		showMessage("\
Please, no mailinator email addresses. We're not some huge soulless <br>\
corporation looking to get your personal information, sell it, and have <br>\
you spammed to death. The only reason we're asking for an email <br>\
address is in case you forget your password or in rare cases where <br>\
an important announcement regarding this account would need to be <br>\
made. So please, consider giving us a real email address.", false);
		return false;
	}

	return true;
}

function validatePassForm(thisform)
{
	if(thisform.pwd1.value.length <= 5)
	{
		showMessage("Please enter a valid password. Passwords must be at least six characters long.", true);
		return false;
	}

	if(thisform.pwd2.value != thisform.pwd1.value)
	{
		showMessage("The passwords do not match.", true);
		return false;
	}
	
	if(!isAlphaNum(thisform.pwd1.value))
	{
		showMessage("Only letters and numbers are allowed in the password.", true);
		return false;
	}
	
	return true;
}
/********************************************************************************/
/* Dialog code */
/********************************************************************************/
var gModalDialog = null;

function closeDialog()
{
	if(gModalDialog)
		gModalDialog.close();
	gModalDialog = null;
}

function showMessage(message, do_center)
{
	showMessageFull(message, do_center, "../img/ic_warning.gif");
}

function showMessageFull(message, do_center, icon_link)
{
	htmlCode = "<center><table border=\"0\"><tr><td>";
	if(icon_link != "")
		htmlCode += "<img src=\""+icon_link+"\" style=\"padding:10px;\">";
	htmlCode +="</td><td>";
	if(do_center)
		{ htmlCode += "<center>"+message + "</center>"; }
	else
		{ htmlCode += message; }
	htmlCode += "</td></tr></table>";
	htmlCode += "<br><a href=\"javascript:closeDialog();\"><div class=\"formInputSubmitButtonSmallDiv\">Ok</div></a>";
	htmlCode += "</center>";
	gModalDialog = new Control.Modal(htmlCode, { className: 'modal', overlayOpacity: 0.5, fade: true });
    gModalDialog.open();
}

function showImage(image_path, image_w, image_h)
{
	getWindowSize();
	
	maxOverlapPerc = 0.2;
	
	maxW = g_windowWidth*(1.0 - maxOverlapPerc);
	maxH = g_windowHeight*(1.0 - maxOverlapPerc);
	
	if(image_w > maxW || image_h > maxH)
	{
		fScaleW = maxW/image_w;
		fScaleH = maxH/image_h;
		
		if(fScaleW > 1.0)
		{ fScaleW = 1.0; }
		
		if(fScaleH > 1.0)
		{ fScaleH = 1.0; }
		
		if(fScaleW < fScaleH)
		{
			image_w = fScaleW*image_w;
			image_h = fScaleW*image_h;
		}
		else
		{
			image_w = fScaleH*image_w;
			image_h = fScaleH*image_h;
		}
	}

	htmlCode = "<center><a href=\"javascript:closeDialog();\"><img width=\""+image_w+"\" height=\""+image_h+"\" src=\"";
	htmlCode += image_path;
	htmlCode += "\"></a></center>";
	gModalDialog = new Control.Modal(htmlCode, { className: 'modal_img', overlayOpacity: 0.5, fade: true });
    gModalDialog.open();
}

function showOkCancelDialog(ok_link, ok_text, cancel_text)
{
	htmlCode = "<table border=\"0\"><tr><td><img src=\"../img/ic_warning.gif\" style=\"padding:10px;\"></td><td>";
 
	htmlCode += "<center>Are you sure you want to delete this level?</center>";
	htmlCode += "</td></tr></table><br>\n<table width=\"100%\" border=\"0\"><tr><td width=\"50%\">";
	htmlCode += "<center><a href=\""+ok_link+"\">";
	htmlCode += "<div class=\"formInputSubmitButtonSmallDiv\">"+ok_text+"</div></a></center></td>\n<td>";
	htmlCode += "<center><a href=\"javascript:closeDialog();\"><div class=\"formInputSubmitButtonSmallDiv\">"+cancel_text+"</div></a></center>";
	htmlCode += "</td></tr></table>";

    gModalDialog = new Control.Modal(htmlCode, { className: 'modal', overlayOpacity: 0.5, fade: true });
    gModalDialog.open();
}

function showUploadTerms()
{
	showMessageFull("\
By uploading any files to our servers you agree to give us non-exclusive, <br>\
non-revokable rights to the content, including, but not limited to, modifing, <br>\
distributing, publishing and selling it. In plain English, this means you <br>\
give us the right to do pretty much anythign with the file, without giving up <br>\
any of your rights to do the same, and without being able to us later that <br>\
you've changed your mind and want to revoke the rights. It's a pretty standard <br>\
and reasonable agreement, we think.", false, "");
}

/********************************************************************************/
function validateUploadForm(thisform)
{
	if(thisform.uploaded.value.length <= 0)
	{
		showMessage("Please select a file to upload.", true);
		return false;
	}
	
	if(!thisform.agree.checked)
	{
		showMessage("Please agree to the terms and conditions before uploading.", true);
		return false;
	}
	
	return true;
}
/********************************************************************************/
function fileExt(filename)
{
	var dot = filename.lastIndexOf(".");
	if( dot == -1 ) return "";
	var extension = filename.substr(dot + 1,filename.length);
	return extension;
}
/********************************************************************************/
function validateUploadUserpic(thisform)
{
	if(thisform.uploaded.value.length <= 0)
	{
		showMessage("Please select a file to upload.", true);
		return false;
	}
	
	var file_ext = fileExt(thisform.uploaded.value);
	if(file_ext != "jpg" && file_ext != "JPG")
	{
		showMessage("The file must a JPG file.", true);
		return false;
	}
	
	return true;
}
/********************************************************************************/
function validateChangePassword(thisform)
{
	passresult = validatePassForm(thisform);
	if(!passresult)
		return false;
		
	if(thisform.currpass.value.length <= 5)
	{
		showMessage("Please enter a valid current password.", true);
		return false;
	}
	
	if(!isAlphaNum(thisform.currpass.value))
	{
		showMessage("Only letters and numbers are allowed in the password.", true);
		return false;
	}
		
	return true;
}
/********************************************************************************/
function isAlphaNum(alphane)
{
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
	{
		var alphaa = numaric.charAt(j);
		var hh = alphaa.charCodeAt(0);
		if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
		{
		}
		else	
		{
			 return false;
		}
	}
	return true;
}
/********************************************************************************/
