function checkLoginForm(formular)
{
  var ok = true;
    
  if(ok && !formular.username.value)
  {
    alert(messages["missing_login_user_name"]);
    formular.username.focus();
    ok = false;
  }
  
  if(ok && !formular.userpasswd.value)
  {
    alert(messages["missing_login_passwd"]);
    formular.userpasswd.focus();
    ok = false;
  }
  
  return ok;
}


function checkRetrPasswd(formular)
{
  var ok = true;
  
  if(ok && !formular.username.value)
  {
    alert(messages["missing_login_user_name"]);
    formular.username.focus();
    ok = false;
  }
  
  return ok;
}



// ****************************************************************************
// CheckSingupForm


function checkSignupForm(formular, how)
{
  var ok = true;
  
  // signup (new user account)
  if(how == 1)
  {
    if(ok && !formular.username.value)
    {
      alert(messages["missing_user_name"]);
      formular.username.focus();
      ok = false;
    }
    
    if(ok && !formular.userpasswd.value)
    {
      alert(messages["missing_passwd"]);
      formular.userpasswd.focus();
      ok = false;
    }
    
    if(ok && !formular.userpasswd2.value)
    {
      alert(messages["retype_passwd"]);
      formular.userpasswd2.focus();
      ok = false;
    }
    
    if(ok && (formular.userpasswd.value != formular.userpasswd2.value))
    {
      alert(messages["not_matching_passwd"]);
      formular.userpasswd.value = "";
      formular.userpasswd2.value = "";
      
      formular.userpasswd.focus();
      ok = false;
    }
  }
  
  if(ok && !formular.email.value)
  {
    alert(messages["missing_email"]);
    formular.email.focus();
    ok = false;
  }
  
  if(ok && !is_email(formular.email.value))
  {
    alert(messages["invalid_email"]);
    formular.email.focus();
    ok = false;
  }
  
  if(ok && !formular.first_name.value)
  {
    alert(messages["missing_first_name"]);
    formular.first_name.focus();
    ok = false;
  }
  
  if(ok && !formular.last_name.value)
  {
    alert(messages["missing_last_name"]);
    formular.last_name.focus();
    ok = false;
  }
  
  if(ok && formular.customerType.value == -1)
  {
    alert(messages["missing_cust_type"]);
    formular.customerType.focus();
    ok = false;
  }
  
  
  return ok;
}





function visibleOneAddressOnly()
{
  var div = document.getElementById("one-address-only");
  div.style.visibility = "visible";
}

function useShippingAddress()
{
  var formular;
  formular = document.getElementById("checkoutForm");
  
  fillBillingAddress(formular.one_address_only.checked);
}

function synchronizeAddress()
{
  var formular;
  formular = document.getElementById("checkoutForm");
  
  if(formular.one_address_only.checked == true)
    fillBillingAddress(true);
}

function fillBillingAddress(fill)
{
  var formular;
  formular = document.getElementById("checkoutForm");
  
  if(fill == true)
  {
    formular.bill_first_name.value = formular.first_name.value;
    formular.bill_last_name.value = formular.last_name.value;
    formular.bill_company.value = formular.company.value;
    
    formular.bill_street.value = formular.street.value;
    formular.bill_city.value = formular.city.value;
    formular.bill_country.value = formular.country.value;
    formular.bill_state.value = formular.state.value;
    formular.bill_zip.value = formular.zip.value;
    
    disableBillingAddress(true);  
  }
  else
  {
    disableBillingAddress(false);
  }
}

function disableBillingAddress(how)
{
  var formular;
  formular = document.getElementById("checkoutForm");
  
  formular.bill_first_name.disabled = how;
  formular.bill_last_name.disabled = how;
  formular.bill_company.disabled = how;
  
  formular.bill_street.disabled = how;
  formular.bill_city.disabled = how;
  formular.bill_country.disabled = how;
  formular.bill_state.disabled = how;
  formular.bill_zip.disabled = how;
}

function checkoutDefaultCursor()
{
  var formular;
  formular = document.getElementById("checkoutForm");
  
  formular.first_name.focus();
}

