// JavaScript Document


function postwith (to,p) {
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = to ;
  for (var k in p) {
    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", k) ;
    myInput.setAttribute("value", p[k]);
    myForm.appendChild(myInput) ;
  }
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;
}

function anketaHlasuj(id, idprogreswithoutid, maxwidth) {

  $.post("/anketa.php?action=hlasuj&id="+id, 
    { 'id': id },
    function(data) {

      anketaHlasujResponse(data, id, idprogreswithoutid, maxwidth);
      
    }, "text"
   );  

}

function anketaHlasujResponse(data, id, idprogreswithoutid, maxwidth) {
  //alert(data);
  
  if(data=='false') return false;
  
  var ankmsg = document.getElementById('anketa_msg');
  
  if(data=='hlasovano') {
    ankmsg.innerHTML = 'Již jste hlasovali.';
    ankmsg.style.display = 'block';
    return true;
  }
  
  var splitted = data.split('|');
  var spl2;
  var progid;
  var w;
  
  for(var i=0; i<splitted.length; i++)
  {
    spl2 = splitted[i].split(";");
    
    progid = idprogreswithoutid+spl2[0];
    //alert(progid);
    w = (spl2[1] / 100) * 155;
    //alert(w);
    document.getElementById(progid).style.width = w+'px'; 
    document.getElementById('odpoved_proc_'+spl2[0]).innerHTML = spl2[1]+'%';
    
  }

    ankmsg.innerHTML = 'Váš hlas byl zaznamenán.';
    ankmsg.style.display = 'block';
    return true;  

}

function basketChangeCount(id, flag) {
  
  var cel = document.getElementById(id);
  
  if(!isStrNumber(cel.value.toString())) { 
    alert('Neplatná hodnota.');
    cel.value = 1;
    return false;
  } 
  
  if(flag=='+') {
    var v = parseInt(cel.value);
    cel.value = v+1; 
    //cel.value = cel.value++;
  } 
  else if(flag=='-' && cel.value>1)
  {
    cel.value = cel.value-1;
  }
    
  if(cel.value<=0) cel.value=1;
  
}

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';
	}
}
