var sizes = new Array("S","M","L","XL");
var productSize = new Array();
var productDesc = new Array();

var d=new Date(); // a datum a cookiehoz kell

function getStyleById(objectId) {
    if(document.getElementById && document.getElementById(objectId)) {
  return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
  return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
  return document.layers[objectId];
    } else {
  return false;
    }
}

function getObjectById(objectId) {
    if(document.getElementById && document.getElementById(objectId)) {
  return document.getElementById(objectId);
    } else if (document.all && document.all(objectId)) {
  return document.all(objectId);
    } else if (document.layers && document.layers[objectId]) {
  return document.layers[objectId];
    } else {
  return false;
    }
}

function showToolTip(e, txt) {
  var styleObject = getStyleById("toolTip");
  var htmlObject = getObjectById("toolTip");
	var isIE = document.all;
	var mouseX = 0;
	var mouseY = 0;
  if (!e) e = window.event;
	mouseX = isIE ? (e.clientX + document.body.scrollLeft) : e.pageX;
	mouseY = isIE ? (e.clientY + document.body.scrollTop) : e.pageY;

  htmlObject.innerHTML = txt;
  styleObject.top = -24+mouseY+"px";
  styleObject.left = -80+mouseX+"px";
  styleObject.visibility = "visible";
	return false;
}

function hideToolTip() {
  tts = getStyleById('toolTip');
  tts.visibility="hidden";
  return false;
}

function getPrice(prodId, size) {
  for (var i=0; i < sizes.length; i++) {
    if ((size != '') && (size == sizes[i])) {
      return productSize[prodId][i];
    }
  }
}

function printPrice(prodId) {
  var formId = eval("document.prod"+prodId);
  var objPrice = getObjectById("price"+prodId);
  var price = 0;
  price = getPrice(prodId,formId.size.value);
  priceTxt = ""+price;
  if ((price > 9999) && (price < 100000)){
    var priceTxt = priceTxt.substr(0,2)+"."+priceTxt.substr(2);
  }
  objPrice.innerHTML = priceTxt+" Ft";
}

function updatePrice(prodId) {
  var formId = eval("document.prod"+prodId);
  var objPrice = getObjectById("price"+prodId);
  var price = 0;
  price = formId.quantity.value * getPrice(prodId,formId.size.value);
  priceTxt = ""+price;
  if ((price > 9999) && (price < 100000)){
    var priceTxt = priceTxt.substr(0,2)+"."+priceTxt.substr(2);
  }
  objPrice.innerHTML = priceTxt+" Ft";
}

function selectSize(prodId, size) {
  var obj = getObjectById("size"+prodId);

  var noOfSizes = 0;
  var inputTxt = "";
  var minSize = "";
  
  for (var i=0; i < sizes.length; i++) {
    if (productSize[prodId][i] > 0) {
      noOfSizes++;
      if (minSize == "") {
        minSize = sizes[i];
      }
    }
  }
  obj.innerHTML  = "";
  var txt = new String();
  txt= "";
  if (noOfSizes > 1) {
    for (var i=0; i < sizes.length; i++) {
      selectedSize = "";
      if ((size != '') && (size == sizes[i])) {
        inputTxt = "<input type=hidden name=size value="+sizes[i]+">";
        selectedSize = "class=selectedSize";
      } else if ((size == '') && (minSize == sizes[i])) {
        inputTxt = "<input type=hidden name=size value="+sizes[i]+">";
        selectedSize = "class=selectedSize";
        minSize = '';
      }
      if (productSize[prodId][i] > 0) {
        txt += "<a href=\"javascript:void(0);\" "+selectedSize+" ";
        txt += "onClick=\"selectSize('"+prodId+"','"+sizes[i]+"')\" ";
        if (productDesc[prodId][i] != '') {
          txt += " onMouseOver=\"return showToolTip(event, '"+productDesc[prodId][i]+"')\" ";
          txt += " onMouseOut=\"return hideToolTip()\"";
        }
        txt += ">"+sizes[i]+"</a>";
      }
    }
  } else {
    inputTxt = "<input type=hidden name=size value="+minSize+">";
  }
  obj.innerHTML += txt+inputTxt;
  printPrice(prodId);
  return false;
}

function adjustCount(prodId, count) {
  var obj = getObjectById("count"+prodId);
  if ((count < 10) && (count > 0)) {
    obj.innerHTML = "";
    obj.innerHTML += "<span class=productvaluecount>x"+count+"</span>";
    obj.innerHTML += "<a href=\"javascript:void(0);\" onClick=\"adjustCount("+prodId+","+(count+1)+")\">+</a>";
    obj.innerHTML += "<a href=\"javascript:void(0);\" onClick=\"adjustCount("+prodId+","+(count-1)+")\">-</a>";
    obj.innerHTML += "<input type=hidden name=quantity value="+count+">";
    printPrice(prodId);
  }
  return false;
}

