/****************************************************************

  Traction Software, Inc. Confidential and Proprietary Information

  Copyright (c) 1996-2010 Traction Software, Inc.
  All rights reserved.

****************************************************************/

// PLEASE DO NOT DELETE THIS LINE -- make copyright depends on it.
function fixWindowName( name ) {

  var ALLOWED_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
  var REPLACEMENT_CHAR = "_";
  var newname = "";

  for (var i = 0; i < name.length; i ++) {
    if (ALLOWED_CHARS.indexOf(name.charAt(i)) == -1) {
      newname = newname + REPLACEMENT_CHAR;
    } else {
      newname = newname + name.charAt(i);
    }
  }
  return newname;

}

function showdetails2( filepath ) {
  var name = fixWindowName(filepath);
}

function showdetails( filepath, name ) {
  var url = FORM_ACTION_READ_ONLY + "?type=filedetails&path=" + filepath;
  name = fixWindowName(name);
  var w = 400;
  try {
    if (userAgentName == "iewin") {
      w = parseInt(ua("filedetails_window_width", "400")) + parseInt(i18n("filedetails_window_width_extra_iewin", "0"));
    } else {
      w = parseInt(ua("filedetails_window_width", "400"));
    }
  } catch (xcp) { }
  var features = ua("filedetails_window_features") + getSizeAndPosition(new String(w), ua("filedetails_window_height"));

  // if there's already a window open with this name, this gives us a reference and
  // doesn't change the location being displayed yet
  var detailswin = window.open( "", name, features );

  // if the location is blank, then it was just opened by us and we need to set the url

  // In Safari, detailswin.location.href is set to "/" in this case. [cjn 20.Jul.2004]
  // Ah, now in Safari, the popup starts with the same location as the originating window.  Added an additiona condition to support this. [cjn 24.Nov.2004]
  if (detailswin.location.href == "/" || detailswin.location.href == "about:blank" || detailswin.location.href == "" || detailswin.location.href == window.location.href ) {
    window.open( url, name, features );
  }
  // otherwise we just need to give the existing window focus
  else {
    detailswin.focus();
  }
  //  return false;
}

function create_folder() {
  if (trim(document.mkdir.name.value).length == "") {
    alert(i18n("cfi_directory_name_not_specified_message", "Please specify a directory name to create."));
    return false;
  } else {
    return true;
  } 
}

function upload_files() {
  return true;
}

var EXPAND_MAX = 4;
var expand = 0;

function upload_more_rows() {
  try {
    expand++;
    var cur = document.getElementById("put_expand"+expand);
    if (cur) {
      cur.style.display = "block";
    }
    if (expand == EXPAND_MAX) {
      hide_more_rows();
    }

  } catch (e) {
    alert(i18n("cfi_more_rows_not_supported_message", "Sorry, but this is not supported by your browser."));
    hide_more_rows();
  }
}

function hide_more_rows() {
  try {
    var showmore = document.getElementById("put_showmorelink");
    if (showmore) {
      showmore.style.display = "none";
    }
  } catch (e) {}
}

function clear_caches() {
  return true;
}

function showClientSecuritySettingsHelp() {
  window.open( i18n_helplinks("help_windows_client_security_settings", "http://v4.help.tractionsoftware.com/traction/page/Doc/Windows%20Client%20Security%20Settings"), "help");
}


// Opening MS Office documents directly via WebDAV, from IE

function openWordDocument(filepath) {
  var Word;
  try {
    Word = new ActiveXObject("Word.Application");
    if (Word != null) {
      Word.Visible = true;
      Word.Documents.Open(filepath);
    } else {

      if ( confirm(i18n("cfi_direct_open_failure_message_msword", "Could not open Microsoft Word.  Microsoft Word may not be installed on your system, or you may need to change your Internet Explorer security settings to allow execution of ActiveX controls.") )) {
	showClientSecuritySettingsHelp();
      }
    }
  }
  catch (e) {
    if ( confirm(i18n("cfi_direct_open_failure_message_msword", "Could not open Microsoft Word.  Microsoft Word may not be installed on your system, or you may need to change your Internet Explorer security settings to allow execution of ActiveX controls.")) ) {
      showClientSecuritySettingsHelp();
    }
  }
}

function openExcelDocument(filepath) {
  var Excel;
  try {
    Excel = new ActiveXObject("Excel.Application");
    if (Excel != null) {
      Excel.Visible = true;
      Excel.Workbooks.Open(filepath);
    } else {
      if ( confirm(i18n("cfi_direct_open_failure_message_msexcel", "Could not open Microsoft Excel.  Microsoft Excel may not be installed on your system, or you may need to change your Internet Explorer security settings to allow execution of ActiveX controls.") )) {
	showClientSecuritySettingsHelp();
      }
    }
  }
  catch (e) {
    if ( confirm(i18n("cfi_direct_open_failure_message_msexcel", "Could not open Microsoft Excel.  Microsoft Excel may not be installed on your system, or you may need to change your Internet Explorer security settings to allow execution of ActiveX controls.") )) {
      showClientSecuritySettingsHelp();
    }
  }
}

function openPowerpointDocument(filepath) {
  var Powerpoint;
  try {
    Powerpoint = new ActiveXObject("PowerPoint.Application");
    if (Powerpoint != null) {
      Powerpoint.Visible = true;
      Powerpoint.Presentations.Open(filepath);
    } else {
      if ( confirm (i18n("cfi_direct_open_failure_message_mspowerpoint", "Could not open Microsoft PowerPoint.  Microsoft PowerPoint may not be installed on your system, or you may need to change your Internet Explorer security settings to allow execution of ActiveX controls."))) {
	showClientSecuritySettingsHelp();
      }
    }
  }
  catch (e) {
    if ( confirm (i18n("cfi_direct_open_failure_message_mspowerpoint", "Could not open Microsoft PowerPoint.  Microsoft PowerPoint may not be installed on your system, or you may need to change your Internet Explorer security settings to allow execution of ActiveX controls."))) {
      showClientSecuritySettingsHelp();
    }
  }
}

function onShowSize(standardButton) {
  standardButton.disabled = true;
  var url = FORM_ACTION_READ_ONLY + "?type=directorysize_&path=" + encode_url_parameter(filepath);
  var args = {
    "insertResult": standardButton.parentNode,
    "standardButton": standardButton
  };
  xmlget_async(url, standardButton.nextSibling, true, onShowSize_wakeup, args);
}

function onShowSize_wakeup(content, args) {

  var insertResult   = args["insertResult"];
  var standardButton = args["standardButton"];

  if (content == null) {
    alert(i18n("xmlrequest_unauthorized_alert_message", "You are not authorized to perform the requested action with your current credentials.  Please open another browser window, sign in, and then try the action again."));
    if (standardButton) {
      standardButton.disabled = false;
    }
    return; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  }

  if ( content == "" ) {
    alert(i18n("xmlrequest_failed_alert_message", "The request failed because the server could not be reached.  Please ensure that your network connection has not been interrupted and that the server is still online."));
    if (standardButton) {
      standardButton.disabled = false;
    }
    return; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  }

  if (insertResult) {
    insertResult.innerHTML = content;
  } else {
    alert(content);
  }

}
