function calcMaxProportions(width, height, maxWidth, maxHeight) {
  if(width > maxWidth || height > maxHeight) {
    if(width / height > maxWidth / maxHeight) {
      k = maxWidth / width;
      width = width * k;
      height = height * k;
    } else {
      k = maxHeight / height;
      width = width * k;
      height = height * k;
    }
  }
  var res = new Array();
  res[0] = width;
  res[1] = height;
  return res;
}

function openMaximized(imageId, width, height) {
  openMaximizedURL("/exec/gallery/maximized/" + imageId, width, height);
}

function openMaximizedURL(url, width, height) {
  var wh = calcMaxProportions(width, height, screen.width, screen.height);
  width = wh[0];
  height = wh[1];
  var winl = (screen.width - width) / 2;
  var wint = (screen.height - height) / 2;
  winprops = 'height='+height+',width='+width+',top='+wint+',left='+winl+',scrollbars=0,resizable=0';
  win = window.open(url, '', winprops);
  if (parseInt(navigator.appVersion) >= 4) {
    win.window.focus();
  }
}

function openStatus(form, actionUrl, statusUrl, addTime) {
  var randomCode = "";

  if(addTime) {
    var now = new Date();
    randomCode = now.getFullYear() + '' + now.getMonth() + '' + now.getDate();
    randomCode = randomCode + now.getHours() + '' + now.getMinutes() + '' + now.getSeconds() + '' + now.getMilliseconds();
  }
  var width = 350;
  var height = 160;
  var winl = (screen.width - width) / 2;
  var wint = (screen.height - height) / 2;
  winprops = 'height='+height+',width='+width+',top='+wint+',left='+winl+',scrollbars=0,resizable=0';
  win = window.open(statusUrl + randomCode, 'uploadstatus', winprops);
  if (parseInt(navigator.appVersion) >= 4) {
    win.window.focus();
  }
  form.action = actionUrl + randomCode;
}

function NVL(value, defaultValue) {
  if(typeof(value) == "undefined" || value == null) {
    return defaultValue;
  }
  return value;
}

function onBodyLoad() {
  initFormHandlers();
  if(typeof(HTMLArea) != "undefined" && typeof(_html_area_id) != "undefined") {
    if(_html_area_id == '') {
      HTMLArea.replaceAll();
    } else {
      HTMLArea.replace(_html_area_id);
    }
  }
}

function onWithdrawTypeChanged(withdrawTypeSelect, rowName) {
  var rows = document.getElementsByName(rowName);
  var withdrawType = withdrawTypeSelect.options[withdrawTypeSelect.selectedIndex].value;
  var i;
  for(i=0; i<rows.length; i++) {
    var withdraw = rows[i].getAttribute('withdraw');
    if(withdraw != null) {
      if(withdraw == withdrawType) {
        rows[i].style.display = '';
      } else {
        rows[i].style.display = 'none';
      }
    }
  }
}

function showNextUploadFile(rowId, nextButtonId) {
  var row = document.getElementById(rowId);
  var nextButton = document.getElementById(nextButtonId);
  row.style.display='';
  nextButton.style.visibility='hidden';
}

function closeWindow() {
  window.opener = window;
  window.close();
}

function resizeWindow(w, h) {
  var left = (screen.width - width) / 2;
  var top = (screen.height - height) / 2;

  winprops = 'height='+height+',width='+width+',top='+wint+',left='+winl+',scrollbars=0,resizable=0';
  win = window.open("/exec/gallery/maximized/" + imageId, '', winprops);
  if (parseInt(navigator.appVersion) >= 4) {
    win.window.focus();
  }
}

function changeTextFieldFromSelect(select, textField) {
  textField.value = select.options[select.selectedIndex].value;
}

function changeSelectFromTextField(textField, select) {
  for(var i=0; i<select.options.length; i++) {
    if(select.options[i].value.toLowerCase() == textField.value.toLowerCase()) {
      select.selectedIndex = i;
      return;
    }
  }
  select.selectedIndex = 0;
  if(textField.value.length == 0) textField.value = select.options[select.selectedIndex].value;
}

function updateValuesByFieldName(form, value, namePrefix) {
  for(var i=0; i<form.elements.length; i++) if(form.elements[i].name.indexOf(namePrefix) == 0) {
    form.elements[i].value = value;
  }
}

