// LDCC_Misc.js

// ==============================================================================
// Copyright © 2001-2008, LDC Consulting (LDCC). All rights reserved.
// No part of this document may be reproduced, stored in a retrieval system, or
// transmitted in any form or by any means, electronic, mechanical, photocopying,
// recording, or otherwise, without the prior permission of LDCC.
// ==============================================================================

// CSS CONSTANTS

// DISPLAY CONSTANTS

ALL_YEARS        = 0x00001;
BLANK_OPTION     = 0x00002;
COPY_FORM        = 0x00004;
DISABLED         = 0x00008;
FUTURE_YEAR      = 0x00010;
LEADING_ZERO     = 0x00020;
LOCAL_VALIDATION = 0x00040;
PASSWORD         = 0x00080;
PAST_YEAR        = 0x00100;
POPUP            = 0x00200;
PROMPT_AFTER     = 0x00400;
REQUIRED         = 0x00800;
SAME_LINE        = 0x01000;
SELECTED         = 0x02000;
SHOW_LENGTH      = 0x04000;
SHOW_SCORE       = 0x08000;
THIS_YEAR        = 0x10000;
VALUE_ONLY       = 0x20000;

// ENVIRONMENT CONSTANTS

ORG_ABBREV = 'WHOA';
ORG_NAME   = 'Wisconsin Hockey Officials Association';

// ==============================================================================

var strDialogReturn;

// ==============================================================================

function LDCC_AgeLevelChange (strSelect) {

    var strAssociation = $('#Association' + strSelect).val();
    var strAgeLevel = $('#AgeLevel' + strSelect).val();
    if ((strAssociation == '') || (strAgeLevel == ''))
      $('#Team' + strSelect).html('');
    else LDCC_SelectFill ('#Team' + strSelect, 'Team', '', strAgeLevel,
      $('#Association' + strSelect).val(), '', BLANK_OPTION);
    }

// ==============================================================================

function LDCC_AssociationChange (strSelect) {

    $('#Team' + strSelect).html('');
    //$('#Team' + strSelect).options.length = 0;
    var strAssociationID = $('#Association' + strSelect).val();
    if (strAssociationID == '') $('#AgeLevel' + strSelect).html('');
    //if (intAssociationID == -1) $('#AgeLevel' + strSelect).options.length = 0;
    else LDCC_SelectFill ('#AgeLevel' + strSelect, 'AgeLevel', '',
      strAssociationID, '', "", BLANK_OPTION);
    }

// ============================================================================

function LDCC_BuildEM (strShow, strName, strWho, strSub, strDomain) {

    var strAddr = strWho + '&#64;' + strSub + '.' + strDomain;
    document.write('<A href="' + 'mail' + 'to' + ':' + strName + ' <' +
      strAddr + '>">' + strShow + strAddr + '</A>');
    }

// ==============================================================================

function LDCC_DialogBox (strType, strMessage, objOptions) {

    var objShow = ((objOptions != null) ? objOptions : {});
    if (typeof (objShow.overlayspeed) == undefined)
      objShow.overlayspeed = 'fast';
    switch(strType) {
        case 'OK':
            var strDisplay =
              '<IMG src="../images/info.png" align="left" alt="OKAY">' +
              strMessage;
            if (typeof (objShow.buttons) == undefined)
              objShow.buttons = {OK: 1};
            break;
        case 'Mail':
            var strDisplay =
              '<IMG src="../images/info.png" align="left" alt="SENT">' +
              strMessage;
            if (typeof (objShow.buttons) == undefined)
              objShow.buttons = {OK: 1};
            break;
        case 'Inform':
            var strDisplay =
              '<IMG src="../images/info.png" align="left" alt="INFORM">' +
              strMessage;
            if (typeof (objShow.buttons) == undefined)
              objShow.buttons = {OK: 1};
            break;
        case 'Warning':
            var strDisplay =
              '<IMG src="../images/warn.png" align="left" alt="WARNING">' +
              strMessage;
            if (typeof (objShow.buttons) == undefined)
              objShow.buttons = {Yes: 1, No: 0};
            if (typeof (objShow.focus) == undefined)
              objShow.focus = 0;
            break;
        case 'Error':
            var strDisplay =
              '<IMG src="../images/error.png" align="left" alt="ERROR">' +
              strMessage;
            if (typeof (objShow.buttons) == undefined)
              objShow.buttons = {'Fix Errors': 1};
            break;
        default:
            var strDisplay = strMessage;
            if (typeof (objShow.buttons) == undefined)
              objShow.buttons = {OK: 1};
            break;
        }
    $.prompt(strDisplay, objShow).children('#jqi').corner('10px');
    }

