/*------------------------------------------------------------------------------------------------------
 *
 * Title : 		DCCC Helper
 * Author : 		Timothy Crowe
 *
 * Description :	This is a compiled list of progressive enhancements for dccc.edu.  Information 
 * 			about each script can be found at each of the functions respective site.
 * 			A great thanks is in order for those awesome individuals mentioned below
 * 			for making my life and my work alot easier.
 *
 * Modified : 		12.22.2005
 *
---------------------------------------------------------------------------------------------------------*/
addLoadEvent(linkPreview);
addLoadEvent(attachFormHandlers);
function stripe() {
     striper('table','striper','tr','odd,even')
 }

addLoadEvent(stripe);
/* the addLoadEvent for mainNav is found below */


/*------------------------------------------------------------------------------------------------------
 *
 * Title : 		Generic Form Validation Routine
 * Author : 		Gez Lemon
 * URL : 		http://juicystudio.com/article/generic-form-validation.php
 *
 * Description :	This article investigates associating a generic form handler to all forms 
 * 			on a web page using ECMAScript.
 *
 * Created : 		4/24/2005
 *
---------------------------------------------------------------------------------------------------------*/
function attachFormHandlers()
{
  // Ensure we're working with a 'relatively' standards 
  // compliant browser
  if (document.getElementsByTagName) {

	var objForm = document.getElementsByTagName('form')


    for (var iCounter=0; iCounter<objForm.length; objForm++)
      objForm[iCounter].onsubmit = function(){return checkForm(this);}
   }
}

function checkForm(objForm)
{
  var arClass, bValid;
  var objField = objForm.getElementsByTagName('*');

  for (var iFieldCounter=0; iFieldCounter<objField.length; iFieldCounter++)
  {
    // Allow for multiple values being assigned to the class attribute
    arClass = objField[iFieldCounter].className.split(' ');
    for (var iClassCounter=0; iClassCounter<arClass.length; iClassCounter++)
    {
      switch (arClass[iClassCounter])
      {
        case 'string':
           bValid = isString(objField[iFieldCounter].value.replace(/^\s*|\s*$/g, ''));
           break;
        case 'number' :
           bValid = isNumber(objField[iFieldCounter].value);
           break;
        case 'email' :
           bValid = isEmail(objField[iFieldCounter].value);
             break;
        default:
           bValid = true;
      }

      if (bValid == false)
      {
        // If this field is invalid, leave the tesing early,
        // and alert the visitor to this error
        alert('Please review the value you provided for ' + objField[iFieldCounter].name);
        objField[iFieldCounter].select();
        objField[iFieldCounter].focus();
        return false;
      }
    }
  }
  return true;
}

function isString(strValue)
{
  return (typeof strValue == 'string' && strValue != '' && isNaN(strValue));
}

function isNumber(strValue)
{
  return (!isNaN(strValue) && strValue != '');
}

function isEmail(strValue)
{
  var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;

  return (strValue != '' && objRE.test(strValue));
}



/*------------------------------------------------------------------------------------------------------
 *
 * Title : 		Suckerfish Dropdowns
 * Author : 		Patrick Griffiths and Dan Webb
 * URL : 		http://www.alistapart.com/articles/dropdowns/
 *
 * Description :	“DHTML” dropdown menus have notoriously involved nasty big chunks of JavaScript 
 *			with numerous browser-specific hacks that render any otherwise neat, semantic HTML 
 *			quite inaccessible. Oh, the dream of a lightweight, accessible, standards-compliant, 
 *			cross-browser-compatible method! Enter Suckerfish Dropdowns.
 *
 * Created : 		11/7/2003
 *
---------------------------------------------------------------------------------------------------------*/
mainNav = function () {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("main-option-list");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}
addLoadEvent(mainNav);


/*------------------------------------------------------------------------------------------------------
 *
 * Title : 		Preview Your Links with Unobtrusive JavaScript
 * Author : 		Chris Campbell
 * URL : 		http://particletree.com/features/preview-your-links
 *
 * Description :	Inspired by the TargetAlert Firefox extension that “provides visual cues for
 *			the destinations of hyperlinks” and Christian Heilmann’s Image previews with 
 *			DOM JavaScript, we wrote a simple set of unobtrusive JavaScript functions to 
 *			find links on a page that go to amazon product pages, pdfs, word documents or
 *			whatever destination that might slow down the browsing process and adds an 
 *			icon next to them to let you know what you might expect to find behind a link.
 *
 * Created : 		7/24/2005
 * Modified : 		7/27/2005
 *
---------------------------------------------------------------------------------------------------------*/

