/************************************************************
Autism Speaks.org

GLOBAL CLIENT-SIDE SCRIPTS MODULE

This module contains all functions and variables used throughout
the website.
*************************************************************/

/************************************************************
GLOBAL VARIABLES
*************************************************************/

var bName = navigator.appName;				// browser name
var bPlatform = navigator.platform;			// platform
var bVer = parseInt(navigator.appVersion);	// version
var browser = "";							// browser name shorthand
var doc = "";								// for dHTML document object model reference
var sty = "";								// for dHTML style attribute reference
var ns6mod = "";							// to work method into object model reference generation

var initFunctions = new Array();			// array of functions to execute on init
var documentReady = false;					// whether or not the document has loaded and is ready for rollovers etc

var NNResize_initWidth = 0;					// window width & height used to handle NN resize bug				
var NNResize_initHeight = 0;

/************************************************************
ON-COMPILE EXECUTABLES
*************************************************************/

// set up dhtml references depending on browser
if (document.all) {
	// MSIE 4+
	browser="IE";
	doc = "document.all.";
	sty = ".style";
	if (bPlatform=="MacPPC") {
		bVerStart = navigator.appVersion.indexOf("MSIE")+5;
		bVer = parseInt(navigator.appVersion.substr(bVerStart,5));
	} 
} else if (document.layers) {
	// Netscape Navigator 4+
	browser="NN";
	doc = "document.";
} else if (document.getElementById) {
	// Netscape 6
	browser="NS6";
	doc = "document.getElementById('";
	ns6mod = "')";
	sty = ".style";
	bVerStart = navigator.userAgent.lastIndexOf("/")+1;		
	bVer = parseInt(navigator.userAgent.substr(bVerStart,navigator.userAgent.length));
}

/************************************************************
FUNCTIONS
*************************************************************/
/************************************************************
initialization
*************************************************************/

function init() {
	// generic init function, called onload of all documents
	// specific functions to execute on init should be added to the init queue via requestInit() below

	// execute functions from init queue
	for (var i=0;i<initFunctions.length;i++) {
		eval(initFunctions[i]);
	}

	// set ready status
	documentReady = true;
	
	// handle NS4.x resize bug
	if ((browser=="NN")&&(bVer==4)) {
		// store current window width & height
		NNResize_initWidth = window.innerWidth;
		NNResize_initHeight = window.innerHeight;
		window.captureEvents(Event.RESIZE);
		window.onresize = NNResize_fix();
	}
}

function requestInit(fn) {
	// adds a function to the list of functions to execute on init
	initFunctions[initFunctions.length] = fn;
}

/************************************************************
rollovers
*************************************************************/

function getImgName(imgRef) {
	// returns a name based on the image reference given
	var str = (imgRef.indexOf("/")==-1)?imgRef.substring(0,imgRef.lastIndexOf(".")):imgRef.substring((imgRef.lastIndexOf("/")+1),imgRef.lastIndexOf("."));
	str = str.replace(/-/gi,"_"); // replace dashes
	return str;
}

function loadImages() {
	// loads N images passed as a parameter, gives each a unique name based on src
	// to be referred later by "changeImages"
	var tempImgName="";
	if (document.images) {
		for (i=0;i<loadImages.arguments.length; i++) {
			tempImgName = getImgName(loadImages.arguments[i]);
			eval(tempImgName + "= new Image()");
			eval(tempImgName + ".src='" + loadImages.arguments[i] + "'");
		}
	}
}

function changeImages(imgObjName,newImgRef,layerName) {
	// pass an empty string for layerName if the image isn't within a layer
	if ((documentReady)&&(document.images)) {
		var newImgObj = eval(getImgName(newImgRef));
		var obj=((document.layers)&&(layerName!=""))?eval("document."+layerName+".document."+imgObjName):eval("document."+imgObjName);
		obj.src=newImgObj.src;
	}
}

/************************************************************
new windows
*************************************************************/
function getWindowName(url) {
	// returns a valid window name argument based on a given url
	var str="";
	
	// remove any non-letters from url to make name
	for (var i=0;i<url.length;i++) {
		if (((url.charCodeAt(i)>=65)&&(url.charCodeAt(i)<=90))||((url.charCodeAt(i)>=97)&&(url.charCodeAt(i)<=122))) {
			str += url.charAt(i);
		}
	}
	
	return str;
}

