function myPopup(url, width, height) {
  window.open(url, "myWindow", "status = 1, width = " + width + ", height = " + height + ", resizable = 1, scrollbars = 1, menubar = 1");
}

function mySubmit(button, nextActionVar, nextActionValue) {
  na = document.getElementById(nextActionVar);
  na.value = nextActionValue;
  b = document.getElementById(button);
  b.click();
  return false;
}

function updatePhotoSize(eCombo, eLabel, printSizes, mPrintable, mNonPrintable) {
  nr = document.getElementById(eCombo);
  label = document.getElementById(eLabel);
  if (nr.value && printSizes) {
    contains = 0;
    val = nr.value;
    do {
      i = printSizes.indexOf(',');
      if (i != -1) {
        size = printSizes.slice(0,i);
      } else {
        size = printSizes;
      }
      printSizes = printSizes.slice(i+1);
      if (size == val) {        
        contains = 1;
        break;
      }
    } while (i != -1);
    if (contains) {
      label.value = mPrintable;
    } else {
      label.value = mNonPrintable;
    }
  } else {
    label.value = '';
  }  
}

function updateReservationPrice(eCombo, eLabel, pricePerDay) {
  nr = document.getElementById(eCombo);
  label = document.getElementById(eLabel);
  if (nr.value) {
    label.value = nr.value * pricePerDay;
  } else {
    label.value = '0,00';
  }
}

function checkAuthorization(userId, text) {
  if (userId == null) {
     alert(text);
     return false;
  }
  return true;
}

function updateRegion(eCombo, eRegion, eCharge, ignoreId) {
  nr = document.getElementById(eCombo);
  if (nr.value) {
    val = nr.value;
    i = val.indexOf('_');
    if (ignoreId) {
      val = val.slice(i+1);
      i = val.indexOf('_');
    }
    regionStr = val.slice(0, i);
    chargeStr = val.slice(i+1);
  } else {
    regionStr = chargeStr = '';
  }
  if (eRegion) {
    region = document.getElementById(eRegion);
    region.value = regionStr;
  }
  if (eCharge) {
    charge = document.getElementById(eCharge);
    charge.value = chargeStr + '%';
  }
}

function addClass (elementName, newClass) {
  elementName.className = newClass + (elementName.className ? ' ' : '') + elementName.className;
}

function removeClass(elementName, oldClass) {
  var classesOld = elementName.className.split(' ');
  var classesNew = new Array();
  var i;

  for (i in classesOld) {
    if (classesOld[i] != oldClass) {
      classesNew.push(classesOld[i]);
    }
  }
  elementName.className = classesNew.join(' ');
}

function showHidePeriod() {
	licenceCheckbox = document.getElementById('fi_licence');
	periodDiv = document.getElementById('period');
	if (licenceCheckbox.value == 'RM') {
		periodDiv.style.display = 'inline';
	} else {
		periodDiv.style.display = 'none';
		periodCheckbox = document.getElementById('fi_period');
		periodCheckbox.value = null;
	}
}

function changePriceTypeAndSubmit(button, nextAction, action) {
	priceTypeSelect = document.getElementById('fi_priceType');
	priceTypeSelect.value = 'SPECIAL';
	oldPriceType = document.getElementById('fi_oldPriceType');
	if (oldPriceType.value != priceTypeSelect.value) {
		changed='?changed=yes';
	} else {
		changed = '';
	}
	oldPriceType.value = priceTypeSelect.value;
	return mySubmit(button, nextAction, action+changed);
}

function priceTypeChanged(text, button, nextActionVar, nextActionValue) {
	priceTypeSelect = document.getElementById('fi_priceType');
	oldPriceType = document.getElementById('fi_oldPriceType');
	if (priceTypeSelect.value != 'SPECIAL' && oldPriceType.value == 'SPECIAL') {
		if (confirm(text)) {
			oldPriceType.value = priceTypeSelect.value;
			return mySubmit(button, nextActionVar, nextActionValue);
		} else {
			priceTypeSelect.value = oldPriceType.value;
			return false;
		}
	} else {
		oldPriceType.value = priceTypeSelect.value;
		return mySubmit(button, nextActionVar, nextActionValue);
	}
	oldPriceType.value = priceTypeSelect.value;
	return true;
}

function myConfirmedSubmit(message, button, nextActionVar, nextActionValue) {
  if (confirm(message)) {
    mySubmit(button, nextActionVar, nextActionValue);
  }
  return false;
}

