// JavaScript Document

function ajax_put_into_basket(idprod, id) {	
	var variants_count = document.getElementById('order_variants_count_'+idprod).value;
	var varr = new Array();
	if(variants_count > 0) {
		for(var i=0; i<variants_count; i++) {
			var v = document.getElementById('order_varianat_'+idprod+'_'+i);
			var vnh = new Array(v.name, v.value);
			varr[i] = vnh;
		}
	}
	
	if (!ajaxpack.getAjaxRequest("/basket.php", "ajax_action=put_into_basket&id="+idprod+"&count="+document.getElementById(id).value+(varr.length > 0 ? '&variants='+varr : ''), put_into_basket, "text"))		
	{ 
		return false; 		
	} 
	return true; 
}

function put_into_basket() {
	var ajax=ajaxpack.ajaxobj;

	if (ajax.readyState == 4) { 
		if (ajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
				var html = ajax.responseText.toString().split(";");
				if(html > '') {
					document.getElementById('NAKUPNI_KOSIK_ITEM_COUNT').innerHTML = html[0];
					document.getElementById('NAKUPNI_KOSIK_ITEM_ALLPRICE').innerHTML = html[1];
					
					createBastketConfirm();
					
				}
		}
	}	
}

function buySend(id) {

  $('#OrderForm'+id).ajaxForm({success: BasketUpdateHTML}).submit();
  
  return false;
}

function BasketUpdateHTML(response) {
  
  var html = response.toString().split(";");
  
  //alert(html[0]);
  if(html[0]=="false") 
  {
    alert(html[1]);
    return false;
  }
  
	
	if(html.length > 1) {
		document.getElementById('NAKUPNI_KOSIK_ITEM_COUNT').innerHTML = html[0];
		document.getElementById('NAKUPNI_KOSIK_ITEM_ALLPRICE').innerHTML = addCommas(html[1])+',-';
				
		createBastketConfirm();				
	}
}
 
function isStrNumber(str) {
  if(str.length<1) return false;
    
  for(var i=0; i<str.length; i++) {
    
    if(i==0) {
      if(str[i]<'1' || str[i]>'9') return false;
    }
    else {
      if(str[i]<'0' || str[i]>'9') return false;
    }
    
  }
  
  return true;
} 

function BasketUpdateKs(response) {
	var vars = response.toString().split(";");
	if(vars.length > 1) {
		document.getElementById(vars[1]).value = vars[0];
		alert('zmena');	
	}
}
	
var mouseX,mouseY;
function mousePosition(e) {
	var d,b;
	if (!e) {var e=window.event;} //IE mouse event
	if (e.pageX || e.pageY) //other 
		{mouseX=e.pageX; mouseY=e.pageY;}
	else if (e.clientX || e.clientY) { //IE
		d=document; d=d.documentElement?d.documentElement:d.body;
		mouseX=e.clientX+d.scrollLeft; mouseY=e.clientY+d.scrollTop;
	}
}
document.onmousemove=mousePosition;

function createBastketConfirm() {	

	var basket_confirm_box = document.getElementById('basket_confirm_box');
	basket_confirm_box.style.top = mouseY+'px';
	basket_confirm_box.style.left = mouseX+'px';
	basket_confirm_box.style.display = 'block';
}

function registrace_check(chflag) {
	if(chflag) {
		document.getElementById('registrer_flag').checked = true;
		document.getElementById('pass').disabled = false;
		document.getElementById('pass2').disabled = false;
		//document.getElementById('email_requied').style.visibility = 'visible';
		document.getElementById('pass_requied').style.display = '';
		document.getElementById('pass2_requied').style.display = '';
	}
	else {
		document.getElementById('registrer_flag').checked = false;
		document.getElementById('pass').disabled = true;
		document.getElementById('pass2').disabled = true;	
		//document.getElementById('email_requied').style.visibility = 'hidden';
		document.getElementById('pass_requied').style.display = 'none';
		document.getElementById('pass2_requied').style.display = 'none';
	}
}


function order_change_variant(id, idvarianty) {
  
  //alert(id+', '+idvarianty);
  if( (!(id>0)) || (!(idvarianty>=0)) ) return false;
  
  if(idvarianty==0) {
    document.getElementById('order_product_'+id+'_price').value='dle varianty';
    document.getElementById('order_product_'+id+'_variantID').value=-1;
    var nxsl = document.getElementById('order_product_'+id+'_variant_rozmer');
    nxsl.innerHTML='';
    nxsl.disabled = true;
    return 0;
  }

  document.getElementById('buyprodabutton').style.display = 'none';

  $.post("/_ajax.php?action=order_change_variant", 
    { 'id' : id, 'idvarianty' : idvarianty},
    function(data) {
      
      order_change_variantResponse(data, id, idvarianty);
      
    }, "text"
   );   

}

