// i think this is defunct
function changeSecurityType(value) {
    switch(value) {
    case '':
        break;
    default:
    	if (value == 'High Grade' || value == 'Credit' || value.match(/^QIB/)){
    	    LoadFrames('leftNav.php?graveyardMode=0&securityType=' + value, "/searchSecurities.php?graveyardMode=0&securityType=" + value);
    	}	else {
    	    LoadFrames('leftNav.php?graveyardMode=0&securityType=' + value, "/news.php?MODE=LIST&graveyardMode=0&securityType=" + value);
    	}
    }
}

function gotoContact() {
    if (top.leftFrame){
        LoadFrames('', 'contact.html');
    } else {
        top.location.href = '/contact.php';
    }
}

function logout() {
    location.href = '/logout.php';
}

function login() {
    location.href = '/newHome.php';
}

var chargeToFlag = false;

function changeChargeToInit() {
  $('selectChargeTo').disabled = false;
  $('selectChargeTo').load('changeBillToClient.php?MODE=getOptions&r=' + Math.random());
  $('selectChargeTo').setStyle('display', '');
  $('currentlyChargingTo').hide();
  $('chargeToBut').hide();
  chargeToFlag = true;
}

function clientPoll(){
    if (!$('currentlyChargingTo'))
        return;
    var req = new Request.JSON({url: 'changeBillToClient.php?MODE=poll',
                             method: 'get',
                          onSuccess: function(message) {
                if (message.data.status == 'success') {
                    if (!chargeToFlag) {
                        if ($('currentlyChargingTo').value != message.data.billToID) {
                            if (window.location.href.indexOf('compareCovsAcross') != -1) {
                                undoSearch();
                                undoSearch();
                                undoSearch();
                                undoSearch();
                                undoSearch();
                            }
                            if (window.location.href.indexOf('searchSecurities') != -1) {
                                var elements = $$('input');
                                for (var i =0; i < elements.length; i++) {
                                    if (elements[i].name.substring(0, 8) == 'compare[') {
                                        elements[i].disabled = true;
                                    }
                                }
                                for (var j=0; j < message.data.compares.length; j++) {
                                    var id = $('compare[' + message.data.compares[j] + ']');
                                    if (id != null)
                                        id.disabled = false;
                                }
                            }
                        }
                        if (message.data.billToID == '')
                            $('currentlyChargingTo').value = '';
                        else
                            $('currentlyChargingTo').value = message.data.billToID;
                    }
                }
                var t = setTimeout('clientPoll()', 30000);
            }
        });
    req.send(null);
}

function onChargeToChange(th) {
  charge = $('currentlyChargingTo');
  if (th == 0)	{
    changeChargeToComplete();
  } else if (th == -1)	{
    wait("<center>New Client</center>", 
         '<center>Please enter the new client name:<br />' +
         '<input type="text" name="newClientName" id="newClientName" /><br /><br />' + 
         '<input type="button" onclick="saveNewChargeTo()" value="Save" /><input type="button" onclick="changeChargeToComplete();activate()" value="Cancel" /></center>');
    if ($('currentMask').value != '')
      $('newClientName').meiomask('fixed.client');
    
  } else {
    charge.value = th;
    charge.disabled = true;
    changeChargeToComplete();
  }
}

function saveNewChargeTo() {
  $('selectChargeTo').options[$('selectChargeTo').options.length] = new Option($('newClientName').value, $('newClientName').value, true, true);
  charge.value = $('newClientName').value;
  changeChargeToComplete();
  activate();  
}

function changeChargeToComplete() {
  var charge = $('currentlyChargingTo');
  if ($('selectChargeTo').options[$('selectChargeTo').selectedIndex].value <= 0) {
    $('currentlyChargingTo').disabled = true;
    selectCharge = $('selectChargeTo').disabled = true;
    $('currentlyChargingTo').show();
    $('selectChargeTo').hide();
    chargeToFlag = false;
    $('chargeToBut').show();
    clientPoll();
    return;
  }
  var req = new Request.JSON({url: 'changeBillToClient.php?MODE=changeClient&d=' + charge.value,
                           method: 'get',
                        onSuccess: function(message) {
    if (message.status == 'success') {
      if (window.location.href.indexOf('searchString') != -1) {
        var elements = $$('input');
        for (var i=0; i < elements.length; i++) {
          if (elements[i].name.substring(0, 8) == 'compare[') {
            elements[i].disabled = true;
          }
        }
        for (var j =0; j < message.data.compares.length; j++) {
          var id = $('compare[' + message.data.compares[j] + ']');
            if (id != null)
              id.disabled = false;
        }
      }
      if (window.location.href.indexOf('compareCovsAcross') != -1) {
        undoSearch();
        undoSearch();
        undoSearch();
        undoSearch();
        undoSearch();
      }
      $('currentlyChargingTo').disabled = true;
      selectCharge = $('selectChargeTo').disabled = true;
      $('currentlyChargingTo').show();
      $('selectChargeTo').hide();
      chargeToFlag = false;
      $('chargeToBut').show();
      alert("Change of Client Successful");
    } else {
      alert("Change of Client to Charge to Failed due to network failure");
    }
  }});
  req.send(null);
}


function chargeTo() {
  if (chargeToFlag)
      changeChargeToComplete();
  else
      changeChargeToInit();
}
var t = setTimeout('clientPoll()', 1000);

window.addEvent('load', function() {
  if ($('currentMask'))
    Meio.Mask.createMasks('Fixed', {'client': { mask: $('currentMask').value }});
});