//++
//============================================================================
// Produced by Logica UK, Energy & Utilities Division.
// Copyright (c) 2000 Logica UK Ltd.
//
// FILENAME:    bwx_generic.ujs
// VERSION:    1.19
// PATHNAME:    /app/kfigkernel/master/neta/sw/bmra/server/bwx/s.bwx_generic.ujs
// LAST CHANGE:    05/01/20
//
// DESCRIPTION
//  Generic JavaScript Functions used on most web pages.
//
// CONTENTS
//
//============================================================================
//--

// Minimum settlement period.
var MIN_PERIOD_NUM = 1;
// Maximum settlement period.
var MAX_PERIOD_NUM = 50;

onload = setFocus;

//
// Set Focus to the first Input box on the page.
//
function setFocus ()
{
    if (document.forms[0])
    {
        if (document.forms[0].elements[0])
        {
            if (document.forms[0].elements[0].type != "hidden")
            {
                document.forms[0].elements[0].focus ();
            }
        }
    }
}

//
// Generates the 'CSV file' string and returns it.
//
function generateContent ()
{
    return gs_csv;
}

//
//  Function that checks if the browser is Internet Explorer Version 4
//  and returns true or false
//

function isBrowserIEVersion4 ()
{
    var found = false;
    var s_appString = navigator.appVersion;
    var a_appVersion = s_appString.split (";");
    if (a_appVersion.length > 1)
    {
        var regExp = / 4./g;
        var i_matches = a_appVersion[1].search (regExp);
        if (i_matches > 0)
        {
            found = true;
        }
    }
    return found;
}

//
// Opens a new browser and writes the 'CSV file' to it.
//
function openCsvWindow ()
{
    var o_nw = window.open ("", windowId (), "menubar,scrollbars,resizable,status");
    o_nw.document.open ("text/plain");
    if (isBrowserIEVersion4 ())
    {
        o_nw.document.charset = "csASCII";
    }
    o_nw.status = "Loading";
    o_nw.document.write (generateContent ());
    o_nw.document.close ();
    o_nw.status = "Done";
}

//
// Opens a new window with the specified URL, no menu or tool bars.
//
function newWin (s_url)
{
    var o_nw = window.open (s_url, windowId (), "scrollbars,resizable");
    o_nw.document.close ();
}

//
// Opens a new window with the specified URL, including the menu bar.
//
function newWinMenuBar (s_url)
{
    var o_nw = window.open (s_url, windowId (), "menubar,scrollbars,resizable,status");
    o_nw.document.close ();
}

//
// Rewrites the content of the current window with the 'CSV file'.
//
function rewriteAsCsv ()
{
    var s_content = generateContent ();
    document.open ("text/plain");
    if (isBrowserIEVersion4 ())
    {
        document.charset = "csASCII";
    }
   status = "Loading";
    document.write (s_content);
    document.close ();
    status = "Done";
}

//
// Verifies that string is in the correct format, YYYY-MM-DD.
//
function isValidDay (s_dayIn)
{
    // find a match for the pattern yyyy-mm-dd
    var dayRegExp = new RegExp ("^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$");
    if (dayRegExp.test (s_dayIn))
    {
        var a_date = s_dayIn.split ("-");
        return isDate (a_date[0], a_date[1], a_date[2]);
    }
    return false;
}

//
// Verifies that string is the correct format for a settlement period.
//
function isValidPeriod (s_periodIn)
{
    // find a match for '*'.
    if (s_periodIn == "*")
    {
        return true;
    }
    // find a match for the pattern n or nn.
    var periodRegExp = new RegExp ("^[0-9]{1,2}$");
    if (periodRegExp.test (s_periodIn))
    {
        return (s_periodIn >= MIN_PERIOD_NUM && s_periodIn <= MAX_PERIOD_NUM);
    }
    return false;
}

