﻿///Toggle the visibility of an HTML object
function toggleView(obj) {
   var currentView = obj.style.display;
   obj.style.display = currentView == "block" ? "none" : "block";
}

/***************
Toggles visibility for specific pages that have 
multiple Div Tags that need to show / hide on Mouseover
param : what == the item to SHOW
param : num == the number of div tags to HIDE
****************/
function swap(num, what) {
   for (var i = 0; i < num; i++) {
      document.getElementById('swap' + i).style.display = 'none'; //Hide All
   }
   document.getElementById('swap' + what).style.display = 'block'; //Show the one
}


/*****************
Toggles the image to an image within the same folder as the originalSrc
img == The Image object
newImg == The image object within the same folder as the Current Src
i.e. someImage.jpg -- No other pathing required
Solves problems with ASP.NET Image Control Toggling and works with standard images too
******************/
function toggleImage(img, newImg) {
   img.src = img.src.replace(/\w+\.\w{3}$/gi, newImg);
}

/*****************
Resizes an image to a certain percentage aspect ratio
img == The Image Object
percent == The total percentage to be removed from the total Size Ratio
example : 80 will resize the image to remove 80% of its original size
*******************/
function getThumbNail(img, percent) {
   img.height -= (percent / 100) * img.height;
   img.Width -= (percent / 100) * img.Width;
}

/****************
Following code is used to add automatic tabbing functionality to forms
*****************/
function gotoNext(control, contentLength) {
   if (control.value.length >= contentLength) {
      var controlIndex = getIndex(control);
      control.form[new Number(controlIndex + 1)].focus();
   }

   return true;
}

function getIndex(input) {
   input.index = 0;
   var currControl;
   for (var i = 0; i < input.form.length; i++) {
      currControl = input.form[i];
      if (currControl.name == input.name && currControl.id == input.id) {
         input.index = i;
         break;
      }
   }

   return input.index;
}
/****************
END automatic tabbing functionality to forms
*****************/

/****************
Form Validation Routines
*****************/
function validate(form) {
   var bValidForm = new Boolean(true);
   var formElement;
   var bElementRequired = new Boolean(false);
   var bElementHasValue = new Boolean(false);
   for (var i = 0; i < form.elements.length; i++) {
      formElement = form.elements[i];
      if (formElement.type.toLowerCase() == "submit" || formElement.type.toLowerCase() == "button") continue;
      if (formElement.getAttribute("required") == null) {
         var controlName = (formElement.id == "" || formElement.id == null) ? formElement.name : formElement.id;
         alert("All Form Elements require a 'Required Attribute'. Missing on Element ID: " + controlName); //For Developers
         bValidForm = false;
      }
      else {
         switch (formElement.type.toLowerCase()) {
            case "select-one":
               bElementHasValue = new Number(formElement.selectedIndex) > 0;
               break;
            case "checkbox":
               bElementHasValue = formElement.checked;
               break;
            case "radio":
               bElementHasValue = formElement.checked;
               break;
            default:
               bElementHasValue = formElement.value != "";
               break;
         }

         bElementRequired = formElement.getAttribute("required").toLowerCase() == "true";
         if (bElementRequired && !bElementHasValue) {
            //Flag the mistake for the user by highlighting							
            toggleHighLight(formElement);
            formElement.setAttribute("onfocus", "className = 'notHighlighted')"); //Mozilla Compatible White

            bValidForm = false;
         }
      }
   }

   if (!bValidForm) alert("Required fields are missing and highlighted. Please complete accordingly.");

   return bValidForm;
}

function toggleHighLight(obj) {
   obj.className = (obj.className == "highlighted") ? "notHighlighted" : "highlighted";
}
/****************
End Form Validation Routines
*****************/


///Macromedia CRAP functions. No time to change these but ALL can be erased and
///use toggleImage instead. MUCH SIMPLIER!
function MM_swapImgRestore() { //v3.0
   var i, x, a = document.MM_sr; 
   for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_preloadImages() { //v3.0
   var d = document; if (d.images) {
      if (!d.MM_p) d.MM_p = new Array();
      var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
         if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; } 
   }
}

function MM_findObj(n, d) { //v4.01
   var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
      d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
   }
   if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
   for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
   if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
   var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
      if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}