function linkPreview(){
	var links = document.getElementsByTagName("a");

	for (i=0; i<links.length; i++){
		var currentLink = links[i];
		var	images = currentLink.getElementsByTagName("img");
		
	 	// Check if the link is an image. We don't want icons next to images.
		if (images.length == 0){
			var linkHref = currentLink.href;
			
			// Find all links directed to amazon.com 
			if (linkHref.match(/amazon.com/)){
				append(currentLink, "amazon");
			}
			else{
				checkLinks(linkHref, currentLink)
			}
		}
	}
}

function checkLinks(linkHref, currentLink){
	var linkHrefParts = linkHref.split(".");
	
	// extension is the last element in the LinkSplit array
	var extension = linkHrefParts[linkHrefParts.length - 1];
	
	// In some browsers there is a "/" placed after the link. removes the "/"
	extension = extension.replace("/","");
	
	if( extension in { doc:1, pdf:1, ppt:1, txt:1, xls:1, zip:1 } ){
		append(currentLink, extension );
	}
}

function append(currentLink, extension){
	var span = document.createElement('span');
	span.innerHTML = "&nbsp;";
	currentLink.parentNode.insertBefore(span,currentLink.nextSibling);
	span.className = extension;
}
 
 /*------------------------------------------------------------------------------------------------------
 *
 * Title : 		Splintered striper 1.3
 * Author : 	Patrick H. Lauke aka redux / www.splintered.co.uk
 * URL : 		http://24ways.org/advent/splintered-striper
 * Distributed under the Creative Commons Attribution-ShareAlike license - http://creativecommons.org/licenses/by-sa/2.0/
 *
 * Description:	reworking of Zebra Tables and similar methods which works not only for tables and even/odd rows,
 * 				but as a general DOM means of assigning any number of classes to children of a parent element.
 *
 * Summary:      Core experiment function that applies any number of classes to all child elements
 *               contained in all occurences of a parent element (either with or without a specific class)
 * Parameters:   parentElementTag - parent tag name
 *               parentElementClass - class assigned to the parent; if null, all parentElementTag elements will be affected
 *               childElementTag -  tag name of the child elements to apply the styles to
 *               styleClasses - comma separated list of any number of style classes (using 2 classes gives the classic "zebra" effect)
 * Return:       none
 *
 * Modified : 		8/9/2005
 *
---------------------------------------------------------------------------------------------------------*/
function striper(parentElementTag, parentElementClass, childElementTag, styleClasses)
{
	var i=0,currentParent,currentChild;
	// capability and sanity check
	if ((document.getElementsByTagName)&&(parentElementTag)&&(childElementTag)&&(styleClasses)) {
		// turn the comma separate list of classes into an array
		var styles = styleClasses.split(',');
		// get an array of all parent tags
		var parentItems = document.getElementsByTagName(parentElementTag);
		// loop through all parent elements
		while (currentParent = parentItems[i++]) {
			// if parentElementClass was null, or if the current parent's class matches the specified class
			if ((parentElementClass == null)||(currentParent.className == parentElementClass)) {
				var j=0,k=0;
				// get all child elements in the current parent element
				var childItems = currentParent.getElementsByTagName(childElementTag);
				// loop through all child elements
				while (currentChild = childItems[j++]) {
					// based on the current element and the number of styles in the array, work out which class to apply
					k = (j+(styles.length-1)) % styles.length;
					// add the class to the child element - if any other classes were already present, they're kept intact
					currentChild.className = currentChild.className+" "+styles[k];
				}
			}
		}
	}
}
/*
	Copyright Robert Nyman, http://www.robertnyman.com
	Free to use if this text is included
*/
// ---
function $(strId){
	return document.getElementById(strId);
}
// ---
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];		
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}	
	}
	return (arrReturnElements)
}
// ---
function addClassName(oElm, strClassName){
	var strCurrentClass = oElm.className;
	if(!new RegExp(strClassName, "i").test(strCurrentClass)){
		oElm.className = strCurrentClass + ((strCurrentClass.length > 0)? " " : "") + strClassName;
	}
}
// ---
function removeClassName(oElm, strClassName){
	var oClassToRemove = new RegExp((strClassName + "\s?"), "i");
	oElm.className = oElm.className.replace(oClassToRemove, "").replace(/^\s?|\s?$/g, "");
}
// ---









/*------------------------------------------------------------------------------------------------------
 *
 * Title : 		Add Class 
 * Author : 	Takeshi Tanabe
 * Base code : 	Yahoo UI
 *
 * Description :	Add class to element. Originally, designed for strip table. But useful for any ohter 
					function. Don't use incorrectlly!! 
 *
 * Created : 		3/29/2007
 *
---------------------------------------------------------------------------------------------------------*/

function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}






/*------------------------------------------------------------------------------------------------------
 *
 * Title : 		Executing JavaScript on page load
 * Author : 		Simon Willison
 * URL : 		http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 *
 * Description :	This function assists in adding more that one onload to a page when it 
 * 			is finished loading.
 *
 * Created : 		5/26/2004
 *
---------------------------------------------------------------------------------------------------------*/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}









