// Improve form error handling
// ----------------------------------------------
function setfocus(objectid)
{
    if(document.getElementById(objectid))
    {
        document.getElementById(objectid).focus();
    }
}

function showMore(varA1, varB1){
var123 = ('varXYZ' + (varA1));
varABC = ('varP' + (varA1));
if( document.getElementById ) {
if( document.getElementById(var123).style.display ) {
if( varB1 != 0 ) {
document.getElementById(var123).style.display = "block";
document.getElementById(varABC).style.display = "none";
} else { document.getElementById(var123).style.display = "none"; 
document.getElementById(varABC).style.display = "block"; }
} else { location.href = varB1;
return true; }
} else { location.href = varB1;
return true; }
}
// Show/hide
// ----------------------------------------------
function showHide() {
	var shows = document.getElementsByClassName("continue");
	for (var i=0; i<shows.length;i++) {
		showlink = shows[i];
		showlink.onclick = function() {
			this.style.display='none';
			
			para = this.parentNode;
						
			var hider = para.nextSibling;
			while (hider.nodeType != 1)
				hider = hider.nextSibling;

			hider.style.display='block';
			
			var closelinks = document.getElementsByClassName("close",hider);

			if (closelinks.length == 0) {
				var txt = document.createTextNode('close it up');
				var closelink = document.createElement('a');
				closelink.href="#";
				closelink.className = "close";
				closelink.appendChild(txt);
				hider.appendChild(closelink);
				
				closelink.onclick = function() {
					this.parentNode.style.display='none';
					for (j=0;j<shows.length;j++) {
						shows[j].style.display='inline';
					}
					return false;
				}
			}
			return false;
		}
	}
}

// Mailing list functions
// ----------------------------------------------
function mailingList() {
	var mailInput = $('mailingListInput');
	if (mailInput) {
		mailInput.onfocus = function() {
			if (this.value=='enter your e-mail address') {
				this.value = '';
			}
			this.style.color = '#000000';
		}
		mailInput.onblur = function() {
			if (this.value == '') {
			this.value = 'enter your e-mail address';
			}
			this.style.color = '#666666';
		}
	}
}	

// Protect email addresses from spambots
// ----------------------------------------------
function SpamShield(user,domain) {
	locationstring = "mailto:" + user + "@" + domain;
	window.location = locationstring;
}
// Handle new windows
// ----------------------------------------------
function popupLinks() {
 if (!document.getElementsByTagName) return false;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "document") {
     	anchor.href = "javascript:popupDocument('"+anchor.getAttribute("href")+"');";
  }
 }
}
function popupDocument(href) {
 var screenHeight=window.screen.height;
 var screenWidth=window.screen.width;
 var windowWidth = screenWidth-(screenWidth*.05);
 var windowHeight = screenHeight-(screenHeight*.05);
 var Xcoordinate = (screenWidth-windowWidth)/2;
 var Ycoordinate = (screenHeight-windowHeight)/2;
 
 attributes = 'width='+windowWidth+',height='+windowHeight+',scrollbars=auto,toolbar=no,menubar=no,status=no,directories=no,location=no,resizable=yes';
 poppedWindow = window.open(href,'CCT',attributes);
 poppedWindow.moveTo(Xcoordinate,Ycoordinate);
 poppedWindow.focus();
}
// Handle table zebra striping
// ----------------------------------------------
function stripeTables() {
  if (!document.getElementsByTagName) return false;
  var tables = document.getElementsByTagName("tbody");
  for (var i=0; i<tables.length; i++) {
    var odd = false;
    var rows = tables[i].getElementsByTagName("tr");
    for (var j=0; j<rows.length; j++) {
      if (odd == true) {
        addClass(rows[j],"odd");
        odd = false;
      } else {
        odd = true;
      }
    }
  }
}
function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}
// Add a class to a string
// ----------------------------------------------
String.prototype.addClass = function(theClass)
{
	if (this != "")
	{
		if (!this.classExists(theClass))
		{
			return this + " " + theClass;
		}
	}
	else
	{
		return theClass;
	}
	
	return this;
}
// Check if a class exists in a string
// ----------------------------------------------
String.prototype.classExists = function(theClass)
{
	var regString = "(^| )" + theClass + "\W*";
	var regExpression = new RegExp(regString);
	
	if (regExpression.test(this))
	{
		return true;
	}
	return false;
}
// Remove a class from a string
// ----------------------------------------------
String.prototype.removeClass = function(theClass)
{
	var regString = "(^| )" + theClass + "\W*";
	var regExpression = new RegExp(regString);
	
	return this.replace(regExpression, "");
}

String.prototype.validEmail = function()
{
	if (this.match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/))
	{
		return true;
	}
	
	return false;
}

//Polls
if(site_url != 'http://www.themechanism.com/blog') {
	var site_url = 'http://www.themechanism.com/blog';
}

// Attach functions to the window
// ----------------------------------------------
Event.observe(window, 'load', popupLinks, false);
Event.observe(window, 'load', showHide, false);
Event.observe(window, 'load', stripeTables, false);
Event.observe(window, 'load', mailingList, false);

// scrollable contact forms
// ----------------------------------------------
var blnShow = false;

function slShowHideInit(strObject) {
	if (document.getElementById) {
		if (document.getElementById(strObject)) {
			Element.hide(strObject);
			objObject = document.getElementById(strObject);
			objObject.className = "";
		}
	}
}

function slScrollDown(strObject) {
		if (document.getElementById) {
			objObject = document.getElementById(strObject);
			objImage = document.getElementById('ajaxarrow');
			if (blnShow == false) {
				objImage.src = 'http://themechanism.com/images/ajaxarrow_down.gif';
				new Effect.BlindDown(strObject);
				blnShow = true;
			} else {
				objImage.src = 'http://themechanism.com/images/ajaxarrow_right.gif';
				new Effect.BlindUp(strObject);
				blnShow = false;
			}
		}
}