﻿// JScript File
function checkKeys(e)
{
      var kC  = (window.event) ?    // MSIE or Firefox?
                 event.keyCode : e.keyCode;
      var Esc = (window.event) ?   
                27 : e.DOM_VK_ESCAPE // MSIE : Firefox
      if(kC==Esc) {
         PanicButton();
      }  
}

//this works!
function disableEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}

function AddShiftToArray2(txtid, ddl)
{

//create array    
    var ShiftArray = [];
    var txt = document.getElementById(txtid);
    
    var idsplit = ddl.name.split("_");
    var shift_date = idsplit[0];
    var shift_id = idsplit[1];
    var advocate_id = ddl.value;

    if (txt.value == '') {
        ShiftArray = [ddl.name, shift_date, shift_id, advocate_id];
    } else {
        //alert("VALUES ALREADY");
        ShiftArray = txt.value.split(",");
        //alert("Array Length = "+ShiftArray.length);
        //check if ddl.name exists already
        var existidx = ShiftArray.indexOf(ddl.name);
        //alert("Index:"+existidx);
        if (existidx == -1) {
            //alert("NOT EXISTING");
            ShiftArray.push(ddl.name, shift_date, shift_id, advocate_id);
        } else {
            //alert("EXISTING");
            ShiftArray[existidx+3] = ddl.value;
        }
    }
    txt.value = ShiftArray;
   
}

if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

function AddShiftToArray(txtid, ddlid)
{    
    //alert(ddl.value);
    var txt = document.getElementById(txtid);
    var AddedShifts = txt.value;
    if (AddedShifts == '') {
//        alert("empty");
        AddedShifts = ddlid;
    } else {
//        alert("Values");
        if (!ContainedinArray(AddedShifts, ddlid)) {
            AddedShifts += ','+ddlid;
        }
    }
    txt.value = AddedShifts;
}

function ContainedinArray(arraystring, arrayitem)
{
    //alert(arrayitem);
    var index = arraystring.indexOf(arrayitem);
    if (index==-1) {
        return false;
    } else {
        return true;
    }
}

function ChangeCheckBoxState(id, checkState)
{
    var cb = document.getElementById(id);
    if (cb != null)
       cb.checked = checkState;
}

function ChangeAllCheckBoxStates(checkState, grdType)
{
    // Toggles through all of the checkboxes defined in the CheckBoxIDs array
    // and updates their value to the checkState input parameter
    
    switch (grdType)
    {
        case 'Sent':
            var CheckBoxIDs = CheckBoxIDs_Sent;
            break
        case 'Received':
            var CheckBoxIDs = CheckBoxIDs_Received;
            break
        case 'Admin':
            var CheckBoxIDs = CheckBoxIDs_Admin;
            break
    }
        
    
//    if (grdType=='Sent') {
//        var CheckBoxIDs = CheckBoxIDs_Sent;
//    } else {
//        var CheckBoxIDs = CheckBoxIDs_Received;
//    }

    
    if (CheckBoxIDs != null)
    {
        for (var i = 0; i < CheckBoxIDs.length; i++)
           ChangeCheckBoxState(CheckBoxIDs[i], checkState);
    }
}

function ChangeHeaderAsNeeded(grdType)
{
    // 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
    
    //var CheckBoxIDs = CheckBoxIDs_.grdType;
    
    switch (grdType)
    {
        case 'Sent':
            var CheckBoxIDs = CheckBoxIDs_Sent;
            break
        case 'Received':
            var CheckBoxIDs = CheckBoxIDs_Received;
            break
        case 'Admin':
            var CheckBoxIDs = CheckBoxIDs_Admin;
            break
    }
    
//    if (grdType=='Sent') {
//        var CheckBoxIDs = CheckBoxIDs_Sent;
//    } else {
//        var CheckBoxIDs = CheckBoxIDs_Received;
//    }
    
    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 ConfirmDelete()
{
    return confirm('Are you sure you want to delete the selected item(s)?');
}

//this has elements that work...
function ConvertEnterToTab($char, $mozChar)
{
    if($mozChar != null || $mozChar == 13) { // Look for a Mozilla-compatible browser
        alert("Mozilla");
        alert($mozChar);
        return false;
    } else if ($char !=null || $char == 13) { // Must be an IE-compatible Browser
        $char = 9;
        return $char;
        //event.returnValue=false;
        //event.cancel =true;
    }
}

function PanicButton()
{
    window.location.href = 'http://www.google.com/';
    /*if(navigator.userAgent.indexOf("Firefox")!=-1){
        varHowLong = "50";
        varURL = "http://www.google.com";
        self.setTimeout("self.location.href = varURL;",varHowLong);
    } else {
        window.opener='x';
        window.close();
    }*/
}

