// YHA Utility JavaScript functions

// *******************************************
// SubmitForm
// *******************************************
function SubmitForm(formname, formaction) {
   thisform = document.getElementById(formname);
   thisform.action = formaction;
   thisform.submit();
}

// *******************************************
// GenerateContactRow
// *******************************************
function GenerateContactRow(title, eMail, fullName, desc) {
   document.write('<tr>');

   if (eMail.indexOf('@') == -1) {
      eMail = eMail + '@yorkshireha.org.uk';
   }
   document.write('<td class=\"general\" nowrap>');
   GenerateMailTo(eMail, title);
   document.write('</td>');

   document.write('<td class=\"general\" nowrap>');
   document.write(fullName);
   document.write('</td>');

   document.write('<td class=\"general\" nowrap>');
   document.write(title);
   document.write('</td>');

   document.write('<td class=\"general\">');
   document.write(desc);
   document.write('</td>');

   document.writeln('</tr>');
}

// *******************************************
// GenerateMailTo
// *******************************************
function GenerateMailTo(eMail, fullName) {
   if (eMail.indexOf('@') == -1) {
      eMail = eMail + '@yorkshireha.org.uk';
   }
   document.write('<a href="mailto:' + eMail + '" class="mailto">' + fullName + '</a>');
}

// *******************************************
// OpenArticle
// *******************************************
function postponedCheck(id) {
   var el = document.getElementById('formdatapostponed'+id);
   if (el.checked) {
      document.getElementById('formdatapostponedtext'+id).style.display = '';
   } else {
      document.getElementById('formdatapostponedtext'+id).style.display = 'none';
   }
}

// *******************************************
// OpenArticle
// *******************************************
function OpenArticle(articleId) {
   OpenNewWindow("../news/article.php?article=" + articleId, "YHAArticle");
}

// *******************************************
// OpenGroundDetails
// *******************************************
function OpenGroundDetails(clubId) {
   OpenNewWindow("/venues/venues.php?clubid=" + clubId, "YHAVenue");
}

// *******************************************
// OpenNewWindow
// *******************************************
function OpenNewWindow(url, winId) {
   windowTop      = GetTopLeft("top");
   windowLeft     = GetTopLeft("left");

   windowOptions  = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no";
   windowOptions += ",screenX=" + windowLeft;
   windowOptions += ",screenY=" + windowTop;
   windowOptions += ",left=" + windowLeft;
   windowOptions += ",top=" + windowTop;
   windowOptions += "innerwidth=" + windowWidth;
   windowOptions += ",innerheight" + windowHeight;
   windowOptions += ",width="  + windowWidth;
   windowOptions += ",height=" + windowHeight;

   win = window.open(url, winId, windowOptions);
   win.focus();
}

// *******************************************
// GetTopLeft
// *******************************************
function GetTopLeft(which) {
   screenHeight   = window.screen.availHeight;
   screenWidth    = window.screen.availWidth;
   windowHeight   = 600;
   windowWidth    = 600;
   windowTop      = (screenHeight - windowHeight) / 2;
   windowLeft     = (screenWidth - windowWidth) / 2;

   if ("top" == which) {
      return windowTop;
   }
   else {
      return windowLeft;
   }
}

// *******************************************
// validateMaxArticles
// *******************************************
function validateMaxArticles(evt) {
   var temp = parseInt(window.document.filters.maxarticles.value);
   if (!temp) {
      alert("You must enter a number into the Max Articles field.");
      window.document.filters.maxarticles.value = "5";
      return false;
   }

   window.document.filters.maxarticles.value = temp;
   return true;
}

// *******************************************
// checkNumeric
// *******************************************
function checkNumeric(ev) {
   if (typeof ev == "undefined") ev = window.event;
   var key;
   if (ev.keyCode) {
      key = ev.keyCode;
   } else {
      key = ev.which;
   }

   if (ev.ctrlKey || ev.altKey) {
      return true;
   }

   // Cusror keys
   if (key>=37 && key<=40) {
      return true;
   }

   // End/Home keys
   if (key>=35 && key<=36) {
      return true;
   }

   // Tab/Return/Backspace/Delete keys
   if (key==9 || key==13 || key==8 || key==46) {
      return true;
   }

   // Numerics
   if (key>=48 && key<= 57) {
      return true;
   }

   return false;
}

function fnBlur(ev) {
   if (typeof ev == "undefined") ev = window.event;
   //debugOb(ev.srcElement.parentNode.parentNode);
   hasFocus = ev.srcElement.parentNode.parentNode;
}

function fnKeypress(ev) {
   if (typeof ev == "undefined") ev = window.event;
   var key;
   if (ev.keyCode) {
      key = String.fromCharCode(ev.keyCode);
   }
   else {
      key = String.fromCharCode(ev.which);
   }
   document.getElementById('debug').value = key +","+document.getElementById('debug').value;
   var els = document.getElementsByTagName("a");
   for (var i=0; i<els.length; i++) {
      if (els[i].id == "menu"+key) {
         if (els[i].style.visibility != "hidden") {
//            um.closeAllMenus();
            els[i].focus();
            break;
         }
      }
   }
}

function assignUDMReceivers() {
   var els = document.getElementsByTagName("a");
   for (var i=0; i<els.length; i++) {
      if (els[i].id.substr(0,4) == "menu") {
         els[i].onkeypress = fnKeypress;
      }
   }
   um.addReceiver(fn1, '');
}

function fn1(eventObject, eventCode) {
   document.getElementById('debug').value = eventCode +","+document.getElementById('debug').value;
}

function debugOb(obj) {
   var txt = Array();
   for (att in obj) {
      if (obj[att] != null) {
         txt[txt.length] = att + "=" + obj[att] + "\n";
      }
   }
   txt.sort();
   alert(txt.toString());
}

function setButtonValue(button) {
   var bits = button.split('-');

   if (typeof document.forms.yhaform != "undefined") {
      document.forms.yhaform.button.value = bits[0];
      if (bits.length == 2) document.forms.yhaform.option.value = bits[1];
   }
}

function toggleFieldHelp(id) {
   var el = document.getElementById(id);
   el.className = el.className == "fieldhelphidden" ? "fieldhelp" : "fieldhelphidden";
   return true;
}

function findFirstFocusable() {
   var allelements = document.getElementsByTagName("*");
   var foundPageStart  = false;
   for (var i=0; i<allelements.length; i++) {
      if (foundPageStart ) {
         //alert(allelements[i].tagName);
         if (allelements[i].tagName.match(/BUTTON|INPUT|SELECT/i)) {
            if (allelements[i].type != "hidden") {
               //alert(allelements[i].id);
               allelements[i].focus();
               break;
            }
         }
      } else {
         if (allelements[i].id == "pagestart") {
            foundPageStart = true;
         }
      }
   }
}
