////////////////////////////////////////////////////////////////////////////////////////
//......................................................................................
//  This is a part of UI Soft Web Services.                                            .
//  Copyright © 2002 Arthur Amshukov                                                   .
//......................................................................................
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND         .
//  EVERYONE IS PERMITTED TO USE, MODIFY, COPY AND DISTRIBUTE THIS SOFTWARE FREE OF    .
//  CHARGE AND WITHOUT ANY RETRICTIONS                                                 .
//                                                                                     .
//  DO NOT REMOVE MY NAME AND THIS NOTICE FROM THE SOURCE                              .
////////////////////////////////////////////////////////////////////////////////////////
// dhtml functions (f)
// ----- --------- ---
function is_valid(_obj)
{
    // check validity of an object
    var rc  = false;
    var obj = _obj+'';

    if(obj != 'null' && obj != 'undefined')
    {
        rc = true;
    }
    return rc;
}

function find_object(_name, _document)
{
    var o = null;
    
    if(typeof(_name) == 'string' && _name.length > 0)
    {
        // looks for an object in a ducument
        var d = is_valid(_document) ? _document : document;
        var n = is_valid(_name) ? _name : '';

        var index = n.indexOf('?');

        if(index > 0 && parent.frames.length > 0)
        {
            d = parent.frames[n.substring(index+1)].document;
            n = n.substring(0, index);
        }

        o = d[n];

        if(!is_valid(o) && is_valid(d.all))
        {
            o = d.all[n];
        }
        
        for(var i = 0, len = d.forms.length; !is_valid(o) && i < len; i++)
        {
            o = d.forms[i][n];
        }

        for(var i = 0; !is_valid(o) && is_valid(d.layers) && i < d.layers.length; i++)
        {
            o = find_object(n, d.layers[i].document);
        }

        if(!is_valid(o) && is_valid(document.getElementById))
        {
            o = document.getElementById(n);
        }
    }        
    return o;
}

function x_error(_description)
{
    if(typeof(_description) == 'string')
    {
        alert(_description);
    }
}

// up-to-date browsers:
// --------------------
function is_valid_browser()
{
    return is_ie4up || is_nav4up || is_gecko || is_opera5up || is_aol6 || is_hotjava3up;
}

// rollover images
// -------- ------
//  document
//    |
//    --> uisoft (object)
//          |
//          ----> preloaded_images (array of preloaded images)
//          |
//          ----> dynamic_images (array of dynamic images)
//
function preload_images()
{
    //
    // usage:
    //  <body onload="preload_images('Images/Logo.gif', 'Images/MyImg.jpg');">
    //
    //  <script language="JavaScript">
    //  <!--
    //      preload_images('Images/Logo2.gif', 'Images/MyImg2.jpg');
    //   -->
    //   </script>
    //
    if(is_valid(document.images))
    {
        if(!is_valid(document.uisoft))
        {
            document.uisoft = new Object();
        }

        if(!is_valid(document.uisoft.preloaded_images))
        {
            document.uisoft.preloaded_images = new Array();
        }

        if(!is_valid(document.uisoft.dynamic_images))
        {
            document.uisoft.dynamic_images = new Array();
        }

        var j = document.uisoft.preloaded_images.length;
        var args = preload_images.arguments;

        for(var i = 0, len = args.length; i < len; i++)
        {
            document.uisoft.preloaded_images[j] = new Image();
            document.uisoft.preloaded_images[j++].src = args[i];
        }
    }
}

