/*
    Add an eventListener (provided as-is)
    by Scott Andrew
*/
    function addEvent(obj, evType, fn){
    	if (obj.addEventListener){
    		obj.addEventListener(evType, fn, true);
    		return true;
    	} else if (obj.attachEvent){
    		var r = obj.attachEvent("on"+evType, fn);
    		return r;
    	} else {
    		return false;
    	}
    }

/*
   Add to favourites
*/
function addToFavourites() {
   window.external.AddFavorite(window.location.href, window.title)
}

/*
   Print current page
*/

/*
This script is written by Eric (Webcrawl@usa.net)
For full source code, installation instructions,
100's more DHTML scripts, and Terms Of
Use, visit dynamicdrive.com
*/

function printpage(){
   if (window.print) {
       window.print();
   } else {
      var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
      document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
      WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";
   }
}

function preventVote() {
   window.alert('Sorry, you have already voted in this poll. Only one vote per person.');

   return false;
}

function validateVote() {

   var oQuickPoll =
      document.getElementById("quickpoll");

   var aChoices =
      oQuickPoll.getElementsByTagName("INPUT");

   var bSelected =
      false;

   for (var i=0; i<aChoices.length; i++) {
      if (aChoices[i].checked) {
         bSelected = true;
         break;
      }
   }

   if (!bSelected)
      window.alert("Please choose an option before clicking vote button.");

   return bSelected;
}

function checkall(oCheckbox) {
   var oTable = findAncestor(oCheckbox, "TABLE");

   if (oTable) {
      var aCheckboxes =
         oTable.getElementsByTagName("INPUT");

      for (var i=0; i<aCheckboxes.length; i++) {
         if (aCheckboxes[i].name == "selected")
            aCheckboxes[i].checked =  oCheckbox.checked;
      }
   }
}

function findAncestor(oStart, sName) {
   var oAncestor = null;

   if (oStart == null || oStart.nodeName == sName)
      oAncestor = oStart;
   else
      oAncestor = findAncestor(oStart.parentNode, sName);
   return oAncestor;
}

function validateRequired(sCaption, sValue) {
   return sValue == null || sValue == "" ? "- Please supply a value for " + sCaption + "\r\n" : "";
}

function validateCheckbox(sCaption, sValue) {
   return sValue == null || sValue == "" ? "- You must tick the checkbox for " + sCaption + "\r\n" : "";
}

function validateMaxLength(sCaption, sValue, iMax) {
   return sValue != null && sValue.length > iMax ?
      "- Please shorten text for " + sCaption + ". Max length " + iMax + ", currently " + sValue.length + "\r\n"  : "";
}

function validateDecimal(sCaption, sValue) {
   return ""; // TODO
}

function entersubmit(oEvent, sClientId) {
   if (oEvent && oEvent.keyCode == 13) {
      var oTarget =
         document.getElementById(sClientId);

      oEvent.returnValue = false;
      oEvent.cancel = true;

      oTarget.click();
   }
   else
      return true;
}

function clearRadio(sTableId) {
   var oTable =
      document.getElementById(sTableId);
   var aButtons =
      oTable.getElementsByTagName("INPUT");
   for (var i=0; i<aButtons.length; i++)
      aButtons[i].checked = null;
}

function tinyPopup(sUrl) {
   window.open(sUrl, null, 'top=100,left=100,width=300,height=200,scrollbars=yes,resize=yes,toolbar=no');
}

function smallPopup(sUrl) {
   window.open(sUrl, null, 'top=100,left=100,width=450,height=400,scrollbars=yes,resize=yes,toolbar=no');
}

function mediumPopup(sUrl) {
   window.open(sUrl, null, 'top=100,left=100,width=600,height=400,scrollbars=yes,resize=yes,toolbar=no');
}

