
function showChooseArticles(select_field,iframe,token,id_special) {
  wChooseArticles = window.open("/index.php/articles/choose_articles?select_field=" + select_field  + "&token=" +
                  token + "&iframe=" + iframe + "&id_special=" + id_special,
                  "wChooseArticles",C_CHOOSE_ARTICLES_WINDOW_OPTIONS);
}

function showChooseArticle(func_name,id_special, lang_id) {
  wChooseArticles = window.open("/index.php/articles/choose_article?" + 
                    "func_name=" + func_name + 
                    ((id_special !=null) ? "&id_special=" + id_special : "") +
                    ((lang_id !=null) ? "&lang_id=" + lang_id : ""),
                    "wChooseArticles",
                    C_CHOOSE_ARTICLES_WINDOW_OPTIONS);
}

function showArticle(id) {
  if (id) {
    wArticle = window.open("/index.php/articles/view_article/" + id + "/fullscreen",
                          "wArticle",C_ARTICLE_WINDOW_OPTIONS);
  }
}

function getArticlesXML(filter,infos, read_func, lang_id) {
  var dic=new RecursiveDictionnary({"filter":filter})
  if (infos != null) {
    dic.add("infos", infos)
  }
  if (lang_id != null) {
    dic.add("lang_id", lang_id)
  }
  dic.post(
    "/index.php/articles/get_articles_xml",
    read_func)
}

function showChooseCalendars(aArguments) {
  wChooseProducts = window.open(
    "/index.php/config/choose_calendars?func_name=" + aArguments,
    "wChooseCalendars",C_CHOOSE_CALENDARS_WINDOW_OPTIONS);
}
function showCreateCalendar(aFuncName) {
  wCreateProducts = iframePopup(
      "test",
      "/index.php/config/new_calendar?fullscreen=1&func_name=" + aFuncName,
      800,800);  
}
;var wChooseDate
var wShowEvent
var wNewTask
var wNewEvent
var wShowTask

function showDatePicker_Func(sJsFunction, sField) {
  wChooseDate = window.open("/index.php/calendar/date_picker?cal_parent_function=" + sJsFunction + getDPDate(sField),
                  "wChooseDate",C_DATEPICKER_WINDOW_OPTIONS);
}

function showDatePicker_Field(sField) {
  wChooseDate = window.open("/index.php/calendar/date_picker?cal_parent_field=" + escape(sField) + getDPDate(sField),
                "wChooseDate",C_DATEPICKER_WINDOW_OPTIONS);
}
function getDPDate(sField) {
  if ((typeof sField) == "string") {
    sField = _(sField)
  }
  ret = ""
  if(sField.value && (sField.value != T_DATE_FORMAT)) {
    ret = "&cal_date=" + escape(sField.value + " 00:00:00")
  } 
  return ret
}
function newTask(sParameters) {
  iframePopup(T_NEW_TASK,"/index.php/calendar/new_task?ipopup=1&fullscreen=1&" + sParameters,
             800,800);
}
function newEvent(sParameters) {
  iframePopup(T_NEW_EVENT,"/index.php/calendar/new_event?ipopup=1&fullscreen=1&" + sParameters,
             800,800);
  
}
function showEvent(event_id) {
  wShowEvent = window.open("/index.php/calendar/view_event?id=" + event_id,
                          "wShowEvent",C_EVENT_WINDOW_OPTIONS);
  
}
function showTask(event_id) {
  wShowTask = window.open("/index.php/calendar/view_task?id=" + event_id,
                          "wShowTask",C_EVENT_WINDOW_OPTIONS);
  
}

function showPasswordReset() {

  wPasswordReset = window.open("/index.php/admin/reset_token","wPasswordReset",
                               C_PWRESET_WINDOW_OPTIONS);

}

var wProduct
var wProductAudio;

