//
//  The FormCheck function iterates through each field in
//  the specified form checking for user-defined validation
//  criteria and, for each one found, performing the associated
//  check.
//
//  Note that it will only report a single field in error each
//  time and will reset focus to that field.
//

function FormCheck(currForm)
{
    for (var intSub = 0; intSub < currForm.length; intSub++)
                                                    // For each field in the form
    {
        var currField = currForm.elements[intSub];  // Get the current field
        // Set up the field name in case we need to report an error

        var currName = "Field : " + currField.name; // Default to internal name
        if (typeof (currField.errname) != 'undefined'
         || currField.getAttribute("errname") != null)
       {
          if (typeof(currField.errname) != 'undefined')
          {
            currName = currField.errname;
          }
          else if (currField.getAttribute("errname") != null)
          {
            currName = currField.getAttribute("errname");
          }
       }
                                                    // But over-ride with error name if we have one

        // A minlen value indicates that the field must have at least that
        // many characters.  A value of 1 simply indicates the field is manadatory.
        if (typeof(currField.minlen) != 'undefined' || currField.getAttribute("minlen") != null)
        {
          var minLen = 0;

          if (typeof(currField.minlen) != 'undefined')
          {
            minLen = currField.minlen;
          }
          else if (currField.getAttribute("minlen") != null)
          {
            minLen = currField.getAttribute("minlen");
          }

          if(minLen > currField.value.length)
                  // which fails
          {
            var msg = '';
            if (minLen > 1)               // If specified length was greater than 1
            {
              msg = currName + " must have at least "+currField.minlen+" characters";
                      // output specific message
            }
            else                                    // Else (just mandatory field)
            {
              msg = currName + " must be specified";
                      // Output generic message
            }
            alert(msg);
            currField.focus();                      // Reset focus to field
            return false;                           // And abort the submit
          }
        }

        // A usertype value indicates that the field is of a particular user-defined
        // format and must be checked as such (if it is not empty).

        if ((typeof (currField.usertype) != 'undefined' || currField.getAttribute("usertype") != null)
                                                    // If we have a user-defined type
         && (currField.value.length > 0))           // and the field has a value
        {
            var usertype = "";

            if (typeof (currField.usertype) != 'undefined')
            {
              usertype = currField.usertype;
            }
            else if (currField.getAttribute("usertype") != null)
            {
              usertype = currField.getAttribute("usertype");
            }

            if  (usertype == "posint")    // If we require it to be a positive integer
            {
                if  (!CheckPosint(currField.value)) // If it fails the check
                {
                    alert(currName + " must be a positive whole number");
                                                    // Output an error
                    currField.focus();              // Reset focus to field
                    return false;                   // And abort the submit
                }
            }
            else if (usertype == "price") // Else if a price
            {
                if  (ConvertPrice(currField.value) == -1)
                                                    // If it fails the check
                {
                    alert(currName + " must be a valid price");
                                                    // Output an error
                    currField.focus();              // Reset focus to field
                    return false;                   // And abort the submit
                }
            }
            else if (usertype == "date") // Else if a date
            {
                if  (CheckDate(currField.value) != 0)
                                                    // If it fails the check
                {
                    alert(currName + " must be a valid date of the form dd/mm/yyyy ("+CheckDate(currField.value)+")");
                                                    // Output an error
                    currField.focus();              // Reset focus to field
                    return false;                   // And abort the submit
                }
            }
            else if (usertype == "email") // Else if an e-mail address
            {
                if  (!CheckEmail(currField.value))  // If it fails the check
                {
                    alert(currName + " is invalid"); // Output an error
                    currField.focus();              // Reset focus to field
                    return false;                   // And abort the submit
                }
            }
            else if (usertype == "postcode")
                                                    // Else if a postcode
            {
                if  (!CheckPostcode(currField.value)) // If it fails the check
                {
                    alert(currName + " is invalid"); // Output an error
                    currField.focus();              // Reset focus to field
                    return false;                   // And abort the submit
                }
            }
            else if (usertype == "mixeddrop")
                                                    // Else if a drop-down with mixed system and user values
            {
                if  (currField.value.substring(0,2) == "$$")
                                                    // If a system value
                {
                    alert("Please select a valid " + currName);
                                                    // Output an error
                    currField.focus();              // Reset focus to field
                    return false;                   // And abort the submit
                }
            }
        }

        // An either value indicates that either the current field or the target of
        // the either (or both) are specified

        if (typeof (currField.either) != 'undefined' ||
          currField.getAttribute("either") != null) // If we have an EITHER match
        {
            var either = "";

            if (typeof (currField.either) != 'undefined')
            {
              either = currField.either;
            }
            else if (currField.getAttribute("either") != null)
            {
              either = currField.getAttribute("either");
            }

            var checkField = currForm[either];
                                                    // Get field to check against
            var checkName = "field : " + checkField.name;
                                                    // Default name to internal name
            if (typeof(checkField.errname) != 'undefined') checkName = checkField.errname;
                                                    // But over-ride with error name if we have one
            if ((currField.value.length == 0)       // If we don't have a value
             && (checkField.value.length == 0))     // and the other field doesn't have one either
            {
                alert("Either " + currName + " or " + checkName + " must be specified");
                currField.focus();                  // Reset focus to field
                return false;
            }
        }

        // A matchfield value indicates that the current field must match the target of
        // the matchfield

        if (typeof(currField.matchfield) != 'undefined' ||
          currField.getAttribute("matchfield") != null)  // If we have an MATCHFIELD match
        {
            var matchfield = "";

            if (typeof (currField.matchfield) != 'undefined')
            {
              matchfield = currField.matchfield;
            }
            else if (currField.getAttribute("matchfield") != null)
            {
              matchfield = currField.getAttribute("matchfield");
            }

            var checkField = currForm[matchfield];
                                                    // Get field to check against
            var checkName = "field : " + checkField.name;
                                                    // Default name to internal name
            if (typeof(checkField.errname) != 'undefined') checkName = checkField.errname;
                                                    // But over-ride with error name if we have one
            if (currField.value != checkField.value) // If values don't match
            {
                alert("Value specified for " + currName + " does not match that for " + checkName);
                currField.focus();                  // Reset focus to field
                return false;
            }
        }

        // A gt value indicates that the field value must be greater than that
        // of the other specified field

        if ((typeof (currField.gt) != 'undefined' ||
          currField.getAttribute("gt") != null)  // If we have a GT match
         && (currField.value.length > 0))           // and the field has a value

        {
            var gt = 0;

            if (typeof (currField.gt) != 'undefined')
            {
              gt = currField.gt;
            }
            else if (currField.getAttribute("gt") != null)
            {
              gt = currField.getAttribute("gt");
            }

            var checkField = currForm[gt]; // Get field to check against
            var checkName = "field : " + checkField.name;
                                                    // Default name to internal name
            if (typeof(checkField.errname) != 'undefined') checkName = checkField.errname;
                                                    // But over-ride with error name if we have one
            if ((checkField.value.length > 0)       // If target field has a value
             && (currField.value*1.0 <= checkField.value*1.0))
                                                    // and check fails
            {
                alert(currName + " must be greater than " + checkName);
                currField.focus();                  // Reset focus to field
                return false;
            }
        }

        // A gtprice value indicates that the field value must be greater than that
        // of the other specified field, each of which is a price

        if ((typeof (currField.gtprice) != 'undefined' ||
          currField.getAttribute("gtprice") != null)
                                                    // If we have a gtprice match
         && (currField.value.length > 0))           // and the field has a value

        {
            var gtprice = 0;

            if (typeof (currField.gtprice) != 'undefined')
            {
              gtprice = currField.gtprice;
            }
            else if (currField.getAttribute("gtprice") != null)
            {
              gtprice = currField.getAttribute("gtprice");
            }

            var checkField = currForm[gtprice];
                                                    // Get field to check against
            var checkName = "field : " + checkField.name;
                                                    // Default name to internal name
            if (typeof(checkField.errname) != 'undefined') checkName = checkField.errname;
                                                    // But over-ride with error name if we have one
            if ((checkField.value.length > 0)       // If target field has a value
             && (ConvertPrice(currField.value) <= ConvertPrice(checkField.value)))
                                                    // and check fails
            {
                alert(currName + " must be greater than " + checkName);
                currField.focus();                  // Reset focus to field
                return false;
            }
        }

        // A ge value indicates that the field value must be greater than that
        // of the other specified field

        if ((typeof (currField.ge) != 'undefined' ||
          currField.getAttribute("ge") != null)  // If we have a GE match
         && (currField.value.length > 0))           // and the field has a value

        {
            var ge = 0;

            if (typeof (currField.ge) != 'undefined')
            {
              ge = currField.ge;
            }
            else if (currField.getAttribute("ge") != null)
            {
              ge = currField.getAttribute("ge");
            }

            var checkField = currForm[ge]; // Get field to check against
            var checkName = "field : " + checkField.name;
                                                    // Default name to internal name
            if (typeof (checkField.errname) != 'undefined') checkName = checkField.errname;
                                                    // But over-ride with error name if we have one
            if ((checkField.value.length > 0)       // If target field has a value
             && (currField.value*1.0 < checkField.value*1.0))
                                                    // and check fails
            {
                alert(currName + " must be greater than or equal to " + checkName);
                currField.focus();                  // Reset focus to field
                return false;
            }
        }

        // A maxvalue value indicates that the field value must not be greater than
        // the specified value

        if ((typeof (currField.maxvalue) != 'undefined' ||
          currField.getAttribute("maxvalue") != null)
                                                    // If we have a maximum match
         && (currField.value.length > 0))           // and the field has a value

        {
            var maxvalue = 0;

            if (typeof (currField.maxvalue) != 'undefined')
            {
              maxvalue = currField.maxvalue;
            }
            else if (currField.getAttribute("maxvalue") != null)
            {
              maxvalue = currField.getAttribute("maxvalue");
            }

            if (currField.value*1.0 > maxvalue*1.0)
                                                    // If value is too large
            {
                if (typeof (currField.minvalue) != 'undefined'||
                currField.getAttribute("minvalue") != null)
                                                    // If we have a minimum value as well
                {
                  var minvalue2 = 0;

                  if (typeof (currField.minvalue) != 'undefined')
                  {
                    minvalue2 = currField.minvalue;
                  }
                  else if (currField.getAttribute("minvalue") != null)
                  {
                    minvalue2 = currField.getAttribute("minvalue");
                  }

                  alert(currName + " must be in range " + minvalue2 + " to " + maxvalue);
                                                    // output a decent message
                }
                else                                // Else (just a maximum)
                {
                    alert(currName + " must not be greater than " + maxvalue);
                                                    // output a simple message
                }
                currField.focus();                  // Reset focus to field
                return false;
            }
        }

        // A minvalue value indicates that the field value must not be less than
        // the specified value

        if ((typeof (currField.minvalue) != 'undefined' ||
          currField.getAttribute("minvalue") != null)
                                                    // If we have a minimum match
         && (currField.value.length > 0))           // and the field has a value

        {
            var minvalue = 0;

            if (typeof (currField.minvalue) != 'undefined')
            {
              minvalue = currField.minvalue;
            }
            else if (currField.getAttribute("minvalue") != null)
            {
              minvalue = currField.getAttribute("minvalue");
            }

            if (currField.value*1.0 < minvalue*1.0)
                                                    // If value is too small
            {
                if (typeof (currField.maxvalue) != 'undefined' ||
                  currField.getAttribute("maxvalue") != null)
                                                    // If we have a maximum value as well
                {
                  var maxvalue2 = 0;

                  if (typeof (currField.maxvalue) != 'undefined')
                  {
                    maxvalue2 = currField.maxvalue;
                  }
                  else if (currField.getAttribute("maxvalue") != null)
                  {
                    maxvalue2 = currField.getAttribute("maxvalue");
                  }

                  alert(currName + " must be in range " + minvalue + " to " + maxvalue2);
                                                    // output a decent message
                }
                else                                // Else (just a maximum)
                {
                    alert(currName + " must not be less than " + minvalue);
                                                    // output a simple message
                }
                currField.focus();                  // Reset focus to field
                return false;
            }
        }

        // A number attribute means it is any old form of number.  This is (possibly)
        // used by Admin, but the End-User code only uses the more precise checks
        // on the attribute "usertype" (see above)

        if ((typeof (currField.number) != 'undefined' ||
          currField.getAttribute("number") != null)
          && (currField.value.length >0))
        {

            var value = 0;
            var dp = 0;

            if (typeof (currField.dp) != 'undefined')
            {
              dp = currField.dp;
            }
            else if (currField.getAttribute("dp") != null)
            {
              dp = currField.getAttribute("dp");
            }

            if (dp > 0)
                value = parseFloat(currField.value);
            else
                value = parseInt(currField.value);

            if (isNaN(value))
            {
                alert("Field :  must be a number");
                currField.focus();
                return false;
            }
            else
            {
                if (dp > 0)
                {
                    value = Math.round(value*Math.pow(10, currField.dp))/Math.pow(10, dp);
                }
                currField.value = value;
            }
            if (typeof (currField.max) != 'undefined' ||
              currField.getAttribute("max") != null)
            {
              var max = 0;
              if (typeof (currField.max) != 'undefined')
              {
                max = currField.max;
              }
              else if (currField.getAttribute("max") != null)
              {
                max = currField.getAttribute("max");
              }

              if ((max > 0) && (value*1.0 > max*1.0))
              {
                  alert("Field : "+currField.value+" must be less than " + max);
                  currField.focus();
                  return false;
              }
            }
            if (typeof (currField.min) != 'undefined' ||
              currField.getAttribute("min") != null)
            {
              var min = 0;
              if (typeof (currField.min) != 'undefined')
              {
                min = currField.min;
              }
              else if (currField.getAttribute("min") != null)
              {
                min = currField.getAttribute("min");
              }

              if (min*1.0 > min*1.0)
              {
                  alert("Field :  must be greater than "+min);
                  currField.focus();
                  return false;
              }
            }
        }
    }

    return true;
}

