// Everything here copyright (c) NitroSell Limited
// NO REPRODUCTION OF *ANY* KIND PERMITTED!

/** list windows **/
function displaySelectionWindow(aryLabels, strSelectDiv, strConfirmFunction) {
  var strListSelectionContent = 
    "<div id='listselectiondiv' style='width:400px; height:150px;'>"+
    "<div id='listselectiontitle' style='width:400px; height:25px; background-color:#ddd; font-size:16px; font-weight: bold; text-align:center; padding-top: 5px;'>"+aryLabels[0]+"</div>"+
    "<form id='listselctionform' name='listselectionform' onsubmit='return "+strConfirmFunction+"'; return false;'>"+
    "<table cellspacing='0' cellpadding='5' border='0' id='listselectiontable' style='padding:10px;'>"+
    "<tr><td><label for='listselect'>"+aryLabels[1]+"</label></td><td align='left'>"+strSelectDiv+"</td></tr>"+
    "<tr><td><label for='newListNameEntry'>"+aryLabels[2]+"</label></td>"+
    "<td><input name='newListNameEntry' id='newListNameEntry' style='width:200px;' maxlength='100'></td></tr></table>"+
    "<div id='newlistdivbuttons' style='width:400px; height:25px; text-align:center;'>"+
    "<input type='button' name='OK' value='"+aryLabels[3]+"' style='width:100px;' onClick='"+strConfirmFunction+";'> "+
    "<input type='button' name='Cancel' value='"+aryLabels[4]+"' style='width:100px;' onClick='hideModalWindow();'></div>"+
    "</form></div>";
  showModalWindow(400, 150, strListSelectionContent);
  
  pauseIE(250);
  
  if (document.getElementById('listselect').options.length > 1) {
    document.getElementById('listselect').focus();
    document.getElementById('newListNameEntry').disabled = true;
  } else {
    document.getElementById('newListNameEntry').focus();
  }
  return false;
}

function displayDeleteWindow(aryLabels, strConfirmFunction) {
  var strListDeleteContent = 
      "<div id='deletelistdiv' style='width:320px; height:140px;'>"+
      "<div id='deletelistdivtitle' style='width:320px; height:25px; background-color:#ddd; font-size:16px; font-weight: bold; text-align:center; padding-top: 5px;'>"+aryLabels[0]+"</div>"+
      "<form id='listdeleteform' name='listdeleteform' onsubmit='return "+strConfirmFunction+"; return false;'>"+
      "<table cellspacing='0' cellpadding='5' border='0' id='listrenametable' style='padding:10px;'>"+
      "<tr><td><p>"+aryLabels[1]+"</p></td></tr></table>"+
      "<div id='deletelistdivbuttons' style='width:320px; height:25px; text-align:center;'>"+
      "<input type='button' name='Yes' id='deleteyesbutton' value='"+aryLabels[2]+"' style='width:100px;' onClick='"+strConfirmFunction+";'> "+
      "<input type='button' name='No' value='"+aryLabels[3]+"' style='width:100px;' onClick='hideModalWindow();'></div>"+
      "</form></div> ";
  showModalWindow(320, 140, strListDeleteContent);
  pauseIE(250);
  document.getElementById('deleteyesbutton').focus();
}

