// called from all files
function setDefault(myform)
{
    myform.submit();
}

//-----------------------------------------------------------------------------------------------
//called from Mapping.jsp
// adds mappings
function addValueMapping(){
    var valueName = document.main.valueName;
    var valueAlternate = document.main.valueAlternate;
    var valueList = document.main.valueList;
    var valueListLength = valueList.options.length;

    valueName.value=Trim(valueName.value);
    valueAlternate.value=Trim(valueAlternate.value);

    if (valueName.value==null || valueName.value=="")
    {
        alert("Please enter Input Value");
        return;
    }
   if (valueAlternate.value==null || valueAlternate.value=="")
    {
        alert("Please enter Substitute Value");
        return;
    }

    //checks if user enter the value mappings with explicit square brackets
        var inputValue = valueName.value;                                                                                     if(inputValue.charAt(0)=="(" || inputValue.charAt(0)==")"|| inputValue.charAt(inputValue.length-1)==")" || inputValue.charAt(inputValue.length-1)=="("){
            alert("Input Value "+inputValue+" is not valid, as characters ( ) are not allowed in the Input Value.");
            return;
        }

        var substitutValue=valueAlternate.value;
        if(substitutValue.charAt(0)=="(" || substitutValue.charAt(0)==")" || substitutValue.charAt(substitutValue.length-1)==")" || substitutValue.charAt(substitutValue.length-1)=="("){
            alert("Substitute Value "+substitutValue+" is not valid, as characters ( )  are not allowed in the Substitute Value.");
            return;
        }

//End checking Explicit square brackets

    var condition =  valueName.value+" ("+valueAlternate.value+") ";
    for(var valueListIndex = 0; valueListIndex < valueListLength; valueListIndex++) {
        if (condition == document.main.valueList.options[valueListIndex].text) {
           alert("Value mapping already exists. Please use different name.");
           return;
        }
    }
    valueList.options[valueListLength] = new Option(condition);
    valueName.value="";
    valueAlternate.value="";
}

// removes mappings
function removeValueMapping(){
    var valueListLength = document.main.valueList.length;
    var iSelected = document.main.valueList.selectedIndex;
    if(valueListLength<1)
    {
        alert("Value Mapping does not exist.");
        return;
    }

    if (iSelected >= 0) {
        document.main.valueList.options[iSelected] = null;
    }
}

//sets the UDA to default or Others
function setUdaChoice()
{
      var dataName=document.main.dataName;
        if (dataName.value=="Others")
      {
        document.all.OtherUDA.style.visibility="visible";
        document.all.OtherUDALabel.style.visibility="visible";
      }
      else
      {
          document.all.OtherUDA.style.visibility="hidden";
          document.all.OtherUDALabel.style.visibility="hidden";
      }
}



//adds  Default Data Field References
function addDataField(){

            var dataName = document.main.dataName;
        var dataList = document.main.dataList;
        var dataListLength = dataList.options.length;
        var othData=document.main.OthersUDA.value;

        othData=Trim(othData);

           if (dataName.value==null || dataName.value=="")
          {
            alert("Please select user defined attribute");
              return;
        }
        var data =dataName.options[dataName.selectedIndex].value;
            for(var dataListIndex = 0; dataListIndex < dataListLength; dataListIndex++) {
                if (data == document.main.dataList.options[dataListIndex].text) {
               alert("User defined attribute already exists. Please use different name.");
              return;
                }
        }




        for(var othDataListIndex = 0; othDataListIndex < dataListLength; othDataListIndex++) {
                if (othData == document.main.dataList.options[othDataListIndex].text) {
              alert("User defined attribute already exists. Please use different name.");
             return;
                }
        }



        if(dataName.value=="Others")
        {
            if (othData==null || othData=="")
            {
                alert("Please enter user defined attribute");
                return;
            }

            dataList.options[dataListLength] = new Option(othData);
            document.main.OthersUDA.value="";
        }else{
             dataList.options[dataListLength] = new Option(data);
        }

}
// add User Data Field References
function addUserDataField(){

            var txtDataName=document.main.txtDataName;
         var dataList = document.main.dataList;
           var dataListLength = dataList.options.length;

        txtDataName.value=Trim(txtDataName.value);

        if (txtDataName.value==null || txtDataName.value=="" || txtDataName.value=="-     ENTER UDA     -")
            {
                alert("Please enter user defined attribute");
                return;
            }
        var data =txtDataName.value;
           for(var dataListIndex = 0; dataListIndex < dataListLength; dataListIndex++) {
                if (data == document.main.dataList.options[dataListIndex].text) {
               alert("User defined attribute already exists. Please use different name.");
              return;
                }
        }

   dataList.options[dataListLength] = new Option(data);
   txtDataName.value="";
}