function getProductParams(id_field, name_field) {
  params = "";
  if (id_field) {
    params += "&id_field=" + id_field ;
  }
  if (name_field) {
    params += "&name_field=" + name_field ;
  }
  return params
}
/**
* deprecated
**/
function showChooseProducts(select_field,iframe,token) {
  wChooseProducts = window.open("/index.php/catalogue/chooseProducts?select_field=" + select_field  + "&token=" +
                  token + "&iframe=" + iframe,
                  "wChooseProducts",C_CHOOSE_PRODUCTS_WINDOW_OPTIONS);
}
function showChooseProducts2(func_name, lang_id) {
  wChooseProducts = window.open(
    "/index.php/catalogue/chooseProducts?func_name=" + func_name +
    ((lang_id != null) ? "&lang_id=" + lang_id : ""),
    "wChooseProducts",C_CHOOSE_PRODUCTS_WINDOW_OPTIONS);
}
function showChooseProduct(func_name,quantity, lang_id) {
  wChooseProducts = window.open(
    "/index.php/catalogue/chooseProduct?func_name=" + func_name +
    ((quantity != null) ? "&quantity=" + quantity : "") + 
    ((lang_id != null) ? "&lang_id=" + lang_id : ""),
    "wChooseProducts",C_CHOOSE_PRODUCTS_WINDOW_OPTIONS);
}

function showProduct(id) {
  if (id) {
    wProduct = window.open("/index.php/catalogue/product?id=" + id + "&fullscreen=1",
                          "wProduct",C_PRODUCT_WINDOW_OPTIONS);
  }
}
function showProductPictures(id) {
  if (id) {
    wProduct = window.open("/index.php/catalogue/product_pictures?id=" + id + "&fullscreen=1",
                          "wProduct",C_PRODUCT_WINDOW_OPTIONS);
  }
}
function showProductAudio(id) {
  if (id) {
    wProductAudio = window.open("/index.php/catalogue/audio?id=" + id + "&fullscreen=1",
                          "wProductAudio",C_PRODUCT_AUDIO_WINDOW_OPTIONS);
  }
}

function showAddProduct(func_name) {
  wProductAdd = window.open("/index.php/products_admin/quickadd?" +
                            "func_name=" + func_name,
                            "wProductAdd",C_ADDPRODUCT_WINDOW_OPTIONS);
}
function _getTaxes(xdoc) {
  var taxes = new Array();
  var xtaxes = xdoc.getElementsByTagName("tax")
  for (var i=0, j=xtaxes.length; i < j ; i++) {
    taxes[i]=parseFloat(xtaxes[i].getAttribute("value"));
  }
  return taxes;
}
function getProductDetails(product,user, infos, read_func, error_func, lang_id, company, bSingle) {
  var dic = new RecursiveDictionnary({"product":product,"user":user, "infos":infos})
  if (company != null) {
    dic.add("company", company)
  }
  if (lang_id != null) {
    dic.add("lang_id",lang_id)
  }
  if (bSingle == null) {
    bSingle = true
  }
  dic.post(
    bSingle ? "/index.php/catalogue/get_product_details"
            : "/index.php/catalogue/get_products_details",
    read_func, null, error_func)
}
function getProductsDetails(product,user, infos, read_func, error_func, lang_id, company) {
  return getProductDetails(product,user, infos, read_func, error_func, lang_id, company, false)
}

function getProductPrice(product,user,read_func) {
  getProductDetails(product, user, PRODUCT_INFO_DIFFUSION, function(product){
    var prix_ht=parseFloat(product.getValue("prix_ht"));
    if (!isNaN(prix_ht)) {
      read_func(product.getValue("taxes").values(),prix_ht)
    }
  })
}

function getUplParams(select_field, id_parent, token) {
  params = "";
  if (select_field) {
    params += "&select_field=" + select_field ;
  }
  if (id_parent) {
    params += "&id_parent=" + id_parent
  }
  if (token) {
    params += "&token=" + token
  }
  return params
}

