﻿// PCI Question and Answers Related Java Script functions...
function AnyErrorWhenCopyingSecurityQuestionAnswersToHiddenField() {
          var numberOfQuestions = document.getElementById('numberOfQuestions').value;
          var questionIds='',answers='';
          for(var loop = 1;loop<=numberOfQuestions;loop++) {
                var dropDownControlName = 'SecurityQuestion' + loop;
                var answerControlName = 'SecurityQuestionAnswer' + loop;
                var questionNameControl = document.getElementById(dropDownControlName);
                if(questionNameControl.selectedIndex == 0 ) {
                   var message = 'Please select '+ numberOfQuestions + ' question and provide answer to them';
                   alert(message);
                   return true;
                }
                
                questionIds = questionIds + questionNameControl.options[questionNameControl.selectedIndex].value + ',';           
                if(AllowOnlyLettersNumbersSpaces(document.getElementById(answerControlName).value)) { 
                    answers = answers + document.getElementById(answerControlName).value + ',';
                } else {
                   alert('Please use only letters,numbers and spaces as a answer value');
                   return true;
                }
           }       
          document.getElementById('securityQuestionIds').value =questionIds;
	      document.getElementById('securityAnswers').value = answers;
	      return false;
}
    
function AllowOnlyLettersNumbersSpaces(x) {
        // Note: this WILL allow only lettes,numbers and spaces...
        var RegExp = /^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$/;
        // compare the argument to the RegEx
        // the 'match' function returns 0 if the value didn't match
        return x.match(RegExp);
}
function onQuestionChange(id) {
      var labelControl = 'SecurityQuestionLabel'+id;
      var dropDown = 'SecurityQuestion' + id;
      var selectedQuestion = document.getElementById(dropDown);
      var selectedQuestionName = selectedQuestion.options[selectedQuestion.selectedIndex].text;      
      //check for duplicate questions...
      var numberOfQuestions = document.getElementById('numberOfQuestions').value;
      for(var loop = 1;loop<=numberOfQuestions;loop++) {
        var dropDownControlName = 'SecurityQuestion' + loop;
            if(dropDownControlName != dropDown) {
               var questionNameControl = document.getElementById(dropDownControlName);
               var questionName = questionNameControl.options[questionNameControl.selectedIndex].text;               
                   if(questionName == selectedQuestionName) {
                        alert('You may only select each question once');
                        selectedQuestion.selectedIndex = 0;
                        return;
                   }            
            }     
      }
      
      if(document.getElementById(labelControl) != null) {      
	document.getElementById(labelControl).innerHTML = selectedQuestionName;
      }
}