//
// Verifies that string is the correct format for a settlement period
// this function will return false if the * wildcard is supplied
//
function isValidPeriodNoWildCard (s_periodIn)
{
    // find a match for the pattern n or nn.
    var periodRegExp = new RegExp ("^[0-9]{1,2}$");
    if (periodRegExp.test (s_periodIn))
    {
        return (s_periodIn >= MIN_PERIOD_NUM && s_periodIn <= MAX_PERIOD_NUM);
    }
    return false;
}

//
// Verifies that the string does not contain any illegal characters.
//
function isValidString (s_string)
{
    // find a match for anything that isn't a legal character.
    var stringRegExp = new RegExp ("[^A-Za-z0-9-_$]");
    if (s_string.length ==0 || stringRegExp.test (s_string))
    {
        return false;
    }
    return true;
}

//
// Verifies that the string does not contain any illegal characters.
// Added * as valid character
//
function isNewValidString (s_string)
{
    // find a match for anything that isn't a legal character.
    if (s_string.length ==0)
    {
       s_string = "*" 
    }
    var stringRegExp = new RegExp ("[^A-Za-z0-9-_*$]");
    if (s_string.length ==0 || stringRegExp.test (s_string))
    {
        return false;
    }
    return true;
}
//
// Validates Lead Party String - escapes any oracle wildcard characters
// by preceding them with a '\'
//
function validateLeadPartyNameString (s_string)
{

   // Set up regular expression vars.  These need to be global regExps 
   // as we will want to search the entire string for them so they are suffixed with a g.
   var rexpPercentChar    = /%/g;
   var rexpUnderscoreChar = /_/g;
   var rexpBackslashChar  = /\\/g;

   var newLeadPartyNameString = s_string;

   // Use the String.replace method to escape the wildcard characters with a \
   // Note that the \ char will need to be escaped itself

   newLeadPartyNameString = newLeadPartyNameString.replace(rexpBackslashChar,"\\\\");
   newLeadPartyNameString = newLeadPartyNameString.replace(rexpPercentChar,"\\%");
   newLeadPartyNameString = newLeadPartyNameString.replace(rexpUnderscoreChar,"\\_");

   return (newLeadPartyNameString);
}
//
// Verifies that the string does not contain any illegal characters.
// Added space as valid character
//
function isValidLeadPartyNameString (s_string)
{
    // find a match for anything that isn't a legal character.
    if (s_string.length ==0)
    {
       s_string = "*" 
    }
    var stringRegExp = new RegExp ("[^A-Za-z0-9-_ *$]");
    if (s_string.length ==0 || stringRegExp.test (s_string))
    {
        return false;
    }
    return true;
}
//
// Verifies that specified values form a valid date.
//
function isDate (i_yearValue, i_monthValue, i_dayValue)
{
    // number of days in month array
    var a_daysInMonth = new Array (31,28,31,30,31,30,31,31,30,31,30,31);

    // if day/year entered is zero then exit returning false  
    if (i_dayValue < 1 || i_yearValue < 1)
    {
        return false;
    }
  
    // if month entered is incorrect then exit returning false  
    if (i_monthValue <= 0 || i_monthValue >= 13)
    {
        return false;
    }

    // if year is a leap year then set # days in Feb to 29
    if (i_yearValue % 4 == 0)
    {
        if (!((i_yearValue % 100 == 0) && (i_yearValue % 400 != 0)))
        {
            a_daysInMonth[1] = 29;
        }
    }

    return (i_dayValue <= a_daysInMonth[i_monthValue-1]);
}

//
// Displays a pop-up error message.
//
function displayError ()
{
    alert ("One or more values are incorrect.\nPlease check and re-enter.");
}

//
// Returns a unique identifier for new windows, based on the number
// of milliseconds since 1 Jan 1970.
//
function windowId ()
{
    var o_now = new Date ();

    // Convert to string containing number of milliseconds since 1970.
    var s_now = o_now.getTime () + "";

    return s_now;
}
