﻿
    function hidestatus() {
        var statusmsg = ""
        window.status = statusmsg
        return true
    }

    function expandcollapse(obj,row)
    {
        var div = document.getElementById(obj);
        var img = document.getElementById('img' + obj);
        
        if (div.style.display == "none")
        {
            div.style.display = "block";
            if (row == 'alt')
            {
                img.src = "images/minus.gif";
            }
            else
            {
                img.src = "images/minus.gif";
            }
            img.alt = "Close the view of campaign details";
        }
        else
        {
            div.style.display = "none";
            if (row == 'alt')
            {
                img.src = "images/plus.gif";
            }
            else
            {
                img.src = "images/plus.gif";
            }
            img.alt = "Expand to show campaign details";
        }
    }


    function isNumberKey(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 46 || charCode > 57)) {
            alert('Only numbers are allowed');
            return false;
        }

        return true;
    }

    function isOneDecimalLocal(val) {

        if (
                    (val.value.split(".").length - 1 > 1)
                    )
            val.value = 0.00;

        var i;
        var txtArray = new Array();
        txtArray = val.value.split("");
        for (i = 0; i < txtArray.length; i++) {
            if ((txtArray[i] >= 0 && txtArray[i] <= 9) || txtArray[i] == ".") {
            }
            else {
                val.value = 0.00;
                return;
            }
        }
    }

    function isGreaterThan100andOneDecimal(val) {

        if (
            (val.value > 100 || val.value.split(".").length - 1 > 1)
            )
            val.value = 0.00;
            
            var i;
            var txtArray = new Array();
            txtArray = val.value.split("");
            for(i=0;i<txtArray.length;i++) {
                if ((txtArray[i] >= 0 && txtArray[i] <= 9) || txtArray[i] == ".") 
                {
                }
                else {
                    val.value = 0.00;
                    return;
                }    
            }
    }


    function disAllowTextInput(evt) {
        return false;
    }

    function zipCodeFormat(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 44 || charCode > 57 || charCode == 47)) {
            alert('Enter proper US zipcode');
            return false;
        }

        return true;
    }

    function isZipCodeFormatVerify(val) {

        if (
                    (val.value.split(".").length - 1 > 1)
                    )
            val.value = 0.00;

        var i;
        var txtArray = new Array();
        txtArray = val.value.split("");
        for (i = 0; i < txtArray.length; i++) {
            if ((txtArray[i] >= 0 && txtArray[i] <= 9) || txtArray[i] == "-" || txtArray[i] == ",") {
            }
            else {
                val.value = 0.00;
                return;
            }
        }
    }
    
    function ChangeCheckBoxState(id, checkState) {
        var cb = document.getElementById(id);
        if (cb != null)
           cb.checked = checkState;
    }
    
    function ChangeAllCheckBoxStates(checkState) {
        // Toggles through all of the checkboxes defined in the CheckBoxIDs array
        // and updates their value to the checkState input parameter
        if (CheckBoxIDs != null) {
            for (var i = 0; i < CheckBoxIDs.length; i++)
               ChangeCheckBoxState(CheckBoxIDs[i], checkState);
        }
    }
    
    function ChangeHeaderAsNeeded()
    {
        // Whenever a checkbox in the GridView is toggled, we need to
        // check the Header checkbox if ALL of the GridView checkboxes are
        // checked, and uncheck it otherwise
        if (CheckBoxIDs != null)
        {
            // check to see if all other checkboxes are checked
            for (var i = 1; i < CheckBoxIDs.length; i++)
            {
                var cb = document.getElementById(CheckBoxIDs[i]);
                if (!cb.checked)
                {
                    // Whoops, there is an unchecked checkbox, make sure
                    // that the header checkbox is unchecked
                    ChangeCheckBoxState(CheckBoxIDs[0], false);
                    return;
                }
            }
            
            // If we reach here, ALL GridView checkboxes are checked
            ChangeCheckBoxState(CheckBoxIDs[0], true);
        }
    }

    function CheckBoxListSelect(cbControl, state) {
        var chkBoxList = document.getElementById(cbControl);
        var chkBoxCount = chkBoxList.getElementsByTagName("input");
        for (var i = 0; i < chkBoxCount.length; i++) {
            chkBoxCount[i].checked = state;
        }

        return false;
    }

    // IsDouble Function to Validate Numeric Values
    function IsDouble(ControlName) {
        if (ControlName.value.length == 0)
            return true;

        checkOK = "0123456789.";
        checkStr = ControlName.value;
        allValid = true;
        decPoints = 0;

        for (i = 0; i < checkStr.length; i++) {
            ch = checkStr.charAt(i);
            for (j = 0; j < checkOK.length; j++)
                if (ch == checkOK.charAt(j)) {
                if (ch == ".") {
                    decPoints++;
                }
                break;
            }
            if (j == checkOK.length) {
                allValid = false;
                break;
            }
            /*if (ch == ".")
            {
            allNum += ".";
            decPoints++;
            }
            else
            allNum += ch;*/
        }
        if (!allValid) {
            alert("Please enter only digits");
            ControlName.focus();
            ControlName.select();
            return (false);
        }

        if (decPoints > 1) {
            alert("Please enter a valid number");
            ControlName.focus();
            ControlName.select();
            return (false);
        }

        roundNumber(ControlName);
        
        return true;
    }

    // IsNumeric Function to Validate Numeric Values
    function IsNumeric(ControlName) {
        if (ControlName.value.length == 0)
            return true;

        checkOK = "0123456789.";
        checkStr = ControlName.value;
        allValid = true;
        decPoints = 0;

        for (i = 0; i < checkStr.length; i++) {
            ch = checkStr.charAt(i);
            for (j = 0; j < checkOK.length; j++)
                if (ch == checkOK.charAt(j)) {
                if (ch == ".") {
                    decPoints++;
                }
                break;
            }
            if (j == checkOK.length) {
                allValid = false;
                break;
            }
            /*if (ch == ".")
            {
            allNum += ".";
            decPoints++;
            }
            else
            allNum += ch;*/
        }
        if (!allValid) {
            alert("Please enter only digits");
            ControlName.focus();
            ControlName.select();
            return (false);
        }

        if (decPoints > 1) {
            alert("Please enter a valid number");
            ControlName.focus();
            ControlName.select();
            return (false);
        }
        return true;
    }

    //****************************************************************
    // IsNumeric Function to Validate Numeric Values with comma
    function IsNumericWithComma(ControlName, Caption, Home) {
        if (ControlName.value.length == 0)
            return true;

        checkOK = "0123456789.,";
        checkStr = ControlName.value;
        allValid = true;
        decPoints = 0;

        for (i = 0; i < checkStr.length; i++) {
            ch = checkStr.charAt(i);
            for (j = 0; j < checkOK.length; j++)
                if (ch == checkOK.charAt(j)) {
                if (ch == ".") {
                    decPoints++;
                }
                break;
            }
            if (j == checkOK.length) {
                allValid = false;
                break;
            }
            /*if (ch == ".")
            {
            allNum += ".";
            decPoints++;
            }
            else
            allNum += ch;*/
        }
        if (!allValid) {
            if (Home) {
                ShowCAlert("Please enter only digits in " + Caption, Home);
            }
            else {
                alert("Please enter only digits in " + Caption);
            }
            ControlName.focus();
            ControlName.select();
            return (false);
        }

        if (decPoints > 1) {
            if (Home) {
                ShowCAlert("Please enter a valid number in " + Caption, Home);
            }
            else {
                alert("Please enter a valid number in " + Caption);
            }
            ControlName.focus();
            ControlName.select();
            return (false);
        }
        return true;
    }

    function roundNumber(ControlName) { // Arguments: number to round, number of decimal places
 
        if (ControlName.value.length > 0) {
            var newnumber = Math.round(ControlName.value * Math.pow(10, 2)) / Math.pow(10, 2);

            if (newnumber != 'NaN') {
                ControlName.value = newnumber;
            }
        }
    }

    //****************************************************************
    function IsAplhaString(ControlName) {
        
        checkOK = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ, ";
        
        checkStr = ControlName.value;
        allValid = true;
        decPoints = 0;
        allNum = "";
        
        for (i = 0; i < checkStr.length; i++) {
            ch = checkStr.charAt(i);
            for (j = 0; j < checkOK.length; j++)
                if (ch == checkOK.charAt(j))
                break;
            if (j == checkOK.length) {
                allValid = false;
                break;
            }
            if (ch == ".") {
                allNum += ".";
                decPoints++;
            }
            else
                allNum += ch;
        }
        if (!allValid) {
            alert("Please enter only alpha string");
            ControlName.focus();
            ControlName.select();
            return (false);
        }

        if (decPoints > 0) {
            alert("Please enter a valid alpha string");
            ControlName.focus();
            ControlName.select();
            return (false);
        }
        return true;
    }

    //****************************************************************
    function IsValidURL(ControlName) {
        if (trim(ControlName.value).length == 0)
            return true;

        str = ControlName.value;
        //var filter = /(http):\/\/(w+:{0,1}w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
        //var filter = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
        var filter =/^(http(s)?\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/;
        //var filter = /^((http|https|ftp|ftps)+(:\/\/))?(www\.)? (([a-z0-9\.-]{2,})\.(ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|fx|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|aero|asia|cat|coop|edu|gov|jobs|mil|mobi|museum|tel|travel|pro|post|biz|com|info|int|name|net|org|pro|arpa) |((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]))) (:([1-9][0-9]?[0-9]?[0-9]?|[1-5][0-9][0-9][0-9][0-9]|6[0-4][0-9][0-9][0-9]|65[0-4][0-9][0-9]|655[0-2][0-9]|6553[0-5]|))? (((\/(([a-zA-Z0-9_\-\%\~\+\&\;]{1,})+)*)*)|\/$)? (\.(php|html|htm|zip$|arj$|rar$|sit$|pdf$|gif$|jpg$|jpeg$|jpe$|tif$|tiff$))? (\?([a-zA-Z0-9_\-]+\=[a-z-A-Z0-9_\-\%\~\+]+)?(\&([a-zA-Z0-9_\-]+\=[a-z-A-Z0-9_\-\%\~\+]+))*)? (\=\?([a-zA-Z0-9_\-])*)?(((\+([a-zA-Z0-9_])*)?(\-([a-zA-Z0-9_])*)?)*)? (\#([a-z-A-Z0-9_\-\%\~\+\&\;]*$))?$/;
        //var filter = /^(http(s?):\/\/{1})((\w+\.){1,})\w{2,}(\/?)$/i;
        if (filter.test(str))
            return true;
        else {
            //            if (Home) {
            //                ShowCAlert("Please enter a valid e-mail address in " + Caption, Home);
            //            }
            //            else {
            //                alert("Please enter a valid e-mail address in " + Caption);
            //            }
            alert("Please enter a valid URL");
            ControlName.focus();
            ControlName.select();
            return false;
        }
    }

    //****************************************************************
    
    //****************************************************************
    function IsValidEmail(ControlName) {
        if (trim(ControlName.value).length == 0)
            return true;

        str = ControlName.value;
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        if (filter.test(str))
            return true;
        else {
//            if (Home) {
//                ShowCAlert("Please enter a valid e-mail address in " + Caption, Home);
//            }
//            else {
//                alert("Please enter a valid e-mail address in " + Caption);
            //            }
            alert("Please enter a valid e-mail address");
            ControlName.focus();
            ControlName.select();
            return false;
        }
    }

    //****************************************************************


    //****************************************************************
    function IsValidMultipleEmail(ControlName) {
        if (trim(ControlName.value).length == 0)
            return true;

        //str = ControlName.value;
        var str = ControlName.value.split(",");

        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        for (i = 0; i < str.length; i++) {
            //if (MyRegExp.test(ControlName.value))
            if (filter.test(mySplitResult[i]))
            { }
            //return true;
            else {
                //            if (Home) {
                //                ShowCAlert("Please enter a valid e-mail address in " + Caption, Home);
                //            }
                //            else {
                //                alert("Please enter a valid e-mail address in " + Caption);
                //            }
                alert("Please enter a valid e-mail address");
                ControlName.focus();
                ControlName.select();
                return false;
            }

        }
        return true;
    }
    //****************************************************************
    
    function ltrim(str) {
        var i;
        for (i = 0; i < str.length; i++) {
            if (str.charAt(i) != ' ')
                break;
        }
        str = str.substring(i, str.length);
        return str;
    }

    //****************************************************************

    function rtrim(str) {
        var i;
        var spaceIndex = 0;
        var blnTrim = false;
        for (i = 0; i < str.length; i++) {
            if (str.charAt(i) == ' ') {
                if (!blnTrim) {
                    spaceIndex = i;
                    blnTrim = true;
                }
            }
            else {
                blnTrim = false;
            }
        }

        if (blnTrim) {
            str = str.substring(0, spaceIndex);
        }
        return str;
    }

    //****************************************************************

    function trim(str) {
        return rtrim(ltrim(str));
    }
    
    function ValidateUSZip(ControlName) 
    {
        if (trim(ControlName.value).length == 0)
            return true;
        
        var MyRegExp = /^\d{5}$|^\d{5}-\d{4}$/;
        
        //check for valid US Zipcode
        if (MyRegExp.test(ControlName.value))
            return true;
        else {
            alert('Enter proper US zipcode');
            ControlName.focus();
            ControlName.select();
            return false;
        }
    }

    function ValidateUSZipCommaSeparated(ControlName) {
        if (trim(ControlName.value).length == 0)
            return true;

        //\w{6}\d{2}(,\w{6}\d{2})*

        //var MyRegExp = /^\d{5}$|^\d{5},\d{5}$|^\d{5}-\d{4}$|^\d{5}-\d{4}$,\d{5}-\d{4}$|^\d{4}$,\d{5}-\d{4}$|^\d{5}$,\d{5}-\d{4}$|^\d{4}$,\d{5}-\d{4}$/;
        /////var MyRegExp = /^\d{5}$|^\d{5}-\d{4}$|^\d{5}(,\d{5})*$|^\d{5}-\d{4}(,\d{5}-\d{4})*$|^(\d{5}-\d{4})*(,\d{5})*$|^(\d{5})*(,\d{5}-\d{4})*$|^(\d{5}-\d{4})*(,\d{5})*(,\d{5}-\d{4})*$|^(\d{5})*(,\d{5})*(,\d{5}-\d{4})*$|^(\d{5}-d{4})*(,\d{5}-\d{4})*(,\d{5})*$/;
        var MyRegExp = /^([0-9]){5}(([ ]|[-])?([0-9]){4})?$/;
        //^\d{5}(,\d{5}-\d{4})*$|^\d{5}-\d{4}(,\d{5})*$|

        var mySplitResult = ControlName.value.split(",");
        for (i = 0; i < mySplitResult.length; i++) 
        {
            //if (MyRegExp.test(ControlName.value))
            if (MyRegExp.test(mySplitResult[i]))
            { }
            //return true;
            else {
                alert('Enter proper US zip code(s) separated by comma(s)');
                ControlName.focus();
                ControlName.select();
                return false;
            }

        }
        return true;
        //check for valid US Zipcode
        
    }

    function PhoneValidation(ControlName) {

        if (trim(ControlName.value).length == 0)
            return true;

        if (!ControlName.value.replace(/[^0-9]/g, "").match(/^[2-9]{1}[0-9]{2}[2-9]{1}[0-9]{2}[0-9]{4}$/)) {
            alert("Enter proper US phone number");
            ControlName.focus();
            ControlName.select();
            return false;
        }
    }

    function FaxValidation(ControlName) {

        if (trim(ControlName.value).length == 0)
            return true;

        if (!ControlName.value.replace(/[^0-9]/g, "").match(/^[2-9]{1}[0-9]{2}[2-9]{1}[0-9]{2}[0-9]{4}$/)) {
            alert("Enter proper US fax number");
            ControlName.focus();
            ControlName.select();
            return false;
        }
    }


    //****************************************************************
    function IsMatchPassword(ControlName1, ControlName2) {

        if (ControlName1.value == ControlName2.value || ControlName2.value == '')
            return true;
        else {
            alert("The passwords do not match. Please re-enter the password");
            ControlName2.focus();
            ControlName2.select();
            return false;
        }
    }
    
    
    function textboxMultilineMaxNumber(txt,maxLen){
        try{
            if (txt.value.length > (maxLen - 1)) {
                txt.value = txt.value.substring(0, maxLen - 1);
                return false;
            }
           }catch(e){
        }
    }

    function FormatNumberBy3(num, decpoint, sep) {
        // check for missing parameters and use defaults if so
        if (arguments.length == 2) {
            sep = ",";
        }
        if (arguments.length == 1) {
            sep = ",";
            decpoint = ".";
        }
        // need a string for operations
        num = num.toString();
        // separate the whole number and the fraction if possible
        a = num.split(decpoint);
        x = a[0]; // decimal
        y = a[1]; // fraction
        z = "";


        if (typeof (x) != "undefined") {
            // reverse the digits. regexp works from left to right.
            for (i = x.length - 1; i >= 0; i--)
                z += x.charAt(i);
            // add seperators. but undo the trailing one, if there
            z = z.replace(/(\d{3})/g, "$1" + sep);
            if (z.slice(-sep.length) == sep)
                z = z.slice(0, -sep.length);
            x = "";
            // reverse again to get back the number
            for (i = z.length - 1; i >= 0; i--)
                x += z.charAt(i);
            // add the fraction back in, if it was there
            if (typeof (y) != "undefined" && y.length > 0)
                x += decpoint + y;
        }
        return x;
    }