// ==============================================================================

  function LDCC_DialogSubmit (strValue, objMessage){

      strDialogStatus = strValue;
      return true;
      }

// ==============================================================================

function LDCC_ExpandName(Name) {

    return Name;
    }

// ==============================================================================

function LDCC_ModalShow (objShow) {

    var objModal =
      ((typeof(objShow.options) == 'undefined') ? {} : objShow.options);

    var strTitle = ((typeof(objShow.title) == 'undefined') ? '' :
      '<DIV class="modalTitle">' + objShow.title + '</DIV>');

    if (typeof (objShow.type) == undefined) objShow.type = '';
    switch(objShow.type) {
        case 'OK':
            var strIcon =
              '<IMG src="../images/info.png" align="left" alt="OKAY">';
            if (typeof (objShow.buttons) == 'undefined') objShow.buttons =
              new Array (['OK', 'modalClose']);
            break;
        case 'Mail':
            var strIcon =
              '<IMG src="../images/info.png" align="left" alt="SENT">';
            if (typeof (objShow.buttons) == 'undefined') objShow.buttons =
              new Array (['OK', 'modalClose']);
            break;
        case 'Inform':
            var strIcon =
              '<IMG src="../images/info.png" align="left" alt="INFORM">';
            if (typeof (objShow.buttons) == 'undefined') objShow.buttons =
              new Array (['OK', 'modalClose']);
            break;
        case 'Warning':
            var strIcon =
              '<IMG src="../images/warn.png" align="left" alt="WARNING">';
            if (typeof (objShow.buttons) == 'undefined') objShow.buttons =
              new Array (['OK', 'modalDo'], ['Cancel', 'modalClose']);
            break;
        case 'Error':
            var strIcon =
              '<IMG src="../images/error.png" align="left" alt="ERROR">';
            if (typeof (objShow.buttons) == 'undefined') objShow.buttons =
              new Array (['Fix Errors', 'modalClose']);
            break;
        default:
            var strIcon = '';
            if (typeof (objShow.buttons) == 'undefined') objShow.buttons =
              new Array (['OK', 'modalClose']);
            break;
        }

    var strButtons = '';
    for (var ix = 0; ix < objShow.buttons.length; ix++) {
        strButtons = strButtons + ((strButtons == '') ?
          '<DIV style="clear: both; text-align: center;">' : '&nbsp;&nbsp;');
        strButtons = strButtons +
          '<INPUT type="button" value="' + objShow.buttons[ix][0] +
          '" class="' + objShow.buttons[ix][1] + '"/>';
        }
    if (strButtons != '') strButtons = strButtons + '</DIV>';

    if (typeof (objModal.close) == 'undefined') objModal.close = false;

    $.modal((strTitle + '<DIV class="modalDialog">' + strIcon + objShow.message +
      strButtons + '</DIV>'), objModal);
    }

// ==============================================================================

  function LDCC_PleaseWait (intStripes) {


      var intHeight = (arguments.length < 1) ? 10 : intStripes;
      return LDCC_PleaseWaitMessage ('&nbsp;Please Wait&nbsp;', intHeight);
      }