function orderLoad()
{
  checkoutDefaultCursor();
  visibleOneAddressOnly();
}

// ***************************************************************************
// checkOrder
// ***************************************************************************

function checkOrder(formular)
{
  var ok = true;
  
  // ******************************************************
  // Shipping address 
  // ******************************************************
  
  if(ok && !formular.first_name.value)
  {
    alert(messages["missing_first_name"]);
    formular.first_name.focus();
    ok = false;
  }
  
  if(ok && !formular.last_name.value)
  {
    alert(messages["missing_last_name"]);
    formular.last_name.focus();
    ok = false;
  }
  
  if(ok && !formular.street.value)
  {
    alert(messages["missing_address"]);
    formular.street.focus();
    ok = false;
  }
  
  if(ok && !formular.city.value)
  {
    alert(messages["missing_city"]);
    formular.city.focus();
    ok = false;
  }
  
  if(ok && !formular.country.value)
  {
    formular.state.value = "";
    alert(messages["missing_country"]);
    formular.country.focus();
    ok = false;
  }
  
  if(ok 
    && (formular.country.value == signs["usa"] || formular.country.value == signs["can"]
        || formular.country.value == signs["usa_short"] || formular.country.value == signs["can_short"]) 
    && (!formular.state.value || formular.state.value == signs["non_us"]))
  {
    formular.state.value = "";
    alert(messages["missing_state"]);
    formular.state.focus();
    ok = false;
  }
  
  
  if(ok  
    && formular.country.value != signs["usa"] 
    && formular.country.value != signs["usa_short"]
    && formular.country.value != signs["can"]
    && formular.country.value != signs["can_short"]
    && formular.state.value != ""
    && formular.state.value != signs["non_us"])
  {
    //formular.state.value = "";
    alert(messages["non_corresponding_country_state"]);
    
    if(formular.one_address_only.checked == true)
      formular.bill_state.value = formular.state.value;
    formular.state.focus();
    ok = false;
  }
  
  if(ok && !formular.zip.value)
  {
    alert(messages["missing_zip"]);
    formular.zip.focus();
    ok = false;
  }
  
  
  // ******************************************************
  // Billing address 
  // ******************************************************
  
  if(ok && !formular.bill_first_name.value)
  {
    alert(messages["missing_first_name"]);
    formular.bill_first_name.focus();
    ok = false;
  }
  
  if(ok && !formular.bill_last_name.value)
  {
    alert(messages["missing_last_name"]);
    formular.bill_last_name.focus();
    ok = false;
  }
  
  if(ok && !formular.bill_country.value)
  {
    formular.bill_state.value = "";
    alert(messages["missing_country"]);
    formular.bill_country.focus();
    ok = false;
  }
  
  if(ok 
    && (formular.bill_country.value == signs["usa"] || formular.bill_country.value == signs["can"]
        || formular.bill_country.value == signs["usa_short"] || formular.bill_country.value == signs["can_short"]) 
    && (!formular.bill_state.value || formular.bill_state.value == signs["non_us"]))
  {
    formular.bill_state.value = "";
    alert(messages["missing_state"]);
    formular.bill_state.focus();
    ok = false;
  }
  
  
  if(ok  
    && formular.bill_country.value != signs["usa"] 
    && formular.bill_country.value != signs["usa_short"]
    && formular.bill_country.value != signs["can"]
    && formular.bill_country.value != signs["can_short"]
    && formular.bill_state.value != ""
    && formular.bill_state.value != signs["non_us"])
  {
    //formular.bill_state.value = signs["non_us"];
    alert(messages["non_corresponding_country_state"]);
    
    if(formular.one_address_only.checked == true)
      formular.bill_state.value = formular.bill_state.value;
    formular.bill_state.focus();
    ok = false;
  }
  
  
  // ******************************************************
  // E-mail a Phone
  // ******************************************************
  
  if(ok && !formular.phone.value)
  {
    alert(messages["missing_phone"]);
    formular.phone.focus();
    ok = false;
  }
  
  if(ok && !formular.email.value)
  {
    alert(messages["missing_email"]);
    formular.email.focus();
    ok = false;
  }
  
  if(ok && !is_email(formular.email.value))
  {
    alert(messages["invalid_email"]);
    formular.email.focus();
    ok = false;
  }
  
  
  // ******************************************************
  // Customer Registration 
  // ******************************************************
  
  if(ok && formular.username.value)
  {
    if(ok && !formular.userpasswd.value)
    {
      alert(messages["missing_passwd"]);
      formular.userpasswd.focus();
      ok = false;
    }
    
    if(ok && !formular.userpasswd2.value)
    {
      alert(messages["retype_passwd"]);
      formular.userpasswd2.focus();
      ok = false;
    }
    
    if(ok && (formular.userpasswd.value != formular.userpasswd2.value))
    {
      alert(messages["not_matching_passwd"]);
      formular.userpasswd.value = "";
      formular.userpasswd2.value = "";
      
      formular.userpasswd.focus();
      ok = false;
    }
  }
  
  return ok;
}


