//PLACE AT THE END OF THE DEFAULT JAVASCRIPT CODE
//START - Drop-list which dynamically fills-in the NAMES of Primary, Joint 1, Joint 2, Joint 3, etc…
function DropListNameSelection()
{
  var A_NAME = this.getField("A_NAME");
  var CSTM_NAME_DROP_LIST_1 = this.getField("CSTM_NAME_DROP_LIST_1");
  var CSTM_NAME_DROP_LIST_2 = this.getField("CSTM_NAME_DROP_LIST_2");
  var dropListArr1 = [["Select Member Name","[None]"]];//Stores all items in the drop-list
  var dropListArr2 = [["Select Member Name","[None]"]];//Stores all items in the drop-list
  var prevName1 = "";//Used for retaining value after reprocessing
  var prevName2 = "";//Used for retaining value after reprocessing
  var checkNames = ["Select Member Name"];//Makes sure all values in drop list are unique

  //If the drop list has a value from being processed again, prevName will store the value and will add the prevName to checkNames so it is not an item in the other drop-list
  if (CSTM_NAME_DROP_LIST_1.value != "" && CSTM_NAME_DROP_LIST_1.value != "Select Member Name" && CSTM_NAME_DROP_LIST_1.value != "[None]"){
    prevName1 = CSTM_NAME_DROP_LIST_1.value;
  	checkNames.push(prevName1);
  	dropListArr1.push([prevName1,prevName1]);
  	CSTM_NAME_DROP_LIST_1.setItems(dropListArr1);
  }else{
	  CSTM_NAME_DROP_LIST_1.setItems(["Select Member Name","[None]"]);
  }
  if (CSTM_NAME_DROP_LIST_2.value != "" && CSTM_NAME_DROP_LIST_2.value != "Select Member Name" && CSTM_NAME_DROP_LIST_2.value != "[None]"){
    prevName2 = CSTM_NAME_DROP_LIST_2.value;
  	checkNames.push(prevName2);
  	dropListArr2.push([prevName2,prevName2]);
  	CSTM_NAME_DROP_LIST_2.setItems(dropListArr2);
  }else{
	  CSTM_NAME_DROP_LIST_2.setItems(["Select Member Name","[None]"]);
  }
  
  if (A_NAME.value != "" && this.getField("TEAWEB_SIG_A_1_TXT").value != 1 && this.getField("TEAWEB_SIG_A_2_TXT").value != 1){
    dropListArr1.push([A_NAME.value, A_NAME.value]);//Inserts name at the end of the drop list
	  dropListArr2.push([A_NAME.value, A_NAME.value]);//Inserts name at the end of the drop list
    checkNames.push(A_NAME.value);
  }
  
  //The number 4 must be updated depending on how many JNi_NAME mapped on the document.
  for (var i = 1; i <= 4; i++) {
    if (this.getField("JN" + i + "_NAME").value != "" && checkNames.indexOf(this.getField("JN" + i + "_NAME").value) < 0){ //Fills the name in the drop list only if it has not already been added to the drop list.
      if (this.getField("TEAWEB_SIG_JN" + i + "_1_TXT").value != 1 && this.getField("TEAWEB_SIG_JN" + i + "_2_TXT").value != 1){
    		dropListArr1.push([this.getField("JN" + i + "_NAME").value, this.getField("JN" + i + "_NAME").value]);//Inserts name at the end of the drop list
    		dropListArr2.push([this.getField("JN" + i + "_NAME").value, this.getField("JN" + i + "_NAME").value]);//Inserts name at the end of the drop list
    		checkNames.push(this.getField("JN" + i + "_NAME").value);
	    }
    }
  }

  //The number 4 must be updated depending on how many ASi_NAME mapped on the document.
  for (var i = 1; i <= 4; i++)  {
    if (this.getField("AS" + i + "_NAME").value != ""  && checkNames.indexOf(this.getField("AS" + i + "_NAME").value) < 0){ //Fills the name in the drop list only if it has not already been added to the drop list.
	    if (this.getField("TEAWEB_SIG_AS" + i + "_1_TXT").value != 1 && this.getField("TEAWEB_SIG_AS" + i + "_2_TXT").value != 1){
		    dropListArr1.push([this.getField("AS" + i + "_NAME").value, this.getField("AS" + i + "_NAME").value]);//Inserts name at the end of the drop list
		    dropListArr2.push([this.getField("AS" + i + "_NAME").value, this.getField("AS" + i + "_NAME").value]);//Inserts name at the end of the drop list
		    checkNames.push(this.getField("AS" + i + "_NAME").value);
	    }
    }
  }

  //The number 4 must be updated depending on how many Pi_NAME mapped on the document.
  for (var i = 1; i <= 4; i++)  {
    if (this.getField("P" + i + "_NAME").value != ""  && checkNames.indexOf(this.getField("P" + i + "_NAME").value) < 0){ //Fills the name in the drop list only if it has not already been added to the drop list.
	    if (this.getField("TEAWEB_SIG_P" + i + "_1_TXT").value != 1 && this.getField("TEAWEB_SIG_P" + i + "_2_TXT").value != 1){
		    dropListArr1.push([this.getField("P" + i + "_NAME").value, this.getField("P" + i + "_NAME").value]);//Inserts name at the end of the drop list
		    dropListArr2.push([this.getField("P" + i + "_NAME").value, this.getField("P" + i + "_NAME").value]);//Inserts name at the end of the drop list
		    checkNames.push(this.getField("P" + i + "_NAME").value);
	    }
    }
  }
  CSTM_NAME_DROP_LIST_1.setItems(dropListArr1);
  CSTM_NAME_DROP_LIST_2.setItems(dropListArr2);
  
  //Retains the drop list value when reprocessed 
  if (prevName1 != "" && prevName1 != "Select Member Name" && prevName1 != "[None]"){
    CSTM_NAME_DROP_LIST_1.value = prevName1;
  }else{
    CSTM_NAME_DROP_LIST_1.currentValueIndices = 0;//Sets the current value of the drop list to the first option
  }
  if (prevName2 != "" && prevName2 != "Select Member Name" && prevName2 != "[None]"){
    CSTM_NAME_DROP_LIST_2.value = prevName2;
  }else{
    CSTM_NAME_DROP_LIST_2.currentValueIndices = 0;//Sets the current value of the drop list to the first option
  }
}
//END - Drop-list which dynamically fills-in the NAMES of Primary, Joint 1, Joint 2, Joint 3, etc…