function onimage(_event)
{
    //
    //  params:
    //     _event: 'over', 'out'
    //      arguments (optional): 1 - image to change
    //                            2 - image to change with
    //                            ...
    //                            1   - image to change
    //                            n-1 - image to change with
    //      1 always is a string; 2 can be an object or a string (name of a image)
    //
    //  usage:
    //      <a href="target.htm" onmouseover="onimage('over', "exist_image", "Images/rollover_image.jpg")" onmouseout="onimage('out')"
    //          <img name="exist_image" src="existimage.jpg" width=140 height=44></img>
    //      </a>
    //
    if(typeof(_event) != 'string')
    {
        return;
    }

    if(!is_valid(document.uisoft) || !is_valid(document.uisoft.dynamic_images))
    {
        return;
    }

    var args = onimage.arguments;

    if(_event == 'over')
    {
        var len = args.length;
        
        if(len > 2)
        {
            for(var i = 1, n = len-1; i < n; i += 2)
            {
                var image = find_object(args[i]);

                if(is_valid(image))
                {
                    // zasushit' for 'out'
                    var k = document.uisoft.dynamic_images.length;

                    document.uisoft.dynamic_images[k] = image;
                    document.uisoft.dynamic_images[k].original_src = image.src;

                    // update source
                    image.src = typeof(args[i+1]) == 'object' ? args[i+1].src : args[i+1];
                }
            }
        }
    }
    else if(_event == 'out')
    {
        for(var i = 0, len = document.uisoft.dynamic_images.length; i < len; i++)
        {
            var image = document.uisoft.dynamic_images[i];
        
            if(is_valid(image))
            {
                image.src = document.uisoft.dynamic_images[i].original_src;
            }
        }

        document.uisoft.dynamic_images.length = 0;
    }
    return true;
}

function remove_array_entry(_array, _entry, _all)
{
    // params:
    //  _entry - 'object' | index in _array
    //  _all - removes all entries of _entry
    //
    var entry = typeof(_entry) == 'object' ? _entry : _array[_entry];

    if(is_valid(entry))
    {
        for(var i = 0; i < _array.length; i++)
        {
            if(_array[i] == entry)
            {
                if(is_valid(_array.splice))
                {
                   _array.splice(i, 1);
                }
                else
                {
                    for(var k = i; k < _array.length-1; k++)
                    {
                       _array[k] = _array[k+1];
                    }

                   _array.length -= 1;
                }

                if(!_all)
                {
                    break;
                }
            }
        }
    }
    return _array;
}

function dhtml_debug(_text)
{
    if(is_debug_mode == 1)
    {
        window.status = _text;
    }
}

function dhtml_prompt(_text)
{
    window.status = _text;
}

function round_off(_value, _precision)
{
    var result = 0;

    if(typeof(_value) == 'number' && _value != 0)
    {
        var value     = _value+'';
        var precision = is_valid(_precision) ? parseInt(precision) : 0;

        with(Math)
        {
            var result_str = round(value*pow(10, precision))+'';

            var period_pos = result_str.length-precision;

            if(period_pos > 0)
            {
                result = result_str.substring(0, period_pos);
                result += '.';
                result += result_str.substring(period_pos, result_str.length);
            }
            else
            {
                result = result_str;
            }
        }
    }        
    return result;
}

function dhtml_collection_to_array(_collection)
{
    var array = [];
    
    if(is_valid(_collection))
    {
        for(var i = 0, n = _collection.length; i < n; i++)
        {
            array[i] = _collection[i];
        }
    }
    return array;
}

function is_leap_year(_year)
{
    var rc = false;
    
    if(_year%4 == 0)
    {
        if(_year < 1582) // !
        {
            rc = true;
        }

        if(_year%100 != 0)
        {
            rc = true;
        }

        if(_year%400 != 0)
        {
            rc = true;
        }
    }
    return rc;
}

function array_from_string(_string)
{
    var array = null;

    if(typeof(_string) == 'string')
    {
        // create
        array = _string.split(';');

        // clean up
        if(is_valid(array))
        {
            for(var i = 0; i < array.length;)
            {
                var entry = array[i];
                var index = entry.indexOf('=');
                
                if(index <= 0)
                {
                    remove_array_entry(array, i, false);
                    i = 0;
                }
                else
                {
                    i++;
                }
            }
        }
    }
    return array;
}