function setCartCookie(cookieValue) {
  var expdate = new Date(); 
  //expdate.setTime(expdate.getTime() + 24*60*60*1000); //1 napos cookie
  //document.cookie = "cart="+cookieValue+"; expires="+(expdate.toGMTString())+"; ";
  document.cookie = "cart="+cookieValue;
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function toCart(prodId) {
  var formId = eval("document.prod"+prodId);
  var qty = formId.quantity.value;
  var size = formId.size.value;
  var v = "|"+prodId+","+size+","+qty;
  var c = readCookie("cart");
  var cookieTxt = "";

  if ((c == null) || (c == "")) {
    cookieTxt = v;
  } else {
    var products = new Array();
    products = c.split('|');
    added = false;
    for (i =0; i< products.length; i++) {
      if (products[i]) {
        product = new Array();
        product = products[i].split(',');
        if ( (product[0] == prodId) &&
             (product[1] == size) ) {
           product[2] = Number(product[2]);
           if (product[2]+Number(qty) > 9) {
              alert("Csak 9 azonos terméket rendelhetsz, ebből pedig már "+product[2]+" van a kosaradban.");
              return false;
            }
           product[2] += Number(qty);
           added = true;
        }
        cookieTxt += "|"+product[0]+","+product[1]+","+product[2];
      }
    }
    if (added == false) {
      cookieTxt = c+v;
    }
  }
  /*
  if ((c == null) || (c == "")) {
    cookieTxt = v;
  } else {
    cookieTxt = c+v;
  }*/
  setCartCookie(cookieTxt);
  return true;
}

function removeFromCart(prod) {
  var c = readCookie("cart");
  var cookieTxt = "";
  if ((c == null) || (c == "")) {
    return true;
  } else {
    if ((startPos = c.indexOf(prod,0)) > -1) {
      cookieTxt = c.substr(0,startPos);
      if ((endPos = c.indexOf("|",startPos+1)) < 1) {
      } else {
        cookieTxt += c.substr(endPos);
      }
    } else {
      cookieTxt = '';
    }
  }
  setCartCookie(cookieTxt);
  return true;
}

function selectByValue(sel, value) {
  var i = 0;
  var end = sel.options.length;
  while(i < end) {
    if (sel.options[i].selected == true) {
      return sel.options[i].value;
    }
    i++;
  }
}

function refreshCart(prodId, prodSize, prodPrice) {
  var qty = eval("document.cart.cartqty"+prodId+prodSize+".selectedIndex");
  qty++;

  var c = readCookie("cart");
  var cookieTxt = "";
  if ((c == null) || (c == "")) {
    return true;
  } else {
    if ((startPos = c.indexOf("|"+prodId+","+prodSize,0)) > -1) {
      cookieTxt = c.substr(0,startPos);
      cookieTxt += "|"+prodId+","+prodSize+","+qty;
      if ((endPos = c.indexOf("|",startPos+1)) < 1) {
      } else {
        cookieTxt += c.substr(endPos);
      }
    }
  }
  setCartCookie(cookieTxt);
  document.cart.submit();
}

function trim(str)
{
  return str.replace(/^\s+|\s+$/g,'');
}

function isEmpty(formElement, message) {
  formElement.value = trim(formElement.value);
  _isEmpty = false;
  if (formElement.value == '') {
    _isEmpty = true;
    alert(message);
    formElement.focus();
  }
  
  return _isEmpty;
}

function checkShippingAndPaymentInfo()
{
  alert("checkShippingAndPaymentInfo");
  with (window.frmCheckout) {
    if (isEmpty(txtShippingName, 'Adja meg a nevét!')) {
      return false;
    } else if (isEmpty(txtShippingCity, 'Adja meg a várost!')) {
      return false;
    } else if (isEmpty(txtShippingAddress, 'Adja meg az utcanevet és a házszámot!')) {
      return false;
    } else if (isEmpty(txtShippingPostalcode, 'Adja meg az irányítószámot!')) {
      return false;
    } else if (isEmpty(txtShippingPhone, 'Adja meg a telefonszámot!')) {
      return false;
    } else if (isEmpty(txtPaymentName, 'Adja meg a vezetéknevét!')) {
      return false;
    } else if (isEmpty(txtPaymentCity, 'Adja meg a várost!')) {
      return false;
    } else if (isEmpty(txtPaymentAddress1, 'Adja meg az utcanevet és a házszámot!')) {
      return false;
    } else if (isEmpty(txtPaymentPostalcode, 'Adja meg az irányítószámot!')) {
      return false;
    } else if (isEmpty(txtPaymentPhone, 'Adja meg a telefonszámot!')) {
      return false;
    } else {
      return true;
    }
  }
}

function setPaymentInfo(isChecked)
{
  with (document.frmCheckout) {
    if (isChecked) {
      txtPaymentName.value       = txtShippingContact.value;
      txtPaymentPostalCode.value = txtShippingPostalCode.value;
      txtPaymentCity.value       = txtShippingCity.value;
      txtPaymentAddress.value    = txtShippingAddress.value;
      
      txtPaymentName.readOnly       = true;
      txtPaymentAddress.readOnly    = true;
      txtPaymentCity.readOnly       = true;
      txtPaymentPostalCode.readOnly = true;

      //var obj = getStyleById("txtPaymentBlock");
      //obj.display = "none";
    } else {
      txtPaymentName.readOnly       = false;
      txtPaymentAddress.readOnly    = false;
      txtPaymentCity.readOnly       = false;
      txtPaymentPostalCode.readOnly = false;      

      txtPaymentName.value       = '';
      txtPaymentAddress.value    = '';
      txtPaymentCity.value       = '';
      txtPaymentPostalCode.value = '';
      //var obj = getStyleById("txtPaymentBlock");
      //obj.display = "block";
    }
  }
}

function checkPostalCode(t, p) {
  var code = Number(t.value);
  if ( (code >= 1011) && (code <= 1239) ) { p.value = "Budapest" } else
  if ( (code >= 3501) && (code <= 3535) ) { p.value = "Miskolc"}  else
  if ( (code >= 4002) && (code <= 4225) ) { p.value = "Debrecen"} else
  if ( (code >= 6710) && (code <= 6791) ) { p.value = "Szeged"}   else
  if ( (code >= 7621) && (code <= 7693) ) { p.value = "Pécs"}     else
  if ( (code >= 9011) && (code <= 9030) ) { p.value = "Győr"}     else
  if ( pc[code] != undefined ) p.value = pc[code];
}

function checkOut1_check() {
  with (document.frmCheckout) {
    if (chkSame.checked == true) {
      setPaymentInfo(true);
    }

    var emailFilter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    var telFilter=/^([\\\/\+\- 0-9]+){7}$/i
    if (txtShippingName.value == "") { alert ('Az átvevő neve nem lett kitöltve.'); return false;}
    if (txtShippingName.value.length < 6) { alert ('Az átvevő neve túl rövid.'); return false;}
    if ((txtShippingPostalCode.value == "") ||
        (txtShippingCity.value == "") ||
        (txtShippingAddress.value == "") ||
        (txtShippingPostalCode.value == "") )
        { alert ('A kiszállítás címe nem lett kitöltve.'); return false;}

    if (txtShippingContact.value == "") { alert ('A kapcsolattartó neve nem lett kitöltve.'); return false;}
    if (txtShippingContact.value.length < 6) { alert ('A kapcsolattartó neve túl rövid.'); return false;}
    /*if (telFilter.test(txtShippingPhone.value)) { } else { alert ('A kapcsolattartó telefonszáma helytelen.'); return false; }*/
    if (emailFilter.test(txtShippingContactEmail.value)) { } else { alert ('A kapcsolattartó e-mail címe helytelen.'); return false; }

    if (chkSame.checked == false) {
      if (txtPaymentName.value == "") { alert ('A számlázási név nem lett kitöltve. Ha a számlázási cím megegyezik a kiszállítás címével, pipáld be a \"Megegyezik a kiszállítás címével\" szöveg mellett.'); return false;}
      if ((txtShippingPostalCode.value == "") ||
          (txtShippingCity.value == "") ||
          (txtShippingAddress.value == "") ||
          (txtShippingPostalCode.value == "") )
          { alert ('A számlázási cím nem lett kitöltve. Ha a számlázási cím megegyezik a kiszállítás címével, pipáld be a \"Megegyezik a kiszállítás címével\" szöveg mellett.'); return false;}
    }
    document.frmCheckout.submit();
  }
  return false;
}
function orderSubmit() {
  document.order.submit();
  return false;
}
function deleteFields() {
  with (document.frmCheckout) {
    txtShippingName        .value = '';
    txtShippingPostalCode  .value = '';
    txtShippingCity        .value = '';
    txtShippingAddress     .value = '';
    txtShippingContact     .value = '';
    txtShippingPhone       .value = '';
    txtShippingContactEmail.value = '';
    optShipment[0]         .checked = true;
    chkSame                .checked = false;
    setPaymentInfo(chkSame.checked);
    txtPaymentName         .value = '';
    txtPaymentPostalCode   .value = '';
    txtPaymentCity         .value = '';
    txtPaymentAddress      .value = '';
    optPayment[0]          .checked = true;
    txtComment             .value = '';
    optSave                .checked = false;
  }
  document.cookie = "userinfo=; expires=-1; ";
}

function hidePaymentTypes() {
  var obj = getStyleById("paymentDelivery");
  obj.display = "none";  
  var obj = getStyleById("paymentCheque");
  obj.display = "none";
  if ( document.frmCheckout.optPayment[1].checked ) {
    document.frmCheckout.optPayment[0].checked = false;
    document.frmCheckout.optPayment[1].checked = true;
  } else {
    document.frmCheckout.optPayment[0].checked = true;
    document.frmCheckout.optPayment[1].checked = false;
  }
  document.frmCheckout.optPayment[2].checked = false;
  document.frmCheckout.optPayment[3].checked = false;
}
function showPaymentTypes() {
  var obj = getStyleById("paymentDelivery");
  obj.display = "block";
  var obj = getStyleById("paymentCheque");
  obj.display = "block";  
}
