//--------------------------------------------------------------------------------------------------------------------------

var dartWindow = null;
function dartPopup(type, contentLink, width, height)
{
	var widthAnim = width;
	var heightAnim = height;
	
	width += 18; // 18px for scrollbar
	
	// Center the popup
	var fullWidth = width + 6 + 6; // values for IE with Windows 2000 without status and menu bar
	var fullHeight = height + 25 + 6; // values for IE with Windows 2000 without status and menu bar

	var left = (screen.availWidth - fullWidth) / 2;
	var top = (screen.availHeight - fullHeight) / 2;
	
	closePopup();

	var param = "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + ",resizable=1,scrollbars=1,status=0,menubar=0";

	// Type = 1: HTML popup
	if (type == 1)
	{
		dartWindow = window.open(contentLink, "Dartfish", param);
	}

	// Type = 2: Image popup
	if (type == 2)
	{
		dartWindow = window.open("about:blank", "Dartfish", param);

		var w = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
		w += "<html><head><title>Dartfish</title><style type='text/css'>body {margin: 0px;}</style></head><body>";
		w += "<img src='" + contentLink + "' border='0' alt=''></body></html>";
	
		writeInWindow(dartWindow, w);
	}	

	// Type = 3: Flash popup
	if (type == 3)
	{
		contentLink = "/open.jsp?file=" + contentLink + "&width=" + widthAnim + "&height=" + heightAnim;
		dartWindow = window.open(contentLink, "Dartfish", param);
	}	

	// Resize and recenter the popup
	if (window.outerWidth) // not available on IE
	{
		fullWidth = dartWindow.outerWidth;
		fullHeight = dartWindow.outerHeight;
	}

	if (fullWidth > screen.availWidth)
		fullWidth = screen.availWidth;
		
	if (fullHeight > screen.availHeight)
		fullHeight = screen.availHeight;
		
	left = (screen.availWidth - fullWidth) / 2;
	top = (screen.availHeight - fullHeight) / 2;
	
	dartWindow.resizeTo(fullWidth, fullHeight);
	dartWindow.moveTo(left, top);

	return;
}

//--------------------------------------------------------------------------------------------------------------------------

function closePopup()
{
	if(dartWindow != null && !dartWindow.closed)
		dartWindow.close();
}

//--------------------------------------------------------------------------------------------------------------------------

function writeInWindow(win, text)
{
	win.document.open('text/html');
	win.document.write(text);
	win.document.close();
}

//--------------------------------------------------------------------------------------------------------------------------

function goToPage(url)
{
	if (window.opener != null && !window.opener.closed)
	{
		window.opener.location = url;
		window.opener.focus();
		window.close();
	}
	else
		window.location = url;
}

//--------------------------------------------------------------------------------------------------------------------------

var newWindow = null;
function goToNewPage(url)
{
	if (window.opener != null && !window.opener.closed)
	{
		newWindow = window.opener.open(url, "_blank");
		newWindow.focus();
		window.close();
	}
	else
		window.location = url;
}

//--------------------------------------------------------------------------------------------------------------------------

var showWindow = null;
function showPage(url)
{
	if (window.opener != null && !window.opener.closed)
	{
		window.opener.location = url;
		window.opener.focus();
	}
	else
	{
		if(showWindow != null && !showWindow.closed)
			showWindow.location = url;
		else
			showWindow = window.open(url, "_blank");

		showWindow.focus();
	}
}

//--------------------------------------------------------------------------------------------------------------------------

function emailIsValid(email)
{
  var error = false;

  if (email != "")
  {
		if (email.indexOf("@") == -1)                           // no @
      error = true;
    else if (email.lastIndexOf("@") > email.indexOf("@"))   // more than 1 @
      error = true;
    else if (email.lastIndexOf(".") < email.indexOf("@"))   // no . after @
      error = true;
    else if (email.lastIndexOf(".") == (email.length - 1))  // . at the end
      error = true;
		else if (email.match(/[^\w\-@.]/))
      error = true;
  }
	
  return !error;
}

//--------------------------------------------------------------------------------------------------------------------------

function writeSessionCookie(cookieName, cookieValue)
{
	document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/";
	return true;
}

