// JavaScript Document

function showElement(id) {
  try {
    document.getElementById(id).style.display='block';
  }
  catch (err) {
    ;
  }
}

function hideElement(id) {
  try {
    document.getElementById(id).style.display='none';
  }
  catch (err) {
    ;
  }
}

function showhideElement(idshow, idhide) {
  if(document.getElementById(idshow) && document.getElementById(idhide)) {
    document.getElementById(idhide).style.display='none';
    document.getElementById(idshow).style.display='block';
  }

}

function isChecked(id) {
  if(document.getElementById(id).checked=='true') {
    return true;
  }
  return false;
}

function dimensionOwnOnblurHandle() {
  //alert('aaa');
  if(isChecked('dimension_own')) {
    showhideElement('dimension_own_label', 'dimension_own_input');
    alert("showhideElement('dimension_own_label', 'dimension_own_input');");
  }
}
function dimensionOwnOnblur() {

  //setTimeout("dimensionOwnOnblurHandle()", 200);
  if(!isChecked('dimension_own')) {
    showhideElement('dimension_own_label', 'dimension_own_input');
    //alert("showhideElement('dimension_own_label', 'dimension_own_input');");
  }
}

function evalDimensionOwn2(val, cenazam2, idprice, idowndiminput_radio, owndiminput_str) 
{
  var priceelwiev = document.getElementById(idprice);
  var elowndiminput_radio = document.getElementById(idowndiminput_radio);
  var elowndiminput_str = document.getElementById(owndiminput_str);
  var nonumflag = false;
  var elsirka = document.getElementById('owndiminput_width');
  var elvyska = document.getElementById('owndiminput_height');
  
  var splitted = new Array(elsirka.value, elvyska.value);

  for(var i=0; i<splitted.length;i++) {
    if(!isStrNumber(splitted[i])) var nonumflag = true;
  } 
  
  if(nonumflag) {
    priceelwiev.innerHTML = '-';
    elowndiminput_radio.value = 0;
    elowndiminput_str.value='-null-';
    return false;
  }
  
  var mezivyp = parseInt(splitted[0])*parseInt(splitted[1]);
  
  var cena = Math.round(cenazam2 * (mezivyp / 10000));  
  
  priceelwiev.innerHTML = cena+' Kč';
  elowndiminput_radio.value = cena; 
  elowndiminput_str.value = splitted[0]+'x'+splitted[1];  
}

function evalDimensionOwn(strvalue, cenazam2, idprice) {
  var priceelwiev = document.getElementById(idprice);
  var nonumflag = false;
  var splitted = strvalue.replace(/[/s]/,'').toString().split('x');
  
  if(splitted.length!=3) return false;
  
  //alert(splitted[0]+', '+splitted[1]+', '+splitted[2]);
  for(var i=0; i<splitted.length;i++) {
    if(!isStrNumber(splitted[i])) var nonumflag = true;
  }
  
  if(nonumflag) return false;
  
  var mezivyp = parseInt(splitted[0])*parseInt(splitted[1])*parseInt(splitted[2]);
  
  var cena = cenazam2 * (mezivyp / 1000);  
  
  priceelwiev.innerHTML = cena+' Kč';
}

function isOrderValid_BeforeSubmit(idprod) {
  var elform = document.getElementById('OrderForm'+idprod);
  var elcount = document.getElementById('ordered_count'+idprod);

  if(!isStrNumber(elcount.value)) {
    alert('Zadejte správný počet (Celé číslo vetší než 0).');
    return false;
  }
  
  var radios = elform.getElementsByTagName('input');
  var active_radio = false;
  var v_checked = false;
  var radioiterator = 0;
  for(var i=0; i<radios.length; i++)
  {
    if(radios[i].type=="radio" && radios[i].name=="order_varianats_radio")
    { 
      radioiterator++;
      if(radios[i].checked || radios[i].checked=='true') {
        v_checked = true;
        active_radio = radios[i];
        break;
      }
    }
  }
  
  if(radioiterator && !v_checked) {
    alert('Nejprve musíte zvolit variantu.');
    return false;
  }
  else if(active_radio && active_radio.value=='ownadd') {
    if(document.getElementById('owndiminput_radio').value>0
      && document.getElementById('owndiminput_rozmerstr').value!='-null-') ;
    else {
      alert('Zadejte sprvné hodnoty u vlastní varianty.');
      return false;
    }
  }
  
  return true;
}

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 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 BasketUpdateHTML(response) {
	var html = response.toString().split(";");
	if(html.length > 1) {
		document.getElementById('NAKUPNI_KOSIK_ITEM_COUNT').innerHTML = html[0];
		document.getElementById('NAKUPNI_KOSIK_ITEM_ALLPRICE').innerHTML = html[1];
				
		createBastketConfirm();				
	}
}
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';
    return 0;
  }

  $.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 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 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;
}
