/*
 * Javascript functions for MCC's e-shop
 * Copyright (c) 2005 by Pavel Krpata, MC Com s.r.o.
 */

// otevre stranku v novem okne bez ovladacich prvku
function newWin(URL, name, width, height) {
  var anon_win = window.open(URL, name, 'scrollbars=no,resizable=no,width='+width+',height='+height)
  anon_win.focus()
}

// otevre nove okno s obrazkem
function newLargeImage(name, title, width, height) {
  imgwin = window.open('', 'LargeImage', 'scrollbars=no,resizable=no,'+
                       'width='+width+',height='+height)
  imgwin.document.location.reload()
  imgwin.document.write('<body leftmargin="0" topmargin="0" bottommargin="0" onclick="window.close()" title="zavřít"><img src="'+name+'" /></body>')
  imgwin.document.title = title
  imgwin.focus()
}

// trim leading & trailing whitespaces
function trim(str) {
  return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '')
}

// Kontrola maxlen pro textarea, obsluhuje onchange, onkeyup a onmousemove
function maxLen(item, len) {
  if (item.value.length > len) {
    item.value = item.value.substring(0,len)
    alert('Maximální povolená délka textu je ' + len + ' znaků.')
  }
}

// a simple check of e-mail addr
function checkEmail(email) {
  return ((form.email.value.indexOf('@') > 0 && form.email.value.indexOf('@') < form.email.value.length-1))
}

// handle for onkeyup, etc.
function checkNumberKeyUp(item) {
  if (item.value == "")
    return true

  newval = item.value;
// remove leading zeros
  if (newval != 0)
    newval = newval.replace(/^0+/, "")
  newval = parseInt(newval)
  if (isNaN(newval))
    newval = ""
  item.value = newval
  return true
}

// handle for onchange, etc.
function checkNumber(item) {
  if (isNaN(parseInt(item.value))) {
    alert("Chybně zadané číslo.")
  }
}

// get value of selected radio button from a group
function getRadioValue(item) {
  var val = -1
  if (item.length == undefined) {   // handle case of only one choice correctly
    if (item.checked && !item.disabled)
      val = item.value;
  }
  else {
    for (var i = 0; i < item.length; i++) {
      if (item[i].checked && !item[i].disabled)
        val = item[i].value
    }
  }
  return val
}

function checkLoginForm(form) {
  if (form.user.value == "" || form.passwd.value == "") {
    alert("Přihlašovací jméno a heslo musí být vyplněné.")
    return false
  }
  else
    return true
}

// pridani polozky do kosiku - kontroluje zadani ID a mnozstvi
function checkAddItemForm(form) {
  var msg = ""

  if (form.cart_addextid.value == "") {
    msg += "Zadejte kód zboží.\n"
  }
  if (isNaN(parseInt(form.cart_addnum.value)) || parseInt(form.cart_addnum.value) <= 0) {
    msg += "Množství zboží musí být kladné číslo.\n"
  }

  if (msg != "") {
    alert(msg)
    return false
  }
  else
    return true
}

// aktualizuje povolene platby podle zvoleneho zpusobu dopravy
function updatePayments(form) {
  deliv_id = getRadioValue(form.deliv_id)
  if (deliv_id == -1)
    return
  for (var pay_id in pay_enab_arr[deliv_id]) {
    paybutt = eval("form.pay"+pay_id)
    if (paybutt !== undefined) {
      paybutt.disabled = !pay_enab_arr[deliv_id][pay_id]
      if (!pay_enab_arr[deliv_id][pay_id])
        paybutt.checked = false
    }
  }
}

function checkOrderForm(form) {
  if (getRadioValue(form.deliv_id) == -1) {
    alert("Nejprve je třeba zvolit způsob doručení a platby.")
    return false
  }
  else if (getRadioValue(form.payment_id) == -1) {
    alert("Zvolte prosím způsob platby.")
    return false
  }
  else
    return true
}
