// JavaScript Document
var processComplete = false;
var beginProcess;

setActions = function()
{
	DoProcess();
	if (processComplete)
	{
		clearTimeout(beginProcess); 
	}
	else
	{
		beginProcess = window.setTimeout(setActions, 15000);
	}
	
}

function shouldbeCleared(fieldContents) {
	var acceptableValues = new Array("...", "Username", "Password", "Enter keyword")
	var found = false;

	for (clearTest in acceptableValues) {
		if (acceptableValues[clearTest] == fieldContents) found = acceptableValues[clearTest];
	}
	
	return found;
}

function DoProcess() {
	if (processComplete) return;
	if (document.forms) {
		// Iterate forms
		formsCollection = document.forms;
		var noclear = document.getElementById("research-interests");
		
		for(i=0; i<formsCollection.length; i++)
		{
			//alert("inForms"+formsCollection[i].name);
			// Iterate Element
			elementsCollection = formsCollection[i].elements;
			for(j=0; j<elementsCollection.length; j++)
			{
								
				node = elementsCollection[j];
				//alert(node.nodeName);
				if (node.nodeName == "INPUT")
				{
					if ((node.type == "text") || (node.type == "password"))
					node.onfocus = function() {
						if (this.value == "Password") this.type = "password";
						if (shouldbeCleared(this.value) != false) this.value = "";
						this.onfocus = function(){
							// Unset The onFocus event for this object
							return false;
						}
					}
				}
				else if (node.nodeName == "TEXTAREA" && node.readOnly == false && !noclear )
				//else if (node.nodeName == "TEXTAREA")
				{
					node.onfocus = function() {
						//alert(this.defaultValue);
						//this.defaultValue = "";
						this.value = "";
						this.onfocus = function(){
							// Unset The onFocus event for this object
							return false;
						}
					}
				}
			}
		}
	}
	processComplete = true;
}



function change(id, newClass) {
	identity=document.getElementById(id);	
	identity.className=newClass;
}

function changeall(thisid, totalelements){
	var a=1;
	while (a <= totalelements){
		tmpstring = thisid + '-f' + a + '-1';
		tmpstring2 = thisid + '-f' + a + '-0';
		
		change(tmpstring, 'hide');
		change(tmpstring2, '');
		a++;
	}
}

function changeallv2(thisid, totalelements, showthis){
	var a=1;
	while (a <= totalelements){
		if (a != showthis){
			tmpstring = thisid + '-pt' + a;
			change(tmpstring, 'hide');
		}
		a++;
	}
}

function changeone(thissmlid, thistopid, totalelements){
	var a=1;
	while (a <= totalelements){
		
		tmpstring = thistopid + '-f' + a + '-1';
		tmpstring2 = thistopid + '-f' + a + '-0';
		
		if(tmpstring != thissmlid){
			change(tmpstring2, '');
			change(tmpstring, 'hide');
		} else {
			change(tmpstring2, 'hide');
			change(tmpstring, 'shown-feature');
		}
		a++;
	}
}

function changeonev2(thissmlid, thistopid, totalelements){
	var a=1;
	while (a <= totalelements){
		
		tmpstring = thistopid + '-pt' + a;
		
		if(tmpstring != thissmlid){
			change(tmpstring, 'hide');
		} else {
			if (document.getElementById(tmpstring).className == ''){
				change(tmpstring, 'hide');
			} else {
				change(tmpstring, '');
			}
		}
		a++;
	}
}

function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
     anchor.getAttribute("rel") == "external") 
     anchor.target = "_blank"; 
 } 
} 

function closePpPops() {
	var i = 1;
	while (i < 50){
		myID = "pp-popup-" + i;
		identity=document.getElementById(myID);	
		if (identity && identity.className == "pp-popup"){
			identity.className = "hide";
		} 
		i ++;
	}
}

function closePpPops2(thisID) {
	
	id = "pp-pf-link-" + thisID;
	identity=document.getElementById(id);
	identity.href="javascript:donothing();";
	
	var i = 1;
	while (i < 50){
		myID = "pp-popup-" + i;
		identity=document.getElementById(myID);	
		if (identity && identity.className == "pp-popup"){
			identity.className = "hide";
		} 
		i ++;
	}
	
	var contentDiv = document.getElementById("content")
	var selectElements = contentDiv.getElementsByTagName("select")	
	for ( var i = 0 ; i < selectElements.length; i++) {
	selectElements[i].style.visibility = "visible";
	}
}


function fieldFocus(element) {
	// element.focus();
}

function donothing(){
	/* nothing goes here */
	x=1;
}

function hideSomething(something) {
	document.getElementById(something).className='hide';
}

function showSomething(something) {
	document.getElementById(something).className='';
}

function showAS() {
	open = document.getElementById("openAS");
	shut = document.getElementById("shutAS");

	open.style.display = "inline";
	shut.style.display = "none";
}

function hideAS() {
	open = document.getElementById("openAS");
	shut = document.getElementById("shutAS");

	open.style.display = "none";
	shut.style.display = "inline";
}

function ppPopFunction(popNumber) {
	id = "pp-popup-" + popNumber;
	idfm = "pp-popfrom-" + popNumber;
		
	identity=document.getElementById(id);
	identity.className = "pp-popup";
	
	identityfm=document.getElementById(idfm);
	
	identity.href="javascript:donothing();";
	identityfm.href="javascript:donothing();";

    winWidth = window.innerWidth;
    winHeight = window.innerHeight;

	divWidth = identity.offsetWidth;
	divHeight = identity.offsetHeight;
	
	leftPosn = Math.floor((winWidth - divWidth) / 2);
	topPosn = Math.floor((winHeight - divHeight) / 2);

	identity.style.top = mousey - 15 +"px";	
	identity.style.left = mousex - 50 +"px";	

	var i = 1;
	while (i < 50){
		myID = "pp-popup-" + i;
		identity=document.getElementById(myID);			
		if (identity && identity.className == "pp-popup" && i != popNumber){
			identity.className = "hide";
		} 
		i ++;
	}
	
	var contentDiv = document.getElementById("content")
	var selectElements = contentDiv.getElementsByTagName("select")	
	for ( var i = 0 ; i < selectElements.length; i++) {
	selectElements[i].style.visibility = "hidden";
	}
}

function initMouse() {
  document.onmousemove = update; // update(event) implied on NS, update(null) implied on IE
  update();
}

function update(e) {
  getMouseXY(e); // NS is passing (event), while IE is passing (null)
}

function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
 
  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
      //algor = '[e.pageX]';
      //if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
      //algor = '[e.clientX]';
      //if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }
  }
}

initMouse();
window.onload = externalLinks;
window.onload = setActions;