function synchShippingPayment(active, type)
{
  // ***********************************************************************
  // disable nevhodnych payment metod na zaklade zvoleni shipping metody 
  // ***********************************************************************
  
  if(type == 1)
  {
    var shipping_is_cod = false;
    for(i=0; shipping_is_cod == false && i<shipping_cod.length; i++)
    {
      if(active == shipping_cod[i])
        shipping_is_cod = true;
    }
   
   
    var pms = document.forms["ospform"].elements["payment_method"];
    
    if(shipping_is_cod == false)
      var disableOptions = payment_cod;
    else
    {
      var disableOptions = new Array();
      
      for(i=0; i<pms.length; i++)
      { 
        var found = false;
        for(j=0; found == false && j<payment_cod.length; j++)
        {
          if(pms[i].value == payment_cod[j])
            found = true;
        }
        
        if(found == false)
          disableOptions.push(pms[i].value);
      }
    }  
    
      
    for(i=0; i<pms.length; i++)
    {
      //alert("resim prvek: " + pms[i].value);
  
      var found = false;  
      for(j=0; found == false && j<disableOptions.length; j++)
      {
        if(pms[i].value == disableOptions[j])
        {
          pms[i].disabled = "disabled";
          pms[i].checked = "";
          found = true;
        }
      }
      if(found == false)
        pms[i].disabled = "";
    }
  }
  
  
  // ***********************************************************************
  // disable nevhodnych payment metod na zaklade zvoleni shipping metody 
  // ***********************************************************************
  
  if(type == 1)
  {
    var shipping_is_cod = false;
    for(i=0; shipping_is_cod == false && i<shipping_cod.length; i++)
    {
      if(active == shipping_cod[i])
        shipping_is_cod = true;
    }
   
   
    var pms = document.forms["ospform"].elements["payment_method"];
    
    if(shipping_is_cod == false)
      var disableOptions = payment_cod;
    else
    {
      var disableOptions = new Array();
      
      for(i=0; i<pms.length; i++)
      { 
        var found = false;
        for(j=0; found == false && j<payment_cod.length; j++)
        {
          if(pms[i].value == payment_cod[j])
            found = true;
        }
        
        if(found == false)
          disableOptions.push(pms[i].value);
      }
    }  
    
    
    for(i=0; i<pms.length; i++)
    {
      //alert("resim prvek: " + pms[i].value);
  
      var found = false;  
      for(j=0; found == false && j<disableOptions.length; j++)
      {
        if(pms[i].value == disableOptions[j])
        {
          pms[i].disabled = "disabled";
          pms[i].checked = "";
          found = true;
        }
      }
      if(found == false)
        pms[i].disabled = "";
    }
  }
}



