// functions.js
// This is a library of commonly used functions for the K12@USC
// website. 
// Copyright (C) 2008 University of Southern California
// Author: Jean-Philippe Steinmetz
//
// Any unauthorized reproduction, distribution or use of the
// source code provided in this file is strictly prohibited.

// GLOBAL VARS //
var MouseX;
var MouseY;

// CONSTANTS
var STANDARD_BROWSER = 0; // Standard browser type
var NETSCAPE_BROWSER = 1; // Netscape browser type
var IE_BROWSER = 2; // Internet Explorer browser type
var UNKNOWN_BROWSER = 3; // Unknown browser type

// EVENT HANDLERS //
document.onmousemove = function(evt) {
	var browserType = getBrowserType();
	
	if(browserType == IE_BROWSER)
	{
		MouseX = event.clientX + document.documentElement.scrollLeft;
		MouseY = event.clientY + document.documentElement.scrollTop;
	} else {
		MouseX = evt.pageX;
		MouseY = evt.pageY;
	}
};

// FUNCTIONS //

/**
 * Checks and returns the browser type
 * @return The int of the browser type
 */
function getBrowserType() {
	if(document.all) return IE_BROWSER;
	else if(document.layers) return NETSCAPE_BROWSER;
	else if(document.getElementById) return STANDARD_BROWSER;
	else return UNKNOWN_BROWSER;
}

/**
 * Grabs the reference to the element id
 * This is used to ensure compatibility with multiple browsers
 * @return The reference to the element id
 */
function getElement(id) {
	var elem;
	var browserType = getBrowserType();
	
	if(browserType == STANDARD_BROWSER)	elem = document.getElementById(id); // Standard
	else if(browserType == IE_BROWSER) elem = document.all[id]; // IE6 and previous
	else if(broswerType == NETSCAPE_BROWSER) elem = document.layers[id]; // Netscape
	
	return elem;
}

/**
 * Opens a new browser window for the AW/WO application
 * @param myURL The URL of the html file to load in the new window
 */
function openSysWin(myURL) {
    var wx = screen.width / 2 - 400;
    var wy = screen.height / 2 - 300;
    var attributes = "scrollbars=no,resizable=no,width=800,height=600,left=" + wx + ",top=" + wy;
    var newwin = window.open(myURL,'K12_at_USC',attributes);
    newwin.focus();
}

/**
 * Opens a new browser window to display screenshots
 * @param win The URL of the file to load in the new window
 */
function openSSWin(win) {
   window.open(win,'demo','width=820,height=700,top=0,left=0,status=no,toolbar=no,menubar=no,location=no');
   return false;
}

/**
 * Displays a pop-over window with contents message
 * @param message The HTML message to display
 */
function showPopOver(message)
{
	var elem = getElement('popOverDiv');
	
	// If the elem doesn't exist lets add it to the existing document
	if(elem == null)
	{
		document.body.innerHTML += "<div id='popOverDiv'><div id='popOverDivMessage'></div><br/>"
									+ "<div align='right'><a href='javascript:hidePopOver()'>Close</a></div></div>";
		elem = getElement('popOverDiv');
	}
	
	getElement('popOverDivMessage').innerHTML = message;
	
	if(MouseX != null && MouseY != null)
	{
		elem.style.left = MouseX+'px';
		elem.style.top = MouseY+'px';
		
		// If the new window doesn't fit inside the width of
		// the window, we need to reposition it so that it does.
		var winWidth;
		if(getBrowserType() == IE_BROWSER)
			winWidth = document.body.clientWidth;
		else
			winWidth = window.innerWidth;
		
		if( (MouseX+150) > winWidth )
			elem.style.left = (MouseX-150)+'px';
	} else { // Default position as a failsafe
		elem.style.left = '100px';
		elem.style.top = '100px';
	}
}

/**
 * Hides the current popup window
 */
function hidePopOver()
{
	var elem = getElement('popOverDiv');
	var elem2 = getElement('popOverDivMessage');
	elem.style.left = '-4000px';
	elem2.innerHTML = '';
}

/**
 * Checks a string to verifiy if it is a properly formatted e-mail address
 * @param address The e-mail address to verify
 */
function verifyEmail(address) {
	regExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
	return regExp.test(address);
}

/**
 * Checks a string to verify if it is a properly formatted phone number
 * This allows for country codes and extensions to be used.
 *
 * @param number The phone number to verify
 */
function verifyPhoneNumber(number) {
	regExp = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/
	return regExp.test(number);
}

// The following functions are used for rollovers, provided by Dreamweaver
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 MM_preloadImages() { //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=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=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];}
}
// End Dreamweaver provided functions

