//////////////////////GENERAL.JS///////////////////////////////
//CREATE TABS
var Spry;
if (!Spry) Spry = {};
if (!Spry.Widget) Spry.Widget = {};

Spry.Widget.TabbedPanels = function(element, opts)
{
	this.element = this.getElement(element);
	this.defaultTab = 0; // Show the first panel by default.
	this.bindings = [];
	this.tabSelectedClass = "TabbedPanelsTabSelected";
	this.tabHoverClass = "TabbedPanelsTabHover";
	this.tabFocusedClass = "TabbedPanelsTabFocused";
	this.panelVisibleClass = "TabbedPanelsContentVisible";
	this.focusElement = null;
	this.hasFocus = false;
	this.currentTabIndex = 0;
	this.enableKeyboardNavigation = true;

	Spry.Widget.TabbedPanels.setOptions(this, opts);

	if (typeof (this.defaultTab) == "number")
	{
		if (this.defaultTab < 0)
			this.defaultTab = 0;
		else
		{
			var count = this.getTabbedPanelCount();
			if (this.defaultTab >= count)
				this.defaultTab = (count > 1) ? (count - 1) : 0;
		}

		this.defaultTab = this.getTabs()[this.defaultTab];
	}

	if (this.defaultTab)
		this.defaultTab = this.getElement(this.defaultTab);

	this.attachBehaviors();
};

Spry.Widget.TabbedPanels.prototype.getElement = function(ele)
{
	if (ele && typeof ele == "string")
		return document.getElementById(ele);
	return ele;
}

Spry.Widget.TabbedPanels.prototype.getElementChildren = function(element)
{
	var children = [];
	var child = element.firstChild;
	while (child)
	{
		if (child.nodeType == 1 /* Node.ELEMENT_NODE */)
			children.push(child);
		child = child.nextSibling;
	}
	return children;
};

Spry.Widget.TabbedPanels.prototype.addClassName = function(ele, className)
{
	if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) != -1))
		return;
	ele.className += (ele.className ? " " : "") + className;
};

Spry.Widget.TabbedPanels.prototype.removeClassName = function(ele, className)
{
	if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) == -1))
		return;
	ele.className = ele.className.replace(new RegExp("\\s*\\b" + className + "\\b", "g"), "");
};

Spry.Widget.TabbedPanels.setOptions = function(obj, optionsObj, ignoreUndefinedProps)
{
	if (!optionsObj)
		return;
	for (var optionName in optionsObj)
	{
		if (ignoreUndefinedProps && optionsObj[optionName] == undefined)
			continue;
		obj[optionName] = optionsObj[optionName];
	}
};

Spry.Widget.TabbedPanels.prototype.getTabGroup = function()
{
	if (this.element)
	{
		var children = this.getElementChildren(this.element);
		if (children.length)
			return children[0];
	}
	return null;
};

Spry.Widget.TabbedPanels.prototype.getTabs = function()
{
	var tabs = [];
	var tg = this.getTabGroup();
	if (tg)
		tabs = this.getElementChildren(tg);
	return tabs;
};

Spry.Widget.TabbedPanels.prototype.getContentPanelGroup = function()
{
	if (this.element)
	{
		var children = this.getElementChildren(this.element);
		if (children.length > 1)
			return children[1];
	}
	return null;
};

Spry.Widget.TabbedPanels.prototype.getContentPanels = function()
{
	var panels = [];
	var pg = this.getContentPanelGroup();
	if (pg)
		panels = this.getElementChildren(pg);
	return panels;
};

Spry.Widget.TabbedPanels.prototype.getIndex = function(ele, arr)
{
	ele = this.getElement(ele);
	if (ele && arr && arr.length)
	{
		for (var i = 0; i < arr.length; i++)
		{
			if (ele == arr[i])
				return i;
		}
	}
	return -1;
};

Spry.Widget.TabbedPanels.prototype.getTabIndex = function(ele)
{
	var i = this.getIndex(ele, this.getTabs());
	if (i < 0)
		i = this.getIndex(ele, this.getContentPanels());
	return i;
};

Spry.Widget.TabbedPanels.prototype.getCurrentTabIndex = function()
{
	return this.currentTabIndex;
};

Spry.Widget.TabbedPanels.prototype.getTabbedPanelCount = function(ele)
{
	return Math.min(this.getTabs().length, this.getContentPanels().length);
};

