/* formcheck.js for UD */

// VARIABLE DECLARATIONS

// whitespace characters
var whitespace = " \t\n\r";

// CONSTANT STRING DECLARATIONS
// (grouped for ease of translation and localization)

// m is an abbreviation for "missing"
var mPrefix = "Du har ikke indtastet i rubrikken ";

var mSuffix = ". Denne er obligatorisk. Indtast venligst nu.";

var msPrefix = "Du har ikke valgt i rubrikken ";

var msSuffix = ". Denne er obligatorisk. Vælg venligst nu.";

var mcPrefix = "Du har ikke afkrydset rubrikken ";

var mcSuffix = ". Dette er obligatorisk. Afkryds venligst nu.";

// s is an abbreviation for "string"
var sWorldPartnerFirstName = "ægtefælles Fornavn ";
var sWorldPartnerLastName = "ægtefælles Efternavn ";
var sWorldName = "Navn";
var sWorldLastName = "Efternavn";
var sWorldFirstName = "Fornavn";
var sWorldAddress1 = "Vejadresse linie 1";
var sWorldAddress2 = "Vejadresse linie 2";
var sWorldPostalCode = "Postnummer";
var sCity = "By";
var sCountry = "Land";
var sComment = "eventuelle kommentarer";

// i is an abbreviation for "invalid"
var iWorldPhone = "Dette skal være et gyldigt telefonnummer kun indeholdende tal, mellemrum og tegnene + ( ).\nGenindtast venligst nu.";

var iEmail = "Dette skal være en gyldig e-mail adresse (som kontor@union-danoise.com). Genindtast venligst nu.";

// p is an abbreviation for "prompt"
var pEntryPrompt = "Indtast venligst ";
var pWorldPhone = "et telefonnummer.";
var pEmail = "en gyldig e-mail adresse (som kontor@union-danoise.com).";
var pSelectPrompt = "Angiv venligst ";
var pRadioPrompt = "Angiv venligst ";
var pCheckPrompt = "Afkryds venligst ";

var pMemberType = "type af medlemskab.";

/* FUNCTIONS TO NOTIFY USER OF INPUT REQUIREMENTS OR MISTAKES. */

// Display data entry prompt string s in status bar.

function promptEntry (s)
{   window.status = pEntryPrompt + s;
}

// Display select prompt string s in status bar.

function promptSelect (s)
{   window.status = pSelectPrompt + s;
}

// Display radio buton prompt string s in status bar.

function promptRadio (s)
{   window.status = pRadioPrompt + s;
}

// Display check box prompt string s in status bar.

function promptCheck (s)
{   window.status = pCheckPrompt + s;
}

/* FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS. */
// Many of the below functions take an optional parameter eok (for "emptyOK")
// which determines whether the empty string will return true or false.
// Default behavior is controlled by global variable defaultEmptyOK.

// Global variable defaultEmptyOK defines default return value 
// for many functions when they are passed the empty string. 
// By default, they will return defaultEmptyOK.
//
// defaultEmptyOK is false, which means that by default, 
// these functions will do "strict" validation.  Function
// isInteger, for example, will only return true if it is
// passed a string containing an integer; if it is passed
// the empty string, it will return false.
//
// You can change this default behavior globally (for all 
// functions which use defaultEmptyOK) by changing the value
// of defaultEmptyOK.
//
// Most of these functions have an optional argument emptyOK
// which allows you to override the default behavior for 
// the duration of a function call.
//
// This functionality is useful because it is possible to
// say "if the user puts anything in this field, it must
// be an integer (or a phone number, or a string, etc.), 
// but it's OK to leave the field empty too."
// This is the case for fields which are optional but which
// must have a certain kind of content if filled in.

var defaultEmptyOK = false;

// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkString (theField, s, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    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;
}

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0));
}



// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

function warnEmpty (theField, s)
{   //theField.focus();
    alert(mPrefix + s + mSuffix);
theField.focus();
    return false;
}

// Notify user that no required Select has been done
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

function warnNoSelect (theField, s)
{   //theField.focus();
    alert(msPrefix + s + msSuffix);
theField.focus();
    return false;
}

// Notify user that the required Checkbox has not been checked
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

function warnNoCheck (theField, s)
{   //theField.focus();
    alert(mcPrefix + s + mcSuffix);
theField.focus();
    return false;
}


// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.

function warnInvalid (theField, s)
{   //theField.focus();
    //theField.select();
    alert(s);
theField.focus();
theField.select();
    return false;
}

// isInternationalPhoneNumber (STRING s [, BOOLEAN emptyOK])
// 
// isInternationalPhoneNumber returns true if string s is a valid 
// international phone number.  Must be digits only; any length OK.
// May be prefixed by + character.
//
// NOTE: A phone number of all zeros would not be accepted.
// I don't think that is a valid phone number anyway.
//
// NOTE: Strip out any delimiters (spaces, hyphens, parentheses, etc.)
// from string s before calling this function.  You may leave in 
// leading + character if you wish.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isInternationalPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isInternationalPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isInternationalPhoneNumber.arguments[1] == true);
//    return (isPositiveInteger(s));
    return (isPhoneChar(s));
}

// checkInternationalPhone (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid International Phone.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

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;
    }
}

// isPositiveInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is an integer > 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.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 positive, not negative, number

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}

function isPhoneChar (s)
{   var secondArg = defaultEmptyOK;
    if (isPhoneChar.arguments.length > 1)
        secondArg = isPhoneChar.arguments[1];

    var len = s.length;
    var return_val = true;

    for (var i = 0; i < len; i++) {
      var sign = s.charAt(i);
      if ((!isDigit(sign) && (sign != " ") && (sign != "+") && (sign != "(") && (sign != ")"))) {
        return_val = false;
      }
    }
    return ( (isEmpty(s) && secondArg)  || (return_val)) ;
} // End isPhoneChar 


// isSignedInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters are numbers; 
// first character is allowed to be + or - as well.
//
// Does not accept floating point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// EXAMPLE FUNCTION CALL:          RESULT:
// isSignedInteger ("5")           true 
// isSignedInteger ("")            defaultEmptyOK
// isSignedInteger ("-5")          true
// isSignedInteger ("+5")          true
// isSignedInteger ("", false)     false
// isSignedInteger ("", true)      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));
    }
}

// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"));
}

// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Email.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

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;
}
// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

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;
}

// checkSelect (SELECT theField)
//
// Check that index 1 or more has been selected

function checkSelect(theSelect, s)
{   if (theSelect.selectedIndex < 1) 
          return warnNoSelect (theSelect, s);
       else return true;
}

// checkCheckbox (theCheckbox, theField)
//
// Check that checkbox has been checked

function checkCheckbox(theCheckbox, s)
{   if (!theCheckbox.checked) 
          return warnNoCheck (theCheckbox, s);
       else return true;
}

// Function to check form for member data
function validateMemberDataForm(form)
{   if 
    (
      checkString(form.elements["frm_member_last_name"],sWorldLastName) &&
      checkString(form.elements["frm_member_first_name"],sWorldFirstName) &&
      checkString(form.elements["frm_street_address1"],sWorldAddress1) &&
      checkString(form.elements["frm_postal_code"],sWorldPostalCode) &&
      checkString(form.elements["frm_city"],sCity) &&
      checkString(form.elements["frm_country"],sCountry, true) &&
      checkInternationalPhone(form.elements["frm_phone"]) &&
      checkInternationalPhone(form.elements["frm_fax"], true) &&
      checkInternationalPhone(form.elements["frm_mobile"], true) &&
      checkEmail(form.elements["frm_email"], true)
    )
	{
	 return true;
	} 
	else return false;
}

// Function to check form for member data
function validateEntryForm(form)
{   if 
    (
      checkString(form.elements["frm_member_last_name"],sWorldLastName) &&
      checkString(form.elements["frm_member_first_name"],sWorldFirstName) &&
      checkString(form.elements["frm_street_address1"],sWorldAddress1) &&
      checkString(form.elements["frm_postal_code"],sWorldPostalCode) &&
      checkString(form.elements["frm_city"],sCity) &&
      checkString(form.elements["frm_country"],sCountry, true) &&
      checkInternationalPhone(form.elements["frm_phone"]) &&
      checkInternationalPhone(form.elements["frm_fax"], true) &&
      checkInternationalPhone(form.elements["frm_mobile"], true) &&
      checkEmail(form.elements["frm_email"], true)
    )
	{
	 return true;
	} 
	else return false;
}