// Check the number is a positive number
function CheckPosint(s)
{
    var valid = "0123456789";
    var ok = true;
    var temp;
    for (var isub=0; isub < s.length; isub++)
    {
        temp = "" + s.substring(isub, isub+1);
        if (valid.indexOf(temp) == "-1") ok = false;
    }
    return ok;
}

// Check the number is a valid postcode.  We don't actually know
// what that means so we'll just check we have a valid locality
// code followed by a number and that the field contains only
// numbers, letters and spaces

function CheckPostcode(s)
{
    var checkStr = s.toUpperCase();                 // Convert to upper case
    var validnums = "0123456789";
    var validletters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var temp;
    for (var isub=0; isub < checkStr.length; isub++)
    {
        temp = "" + checkStr.substring(isub, isub+1);
        if ((temp != " ") && (validnums.indexOf(temp) == "-1") && (validletters.indexOf(temp) == "-1")) return false;
                                                    // If characters is neither alphanumeric nor space, return error
    }

    // Check for single letter postcodes

    temp = checkStr.substring(1,2);                 // Get second character
    if  (validnums.indexOf(temp) != "-1")           // If it's a digit
    {
        temp = checkStr.substring(0,1);             // Get first character
        if  ((temp == "B")
         ||  (temp == "E")
         ||  (temp == "G")
         ||  (temp == "L")
         ||  (temp == "M")
         ||  (temp == "N")
         ||  (temp == "S")
         ||  (temp == "W"))                         // If valid 1-letter locality
        {
            return true;                            // return success
        }
        else
        {
            return false;
        }
    }

    // Check for double letter postcode localities

    temp = checkStr.substring(2,3);                 // Get second character
    if  (validnums.indexOf(temp) != "-1")           // If it's a digit
    {
        temp = checkStr.substring(0,2);             // Get first two characters
        if  ((temp == "AB")
         ||  (temp == "AL")
         ||  (temp == "BA")
         ||  (temp == "BB")
         ||  (temp == "BD")
         ||  (temp == "BH")
         ||  (temp == "BL")
         ||  (temp == "BN")
         ||  (temp == "BR")
         ||  (temp == "BS")
         ||  (temp == "CA")
         ||  (temp == "CB")
         ||  (temp == "CF")
         ||  (temp == "CH")
         ||  (temp == "CM")
         ||  (temp == "CO")
         ||  (temp == "CR")
         ||  (temp == "CT")
         ||  (temp == "CV")
         ||  (temp == "CW")
         ||  (temp == "DA")
         ||  (temp == "DD")
         ||  (temp == "DE")
         ||  (temp == "DG")
         ||  (temp == "DH")
         ||  (temp == "DL")
         ||  (temp == "DN")
         ||  (temp == "DT")
         ||  (temp == "DY")
         ||  (temp == "EC")
         ||  (temp == "EH")
         ||  (temp == "EN")
         ||  (temp == "EX")
         ||  (temp == "FK")
         ||  (temp == "FY")
         ||  (temp == "GL")
         ||  (temp == "GU")
         ||  (temp == "HA")
         ||  (temp == "HD")
         ||  (temp == "HG")
         ||  (temp == "HP")
         ||  (temp == "HR")
         ||  (temp == "HU")
         ||  (temp == "HX")
         ||  (temp == "IG")
         ||  (temp == "IP")
         ||  (temp == "IV")
         ||  (temp == "KA")
         ||  (temp == "KT")
         ||  (temp == "KW")
         ||  (temp == "KY")
         ||  (temp == "LA")
         ||  (temp == "LD")
         ||  (temp == "LE")
         ||  (temp == "LL")
         ||  (temp == "LN")
         ||  (temp == "LS")
         ||  (temp == "LU")
         ||  (temp == "ME")
         ||  (temp == "MK")
         ||  (temp == "ML")
         ||  (temp == "NE")
         ||  (temp == "NG")
         ||  (temp == "NN")
         ||  (temp == "NP")
         ||  (temp == "NR")
         ||  (temp == "NW")
         ||  (temp == "OL")
         ||  (temp == "OX")
         ||  (temp == "PA")
         ||  (temp == "PE")
         ||  (temp == "PH")
         ||  (temp == "PL")
         ||  (temp == "PO")
         ||  (temp == "PR")
         ||  (temp == "RG")
         ||  (temp == "RH")
         ||  (temp == "RM")
         ||  (temp == "SA")
         ||  (temp == "SE")
         ||  (temp == "SG")
         ||  (temp == "SK")
         ||  (temp == "SL")
         ||  (temp == "SM")
         ||  (temp == "SN")
         ||  (temp == "SO")
         ||  (temp == "SP")
         ||  (temp == "SR")
         ||  (temp == "SS")
         ||  (temp == "ST")
         ||  (temp == "SW")
         ||  (temp == "SY")
         ||  (temp == "TA")
         ||  (temp == "TD")
         ||  (temp == "TF")
         ||  (temp == "TN")
         ||  (temp == "TQ")
         ||  (temp == "TR")
         ||  (temp == "TS")
         ||  (temp == "TW")
         ||  (temp == "UB")
         ||  (temp == "WA")
         ||  (temp == "WC")
         ||  (temp == "WD")
         ||  (temp == "WF")
         ||  (temp == "WN")
         ||  (temp == "WR")
         ||  (temp == "WS")
         ||  (temp == "WV")
         ||  (temp == "YO"))                        // If valid 2-letter locality
        {
            return true;                            // return success
        }
        else
        {
            return false;
        }
    }

    return false;                                   // Neither 2nd nor 3rd character is digit, must be bad format
}