Spry.Widget.TabbedPanels.addEventListener = function(element, eventType, handler, capture)
{
	try
	{
		if (element.addEventListener)
			element.addEventListener(eventType, handler, capture);
		else if (element.attachEvent)
			element.attachEvent("on" + eventType, handler);
	}
	catch (e) {}
};

Spry.Widget.TabbedPanels.prototype.onTabClick = function(e, tab)
{
	this.showPanel(tab);
};

Spry.Widget.TabbedPanels.prototype.onTabMouseOver = function(e, tab)
{
	this.addClassName(tab, this.tabHoverClass);
};

Spry.Widget.TabbedPanels.prototype.onTabMouseOut = function(e, tab)
{
	this.removeClassName(tab, this.tabHoverClass);
};

Spry.Widget.TabbedPanels.prototype.onTabFocus = function(e, tab)
{
	this.hasFocus = true;
	this.addClassName(this.element, this.tabFocusedClass);
};

Spry.Widget.TabbedPanels.prototype.onTabBlur = function(e, tab)
{
	this.hasFocus = false;
	this.removeClassName(this.element, this.tabFocusedClass);
};

Spry.Widget.TabbedPanels.ENTER_KEY = 13;
Spry.Widget.TabbedPanels.SPACE_KEY = 32;

Spry.Widget.TabbedPanels.prototype.onTabKeyDown = function(e, tab)
{
	var key = e.keyCode;
	if (!this.hasFocus || (key != Spry.Widget.TabbedPanels.ENTER_KEY && key != Spry.Widget.TabbedPanels.SPACE_KEY))
		return true;

	this.showPanel(tab);

	if (e.stopPropagation)
		e.stopPropagation();
	if (e.preventDefault)
		e.preventDefault();

	return false;
};

Spry.Widget.TabbedPanels.prototype.preorderTraversal = function(root, func)
{
	var stopTraversal = false;
	if (root)
	{
		stopTraversal = func(root);
		if (root.hasChildNodes())
		{
			var child = root.firstChild;
			while (!stopTraversal && child)
			{
				stopTraversal = this.preorderTraversal(child, func);
				try { child = child.nextSibling; } catch (e) { child = null; }
			}
		}
	}
	return stopTraversal;
};

Spry.Widget.TabbedPanels.prototype.addPanelEventListeners = function(tab, panel)
{
	var self = this;
	Spry.Widget.TabbedPanels.addEventListener(tab, "click", function(e) { return self.onTabClick(e, tab); }, false);
	Spry.Widget.TabbedPanels.addEventListener(tab, "mouseover", function(e) { return self.onTabMouseOver(e, tab); }, false);
	Spry.Widget.TabbedPanels.addEventListener(tab, "mouseout", function(e) { return self.onTabMouseOut(e, tab); }, false);

	if (this.enableKeyboardNavigation)
	{
		var tabIndexEle = null;
		var tabAnchorEle = null;

		this.preorderTraversal(tab, function(node) {
			if (node.nodeType == 1 /* NODE.ELEMENT_NODE */)
			{
				var tabIndexAttr = tab.attributes.getNamedItem("tabindex");
				if (tabIndexAttr)
				{
					tabIndexEle = node;
					return true;
				}
				if (!tabAnchorEle && node.nodeName.toLowerCase() == "a")
					tabAnchorEle = node;
			}
			return false;
		});

		if (tabIndexEle)
			this.focusElement = tabIndexEle;
		else if (tabAnchorEle)
			this.focusElement = tabAnchorEle;

		if (this.focusElement)
		{
			Spry.Widget.TabbedPanels.addEventListener(this.focusElement, "focus", function(e) { return self.onTabFocus(e, tab); }, false);
			Spry.Widget.TabbedPanels.addEventListener(this.focusElement, "blur", function(e) { return self.onTabBlur(e, tab); }, false);
			Spry.Widget.TabbedPanels.addEventListener(this.focusElement, "keydown", function(e) { return self.onTabKeyDown(e, tab); }, false);
		}
	}
};

