﻿// JScript File

function popupWindow(strContent) {
    alert('now inside popupWindow...');
    window.open('popup.aspx?Content=' + strContent, 'blank', 'toolbar=no, width=1000, height=400');
}

function getQueryString(key, default_)
{
    // input the complete current URL, and the function returns the value of a QueryString variable,
    // if it exists
    if (default_==null) default_=""; 
    key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if(qs == null)
    {
        return default_;
    }
    else
    {
        return qs[1];
    }
} 

function toggleHxUser(obj)
{   // Shows/Hides the history listbox
    // The input control is the command button, but the listbox name can be derived from this name, as the
    // naming conventions are 'cmd<controlname>HxUser' and 'lst<controlname>HxUser', respectively.
    if (obj.value.length > 0)
      {
        var objHxUser = obj.name;
        var objHxUserLen = objHxUser.length;
        objHxUser = objHxUser.substring(0, 5) + '_lst' + objHxUser.substring(9, objHxUserLen);

        if (document.getElementById(objHxUser).style.visibility == 'hidden')
          {
            document.getElementById(objHxUser).style.height = '100px';
            document.getElementById(objHxUser).style.width = '390px';
            document.getElementById(objHxUser).style.visibility = 'visible';
          }
        else
          {
            document.getElementById(objHxUser).style.height = '1px';
            document.getElementById(objHxUser).style.width = '1px';
            document.getElementById(objHxUser).style.visibility = 'hidden';
          }
      }
}

function ShowHelpFile(obj)
{  // Displays a PDF Help Document
    window.open('help_docs/'+obj);
}

/*--------------------------------------------------------------------------/
/  Validate Date Field script- By JavaScriptKit.com                         /
/  For this script and 100s more, visit http://www.javascriptkit.com        /
/  This notice must stay intact for usage                                   /
/--------------------------------------------------------------------------*/
function checkdate(input)
{
    var validformat1 = /^\d{1}\/\d{1}\/\d{2}$/ //Basic check for format validity
    var validformat2 = /^\d{1}\/\d{2}\/\d{2}$/ //Basic check for format validity
    var validformat3 = /^\d{2}\/\d{1}\/\d{2}$/ //Basic check for format validity
    var validformat4 = /^\d{2}\/\d{2}\/\d{2}$/ //Basic check for format validity
    var validformat5 = /^\d{1}\/\d{1}\/\d{4}$/ //Basic check for format validity
    var validformat6 = /^\d{1}\/\d{2}\/\d{4}$/ //Basic check for format validity
    var validformat7 = /^\d{2}\/\d{1}\/\d{4}$/ //Basic check for format validity
    var validformat8 = /^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
    var returnval = ""

    if (!validformat1.test(input) && !validformat2.test(input) && !validformat3.test(input) && !validformat4.test(input) &&
        !validformat5.test(input) && !validformat6.test(input) && !validformat7.test(input) && !validformat8.test(input))
      {
        returnval = "Invalid Date Format. Please correct and submit again."
      }
//    else   //Detailed check for valid date ranges
//      { 
//        var monthfield=input.split("/")[0]
//        var dayfield=input.split("/")[1]
//        var yearfield=input.split("/")[2]
//        var dayobj = new Date(yearfield, monthfield-1, dayfield)
//        if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
//          {
//            returnval = "Invalid Day, Month, or Year range detected. Please correct and submit again."
//          }
//      }
    return returnval
}

function isNonNegativeNumber(input)
{
    var returnVal = '';

    if (input == '') {
        returnVal = "Nothing to check.";
    }
    else if (isNaN(input)) {
        returnVal = "Not a Number.";
    }
    else if (input < 0) {
        returnVal = "Number is negative.";
    }
    return returnVal;
}

function IsEmailAddressValid(strEmail) {
    // Return values are:
    // True:  '' (empty string)
    // False: Error message
    
    var strErrors = '';
    var boolValidChars = true;
    
    for (var i = 0; i < strEmail.length; i++) {
        if ((strEmail.substr(i, 1) < 'a' || strEmail.substr(i, 1) > 'z') &&
            (strEmail.substr(i, 1) < 'A' || strEmail.substr(i, 1) > 'Z') &&
            (strEmail.substr(i, 1) < '0' || strEmail.substr(i, 1) > '9') &&
            strEmail.substr(i, 1) != '-' && strEmail.substr(i, 1) != '.' &&
            strEmail.substr(i, 1) != '@') {
             boolValidChars = false;
        }
    }

    if (!boolValidChars) {
        strErrors = 'Email may only contain alphanumeric characters, hyphens, periods and the @ symbol.';
    }
    else {
        // Only valid characters exist in Email - check for format.
        var intAtSymbolPosition = strEmail.indexOf('@');
        var intAtSymbolPositionLast = strEmail.lastIndexOf('@');
        var strDomain = strEmail.substr(intAtSymbolPositionLast + 1,
                                        strEmail.length - intAtSymbolPositionLast);
        var intPeriodPosition = strDomain.indexOf('.');
        var intPeriodPositionLast = strDomain.lastIndexOf('.');

        if (intAtSymbolPosition == -1 || intAtSymbolPosition == 0 ||
            intAtSymbolPosition != intAtSymbolPositionLast ||
            strDomain.length == 0 ||
            intPeriodPosition == -1 || intPeriodPosition == 0 ||
            intPeriodPositionLast == strDomain.substr(0,strDomain.length-1) ||
            intPeriodPosition != intPeriodPositionLast) {
             strErrors = 'Email does not match standard email format ' +
                         '(e.g. name@domain.com, fname.lname@domain.edu, etc.).';
        }
    }
    return strErrors;
}

function GetDayOfWeek(datDate) {
    // Returns a string version of the Day of the Week
    var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    return dayNames[datDate.getDay()];

}