//START - Depending on whose name is selected in Drop-list FIELD dynamically fills-in the names
function MemberSelected()
{
	var CSTM_NAME_DROP_LIST_1 = this.getField("CSTM_NAME_DROP_LIST_1");
	var CSTM_NAME_DROP_LIST_2 = this.getField("CSTM_NAME_DROP_LIST_2");
	var CSTM_SIG_NAME_1 = this.getField("CSTM_SIG_NAME_1");
	var CSTM_SIG_NAME_2 = this.getField("CSTM_SIG_NAME_2");

	for (var i = 1; i <= 2; i++){//Loops twice because there are two drop-lists
	  this.getField("TEAWEB_SIG_A_" + i + "_TXT").value = 0;

	  this.getField("TEAWEB_SIG_JN1_" + i + "_TXT").value = 0;
	  this.getField("TEAWEB_SIG_JN2_" + i + "_TXT").value = 0;
	  this.getField("TEAWEB_SIG_JN3_" + i + "_TXT").value = 0;
	  this.getField("TEAWEB_SIG_JN4_" + i + "_TXT").value = 0;

	  this.getField("TEAWEB_SIG_AS1_" + i + "_TXT").value = 0;
	  this.getField("TEAWEB_SIG_AS2_" + i + "_TXT").value = 0;
	  this.getField("TEAWEB_SIG_AS3_" + i + "_TXT").value = 0;
	  this.getField("TEAWEB_SIG_AS4_" + i + "_TXT").value = 0;

    this.getField("TEAWEB_SIG_P1_" + i + "_TXT").value = 0;
	  this.getField("TEAWEB_SIG_P2_" + i + "_TXT").value = 0;
	  this.getField("TEAWEB_SIG_P3_" + i + "_TXT").value = 0;
	  this.getField("TEAWEB_SIG_P4_" + i + "_TXT").value = 0;
	}
  this.getField("TEAWEB_SIG_NM1_1_TXT").value = 0;
  this.getField("TEAWEB_SIG_NM2_1_TXT").value = 0;

  CSTM_SIG_NAME_1.value = "";
  CSTM_SIG_NAME_2.value = "";
  
  //Used to check if the drop-list values match with an existing names. If this is not the case, then NMi_NAME is generated.
  var signerMatched1 = false;
  var signerMatched2 = false;  

	if(CSTM_NAME_DROP_LIST_1.value == this.getField("A_NAME").value && signerMatched1 == false) 
	{
		this.getField("TEAWEB_SIG_A_1_TXT").value = 1;
		CSTM_SIG_NAME_1.value = this.getField("A_NAME").value;  
    signerMatched1 = true;	  
	}
	if(CSTM_NAME_DROP_LIST_2.value == this.getField("A_NAME").value && signerMatched2 == false) 
	{
		this.getField("TEAWEB_SIG_A_2_TXT").value = 1;
		CSTM_SIG_NAME_2.value = this.getField("A_NAME").value;
    signerMatched2 = true;  	  
	}

	for (var i = 1; i <= 4; i++) {
		if (CSTM_NAME_DROP_LIST_1.value == this.getField("JN" + i + "_NAME").value && signerMatched1 == false){
			this.getField("TEAWEB_SIG_JN" + i + "_1_TXT").value = 1; 
			CSTM_SIG_NAME_1.value = this.getField("JN" + i + "_NAME").value;
      signerMatched1 = true;
		}
		if (CSTM_NAME_DROP_LIST_2.value == this.getField("JN" + i + "_NAME").value && signerMatched2 == false){
			this.getField("TEAWEB_SIG_JN" + i + "_2_TXT").value = 1; 
			CSTM_SIG_NAME_2.value = this.getField("JN" + i + "_NAME").value;
      signerMatched2 = true;
		}
	}

	for (var i = 1; i <= 4; i++) {
		if (CSTM_NAME_DROP_LIST_1.value == this.getField("AS" + i + "_NAME").value && signerMatched1 == false){
			this.getField("TEAWEB_SIG_AS" + i + "_1_TXT").value = 1;
			CSTM_SIG_NAME_1.value = this.getField("AS" + i + "_NAME").value;
      signerMatched1 = true;
		}
		if (CSTM_NAME_DROP_LIST_2.value == this.getField("AS" + i + "_NAME").value && signerMatched2 == false){
			this.getField("TEAWEB_SIG_AS" + i + "_2_TXT").value = 1;
			CSTM_SIG_NAME_2.value = this.getField("AS" + i + "_NAME").value;
      signerMatched2 = true;
		}
	}

  for (var i = 1; i <= 4; i++) {
		if (CSTM_NAME_DROP_LIST_1.value == this.getField("P" + i + "_NAME").value && signerMatched1 == false){
			this.getField("TEAWEB_SIG_P" + i + "_1_TXT").value = 1;
			CSTM_SIG_NAME_1.value = this.getField("P" + i + "_NAME").value;
      signerMatched1 = true;
		}
		if (CSTM_NAME_DROP_LIST_2.value == this.getField("P" + i + "_NAME").value && signerMatched2 == false){
			this.getField("TEAWEB_SIG_P" + i + "_2_TXT").value = 1;
			CSTM_SIG_NAME_2.value = this.getField("P" + i + "_NAME").value;
      signerMatched2 = true;
		}
	}
  
  if (signerMatched1 == false && CSTM_NAME_DROP_LIST_1.value != "Select Member Name" && CSTM_NAME_DROP_LIST_1.value != "[None]"){
    this.getField("NM1_NAME").value = CSTM_NAME_DROP_LIST_1.value;
    CSTM_SIG_NAME_1.value = this.getField("NM1_NAME").value;
    this.getField("TEAWEB_SIG_NM1_1_TXT").value = 1;
  }
  if (signerMatched2 == false && CSTM_NAME_DROP_LIST_2.value != "Select Member Name" && CSTM_NAME_DROP_LIST_2.value != "[None]"){
    this.getField("NM2_NAME").value = CSTM_NAME_DROP_LIST_2.value;
    CSTM_SIG_NAME_2.value = this.getField("NM2_NAME").value;
    this.getField("TEAWEB_SIG_NM2_1_TXT").value = 1;
  }

}

