function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}

/* set up history.back links */
function historyBack() {
    if (! document.getElementsByTagName) return;
    var links = document.getElementsByTagName("a");
    for (var c=0; c<links.length; c++) {
        var anchorlink = links[c];
        if (anchorlink.getAttribute("rel") == "previous") {
            anchorlink.onclick = function() {
                                this.href='javascript:history.back()';
                                //return false;
                             }
        }
    }
}

/*
open external link targets in new (_blank) window
params: pass a specific array of anchor elements (e.g. all within a certain container),
else function defaults to all anchors with rel='external'
*/
function externalLinks(targetAnchors) {
    if (!document.getElementsByTagName) return;
    if(! targetAnchors) {
        var anchors = document.getElementsByTagName("a");
        for (var i=0; i<anchors.length; i++) {
            var anchor = anchors[i];
            if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "external" || anchor.parentNode.className=='external')) anchor.target = "_blank";
        }
    } else {
        var anchors = targetAnchors;
        for (var i=0; i<anchors.length; i++) {
            var anchor = anchors[i];
            anchor.target = "_blank";
        }
    }
}

/* print */
function printPage() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "print") {
        anchor.onclick = function() {
                                    window.print();
                                    return false;
                                    }
        }
    }
}

/* Check that an email address is valid based on RFC 821 (?) */
function isValidEmail(address) {
    if (address != '' && address.search) {
        if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
            return true;
        } else return false;
    } else return true;
}

function isValidPhone(NumStr, String) {
    for(var Idx=0; Idx<NumStr.length; Idx++) {
        var Char = NumStr.charAt(Idx);
        var Match = false;

        for(var Idx1=0; Idx1<String.length; Idx1++)
        {
            if(Char == String.charAt (Idx1))
                Match = true;
        }

        if (!Match)
            return false;
    }
    return true;
}

var QuoteShow = Class.create();

  QuoteShow.prototype = {
    initialize: function(element, options) {
      this.element = $(element);
      this.options = Object.extend({className: 'quote', duration: 10}, options);
      this.quotes = document.getElementsByClassName(this.options.className, this.element);

      this.prepareQuotes();
      this.registerCallback();
    },

    prepareQuotes: function() {
      this.currentQuote = this.quotes.first();
      this.element.style.position = 'relative';
      this.element.style.height = this.quotes.max(function(quote) {
        var visible = Element.visible(quote), height;
        Element.setStyle(quote, {position: 'absolute', width: '100%', left: '0px'});
        if (!visible) Element.show(quote);
        height = Element.getHeight(quote);
        if (!visible) Element.hide(quote);
        return height;
      }).toString() + 'px';
    },

    nextQuote: function() {
      return this.quotes[(this.quotes.indexOf(this.currentQuote) + 1) % this.quotes.length];
    },

    registerCallback: function() {
      window.setTimeout(this.tick.bind(this), this.options.duration * 1000);
    },

    tick: function() {
      var currentQuote = this.currentQuote, nextQuote = this.nextQuote();

      new Effect.Parallel([
        new Effect.Fade(currentQuote, {sync: true}),
        new Effect.Appear(nextQuote, {sync: true})
      ], {
        duration: 2,
        afterFinish: (function(effect) {
          this.currentQuote = nextQuote;
          this.registerCallback();
        }).bind(this)
      })
    }
  }

