//
//==== JAVASCRIPT FOR DEPT WEBSITES =====
//


//loads on page load
function init() {
	//add mouse events to top level toolbar
	var Box = document.getElementById("Header_Toolbar");
	for(var i=0; i<Box.firstChild.childNodes.length; i++) {
		if(Box.firstChild.childNodes[i].firstChild.id == "H_"+Content) Box.firstChild.childNodes[i].className = "MenuButtonHA";
	}
	
	var SBox = document.getElementById("Header_SubToolbar");
	for(var i=0; i<SBox.firstChild.childNodes.length; i++) {
		if(SBox.firstChild.childNodes.length>1) {
			if(SBox.firstChild.childNodes[i].firstChild.id == "SH_"+Content) {
				SBox.firstChild.childNodes[i].className = "MenuButtonSHA";
			}
		}
	}
	
	HeaderImageGenerator();	//randomly swiches out the header image
	
	//create splash content
	if(Content=="home") {		
		//var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
		//if(hasRightVersion) {BuildFlashObject('splash','/pics/home/splash_'+SplashImageGenerator()+'.swf','878','284');} else {document.getElementById("Content_SplashBox").innerHTML = '<img src="/pics/home/splash_building.jpg" width="878" height="284" alt="A world of Opportunity awaits..." />'}
	}
	
	//hook for AJAX functionality
	XmlHttp = setXMLHttpObject();
	if(XmlHttp && document.getElementById("FacultyFilter")!=null) document.getElementById("FacultyFilter").style.display="block";
	//if(XmlHttp!=false && document.getElementById("FacultyFilter")!=null) document.getElementById("FacultyFilter").style.display="block";
}

//generates random header image
function HeaderImageGenerator() {
	headerName = new Array(
							 "header_bg1a",
							 "header_bg2a",
							 "header_bg3a",
							 "header_bg4a",
							 "header_bg5a",
							 "header_bg6a"
							 );
	
	//return headerName[Math.floor(Math.random() * headerName.length)];	
	var nH = headerName[Math.floor(Math.random() * headerName.length)];
	document.getElementById("Header_Dept").style.background = "url(/pics/_template/"+nH+".jpg)";
}

//generates random words on login
function SplashImageGenerator() {
	splashName = new Array(
							 "high-expectations",
							 "on-the-move",
							 "high-expectations",
							 "on-the-move"
							 );
	
	return splashName[Math.floor(Math.random() * splashName.length)];	
}

//builds flash object on page
function BuildFlashObject(who,what,width,height) {
	//build flash object
	document.getElementById("Content_SplashBox").innerHTML = ''+
		'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" id="'+who+'" align="middle">' +
		'<param name="allowScriptAccess" value="sameDomain" />'+
		'<param name="movie" value="'+what+'" />'+
		'<param name="loop" value="false" />'+
		'<param name="menu" value="false" />'+
		'<param name="quality" value="high" />'+
		'<param name="wmode" value="transparent" />'+
		'<embed src="'+what+'" loop="false" menu="false" quality="high" wmode="transparent" width="'+width+'" height="'+height+'" name="'+who+'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'+
		'</object>';
}

//create XMLHttpRequest Object
function setXMLHttpObject() { 
	//xho=false;
	if(window.XMLHttpRequest) return new XMLHttpRequest();
	if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
		try{ return new ActiveXObject("Msxml2.XMLHTTP.6.0") }catch(e){}
		try{ return new ActiveXObject("Msxml2.XMLHTTP.3.0") }catch(e){}
		try{ return new ActiveXObject("Msxml2.XMLHTTP") }catch(e){}
		try{ return new ActiveXObject("Microsoft.XMLHTTP") }catch(e){}
		throw new Error("This browser does not support XMLHttpRequest or XMLHTTP.")
	};
	//if(window.ActiveXObject) try {xho = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { xho = new ActiveXObject("Microsoft.XMLHTTP"); }
	//return xho;
}

//retrieve specified data from server
function RetrieveData(who,what,filter) {
	//alert(filter);
	var ie= new Date().getTime(); //because ie sucks!
	var parameters = "dept="+who+"&class="+what+"&filter="+filter+"&time="+ie;
	
	XmlHttp.open("GET", "/content/getdata.php?"+parameters, true);         
	XmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	XmlHttp.setRequestHeader("Content-length", parameters.length);
	XmlHttp.setRequestHeader("Connection", "close");
	XmlHttp.send(parameters);
	XmlHttp.onreadystatechange = function() {
		if(XmlHttp.readyState == 4 && XmlHttp.status == 200) {
			Data = XmlHttp.responseXML.getElementsByTagName("data")[0].firstChild.nodeValue;
			document.getElementById("FSFilteredResults").innerHTML=Data;
		}
	}
}

//switch out page based on quicklist selection
function ProgramSelect(who) {
	document.location="/programs/"+who;
}

