// JavaScript Document
// JavaScript Document
var active = "link_0";
var active2 = "page-1-marker";
var curpage = "page-1";
var nextpage = "page-0";
var search_data = null;

function validate_phone(phone){
	var rgEx = /[\d]{3}-{0,1}[\d]{3}-{0,1}[\d]{4}/
	if(phone.match(rgEx) != null){
		return true;
	}
	else{
		return false;
	}
}
function validate_email(email) {
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(email.match(filter) != null){
		return true;
	}
	else{
		return false;
	}
}
function validate_form(name, required, values){
	var valid = true;
	var error = "";
	var form = document.getElementById(name);
	if(required = "all"){
		var elem = form.elements;
		for(var i = 0; i < elem.length; i++){
			if(validate(elem[i].name,elem[i].value, true) == false){
				valid = false;
				error += "Problem with the field "+elem[i].name+"\n";
			}
		}
	}
	if(valid == true){
		document.validateform.submit();
	}
	else{
		alert(error);
	}
}
function validate(name , value, required){
	var status = true;
	if(name == "email"){
		status = validate_email(value);
		
	}else if(required == true){
		if(name == "phone"){
			status = validate_phone(value);
		}else {
			if(value == '' || value == null){
				status = false;
			}
		}
	}
		if(status == true){
			return true;
		}
		else{
			return false;
	}
}

function copy_text(source, destination){
	var src = document.getElementById(source);
	var dest = document.getElementById(destination);
	dest.value = src.value;
}
function get_extension(text){
	var l = text.length;
	var i = text.lastIndexOf(".");
	if(l !=0 && i != -1){
		return text.substr(i+1,l);
	}
	else{
		return false;
	}
}
function accepted_extensions(type,ext){
	var extensions = new Array();
	extensions["video"] = Array("mpg","mpeg","mp4","mpga");
	extensions["image"] = Array("jpg","jpeg","png","gif");
	for(var i in extensions[type]){
		alert(i);
		if(i == ext){
			return true
		}
	}
	return false;
}
function validate_uploads(id){
	alert("IT WAS CALLED");
	var text = document.getElementById(id);
	var type = document.getElementById("mediatype");
	if(accepted_extensions(type.value,get_extension(text.value))){
		alert("IT SHOULD RETURN TRUE");
		return true;
	}
	else{
		alert("Please Enter A Correct File Format! ."+get_extension(text.value)+" is an invalid extension");
		return false;
	}
}

function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }


function hidediv(thediv) {
		jQuery("#"+thediv).hide()
	}
function showdiv(thediv) {
		jQuery("#"+thediv).show();
	}
	
function swapActive(id){
		//alert(curpage);
		//alert(id);
		hidediv(curpage);
		showdiv(id);		
		curpage = id;
		changeClass2("active",curpage+"-marker");
		
	}
	function swapActive2(id){
		//alert(curpage);
		//alert(id);
		//alert(nextpage);
		//alert(id);
		hidediv(nextpage);
		showdiv(id);
		nextpage = id;
		
	}
	
	
	function changeClass(classname,id){
		document.getElementById(id).className = classname;
		document.getElementById(active).className ="";
		active = id;		
	}
	function changeClass2(classname,id){
		document.getElementById(id).className = classname;
		document.getElementById(active2).className ="";
		active2 = id;		
		jQuery(".active")
	}
	function makeRelative(value){
	if(value == "featured"){
		document.getElementById(value).className = "relative";
	}
}
function swapNews(id){
	
	hidediv(curpage);
	showdiv(id);		
	curpage = id;
	
	
	changeNewsClass("active",curpage+"-marker");
}

function changeNewsClass(theclass,id){
	jQuery("."+theclass).each(function(){
		jQuery(this).removeClass(theclass);
	});
	jQuery("."+id).each(function(){
		jQuery(this).addClass(theclass);
	});
}


function unRelative(value){
	if(value == "featured"){
		document.getElementById(value).className = "";
	}
}
function refreshPage(){
location.reload(true);
}

function loadProjectResult(id,container){
	jQuery.ajax({
	  url: '/cms/index.php?option=com_projects',
	  type:'POST',
	  data: {'option' : 'com_projects', 'layout' : 'projects', 'project' : id, 'format':'raw'},
	  success: function(data) {
		//alert(data);
		search_data = data;
		fadeOut(container);
		//jQuery('#'+container).fadeOut(600,function(){ jQuery('#'+container).html(data); setTimeout("jQuery('#"+container+"').fadeIn(600);",300); });
		//jQuery('#'+id).attr('disabled',false);
	  }
	});
}