Spry.Widget.TabbedPanels.prototype.showPanel = function(elementOrIndex)
{
	var tpIndex = -1;
	
	if (typeof elementOrIndex == "number")
		tpIndex = elementOrIndex;
	else // Must be the element for the tab or content panel.
		tpIndex = this.getTabIndex(elementOrIndex);
	
	if (!tpIndex < 0 || tpIndex >= this.getTabbedPanelCount())
		return;

	var tabs = this.getTabs();
	var panels = this.getContentPanels();

	var numTabbedPanels = Math.max(tabs.length, panels.length);

	for (var i = 0; i < numTabbedPanels; i++)
	{
		if (i != tpIndex)
		{
			if (tabs[i])
				this.removeClassName(tabs[i], this.tabSelectedClass);
			if (panels[i])
			{
				this.removeClassName(panels[i], this.panelVisibleClass);
				panels[i].style.display = "none";
			}
		}
	}

	this.addClassName(tabs[tpIndex], this.tabSelectedClass);
	this.addClassName(panels[tpIndex], this.panelVisibleClass);
	panels[tpIndex].style.display = "block";

	this.currentTabIndex = tpIndex;
};

Spry.Widget.TabbedPanels.prototype.attachBehaviors = function(element)
{
	var tabs = this.getTabs();
	var panels = this.getContentPanels();
	var panelCount = this.getTabbedPanelCount();

	for (var i = 0; i < panelCount; i++)
		this.addPanelEventListeners(tabs[i], panels[i]);

	this.showPanel(this.defaultTab);
};
//END CREATE TABS


//search suggestion function
/*
	For the rest of the code visit http://www.DynamicAJAX.com
	Copyright 2006 Ryan Smith / 345 Technical / 345 Group.	
*/
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("This Feature Is Not Available With Your Browser");
	}
}

var searchReq = getXmlHttpRequestObject();


function searchSuggest() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('txtSearch').value);
		searchReq.open("GET", '/searchsuggest.php?search=' + str, true);
		searchReq.onreadystatechange = handleSearchSuggest; 
				searchReq.send(null);
	}
	
}

//Called when the AJAX response is returned.
function submitsearchform(){
document.forms['cse-search-box'].submit();
}

function handleSearchSuggest() {
	if (searchReq.readyState == 4) {
		var ss = document.getElementById('search_suggest')
		ss.innerHTML = '';
		var str = searchReq.responseText.split("\n");
		for(i=0; i < str.length - 1; i++) {
		ss.innerHTML += '<div style="border: 1px solid #fff; z-index: 1;">';
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch(this.innerHTML);submitsearchform();"';
			suggest += 'class="suggest_link">' + str[i].replace(' & ',' and ') + '</div>';
	ss.innerHTML +=  '</div>';
		ss.innerHTML += suggest;
		}

if(str.length>4) {
document.getElementById('search_suggest').style.border = '1px solid #c0c0c0';
document.getElementById('search_suggest').style.height = '250px';
document.getElementById('search_suggest').style.width = '250px';
document.getElementById('search_suggest').style.overflow = 'auto';
}else if(str.length==2 || str.length==3){
document.getElementById('search_suggest').style.border = '1px solid #c0c0c0';
document.getElementById('search_suggest').style.height = 'auto';
document.getElementById('search_suggest').style.width = '250px';
document.getElementById('search_suggest').style.overflow = 'none';
}else{
document.getElementById('search_suggest').style.border = '0px';
document.getElementById('search_suggest').style.height = '0px';
}
}
}

//Mouse over function
function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
 }
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}
//Click function
function setSearch(value) {
  value = value.replace('<br>',' ');
	document.getElementById('txtSearch').value = value.replace('<BR>',' ');
	document.getElementById('search_suggest').innerHTML = '';
	document.getElementById('search_suggest').style.height = '0px';
	document.getElementById('search_suggest').style.border = '0px';
	
}
//end search suggestion function


//drop down menu auto select
function _drop_down(obj){

if(obj=='homepage'){page='state-colleges';}
if(obj=='state-colleges' || obj=='city-colleges' || obj=='schools-degree'){page='college';}
if(obj=='career-degree'){page='schools-degree';}
PageIndex1=document.hanging_options_form.hanging_options.selectedIndex
if (document.hanging_options_form.hanging_options.options[PageIndex1].value != "none")
{
location = "/"+page+"/"+document.hanging_options_form.hanging_options.options[PageIndex1].value
}
}
// end