// removes datafield
function removeDataField(){
   var dataListLength = document.main.dataList.options.length;
    var iSelected = document.main.dataList.selectedIndex;
    // alerts if no data field references
    if(dataListLength<1)
    {
        alert("User defined attribute does not exist.");
        return;
    }
     if (iSelected >= 0) {
        document.main.dataList.options[iSelected] = null;
    }
}

//------------------------------------------------------------------------------------------------
//called from ResultMapping.jsp file
function addResultDataField()
{
    var dataName = document.main.dataName;
        var dataList = document.main.dataList;
        var dataListLength = dataList.options.length;
        var othData=document.main.OthersUDA.value;

        othData=Trim(othData);

           if (dataName.value==null || dataName.value=="")
          {
            alert("Please select user defined attribute");
              return;
        }
        var data =dataName.options[dataName.selectedIndex].value;
            for(var dataListIndex = 0; dataListIndex < dataListLength; dataListIndex++) {
                if (data == document.main.dataList.options[dataListIndex].text) {
              alert("User defined attribute already exists. Please use different name.");
              return;
                }
        }


        for(var othDataListIndex = 0; othDataListIndex < dataListLength; othDataListIndex++) {
                if (othData == document.main.dataList.options[othDataListIndex].text) {
                alert("User defined attribute already exists. Please use different name.");
             return;
                }
        }

        if( dataListLength >0)
        {
            alert("User defined attribute cannot be more than one.");
            return;
        }


        if(dataName.value=="Others")
        {
            if (othData==null || othData=="")
            {
                alert("Please enter user defined attribute");
                return;
            }

            dataList.options[dataListLength] = new Option(othData);
            document.main.OthersUDA.value="";
        }else{
             dataList.options[dataListLength] = new Option(data);
        }

}

function addResultUserDataField()
{
    var txtDataName=document.main.txtDataName;
         var dataList = document.main.dataList;
           var dataListLength = dataList.options.length;

        txtDataName.value=Trim(txtDataName.value);

        if (txtDataName.value==null || txtDataName.value=="" || txtDataName.value=="-     ENTER UDA     -")
            {
                alert("Please Enter user defined attribute");
                return;
            }
        var data =txtDataName.value;
           for(var dataListIndex = 0; dataListIndex < dataListLength; dataListIndex++) {
                if (data == document.main.dataList.options[dataListIndex].text) {
              alert("User defined attribute already exists. Please use different name.");
              return;
                }
        }

        if( dataListLength >0)
        {
            alert("User defined attribute cannot be more than one.");
            return;
        }
   dataList.options[dataListLength] = new Option(data);
   txtDataName.value="";
}



//------------------------------------------------------------------------------------------------
//called from DecisionTableConfig.jsp file


