
var toPreload = new Array();
var onLoadFunctions = new Array();
var activityWindows = new Object();

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MyMM_preloadImages( imgArray ) { //based on v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=imgArray; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=webRoot+a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function loaded() {
	/*
	MyMM_preloadImages( [ "i/images/home_over.jpg",
						"i/images/about_over.jpg",
						"i/images/brows_over.jpg",
						"i/images/faq_over.jpg",
						"i/images/login_over.jpg",
						"i/images/logout_over.jpg",
						"i/images/register_over.jpg" ] );
	*/
	MyMM_preloadImages( toPreload );
	for ( var n = 0 ; n < onLoadFunctions.length; n++ )
		onLoadFunctions[n]();
}

function viewActivity( id, sessionId, fullScreen, aWidth, aHeight ) {

	var wind = activityWindows[ id ];
	if ( wind != null && !wind.closed ) {
		wind.focus();
		return;
	}
	
	var w, h, t, l;
	var sw = screen.width;
	var sh = screen.height;
	var isFull = (fullScreen == true);
	if ( isFull ) {
		isFull = (aWidth > 10) && (aHeight > 10);
	}
	if ( isFull ) {
		w = sw;
		h = sh;
		t = 0;
		l = 0;
		var agent = navigator.userAgent.toLowerCase();
		if ( agent.indexOf("mac") != -1 )
			h -= 40;
		else {
			w -= 10;
			h -= 70;
		}
	}
	else {
		w = aWidth;
		h = aHeight;
		t = ((sh-h)/2);
		l = ((sw-w)/2);
	}
	wstatus = 'width=' + w + ',height=' + h + ',top=' + t + ',left=' + l + ',status=no,resizable=yes,scrollbars=no';
	activityWindows[ id ] = window.open( servletRoot+"ViewActivity/" + id + "/" + sessionId + "/" + "?start=" + id, "activity_" + id, wstatus );
}

function launchPlayerDemo(url) {
	wstatus = 'width=790,height=530,status=no,resizable=yes,scrollbars=no';
	activityWindow = window.open( url, 'demo', wstatus );
}

function runSystemTest() {
	var w = 700;
	var h = 500;
	var sw = screen.width;
	var sh = screen.height;
	wstatus = 'width=' + w + ',height=' + h + ',top=' + ((sh-h)/2) + ',left=' + ((sw-w)/2) + ',status=no,resizable=yes,scrollbars=no';
	window.open( servletRoot+"SystemTest", 'systest', wstatus );
}

function goAnnotate( id ) {
	wstatus = 'width=820,height=620,status=no,resizable=yes,scrollbars=no';
	window.open( servletRoot+"Annotate?start=" + id, 'Annotate', wstatus );
}

function getCookieValue( cookieName ) {
	var theCookie = "" + document.cookie;
	var ind = theCookie.indexOf( cookieName );
	if ( ind == -1 || cookieName == "" )
		return "";
	var ind1 = theCookie.indexOf( ';', ind );
	if ( ind1 == -1 )
		ind1 = theCookie.length;
	return unescape( theCookie.substring(ind + cookieName.length + 1, ind1) );
}

function testForCookies() {
	var v = "" + Math.floor( 1000 * Math.random() );
	document.cookie = "cooktest=" + v + ";PATH=/";
	return ( getCookieValue("cooktest") == v )
}

function makemailto( nm, pre, post, cls ) {
	if ( !nm )
		nm = "";
	document.write( "\<a href=\"javascript:mailto('" + nm + "','" + pre + "','" + post + "')\"" );
	if ( cls && cls.length > 0 )
		document.write( " class=\"" + cls + "\"" );
	document.write( "\>" + pre + "@" + post + "\</a\>" );
}

function mailto( nm, pre, post ) {
	var addr;
	if ( nm && nm.length > 0 )
		addr = nm + " <" + pre + "@" + post + ">";
	else
		addr = pre + "@" + post;
	document.location = "mailto:"+addr;
}

function validateLogin( inFormName ) {
	if ( !testForCookies() )
		alert( "This site requires you to have cookies enabled.\nPlease enable cookies, and try again." );
	else if ( document.forms[inFormName].user.value.length == 0 )
		alert( "Please enter your email address." );
	else if ( document.forms[inFormName].pwd.value.length == 0 )
		alert( "Please enter your password." );
	else
		return true;
	return false;
}

function setElementClass( inID, inClass ) {
	var elt = document.getElementById( inID );
	if ( elt )
		elt.className = inClass;
}