function showUploader(upl_type, select_field, token_field, id_parent_field) {
  id_parent_field = _(id_parent_field)
  if (id_parent_field) {
    id_parent = id_parent_field.value
  } else {
    id_parent = 0
  }
  token_field = _(token_field)

  if (token_field) {
    token = token_field.value
  } else {
    token = ""
  }

  wUpload = window.open("/index.php/files/upload_" + upl_type + '?' + getUplParams(select_field, id_parent, token),"wUpload",C_UPLOADER_WINDOW_OPTIONS);
}
function showMailUploader(select_field, token_field, id_parent_field) {
  showUploader("email", select_field, token_field, id_parent_field)
}
function addNewUpload(select_field, id, title) {
  select_field =   _(select_field)
  select_field.options[select_field.options.length] = new Option(title, id,false,true)
}
function removeUploads(select_field, ids) {
  select_field =   _(select_field)
  for (i= select_field.options.length-1 ; i >= 0 ; i--) {
    for(j=0; j < ids.length ; j++) {
    if (ids[j] == select_field.options[i].value) {
        select_field.options[i] = null
    }
    }
  }
}
function deleteUpload(upl_type, select_field, token_field, id_parent_field, iframe) { 
  id_parent_field = _(id_parent_field)
  if (id_parent_field) {
    id_parent = id_parent_field.value
  } else {
    id_parent = 0
  }
  token_field = _(token_field)
  if (token_field) {
    token = token_field.value
  } else {
    token = ""
  } 
  ids = ""
  _select_field =   _(select_field)
  for (i=0;i<_select_field.options.length;i++) {
    op = _select_field.options[i]
    if (op.selected) {
      ids = ids + "&id[]=" + op.value
    }
  }

  frames[iframe].location = "/index.php/files/delete_" + upl_type + '?' + getUplParams(select_field, id_parent, token) + ids
}
function deleteMailUploads(select_field, token_field, id_parent_field, iframe) {
  deleteUpload("email", select_field, token_field, id_parent_field, iframe)
}

function getMailUpload(select_field) {
  document.location = "/index.php/files/download_email/attach?id=" + _(select_field).value
}
function showProductUploader(id_parent,thumbnail,id_parent_image,func_name,display_image) {
  if(!thumbnail) thumbnail=0
  if(!id_parent_image) id_parent_image=0
  if(!func_name) func_name=""
  if (display_image== null) display_image=true
  wUpload = window.open("/index.php/files/upload_product?id_parent=" + id_parent +
                        "&thumbnail=" + thumbnail + "&id_parent_image="  + id_parent_image +
                        "&func_name=" + func_name + "&display_image=" + (display_image ? '1' : '0'),
                        "wUpload", C_UPLOADER_WINDOW_OPTIONS);
}
function showUserUploader(id_parent,thumbnail,id_parent_image,func_name,display_image) {
  if(!thumbnail) thumbnail=0
  if(!id_parent_image) id_parent_image=0
  if(!func_name) func_name=""
  if (display_image== null) display_image=true
  wUpload = window.open("/index.php/files/upload_user?id_parent=" + id_parent +
                        "&thumbnail=" + thumbnail + "&id_parent_image="  + id_parent_image +
                        "&func_name=" + func_name + "&display_image=" + (display_image ? '1' : '0'),
                        "wUpload", C_UPLOADER_WINDOW_OPTIONS);
}
function showArticleUploader(id_parent,thumbnail,id_parent_image) {
  if(!thumbnail) thumbnail=0
  if(!id_parent_image) id_parent_image=0
  wUpload = window.open("/index.php/files/upload_article?id_parent=" + id_parent +
                        "&thumbnail=" + thumbnail + "&id_parent_image="  + id_parent_image,
                        "wUpload", C_UPLOADER_WINDOW_OPTIONS);
}
function showCategoryUploader(id_parent,thumbnail,id_parent_image) {
  if(!thumbnail) thumbnail=0
  if(!id_parent_image) id_parent_image=0
  wUpload = window.open("/index.php/files/upload_category?id_parent=" + id_parent +
                        "&thumbnail=" + thumbnail + "&id_parent_image="  + id_parent_image,
                        "wUpload", C_UPLOADER_WINDOW_OPTIONS);
}
function showEditFileProduct(id,thumbnail) {
  if(!thumbnail) thumbnail=0
  wUpload = window.open("/index.php/files/edit_product?id=" + id + "&thumbnail=" + thumbnail,
                        "wUpload",C_UPLOADER_WINDOW_OPTIONS);  
}
function showEditFileUser(id,thumbnail) {
  if(!thumbnail) thumbnail=0
  wUpload = window.open("/index.php/files/edit_user?id=" + id + "&thumbnail=" + thumbnail,
                        "wUpload",C_UPLOADER_WINDOW_OPTIONS);  
}
function showEditFileArticle(id,thumbnail) {
  if(!thumbnail) thumbnail=0
  wUpload = window.open("/index.php/files/edit_article?id=" + id + "&thumbnail=" + thumbnail,
                        "wUpload",C_UPLOADER_WINDOW_OPTIONS);  
}
function showEditFileCategory(id,thumbnail) {
  if(!thumbnail) thumbnail=0
  wUpload = window.open("/index.php/files/edit_category?id=" + id + "&thumbnail=" + thumbnail,
                        "wUpload",C_UPLOADER_WINDOW_OPTIONS); 
}