previousLength=0;
currentField="";

//checks status on form field
function CheckFillStatus(who) {	
	if(currentField=="ksu-n1") if(document.getElementById(who).value.length==4 && previousLength==3) { document.getElementById("ksu-n2").focus(); previousLength=0; }
	if(currentField=="ksu-v1") if(document.getElementById(who).value.length==4 && previousLength==3) { document.getElementById("ksu-v2").focus(); previousLength=0; }
	if(currentField=="phone-n1") if(document.getElementById(who).value.length==3 && previousLength==2) { document.getElementById("phone-n2").focus(); previousLength=0; }
	if(currentField=="phone-n2") if(document.getElementById(who).value.length==3 && previousLength==2) { document.getElementById("phone-n3").focus(); previousLength=0; }
	
	previousLength=document.getElementById(who).value.length;
	currentField=who;
}

//validate value of specific fields
function ValidateField(who,what) {
	//this will be used to validate that the user has properly completed certain fields
	
	switch(who) {
		case 'email-v':
			if(what=="key") {
				if(document.getElementById("email").value.substring(0,document.getElementById("email-v").value.length)==document.getElementById("email-v").value) {
					document.getElementById("email-v").style.backgroundColor="";
				} else {
					document.getElementById("email-v").style.backgroundColor="rgb(255,153,153)";
				}
			} else if(what=="blur") {
				if(document.getElementById("email").value==document.getElementById("email-v").value) {
					document.getElementById("email-v").style.backgroundColor="";
				} else {
					document.getElementById("email-v").style.backgroundColor="rgb(255,153,153)";
				}
			}
		break;
		case 'ksu-v1': case 'ksu-v2':
			if(what=="key") {
				if(document.getElementById("ksu-n1").value.substring(0,document.getElementById("ksu-v1").value.length)==document.getElementById("ksu-v1").value) {
					document.getElementById("ksu-v1").style.backgroundColor="";
				} else {
					document.getElementById("ksu-v1").style.backgroundColor="rgb(255,153,153)";
				}
				if(document.getElementById("ksu-n2").value.substring(0,document.getElementById("ksu-v2").value.length)==document.getElementById("ksu-v2").value) {
					document.getElementById("ksu-v2").style.backgroundColor="";
				} else {
					document.getElementById("ksu-v2").style.backgroundColor="rgb(255,153,153)";
				}
			} else if(what=="blur") {
				if(document.getElementById("ksu-n1").value==document.getElementById("ksu-v1").value) {
					document.getElementById("ksu-v1").style.backgroundColor="";
				} else {
					document.getElementById("ksu-v1").style.backgroundColor="rgb(255,153,153)";
				}
				if(document.getElementById("ksu-n2").value==document.getElementById("ksu-v2").value) {
					document.getElementById("ksu-v2").style.backgroundColor="";
				} else {
					document.getElementById("ksu-v2").style.backgroundColor="rgb(255,153,153)";
				}
			}
		break;
	}
}

//validate override form
function ValidateForm() {
	validated='yes';
	ValidateField('email-v','blur');
	ValidateField('ksu-v1','blur');
	ValidateField('ksu-v2','blur');
	
	if(document.getElementById("name-f").value=="") validated='no';
	if(document.getElementById("name-l").value=="") validated='no';
	if(document.getElementById("email").value=="") validated='no';
	if(document.getElementById("ksu-n1").value=="") validated='no'; 
	if(document.getElementById("ksu-n2").value=="") validated='no';
	if(document.getElementById("status").selectedIndex  == 0) validated='no';
	if(document.getElementById("graduation-s").selectedIndex == 0) validated='no';
	if(document.getElementById("graduation-y").value=="" || document.getElementById("graduation-y").value=="0000") validated='no';
	if(document.getElementById("program").selectedIndex == 0) validated='no';
	if(document.getElementById("semester").selectedIndex  == 0) validated='no';	
	if(document.getElementById("course-n1").selectedIndex  == 0) validated='no';
	if(document.getElementById("course-n2").value=="" || document.getElementById("course-n2").value=="0000") validated='no';
	if(document.getElementById("section").value=="" || document.getElementById("section").value=="00") validated='no';
	if(document.getElementById("crn").value=="") validated='no';
	if(document.getElementById("issue").selectedIndex  == 0) validated='no';
	if(document.getElementById("reason").value=="") validated='no';
	
	if(validated=='no') { alert('Please complete all fields before submitting form.'); return false; }
}



//toggle
function ShowHideFAQ(who) {
	if(who.childNodes[2].className=='FAQHideAnswer') {
		who.childNodes[2].className='FAQShowAnswer';
		//who.childNodes[2].style.display='block';
	} else if(who.childNodes[2].className=='FAQShowAnswer') {
		who.childNodes[2].className='FAQHideAnswer';
		//who.childNodes[2].style.display='none';
	}
}




onload=init;