var elements = new Array();
var elementNames = new Array();
var elementValidation = new Array();
var formName;
var thisForm;
var formErrorMsg,formEMailFormatErrorMsg, formEULAErrorMsg, formFileTypeErrorMsg;

function initValidationForm() {
   for ( i = 3; i < arguments.length; i += 3 ) {
      elements.push( arguments[ i ] );
      elementNames.push( arguments[ i + 1 ] );
      elementValidation.push( arguments[ i + 2 ] );
   }
}

function setFormName( formNameIn, errorMsg, eMailFormatErrorMsg, unused1, unused2, eulaErrorMsg, fileTypeErrorMsg ) {
   formName = formNameIn;
   thisForm = eval( "document." + formName );
   formErrorMsg = errorMsg;
   formEMailFormatErrorMsg = eMailFormatErrorMsg;
   formEULAErrorMsg = eulaErrorMsg;
   formFileTypeErrorMsg = fileTypeErrorMsg
}

function setFormFields() {
   for( r = 2; r < arguments.length; r += 2 ) {
      if ( thisForm.elements[ arguments[ r ] ] ) {
         if ( thisForm.elements[ arguments[ r ] ].type == "text" ) {
            thisForm.elements[ arguments[ r ] ].value = arguments[ r + 1 ];
         } else {
            var thisElement = thisForm.elements[ arguments[ r ] ];
            if( thisElement.length != null ) {  
               for (var n = 0; n < thisElement.length; ++n ) {  
                  thisElement.options[ n ].selected = ( thisElement.options[ n ].value == arguments[ r + 1 ] );
               }
            }
         }
      }
   }
}

function validateForm( process, urlForBasePage ) {
   var errors =  formErrorMsg + "\n";
   var formatEMailErrors, eulaErrors, fileTypeErrors;
   var isErrors = false;
   var isEMailErrors = false;
   var isEULAErrors = false;
   var isFileTypeErrors = false;
   
   for (i=0; i<elements.length; i++ ) {
      var el = thisForm.elements[elements[i]];
      if (el) {
         if ( el.value == "" || el.value == "spacer" || !el.type == "checkbox" ) {
            errors += " - " + elementNames[i] + "\n";
            isErrors = true;
         }
         else if (elementValidation[i] == "radio" ){
            //added radio validation for SCP420 PTM 
            var radioLength = el.length;
            var radioFound = false;
            for(var j = 0; (j < radioLength) && !radioFound; j++) {
               if((el)[j] != null && (el)[j].checked) {
                  radioFound = true;
               }
            }
            if( !radioFound ){
               errors += " - " + elementNames[i] + "\n";
               isErrors = true;
            }
         } else if ( elementValidation[i] == 'eMailAddress' ) {
            var eMailAddr = el.value;
            var att = eMailAddr.indexOf('@');
            var dot = eMailAddr.lastIndexOf('.');
            var len = eMailAddr.length;
            if ( att < 1 || dot < 3 || att == len - 1 || dot >= len - 1 || att > dot || dot == att + 1 ) {
               formatEMailErrors = formEMailFormatErrorMsg;
               isEMailErrors = true;
            }
         } else if ( elementValidation[i] == 'eula' ) {
            if ( el.type == "checkbox" && !el.checked ) {
               eulaErrors = formEULAErrorMsg;
               isEULAErrors = true;
            }
         }
      }
   }
					  		   
   var el = thisForm.Attachment;
   if (el) {
      var fileName = el.value;
      if (fileName != null) {
         if (fileName.length > 4) {
            var lastFour = fileName.substring(fileName.length - 4).toLowerCase();
            if (lastFour != ".doc" && lastFour != "docx" && lastFour != ".bmp" && lastFour != ".jpg" && lastFour != "jpeg" && lastFour != ".gif" && lastFour != ".png") {
               fileTypeErrors = formFileTypeErrorMsg;
               isFileTypeErrors = true;
            }
         } else if (fileName.length > 0) {
            fileTypeErrors = formFileTypeErrorMsg;
            isFileTypeErrors = true;
         }
      }
   }

   if ( isErrors ) {
      alert( errors );
   } else if ( isEMailErrors ) {
      alert( formatEMailErrors );
   } else if ( isEULAErrors ) {
      alert( eulaErrors );
   } else if ( isFileTypeErrors ) {
      alert( fileTypeErrors );
   } else {
      if ( process == "submit" ) {
         thisForm.submit();
      } else if ( process == "supportEmailRequest" ) {
         popupProcessWindow( urlForBasePage );
      }
   }
}
/*
 * 
 *
 * *** VALIDATION OBJECT ***
 *
 *
 *
 */
function Validation( formName ) {
   this.formName = formName;
   this.formRef = eval( "document." + this.formName );

   this.fields = new LMNKArray();

   this.errorMessage = "";

   this.errors = 0;

   this.addField = add_Field_To_Validate;
   this.addtext = add_Form_Text_Field;
   this.addtextarea  = add_Form_Text_Area;
   this.addradio = add_Form_Radio_Button;
   this.addcheckbox = add_Form_Checkbox;
   this.addchoice  = add_Product_Choice;
   this.validate = validate_Form;

   this.setDefaultErrorMessage = set_Default_Error_Message;

   return this;
}

function add_Field_To_Validate( fieldRef ) {
   this.fields.push( fieldRef );
}

