/**********************************************
 LMCore.js
 Basic ActiveCampus JavaScript functions including
    imageswapper, popup, external site frame, and quicklinks
 Author:  M. Dempster 3//01
 Copyright:  LiquidMatrix Corp.
**********************************************/

/**********************************************
 Browser control variables  
 These variables determine which browser is being used
**********************************************/ 
var isNav = (document.layers) ? true:false				//Layers compliant browser (Netscape 4.x)
var isIE = (document.all) ? true:false				//IE4/5 compliant browser 
var isDOM = (document.getElementById) ? true:false	   	//DOM Compliant browser (NS6, Opera 5)
var isOther = (!isNav&&!isDOM&&!isIE) ? true:false				//Other browser (NS3, Lynx)

/**********************************************
 Global Variables  
 Variables used in ALL pages, or by multiple JS functions
**********************************************/ 
var preloadFlag = false 								//Determines if images for swapping in page are loaded

/**********************************************
 newImage(stringexp)    
 arg:  
 	use:  Required
	Datatype:  String
	A valid string expression (An image filename, with path)
 Return:  JS image object
 Description:  Creates an image object containing the file found at arg
**********************************************/ 
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

/**********************************************
 changeImages([stringexp, (obj2|str2)])    
 Return:  Nothing
 Description:   Replaces image found at reference stringexp with specified image obj2 or str2.  Image may
 				may be pulled from an Image Object (eg: Imageobj.src) or a string (eg: "/images/x.gif").
 Tips:  You may use any number of argument pairs to swap multiple images at the same time.  Arguments MUST be 
  		paired;  args not paired will result in an error.
**********************************************/ 
function changeImages() {
  	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

/**********************************************
 jumpto(formobj, stringexp)
 form:  
 	use:  Required
	Datatype:  JS Form Obj
	A valid reference to a form object on the page.
 name:  
 	use:  Required
	Datatype:  String
	A valid reference to a select control.    
 Return:  Nothing
 Description:   Takes the value of the selected item in selector name, and forwards browser to that value.  
 				During the forward process, it resets the selector to default (index=0)
 Tips:  Normally, form is passed this.form, which is a generic reference to the form enclosing name
**********************************************/ 
function jumpto(form, name) {
	var myindex= eval('form.'+ name + '.selectedIndex');			//Gets index of user selection in Stringexp
    var myval=eval('form.' + name + '.options[myindex].value');		//Gets value at myindex
    eval('form.'+ name + '.selectedIndex = 0');						//Resets selected index to 0 (default)
	if (myval != "") {
        
	     location.href=myval;									//If value is local, send this window to new page.
       
    }
}


/**********************************************
 popup(stringexp, stringexp, integer, integer, stringexp, stringexp, )
 Note:  All vars are type STring and are required
 URL:  Valid URL of a webpage (determines page to be shown in new window)
 Name: String (determines name of new window)
 wwidth: Integer (determines width of new window)
 wheight: Integer (determined height of new window)
 wresize: 0 or 1 (determines if window can be resized or not)
 wscrolls:  yes or no (determines if scrollbars will be created on window)
 Return:  Nothing
 Description:   Pops open new window with parameters specified above.
**********************************************/ 
function popup(url, name, wwidth, wheight, wresize, wscrolls){
    eval("window.open('" + url + "','" + name + "','toolbar=no,menubar=no,address=no,status=no,dependent=no,resizable=" + wresize + ",scrollbars=" + wscrolls + ",height=" + wheight + ",width=" + wwidth + "')");
}


/**********************************************
 external(stringexp)
 url:  
 	use:  Required
	Datatype: String
	A valid reference to a web site address   
 Return:  Nothing
 Description:   Takes the value of URL, and creates a new window containing the External site frame, which displays the address specified
**********************************************/ 
function external(url){
    eval("window.open('/external.asp?URL=" + escape(url) + "','ACEXT','toolbar=yes,menubar=yes,address=yes,status=yes,dependent=no,resizable=1,height=540,width=760')");
}


/**********************************************
 autojump(formobj,formobj,stringexp,integer)
 formname:    
 	use:  Required
	Datatype: JS Form Object
	A valid reference to a form  
 thisfield:    
 	use:  Required
	Datatype: JS Form Control object
	A valid reference to a form control contained within formname
 fieldname:    
 	use:  Required
	Datatype: String
	A valid reference to a name of form control within formname  
 maxlen:    
 	use:  Required
	Datatype: Integer    
 Return:  Nothing
 Description:   On keypress in thisfield, tests to see if the length of the data entered is longer than maxlen.
 				If so, switch focus to fieldname.
 Tips:  Normally, formname and thisfield are passed "this.form" and "this" respectively, referencing the currently selected control and form.
**********************************************/ 
function autojump(formname, thisfield, fieldname, maxlen) {
	maxlen = (isNav) ? maxlen - 1:maxlen						//If NS4.0, length needs to be truncated by 1
	if (thisfield.value.length >= maxlen) {						//On keypress, if value is longer than maxlen
		formname.elements[fieldname].focus();					//Focus on fieldname
	}
}

/*************************
TODO LIST FUINCTIONS
***************************/
function openList() {
	setElementVisibility('todoOpen', 'visible');
	if (navigator.appVersion.indexOf('Mac') != -1) {
		top.todoList.bodyheight();
	} 
}
function createReference(Element) {
	if (isDOM && !isIE) {
		oRef = eval("document.getElementById('" + Element + "').style")
	} else if (isNav) {
		oRef = eval("document.layers['" + Element + "']")
	} else {
		oRef = eval("document.all." + Element + ".style")
	}
	return oRef
}

function setElementVisibility(Element, state) {
	oElement = createReference(Element)
	if (state.toLowerCase() == 'visible'){
		document.getElementById(Element).className = 'displayOnPage';
	}
	oElement.visibility = state.toLowerCase(); 
}


function getElementVisibility(Element, state) {
	oElement = createReference(Element)
	elementVis = oElement.visibility;
	return elementVis
}

function adjustIFrameSize (iframeWindow) {
  if (iframeWindow.document.height) {
	var iframeElement = parent.document.getElementById(iframeWindow.name);
	iframeElement.style.height = (iframeWindow.document.height) + 'px';
	iframeElement.style.width = iframeWindow.document.width + 'px';
  }
  else if (document.all) {
	var iframeElement = parent.document.all[iframeWindow.name];
	if (iframeWindow.document.compatMode &&  iframeWindow.document.compatMode != 'BackCompat') 
	{
	  iframeElement.style.height = iframeWindow.document.documentElement.scrollHeight + 'px';
	  iframeElement.style.width = iframeWindow.document.documentElement.scrollWidth + 'px';
	}
	else {
	  iframeElement.style.height = iframeWindow.document.body.scrollHeight +  'px';
	  iframeElement.style.width = iframeWindow.document.body.scrollWidth + 'px';
	}
  }
}

function bodyheight () {
			x = document.body.offsetHeight
			parent.document.getElementById("todoList").style.height = x + 'px'
}

function setup() {
	if (navigator.appVersion.indexOf('Mac') == -1) {
		if (parent.adjustIFrameSize) parent.adjustIFrameSize(window);
	}
}
function setElementClass() {
    for (var i=0; i<setElementClass.arguments.length; i+=2) {
		if (top.isIE) { 
			eval('document.all.' + setElementClass.arguments[i] + '.className = "' + setElementClass.arguments[i+1] + '"'); 
		} else if (top.isDOM) {
			eval('document.getElementById(\'' + setElementClass.arguments[i] + '\').className = "' + setElementClass.arguments[i+1] + '"'); 
		}
	}
}


/* Special  ONly function to generate open/close effect on aud paths, left sidebar, subpages. */
function toggle(el) {
	for(ii=0; ii<el.parentNode.childNodes.length; ii++)
	{
		if (el.parentNode.childNodes[ii].tagName == "UL") 
		{
			if (el.parentNode.childNodes[ii].style.display == "none" || el.parentNode.childNodes[ii].style.display == "" ) {
				el.parentNode.childNodes[ii].style.display = "block"
				el.className = "opened"
			} else {
				el.parentNode.childNodes[ii].style.display = "none"
				el.className = "closed"
			}
		}
	}
}



/**********************************************
Called in FAQ Search - Advanced Mode
"All of the Above" is checked by default. 
If any other group is checked, "All of the Above" unchecks.
M. Taylor 1/30/07
**********************************************/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addClick(){
    if( document.searchFrm.ckGroups ){
      var items = document.searchFrm.ckGroups;
      for( var i=0; i < items.length; i++ ){
        items[i].onclick = function(){
          document.searchFrm.ckAll.checked = false;
        };
      }
	 if (document.searchFrm.ckAll ) {
		 document.searchFrm.ckAll.onclick = function() {
			 toggleChecked();
		 };
	 }
    }
}

function toggleChecked() {
	 var items = document.searchFrm.ckGroups;
	if (document.searchFrm.ckAll.checked == true) {
		checkAll();
	} else {
		unCheckAll();
	}
}

function checkAll()
{
	var items = document.searchFrm.ckGroups;
    for (i=0; i < items.length; i++) 
	{
    if(items[i].checked == true)
    	{items[i].checked = true; }
    else {items[i].checked = true;}
	}
}
function unCheckAll(){
	var items = document.searchFrm.ckGroups;
    for (i=0; i < items.length; i++) 
	{
    if(items[i].checked == true)
    	{items[i].checked = false; }
    else {items[i].checked = false;}
	}
}


/**********************************************
Ajax for main image on Admissions Home page
M. Taylor 3/22/07
**********************************************/

function attachLinks() {
	var buttons = document.getElementsByTagName('div');
	var atemp = (Math.floor(Math.random()*6)+1);
	
	for (i=0; i<buttons.length; i++) {
	
	switch(buttons[i].className) {
		case "btn1":
		  	buttons[i].onclick = function() { requestImage('1'); }
	 		break
  		case "btn2":
		  	buttons[i].onclick = function() { requestImage('2'); }
	 		break
		case "btn3":
		  	buttons[i].onclick = function() { requestImage('3'); }
	 		break
		case "btn4":
		  	buttons[i].onclick = function() { requestImage('4'); }
	 		break
		case "btn5":
		  	buttons[i].onclick = function() { requestImage('5'); }
	 		break
		case "btn6":
		  	buttons[i].onclick = function() { requestImage('6'); }
	 		break
		}
	}
	//requestImage(atemp);
	loop(atemp);
}

var xmlHttp;

function createXMLHttpRequest() {
	if(window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function requestImage(num) {
	var value = num;
	if (value != "") {
		//setOpacity(4);
		createXMLHttpRequest();
		xmlHttp.open("GET", "/includes/get_image.asp?value=" + value);
		xmlHttp.onreadystatechange = loadImage;
		xmlHttp.send(null);
	}
}

function loadImage() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			var newImage = xmlHttp.responseText.split(",");
			img = document.getElementById('image');
			img.setAttribute("src", newImage[0]);
			img.setAttribute("alt", newImage[1]);
			document.getElementById("buttonSet").className = newImage[2];
			//setOpacity(10);
		}
	}
}

function setOpacity(value) {
	img = document.getElementById('photoCt');
	img.style.opacity = value/10;
	img.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function loop(input) {
	var current = input;
	requestImage(current);
	current = current + 1;
	if(current > 6) {
		current = 1;
	}
	//alert("test");
	setTimeout("loop(" + current + ");", 7000);
	//setTimeout(this.name + ".loop(input)",10000);
}