﻿// JScript File



/* ------------ Validation ------------------ */

    var isValid = true;
    var strMessage = "";
    var validateNoNulls = new Array();
    var numValControls = 0;
 
    function addControlToValidate (controlId, alertString, validationType, groupSet) {
        var controlToValidate = new ValidateControl(controlId, alertString, validationType, groupSet);
        validateNoNulls[numValControls] = controlToValidate;
        numValControls++;
    }

    // Object that stores the details for a control that needs validation
    function ValidateControl(controlId, alertString, validationType, groupSet) 
    {
        this.controlId =  controlId; 
        this.alertString = alertString;
        this.validationType = validationType;
        this.groupSet = groupSet;
    }   

    function resetValidation() {
        isValid = true;
        strMessage = "";
    }

    // run on final button click to check all controls in validators
    function validateControls(resetFormValidation, validateGroupSet) {   
        if (resetFormValidation) {
            resetValidation();
        }
        
        
        
        valControlsCount = 0;
        
        while (valControlsCount < numValControls) {
            var theControl =  validateNoNulls[valControlsCount];
            
            // only validate those within the provided groupset
            if (theControl.groupSet == validateGroupSet) {
                
                if (theControl.validationType == "tbNoNull") {
                    checkNoText(theControl.controlId, theControl.alertString);
                } else if (theControl.validationType == "ddNoNull") {
                    dropDownNoValue(theControl.controlId, theControl.alertString);
                } else if (theControl.validationType == "currencyNoNull") {
                    
                   checkCurrencyText(theControl.controlId, theControl.alertString);
                } else if (theControl.validationType == "DateNoNull") {
                   checkDateText(theControl.controlId, theControl.alertString);
                } else if (theControl.validationType == "DateOrNull") {
                    if (document.getElementById(theControl.controlId).value != "") 
                        checkDateText(theControl.controlId, theControl.alertString);
                }
                
            }
            
            valControlsCount++;
        }

        if (!isValid)
            alert(strMessage);
        
        //return false;
        return isValid;
    }
    
    function dropDownNoValue(strElementId, strAlert) {
        
        var myindex  = document.getElementById(strElementId).selectedIndex
        var SelValue = document.getElementById(strElementId).options[myindex].value

        if (SelValue == "") {
           strMessage += "Please enter a value for the dropdown box: " + strAlert + "\n";
           document.getElementById(strElementId).style.border = "solid 1px red";
           isValid = false;
        } else {
            //alert(document.getElementById(strElementId).style.bordercolor);
            //if (document.getElementById(strElementId).style.border == "solid 1px red")
            document.getElementById(strElementId).style.border = "solid 1px #CCCCCC";
        }
    }
    
    function checkNoText(strElementId, strAlert) {
        if (document.getElementById(strElementId).value == "") {
           strMessage += "Please enter a value for the form field: " + strAlert + "\n";
           document.getElementById(strElementId).style.border = "solid 1px red";
           isValid = false;
        } else {
            //if (document.getElementById(strElementId).style.border == "solid 1px red")
                document.getElementById(strElementId).style.border = "solid 1px #CCCCCC";
        }
    }
    
    function checkCurrencyText(strElementId, strAlert) {
          var txtValue = document.getElementById(strElementId).value;
          var isValidCurrency = RegExp(/^\$?[0-9\,]+(\.\d{2})?$/).test(String(txtValue).replace(/^\s+|\s+$/g, ""));
           
         if (!isValidCurrency) {
           strMessage += "Please enter a currency format for the form field: " + strAlert + "\n";
           document.getElementById(strElementId).style.border = "solid 1px orange";
           isValid = false;
         } else {
               document.getElementById(strElementId).style.border = "solid 1px #CCCCCC";
         }
    }
    
   function checkDateText(strElementId, strAlert) {
         var txtDate = document.getElementById(strElementId).value;
         var isValidDate = checkValidDate(txtDate);
           
         if (!isValidDate) {
           strMessage += "Please enter a date format ('dd/mm/yyyy') for the form field: " + strAlert + "\n";
           document.getElementById(strElementId).style.border = "solid 1px orange";
           isValid = false;
         } else {
               document.getElementById(strElementId).style.border = "solid 1px #CCCCCC";
         }
    }

    
    function getDropDownSelectedValue(strElementId) {
        var myindex  = document.getElementById(strElementId).selectedIndex
        var selValue = document.getElementById(strElementId).options[myindex].value
        return selValue;
    }
    
    // copies textbox values from one to another
    function copyTextFromTo(fromObjectId, toObjectId) {
       document.getElementById(toObjectId).value = document.getElementById(fromObjectId).value;
    }
    
    // copies dropdown Selected value to another
    function copyDropDownSelectedFromTo(fromObjectId, toObjectId) {
        var fromTextBox = document.getElementById(fromObjectId);
        var toTextBox = document.getElementById(toObjectId);
        
        toTextBox.selectedIndex = fromTextBox.selectedIndex;        
    }


    function checkValidDate(dateStr, format) {
    
    
    
        var RegExPattern = /^(?=\d)(?:(?!(?:(?:0?[5-9]|1[0-4])(?:\.|-|\/)10(?:\.|-|\/)(?:1582))|(?:(?:0?[3-9]|1[0-3])(?:\.|-|\/)0?9(?:\.|-|\/)(?:1752)))(31(?!(?:\.|-|\/)(?:0?[2469]|11))|30(?!(?:\.|-|\/)0?2)|(?:29(?:(?!(?:\.|-|\/)0?2(?:\.|-|\/))|(?=\D0?2\D(?:(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|2[0-8]|1\d|0?[1-9])([-.\/])(1[012]|(?:0?[1-9]))\2((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?:$|(?=\x20\d)\x20)))\d{4}(?:\x20BC)?))?$/;

//        var RegExPatternMDY = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;

        var errorMessage = 'Please enter valid date as month, day, and four digit year.\nYou may use a slash, hyphen or period to separate the values.\nThe date must be a real date. 2-30-2000 would not be accepted.\nFormay mm/dd/yyyy.';
        if ((dateStr.match(RegExPattern)) && (dateStr!='')) {
            return true;
        } else {
            return false;
            //alert(errorMessage);
        } 
    }
    
//       if (format == null) { format = "MDY"; }
//       format = format.toUpperCase();
//       alert("test");
//       if (format.length != 3) { format = "MDY"; }
//       if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || _
//          (format.indexOf("Y") == -1) ) { format = "MDY"; }
//       
//       alert("test2");
//       if (format.substring(0, 1) == "Y") { // If the year is first
//          var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
//          var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
//       } else if (format.substring(1, 2) == "Y") { // If the year is second
//          var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
//          var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
//       } else { // The year must be third
//          var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
//          var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
//       }
//       
//       // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
//       if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
//       var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
//       // Check to see if the 3 parts end up making a valid date
//       if (format.substring(0, 1) == "M") { var mm = parts[0]; } else _
//          if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
//       if (format.substring(0, 1) == "D") { var dd = parts[0]; } else _
//          if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
//       if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else _
//          if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
//       if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
//       if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
//       var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
//       if (parseFloat(dd) != dt.getDate()) { return false; }
//       if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
//       return true;
//    }


/* ------------ Dropdown Boxes ------------------ */
// Copies the options from one dropdown box to another
function copyDropDown(fromObjectId, toObjectId) {
   
    fromDD = document.getElementById(fromObjectId);
    toDD = document.getElementById(toObjectId);
    
    var numItems = fromDD.options.length;
    var i = 0;
    while (i < numItems) {
        addOption(toDD, fromDD.options[i].text, fromDD.options[i].value);
        i++;
    }
}

function addOption(selectbox, text, value )
{
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}