addLoadEvent(historyBack);
addLoadEvent(externalLinks);
//addLoadEvent(printPage);
var myrules = {

/*  '#clientNavPoint' : function(el){
		el.onclick = function(){
    	new Effect.toggle('clientNavList','blind');
        return false;
    }
	}, */
  '#quote-show' : function(el){
  		new QuoteShow('quote-show');
  },
  '#printLink' : function(el){
  		printPage();
  },
  '#showFullDescription0' : function(el){
		el.onclick = function(){
    	new Effect.toggle('fullDescription0','blind', {
				afterFinish:function(){
						var nodeObj = document.getElementById('fullDescription0');
						if(nodeObj.style.display != 'none'){
							el.innerHTML = 'close full profile';
						}else{
							el.innerHTML = 'read full profile';
						}
					}
				}
		);
			return false;
    }
	},
	'#showFullDescription1' : function(el){
		el.onclick = function(){
    	new Effect.toggle('fullDescription1','blind', {
				afterFinish:function(){
						var nodeObj = document.getElementById('fullDescription1');
						if(nodeObj.style.display != 'none'){
							el.innerHTML = 'close full profile';
						}else{
							el.innerHTML = 'read full profile';
						}
					}
				}
		);
			return false;
    }
	},
	'#showFullDescription2' : function(el){
		el.onclick = function(){
    	new Effect.toggle('fullDescription2','blind', {
				afterFinish:function(){
						var nodeObj = document.getElementById('fullDescription2');
						if(nodeObj.style.display != 'none'){
							el.innerHTML = 'close full profile';
						}else{
							el.innerHTML = 'read full profile';
						}
					}
				}
		);
			return false;
    }
	},

	/* this is the rules for ventilate */
	'#newTopic' : function(el){
		el.onclick = function(){
			newTopic();
			return false;
		}
	},

	'.editInPlace' : function(el){
			new Ajax.InPlaceEditor(el.id, '/ajaxControl.php', { parameters : "request=updateTopic&id="+el.id+"&val=" + escape(el.innerHTML) })
	},

	'#hideAll' : function(el){
		el.onclick = function(){
			var node = $('discussionPannel');
			hideAll(node, 'topicEntrys');
			closeAll(node, 'toggle');
			return false;
		}
	},

	'#showAll' : function(el){
		el.onclick = function(){
			var node = $('discussionPannel');
			showAll(node, 'topicEntrys');
			openAll(node, 'toggle');
			return false;
		}
	},

	'.toggle' : function(el){
		el.onclick = function(){
			var parent = el.parentNode;
			var str = parent.id
			var state = toggle(str.replace(/titleFor/,"entrysFor"));
			if(state == 1){
				el.className = 'toggle open';
			}else{
				el.className = 'toggle closed';
			}
			return false;
		}
	}

};

Behaviour.register(myrules);


function gup(name){
		var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var tmpURL = window.location.href;
    var results = regex.exec( tmpURL );
    if( results == null )
    	return "";
		else
			return results[1];
}

              function createCookie(name,value,days) {
               if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
               }
               else var expires = "";
               document.cookie = name+"="+value+expires+"; path=/";
              }

              function readCookie(name) {
               var nameEQ = name + "=";
               var ca = document.cookie.split(';');
               for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
               }
               return null;
              }

              function eraseCookie(name) {
               createCookie(name,"",-1);
              }

              var campaign = gup('campaign');
              var userid = gup('userid');

              if(campaign.toString()!=''){
                  document.write('<img src=http://2fluid.propagatedigital.com/campaignTracker.php?campaign='+campaign.toString()+'&userid='+userid.toString()+'&trackType=click width=1 height=1 border=0>');
                  var cookieStr = campaign.toString()+':'+userid.toString();
                  createCookie('propagate',cookieStr,31);
              }


/* Add onDomReady support for all browsers
*  better than window.onload as	we do NOT have to wait for all media to	load
*  we can start	working	with the DOM as	soon as	it is ready	*/
Object.extend(Event, {
  _domReady	: function() {
	if (arguments.callee.done) return;
	arguments.callee.done =	true;

	if (this._timer)  clearInterval(this._timer);

	this._readyCallbacks.each(function(f) {	f()	});
	this._readyCallbacks = null;
},
  onDOMReady : function(f) {
	if (!this._readyCallbacks) {
	  var domReady = this._domReady.bind(this);

	  if (document.addEventListener)
		document.addEventListener("DOMContentLoaded", domReady,	false);

		/*@cc_on @*/
		/*@if (@_win32)
			document.write("<script	id=__ie_onload defer src=javascript:void(0)><\/script>");
			document.getElementById("__ie_onload").onreadystatechange =	function() {
				if (this.readyState	== "complete") domReady();
			};
		/*@end @*/

		if (/WebKit/i.test(navigator.userAgent)) {
		  this._timer =	setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) domReady();
		  }, 10);
		}

		Event.observe(window, 'load', domReady);
		Event._readyCallbacks =	 [];
	}
	Event._readyCallbacks.push(f);
  }
});