/* gloabl variable for root name of browser category tree                       */
var ROOT_YP = "YP"; var ROOT_BUSINESS = "Business"; var ROOT_YP_SP = "YP_SP";
var MENU_ACTION_VIEW = "View";
var MENU_ACTION_ADD = "Add";
var MENU_ACTION_MODIFY = "Modify";
var MENU_ACTION_DELETE = "Delete";
var MENU_ACTION_FIND_CAT = "FindCategory";
/*-----------------------------------------------------------------------------*/
function popupConsolidDoc()
{
   var popup = window.open("CatConsol_Facts1.htm", null, "toolbar=no,status,resizable,scrollbars,width=750,height=700");

}


function generateXml() {
   var formName = "categoryItemForm";
  if (document.forms[formName] == null || document.forms[formName].elements["_menuAction"] == null)
    return;

   if (!confirm("Are you sure you want to generate the xml")) {
       return;
   }

  document.body.style.cursor='wait';

  document.forms[formName].elements["_menuAction"].value = "GenerateXml";

  document.forms[formName].submit();

}

function showHideCategoryItem(){
  var formName = "categoryItemForm";
  var cateType = document.forms[formName].elements["childType"].value;

  if (cateType == "LE" || cateType == "EX"){
    document.forms[formName].all["mappingItemCaption"].style.display="";
    document.forms[formName].all["mappingField"].style.display="";
  }
  else {
   document.forms[formName].all["mappingItemCaption"].style.display="none";
   document.forms[formName].all["mappingField"].style.display="none";
  }
  if (cateType == "SA") { //"catAltNameSpanId"
       document.forms[formName].all["catAltNameSpanId"].style.display="none";
       document.forms[formName].all["category_alt_name"].style.display="none";
  }
  else {
    document.forms[formName].all["catAltNameSpanId"].style.display="";
    document.forms[formName].all["category_alt_name"].style.display="";
  }
}

/*
function changeCheckboxLabel() {
  var formName = "categoryItemForm";
  var source =  document.forms[formName].elements["search_source"].value;
  var str = "Show alternative category name";
  if (source == "US-ENGLISH") {
     str = "Show Spanish Translation";
  }
  else if (source == "US-SPANISH") {
     str = "Show English Translation";
  }
  else if (source == "CA-ENGLISH") {
     str = "Show French Translation";
  }
  else if (source == "CA-FRENCH") {
     str = "Show English Translation";
  }

  document.all["checkboxLabelId"].innerHTML = str;
}
*/

function setFormData(formName) {

  var mixedFormName = "mixedForm";
  var source =  document.forms[mixedFormName].elements["search_source"].value;
  var allowAltName = "false";
  if (document.forms[mixedFormName].elements["allow_alt_lang"] != null && document.forms[mixedFormName].elements["allow_alt_lang"].checked) {
     allowAltName = "true";
  }

  var showTidCidMap = "false";
  if (document.forms[mixedFormName].elements["show_tid_cid_map"] != null && document.forms[mixedFormName].elements["show_tid_cid_map"].checked) {
     showTidCidMap = "true";
  }

//alert("showcidMap: " + showTidCidMap + " showEqPalFlag: " + showEqPalFlag);
  var showPathFlag = document.forms[mixedFormName].elements["show_path_flag"].value;
  var showEqPalFlag = document.forms[mixedFormName].elements["show_eq_pal_flag"].value;

  document.forms[formName].elements["showPathFlag"].value = showPathFlag;
  document.forms[formName].elements["language"].value = source;
  document.forms[formName].elements["showTranslationFlag"].value = allowAltName;
  document.forms[formName].elements["showTidCidMapFlag"].value = showTidCidMap;
  document.forms[formName].elements["showEqPalFlag"].value = showEqPalFlag;

}

