function IsHex(i)
// return true if chr(i) is in ['0'..'9'] or ['A'..'F']
{ return (((i>47) && (i<58)) || ((i>64) && (i<71))) }

function IsAlphaNum(i)
// return true if chr(i) is in ['0'..'9'] or ['A'..'Z'] or ['a'..'z'] or '_'
{  return (((i>47) && (i<58)) || ((i>64) && (i<91)) || ((i>96) && (i<123)) || (i==95)) }

function IsHex44(s) // s must be upper case string
{ if ((s.length != 9) || (s.charAt(4) != "-")) return false
  else { var temp = s.substring(0,4) + s.substring(5,9);
         for (var i=0; i < 8; i++) if (!IsHex(temp.charCodeAt(i))) return false;
         return true;
}}

function ValidateHex44(n,ed,edname)
{ ed.value = ed.value.toUpperCase();
  if (IsHex44(ed.value)) return true
  else { alert("License "+n+"\nThe " + edname + " is invalid");
         ed.focus();
         return false;
}}

function ValidateDesignation(n,ed)
{ var x = ed.value.length;
  for (var i=0; i < x; i++)
    if (IsAlphaNum(ed.value.charCodeAt(i))) continue
    else { alert('License '+n+'\nThe Designation may contain only letters, digits and underscore.');
           ed.focus();
           return false }; 
  return true;
}

function IsValidLicense(n) // Sn may be empty, however, count will not be incremented
{ with (window.document.forms[0]) {
    var result = true;
    if (eval("S"+n+".value != ''")) {
      var temp =     "ValidateHex44(n,S"+n+",'System ID')"
               + " && ValidateHex44(n,M"+n+",'Modifier')"
               + " && ValidateDesignation(n,D"+n+")";

      if (eval(temp)) count.value++ 
      else result = false; };
    return result;
}}

function ValidateForm()
{ with (window.document.forms[0]) {
    if (! ValidateEmail(email)) return false;

    count.value = 0;
    for (var n = 1; n < 33; n++) {
      if (eval("typeof(S" + n + ") != typeof(S1)")) break;
      if (! IsValidLicense(n)) return false;
    }; // for

    return true;
}}

function RegnString(n)
{ with (window.document.forms[0]) {
    var result = eval('S' + n + '.value'); if (result == '') return result;
        result = result + '/' + eval('M' + n + '.value');
    var temp   = eval('D' + n + '.value');
        if (temp != '') result = result + '/' + temp;
    return result;
}}

function SubmitFormEx()
{ with (document.forms[0]) {

    var temp = '';
    var url  = '';

    count.value = 0;

    for (var n = 1; n < 33; n++) {
      if (eval('typeof(S' + n + ') != typeof(S1)')) break;
      if (! IsValidLicense(n)) return false;
      temp = RegnString(n); if (temp == '') continue;

      if (url == '') url = temp
      else url = url + '%20' + temp;
    };

    if (count.value == '0') {
      alert('You must enter the System ID and Modifier.');
      S1.focus();
      return false; }

    url = 'http://shareit1.element5.com/checkout.html?PRODUCT[160785]='
        + count.value + '&additional1=' + url;

  window.open(url,"MvrkRegn");//,"toolbar=no,menubar=no,scrollbars=yes,status=yes,location=no");
  return true;
}}
