// declaration
var brVert = parseInt(navigator.appVersion);
var brName = navigator.appName;
var b1stTime = (readCookie("times") == null);

// events setup
//window.onerror = errortrap;

function errortrap(msg, url, line)
{
	var fileName = url.substr(url.lastIndexOf("/")+1);
	window.status = "Error has occure: " + msg + " in file "+ fileName +" on line " + line;
	return true;
}

function ReturnFalse()
{
	return false;
}

function GoToForum(nForum)
{
	if (nForum == null || nForum <= 0)
		nForum = 1;

	window.open("/forums/default.aspx?fid="+nForum, "_Forums", "toolbar=no,status=no,resizable=yes,top=0,width=700,height=640");
}

function GoToForumMsg(nMsgId, nForum)
{
	if (nForum == null || nForum <= 0)
		nForum = 1;

	window.open("/forums/default.aspx?mid="+ nMsgId +"&fid="+nForum, "_Forums", "toolbar=no,status=no,resizable=yes,top=0,width=700,height=640");
}

function GoToOutboundLink(stURL)
{
	var nLeft = window.screen.availWidth-150;
	window.open("/site/docs/returntous.htm", "_New", "titlebar=no,toolbar=no,status=no,resizable=no,scrollbar=no,width=140,height=50,top=0,left="+nLeft)
	window.location.href = stURL;
}

function LaunchChat()
{
	var win = window.open("/shared/tools/chat/chat.aspx", "", "height=380, width=300, toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=0,resizable=1");
	win.focus();
}

function SignUpAndReturn()
{
	window.location.href = "/membership/join.aspx?ReturnUrl="+ window.location.href;
}

function SignInAndReturn()
{
	window.location.href = "/membership/signin.aspx?ReturnUrl="+ window.location.href;
}

function IsValidEmail(stEmail)
{
	var bIsValid = true;
	stEmail += "";
		
	if (stEmail.lenght < 5)
		bIsValid = false;
	else
	{
		if (stEmail.indexOf(" ") != -1)
			bIsValid = false;
		else
		{
			if (stEmail.indexOf("@", 1) < 2)
				bIsValid = false;
			else
			{
				if (stEmail.lastIndexOf(".") < stEmail.indexOf("@", 1) + 2)
					bIsValid = false;
			}
		}
	}

	return bIsValid;
}

////////////////////////////////////////////////////////////////
// COOKIES
function saveCookie(name, value, days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime( date.getTime()+(days*24*60*60*1000) );
		var expires = "; expires="+ date.toGMTString();
	}
	else 
		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 readSubCookie(name, subName) 
{
	var name = readCookie(name);
	if (name == null)
		return "";
		
	var index = name.indexOf(subName);
	if (index == -1)
		return "";
	
	index = index + subName.length + 1;
	var ei = name.indexOf(' ', index);
	return (ei == -1) ? name.substr(index) : name.substring(index, ei);
}

function deleteCookie(name) 
{
	saveCookie(name,"",-1)
}

/////UTILITY//////////////////////////////////
var isIE4 = ((brName == "Microsoft Internet Explorer") && (brVert >= 4));
var isNBReady = false;

function isNTBReady()
{
	return (isIE4 && isNBReady);
}

var oTitle = document.getElementById("phTitle");
if (oTitle != null)
{
	var sTitle = document.title;
	var nIndex = sTitle.indexOf("WinInsider");
	if (nIndex == -1)
		document.title = "WinInsider | "+ sTitle;
	else
		sTitle = sTitle.substr(nIndex+13);
		
	if (sTitle.length > 28)
		sTitle = sTitle.substr(0, 28) +"...";
		
	oTitle.innerText = sTitle;
}