// CheckEmail - Check field is a valid e-mail address
//
//  The checks we perform are as follows:
//
//  1. An e-mail address must consist of a user name (one or more characters),
//     followed by an '@' sign followed by a domain name; e.g. abc.def@dom.fred.com
//  2. The domain name must consist of at least two parts, each part being separated
//     by a dot '.'. The final part must consist of two or three characters.
//  3. Only one occurrence of the '@' sign is permitted

function CheckEmail(field)
{
    if  (field.length == 0) return true;            // Empty string always valid
    var off1 = field.indexOf('@');                  // Look for first '@
    if  (off1 < 1) return false;                    // No '@' or at start of string is invalid
    if  (field.indexOf('@', off1+1) != -1) return false;
                                                    // A second '@' is illegal
    var off2 = field.indexOf('.', off1+1);          // Look for the first domain divider
    if  (off2 < (off1+2)) return false;             // If missing or next to '@' is illegal
    off2 = field.lastIndexOf('.');                  // Look for the last domain divider
    off1 = field.length - off2 - 1;                 // Calculate length of final part
    if  (off1 >= 2) return true;   // If 2 or 3 characters say OK
    return false;                                   // Else an error
}

// Check the number is a price and return the equivalent numeric value.
// Modified by CAP 11/09/01 to handle pennies and remove , and pound sign check
function ConvertPrice(s)
{

	// Penny handler
	// First split on decimal point.
	var pennies = 0
	var parts = s.split('.');
	if (parts.length > 2)
		return -1;
	if (parts.length == 2)
	{
		pennies = parts[1];

		if (pennies.length > 2)
			return -1;
		if (pennies.length == 0)
			return -1;

		s = parts[0];
	}

    var price = s;
    var valid = "0123456789";
    var ok = true;
    var priceInt = "";
    var temp;
    for (var isub=price.length-1; isub >= 0; isub--)
    {
        temp = "" + price.substring(isub, isub+1);
        if (valid.indexOf(temp) == "-1") ok = false;
                                                // If not valid digit, flag it
        else priceInt = temp + priceInt;        // Else count it
    }
    if (ok) return (priceInt * 1.0) + (pennies/100.0); // If OK, return value
    else return -1;                                    // else return error
}

