// JavaScript Document
function init () {
	if (!document.getElementById) {
		for (i=0;i<document.styleSheets.length;i++) {
			document.styleSheets(i).disabled = true;
		}
	}
}

function showTab(show,hide) {
	hideContent = document.getElementById(hide);
	hiddenContentLink = document.getElementById(hide+'-link');
	showContent = document.getElementById(show);
	visibleContentLink = document.getElementById(show+'-link');
	hideContent.style.display = 'none';
	hiddenContentLink.className = '';
	showContent.style.display = 'block';
	visibleContentLink.className = 'active';
	visibleContentLink.blur();	
}
function hem(id,text) {
	if(id!="" || text!="") {
		id_a = id.split(".");
		str = "";
		for(i=0;i<id_a.length;i++) {
			str += String.fromCharCode(id_a[i]/5);
		}
		str2 = text == "" ? str : text;
		return "<a href=\"mailto:" + str + "\">" + str2 + "</a>";
	} else {
		return "";
	}
}
error_msg = " -- error: required field -- ";
function checkform(action,name) {
	var error=false;
	for(i=0;i<document.forms[name].length-2;i++) {
		if(document.forms[name].elements[i].getAttribute('rel')!="optional") {
			if(document.forms[name].elements[i].value=="" || document.forms[name].elements[i].value==error_msg) {
				document.forms[name].elements[i].style.border='solid #FF0000 1px';
				document.forms[name].elements[i].style.borderColor='#FF0000';
				if(document.forms[name].elements[i].type!="file" && document.forms[name].elements[i].type!="password" && action!='rec_form') {
					document.forms[name].elements[i].value=error_msg;
				} else if(document.forms[name].elements[i].type=="file"){
					alert ("The highlighted file field does not containt a path to your file.");
				} else if(document.forms[name].elements[i].type=="password"){
					alert ("The highlighted field is empty.");
				}
				error=true;
			} else if(document.forms[name].elements[i].id=="email") {
				mail_str = document.forms[name].elements[i].value;
				if(mail_str.indexOf(".", mail_str.indexOf("@")) < mail_str.indexOf("@") + 2
					|| mail_str.indexOf(".") < 1
					|| mail_str.indexOf(".") > mail_str.length-2
					|| mail_str.indexOf("@") < 1
					|| mail_str.indexOf("@") > mail_str.length-2)
					{
					error = true;
					document.forms[name].elements[i].style.borderColor='#FF0000';
					document.forms[name].elements[i].value="-- error: invalid email address --";
				}
				
			} else {
				document.forms[name].elements[i].style.borderColor='#C9D8D3';
				if(action=='rec_form') {
					document.forms[name].elements[i].style.border='solid #000000 0px';
				}
			}
		}
	}
	
	if(error) {
		alert("Please ensure that all highlighted fields are completed.");
		return false;
	}

}

function clear_error(field) {
	if(field.value==error_msg || field.value=='-- error: invalid email address --') {
		field.value="";
	}
}
function update_breadcrumbs(xml) {
   document.getElementById('breadcrumbs').innerHTML = xml;

}
function bio_change() {
	if(document.getElementById('bio_fr').style.display=='none' || document.getElementById('bio_fr').style.display=='') {
		document.getElementById('bio_en').style.display='none';
		document.getElementById('bio_fr').style.display='block';
		document.getElementById('bio_fe').innerHTML='<a onclick="bio_change()" href="#">English</a> | French'
	} else {
   		document.getElementById('bio_en').style.display='block';
		document.getElementById('bio_fr').style.display='none';
		document.getElementById('bio_fe').innerHTML='English | <a onclick="bio_change()" href="#">French</a>'
	}
}


//-----------------rotation images------------------
var newBanner = Math.floor(Math.random()*4);

var current_image = "";
var next_image = "";
var prev_image = "";
var nextBanner = "";
var prevBanner = "";
var t;

//rotation image array
var links = new Array();
links[0]= "http://www.usask.ca/ocn";
links[1]= "http://www.usask.ca/ocn";
links[2]= "http://www.usask.ca/ocn";
links[3]= "http://www.usask.ca/ocn";

//front image rotation
function startRotation() {

  newBanner++;

  if (newBanner > 3) {
    newBanner = 0;
  }

  if (newBanner < 0) {
    newBanner = 3;
  }

  var filename = "rotation" + newBanner + ".jpg"; 

  //set image link
  document.getElementById('image_link').href=links[newBanner];

  current_image = "http://www.usask.ca/communications/images/rotation_images/"+filename

  document.getElementById('pic').src = current_image;

  //code with image transition
  //crossfade(document.getElementById('pic'),current_image, '1', '');

  //set next image link
  //if(newBanner==3)
  //  nextBanner = 0;
  //else
  //  nextBanner = newBanner+1;

  //var nextFile = "rotation" + nextBanner + ".jpg";
  //next_image = "http://www.usask.ca/communications/images/rotation_images/"+nextFile; 

  //set previous image link
  //if(newBanner==0)
  //  prevBanner = 3;
  //else
  //  prevBanner = newBanner-1;

  //var prevFile = "rotation" + prevBanner + ".jpg";
  //prev_image = "http://www.usask.ca/communications/images/rotation_images/"+prevFile; 

  //t = setTimeout('startRotation()',7200);
}

function goNext()
{
  clearTimeout(t);
  playRotation();
}

function goBack()
{
  newBanner = newBanner - 2;
  clearTimeout(t);
  playRotation();
}

function stopRotation()
{
  var el = document.getElementById('stop_image');
  el.innerHTML = "PLAY";
  el.onclick = function(){playRotation()};
  clearTimeout(t);
}

function playRotation()
{
  var el = document.getElementById('stop_image');
  el.innerHTML = "STOP";
  el.onclick = function(){stopRotation()};
  startRotation();
}
