
window.onload = function() {
	if (thisPage!="m_none") {
		changeMenuClass(thisPage, "tabCurrent");
	}
	setOnClick();
	if (document.getElementById("tabMenu")) {
		setTabs();
		fixLinks();
	}
}

function fixLinks() {
  var anchors = document.getElementsByTagName("a");
  for(var i=0; i < anchors.length; i++)
  {
    var a = anchors[i];
    var href = a.href;
    var title = a.title;
    if (href.indexOf("#pagetop") != -1) { // back to top
      a.className = "hidden";
    }
	else if ((href.indexOf("#") != -1) && (href.indexOf("content") == -1)) { // jump ref
      var index = href.indexOf("#") + 1;
      href = "javascript:changeTabs('" + href.substring(index) + "');";  //href = "#" + href.substring(index);
      //a.setAttribute("href",href);
      a.href=href;
    }
  }
}

function changeMenuClass(id, newClass) {
	identity=document.getElementById(id);
	identity.className=newClass;
}

function setTabs() {
	document.getElementById("questions").style.display = "none";
	document.getElementById("realworld").style.display = "none";
	document.getElementById("li_why").className = "tabMenuCurrent";
}
function changeTabs(url) {
	//var divShow = url.substring(url.lastIndexOf("#")+1,url.length);
	var divShow = url;
	
	var divArray = new Array("why","realworld","questions");

	for (var i = 0; i < divArray.length; i++) {
		document.getElementById(divArray[i]).style.display = "none";
	}
	for (var i = 0; i < divArray.length; i++) {
		document.getElementById("li_" + divArray[i]).className = "";
	}
	document.getElementById(divShow).style.display = "block";

	var currentTab = "li_" + divShow;
	document.getElementById(currentTab).className = "tabMenuCurrent";
}

function openWindow(href) {
  var openWindow = this.open(href, "");
}

function openPicture(href) {
  var width = 610;
  var height = 560;
  var x = parseInt((screen.availWidth/2) - (width/2));
  var y = parseInt((screen.availHeight/2) - (height/2));
  var windowFeatures = "width=" + width + ",height=" + height + ",left=" + x +",screenX=" + x +",top=" + y + ",screenY=" + y;
  windowFeatures += ",toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes";
  var openPicWindow = this.open(href, "pic", windowFeatures);
  openPicWindow.focus();
}

function setOnClick() {
  if(!document.getElementsByTagName) {
    return;
  }
  var anchors = document.getElementsByTagName("a");
  for (var i=anchors.length; i !=0; i--) {
    var a=anchors[i-1];
	if (a.rel.indexOf("external") != -1) {
		a.title += " (opens in new window)";
		a.onclick = function(){openWindow(this.href);return false;}
	}
	if (a.rel.indexOf("picture") != -1) {
		a.title += " (opens picture in new window)";
		a.onclick = function(){openPicture(this.href);return false;}
	}
	if (a.rel.indexOf("email") != -1) {
		a.title += "(email link)";
	}
  }
/*  for (var i=anchors.length; i !=0; i--) {
    var a=anchors[i-1];
    if (a.rel=="changeTabs") {
		a.onclick = function(){changeTabs(this.href)};
    }
  }*/

}

//email validation
function validEmail(email) {
	invalidChars = " /:,;"
	if (email == "") {
		return false
	}
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) != -1) {
			return false
		}
	}
	atPos = email.indexOf("@",1)
	if (atPos == -1) {
		return false
	}
	if (email.indexOf("@",atPos+1) != -1) {
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {
		return false
	}
	if (periodPos+3 > email.length)	{
		return false
	}
	return true
}


//Email Spambot Buster function by Dennis Lembree
function spambotBuster() {
	//ensure user agent can do DOM
	if(!document.getElementById || !document.createElement) return;

	//loop through email addresses
	for (var i=0;i<emailAddress.length;i++) {
		//put data in array
		x = emailAddress[i].split(',');
		
		//grab the email span element
		el = document.getElementById(x[0]);
		
		//create the actual link
		theLink = "mailto:" + x[2] + "@" + x[3] + "." + x[4];
		
		//build node for email link
		emailLink = document.createElement("a");
		emailLink.appendChild(document.createTextNode(x[1]));
		emailLink.href = theLink;
		emailLink.title = "email link";
		emailLink.rel = "email";
		
		//replace it!
		el.parentNode.replaceChild(emailLink,el)
	}
}

//the awesome addLoad Event function by Simon Willison
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function getHTTPObject() {
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xhr = false;
      }
    }
  }
  return xhr;
}