function displayRenameWindow(aryLabels, strConfirmFunction) {
  var strCurrentName = document.forms.frmMain.listName.value;
  var strListRenameContent = 
      "<div id='renamelistdiv' style='width:350px; height:120px;'>"+
      "<div id='renamelistdivtitle' style='width:350px; height:25px; background-color:#ddd; font-size:16px; font-weight: bold; text-align:center; padding-top: 5px;'>"+aryLabels[0]+"</div>"+
      "<form id='listrenameform' name='listrenameform' onsubmit='return "+strConfirmFunction+"; return false;'>"+
      "<table cellspacing='0' cellpadding='5' border='0' id='listrenametable' style='padding:10px;'>"+
      "<tr><td><label for='renameListEntry'>"+aryLabels[1]+"</label></td>"+
      "<td><input name='renameListEntry' id='renameListEntry' style='width:200px;' maxlength='100' value=\""+strCurrentName+"\"></td></tr></table>"+
      "<div id='renamelistdivbuttons' style='width:350px; height:25px; text-align:center;'>"+
      "<input type='button' name='OK' value='"+aryLabels[2]+"' style='width:100px;' onClick='"+strConfirmFunction+";'> "+
      "<input type='button' name='Cancel' value='"+aryLabels[3]+"' style='width:100px;' onClick='hideModalWindow();'></div>"+
      "</form></div> ";
  showModalWindow(350, 120, strListRenameContent);
  pauseIE(250);
  document.getElementById('renameListEntry').focus();
}

/** renaming functions **/
function renameList() {
  displayRenameWindow(['Rename List', 'List name:', 'OK', 'Cancel'], 'checkRenameList()');
}

function renameRegistry() {
  displayRenameWindow(['Rename Registry', 'Registry name:', 'OK', 'Cancel'], 'checkRenameList()');
}

function checkRenameList() {
  var bValid = false;
  var strNewName = document.getElementById('renameListEntry').value;
  if (strNewName && strNewName.length > 0) {
    bValid = true;
    hideModalWindow();
    document.forms.frmMain.listName.value = strNewName;
    document.forms.frmMain.action.value = 'Rename List';
    document.forms.frmMain.submit();
  }
  
  if (!bValid) {
    alert("Please enter a valid name!");
    document.getElementById('renameListEntry').focus();
  }
  return false;
}

/** deletion functions **/
function deleteList() {
  displayDeleteWindow(['Delete List Confirmation', 'Are you sure you wish to delete this list (<strong>'+document.forms.frmMain.listName.value+'</strong>)?', 'Yes', 'No'], 'checkDeleteList()');
}

function deleteRegistry() {
  displayDeleteWindow(['Delete Registry Confirmation', 'Are you sure you wish to delete this registry (<strong>'+document.forms.frmMain.listName.value+'</strong>)?', 'Yes', 'No'], 'checkDeleteList()');
}

function checkDeleteList() {
  hideModalWindow();
  document.forms.frmMain.action.value = 'Delete List';
  document.forms.frmMain.submit();
}

/** add to list functions **/
function displayListSelection(bSuccess, strSelectDiv) {
  if (bSuccess) {
    displaySelectionWindow(['Add to List', 'Select list:', 'List name:', 'OK', 'Cancel'], strSelectDiv, 'checkListName(false)');
    return false;
  }
}

function addToList(nProductId, nQuantity) {
  document.forms.frmAddToListHandler.listProdcode.value = nProductId;
  document.forms.frmAddToListHandler.quantity.value = nQuantity;
  document.forms.frmAddToListHandler.action.value = "Add to List";
  ajax_get('/store/store.asp?action=do&doaction=getuserlists',displayListSelection);
}

function displayAddAllListSelection(bSuccess, strSelectDiv) {
  if (bSuccess) {
    displaySelectionWindow(['Add All to List', 'Select list:', 'List name:', 'OK', 'Cancel'], strSelectDiv, 'checkListName(true)');
    return false;
  }
}

function addAllToList() {
  document.forms.frmMultiAddProductHandler.action.value = "Add to List";
  ajax_get('/store/store.asp?action=do&doaction=getuserlists',displayAddAllListSelection);
}

/** Add to registry functions **/
function displayRegistrySelection(bSuccess, strSelectDiv) {
  if (bSuccess) {
    displaySelectionWindow(['Add to Registry', 'Select registry:', 'Registry name:', 'OK', 'Cancel'], strSelectDiv, 'checkListName(false)');
    return false;
  }
}