function checkShippingPayment(formular, smCount, pmCount)
{
  var ok = true;
  var sm;
  var pm;

  
  for (var i=0; i < smCount; i++)
  {
    if (formular.shipping_method[i].checked)
      sm = formular.shipping_method[i].value;
  }
  
  for (i=0; i < pmCount; i++)
  {
    if (formular.payment_method[i].checked)
      pm = formular.payment_method[i].value;
  }
  
  
  // ***************************************************************
  
  
  if(ok && !sm)
  {
    alert(messages["missing_shipping_method"]);
    ok = false;
  }
  
  if(ok && !pm)
  {
    alert(messages["missing_payment_method"]);
    ok = false;
  }

  // ***************************************************************
  
  if(ok)
  {
    var shipping_is_cod = false;
    for(i=0; shipping_is_cod == false && i<shipping_cod.length; i++)
    {
      if(sm == shipping_cod[i])
        shipping_is_cod = true;
    }
    //alert ("shipping_is_cod: " + shipping_is_cod);
    
    
    var payment_is_cod = false;
    for(i=0; payment_is_cod == false && i<payment_cod.length; i++)
    {
      if(pm == payment_cod[i])
        payment_is_cod = true;
    }
    //alert ("payment_is_cod: " + payment_is_cod);
    
    
    if(shipping_is_cod != payment_is_cod)
    { 
      alert(messages["not_corresponding_shipping_payment"]);
      ok = false;
    }
  }
  
  return ok;  
}

function checkPolicyAgreement(formular)
{
  var ok = true;
  
  if(ok && !formular.agreement.checked)
  {
    alert(messages["order_agreement"]);
    formular.agreement.focus();

    ok = false;
  }
  
  
  return ok;
}

function is_email(string) 
{
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}

function newsletterRemoveText()
{
  document.getElementById("newsletter").value = "";
}

function setMenuHighlights(id)
{
  setMainMenuHighlights(id);
}






function deleteCustomerConfirm()
{
  return confirm(messages["account_remove_confirm"]);
}


function checkCheckoutRegisteredUsers(formular)
{
  var ok = true;
  
  if(ok && !formular.username.value)
  {
    alert(messages["missing_login_user_name"]);
    formular.username.focus();
    ok = false;
  }
  
  if(ok && !formular.userpasswd.value)
  {
    alert(messages["missing_login_passwd"]);
    formular.userpasswd.focus();
    ok = false;
  }
  
  return ok;
}

function checkCart(formular)
{
  var q = formular.q.value;
  var onstock = formular.onstock.value;
  var incart = formular.incart.value;
    
  
  var request = onstock - q - incart;
  var msg;
  
  if(request < 0)
  {
    if(onstock == 1)
      msg = messages["last_piece_on_stock"];
    else
      msg = messages["last_pieces_on_stock_1"] + onstock + messages["last_pieces_on_stock_2"];
   
    if(incart > 0)
      if(incart == 1)
        msg += messages["incart_pieces"];
      else
        msg += messages["incart_pieces_1"] + incart + messages["incart_pieces_2"];
        
    alert(msg);
  }   
    
  
  return (request >= 0);
}


function emptyCartConfirm()
{
  return confirm(messages["empty_cart_confirm"]);
}


function checkNewsletterRegistration(formular)
{
  var ok = true;
  
  if(!is_email(formular.newsletter_email.value))
  {
    alert(messages["invalid_email"]);
    ok = false;
  }
  
  return ok;
  
}


function productDetailMove(basePath, how, cntPerPage)
{
  if(!((how == 1 && detailImgFirst == 0) || (how == 2 && detailImgFirst == (detailImgs.length - cntPerPage))))
  {
    var visibleImgs = document.getElementsByName("detailImg");
    var i;
            
    if(how == 1)
    {
      if(detailImgFirst > 0)
      {
        visibleImgs[0].src = basePath + detailImgs[detailImgFirst - 1];
        visibleImgs[1].src = basePath + detailImgs[detailImgFirst];
        visibleImgs[2].src = basePath + detailImgs[detailImgFirst + 1];
        visibleImgs[3].src = basePath + detailImgs[detailImgFirst + 2];
      }
    
      detailImgFirst = detailImgFirst - 1;
    }  
    
    if(how == 2)
    {
      if(detailImgFirst < (detailImgs.length - cntPerPage))
      {
        visibleImgs[0].src = basePath + detailImgs[detailImgFirst + 1];
        visibleImgs[1].src = basePath + detailImgs[detailImgFirst + 2];
        visibleImgs[2].src = basePath + detailImgs[detailImgFirst + 3];
        visibleImgs[3].src = basePath + detailImgs[detailImgFirst + 4];
      }
      
      detailImgFirst = detailImgFirst + 1;
    }
    
    
    var imgBack = document.getElementById("detailBack");
    var imgFwd = document.getElementById("detailFwd");
    
    if(detailImgFirst == 0) 
    {
      imgBack.src = basePath + "detail_back_disabled.gif";
      imgBack.style.cursor = "auto";
    }
    else
    {
      imgBack.src = basePath + "detail_back.gif";
      imgBack.style.cursor = "hand";
    }
    
      
    if(detailImgFirst == (detailImgs.length - cntPerPage)) 
    {
      imgFwd.src = basePath + "detail_fwd_disabled.gif";
      imgFwd.style.cursor = "auto";
    }
    else
    {
      imgFwd.src = basePath + "detail_fwd.gif";
      imgFwd.style.cursor = "hand";
    }
  }
}