function P1_NAME_PDFBlur()
{
  DropListNameSelection();
}

function P2_NAME_PDFBlur()
{
  DropListNameSelection();
}

function P3_NAME_PDFBlur()
{
  DropListNameSelection();
}

function P4_NAME_PDFBlur()
{
  DropListNameSelection();
}

//The two events below are needed for the Drop-Lists to ensure Validate button will be required if the Drop List Selection is changed when "Process Sessions in All Browsers" is checked.
function CSTM_NAME_DROP_LIST_1_PDFBlur()
{
  MemberSelected();
  DropListNameSelection();
}

function CSTM_NAME_DROP_LIST_1_PDFMouseDown()
{
  this.getField("VALIDATE_SIGNERS_FIELD_1").value = ""; //Clears the name selection validation if the user chooses a different name.
}

function CSTM_NAME_DROP_LIST_2_PDFBlur()
{
  MemberSelected();
  DropListNameSelection();
}

function CSTM_NAME_DROP_LIST_2_PDFMouseDown()
{
  this.getField("VALIDATE_SIGNERS_FIELD_2").value = ""; //Clears the name selection validation if the user chooses a different name.
}

function VALIDATE_SIGNERS_BTN_1_PDFMouseUp()
{
  MemberSelected();
  this.getField("VALIDATE_SIGNERS_FIELD_1").value = "VALIDATED";
} 

function VALIDATE_SIGNERS_BTN_2_PDFMouseUp()
{
  MemberSelected();
  this.getField("VALIDATE_SIGNERS_FIELD_2").value = "VALIDATED";
}
//END - Depending on whose name is selected in Drop-list FIELD dynamically fills-in the names