// BASIC DATA VALIDATION FUNCTIONS:
// (c) 1997 Netscape Communications Corporation

var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = "\t\n\r";
var decimalPointDelimiter = "."
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = digits + phoneNumberDelimiters + "+";
var digitsInMobilePhoneNumber = 10;
var digitsInPhoneNumber = 10;
var digitsInPostCode = 4

var mPrefix = "You did not enter a value into the: "
var mSuffix = " field.\nThis is a required field. Please enter it now."

var sGender = "Gender"
var sTitle = "Title"
var sFirstName = "First Name"
var sLastName = "Last Name"
var sJobTitle = "Job Title"
var sUserID = "User ID"
var sUName = "User Name"
var sPW = "Password"
var sPWConfirmation = "Confirmation of Password"
var sCompanyName = "Company Name"
var sCompanyShortName = "Company Short Name (for login)"
var sCompanyLegalName = "Company Legal Name"
var sAddress = "Street Address"
var sState = "State"
var sSuburb = "Suburb"
var sCountry = "Country"
var sPCode = "Postcode"
var sWorldAddress = "Address"
var sWorldState = "State"
var sCountry = "Country"
var sWorldPostalCode = "Postal Code"
var sPhoneAreaCode = "Phone Area Code"
var sPhone = "Phone Number"
var sMobile = "Mobile Phone Number"
var sFax = "Fax Number"
var sEmail = "Email Address"
var sWeb = "Website Address"

// Buyer RFQ Record:
var sRfqName = "Request For Quote Name";
var sRfqDeadline = "Date suppliers are to have responded by (in the format dd/mm/yyyy)";
var sRfqAcceptance = "Date the buyer will select the supplier (in the format dd/mm/yyyy)";
var sRfqArtApp = "Date by which the artwork is to be agreed upon (in the format dd/mm/yyyy)";
var sRfqDelivery = "Date the product is to be delivered by (in the format dd/mm/yyyy)";
var sProdName = "Product Name";
var sProdCode = "Product Code";
var sProdType = "Product Type";
var sProdPlys = "The number of plys for the product";
var sProdUnits = "Product Units";
var sProdDesc = "Product Description";
var sDiskFormat = "Disk Format";
var sArtFileFormat = "Art File Format";
var sArtFileFormatOther = "Art File Format Other";
var sRfqQuoteQty = "Request For Quote - Quantity to Quote On";
var sProdOtherDetails = "Product Other Details";
var sRFQOtherDetails = "RFQ Other Details";

// Common Specific Product Details
var sProdHeight = "Product Height";
var sProdDepth = "Product Depth";
var sProdWidth = "Product Width";
var sProdSpineDepth = "Product Depth";
var sProdSpineWidth = "Product Width";
var sProdCoverDepth = "Product Depth";
var sProdCoverWidth = "Product Width";
var sProdInkFront = "Product Ink Front";
var sProdInkRear = "Product Ink Rear";
var sProdStckClr = "Product Stock Colour";
var sProdGSM = "Product Weight";
var sProdNumFolds = "Product Number of Folds";
var sProdFoldedDepth = "Product Folded Depth";
var sProdFoldedWidth = "Product Folded Width";
var sProdGrdDesc = "Product Grade / Description";
var sProdPerf = "Product Perforations";
var sProdPerfNum = "Product Number of Perforations";
var sProdPerfDetails = "Product Perforations Details";
var sProdPadding = "Product Padding";
var sProdPaddingPos = "Product Padding Position";
var sProdBindingType = "Product Binding Type";
var sProdPunching = "Product Hole Punching";
var sProdPunchPos = "Product Punch Position";
var sProdPunchNumHoles = "Product Punch Number of Holes";
var sProdPunchDiam = "Product Hole Punch Diameter";
var sProdPunchC2C = "Product Hole Punch Centre to Centre";
var sProdAllocation = "Product Allocation";
var sProdUnits = "Product Units";
var sProdNumPos = "Product Numbering Position";
var sProdNumbering = "Product Numbering";
var sProdNumStart = "Product Numbering Start Number";
var sProdNumPages = "Number of pages";
var sProdNumLeaves = "Number of leaves";
var sProdNumFin = "Product Numbering Finishing Number";
var sProdStub = "Product Stub";
var sProdStubPos = "Product Stub Position";
var sProdStubSize = "Product Stub Size";
var sProdFinishing = "Product Finishing";
var sProdFinishingID = "Product Finishing Type";
var sProdPrintedCover = "Product Printed Cover";
var sProdCoverSides = "Product Cover Sides";
var sProdCoverColours = "Product Cover Colours";
var sProdGlue = "Product Glueing";
var sProdGluePos = "Product Glue Position";
var sProdPackType = "Product Packaging Type";
var sPackaging = "Product Packaging";
var sDeliveryNumCartons = "Product Delivery Number of Cartons";
var sDeliveryCat = "Product Delivery Category";
var sDeliveryCatOther = "Product Delivery Category Other";
var sDeliveryDetails = "Product Delivery Details";
var sInkDesensitising = "Ink Desensitising";
var sInkDesensitisingArea = "Ink Desensitising Area";

