
//Title: 	Global Javascript Functions for Senate of Canada 
//Author:	jay.west for the Mediabox
//Contact:	creative@jaywest.com
//Updated:	Tuesday, July 25, 2006



// Opens a link in a new window when class = linkExternal

function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
	if (links[i].className.match("linkExternal")) {
      links[i].onclick = function() {
        window.open(this.href);
        return false;
      }
    }
  }
}


function externalLinks() {
	if(!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName('a');
	for(var i=0; i<links.length; i++) {
	//the second argument in getAttribute is required for IE.
	//without it, every link will contain http://
	var source = links[i].getAttribute('href', 2);
	if(source.match('http://') || source.match('https://')) {
		links[i].setAttribute('target', '_blank');
	}
	}
}





// Sets the class of an input field to "fieldFocus" on focus or removes the className on blur.
// Will also clear the default field contents

function findField() {
  if (!document.getElementsByTagName) return false;
  var fieldList = document.getElementsByName("txtKeywords");
  for ( var i=0; i < fieldList.length; i++) {
    fieldList[i].onfocus = function() {
      this.className ="fieldFocus";
	  this.value = "";
    }
	fieldList[i].onblur = function() {
      this.className ="";
    }
  }
}




// adds history.go function to cancel buttons

function cancelButton() {
  if (!document.getElementsByName) return false;
  var cancel = document.getElementsByName("cancel");
  for (var i=0; i < cancel.length; i++) {
    if (cancel[i].name.match("cancel")) {
      cancel[i].onclick = function() {
		window.history.go(-1)
        //window.open(this.href);
        return false;
      }
    }
  }
}



// Add the OpenWYSIWYG editor to all textarea fields

function addWYSIWYGtools() {
	if (!document.getElementsByTagName) return false;
	var textareaList = document.getElementsByTagName("textarea");
	for (var i=0; i < textareaList.length; i++) {
		var strFieldName = textareaList[i].getAttribute('id');
		if (strFieldName != "comments" && strFieldName != "comments2" && strFieldName != "txtSummaryEn" && strFieldName != "txtSummaryFr") {  // don't add the tools to the comment form
			generate_wysiwyg(strFieldName);
		}
	}
}



// Opens a link in a new window when class = linkExternal

function openHelp() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("help")) {
      links[i].onclick = function() {
		url = this.getAttribute("href");
        window.open(url,'popupHelp','width=350,height=400,menubar=yes,location=no,status=no,scrollbars=yes,resizable=yes');
		return false;
      }
    }
  }
}


// Run the functions once the page has loaded: 

window.onload=function(){
	doPopups();
	findField();
	cancelButton();
	addWYSIWYGtools();
	openHelp();
	externalLinks();
}
