/*
  $Id: validateForm.js,v 1.5 2007/11/16 14:29:46 Tonda Exp $
  
  *****************************************************
  *** The common functions for a vorm validation    ***
  *****************************************************
*/

/*
* The 'validateEmail' function validate the standard email format in the input text field in the form.
* Params:
*   formName ... the name of the current form
*   errorMessage ... the locale specified part of the error message
* The name of the input field in form must be 'email'
*
*/

function validateEmail(formName, errorMessage) {
  var field = formName.email; // email field
  var str = field.value; // email string
  if (field.value == "") { // if email isn't filed 
    return true;
  }

  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
    //alert("Thank your for your feedback."); // this is optional
    return true;
  }
	if (navigator.appName == 'Netscape' && document.layers && !document.getElementById){ // NN4
    var xxx = confirm("\"" + str + "\" " + errorMessage);
  }
  else {
    alert("\"" + str + "\" " + errorMessage);
    field.focus();
    field.select();
    return false;
  }
}

/*
* Similar like the 'validateEmail' function but it takes the field as parameter. It is more general 
* and it can be used for validating form fields with different name than 'email'
* Params:
*   formField ... field to be validated
*   errorMessage ... the locale specified part of the error message
*
*/

function validateEmailField(formField, errorMessage) {
  
  var str = formField.value; // email string
  if (formField.value == "") { // if email isn't filed 
    return true;
  }

  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
    //alert("Thank your for your feedback."); // this is optional
    return true;
  }
	if (navigator.appName == 'Netscape' && document.layers && !document.getElementById){ // NN4
    var xxx = confirm("\"" + str + "\" " + errorMessage);
  }
  else {
    alert("\"" + str + "\" " + errorMessage);
    formField.focus();
    formField.select();
    return false;
  }
}

var butCancel="no";
function validateMandatoryFields(pForm, errorMsg, pFieldName) {
/* ***************************************
  The function needs at least 3 arguments. The first argument is the form object,
  the second argument is the error message
  and the other arguments are names of the fields, which should be mandatory.
                                          *************************************** */

  if (butCancel=="yes") return true; // the function is not use for the cancel button

  if (validateMandatoryFields.arguments.length < 3) {
    alert("The number of arguments is wrong in the function createDateFromElms. Please try the operation again and contact the webmaster if the problem will continue.");
    return false;
  }

  for (var iii=2; iii<validateMandatoryFields.arguments.length; iii++) {
    var fieldName = validateMandatoryFields.arguments[iii];
    if(pForm[fieldName]) {
      if(pForm[fieldName].value=="") {
        alert(errorMsg);
        pForm[fieldName].focus();
        pForm[fieldName].select();
        return false;
      }
    }
  }
  return true;
}

/* The function validates the value to integer.
* 'validatedValue' and 'errorMessage' are mandatory arguments,
* 'validatedField' is optional argument, which represents the validated field.
* Example of implementation: <FORM ACTION="..." ONSUBMIT="return validateInteger(this.numberField.value,'The value is not integer!',this.numberField);">

* Oriflame SW, Tomas, 22.5.2003
*/


function validateInteger(validatedValue, errorMessage, validatedField) {
  if ( isNaN(validatedValue) || // value is not convertable to number
        validatedValue < 0 || 
        validatedValue != Math.floor(validatedValue) || // not integer
        validatedValue.length > 2 && validatedValue.substring(0,2).toLowerCase() == "0x") // hexadecimal
  {
    alert(errorMessage);
    if (validatedField) {
      validatedField.focus();
      validatedField.select();
    }
    return false;
  } else return true;
}

/* The function validates the value to integer. Value has to be greater than zero. It is used for validation of the item quantity added to the shopping basket
* 'validatedValue' and 'errorMessage' are mandatory arguments,
* 'validatedField' is optional argument, which represents the validated field.
* Example of implementation: <FORM ACTION="..." ONSUBMIT="return validateInteger(this.numberField.value,'The value is not integer!',this.numberField);">

* Oriflame SW, Tomas, 22.5.2003
*/


function validateNonZeroInteger(validatedValue, errorMessage, validatedField) {
  if ( isNaN(validatedValue) || // value is not convertable to number
        validatedValue <= 0 || 
        validatedValue != Math.floor(validatedValue) || // not integer
        validatedValue.length > 2 && validatedValue.substring(0,2).toLowerCase() == "0x") // hexadecimal
  {
    alert(errorMessage);
    if (validatedField) {
      validatedField.focus();
      validatedField.select();
    }
    return false;
  } else return true;
}

/*
* The 'validateTelephone' function validate the telephone format in the input text field in the form.
* Params:
*   field ... the validated field of the current form
*   errorMessage ... the locale specified part of the error message
* This script is used for example in '/porfile/edit/editprofile.jhtml'.
*
* Oriflame SW, Tomas Klepal, 2.8.2001
*/

function validateTelephone(field, errorMessage) {
  var str = field.value; // filled string
  if (field.value == "") { // if telephone isn't filed
    return true;
  }

  var alowedChars = "0123456789 ";
  for (iii=0; iii<str.length; iii++) {
    if (alowedChars.indexOf(str.substring(iii,iii+1))==-1) {
    	alert("\"" + str + "\" " + errorMessage);
      field.focus();
      field.select();
      return false;
    }
  }
  //alert("Thank your for your feedback.");
  return true;
}