//  Try a simple Pop-Up Page launcher

function popupLaunch(url, height, width) {
  self.name = "opener";
  var params = "height="+height+",width="+width+",innerHeight="+height+",innerWidth="+width+",screenX=0,left=0,screenY=0,top=0,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars=1,status=0,toolbar=0";
  remote = open(url, "remote", params);
}


// Synchronise a checkbox and a secondary control
function CheckSynch(c)
{
    if (c.checked)
        c.form['bit_'+c.name].value=1;
    else
        c.form['bit_'+c.name].value=0;

}

// Check the number is a positive number
function sigmaIsaPosNum(s)
{
    var valid = "0123456789"
    var ok = true;
    var temp;
    for (var isub=0; isub < s.length; isub++)
    {
        temp = "" + s.substring(isub, isub+1);
        if (valid.indexOf(temp) == "-1") ok = false;
    }
    return ok;
}

// Check if the item has falls within the min and max values
function sigmaQtyCheck(item, min, max)
{
    var returnVal = true;
    if (!sigmaIsaPosNum(item.value))
    {
        alert("Please enter a positive numeric value");
        returnVal = false;
    }
    else if ((item.value < min) || (item.value > max))
    {
        alert("Please enter a quantity in the range " + min + " to " + max);
        returnVal = false;
    }
    if  (!returnVal)
    {
        item.select();
        item.focus();
    }
    return returnVal;
}

