/*
  checkNewName
  ------------
  (array) names is an array of strings
  (str) newName is the name you want to add
  
  (cc) MMVII, Robert Otani. otanistudio.com
  This work is licensed under a 
  Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
  http://creativecommons.org/licenses/by-nc-sa/2.5/
  
*/

function checkNewName( names, newName) {
  var found = false;
  /* insert your own fast binary tree search
     here if you care */
  for (var i=0; i < names.length; i++) {
    if (newName === names[i]) {
      found = true;
      break;
    }
  }
  if(found) {
    var enm = newName.replace(/^(.*)-(\d+)$/, "$2" );
    if (isNaN(parseInt(enm)) || enm === newName) {
      newName = newName + "-" + 1;
    } else {
      enm = parseInt(enm) + 1;
      newName = newName.replace(/^(.*)-(\d+)/, "$1") + "-" + enm;
    }
    return checkNewName(names, newName);  // for those who have asked, the recursion happens here
  } else {
    return newName;
  }
}