var iStateCode = "This field must be a valid Australian state abbreviation (like NSW for New South Wales).\nPlease re-enter it now."
var iPostCode = "This field must be a valid Australian post code\nin the following ranges 0200-0299, 0800-7499, 7800-9799.\nPlease re-enter it now."
var iMobilePhone = "This field must be a 10 digit Mobile phone number (like 0412 123 123).\nPlease re-enter it now."
var iPhone = "This field must be a 10 digit  phone number (like 02 9694 4200).\nPlease re-enter it now."
var iFax = "This field must be a 10 digit  fax number (like 02 9662 1780).\nPlease re-enter it now."
var iWorldPhone = "This field must be a valid international phone number.\nPlease re-enter it now."
var iSSN = "This field must be a 9 digit U.S. social security number (like 123 45 6789).\nPlease re-enter it now."
var iEmail = "This field must be a valid email address (like info@companyname.com.au).\nPlease re-enter it now."
var iWebsite = "This field must be a valid website address (like www.companyname.com.au).\nPlease re-enter it now."
var iCreditCardPrefix = "This is not a valid "
var iCreditCardSuffix = " credit card number. (Click the link on this form to see a list of sample numbers.) Please re-enter it now."
var iDay = "This field must be a day number between 1 and 31.\nPlease re-enter it now."
var iMonth = "This field must be a month number between 1 and 12.\nPlease re-enter it now."
var iYear = "This field must be a 2 or 4 digit year number.\nPlease re-enter it now."
var iDatePrefix = "The Day, Month, and Year for "
var iDateSuffix = " do not form a valid date.\nPlease re-enter them now."
var iCompanyABN = "This field must be a valid Company ABN.\nPlease re-enter it now."
var iCompanyACN = "This field must be a valid Company ACN.\nPlease re-enter it now."

var pEntryPrompt = "The current field requires "
var pStateCode = " state code (like NSW)."
var pMobilePhone = "10 digit Mobile phone number (like 0412 123 123)."
var pPhone = "10 digit  phone number (like 02 9662 4200)."
var pFax = "10 digit  fax number (like 02 9662 1780)."
var pSSN = "9 digit U.S. social security number (like 123 45 6789)."
var pEmail = "valid email address (like info@companyname.com.au)."
var pWebsite = "valid website address (like www.companyname.com.au)."
var pCreditCard = "valid credit card number."

var defaultEmptyOK = false

function makeArray(n) {
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   }
   return this
}

var AusStateCodeDelimiter = "|";
var AusStateCodes = "ACT|NSW|QLD|VIC|TAS| SA| WA| NT"

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{   var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function stripCharsNotInBag (s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}

function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}

function stripWhitespace (s)
{   return stripCharsInBag (s, whitespace)
}

// WORKAROUND FUNCTION FOR NAVIGATOR 2.0.2 COMPATIBILITY.
function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}

function stripInitialWhitespace (s)
{   var i = 0;
    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    return s.substring (i, s.length);
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}

function isInteger (s)
{   var i;
    if (isEmpty(s))
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}

function isSignedInteger (s)
{   if (isEmpty(s))
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);
    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;
        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];
        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK;
    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];
    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number >= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isNegativeInteger (s)
{   var secondArg = defaultEmptyOK;
    if (isNegativeInteger.arguments.length > 1)
        secondArg = isNegativeInteger.arguments[1];
    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) < 0) ) );
}

function isNonpositiveInteger (s)
{   var secondArg = defaultEmptyOK;
    if (isNonpositiveInteger.arguments.length > 1)
        secondArg = isNonpositiveInteger.arguments[1];
    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) <= 0) ) );
}

function isFloat (s)
{   var i;
    var seenDecimalPoint = false;
    if (isEmpty(s))
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);
    if (s == decimalPointDelimiter) return false;
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }
    return true;
}

function isSignedFloat (s)
{   if (isEmpty(s))
       if (isSignedFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedFloat.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedFloat.arguments.length > 1)
            secondArg = isSignedFloat.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;
        return (isFloat(s.substring(startPos, s.length), secondArg))
    }
}

function isAlphanumeric (s)
{   var i;
    if (isEmpty(s))
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }
    return true;
}