function getFlashMovieObject(movieName) {
	// Safari: Mac OS X
	if ( navigator.userAgent.indexOf("Safari") != -1 )
		return document[movieName];
	else {
		// Firefox, Netscape: mac, win
		if ( document.embeds[movieName] )
			return document.embeds[movieName];
		
		// MSIE: Win
		else		
			return document[movieName];
	}
}

function validateForm( inFormName, inFields ) {
	var form = document.forms[ inFormName ];
	var emailRE = /^((?:(?:(?:\w[\.\-\+]?)*)\w)+)\@((?:(?:(?:\w[\.\-\+]?){0,62})\w)+)\.(\w{2,6})$/;
	var dateRE = new RegExp("^([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})$");
	
	if ( !testForCookies() )
		alert( "This site requires you to have cookies enabled.\nPlease enable cookies, and try again." );
	else {
		var missing = false;
		var errors = "";
		var confirm = [];
		

		var validateField = function( fld ) {
			var validated = true;
			if ( form[fld.id] && form[fld.id].value.length > 0 ) {
				if ( fld.validate == "email" ) {
					if ( !form[fld.id].value.match(emailRE) ) {
						ok = false;
						validated = false;
						errors += "Your email address is invalid. ";
					}
				}
				else if ( fld.validate == "date" ) {
					var valid = false;
					var matches = dateRE.exec( form[fld.id].value );
					if ( matches ) {
						var date = new Date( matches[3], (matches[1] - 1), matches[2] );
						valid = ((date.getMonth() == (matches[1] - 1)) && (date.getDate() == matches[2]) && (date.getFullYear() == matches[3]));
					}
					if ( !valid ) {
						ok = false;
						validated = false;
						errors += fld.label + " is invalid. ";
					}
				}
			}
			else {
				ok = false;
				missing = true;
				validated = false;
			}
			return validated;
		}
		
		for ( var n = 0; n < inFields.length; n++ ) {
			var f = inFields[ n ];
			var ok = true;
			if ( f.either && f.or ) {
				var hase = true;
				for ( var e = 0; e < f.either.length; e++ ) {
					if ( form[f.either[e].id] && form[f.either[e].id].value.length == 0 )
						hase = false;
				}
				var haso = true;
				for ( var o = 0; o < f.or.length; o++ ) {
					if ( form[f.or[o].id] && form[f.or[o].id].value.length == 0 )
						haso = false;
				}
				var has = hase || haso;
				for ( var e = 0; e < f.either.length; e++ )
					setElementClass( f.either[e].id, has ? "verified" : "incorrect" );
				for ( var o = 0; o < f.or.length; o++ )
					setElementClass( f.or[o].id, has ? "verified" : "incorrect" );
				if ( !has ) {
					errors += f.msg;
					ok = false;
				}
				else {
					if ( hase ) {
						for ( var e = 0; e < f.either.length; e++ )
							setElementClass( f.either[e].id, validateField(f.either[e]) ? "verified" : "incorrect" );
					}
					if ( haso ) {
						for ( var o = 0; o < f.or.length; o++ )
							setElementClass( f.or[o].id, validateField(f.or[o]) ? "verified" : "incorrect" );
					}
				}
			}
			else {
				if ( f.reqd ) {
					validateField( f );
				}
				if ( f.confirm ) {
					confirm.push( {a:f.id, b:f.confirm, l:f.label} );
				}
				setElementClass( f.id, ok ? "verified" : "incorrect" );
			}
		}
		
		if ( missing )
			errors += "Some required fields are empty. ";
		
		for ( var n = 0; n < confirm.length; n++ ) {
			var c= confirm[ n ];
			if ( form[c.a] && form[c.b] && form[c.a].value.length > 0 && form[c.a].value != form[c.b].value ) {
				setElementClass( c.a, "incorrect" );
				setElementClass( c.b, "incorrect" );
				errors += ( "Your " + c.l + " and " + c.l + " confirmation don't match. " );
			}
		}
		if ( errors.length > 0 ) {
			errors += "Please correct this and try again.";
			alert( errors );
		}
		else
			return true;
			
	}
	return false;
}

function prepareSubmit( inFormName ) {
	prepareCancel( inFormName );
	var elt = document.getElementById( "edit_wait" );
	if ( elt )
		elt.style.display = "inline";
}

function prepareCancel( inFormName ) {
	if ( document.forms[inFormName] ) {
		if ( document.forms[inFormName].cancel )
			document.forms[inFormName].cancel.disabled = true;
		if ( document.forms[inFormName].save )
			document.forms[inFormName].save.disabled = true;
	}
}

window.onload = loaded;