function order_change_variantResponse(data, id, idvarianty) {
  //alert(data);
  
  var nxsl = document.getElementById('order_product_'+id+'_variant_rozmer');
  var obalnxsl = document.getElementById('obal__order_product_'+id+'_variant_rozmer');
  
  if(data=='false') {
    document.getElementById('order_product_'+id+'_price').value='dle varianty';
    document.getElementById('order_product_'+id+'_variantID').value=-1;
    nxsl.innerHTML='';
    nxsl.disabled = true;
    return 0;  
  }
  
  var variants = data.split('|');
  var variants_vals;
  var optionshtml = '<option value="0">- zvolte variantu -</option>';
  var __last_skupina = null;
  for(var i=0; i<variants.length; i++)
  {
    variants_vals = variants[i].split(';'); 
    
    /*if(__last_skupina!=variants_vals[2]){
      if(i>0) optionshtml += '</optgroup>';
      optionshtml += '<optgroup label="'+variants_vals[2]+'">';
      __last_skupina = variants_vals[2];
    }*/
    
    optionshtml += '<option value="'+variants_vals[0]+'">'+variants_vals[1]+'</option>'; 
    
    if(i==variants.length-1) optionshtml += '</optgroup>'; 
  }

  //obalnxsl.removeChild(nxsl);
  //var newselect = document.createElement('select');
  //newselect.setAttribute('id', 'order_product_'+id+'_variant_rozmer"');
  //newselect.setAttribute('name', 'order['+id+'][variant_rozmer]');
  //newselect.onchange = function() { order_change_variant_finish(id, this.value); };
  
    var children = nxsl.childNodes;

    for(var i=children.length-1; i>=0;i--)
       nxsl.removeChild(children[i]);
  
    var newoption = document.createElement('option'); 
    newoption.setAttribute('value', 0);
    newoption.innerHTML = '- zvolte variantu -';
    nxsl.appendChild(newoption);
    
    for(var i=0; i<variants.length; i++)
    {
      variants_vals = variants[i].split(';'); 

      newoption = document.createElement('option'); 
      newoption.setAttribute('value', variants_vals[0]);
      newoption.innerHTML = variants_vals[1];
      nxsl.appendChild(newoption);
    }  
  
  //obalnxsl.appendChild(newselect);
  
  //nxsl.innerHTML = optionshtml;
  //obalnxsl.innerHTML = '<select id="" onchange="">'+optionshtml+'</select>';
  nxsl.disabled = false;
  
  document.getElementById('buyprodabutton').style.display = '';

/*  
  var res = data.split(';');
  
  
    
  if(res[0]!='true') return false;
  
  //alert(res[0]+', '+res[1]+', id:'+id);
  
  var chcenel = document.getElementById('order_product_'+id+'_price');
  
  chcenel.value = res[1];
*/
}




function order_change_variant_finish(id, idvarianty) {
  
  //alert(id+', '+idvarianty);
  if( (!(id>0)) || (!(idvarianty>=0)) ) return false;
  
  if(idvarianty==0) {
    document.getElementById('order_product_'+id+'_price').value='dle varianty';
    document.getElementById('order_product_'+id+'_variantID').value=-1;
    return 0;
  }

  var id_variant_material = document.getElementById('order_product_'+id+'_variant_material').value;
  
  document.getElementById('order_product_'+id+'_price').value='';
  
  document.getElementById('buyprodabutton').style.display = 'none';
  
  $.post("/_ajax.php?action=order_change_variant_finish", 
    { 'id' : id, 'idvarianty' : idvarianty, 'idvarianty_material' : id_variant_material},
    function(data) {
      order_change_variant_finishResponse(data, id, idvarianty, id_variant_material);
      
    }, "text"
   );   

}

function order_change_variant_finishResponse(data, id, idvarianty, id_variant_material) {
  //alert(data);
  
  var priceel = document.getElementById('order_product_'+id+'_price');
  
  if(data=='false') {
    priceel.value='dle varianty';
    return 0;  
  }
  
  var ret = data.split(';');
  
  priceel.value=addCommas(ret[1])+',-';
  document.getElementById('order_product_'+id+'_variantID').value=ret[0];
  
  document.getElementById('buyprodabutton').style.display = '';
}

function order_change_variant_automaticFirst(id)
{
  $(document).ready(function() { 
    document.getElementById('order_product_'+id+'_variant_material').selectedIndex = 1;
    order_change_variant(id, document.getElementById('order_product_'+id+'_variant_material').options[1].value);
  });                      
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

function AddFavorite(linkObj,addUrl,addTitle)
{
  if (document.all && !window.opera)
  {
    window.external.AddFavorite(addUrl,addTitle);
    return false;
  }
  else if (window.opera && window.print)
  {
    linkObj.title = addTitle;
    return true;
  }
  else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function'))
  {
    if (window.confirm('Přidat oblíbenou stránku jako nový panel?'))
    {
      window.sidebar.addPanel(addTitle,addUrl,'');
      return false;
    }
  }
  window.alert('Po potvrzení stiskněte CTRL-D,\nstránka bude přidána k vašim oblíbeným odkazům.');
  return false;
}