// ==============================================================================

  function LDCC_PleaseWaitMessage (strDisplayMessage, intStripes) {

      var intHeight = (arguments.length < 2) ? 10 : intStripes;
      var strStripes = (intHeight <= 0) ? '' :
        ('<TR height="' + intHeight + '">' +
           '<TD></TD><TD BGCOLOR="white"></TD>' +
           '<TD></TD><TD BGCOLOR="white"></TD>' +
           '<TD></TD><TD BGCOLOR="white"></TD>' +
           '<TD></TD><TD BGCOLOR="white"></TD>' +
           '<TD></TD><TD BGCOLOR="white"></TD>' +
           '<TD></TD><TD BGCOLOR="white"></TD>' +
           '<TD></TD>' +
           '</TR>');
      var strHTML =
        '<SPAN style="float: left">' +
        '<TABLE bgcolor="black" align="top" border="0" cellspacing="0">' +
          '<COLGROUP width="20" span="13" />' +
          strStripes +
          '<TR height="4">' +
            '<TD colspan="13"></TD>' +
            '</TR>' +
          '<TR>' +
            '<TD colspan="13" bgcolor="#FF6200"' +
                ' align="center" valign="top"' +
                ' style="margin: 0; font-size: 25px">' +
              '<B>&nbsp;&nbsp;' + strDisplayMessage + '&nbsp;&nbsp;</B>' +
              '</TD>' +
            '</TR>' +
          '<TR height="4"><TD colspan="13"></TD>' +
            '</TR>' +
          strStripes +
          '</TABLE></SPAN>';
      return strHTML;
      }

// ==============================================================================

function LDCC_SelectFill (strSelect, strSource, strSelected, strParent,
                          strGrandParent, strWhere, intOptions) {

    $('select' + strSelect).html('');
    var intValueOnly = ((intOptions & VALUE_ONLY) == VALUE_ONLY) ? 1 : 0;
    var strAction = 'action=SelectFill' +
                    '&Source='      + strSource +
                    '&Selected='    + strSelected +
                    '&Parent='      + strParent +
                    '&GrandParent=' + strGrandParent +
                    '&Where='       + strWhere +
                    '&ValueOnly='   + intValueOnly;
    $.ajax({
      type: 'POST',
      url: '../serv/AjaxServer.php',
      data: strAction,
      success: function (strResponse) {
          var objResponse = eval('(' + strResponse + ')');
          var strStatus = objResponse.status;
          if (strStatus == 'OK') {
              var intS = -1
              if ((intOptions & BLANK_OPTION) == BLANK_OPTION) {
                  $('select' + strSelect)
                    .append('<OPTION value=""></OPTION>');
                  if (strSelected == '') intS = 0;
                  }
              for (var intX = 0; intX < objResponse.options.length; intX++) {
                  strValue = objResponse.options[intX][0];
                  $('select' + strSelect).append('<OPTION value="' +
                    strValue + '"' + objResponse.options[intX][2] + '>' +
                    objResponse.options[intX][1] + '</OPTION>');
                  if (strValue == strSelected) intS = intX + 1;
                  }
              if (intS >= 0)
                $('select' + strSelect).each(function () {
                    this.selectedIndex = intS;
                    if (intValueOnly == 1) this.disabled = true;
                    //alert('sel val=' + this.options[this.selectedIndex].value);
                    //alert('sel text=' + this.options[this.selectedIndex].text);
                    });
              }
          else alert(strStatus);
          }
      });
    }

// ==============================================================================

function LDCC_SendMail (strSubject, strTo, strCC, strBCC) {

    var strLinkTo = 'mailto:' + strTo;
    var strSeparator = '?';
    if (strCC != '') {
        strLinkTo += strSeparator + 'cc=' + strCC;
        strSeparator = '&';
        }
    if (strBCC != '') {
        strLinkTo += strSeparator + 'bcc=' + strBCC;
        strSeparator = '&';
        }
    if (strSubject != '') {
        strLinkTo += strSeparator + 'subject=' + strSubject;
        strSeparator = '&';
        }
    window.open(strLinkTo);
    }

// ==============================================================================

function LDCC_SendTo (strSubject, strTo, strToAt, strToCom, strCC, strCCAt,
                      strCCCom) {

    if (strToAt == '') var strISP = 'wihoa.org';
    else var strISP = strToAt + '.' + strToCom;
    var strLinkTo = 'mailto:' + strTo + '@' + strISP;
    var strSeparator = '?';
    if (strCC != '') {
        if (strCCAt == '') strISP = 'wihoa.org';
        else strISP = strCCAt + '.' + strCCCom;
        strLinkTo += strSeparator + 'cc=' + strCC + '@' + strISP;
        strSeparator = '&';
        }
    if (strSubject != '') {
        strLinkTo += strSeparator + 'subject=' + strSubject;
        strSeparator = '&';
        }
    window.open(strLinkTo);
    }

// ==============================================================================