function fadeOut(div){
	jQuery("#"+div).stop().animate({ opacity: 0}, 1000,swapContent(div));
}
function swapContent(div){
	jQuery("#sub_left").addClass("loading");
	setTimeout("setContent('"+div+"')",1000);
	setTimeout('fadeIn("'+div+'")',1200);
}
function setContent(div){
	jQuery('#'+div).html(search_data);
}
function fadeIn(div){
	jQuery("#sub_left").removeClass("loading");
	jQuery("#"+div).stop().animate({ opacity: 1.0}, 1000);
}


function keywordSearch(val){
	if(val.length == 0){ jQuery("#livesearch").hide(); return false; }
	jQuery("#livesearch").fadeIn(200)
	jQuery("#livesearch").addClass('loading');
	jQuery("#livesearch_results").stop().animate({opacity:0});
	
	jQuery.ajax({
	  url: '/cms/index.php?option=com_projects',
	  type:'POST',
	  data: {'option' : 'com_projects', 'layout' : 'keywords', 'query' : val, 'format':'raw'},
	  error: function(req, status, error){
		 alert(req+" "+status+" "+error);
	  },
	  success: function(data) {
		//alert(data);
		//search_data = data;
		///fadeOut(container);
		//jQuery('#'+container).fadeOut(600,function(){ jQuery('#'+container).html(data); setTimeout("jQuery('#"+container+"').fadeIn(600);",300); });
		//jQuery('#'+id).attr('disabled',false);
		jQuery("#livesearch_results").html(data);
		jQuery("#livesearch").removeClass('loading');
		jQuery("#livesearch_results").stop().animate({opacity:1.0});
	  }
	});
}

function setSearchText(data){
	jQuery("#query").val(data);
	elem = jQuery("#searchForm");
	elem.submit();
}

/******* SEARCH BOX FUN ************/
function searchFilter(id){
	var html = '';
	switch(id){
		case "com_search":
			jQuery("#site_search").attr("action","/cms/search/");
			jQuery("#search_query").attr("name","searchword");
			html = '<input type="hidden" name="searchphrase" value="all" />';
			break;
		case "com_projects":
			jQuery("#site_search").attr("action","/cms/projects/");
			jQuery("#search_query").attr("name","query");
			html = '<input type="hidden" name="view" value="projects"/><input type="hidden" value="results" name="layout" />';
			break;
		case "com_documents":
			jQuery("#site_search").attr("action","/cms/publications/published-papers");
			jQuery("#search_query").attr("name","q");
			html = "<input type='hidden' name = 'd' value = 'keywords' />";
			html += "<input type='hidden' name = 'search' value = 'search' />";
			break;
	}
	//alert(html);
	jQuery("#search_hidden_holder").html(html);
	
	
}


jQuery(document).ready(function(){
	jQuery("#mainlevel_menu li").hover(
		function(){
			browser = whichBrs();
			if(browser == "Internet Explorer"){
				ver = getInternetExplorerVersion();
				if(ver == 6){
					//alert("in");
					jQuery(this).find("ul").css('display', 'block');
				}
				
			}						
		},
		function(){
			browser = whichBrs();
			if(browser == "Internet Explorer"){
				ver = getInternetExplorerVersion();
				if(ver == 6){
					jQuery(this).find("ul").css('display', 'none');
				}
				
			}						
		}
	);
})




// Browser Detection Javascript
// copyright 1 February 2003, by Stephen Chapman, Felgall Pty Ltd
// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.
function whichBrs() {
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) return 'Opera';
if (agt.indexOf("staroffice") != -1) return 'Star Office';
if (agt.indexOf("webtv") != -1) return 'WebTV';
if (agt.indexOf("beonex") != -1) return 'Beonex';
if (agt.indexOf("chimera") != -1) return 'Chimera';
if (agt.indexOf("netpositive") != -1) return 'NetPositive';
if (agt.indexOf("phoenix") != -1) return 'Phoenix';
if (agt.indexOf("firefox") != -1) return 'Firefox';
if (agt.indexOf("safari") != -1) return 'Safari';
if (agt.indexOf("skipstone") != -1) return 'SkipStone';
if (agt.indexOf("msie") != -1) return 'Internet Explorer';
if (agt.indexOf("netscape") != -1) return 'Netscape';
if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
if (agt.indexOf('\/') != -1) {
if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
return navigator.userAgent.substr(0,agt.indexOf('\/'));}
else return 'Netscape';} else if (agt.indexOf(' ') != -1)
return navigator.userAgent.substr(0,agt.indexOf(' '));
else return navigator.userAgent;
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function checkVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();
  if ( ver > -1 )
  {
    if ( ver >= 8.0 ) 
      msg = "You're using a recent copy of Internet Explorer."
    else
      msg = "You should upgrade your copy of Internet Explorer.";
  }
  alert( msg );
}
