function replaceSubstringByPattern(p, r) {
  if (p == null || p == "") return;
  if (r == null) r = "";
  var loc = refreshUrl;
  var ind1 = (loc.indexOf("?" + p) != -1 ) ? loc.indexOf("?" + p) : loc.indexOf("&" + p);
  if (ind1 < 0) return;
  ind1++;
  var ind2 = loc.indexOf("&", ind1);
  if (ind2 < 0) ind2 = loc.length;
  refreshUrl = loc.substring(0, ((r == "")?(ind1 - 1):ind1)) + r + loc.substring(ind2, loc.length);
}

if (refreshUrl.indexOf("cmd=create") != -1) {
  replaceSubstringByPattern("cmd=create", "cmd=refresh");
}

function refreshWin(p, r) {
  var pattern = (p == null)?(typeof(templateViewMode) != 'undefined' && templateViewMode == 'view') ? "cmd=prepare":"cmd=update":p;
  var replace = (r == null) ?"cmd=refresh":r;
  replaceSubstringByPattern(pattern, replace);
  if (refreshUrl.length < 2080) {
    window.location.href = refreshUrl;
    return;
  }

  var form = makeTempForm();
  if (!form) {
    window.location.reload(true);
    return;
  }
  else {
    var params = refreshUrl.substring(refreshUrl.indexOf("?"), refreshUrl.length);
    var pArr = params.split("&");
    //	alert(pArr.length);
    for (var i = 0; i < pArr.length; i++) {
      var pair = pArr[i].split("=");
      if (typeof(pair[1]) == "undefined" || pair[1] == null) pair[1] = "";
      makeInput(form, "hidden", pair[0], pair[1]);
    }
    if (pArr.length > 0) form.submit();
  }
}

function makeTempForm() {
  var formName = "tempForm";
  var body = document.getElementsByTagName("body").item(0);
  if (!body) return false;
  var newTag = document.createElement("FORM");
  newTag.method = "POST";
  newTag.name = formName;
  newTag.className = "eattrFrame";
  body.appendChild(newTag);
  return newTag;
}

