// number of different background images to randomly select from
var numImages = 8;

function moveNav () {
	var classStr = this.className;
	var posClass = new RegExp(" up[0-9]");

	if (!classStr.match(posClass)) {
		this.className += " up1";
	}
	else {
		this.className = classStr.replace(posClass, "");
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(|\\\\s)"+searchClass+"(\\\\s|)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function init() {
	// assign handlers
	var countryDivs = getElementsByClass("country", document, "div");
	if (countryDivs.length) {
		for (var i = 0; i < countryDivs.length; i++) {
			countryDivs[i].onmouseover = moveNav;
			countryDivs[i].onmouseout = moveNav;
		}

		var wrapperObj = document.getElementById("wrapper");
		var sitetoolsObj = document.getElementById("sitetools");		

		// get random class for background images
		var randomNum = Math.floor(Math.random() * numImages) + 1;
		
		// make sure the random number isn't greater than the number of possibilities
		if (randomNum > numImages) {
			randomNum = 1;
		}
		wrapperObj.className = "photo" + randomNum.toString();
	}
	startList();
	findExLinks();
}


// JavaScript Document for IE subnav 

var origWidth = window.innerWidth;

function startList() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("topnav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
						this.className+=" over";
				}
				node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

function findExLinks() {
	var linkObjs = document.getElementsByTagName('a');
	for (var i = 0; i < linkObjs.length; i++) {
		link = linkObjs[i].getAttribute('rel');
		if (link == "external") {
			linkObjs[i].onclick = newWin;
		}
	}
}

function newWin () {
	// window features
	var status = "1";
	var toolbar = "1";
	var location = "1";
	var menubar = "1";
	var directories = "0";
	var resizable = "1";
	var scrollbars = "1";
	
	var features = "status=" + status + ",toolbar=" + toolbar +
					",location=" + location + ",menubar=" + menubar +
					",directories=" + directories + ",resizable=" + resizable +
					",scrollbars=" + scrollbars;
	
	var newWin = window.open(this, "newWin", features);
	newWin.focus();	
	return false;
}

// validation for MISTI > Students > Apply form

function checkApplyForm (formObj) {
	var mitID = document.getElementById('required-mitid').value;
	var errorMsg = "";
	var mitIDpat = /^\d\d\d\d\d\d\d\d\d$/;

	if (!mitIDpat.exec(mitID)) {
		errorMsg += "MIT ID must be nine digits.";
	}
	if (errorMsg != "") {
		document.getElementById('required-mitid').focus();
		alert (errorMsg);
		return false;
	}
	else {
		return true;
	}
}

// set cgiemail to field based on drop-down value

function setEmailTo (selectObj) {
	var program = selectObj.options[selectObj.selectedIndex].value;
	var email = "gabilog@mit.edu";
	switch (program) {
		case "China":
			email = "china@mit.edu";
			break;
		case "CETI":
			email = "china@mit.edu";
			break;
		case "France":
			email = "mit-france@mit.edu";
			break;
		case "Germany":
			email = "mit-germany@mit.edu";
			break;
		case "India":
			email = "mit-india@mit.edu";
			break;
		case "Israel":
			email = "mit-israel@mit.edu";
			break;
		case "Italy":
			email = "mit-italy@mit.edu";
			break;
		case "Japan":
			email = "mit-japan@mit.edu";
			break;
		case "Mexico":
			email = "mit-mexico@mit.edu";
			break;
		case "Spain":
			email = "mit-spain@mit.edu";
			break;
		case "Seed Funds":
			email = "eschenck@mit.edu";
			break;
	}
	selectObj.form.emailTo.value = email;	
}