// Check if all options have values specified
function sigmaFormVerify(form)
{

    var returnVal = true;
    var posQtyCheck = true;
    for (var isub = 0; isub < form.length; isub++)
    {
        var elem = form.elements[isub];
        if  (elem.type == "radio")
        {
            var group = form[elem.name];
            var optionSel = false;
            for (var jsub = 0; jsub < group.length; jsub++)
            {
                if  (group[jsub].checked) optionSel=true;
            }
            if (!optionSel) returnVal = false;
        }

        if  ((elem.type == "select-one")
         &&  ((elem.value == null) || (elem.value == -1)))
        {
            returnVal = false;
        }
    }
    if (!returnVal)
    {
      alert("Please select an option before pressing the Buy button");
      return false;
    }

    return FormCheck(form);
}

// This function forces a submit for the named form, usually applied
// as a form control event handler.
function sigmaSubmitForm(name)
 {
     document[name].submit();
 }


// This function checks that a date is given in the correct format
// returns 0 for success greater than 0 for errors
function CheckDate(DateValue)
{
    var checkstr = "0123456789";
    var Datevalue = "";
    var DateTemp = "";
    var day;
    var month;
    var year;
    var leap = 0;
    var err = 0;
    var i;
       err = 0;

    // ensure day and month are 2 characters
    var arrDate = DateValue.split("/");
    if (arrDate.length != 3)
    {
        err = 28;
        return err;
    }
    if (arrDate[0].length == 1)
    {
        arrDate[0] = '0' + arrDate[0];
    }

    if (arrDate[1].length == 1)
    {
        arrDate[1] = '0' + arrDate[1];
    }

    if (arrDate[2].length == 1)
    {
        arrDate[2] = '0' + arrDate[2];
    }

    if ((arrDate[0].length != 2) || (arrDate[1].length != 2) || ((arrDate[0].length != 2) && (arrDate[0].length != 4)))
    {
        err = 29;
        return err;
    }

       DateValue = arrDate[0]+arrDate[1]+arrDate[2];

       /* Delete all chars except 0..9 */
       for (i = 0; i < DateValue.length; i++) {
          if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
             DateTemp = DateTemp + DateValue.substr(i,1);
          }
       }
       DateValue = DateTemp;
       /* Always change date to 8 digits - string*/
       /* if year is entered as 2-digit / always assume 20xx */
       if (DateValue.length == 6)
       {
          //DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2);

          //adjusting the date value here doesn't change the value
          //that is submitted so we need to pass back an error
          err = 30;
       }
       if (DateValue.length != 8)
       {
          err = 19;
       }
       /* year is wrong if year = 0000 */
       year = DateValue.substr(4,4);
       if (year == 0)
       {
          err = 20;
       }
       /* Validation of month*/
       month = DateValue.substr(2,2);
       if ((month < 1) || (month > 12))
       {
          err = 21;
       }
       /* Validation of day*/
       day = DateValue.substr(0,2);
       if (day < 1)
       {
         err = 22;
       }
       /* Validation leap-year / february / day */
       if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0))
       {
          leap = 1;
       }
       if ((month == 2) && (leap == 1) && (day > 29))
       {
          err = 23;
       }
       if ((month == 2) && (leap != 1) && (day > 28))
       {
          err = 24;
       }
       /* Validation of other months */
       if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12")))
       {
          err = 25;
       }
       if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11")))
       {
          err = 26;
       }
       /* if 00 is entered then error */
       if ((day == 0) && (month == 0) && (year == 00))
       {
          err = 27;
       }

    return err;
}