function validateCategory(searchBy, formName) {
  //alert("get here valicateCategory");

  var catValue = "";
  if (searchBy == "Term") {
   var headingField = Trim(document.forms[formName].elements["searchTerm"].value);
   if (headingField == "") {
     alert("Please input the category name");
     return false;
   }

   catValue = headingField;
   //document.forms[formName].elements["cidField"].value = "";
  }
  if (searchBy == "ID") {
   var cidField = Trim(document.forms[formName].elements["searchId"].value);
   if (cidField == "") {
  	alert("Please input the category ID");
   	return false;
   }

   catValue = cidField;

  }


 if (catValue == "*") {
     alert("Please refine your criteria for a proper search ");
     return false;
  }
/*
  var index = catValue.indexOf("*");
  if (index > 0 && index < 3 && document.forms[formName].elements("_allowWildcard").value == "false") {
     alert("Sorry, wildcard searches must begin with at least three letters.");
     return false;
  }
*/
  setFormData(formName);
//alert(" later showcidMap: " + document.forms[formName].elements["showTidCidMapFlag"].value + " showEqPalFlag: " + document.forms[formName].elements["showEqPalFlag"].value);
  return true;

}

function setOrder(sOrderBy) {
  var formName = "sortForm";
  if (sOrderBy != null) {
  	document.forms[formName].all["orderBy"].value = sOrderBy;
  }
  document.forms[formName].all["searchTerm"].value = document.forms["searchTermForm"].all["searchTerm"].value;
  document.forms[formName].all["searchId"].value = document.forms["searchIDForm"].all["searchId"].value;

//alert("get here in setOrder");
  setFormData(formName);


  document.forms[formName].submit();
}

function viewKeywords(taxId) {
  var formName = "sortForm";
//alert("get here 1");
  document.forms[formName].all["viewKeywordsForTax"].value = taxId;

  var windowprops = "toolbar=0,location=0,directories=0,status=0, menubar=0,scrollbars=yes,resizable=yes,top=50,left=250,width=480,height=550";

  var openWindow = window.open("viewKeywordsPopup.jsp", "keywords", windowprops);

}

function viewCategoryNpts(taxId) {
  var formName = "sortForm";
//alert("get here 1");
  document.forms[formName].all["viewCategoryNptsForTax"].value = taxId;

  var windowprops = "toolbar=0,location=0,directories=0,status=0, menubar=0,scrollbars=yes,resizable=yes,top=50,left=250,width=480,height=550";

  var openWindow = window.open("viewCategoryNptsPopup.jsp", "catNpts", windowprops);

}

function viewPaths(taxId, pathIndex) {
  var formName = "sortForm";
//alert("get here 1");
  document.forms[formName].all["viewPathsForTax"].value = taxId;
  document.forms[formName].all["selectedPathIndex"].value = pathIndex;

  var windowprops = "toolbar=0,location=0,directories=0,status=0, menubar=0,scrollbars=yes,resizable=yes,top=50,left=250,width=480,height=550";

  var openWindow = window.open("viewPathsPopup.jsp", "paths", windowprops);

}

function goToGeoReports(taxId) {
  var formName = "sortForm";
//alert("get here 1");
  document.forms[formName].all["viewKeywordsForTax"].value = taxId;

  var windowprops = "toolbar=1,location=1,directories=1,status=1, menubar=1,scrollbars=yes,resizable=yes,top=50,left=250";
//  var geoRepWin = window.open("http://reports.superpages.com/geo/index.jsp?categoryId=" + taxId, "GeoReports", windowprops);
  var geoRepWin = window.open("http://dss.is.idearc.com/pls/ictp/geo_redirect", "GeoReports", windowprops);

}

function popUpDateChanger() {
  var formName = "sortForm";
  var windowprops = "toolbar=0,location=0,directories=0,status=0, menubar=0,scrollbars=yes,resizable=yes,top=50,left=250,width=480,height=275";
  var dateChangePop = window.open("chageDatePop.jsp", "dateChanger", windowprops);

}

//invoke it to clear all existing data for previous tree action
function clearData(){
  var formName = "categoryItemForm";

  document.forms[formName].elements["_menuActionNodePath"].value = "";
  document.forms[formName].elements["_menuAction"].value = "";
  document.forms[formName].elements["_copiedNodePath"].value = "";

}
//change the browser type to load different branch of category business or the others.
function setBrowserType(){
  clearData();
  var formName = "categoryItemForm";
  //browserSourceType is not in the form categoryItemForm due to the layout reason
  var browType = document.all["browserSourceType"].value;

  document.forms[formName].all["_browserSource"].value = browType;
  document.forms[formName].elements["_categoryType"].value = true;//load new data
  document.forms[formName].submit();
}