function add_Form_Text_Field() {
   var textField = new FormTextField();
   textField.setFieldInformation( arguments, this.formRef );
   this.addField( textField );

   return textField;
}

function add_Form_Text_Area() {
   var textField = new FormTextField();
   textField.setFieldInformation( arguments, this.formRef );
   this.addField( textField );

   return textField;
}

function add_Form_Radio_Button() {
   var radioButtonField = new FormRadioButton();
   radioButtonField.setFieldInformation( arguments, this.formRef );
   this.addField( textField );

   return radioButtonField;
}

function add_Form_Checkbox() {
   var checkboxField = new FormCheckbox();
   checkboxField.setFieldInformation( arguments, this.formRef );
   this.addField( textField );

   return checkboxField;
}

function add_Product_Choice( fieldName ) {
   var productChoice = new FormProductChoice( fieldName, this.formRef );
   this.addField( productChoice );

   return productChoice;
}

function validate_Form() {
   this.errors = 0;
   for ( var i = 0; i < this.fields.length; i++ ) {
      if ( !( this.fields.get( i ).validate() ) ) {
         this.errors++;
      }
   }
   if ( this.errors > 0 ) {
      alert( this.errorMessage );
   }
   else {
      this.formRef.submit();
   }
}

function set_Default_Error_Message( errorMessage ) {
   this.errorMessage = errorMessage;
}

function FormTextField() {
   this.setFieldInformation = set_Form_Field_Information;
   this.getFieldReference = get_Simple_Field_Reference;
   this.getValue = get_Simple_Field_Value;
   this.getLength = get_Simple_Field_Value;
   
   return this;
}

function FormRadioButton() {
   this.setFieldInformation = set_Form_Field_Information;
   this.getFieldReference = get_Simple_Field_Reference;
   this.getValue = get_Simple_Field_Value;
   this.getLength = get_Simple_Field_Value;

   return this;
}

function FormCheckbox() {
   this.setFieldInformation = set_Form_Field_Information;
   this.getFieldReference = get_Simple_Field_Reference;
   this.getValue = get_Simple_Field_Value;
   this.getLength = get_Simple_Field_Value;

   return this;
}

/*
 *
 *
 * *** PRODUCT CHOICE ***
 *
 *
 */
function FormProductChoice( fieldName, formRef ) {
   this.name = fieldName;

   this.formRef = formRef;
   
   this.choices = eval( "this.formRef." + this.name );

   this.rules = null;

   this.addRules = add_Form_Field_Rules;
   this.validate = validate_Product_Choice;

   this.getSelectedCount = get_Selected_Count;

   this.getFieldReference = get_Simple_Field_Reference;
   this.getValue = get_Simple_Field_Value;
   this.getLength = get_Simple_Field_Value;


   return this;
}

function get_Selected_Count() {
   var selected = 0;
   for ( var i = 0; i < this.choices.length; i++ ) {
      if ( this.choices[ i ].checked ) {
         selected++;
      }
   }
   return selected;
}

function validate_Product_Choice() {
   if ( this.rules == null ) {
      return true;
   }
   else {
      return this.rules.validateChoice();
   }
}

function get_Simple_Field_Reference() {
   return eval( this.formRef + "." + this.name );
}

function get_Simple_Field_Value() {
   return this.fieldRef.value;
}

function get_Simple_Field_Length() {
   return this.fieldRef.length;
}

function set_Form_Field_Information( fieldParams, formRef ) {
   var args = new Arguments( fieldParams );

   this.name = args.getString( 'Name' );
   this.formRef = formRef;
   this.fieldRef = this.getFieldReference();
   this.rules = null;

   this.addRules = add_Form_Field_Rules;
   this.validate = validate_Field;
}

function add_Form_Field_Rules( theRules ) {
   var rules = new FormFieldRules( theRules, this );
   this.rules = rules;
}

function validate_Field() {
   if ( this.rules == null ) {
      if ( this.isPopulated() ) {
         return true;
      }
      else {
         return false;
      }
   }
   else {
      return this.rules.validate();
   }
}

function FormFieldRules( rules, field ) {
   var args = new Arguments( rules );

   this.field = field;

   this.minLength = args.getNumber( 'minLength' );
   this.maxLength = args.getNumber( 'maxLength' );
   this.isNumber = args.getBoolean( 'isNumber' );
   this.isString = args.getBoolean( 'isString' );
   this.minSelected = args.getNumber( 'minSelected' );
   this.maxSelected = args.getNumber( 'maxSelected' );
   this.validateChoice = validate_Choice;

   this.validate = validate_Field_Against_Rules;

   return this;
}

function validate_Field_Against_Rules() {
   if ( this.minLength >= 0 ) {
      if ( this.field.getLength() < this.minLength() ) {
         return false;
      }
   }
   if ( this.maxLength > 0 ) {
      if ( this.field.getLength() > this.maxLength() ) {
         return false;
      }
   }
   if ( isNumber ) {
      return !isNaN( Number( this.field.getValue() ) );
   }
}

function validate_Choice() {
   var choicesMade = 0;
   choicesMade = this.field.getSelectedCount();
   if ( this.minSelected > 0 ) {
      if ( (choicesMade >= this.minSelected)
         && (choicesMade <= this.maxSelected) )
      {
         return true;
      }
      else {
         return false;
      }
   }
   else if ( this.maxSelected <= choicesMade ) {
      return true;
   }
   else {
      return true;
   }
}