// adds Conditions or keys
function addCondition(){
    var conditionName = document.main.conditionName;
    var conditionManipulator = document.main.conditionManipulator;
    var conditionType = document.main.conditionType;
    var conditionList = document.main.conditionList;
    var conditionListLength = conditionList.options.length;
    var defaultMultiplierValue=1;

    conditionName.value=Trim(conditionName.value);
    conditionManipulator.value=Trim(conditionManipulator.value);

    //checks if user enter the condition name with explicit square brackets
        var conditionNm = conditionName.value;                                                                                     if(conditionNm.charAt(0)=="(" || conditionNm.charAt(0)==")"|| conditionNm.charAt(conditionNm.length-1)==")" || conditionNm.charAt(conditionNm.length-1)=="("){
            alert("Condition name "+conditionNm+" is not valid, as characters ( ) are not allowed in the Condition name.");
            return;
        }

    //End checking Explicit square brackets



    //enables or disables the multiplier according to data type
    if(conditionType.options[conditionType.selectedIndex].text=="NUMBER")
        document.main.conditionManipulator.disabled=false;
    else
        document.main.conditionManipulator.disabled = true;

    if (conditionName.value==null || conditionName.value=="")
    {
        alert("Please enter Condition name");
        return;
    }
var condition =  conditionName.value+" ("+conditionType.options[conditionType.selectedIndex].text;
        //checks if number multiplier is other than default
    if(conditionManipulator.value.length>0 && conditionType.options[conditionType.selectedIndex].text=="NUMBER"){
        condition+="["+conditionManipulator.value+"]";
    }

    //checks if number multiplier is default
    if(conditionManipulator.value.length<1 && conditionType.options[conditionType.selectedIndex].text=="NUMBER"){
        condition+="["+defaultMultiplierValue+"]";
    }
        condition+=")";


    for(var conditionListIndex = 0; conditionListIndex < conditionListLength; conditionListIndex++) {
        if (condition == document.main.conditionList.options[conditionListIndex].text) {
             alert("Condition already exists. Please use different name.");
            return;
        }
    }
    //cheks for duplicacy of condition Keys
     for(var conditionListIndex = 0; conditionListIndex < conditionListLength; conditionListIndex++) {
        var conditionPart=document.main.conditionList.options[conditionListIndex].text.split(" (");
        if (conditionName.value == conditionPart[0]) {
             alert("Condition already exists. Please use different name.");
             return;
        }
    }
    //checks for case sensitiveness of condition keys
     var tempCondition=conditionName.value.toLowerCase();
     for(var conditionListIndex = 0; conditionListIndex < conditionListLength; conditionListIndex++) {
            var conditionPart=document.main.conditionList.options[conditionListIndex].text.split(" (");
            if (tempCondition==conditionPart[0].toLowerCase()) {
             alert("Condition already exists. Please use different name.");
                return;
        }
    }
    //checks for the blank spaces
    if (conditionManipulator.value==null || conditionManipulator.value=="")
    {
        alert("Please enter a numeric value");
        return;
    }

    if (isNaN(document.main.conditionManipulator.value))
    {
        alert("Please enter a numeric value");
        document.main.conditionManipulator.value="1";
        return;
    }
    conditionList.options[conditionListLength] = new Option(condition);
    document.main.conditionManipulator.value="1";
    document.main.conditionName.value="";
}
// selects condition types
function typeChanged(){
    var conditionManipulator = document.main.conditionManipulator;
    var conditionType = document.main.conditionType;
      conditionManipulator.value="1";
    if (conditionType.options[conditionType.selectedIndex].text=="NUMBER")
    {
        conditionManipulator.disabled = false;
    }else{
        conditionManipulator.disabled = true;
    }
}


// removes conditions
function removeCondition(){
    var conditionListLength = document.main.conditionList.options.length;
    var iSelected = document.main.conditionList.selectedIndex;
    // alerts if no more conditions
    if(conditionListLength<1)
    {
        alert("Condition does not exist.");
        return;
    }
    if (iSelected >= 0) {
        document.main.conditionList.options[iSelected] = null;
    }
}


