/*
function checkinputs accepts the form submitted from and 3 arrays of input types:
e.g. checkinputs(thisform, textboxnames, radiobuttonsname, checkboxidentifier);
thisform             the form (generally "this") containing the inputs being checked and being submitted

textboxnames         an array of names of text inputs to be checked for having a value or not

radiobutton names    an array of radio button names to check to make sure one item is selected in that set of radio buttons
Alt tags may be used and will be displayed in the alert, otherwise the field name will display in the alert.

checkbox identifier  an array of the start of checkbox names to make sure at least one check box in the set is selected

*/

var el;
function checkinputs(thisform)
{
el = thisform.elements;
var required=arguments[1], haveemail=false;

for (var ri=0;ri<required.length;ri++)
  {
  if (required[ri].toLowerCase()=='email') haveemail=true;
  if (el[required[ri]].type.indexOf('select') > -1)
    {
    if (el[required[ri]].selectedIndex == 0)
      {
    var alttag='';
    if ((el[required[ri]].alt=='undefined') || (el[required[ri]].alt==null)) var alttag = '';
    else
      {
      alttag=required[ri];
      if (el[required[ri]].alt.length > 0) alttag = ' ' + el[required[ri]].alt;
      }
      alert('You must select from the' + alttag + ' dropdown list');
      el[required[ri]].focus();
      return false;
      }
    }
  else if (el[required[ri]].value.length==0)
    {
    var alttag=required[ri];
    if (el[required[ri]].alt.length > 0) alttag = el[required[ri]].alt;
    alert('You must fill in ' + alttag + ' information');
    el[required[ri]].focus();
    return false;
    }
  }

if (haveemail==true) {if (checkemail(el['email'])==false) return false;}
if (arguments.length > 2) {if (checkradio(arguments[2]) == false) return false;}
if (arguments.length > 3) {if (checkcheckboxes(arguments[3]) == false) return false;}

return true;
}


function checkemail(emailinput)
{
var email=emailinput.value.toLowerCase();
if (email.length > 0)
  {
  if (email.indexOf('@') < 1)
    {
    alert('Invalid email address');
    emailinput.select();
    emailinput.focus();
    return false;
    }

  var emaildomain = email.substr(email.indexOf('@') + 1);
  if (emaildomain.indexOf('.') < 2)
    {
    alert('Invalid email address');
    emailinput.select();
    emailinput.focus();
    return false;
    }

  var emailextension = email.substr(email.lastIndexOf('.') + 1);
  if (emailextension.length < 2)
    {
    alert('Invalid email address');
    emailinput.select();
    emailinput.focus();
    return false;
    }


  var invalid=' \n\r\t';
  for (ii=0;ii<invalid.length;ii++)
    {
    var oneinvalid=invalid.charAt(ii);
    if (email.indexOf(oneinvalid) > -1)
      {
      alert('Invalid email address');
      emailinput.select();
      emailinput.focus();
      return false;
      }
    }

  }
return true;
}

function checkradio(radioarrays)
{
for (var ri=0;ri<radioarrays.length;ri++)
  {
  var oneradio=el[radioarrays[ri]];
  var radiochecked=false;
  var alttag=radioarrays[ri];
  for (var oi=0;oi<oneradio.length;oi++)
    {
    if (oneradio[oi].checked==true)
      {
      radiochecked=true;
      if (oneradio[oi].name.toLowerCase()=='paymentmethod')
        {
        if (oneradio[oi].value.toLowerCase()=='credit card' || oneradio[oi].value.toLowerCase()=='creditcard')
          {
          if (checkcreditcard(oneradio[oi])==false) return false;
          }
        }
      }
    }

  if (radiochecked==false)
    {
    if (oneradio[0].alt.length > 0) alttag = oneradio[0].alt;
    alert('You must put a dot in one of the circles for ' + alttag + ' field');
    return false;
    }
  }
return true;
}

function checkcheckboxes(checkboxarray)
{
for (var ci=0;ci<checkboxarray.length;ci++)
  {
  var onecheck=checkboxarray[ci];
  for (var ei=0;ei<el.length;ei++)
    {
    var checkmarks=false, oneel=el[ei];
    if (oneel.name.indexOf(onecheck) == 0 && oneel.type == 'checkbox')
      {
      if (oneel.checked) {checkmarks=true;  break;}
      }
    }
  if (checkmarks==false) {alert('You must select at least one checkbox');  return false;}
  }
return true;
}


//this function creates a radio button effect for a set of checkboxes
// that have the same beginning to the element name
function checkonecbox(cbox)
{
var el=cbox.form.elements;
var cboxchecked=cbox.checked, cboxname=cbox.name.substr(0, cbox.name.length-1);
if (cboxchecked==false) return;

for (var ei=0;ei<el.length;ei++)
  {
  if (el[ei].name.indexOf(cboxname) == 0 && el[ei].type == 'checkbox') el[ei].checked=false;
  }

cbox.checked=true;
}


function checkcreditcard(oneradio)
{

var el=oneradio.form.elements;

if (el['cardtype'].selectedIndex==0)
  {
  alert('Credit Card type not specified');
  el['cardtype'].focus();
  return false;
  }

if (el['cardname'].value.length==0)
  {
  alert('Credit Card name not specified');
  el['cardname'].focus();
  return false;
  }

if (el['cardnumber'].value.length==0)
  {
  alert('Credit Card number not specified');
  el['cardnumber'].focus();
  return false;
  }

if (el['expdatemonth'].selectedIndex==0)
  {
  alert('Credit Card expiration month not specified');
  el['expdatemonth'].focus();
  return false;
  }

if (el['expdateyear'].selectedIndex==0)
  {
  var today=new Date();
  var thismonth = today.getMonth() + 1;
  if (el['expdatemonth'].selectedIndex < thismonth)
    {
    alert('Credit Card expiration date not valid');
    el['expdatemonth'].focus();
    return false;
    }
  }

return true;
}