function getPlacementStr(w,h) {
	var left = Math.round((screen.availWidth/2)-(w/2));
	var top = Math.round((screen.availHeight/2)-(h/2));
	return (browser=="NN")?"screenX="+left+",screenY="+top:"left="+left+",top="+top;
}

function openBasicWindow(name,url,w,h,scrollbars,toolbar,statusbar,resize) {
	// opens a window with no extras, centered in screen, with unique name
	var placementStr = getPlacementStr(w,h);
	var x,y;
	
	// resize & scrollbar should be yes/no
	
	// set toolbar
	var tools = (toolbar=="tools")?"yes":"no";
	
	// set statusbar
	if (statusbar=="NSstatus") { 
		var status= ((bName=="Netscape")&&(bPlatform="MacPPC"))?"yes":"no";
	} else {
		var status = (statusbar=="status")?"yes":"no";
	}
	
	eval(name + "Window = window.open(url,'" + name + "', 'toolbar=" + tools + ",location=no,directories=no,status=" + status + ",scrollbars=" + scrollbars + ",resizable=" + resize + ",copyhistory=no," + placementStr + ",width=" + w + ",height=" + h + "')");                     

	// make sure window comes to the front
	eval(name + "Window.focus()");
	eval("setTimeout('"+name+"Window.focus()',50)");
	
	// move window to center after the fact (ie mac problem)
	if((bName=="Microsoft Internet Explorer")&&(bPlatform=="MacPPC")) {
		x = Math.round((screen.availWidth/2)-(w/2));
		y = Math.round((screen.availHeight/2)-(h/2));
		eval(name + "Window.moveTo(x,y)"); 
	}
}

// external site window
function newWin(url) {
	// new window for external url: uses 'mainFocuser' to gaurantee focus of new window
	var windowName = getWindowName(url);

	url = "/util/mainFocuser.jsp?goto="+escape(url);
	mainWindow = window.open(url,windowName,"toolbar=yes,location=yes,directories=yes,status=yes,scrollbars=yes,resizable=yes,copyhistory=yes," + getPlacementStr(760,500) + ",width=760,height=500");           
	
	if((bName=="Microsoft Internet Explorer")&&(bPlatform=="MacPPC")) {
		var x = Math.round((screen.availWidth/2)-380);
		var y = Math.round((screen.availHeight/2)-250);
		mainWindow.moveTo(x,y);
	}
}

// standard window
function standardWin(name,url) {
	openBasicWindow(name,url,520,380,"yes","no","no","yes");
}

/************************************************************
global functions unique to Autism Speaks.org
*************************************************************/
function addToMyLibrary() {
	// get title and url of this page, then send to "new item" form in My Library
	var title = document.title;
	var url = document.location;
	document.location.href="/myaccount/item_edit.php?action=new&ref=local&title=" + escape(title) + "&link=" + escape(url);
}

/************************************************************
util
*************************************************************/

function NNResize_fix(e) {
	// check if window has actually changed size: if so, reload
	if (NNResize_initWidth != window.innerWidth || NNResize_initHeight != window.innerHeight) {
		document.location = document.location;
  	}
}

// get scrollbars to show IE5MacPPC (browser bug: won't show vert scroll if window sized beyond 760px).
// setting the size of the layer seems to "wake the browser up"
function MacScrollBars(){
	if ((bPlatform=="MacPPC")&&(browser=="IE")&&(bVer>=5)) {
		setSize("scrollbarsLayer",1,480);
	}
}

function redirect(url) {
	// redirects the browser to the given url
	document.location.href = url;
}

function removeHTML(str) {
	// removes any HTML tags from given str
	var tagStart = str.indexOf("<");
	var tagEnd = str.indexOf(">");
	var newStr = "";
	
	while ((tagStart!=-1)&&(tagEnd!=-1)) {
		newStr = str.substring(0,tagStart) + str.substring(tagEnd+1,str.length);
		str = newStr;
		tagStart = str.indexOf("<");
		tagEnd = str.indexOf(">");
	}
	return str;
}

function truncate(str,len) {
	// truncates the given string to the given length
	return (str.length>len)?str.substring(0,(len-3)) + "...":str;
}

function isNum(n) {
	// returns whether or not n is a number
	acceptableVals = "0123456789";
	return (acceptableVals.indexOf(n)!=-1);
}

function submitSubmit(stLayer, stForm) {
	if (browser=="NN") {
		eval("document." + stLayer + ".document." + stForm + ".submit()");
	} else {
		eval("document." + stForm + ".submit()");
	}
}