/****************************************************************

Canvas Backup - "canvasbackup.com"

Written by Robert Spriggs for CanvasDesign, (c)2007 Robert Spriggs and Canvas Design, all rights reserved

The code will remain the copyright of Robert Spriggs and Cavas Design, but may be freely modified
for the Canvas Backup website only (currently canvasbackup.com),
or further developed for that website only, without additional permission.

The code will only be used on one server to host one domain's site at any
one time and will not be resold (unless sold as part of the website and services,
in which case this copyright condition continues and is passed to the new owner)
or made publicly available.

Robert Spriggs and Canvas Design reserves the right to use the code elsewhere, but will not divulge
that the code is used for the Canvas Backup site or make the
code publicly available.

No part of this copyright message may be edited or removed.

*****************************************************************/


function MyValidate(frmID, aFields) {

  frm = document.getElementById(frmID);
  if (!frm) {
      return true;
  }

  for(i=0;i<aFields.length;i++) {
    if (aFields[i][2] == 'I' || aFields[i][2] == 'S' || aFields[i][2] == 'D') {
      var MyValue = document.getElementById(aFields[i][0]).value;
      if (aFields[i][3]) {  // Is compulsory
        if (MyValue=="") {
          alert (aFields[i][1] + ' must be entered');
          document.getElementById(aFields[i][0]).focus();
          return false;
        }
      }
      if (aFields[i][4]) {  // Has Limits
        if (aFields[i][2] == 'I') {
          MyValue = parseFloat(MyValue);
          if (MyValue<aFields[i][5] || MyValue>aFields[i][6]) {
            alert (aFields[i][1] + ' must be between ' + aFields[i][5] + ' and ' + aFields[i][6]);
            document.getElementById(aFields[i][0]).focus();
            document.getElementById(aFields[i][0]).select();
            return false;
          }
        } else if (aFields[i][2] == 'S') {
          MyValue = MyValue.length;
          if (MyValue<aFields[i][5] || MyValue>aFields[i][6]) {
            alert (aFields[i][1] + ' must be between ' + aFields[i][5] + ' and ' + aFields[i][6] + ' characters long');
            document.getElementById(aFields[i][0]).focus();
            document.getElementById(aFields[i][0]).select();
            return false;
          }
        }
      }
    } else if (aFields[i][2] == 'R') {
      if (aFields[i][3]) {
        var MyOption = -1;
        var MyRadio = frm.elements[aFields[i][0]];
        for (j=MyRadio.length-1; j > -1; j--) {
          if (MyRadio[j].checked) {
            MyOption = j;
            j = -1;
          }
        }
        if (MyOption == -1) {
          alert ('You must select one of the options for ' + aFields[i][1]);
          document.getElementById(aFields[i][0]).focus();
          return false;
        }
      }
    } else if (aFields[i][2] == 'C') {
      var MyValue = document.getElementById(aFields[i][0]).checked;
      if (aFields[i][3]) {
        if (MyValue==false) {
          alert (aFields[i][1] + ' must be checked');
          document.getElementById(aFields[i][0]).focus();
          return false;
        }
      }
    }
  }
  return ShowBusy(frm, frmID);
}

var submitted = 0;

function ShowBusy(frm, frmID) {
  if (submitted) {
    alert("Form already submitted, please be patient");
    return false;
  }
  submitted = 1;
  if (frm != null) {
    for (i=0; i<frm.elements.length; i++){
//      frm.elements[i].setAttribute('disabled', true);
      frm.elements[i].style.color = "#dddddd";
    }
  }

  var btn = window.event ? window.event.srcElement : null
  if (btn) {
    btn.setAttribute('value', 'Busy');
  }

  msg = document.getElementById('btns' + frm.id);
  if (msg) {
    msg.style.position = "absolute";
    msg.style.left = "-2000px";
    msg.style.width = "0";
  }

  msg = document.getElementById('msg' + frm.id);
  if (msg) {
    msg.style.visibility = "visible";
    msg.style.display = "block";
  }

  setTimeout('document.images["bsy'+frmID+'"].src = document.images["bsy'+frmID+'"].src', 200);

  return true;
}