function addToRegistry(nProductId, nQuantity) {
  document.forms.frmAddToListHandler.listProdcode.value = nProductId;
  document.forms.frmAddToListHandler.quantity.value = nQuantity;
  document.forms.frmAddToListHandler.action.value = "Add to Registry";
  ajax_get('/store/store.asp?action=do&doaction=getuserregistries',displayRegistrySelection);
}

function displayAddAllRegistrySelection(bSuccess, strSelectDiv) {
  if (bSuccess) {
    displaySelectionWindow(['Add All to Registry', 'Select registry:', 'Registry name:', 'OK', 'Cancel'], strSelectDiv, 'checkListName(true)');
    return false;
  }
}

function addAllToRegistry() {
  document.forms.frmMultiAddProductHandler.action.value = "Add to Registry";
  ajax_get('/store/store.asp?action=do&doaction=getuserregistries',displayAddAllRegistrySelection);
}

/** move to list functions **/
function displayMoveToListSelection(bSuccess, strSelectDiv) {
  if (bSuccess) {
    displaySelectionWindow(['Move to List', 'Select list:', 'List name:', 'OK', 'Cancel'], strSelectDiv, 'checkListName(false)');
    return false;
  }
}

function moveToList() {
  document.forms.frmAddToListHandler.action.value = "Move to List";
  ajax_get('/store/store.asp?action=do&doaction=getuserlists',displayMoveToListSelection);
}

/** move to registry functions **/
function displayMoveToRegistrySelection(bSuccess, strSelectDiv) {
  if (bSuccess) {
    displaySelectionWindow(['Move to Registry', 'Select registry:', 'Registry name:', 'OK', 'Cancel'], strSelectDiv, 'checkListName(false)');
    return false;
  }
}

function moveToRegistry() {
  document.forms.frmAddToListHandler.action.value = "Move to Registry";
  ajax_get('/store/store.asp?action=do&doaction=getuserregistries',displayMoveToRegistrySelection);
}

/** list/registry validation and submission **/
function selectList(nListID) {
  var objNewListEntryBox = document.getElementById('newListNameEntry');
  if (nListID == 0 || document.getElementById('listselect').options.length == 1) {
    objNewListEntryBox.disabled = false;
    objNewListEntryBox.focus();
  } else {
    objNewListEntryBox.disabled = true;
  }
}

function checkListName(bMultiAdd) {
  var bValid = false;
  var objListSelect = document.getElementById('listselect');
  var nListId = 0;
  
  if (objListSelect.options[objListSelect.selectedIndex].value == 0) {
    var strNewName = document.getElementById('newListNameEntry').value;
    if (strNewName && strNewName.length > 0) {
      bValid = true;
      document.getElementById('newListName').value = strNewName;
    }
  } else {
    nListId = objListSelect.options[objListSelect.selectedIndex].value;
    bValid = true;
  }
  
  if (bValid) {
    hideModalWindow();
    
    if (document.forms.frmAddToListHandler.action.value == "Move to List" || document.forms.frmAddToListHandler.action.value == "Move to Registry") {
      if (nListId == 0)
        document.forms.frmMain.newListName.value = document.forms.frmAddToListHandler.newListName.value;
      document.forms.frmMain.action.value = document.forms.frmAddToListHandler.action.value;
      document.forms.frmMain.listId.value = nListId;
      document.forms.frmMain.submit();
    } else if (bMultiAdd) {
      if (nListId == 0)
        document.forms.frmMultiAddProductHandler.newListName.value = document.forms.frmAddToListHandler.newListName.value;
      document.forms.frmMultiAddProductHandler.listId.value = nListId;
      document.forms.frmMultiAddProductHandler.submit();
    } else {
      document.forms.frmAddToListHandler.listId.value = nListId;
      document.forms.frmAddToListHandler.submit();
    }
  } else {
    alert("Please enter a valid name!");
    document.getElementById('newListNameEntry').focus();
  }
  return false;
}