/* functions for search box */
function submitForm(formId) {
 document.getElementById(formId).submit();
}
function clearquery(id,text) {
 if(!document.getElementById(id)) return false;
 if (document.getElementById(id).value == text) {
	document.getElementById(id).value = "";
 }
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* stripe table rows */
$(document).ready(function(){
$("table tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
$("table tr:even").addClass("alt");
});


/* no table styles */
$(document).ready(function(){
$("div.vt_table_no_styles table tr").mouseover(function() {$(this).removeClass("over");}).mouseout(function() {$(this).removeClass("over");});
$("div.vt_table_no_styles table tr:even").removeClass("alt");
});

// show left tab open by default
$(document).ready(function() {
	$("div[class*='vt_tc_']").hide();	
	$("div[name*='TabNav']").hide();
	$("div[class*='vt_tc_1']").show();	
	$("div[name='TabNav1']").show();
	$('.vt_tc_l1').show();
	$("div[name='leftTabNav1']").show();
});

//show and hide the left and right tab functions
$(document).ready(function() {					   
   // left tab group, do this when clicked
   $('.vt_ltab_1, .vt_ltab_2').click(function(){
     $(this).parent().parent().parent().find('.vt_tc_l1').show();
	 	 $(this).parent().parent().parent().find('.vt_tc_r1').hide();
		 $(this).parent().parent().parent().find("div[name='rightTabNav1']").hide();
		 $(this).parent().parent().parent().find("div[name='leftTabNav1']").show();	 
	});
   
   // right tab group
    $('.vt_rtab_1,.vt_rtab_2').click(function(){
     	$(this).parent().parent().parent().find('.vt_tc_l1').hide();
	 		$(this).parent().parent().parent().find('.vt_tc_r1').show();
	 		$(this).parent().parent().parent().find("div[name='rightTabNav1']").show();
	 		$(this).parent().parent().parent().find("div[name='leftTabNav1']").hide();	
   	});
	
 });

// show and hide for multi tab group
$(document).ready(function() {			   
	//tab group 1
	$('.vt_tab_1a, .vt_tab_1b, .vt_tab_1c, .vt_tab_1d').click(function(){
		$(this).parent().parent().parent().find("div[class*='vt_tc_']").hide();	
		$(this).parent().parent().parent().find("div[name*='TabNav']").hide();
		$(this).parent().parent().parent().find("div[class*='vt_tc_1']").show();	
		$(this).parent().parent().parent().find("div[name='TabNav1']").show();
	});
   
	//tab group 2
	$('.vt_tab_2a, .vt_tab_2b, .vt_tab_2c, .vt_tab_2d').click(function(){
		$(this).parent().parent().parent().find("div[class*='vt_tc_']").hide();	
		$(this).parent().parent().parent().find("div[name*='TabNav']").hide();
		$(this).parent().parent().parent().find("div[class*='vt_tc_2']").show();	
		$(this).parent().parent().parent().find("div[name='TabNav2']").show();
	});
	
   //tab group 3
   $('.vt_tab_3a, .vt_tab_3b, .vt_tab_3c, .vt_tab_3d').click(function(){
		$(this).parent().parent().parent().find("div[class*='vt_tc_']").hide();	
		$(this).parent().parent().parent().find("div[name*='TabNav']").hide();
		$(this).parent().parent().parent().find("div[class*='vt_tc_3']").show();	
		$(this).parent().parent().parent().find("div[name='TabNav3']").show(); 
	});
   
   //tab group 4
   $('.vt_tab_4a, .vt_tab_4b, .vt_tab_4c, .vt_tab_4d').click(function(){
		$(this).parent().parent().parent().find("div[class*='vt_tc_']").hide();	
		$(this).parent().parent().parent().find("div[name*='TabNav']").hide();
		$(this).parent().parent().parent().find("div[class*='vt_tc_4']").show();	
		$(this).parent().parent().parent().find("div[name='TabNav4']").show();
	});
 });

//ability to choose which tab to have open on page load
//Example: apage.html#opentab=2
var winloc = window.location.toString();
var tabchoice = winloc.search(/#opentab=/i);
if(tabchoice > 0){
	// get the tab number to open
	tabchoice = tabchoice + 9;
	var tabToOpen = winloc.substring(tabchoice, winloc.length);
	if (tabToOpen > 0 ){
		$(document).ready(function(){
			// check for existance of element
			if($("div[name='TabNav"+tabToOpen+"']").length > 0){
				$("div[class*='vt_tc_']").hide();	
				$("div[name*='TabNav']").hide();
				$("div[class*='vt_tc_"+tabToOpen+"']").show();	
				$("div[name='TabNav"+tabToOpen+"']").show();
			}
			// for two tabbed, we have different styles
			if((tabToOpen == 1) && $("div[name='leftTabNav1']").length > 0){
				$("div[class*='vt_tc_r1']").hide();	
				$("div[name='rightTabNav1']").hide();
				$("div[class*='vt_tc_l1']").show();	
				$("div[name='leftTabNav1']").show();
			}else if ((tabToOpen == 2) && $("div[name='rightTabNav1']").length > 0){
				$("div[class*='vt_tc_l1']").hide();	
				$("div[name='leftTabNav1']").hide();
				$("div[class*='vt_tc_r1']").show();	
				$("div[name='rightTabNav1']").show();
			}else if ($("div[name='leftTabNav1']").length > 0){
				// case where there is a two column and more than two columns on same page
				// and the reference is to the third column, default to first tab on 2 col
				$("div[class*='vt_tc_r1']").hide();	
				$("div[name='rightTabNav1']").hide();
				$("div[class*='vt_tc_l1']").show();	
				$("div[name='leftTabNav1']").show();
			}
	 });
	}
}

// slide down for quicklinks
$(document).ready(function() {
   // show quicklinks
   $('#vt_ql_link').css('display', 'block');
   
   // left tab grup this when clicked
   $('#vt_ql_link').click(function(){
     $('#vt_ql_list').slideToggle('fast'); 
	});
	
});

// javascript form validation
$(document).ready(function(){
	// only load necessary files if we have a special class to trigger it												 
	if($(".vt_js_form").size()>0){
		// loading necessary javascript files for validation, wysiwyg editor, datepicker
		$.getScript("/global_assets/js/forms/jquery.ui.core.pack.js", function(){
			$.getScript("/global_assets/js/forms/jquery.wysiwyg.pack.js", function(){
				$(".vt_js_form .wysiwyg").wysiwyg({
					controls : {
					separator01 : { visible: true},
					indent : { visible: true },
					outdent: { visible: true },
							separator04 : { visible : true },
							insertOrderedList : { visible : true },
							insertUnorderedList : { visible : true },
					insertImage : { visible : false },
					increaseFontSize : {visible: false },
					decreaseFontSize : {visible: false },
					separator09 : { visible: false}
					}
				});				
			$.getScript("/global_assets/js/forms/jquery.validate.js", function(){
			$.getScript("/global_assets/js/forms/additional-methods.js", function(){	
			$.getScript("/global_assets/js/forms/ui.datepicker.js", function (){
				$(".vt_js_form .date").datepicker();
				$(".vt_js_form").validate();
			});
			});
			});																	 
			});	
		});
		$('head').append('<link rel="stylesheet" href="/global_assets/js/forms/jquery.wysiwyg.css" type="text/css" media="screen" />');
		$('head').append('<link rel="stylesheet" href="/global_assets/js/jquery-themes/vt-jquery-theme-1/jquery-ui-1.7.2.custom.vt.theme.1.css" type="text/css" media="screen" />');
	}
});

function checkLinkBack(aUrl){
	var regExpMatch = new RegExp("https:\/\/[a-z]*\.cms\.vt\.edu.*|https:\/\/ensemble\.vt\.edu.*");
	var regRes = regExpMatch.exec(aUrl);
	if(regRes && regRes[0].length>0){
		return true;
	}
	return false;
}
/*
// shows preview bar and gives end users ability to change context to show
// what should be published by the cms
$(document).ready(function(){
	if(checkLinkBack(window.location.href)){
		var pscript = document.createElement('script');
		pscript.type = 'text/javascript';
		pscript.src = '/global_assets/js/vt_cms_preview_utils.js';
		$('head').append(pscript);
		showPreviewBar("false");
	}
});
*/