function LDCC_SentAt() {

    var arrMonths = new Array('January', 'February', 'March', 'April', 'May',
      'June', 'July', 'August', 'September', 'October', 'November', 'December');
    var arrWeekdays = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday',
      'Thursday', 'Friday', 'Saturday');

    var datNow = new Date();
    var intHour = datNow.getHours();
    var intMinutes = datNow.getMinutes();
    var strHalf = 'PM';
    if (intHour > 12) {
        intHour -= 12;
        if (intHour == 12) strHalf = 'AM';
        }
    else if (intHour < 12) strHalf = 'AM';
    return arrWeekdays[datNow.getDay()] + ', ' +
      arrMonths[datNow.getMonth()] + ' ' + datNow.getDate() + ', ' +
      datNow.getFullYear() + ' ' + intHour + ':' +
        ((intMinutes < 10) ? '0' : '') + intMinutes + ' ' + strHalf;
    }

// ============================================================================

Holidays = [[1, 1, -1, 'US'], [7, 4, -1, 'Fourth'], [12, 24, -1, 'Christmas'],
            [12, 25, -1, 'Christmas'],
            [12, 31, -1, 'US'],
            [5, 26, 2008, 'US']];

function LDCC_ShowHolidays (date) {

    for (var i = 0; i < Holidays.length; i++) {
        if (date.getMonth() == (Holidays[i][0] - 1)) {
            if (date.getDate() == Holidays[i][1]) {
                if ((Holidays[i][2] == -1) ||
                    (date.getFullYear() == Holidays[i][2]))
                  return [true, Holidays[i][3] + '_day'];
                }
            }
        }
    return [true, ''];
    }

// ==============================================================================

function LDCC_ShowLink (objShow) {

    switch (objShow.type) {
        case 'Ext':
            window.open(objShow.src);
            break;
        case 'Int':
            window.open(objShow.src);
            break;
        default:
            window.open(objShow.src);
        }
    return false;
    }

// ==============================================================================

function LDCC_ShowObject (objShow, intSkip) {

    var strShow = '';
    var intX = 0;
    for (var prop in objShow)
        if (intX++ > intSkip)
          strShow += '[' + prop + ']:' + objShow[prop] + '\n';
    alert(strShow);
    }

// ==============================================================================

function LDCC_ValueString (strBefore, strSelect, blnSendEmpty, strBetween) {

    var arrFields = new Array();
    $(strSelect).each(function () {
        switch (this.type) {
            case 'text':
            case 'textarea':
            case 'hidden':
            case 'password':
            case 'FileUpload':
                if ((this.value != '') || blnSendEmpty)
                  arrFields.push(encodeURIComponent(this.name) + '=' +
                    encodeURIComponent(this.value));
                break;
            case 'radio':
/*
                for (var intX = 0; intX < this.length; intX++) {
                    if (objForm.ScreenCheck[intX].checked) {
                        objForm.ScreenCode.value =
                          objForm.ScreenCheck[intX].value;
                        break;
                        }
                    }
                break;
*/
            case 'checkbox':
                if (this.checked)
                  arrFields.push(
                    encodeURIComponent(this.name) + '=' +
                    encodeURIComponent(this.value));
                break;
            case 'select-one':
                var intIndex = (this.selectedIndex) ? this.selectedIndex : 0;
                var strName = this.name;
                if ((intIndex >= 0) && (intIndex < this.options.length)) {
                    var strValue = this.options[intIndex].value;
                    arrFields.push(encodeURIComponent(strName) + '=' +
                      encodeURIComponent(strValue));
                    strName += 'Text';
                    strValue = this.options[intIndex].text;
                    arrFields.push(encodeURIComponent(strName) + '=' +
                      encodeURIComponent(strValue));
                    }
                else
                  arrFields.push(encodeURIComponent(strName) + '=' +
                    encodeURIComponent("ix out of range"));
/*
                if (this.selectedIndex) {
                    var intIndex = this.selectedIndex;
                    if ((intIndex >= 0) && (intIndex < this.options.length)) {
                        var strName = this.name;
                        var strValue = this.options[intIndex].value;
                        arrFields.push(encodeURIComponent(strName) + '=' +
                          encodeURIComponent(strValue));
                        strName += 'Text';
                        strValue = this.options[intIndex].text;
                        arrFields.push(encodeURIComponent(strName) + '=' +
                          encodeURIComponent(strValue));
                        }
                    else
                      arrFields.push(encodeURIComponent(this.name) + '=' +
                        encodeURIComponent("ix out of range"));
                    }
                else
                  arrFields.push(encodeURIComponent(this.name) + '=' +
                    encodeURIComponent("ix undefined"));
*/
                break;
            case 'select-multiple':
                for (var intS = 0;
                  intS < this.options.length; intS++) {
                    if (this.options[intS].selected) {
                        if(this.options[intS].value == '')
                          arrFields.push(
                            encodeURIComponent(this.name) + '=' +
                            encodeURIComponent(this.options[intS].text));
                        else
                          arrFields.push(encodeURIComponent(this.name) + '=' +
                            encodeURIComponent(this.options[intS].value));
                        }
                    }
                break;
            default:
                break;
            }
        });
    var strSeparate = (arguments.length < 4) ? '&' : strBetween;
    return strBefore + arrFields.join(strSeparate);
    }

