// Format number with separators.
// Note. Use toFixed to set precision after the decimal point.
// Examples:
// var num = 1234.5678;
// numberFormat(num);                      // Returns 1.234,5678
// numberFormat(num", ",", ".");           // Returns 1.234,5678
// numberFormat(num", ".", ",");           // Returns 1,234.5678
// numberFormat(num.toFixed(2));           // Returns 1.234,56
function numberFormat(number, decpoint, sep) {
  var str, n, f, num, i, dp, s=0;
  if (arguments.length == 1) {
    var sep = ".";
    var decpoint = ",";
  }
  str = number.toString();
  str = str.split(".");
  n = str[0];
  f = str[1];
  num = "";
  if (typeof(n) != "undefined") {
    if(n.charAt(0)=="-") {
      n=n.substring(1,n.length);
      s++;
    }
    for(i = n.length-1, dp = 0; i >= 0; i--) { 
      num = n.charAt(i) + "" + num;
	if ((++dp % 3 == 0) && i > 0) {
	  num = sep + "" + num;
	  dp = 0;
	}
    }
    if(s) num="-"+num;
  }
  if (typeof(f) != "undefined") {
    num += decpoint + "" + f;
  }
  return num;
}

// toFixed function for browsers which don't support JS1.5
if (!Number.prototype.toFixed) {
  Number.prototype.toFixed=function(n){
    if (isNaN(this)) return this;
    if (arguments.length <= 0) var n = 0;
    var p = Math.pow(10, n);
    var s = Math.round(this * p) / p;
    s = s.toString();
    s = s.split(".");
    var d = (s.length > 1) ? s[1] : "";
    while (n > d.length) d = d + "0";
    return (n) ? s[0] + "." + d : s[0];
  }
}

function truncate_url(url)
{
  var u=url.split("://");
  var protocol=(u.length>1)?u[0]+"://":"";
  var path=(u.length>1)?u[1].split("/"):u[0].split("/");

  if(url.substr(url.length-1,1)=="/") {
    path=(path.length>3)?new Array(path[0],"...",path[path.length-2],""):path;
  } else {
    path=(path.length>2)?new Array(path[0],"...",path[path.length-1]):path;
  }

  if(!protocol) {
     var len = 50;
     url = path[path.length-1];
     if(url.length > len) {
        path[path.length-1] = url.substring(0, len/2) + "..." + url.substr(url.length-len/2, url.length);
     }
  }
  
  if(protocol)
    return protocol+path.join("/");
  else
    return path[path.length-1];
}

function selectPage(index)
{
  var prgroup="";
  var url = "index.php?page_id=1010&" + $("#product_search").formSerialize() + "&c101_p_start=" + index;  
  $("#product_list").load(url,{},function(){
    $("#product_search_form").css("display", "");
    //scroll(0,0);
  });
  //var url = "index.php?page_id=1007&" + $("#product_search").formSerialize() + "&c101_p_start=" + index;  
  //$("#product_search_container").load(url);  
}

function selectProduct(id)
{
  var url="index.php?page_id=1008&c101_pv_id="+id;
  $("#product_list").load(url,{},function() {
    //scroll(0,0);
  });
}

function addProduct() {
  addProductOptions();
  
  var code=document.product.code.value;
  var qty=document.product.qty.value;
  
  var url="index.php?page_id=1009&c17__view=addcart&c17__pcode="+code+"&c17__qty="+qty;
  $("#product_list").load(url,{},function(){
    $("#product_search_form").hide();
    //scroll(0,0);
  });
}

function addProductOptions() {
  var code=[],qty=[];
  $("#productoptions").find("input[@type$='checkbox']").each(function() {
    var n=this.name.split("_");
    var id=(n.length>1)?n[1]:null;
    if(n[0]=="opt" && id && this.checked) {
      var c=$("#productoptions").find("input[@name$='code_"+id+"']").attr("value");
      var q=$("#productoptions").find("input[@name$='qty_"+id+"']").attr("value");
      if(c && q) {
        code[code.length]=c;
	qty[qty.length]=q;
      }
    }
  });
  if(code.length > 0) {
    code=code.join("|");
    qty=qty.join("|");
    var url="index.php?page_id=1009&c17__view=addcart&c17__pcode="+code+"&c17__qty="+qty;
    $("#product_list").load(url,{},function(){
      //scroll(0,0);
    });
  }
}

function cart() {
  var url="index.php?page_id=1009";
  $("#product_list").load(url,{},function(){
    $("#product_search_form").hide();    
    //scroll(0,0);
  });
}

function orderformEdit() {
  var url="index.php?page_id=1011";
  $("#product_list").load(url,{},function(){
    //scroll(0,0);
  });
}

function submitOrderForm() {

  if($("#terms:checked").length != 1) {
    alert("Käyttöehdot pitää hyväksyä ennen tilausta");
    return false;
  }

  $("#submitorder").val("Tilausta tallennetaan...").attr("disabled", "disabled");
  
  var options = {
    target: '#product_list',
    type: 'post',
    success: function (responseText, statusText) {
      scroll(0,0);
    }
  };
  
  $("#ordertable").ajaxForm(options);
  $("#ordertable").submit();
}

function orderformSendTo() {
  var url="services.php?c=104&c104__view=mail";
  $("#product_list").load(url,null,function(){

  });
}

function addFormHandler() {
  var options = {
    target: '#product_list',
    type: 'post',	    
    beforeSubmit: showFormStatus
  };
  $('#cart_mail_form').ajaxForm(options);
}

function showFormStatus() {
  $("#send_status").html("Lähetetään....");
}

function orderformOffer() {
  var url="services.php?c=104&c104__view=mail&c104__offer=1";
  $("#product_list").load(url);
}

function cartCompare() {
  //var url="services.php?c=104&c104__view=compare";
  var url="index.php?page_id=1015";
  window.open (url, "mywindow","location=1,status=1,scrollbars=1,width=800,height=600"); 
  //  $("#product_list").load(url);
}