// adds roles or selections
function addResult(){
    var roleName = document.main.roleName;
    var roleList = document.main.roleList;
    var roleListLength = roleList.options.length;

    roleName.value=Trim(roleName.value);

    if (roleName.value==null || roleName.value==""||roleName.value=="- ENTER SELECTION -")
    {
        alert("Please enter Result name");
        return;
    }

    var role =  roleName.value;
    for(var roleListIndex = 0; roleListIndex < roleListLength; roleListIndex++) {
        if (role == document.main.roleList.options[roleListIndex].text) {
               alert("Result already exists. Please use different name.");
           return;
        }
    }
     roleList.options[roleListLength] = new Option(role);
     document.main.roleName.value="";
}
// removes roles or selections
function removeRole(){

    var roleListLength = document.main.roleList.options.length;
    var iSelected = document.main.roleList.selectedIndex;
    // alerts if no more selections
    if(roleListLength<1)
    {
        alert("Result does not exist.");
        return;
    }
    if (iSelected >= 0) {
        document.main.roleList.options[iSelected] = null;
    }
}

//sets the Process Definition to default or Others
function setOtherProcessDef()
{
  var processDefinitionName=document.main.processDefinitionName;
        if (processDefinitionName.value=="Others")
      {
        document.all.otherProcDefDiv.style.visibility="visible";
        document.all.otherProcDefDivLabel.style.visibility="visible";
      }
      else
      {
          document.all.otherProcDefDiv.style.visibility="hidden";
          document.all.otherProcDefDivLabel.style.visibility="hidden";
      }
}

// adds default process definitions
function addProcessDefinition(){
    var processDefinitionName = document.main.processDefinitionName;
    var OtherProDef=document.main.OtherProDef;
    var processDefinitionList = document.main.processDefinitionList;
    var processDefinitionListLength = processDefinitionList.options.length;

    OtherProDef.value=Trim(OtherProDef.value);


    if (processDefinitionName.value==null || processDefinitionName.value=="")
    {
        alert("Please select Process Definition name");
        return;
    }
    var processDefinition =  processDefinitionName.options[processDefinitionName.selectedIndex].text;

    for(var processDefinitionListIndex = 0; processDefinitionListIndex < processDefinitionListLength; processDefinitionListIndex++) {
        if (processDefinition == document.main.processDefinitionList.options[processDefinitionListIndex].text) {
            alert("Process definition already exists. Please use different name.");
           return;
        }
    }
     var OtherProcessDefinition=OtherProDef.value;
    for(var processDefinitionListIndex = 0; processDefinitionListIndex < processDefinitionListLength; processDefinitionListIndex++) {
        if (OtherProcessDefinition == document.main.processDefinitionList.options[processDefinitionListIndex].text) {
             alert("Process Definition already exists. Please use different name.");
            return;
        }
    }
    if(processDefinitionName.value=="Others")
    {
        if (OtherProDef.value==null || OtherProDef.value=="")
        {
            alert("Please enter Process Definition name");
            return;
        }
    processDefinitionList.options[processDefinitionListLength] = new Option(OtherProcessDefinition);
    document.main.OtherProDef.value="";
    }else{

     processDefinitionList.options[processDefinitionListLength] = new Option(processDefinition);
    }

}

// removes process definitions
function removeProcessDefinition(){
        var processDefinitionListLength = document.main.processDefinitionList.options.length;
        var iSelected = document.main.processDefinitionList.selectedIndex;
        // alerts if no more process definitions
        if(processDefinitionListLength<1)
        {
            alert("Process Definition does not exist.");
            return;
        }
        if (iSelected >= 0) {
        document.main.processDefinitionList.options[iSelected] = null;
    }
}
//-----------------------------------------------------------------------------------------------






//-----------------------------------------------------------------------------------------------
//called from DecisionTableManagement.jsp

// moves up the DecisionTables in the DecisionTable table
function moveUp(decisionTableId) {
        location.href="DecisionTableManagement.jsp?pageaction=moveUp&decisionTableId=" + decisionTableId ;
    }

// moves down the DecisionTables in the DecisionTable table
function moveDown(decisionTableId) {
        location.href="DecisionTableManagement.jsp?pageaction=moveDown&decisionTableId=" + decisionTableId ;
}