function validateSeqNum(formName) {
   var seqNumObj = document.forms[formName].elements["seq_num"];
   if (seqNumObj == null) {
      alert("seq number element is not there. ");
      return false;
   }
   var seqNum = parseInt(Trim(seqNumObj.value));
   if (isNaN(seqNum)) {
     document.forms[formName].elements["seq_num"].value = "1";
   }
  //alert("seqNum: [" + seqNum + "]");
   return true;

}

//validate wheterh there is a category name is provided when creating or modifying the categrory
function validateCatName(formName) {
  var catNameObj = document.forms[formName].elements["category_name"];
  if (catNameObj == null) {
    alert("Category element is not there.");
    return false;
  }
  var catName = Trim(catNameObj.value);
  if (catName == "") {
   alert("Please input the category name. ");
   return false;
  }
  return true;

}

function submitForm(){
  var formName = "categoryItemForm";
  if (document.forms[formName] == null || document.forms[formName].elements["_menuAction"] == null)
    return;
  var val = document.forms[formName].elements["_menuAction"].value;
  if (val == "Modify") {
    if (validateCatName(formName) && validateSeqNum(formName)) {
        document.forms[formName].elements["_menuAction"].value = "ModifyCommit";
    }
    else {
       return;
    }

  }
  else if (val == "Add") {
    if (validateCatName(formName) && validateSeqNum(formName)) {
       document.forms[formName].elements["_menuAction"].value = "AddCommit";
       //document.forms[formName].method = "Get";
    }
    else {
       return;
    }
  }
  else if (val == "FindCategory") {
    document.forms[formName].elements["_menuAction"].value = "AttachCategory";
  }

  document.forms[formName].submit();
}

//set the isRoot hidden field to indicate the action node is root or not
function setIsRoot(sNodeValue) {
   var formName = "categoryItemForm";
   var isRootObj = document.forms[formName].elements["_isActionNodeRoot"];
   if (isRootObj == null) return;
   if (sNodeValue == ROOT_YP || sNodeValue == ROOT_YP_SP || sNodeValue == ROOT_BUSINESS) {
       isRootObj.value = "true";
   }
   else {
       isRootObj.value = "false";
   }
}

//set menu add action handler
function _setMenuHandler(sFormName, sAction, sActionNodePath, sNodeValue) {
    var formName = "categoryItemForm";
    document.forms[formName].elements["_menuAction"].value  = sAction;
    document.forms[formName].elements["_menuActionNodePath"].value  = sActionNodePath;

    setIsRoot(sNodeValue);

    var catVal = new String(); catVal = sNodeValue;
    catVal = escape(sNodeValue);
    //This url is not filled with the C parameters
    document.forms[formName].elements["_actionNodeName"].value = sNodeValue;

    var sElem = "categoryItemDiv";
    if (sAction == "Copy"){
       document.forms[formName].elements["_copiedNodePath"].value = sActionNodePath;
    }
    else if (sAction == "View" || sAction == "Add" || sAction == "Delete"
            || sAction == "Paste" || sAction == "Modify"
            || sAction == "FindCategory"){
       if (sAction == "Delete" && !confirm("Are you sure you want to delete this category and its children?")) {
           return;
       }
        document.forms[formName].submit();
    }

}


function setCagtegoryBrowserMenu(e,sFormName,sNodePath,allowAdd, sNodeValue, menuItems) {
   linkset[0] = getMenuItemString(sFormName,sNodePath,allowAdd, sNodeValue, menuItems);
   showmenu(e);
}

function getMenuItemString(sFormName,sNodePath,nodeType, sNodeValue, menuItems){
//alert("nodetype in dispath: " + nodeType);
var formName = "categoryItemForm";
menuItems = document.forms[formName].elements["_menuItems"].value;
   if (nodeType == "GroupCategory"){
     return getGroupCategoryMenuItem(sFormName,sNodePath, sNodeValue, menuItems);
   }
   else if (nodeType == "SearchCategory"){
    return getSearchCategoryMenuItem(sFormName,sNodePath, sNodeValue, menuItems)
   }
   else if (nodeType == "MatchCategory"){
    return getMatchCategoryMenuItem(sFormName,sNodePath, sNodeValue)
   }
   else if (nodeType == "SeeAlsoCategory"){
     return getSeeAlsoMenuItem(sFormName,sNodePath, sNodeValue, menuItems)
   }
   else if (nodeType == "RootNode"){
     return getRootMenuItem(sFormName,sNodePath, sNodeValue);
   }
   else if (nodeType == "Augment"){
     return getAugmentMenuItem(sFormName,sNodePath, sNodeValue)
   }
   else if (nodeType == "CategoryHeading"){
     return getHeadingMenuItem(sFormName,sNodePath, sNodeValue)
   }

}

