var timeout;

function setWindow() {
    imgx = document.img.width;
    imgy = document.img.height;
    if(document.img.width > 984)  { imgx = 1024; } else { imgx += 40; }
    if(document.img.height > 723) { imgy = 768; }  else { imgy += 55; }
    window.resizeTo(imgx, imgy)
}

function popup(href) {
window.open(href);	
}

function getE(objectID) {
	return document.getElementById(objectID);
}

function right(e) {
if (navigator.appName == 'Netscape' &&
(e.which == 3 || e.which == 2))
return false;
else if (navigator.appName == 'Microsoft Internet Explorer' &&
(event.button == 2 || event.button == 3)) {
alert("Kliketjte prosím pouze levým tlačítkem.");
return false;
}
return true;
}


function showFaktAdr(objChb)
{
	if (getE(objChb).checked)
		getE('tblFakt').style.display = "";
	else
		getE('tblFakt').style.display = "none";
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         alert('Nesprávný formát PSČ, formát musí být číselný!');
         getE('zip').focus();
         IsNumber = false;
         }
      }
   return IsNumber;
}


function EmailControl(f)
{
	if ((f=='' || f=='Váš@email.cz') ||(f.indexOf('@') < 1 || f.indexOf('@') != f.lastIndexOf('@') || f.lastIndexOf('.') < f.lastIndexOf('@')+2  || f.lastIndexOf('.') > (f.length-3) || f.lastIndexOf('.') < (f.length-4)) || f.indexOf(" ") >= 0  ||  f.indexOf("#") >= 0  || f.indexOf("/") >=0 || f.indexOf(":") >=0 || f.indexOf(";") >=0 || f.indexOf("(") >=0 || f.indexOf(")") >=0 || f.indexOf("[") >=0 || f.indexOf("]") >=0 || f.indexOf("'") >=0)
	{
		alert('Nesprávný formát pole: Email!');
		getE('email').focus();
		return false;
	}
	return true;
}


function validateForm(co)
{
	if (co == 1){
		if (getE('doprava').value == 0) {alert("Není vybráno pole: Platba!"); return false;}
		if (getE('platba').value == 0) {alert("Není vybráno pole: Doprava!"); return false;}
	}

	if (co == 2){
		if (getE('name').value == "") {alert("Není vyplněno pole: Jméno a příjmení!"); getE('name').focus(); return false;}
		if (getE('street').value == "") {alert("Není vyplněno pole: Ulice!"); getE('street').focus(); return false;}
		if (getE('city').value == "") {alert("Není vyplněno pole: Město!"); getE('city').focus(); return false;}
		if (getE('zip').value == "") {alert("Není vyplněno pole: PSČ!"); getE('zip').focus(); return false;}
	  if (IsNumeric(getE('zip').value) == false) {return false;}
		if (getE('tel').value == "") {alert("Není vyplněno pole: Telefon!"); getE('tel').focus(); return false;}
		if (getE('tel').value.length < 9) {alert("Nesprávný formát pole: Telefon!"); getE('tel').focus(); return false;}
		if (getE('email').value == "") {alert("Není vyplněno pole: Email!"); getE('email').focus(); return false;}
	  if (EmailControl(getE('email').value) == false) {return false;}
	}
	if (co == 3){
		if (getE('loginName').value == "") {alert("Není vyplněno přihlašovací jméno!"); getE('loginName').focus(); return false;}
		if (getE('loginName').value.length <5) {alert("Přilašovací jméno musí být dlouhé minimálně 5 znaků."); getE('loginName').focus(); return false;}
		if (getE('loginPass').value == "") {alert("Není vyplněno heslo!"); getE('loginPass').focus(); return false;}
    if (getE('loginPass').value.length <5) {alert("Heslo musí být dlouhé minimálně 5 znaků."); getE('loginPass').focus(); return false;}
		if (getE('testPass').value == "") {alert("Není zadáno ověření hesla!"); getE('testPass').focus(); return false;}
    if (getE('testPass').value.length <5) {alert("Heslo musí být dlouhé minimálně 5 znaků."); getE('testPass').focus(); return false;}
    if (getE('loginPass').value != getE('testPass').value) {alert("Zadané hesla se neshodují!"); getE('testPass').focus(); return false;}
    if (getE('name').value == "") {alert("Není vyplněno pole: Jméno a příjmení!"); getE('name').focus(); return false;}
		if (getE('street').value == "") {alert("Není vyplněno pole: Ulice!"); getE('street').focus(); return false;}
		if (getE('city').value == "") {alert("Není vyplněno pole: Město!"); getE('city').focus(); return false;}
		if (getE('zip').value == "") {alert("Není vyplněno pole: PSČ!"); getE('zip').focus(); return false;}
	  if (IsNumeric(getE('zip').value) == false) {return false;}
		if (getE('tel').value == "") {alert("Není vyplněno pole: Telefon!"); getE('tel').focus(); return false;}
		if (getE('tel').value.length < 9) {alert("Nesprávný formát pole: Telefon!"); getE('tel').focus(); return false;}
		if (getE('email').value == "") {alert("Není vyplněno pole: Email!"); getE('email').focus(); return false;}
	  if (EmailControl(getE('email').value) == false) {return false;}
	}
	return true;
}