// enables when the DecisionTable is checked or unchecked
function enableDecisionTable(decisionTableId) {
        location.href="DecisionTableManagement.jsp?pageaction=enableDecisionTable&decisionTableId=" + decisionTableId ;
}

var newwindow;
function poptastic(url)
{
    newwindow=window.open(url,'name','height=400,width=200');
    if (window.focus) {newwindow.focus()}
}


// deletes the decisionTable when delete button is clicked
function confirmDelete(decisionTableId, decisionTableName) {
    var answer = confirm("Are you sure you want to delete decisionTable: " + decisionTableName +" ?")
    if (answer){
        location.href="DecisionTableManagement.jsp?pageaction=delete&decisionTableId=" + decisionTableId ;
    }
}
//conforms when View Active button is clicked
function confirmViewActive(toBeSaved) {
  if(toBeSaved){
    var answer = confirm("You have made some changes to decisionTables. Your unsaved changes will be lost. Click Cancel and save changes. \nAre you sure you want to view active decisionTables?")
    if (answer){
        location.href="DecisionTableManagement.jsp?pageaction=viewActive";
    }
    }else{
        location.href="DecisionTableManagement.jsp?pageaction=viewActive";
    }
}
///------------------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------------
//called from CompositeKey.jsp
//trims all leading and trailing blank spaces
function Trim(decisionTableName){
    if(decisionTableName.length < 1){
        return"";
    }
    decisionTableName = RTrim(decisionTableName);
    decisionTableName = LTrim(decisionTableName);
    if(decisionTableName==""){
        return "";
    }
    else{
        return decisionTableName;
    }
}
//trims all trailing blank spaces
function RTrim(decisionTableName){
    var w_space = String.fromCharCode(32);
    var v_length = decisionTableName.length;
    var tempDecisionTableName = "";
    if(v_length < 0){
        return"";
    }
    var decisionTableIndex = v_length -1;

    while(decisionTableIndex > -1){
        if(decisionTableName.charAt(decisionTableIndex) == w_space){
        }
        else{
            tempDecisionTableName = decisionTableName.substring(0,decisionTableIndex +1);
            break;
        }
        decisionTableIndex = decisionTableIndex-1;

    }
return tempDecisionTableName;

}
//trims all leading blank spaces
function LTrim(decisionTableName){
    var w_space = String.fromCharCode(32);
    if(v_length < 1){
        return"";
    }
    var v_length = decisionTableName.length;
    var tempDecisionTableName = "";

    var decisionTableIndex = 0;

    while(decisionTableIndex < v_length){
        if(decisionTableName.charAt(decisionTableIndex) == w_space){
        }
        else{
            tempDecisionTableName = decisionTableName.substring(decisionTableIndex,v_length);
            break;
        }
        decisionTableIndex = decisionTableIndex + 1;
    }
return tempDecisionTableName;
}