function getRootMenuItem(sFormName,sNodePath, sNodeValue){
  var menuStr = "";
  menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
    + sFormName + '\',\'Add\', \''+sNodePath +'\',\'' + sNodeValue + '\');">Add Child</a></div>';
  menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
    + sFormName + '\',\'Import\', \''+sNodePath +'\',\'' + sNodeValue + '\');">Import File</a></div>';
  menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
    + sFormName + '\',\'Export\', \''+sNodePath +'\',\'' + sNodeValue + '\');">Export To File</a></div>';
  return menuStr;
}

function getAugmentMenuItem(sFormName,sNodePath, sNodeValue){
  var menuStr = "";
  menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
    + sFormName + '\',\'AddModify\', \''+sNodePath +'\',\'' + sNodeValue + '\');">Add/Modify/Delete Member(s)</a></div>';
  menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
    + sFormName + '\',\'Delete\', \''+sNodePath +'\',\'' + sNodeValue + '\');">Delete Augment</a></div>';
  return menuStr;
}

function getHeadingMenuItem(sFormName,sNodePath, sNodeValue){
  var menuStr = "";
  menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
    + sFormName + '\',\'Delete\', \''+sNodePath +'\',\'' + sNodeValue + '\');">Delete</a></div>';

  return menuStr;
}

function getSearchCategoryMenuItem(sFormName,sNodePath, sNodeValue, allowedItems){
var menuStr = "";

var indexView = allowedItems.indexOf("|" + MENU_ACTION_VIEW + "|");
var indexModify = allowedItems.indexOf("|" + MENU_ACTION_MODIFY + "|");
var indexDelete = allowedItems.indexOf("|" + MENU_ACTION_DELETE + "|");

//alert("alloweditem: " + allowedItems + " indexAdd: " + indexAdd + " indexModify:" + indexModify + " indexDelete: " + indexDelete);
if (indexView >= 0) {
  menuStr +='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
          + sFormName + '\',\'View\', \''+sNodePath +'\',\'' + sNodeValue
          + '\');">View Category</a></div>';
}
var indexFind   = allowedItems.indexOf("|" + MENU_ACTION_FIND_CAT + "|");

if (indexDelete >= 0 ) {
   menuStr += '<div class="menuitems"><a href="javascript:_setMenuHandler(\''
           + sFormName+'\',\'Delete\', \''+sNodePath +'\',\'' + sNodeValue
           + '\');">Delete</a></div>'
}

if (indexModify >= 0) {
   menuStr += '<div class="menuitems"><a href="javascript:_setMenuHandler(\''
           + sFormName+'\',\'Modify\', \''+sNodePath +'\',\'' + sNodeValue
           + '\');">Modify</a></div>'
}
/*
menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
        +sFormName+'\',\'Copy\', \''+sNodePath +'\',\'' + sNodeValue+'\');">Copy</a></div>'
*/

if (indexFind >= 0) {
   menuStr += '<div class="menuitems"><a href="javascript:_setMenuHandler(\''
           + sFormName + '\',\'FindCategory\', \''+sNodePath +'\',\'' + sNodeValue + '\');">Find Category</a></div>';
}
return menuStr;
}