function showCategory(sub, top)
{
	if (getE(sub).style.display == "none")
	{
		getE(sub).style.display = "block";
		getE(top).className = "open";
	}
	else
	{
		getE(sub).style.display = "none";
		getE(top).className = "close";
	}
}


function openWindow(href,width,height){
  if(window.open(href,"mywindow","width="+width+",height="+height+",menubar=no,resizable=yes,location=no,status=no,scrollbars=yes")){
    return true;
  }
  else{
    return false;
  }
}





$(window).load(function () {
  	shopInit();
});


function shopInit(){

	$("input.length").click( function() {
		$(this).select();
	});
	
	$("input.count").click( function() {
		$(this).select();
	});	
	
	$("input.btnAdd2Cart").click( function() {
		
		$(this).siblings("input.note").val($("#custNote").val());
		
    	var length = $(this).parents("tr").children("td").find("input.length");
		var count = $(this).parents("tr").children("td").find("input.count");
		
		if(length.val() =="0.00" || length.val() =="" || length.val() =="0"){
			length.focus().select();
			return false;
		}
		if(count.val() =="0.00" || count.val() =="" || length.val() =="0"){
			count.focus().select();
			return false;
		}	

	});
	
	
	$("input.btnAdd").click( function() {
		
		$("#cnote").val($("#custNote").val());
		
    	var length = $("#length");
		var count = $("#count");
		
		if(length.val() =="0.00" || length.val() ==""){
			length.focus().select();
			return false;
		}
		if(count.val() =="0.00" || count.val() ==""){
			count.focus().select();
			return false;
		}	

	});
		
	
	$("#rememberNote").click( function() {
	
		if($("#rememberNote").is(":checked")){
			$.ajax({
			  type: "POST",
			  timeout: 500,
			  url: "./scripts/save_session.php",
			  data: "sname=customerNote&sval="+$("#custNote").val(),
			  success: function(msg){
			    //alert( "Data Saved: " + msg );
			  }
			});
		}
		else{
			$.ajax({
			  type: "POST",
			  timeout: 500,
			  url: "./scripts/save_session.php",
			  data: "sname=customerNote&sval=",
			  success: function(msg){
			    //alert( "Data Saved: " + msg );
			  }
			});		
		}
	
	});
	
	$("#custNote").keyup( function() {
			if($("#rememberNote").is(":checked")){	
				$.ajax({
				  type: "POST",
				  url: "./scripts/save_session.php",
				  data: "sname=customerNote&sval="+$(this).val(),
				  timeout:1000,
				  beforeSend: function(xh){},
				  success: function(msg){
					//alert( "Data Saved: " + msg );
				  }
				});
			}
	});
	
	
	$("a.eye").ImageTooltip({ 
    	delay: 0,
    	top: -50
	});

	
	$("input").focus(function(){
      $(this).addClass("focus");
    });
  	$("input").blur(function(){
      $(this).removeClass("focus");
   	}); 	
}


function blink(subj){
	subj.css({"border":"1px solid #777777"});
	subj.animate({"border":"1px solid red"},"slow");
	subj.animate({"border":"1px solid #777777"},"slow");
	subj.animate({"border":"1px solid red"},"slow");
	subj.animate({"border":"1px solid #777777"},"slow");
}


$(document).ready(function(){
	$("#sortForm").submit(function(){
		/*alert('haha');*/
		return false;
	});
});




function openImgWindow(href,width,height){
  if(window.open(href,"mywindow","width="+width+",height="+height+",menubar=no,resizable=no,location=no,status=no,scrollbars=no")){
    return true;
  }
  else{
    return false;
  }
}