function reformat (s)
{   var arg;
    var sPos = 0;
    var resultString = "";
    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

function isMobilePhoneNumber (s)
{   if (isEmpty(s))
       if (isMobilePhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isMobilePhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInMobilePhoneNumber)
}

function isPhoneNumber (s)
{   if (isEmpty(s))
       if (isPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isPhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInPhoneNumber)
}

function isInternationalPhoneNumber (s)
{   if (isEmpty(s))
       if (isInternationalPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isInternationalPhoneNumber.arguments[1] == true);
    return (isPositiveInteger(s))
}

function isPostCode (s)
{  if (isEmpty(s))
       if (isPostCode.arguments.length == 1)
	   {
	   return defaultEmptyOK;
	   }
       else
	   {
	   return (isPostCode.arguments[1] == true);
	   }
   return ( (isInteger(s) && (s.length == digitsInPostCode) ) &&
   		  ( (isIntegerInRange (parseFloat(s), 200, 299)) ||
		    (isIntegerInRange (parseFloat(s), 800, 7499)) ||
			(isIntegerInRange (parseFloat(s), 7800, 9799)) ) )
}

function isStateCode(s)
{   if (isEmpty(s))
       if (isStateCode.arguments.length == 1) return defaultEmptyOK;
       else return (isStateCode.arguments[1] == true);
    return ( (AusStateCodes.indexOf(s) != -1) &&
             (s == "ACT") ||
             (s == "NSW") ||
             (s == "QLD") ||
             (s == "VIC") ||
             (s == "TAS") ||
             (s == "SA") ||
             (s == "WA") ||
             (s == "NT") ||
             (s == "AAT") )
}

function isIntegerInRange (s, a, b)
{   if (isEmpty(s))
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);
    if (!isInteger(s, false)) return false;
    var num = parseInt (s);
    return ((num >= parseInt(a)) && (num <= parseInt(b)));
}

function prompt (s)
{   window.status = s
}

function promptEntry (s)
{   window.status = pEntryPrompt + s
}

function clearStatus (s)
{   window.status = ''
}

function warnEmpty (theField, s)
{
    theField.focus();
    alert(mPrefix + s + mSuffix);
    return false
}

function warnInvalid (theField, s)
{
alert(s)
theField.focus()
theField.blur()
theField.select()
return false
}

function checkPassword (theField, s, emptyOK)
{
    if (checkPassword.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)){
       return warnEmpty (theField, s);
    } else {
        var hasLetter = false;
        var hasNumber = false;
        for (i = 0; i < theField.value.length; i++){
            var c = theField.value.charAt(i);
            if (isLetter(c)) {
                hasLetter = true;
            } else {
                if (isDigit(c)) {
                    hasNumber = true;
                }
            }
        }
        if ((hasLetter)&&(hasNumber)) {
            return true;
        } else {
            return warnInvalid (theField, 'This field must contain Letters and Numbers');
        }
    }
}

function checkString (theField, s, emptyOK)
{
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value))
       return warnEmpty (theField, s);
    else return true;
}

function checkFloat (theField, s, emptyOK)
{
    if (checkFloat.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value))
       return warnEmpty (theField, s);

    if (!isFloat(theField.value))
       return warnInvalid (theField, 'Please enter only numbers for this field');

    else return true;
}

function checkInteger (theField, s, emptyOK)
{
    if (checkInteger.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value))
       return warnEmpty (theField, s);
    if (!isInteger(theField.value))
       return warnInvalid (theField, 'Please enter only whole numbers for this field\nno decimal places');
    else return true;
}

function checkMaxValue(theField, MaxValue) {
    if (theField.value > MaxValue) {
        alert('The current field value MUST\nbe less than ' + MaxValue );
        theField.value=MaxValue;
		theField.select();
		theField.focus();
        return false;
    }
    return true;
}

function checkLength(theField, MaxLength) {
    if (theField.value.length > MaxLength) {
        var curFieldValue = theField.value;
        alert('The current field MUST\nbe less than ' + MaxLength + ' characters');
        theField.value=curFieldValue.substring(0,MaxLength);
		theField.select();
		theField.focus();
        return false;
    }
    return true;
}

function checkPhone (theField, emptyOK)
{   if (checkPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
       if (!isPhoneNumber(normalizedPhone, false))
          return warnInvalid (theField, iPhone);
       else
       {
          theField.value = reformatPhone(normalizedPhone)
          return true;
       }
    }
}

function checkInternationalPhone (theField, emptyOK)
{   if (checkInternationalPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  if (!isInternationalPhoneNumber(theField.value, false))
          return warnInvalid (theField, iWorldPhone);
       else return true;
    }
}

function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false))
       return warnInvalid (theField, iEmail);
    else return true;
}

function isEmail (s)
{   if (isEmpty(s))
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);

    // is s whitespace?
    if (isWhitespace(s)) return false;

    // there must be >= 1 character before @, so we
    // start looking at character position 1
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function checkWebsite (theField, emptyOK)
{   if (checkWebsite.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isWebsite(theField.value, false))
       return warnInvalid (theField, iWebsite);
    else return true;
}

function isWebsite (s)
{   if (isEmpty(s))
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    if (isWhitespace(s)) return false;

    var i = 1;
    var sLength = s.length;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != ".")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function checkMinLength(CurField, MinLength) {
    if (CurField.value.length < MinLength) {
        var CurFieldValue = CurField.value;
        alert('The current field MUST\nbe at least ' + MinLength + ' characters in length');
        //CurField.value='xxx';
        CurField.select();
        CurField.focus();
        return false;
    }
    return true;
}
