//-----------------------------------------------------------------------------

//converts cloaked email addresses

// usage: <span class="emailCloak">badboy(at)badboy.ro</span>

//-----------------------------------------------------------------------------

function emailCloak() {

	var alltags = document.getElementsByTagName("span");

	for (i=0; i < alltags.length; i++) {

		if (alltags[i].className == "emailCloak") {

			var oldText = alltags[i].firstChild;

			var emailAddress = alltags[i].firstChild.nodeValue;

			var user = emailAddress.substring(0, emailAddress.indexOf("("));

			var website = emailAddress.substring(emailAddress.indexOf(")")+1, emailAddress.length);

			var newText = user+"@"+website;

			var a = document.createElement("a");

			a.href = "mailto:"+newText;

			var address = document.createTextNode(newText);

			a.appendChild(address);

			alltags[i].replaceChild(a,oldText);

		}

	}

}



//-----------------------------------------------------------------------------

//sets target of external links to _blank and gives them an external class

//so that a little icon is appended

//-----------------------------------------------------------------------------

function externalLinks() {

  var anchors = document.getElementsByTagName("a");



	//the news section links may not have the rel attribute, so a check

	//on the href value will be needed.

  var isNewsSection = false;  

	if( document.getElementById("news") ) isNewsSection = true;

	

  for (var i=0; i<anchors.length; i++) {

    var anchor = anchors[i];

    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {

      anchor.target = "_blank";

			

			if(anchor.className.length == 0) {

				anchor.className = "external";

			} else {			

				//if class name already has a value of pdf, change it

				anchor.className = anchor.className.replace(/pdf/,"external_pdf");

			}

			

		} else if(anchor.getAttribute("href") && isNewsSection) {			

			if (anchor.getAttribute("href").indexOf('http:\/\/')==0 && 

				  anchor.getAttribute("href").indexOf('communitydevelopmentsc.org/')==-1 &&

					anchor.getAttribute("href").indexOf('communitydevelopmentsc.stasmayer.dynalias.com/')==-1) {

	      anchor.target = "_blank";

				anchor.className += " external";

				anchor.rel = "external";

			}//end if

		}//end else		

	}//end for

	

  var forms = document.getElementsByTagName("form");

  for (var i=0; i<forms.length; i++) {

    var form = forms[i];

    if( form.getAttribute("class") ) {

			if (form.getAttribute("class").indexOf('externalForm')==0) {

      	form.target = "_blank";			

    	}		

		}

	}//end for

}//end function



//----------------------

//check the contact form

//----------------------

function validateForm() {

 var x = document.forms[0].elements;

 var hasErrors = false;

 var already_set_focus = false;

 var error = "The following fields were not entered or are invalid...\n";



 for (var i=0;i<x.length;i++) {

  if (x[i].className.indexOf('requiredField') != -1 && !x[i].value) {

		hasErrors = true;

		//append field to error message

		//all that extra stuff is to uppercase the first letter and 

		//strip off "fm-" which prepends every id name

    error += "\""+((x[i].id).substring(3,4)).toUpperCase()+(x[i].id).substring(4)+"\"\n";

		//set focus unless its already been done

    if(!already_set_focus) { 

 		  x[i].focus();

			already_set_focus = true;

		}

		//add onchange trigger

		x[i].onchange = removeError;

		//change the form field to have a red border

		x[i].parentNode.className += ' form_error';

	}	

 }

 

 //if errors, alert

 if(hasErrors) alert(error); 

 

 return !hasErrors;

}

function removeError() {

  this.parentNode.className = this.parentNode.className.substring(0,this.parentNode.className.lastIndexOf(' '));

	this.onchange = null;

}



//----------------------

//fade technique

//----------------------

var color= new Array("#FAFCFE","#F6F8FD","#F2F5FC","#EDF1FB","#E9EEFA","#E4EBF8","#E0E7F7","#E0E7F7","#D7E1F5","#D3DDF4");

function fadeColor(index,location) {

    if (index >= 1) {

        document.getElementById(location).style.backgroundColor = color[index];

		  if (index > 1) {

			  index -= 1;

			  setTimeout("fadeColor("+index+",\""+location+"\")", 100);

			} else {

			  index -= 1;

			  setTimeout("fadeColor("+index+",\""+location+"\")", 100);

			  document.getElementById(location).style.backgroundColor = "white";

			}

    }

}

function fade(element) {

  document.getElementById(element).style.backgroundColor="#CFDAF3";

	setTimeout("fadeColor(10,\""+element+"\")", 2000);		

}

/*individual fade functions*/

function initFootnotes() {

	anchors = document.getElementsByTagName("a");

	for (var i=0; i<anchors.length; i++) {

		if(anchors[i].className.indexOf("footnote") != -1) {

			var id = anchors[i].href.substr(anchors[i].href.indexOf('#fn')+1);

			anchors[i].onclick = function(){fade(id);};

		}

	}

}	

	



function initSearchBox() { 

	input = document.getElementById("header_searchbox");

	input.onfocus = function(){ if (this.defaultValue==this.value) this.value = ""; };

}





//-----------------------------------------------------------------------------

// Function to allow multiple onload functions to be called

//-----------------------------------------------------------------------------

window.onload = function ()

{

  if (!document.getElementsByTagName) return;

  externalLinks();

	emailCloak();

	initFootnotes();

	initSearchBox();

}