var sFuncName
var sSearchString

function showChooseUser(func_name, search_string) {
  var sArgs = ""
  if(search_string)  {
    sArgs = sArgs + "&initialized=1&srch=" + escape(search_string)
  }
  wChooseUser = window.open("/index.php/users/chooseUser?func_name=" + func_name + sArgs
                            ,"wChooseUser",C_USER_WINDOW_OPTIONS);
}
function showAddRecipient(recipient_field) {
  wChooseRecipient = window.open("/index.php/users/addRecipient?recipient_field=" + recipient_field ,"wChooseRecipient",C_CHOOSE_USER_WINDOW_OPTIONS);
}
function showAddRecipient_JS(recipient_jsfunc) {
  wChooseRecipient = window.open("/index.php/users/addRecipient?recipient_jsfunc=" + recipient_jsfunc ,"wChooseRecipient",C_CHOOSE_USER_WINDOW_OPTIONS);
}
function showUser(id) {
  wUser = window.open("/index.php/users/view_user/fullscreen?check_type=1&id=" + id,"wUser",C_USER_WINDOW_OPTIONS);
}

function showAddUser(func_name, id_special_insert) {
  id_special_insert = (id_special_insert) ? "&id_special_insert=" + id_special_insert : ""
  wUser = iframePopup(
    "title", "/index.php/users/quickadd?func_name=" + func_name + id_special_insert,
    800,800);
}
function showAddSupplier(func_name) {
  showAddUser(func_name, SPECIAL_GROUP_PRODUCTSUPPLIERS)
}
function showAddBrand(func_name) {
  showAddUser(func_name, SPECIAL_GROUP_BRANDS)
}
function searchUser(func_name, search_string) {
  if(search_string) {
    sFuncName = func_name
    sSearchString = search_string
    makeRequest("/index.php/users/quick_search?srch=" + escape(search_string),
                readSearchResults)
  } else {
    showChooseUser(func_name, search_string)
  }
}
function readSearchResults(xResponse) {
  var aResults = xResponse.documentElement.getElementsByTagName("user")
  if (aResults.length == 1) {
    var fFunc = eval(sFuncName)
    fFunc(aResults[0].getAttribute("id"),aResults[0].getAttribute("title"))
  } else {
    showChooseUser(sFuncName, sSearchString)
  }
}