function get_value_by_key(_key, _array)
{
    // _array - 'key=value' entries
    //  input 'key', returns 'value'
    var value = null;
    
    if(is_valid(_array) && typeof(_key) == 'string')
    {
        for(var i = 0, n = _array.length; i < n; i++)
        {
            var entry = _array[i];
            var index = entry.indexOf(_key);
            
            if(index == 0)
            {
                index = entry.indexOf('=');
                value = entry.substring(index+1, entry.length);
                break;
            }
        }
    }
    return value;
}

//
dhtml_months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
dhtml_days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
dhtml_days_long = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
dhtml_days_in_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
dhtml_first_day_of_each_month = [1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335];

// validate e-mail value
function is_email_valid(_value)
{
    var rc = false;

    if(is_valid(_value) && typeof(_value) == 'string')
    {
        if(is_opera)
        {
            rc = true; // this stupid browser does not recognize .match(...)
        }
        else
        {
            rc = _value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.tv)|(\.org)|(\..{2,2}))$)\b/gi);
        }
    }
    return is_valid(rc);
}

function validate_email(_field_id)
{
    var rc = false;

    if(is_valid_browser())
    {
        if(is_valid(_field_id) && typeof(_field_id) == 'string')
        {
            var field = find_object(_field_id);

            if(is_valid(field))
            {
                rc = is_email_valid(field.value);

                if(!rc)
                {
                    field.focus();
                    field.select();
                    alert('Please enter a valid e-mail address.');
                }
            }
        }
    }        
    return rc;
}

// today
// -----
function decorate_date(_date)
{
    //
    // usage:
    //   <script language="JavaScript">
    //   <!--
    //      document.write(decorate_date());
    //   -->
    //   </script>
    //
    var today = is_valid(_date) ? _date : new Date();
    var day   = dhtml_days_long[today.getDay()];
    var month = dhtml_months[today.getMonth()];
    var date  = today.getDate();
    var year  = today.getFullYear();
    //
    return day+', '+date+' '+month+', '+year;
}

// page guard
function page_guard(_base, _page)
{
    if(typeof(_page) == 'string' && _page.length > 0 && typeof(_base) == 'string' && _base.length > 0)
    {
        if(is_valid(document) && is_valid(document.referrer))
        {
            if(document.referrer.indexOf(_base) == -1)
            {
                // build query string
                var qstring = 'init='+
                              'is_nav4='+is_nav4+
                              ';is_nav4up='+is_nav4up+
                              ';is_nav6='+is_nav6+
                              ';is_nav6up='+is_nav6up+
                              ';is_gecko='+is_gecko+
                              ';is_ie='+is_ie+
                              ';is_ie3='+is_ie3+
                              ';is_ie4='+is_ie4+
                              ';is_ie4up='+is_ie4up+
                              ';is_ie5='+is_ie5+
                              ';is_ie5_5='+is_ie5_5+
                              ';is_ie5up='+is_ie5up+
                              ';is_ie5_5up='+is_ie5_5up+
                              ';is_ie6='+is_ie6+
                              ';is_ie6up='+is_ie6up+
                              ';is_aol='+is_aol+
                              ';is_aol3='+is_aol3+
                              ';is_aol4='+is_aol4+
                              ';is_aol5='+is_aol5+
                              ';is_aol6='+is_aol6+
                              ';is_opera='+is_opera+
                              ';is_opera2='+is_opera2+
                              ';is_opera3='+is_opera3+
                              ';is_opera4='+is_opera4+
                              ';is_opera5='+is_opera5+
                              ';is_opera5up='+is_opera5up+
                              ';is_dom='+is_dom+
                              '&page='+_page;

                // redirect
                location.href = 'Asp/Dispatcher.asp?'+qstring;
            }
        }
    }
}

function page_guard_lite(_page_to_go, _browser)
{
    if(is_valid(_browser))
    {
        if(_browser == false)
        {
            location.href = _page_to_go;
        }            
    }
    else if(!is_valid_browser())
    {
        location.href = _page_to_go;
    }
}

function modify_flags(_flags, _remove, _add)
{
   _flags = (_flags & ~_remove) | _add;
    return _flags;
}

////////////////////////////////////////////////////////////////////////////////////////