// ***************************************************************************
// Subtotal summary on OrderShippingPayment
// ***************************************************************************

function calculateSubtotal(obj, ctrl)
{
  /*
  alert("discount: " + discount);
  alert("ctrl: " + ctrl);
  alert("ctrlValue: " + ctrl.value);
  alert("checked: " + ctrl.checked);
  */
  
  var found = -1;
  var i;
  
  for(i=0; found<0 && i<summary.length; i++)
  {
    if(obj.name == summary[i].name)
      found = i;
  }
  //alert("found: " + found);
  
  if(found >= 0)
    summary.splice(found, 1);
  
  if(ctrl.checked)
    summary.push(obj);
  
  //alert(summary);
   
  generateSummaryTable();
}

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

function compareRanks(a, b) 
{
  return a.rank - b.rank;
}

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

function generateSummaryTable()
{
  document.getElementById("osp-summary").style.visibility = "visible";
  
  //alert("summary: " + summary.length);
  
  if(summary.length > 0)
  {
    subtotal = 0;
    
    for(var i=0; i<summary.length; i++)
    {
      subtotal += summary[i].price;
    }
    //alert("subtotal: " + subtotal);
    
    summary.sort(compareRanks);
      
    var tbody = document.getElementById("summary-table").getElementsByTagName("TBODY")[0];
    
    while(tbody.rows.length > 0)
      tbody.deleteRow(0);
    
    for(var i=0; i<summary.length; i++)
    {
      var row = document.createElement("tr");
      var th = document.createElement("th");
      th.appendChild(document.createTextNode(summary[i].name + ": "));
      var td = document.createElement("td");
      td.appendChild (document.createTextNode(summary[i].formatedPrice));
      row.appendChild(th);
      row.appendChild(td);
      tbody.appendChild(row);
    }
    
    var row = document.createElement("tr");
    var th = document.createElement("th");
    th.colSpan = 2;
    th.className = "spacer";
    th.appendChild (document.createTextNode(""));
    row.appendChild(th);
    tbody.appendChild(row);
    
    var row = document.createElement("tr");
    var th = document.createElement("th");
    th.appendChild(document.createTextNode(totalName));
    th.className = "summary";
    
    var td = document.createElement("td");
    td.className = "summary";
    td.appendChild (document.createTextNode(priceFormat.replace("%", subtotal)));
    row.appendChild(th);
    row.appendChild(td);
    tbody.appendChild(row);
  }
}

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


/*
function changeUsageCats()
{
  if(usageCats.length > 1)
    window.setTimeout("nextUsageCategory()", 10000);
}


function nextUsageCategory()
{
  var image = document.getElementById("usage-image");
  var descr = document.getElementById("usage-descr");
  
  //alert(image.src);
  //alert(descr.innerHTML);
  //alert("new img: " + usageCats[usageCatCurr].image);
  
  image.src = usageCats[usageCatCurr].image;
  descr.innerHTML = usageCats[usageCatCurr].descr;
  
  usageCatCurr++;
  if(usageCatCurr == usageCats.length)
    usageCatCurr = 0;
  
  
  changeUsageCats();
}
*/