//expand panels
function expand_panel(id,id_link,collapse){

var funding_type=id_link.split("_"); 

var expand_height='auto';
if(-1 != navigator.userAgent.indexOf ("MSIE")){
var collapse_height=document.getElementById(funding_type[0]+'-row-10').offsetTop-document.getElementById(funding_type[0]+'_panel_expand').offsetTop+40;
}else{
var collapse_height=document.getElementById(funding_type[0]+'-row-10').offsetTop-document.getElementById(funding_type[0]+'_panel_expand').offsetTop;
}

if(collapse=='yes')
{
document.getElementById(id).style.height=collapse_height+"px";
document.getElementById(id).style.overflow="hidden";
return;
}

if(document.getElementById(id_link).innerHTML.length>9){
document.getElementById(id).style.height="auto";
document.getElementById(id_link).innerHTML='Show Less';
//collapse
}else{

document.getElementById(id).style.height=collapse_height+"px";
document.getElementById(id_link).innerHTML=document.getElementById(id_link).title;
var scroll_height=document.getElementById(funding_type[0]+'_panel_expand').offsetTop-50;
window.scrollTo(0,scroll_height);
}
}
//end


//expand panels mission statement
function expand_panel_mission_statement(id,id_link,collapse){
               
var expand_height='auto';

if(collapse=='yes')
{
document.getElementById(id).style.height="73px";
document.getElementById(id).style.overflow="hidden";
return;
}

if(document.getElementById(id).style.height!='73px'){
document.getElementById(id).style.height="73px";
document.getElementById(id_link).innerHTML='Show All';
//collapse
}else{ 
document.getElementById(id).style.height="auto";
document.getElementById(id_link).innerHTML='Show Less';
}
}
//end

//expand panels description
function expand_panel_college_description(id,id_link,collapse){
               
var expand_height='auto';
if(-1 != navigator.userAgent.indexOf ("MSIE")){
var collapse_height=document.getElementById('description_id_13').offsetTop-document.getElementById('college_description_expand').offsetTop+50;
}else{
var collapse_height=document.getElementById('description_id_13').offsetTop-document.getElementById('college_description_expand').offsetTop;
}

if(collapse=='yes')
{

document.getElementById(id).style.height=collapse_height+"px";
document.getElementById(id).style.overflow="hidden";
return;
}

if(document.getElementById(id_link).innerHTML.length>14){
document.getElementById(id).style.height="auto";
document.getElementById(id_link).innerHTML='(-) Show Less';
document.getElementById(id_link).title='(-) Show Less';
document.getElementById(id_link).name='(+) Show Full Description';
//collapse
}else{
document.getElementById(id).style.height=collapse_height+"px";
document.getElementById(id_link).innerHTML=document.getElementById(id_link).name;
document.getElementById(id_link).title='(+) Show Full Description';
var scroll_height=document.getElementById('college_description_expand').offsetTop-50;
window.scrollTo(0,scroll_height);
}
}
//end