// ============================================================================

function modalClose (dialog) {

    dialog.data.fadeOut('slow', function () {
        dialog.container.hide('slow', function () {
            dialog.overlay.slideUp('slow', function () {
                $.modal.close();
                });
            });
        });
    }

// ============================================================================

function modalOpen (dialog) {

    dialog.overlay.fadeIn('slow', function () {
        dialog.container.fadeIn('slow', function () {
            dialog.data.slideDown('slow');
            });
        });
    }

// ============================================================================

function modalShow (dialog) {

    dialog.data.find('input.animate').one('click', function () {
        dialog.data.slideUp('slow', function () {
            dialog.data.slideDown('slow');
            });
        });
    }

// ==============================================================================

$(document).ready(function() {

    $('.Linker') // Find all Linker elements
/*
      .mouseover(function () {
          window.status = this.title;
          return true;
          })
      .mouseout(function () {
          window.status = 'Done';
          return true;
          })
*/
      .filter('<IMG>').attr('border', 0)
      .end().filter('.DocLink') // Find all DocLink elements
        .wrap('<A class="LinkNoUnderline" href=""></A>')
        .hover(
          function () { $(this).addClass('ExternalOver'); },
          function () { $(this).removeClass('ExternalOver'); })
      .end().filter('.ExternalLink') // Find all ExternalLink elements
        .wrap('<A href=""></A>')
        .hover(
          function () { $(this).addClass('ExternalOver'); },
          function () { $(this).removeClass('ExternalOver'); })
      .end().filter('.HelpLink') // Find all HelpLink elements
        .attr('src', '../images/Help5.bmp')
        //.attr('border', 0)
        .hover(
          function () { $(this).attr('src', '../images/HelpR.bmp'); },
          function () { $(this).attr('src', '../images/Help.bmp'); })
      .end().filter('.InternalLink') // Find all InternalLink elements
        .wrap('<A href=""></A>')
        .hover(
          function () { $(this).addClass('InternalOver'); },
          function () { $(this).removeClass('InternalOver'); })
      .end().filter('.MailLink') // Find all MailLink elements
        .wrap('<A href=""></A>')
        .hover(
          function () { $(this).addClass('ExternalOver'); },
          function () { $(this).removeClass('ExternalOver'); })
      .end().filter('.MapLink') // Find all MapLink elements
        .wrap('<A href=""></A>')
        .hover(
          function () {
              $(this).addClass('ExternalOver');
              $.meta.setType('class');
              var strMessage = $(this).data().XStatus;
              if (! strMessage) strMessage = '';
              //LDCC_StatusLine (strMessage);
              //window.status = strMessage;
              },
          function () { $(this).removeClass('ExternalOver'); })
      .end().filter('.NewsLink') // Find all NewsLink elements
        .wrap('<A href=""></A>')
        .hover(
          function () {
              $(this).addClass('ExternalOver');
              $.meta.setType('class');
              var strMessage = $(this).data().XStatus;
              if (! strMessage) strMessage = '';
              LDCC_StatusLine (strMessage);
              },
          function () { $(this).removeClass('ExternalOver'); })
      .end().filter('.PopupLink') // Find all PopupLink elements
        .wrap('<A href=""></A>')
        .hover(
          function () { $(this).addClass('ExternalOver'); },
          function () { $(this).removeClass('ExternalOver'); })
    });