// adds Primary Key
function addPrimaryKeyValue(keyTypes,keyNames){
    var primaryKeyValueName = document.main.primaryKeyValueName;
    var primaryKeyValueList = document.main.primaryKeyValueList;
    var primaryKeyValueListLength = primaryKeyValueList.options.length;

    primaryKeyValueName.value=Trim(primaryKeyValueName.value);

    if (primaryKeyValueName.value==null || primaryKeyValueName.value=="")
    {
        alert("Please enter Primary Condition value name");
        return;
    }
    //checks the primary key value according to type of the primary key
    var primaryCondition = document.main.primaryCondition;
    var primaryConditionText = primaryCondition.options[primaryCondition.selectedIndex].text;

    var primaryKeyTypes=keyTypes.split(",");
    var primaryKeyNames=keyNames.split(",");
    var isBooleanTrue=new Boolean("true");
    var isBooleanFalse=new Boolean("false");
    var primaryKeyValueName = document.main.primaryKeyValueName;

     for(var primaryKeyNamesIndex = 0; primaryKeyNamesIndex < primaryKeyNames.length; primaryKeyNamesIndex++) {

        if(primaryConditionText==primaryKeyNames[primaryKeyNamesIndex]){
             if (primaryKeyTypes[primaryKeyNamesIndex]=="NUMBER"){
               var value = document.main.primaryKeyValueName.value;;
               var operator = "";
               if (value.length > 2) {
                 operator = value.substring(0,2);
                 if (operator=="<=" || operator==">=" || operator=="!=") {
                   value = value.substring(2,value.length);
                 } else {
                   operator = value.substring(0,1);
                   if (operator=="=" || operator==">" || operator=="<") {
                     value = value.substring(1,value.length);
                   } else operator = "<=";
                 }
               } else if (value.length > 1) {
                 operator = value.substring(0,1);
                 if (operator=="=" || operator==">" || operator=="<") {
                   value = value.substring(1,value.length);
                 } else operator = "<=";
               } else {
                 operator = "<=";
               }
               if (!isNaN(value)) {
                 document.main.primaryKeyValueName.value = operator + Trim(value);
               } else {
                 alert("Please enter a numeric value");
                 document.main.primaryKeyValueName.value="";
                 return;
               }
            }
        }
        if(primaryConditionText==primaryKeyNames[primaryKeyNamesIndex]){
             if (primaryKeyTypes[primaryKeyNamesIndex]=="BOOLEAN"){
                var primKeyValue=primaryKeyValueName.value;
                if (primKeyValue=="true" || (primKeyValue=="false")){
                }else{
                        alert("Please enter a boolean value");
                        document.main.primaryKeyValueName.value="";
                        return;
                }
            }
        }
    }

    var primaryKeyValue =  primaryKeyValueName.value;
    for(var primaryKeyValueListIndex = 0; primaryKeyValueListIndex < primaryKeyValueListLength; primaryKeyValueListIndex++) {
        if (primaryKeyValue == document.main.primaryKeyValueList.options[primaryKeyValueListIndex].text) {
            alert("Primary Condition value already exists. Please use different name.");
           return;
        }
    }
     primaryKeyValueList.options[primaryKeyValueListLength] = new Option(primaryKeyValue);
     document.main.primaryKeyValueName.value="";
}

// removes Primary Key
function removePrimaryKeyValue(){

    //checks if primary key value list is empty
    var primaryKeyValueListLength = document.main.primaryKeyValueList.options.length;
    if(primaryKeyValueListLength<1)
        {
            alert("Primary condition value does not exist.");
            return;
        }
    var iSelected = document.main.primaryKeyValueList.selectedIndex;
      if (iSelected >= 0) {
        document.main.primaryKeyValueList.options[iSelected] = null;
    }
}

//-------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------
// called from DecisionTableDetail.jsp

// conforms when delete button is clicked
function confirmScenarioDelete(decisionTableId,scenarioId) {
    var answer = confirm("Are you sure you want to delete decision: " + scenarioId +" ?")
    if (answer){
        location.href="DecisionTableDetail.jsp?pageaction=delete&decisionTableId=" + decisionTableId +"&scenarioId=" + scenarioId;
    }
}

// Opens the New Scenarion Window
function newScenario(decisionTableId) {
        location.href="ScenarioDetail.jsp?pageaction=new&decisionTableId=" + decisionTableId;
 }

// moves the scenario up one row
function ScenariomoveUp(decisionTableId,scenarioId) {
        location.href="DecisionTableDetail.jsp?pageaction=moveUp&decisionTableId="+decisionTableId+"&scenarioId=" + scenarioId ;
    }
// moves the scenario down one row
function ScenariomoveDown(decisionTableId,scenarioId) {
        location.href="DecisionTableDetail.jsp?pageaction=moveDown&decisionTableId="+decisionTableId+"&scenarioId=" + scenarioId ;
}
//------------------------------------------------------------------------------------------------