//go to anchor link
function _show_hash_tab(location){
if(location!=''){
location=location.replace(/#/, "");

if(location=='admission' || location=='tuition_fees' || location=='financial_aid' || location=='degrees_offered'){ inactive_content='general_content'; inactive_tab='general_tab';}
if(location=='faculty_wages' || location=='athletics' || location=='graduation_rates' || location=='campus_security'){ inactive_content='enrolled_students_content'; inactive_tab='enrolled_students_tab';}

active_content=location+'_content';
active_tab=location+'_tab';

document.getElementById(inactive_content).style.display="none";
document.getElementById(active_content).style.display="block";
document.getElementById(inactive_tab).className="TabbedPanelsTab";
document.getElementById(active_tab).className="TabbedPanelsTab TabbedPanelsTabSelected";
document.getElementById(active_content).className="TabbedPanelsstats TabbedPanelsContentVisible";


scroll_height=document.getElementById(location+'_tab').offsetTop-30;
//window.scrollBy(0,scroll_height);
window.location.hash = "#"+location+"_tab";
scrollBy(0,-20);
}
return;
}
//end anchor from link string


//rating functions
function $(v,o) { return((typeof(o)=='object'?o:document).getElementById(v)); }
function $S(o) { return((typeof(o)=='object'?o:$(o)).style); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function abPos(o) { var o=(typeof(o)=='object'?o:$(o)), z={X:0,Y:0}; while(o!=null) { z.X+=o.offsetLeft; z.Y+=o.offsetTop; o=o.offsetParent; }; return(z); }
function XY(e,v) { var o=agent('msie')?{'X':event.clientX+document.documentElement.scrollLeft,'Y':event.clientY+document.documentElement.scrollTop}:{'X':e.pageX,'Y':e.pageY}; return(v?o[v]:o); }

star={};

star.mouse=function(e,o) { 

if(star.stop || isNaN(star.stop)) { 

star.stop=0;

	document.onmousemove=function(e) { var n=star.num;
	
		var p=abPos($('star'+n)), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y; star.num=o.id.substr(4);

		if(oX<1 || oX>170 || oY<0 || oY>40) { star.stop=1; star.revert(); 
    
    }else{

			$S('starCur'+n).width=oX+'px';
			$S('starUser'+n).color='#888888';
			rating=Math.round(oX/170*5);
			$('starUser'+n).innerHTML='Rating: '+rating+' / 5';
		}
		
	};
} 
};
star.update=function(e,o) { 
var n=star.num;
var stars=$('starUser'+n).innerHTML;
stars=stars.replace(/Rating: /,"");
v=parseInt(stars);
document.forms['college_reviews_form'].elements['college_review_rating'].value = v;
n=o.id.substr(4); 
$('starCur'+n).title=v;
req=new XMLHttpRequest(); req.open('GET','?vote='+(v/100),false); req.send(null);
};

star.revert=function() { 

var n=star.num;
var v=parseInt($('starCur'+n).title);
	$S('starCur'+n).width=Math.round(v*34)+'px';
  rating = Math.round(v);  
	$('starUser'+n).innerHTML=(v>0?'Rating: '+rating+' / 5':'Please Select Rating Above');
	$('starUser'+n).style.color='#000';
		
	document.onmousemove='';
};
star.num=0;
//end rating function

//textarea formatting
function _textarea_bg(color)
{
var num=document.getElementsByTagName("textarea").length;
for (i=0;i<=num;i++)
document.getElementsByTagName("textarea")[i].style.background=color;
}
//end textarea formatting

//universal ajax
var http_request = false;
   function ajax_post(url,parameters,span) {

   parameters=parameters.replace(/&%/g,"%26%")
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
        
      http_request.onreadystatechange = alert_contents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);    
      
      function alert_contents() {

         if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById(span).innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
         
      }
//end universal ajax

//reviews data verify and submit ajax
function submit_review(obj) {
        
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!filter.test(document.college_reviews_form.email.value)) {alert ( "Please Enter A Valid Email Address" ); return;}
        if ( document.college_reviews_form.email.value == "Email" )
        {
                alert ( "Please Enter A Valid Email Address" );
                return;
        }
        if ( document.college_reviews_form.terms.checked == false )
        {
                alert ( "Please Agree To The Terms Of Use" );
                return;
        }
        if ( document.college_reviews_form.review.value == "Enter your review..." )
        {
                alert ( "Please Enter A Review" );
               return;
        }
        if (  document.college_reviews_form.college_review_rating.value == "" )
        {
                alert ( "Please Rate This College By Selecting A Rating From 1-5" );
               return;
        }

//remove no reviews submitted
document.getElementById('no_reviews_yet').innerHTML='';


var poststr = "entity_id=" + encodeURI( document.getElementById("entity_id").value ) +"&degree_id=" + encodeURI( document.getElementById("degree_id").value ) +"&email=" + encodeURI( document.getElementById("email").value ) +"&review=" + encodeURI( document.getElementById("review").value ) +"&terms=" + encodeURI( document.getElementById("terms").value ) +"&college_review_rating=" + encodeURI( document.getElementById("college_review_rating").value );
ajax_post('http://www.matchcollege.com/forms/post_review.php', poststr,'post_review');


}
//end

//confirm flag function
function confirmflag(flag) {
  if (confirm('Are You Sure You Would Like To Flag As Inappropriate?')) {
    document.location = flag;
  }
}
// end confirm delete function

//create link
function go_to_page(page) 
{ 
thePage=page 
window.location=thePage 
} 
//end create link

//collapse/hide career options links
function collapse_expand_link(id){
if(document.getElementById(id).style.display=='none'){
document.getElementById(id).style.display='block';
document.getElementById('link_'+id).setAttribute("class", "medium_link bold onclick drop_arrow_list_expand");
document.getElementById("action_message_"+id).innerHTML='[Collapse]';
}else{
document.getElementById(id).style.display='none';
document.getElementById('link_'+id).setAttribute("class", "medium_link bold onclick drop_arrow_list_collapse");
document.getElementById('action_message_'+id).innerHTML='[Expand]';
};
}                         