function getGroupCategoryMenuItem(sFormName,sNodePath, sNodeValue, allowedItems){
var menuStr = "";
var indexView = allowedItems.indexOf("|" + MENU_ACTION_VIEW + "|");
var indexAdd = allowedItems.indexOf("|" + MENU_ACTION_ADD + "|");
var indexModify = allowedItems.indexOf("|" + MENU_ACTION_MODIFY + "|");
var indexDelete = allowedItems.indexOf("|" + MENU_ACTION_DELETE + "|");
//alert("alloweditem: " + allowedItems + " indexView: " + indexView +  " indexAdd: " + indexAdd + " indexModify:" + indexModify + " indexDelete: " + indexDelete);
//alert("indexView: " + indexView);
if (indexView >= 0) {
  menuStr +='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
          + sFormName + '\',\'View\', \''+sNodePath +'\',\'' + sNodeValue
          + '\');">View Category</a></div>';
}
if (indexAdd >= 0) {
   menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
          + sFormName + '\',\'Add\', \''+sNodePath +'\',\'' + sNodeValue
          + '\');">Add Child</a></div>';
}
if (sNodeValue != ROOT_YP && sNodeValue != ROOT_BUSINESS
        && sNodeValue != ROOT_YP_SP){
    if (indexDelete >= 0 ) {
        menuStr +='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
                + sFormName+'\',\'Delete\', \''+sNodePath +'\',\'' + sNodeValue
                +'\');">Delete</a></div>'
    }

    if (indexModify >= 0) {
       menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
              +sFormName+'\',\'Modify\', \''+sNodePath +'\',\'' + sNodeValue
              +'\');">Modify</a></div>'
    }
}
/*
menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
        +sFormName+'\',\'Copy\', \''+sNodePath +'\',\'' + sNodeValue+'\');">Copy</a></div>'

menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
       +sFormName+'\',\'Paste\', \''+sNodePath +'\',\'' + sNodeValue+'\');">Paste</a></div>'
*/

return menuStr;
}

function getMatchCategoryMenuItem(sFormName,sNodePath, sNodeValue){
var menuStr = "";

   menuStr += '<div class="menuitems"><a href="javascript:_setMenuHandler(\''
           + sFormName + '\',\'Add\', \''+sNodePath +'\',\'' + sNodeValue + '\');">Add Child</a></div>';

   menuStr += '<div class="menuitems"><a href="javascript:_setMenuHandler(\''
           + sFormName+'\',\'Delete\', \''+sNodePath +'\',\'' + sNodeValue +'\');">Delete</a></div>'
/*
menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
        +sFormName+'\',\'Modify\', \''+sNodePath +'\',\'' + sNodeValue+'\');">Modify</a></div>'
menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
        +sFormName+'\',\'Copy\', \''+sNodePath +'\',\'' + sNodeValue+'\');">Copy</a></div>'

menuStr+='<div class="menuitems"><a href="javascript:_setMenuHandler(\''
        +sFormName+'\',\'Paste\', \''+sNodePath +'\',\'' + sNodeValue+'\');">Paste</a></div>'
*/
   menuStr += '<div class="menuitems"><a href="javascript:_setMenuHandler(\''
           + sFormName + '\',\'ViewSynonym\', \''+sNodePath +'\',\'' + sNodeValue + '\');">View Synonyms</a></div>'
   menuStr += '<div class="menuitems"><a href="javascript:_setMenuHandler(\''
           + sFormName + '\',\'ViewAugment\', \''+sNodePath +'\',\'' + sNodeValue + '\');">View Augmentation</a></div>'
//alert("match menu: " + menuStr);
   return menuStr;
}

function getSeeAlsoMenuItem(sFormName,sNodePath, sNodeValue, allowedItems){
var menuStr = "";

var indexDelete = allowedItems.indexOf("|" + MENU_ACTION_DELETE + "|");
if (indexDelete >= 0 ) {
   menuStr += '<div class="menuitems"><a href="javascript:_setMenuHandler(\''
           + sFormName+'\',\'Delete\', \''+sNodePath +'\',\'' + sNodeValue
           + '\');">Delete</a></div>'
}

return menuStr;
}

function showHideCate(formName,elm){

   var flag = document.forms[formName].elements["allow_search"].checked;

   showHide(formName,elm,flag);
}

function showHide(formName,elm,flag){
   if (formName == null || elm == null) return;
//alert("elem:  " +document.forms[formName].all[elm] + " ELM: " + elm);
   if (flag == true){
      document.forms[formName].all[elm].style.display="none";
   }
   else {
      document.forms[formName].all[elm].style.display="";
   }
}

//submit form to add /change augment or augment member
function changeAugmentMember(){
  var formName = "augmentForm";

  //validate the augment phrase, it has to have a value;
  var augPhrase = "";
  if (document.forms[formName].elements["augmentPhrase"] != null){
    augPhrase = document.forms[formName].elements["augmentPhrase"].value;
    augPhrase = augPhrase.trim();
    if (augPhrase == "") {
      alert("Please input augment phrase."); return;
    }
  }

  document.forms[formName].elements["_menuAction"].value = "Add";
}



