/*
 * jQuery extend for www.dvb-c-shop.cz
 *
 * Copyright (c) 2008 Lukas Svoboda (lotofidea s.r.o.)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://www.lotofidea.com
 *
 */
isInteger = function(s){
    return !isNaN(parseInt(s));
}
/**
 * Funkce pro aktualizaci kosiku
 */

$(document).ready(function(){
    $('.buy').click(function(){
        var pocet;
        id_produkt = $(this).attr('href');
        id_produkt = id_produkt.replace(/#/, '');
        // pridani do kosiku
        $.get("/inc/eshop.inc.php?add=" + id_produkt, function(data){ 
         	window.location.replace('/objednavka.htm');
        });  
    });
    $('.remove').click(function(){
        var idproduct = $(this).parent().children(".product").attr('value');
        var radek   = $(this).parents('tr');
        $.get("/inc/eshop.inc.php?remove=" + idproduct, function(data){
            if (data == true) {
                $.get("/inc/eshop.inc.php?show=1&nolook=yes", function(data){
                });
                radek.fadeOut('fast');
                //window.location.reload();
                //window.location.href='/objednavka.htm';
                window.location.replace('/objednavka.htm');
            }
        });
    });
    $('.count').keyup(function(){
        var id_product = $(this).parents('tr').find('.product').attr('value');
        var value = $(this).attr('value');
        if (isInteger(value) && (value > 0)) {
            $.get("/inc/eshop.inc.php?setcount=" + id_product + '&count=' + value, function(data){
                 window.location.replace('/objednavka.htm');
            });
        }else {
             $(this).val("1");
			 var id_product = $(this).parents('tr').find('.product').attr('value');
             var value = $(this).attr('value');
			 $.get("/inc/eshop.inc.php?setcount=" + id_product + '&count=' + value, function(data){
                if (data == true) {
                    //$.get("/inc/eshop.inc.php?show=1&nolook=yes", function(data){});
                    window.location.replace('/objednavka.htm'); 
                }
            });
        }
    });
    
    $(function(){
        //$('#ProductBox > ul').tabs({ fx: { height: 'toggle', opacity: 'toggle' } });
        $('#ProductBox > ul').tabs();

    });

    $('.LoginTypeLogin').click(function(){
            if ($('.login').css("display") == 'none') {
				$("#c_registration").attr("disabled","");
				$("#c_registration").val("PŘIHLÁSIT");
                $(".helpBox").hide('fast'); 
                $('.login').fadeIn('fast');
            }
            $('.registration').fadeOut('fast');
    });
    $('.LoginTypeReg').click(function(){
            if ($('.registration').css("display") == 'none') {
				$("#c_registration").val("REGISTROVAT");
                $('.registration').fadeIn('fast');
            }else{
				$("#c_step2").val("REGISTROVAT");
           }
            /**
            if($('input[@name=agree]').attr('checked',false)){
                $('input[@name=c_step2a]').attr("disabled","disabled");
            }
            */
            $('.login').fadeOut('fast');
    });
 

    $("#reg_logname").change(function(){
        $.get("./inc/registrace.check.inc.php?value=" + this.value, function(data){
            if (data == true) {
                $("#send").attr("disabled", "disabled");
            }
            else {
                $("#send").attr("disabled", "");
            }
        });
    });
    
	$("input[@name=agree]").click(function(){
		if($('input[@name=agree1]').attr('checked')){
            if($('input[@name=agree]').attr('checked')){
			    //$("#send > .button").attr("disabled","");
			    $('input[@name=c_step2a]').attr("disabled","");
                $('#c_registration]').attr("disabled","");
                $('#c_save').attr("disabled",""); 
		    }else{
			    //$("#send > .button").attr("disabled","disabled");
			    $('input[@name=c_step2a]').attr("disabled","disabled");
                $('#c_registration]').attr("disabled","disabled"); 
                $('#c_save').attr("disabled","disabled");         
		    } 
        }
	});	
	$("input[@name=agree1]").click(function(){
        if($('input[@name=agree]').attr('checked')){  
            if($('input[@name=agree1]').attr('checked')){
                //$("#send > .button").attr("disabled","");
                $('input[@name=c_step2a]').attr("disabled","");
                $('#c_registration]').attr("disabled",""); 
                 $('#c_save').attr("disabled","");
            }else{
                //$("#send > .button").attr("disabled","disabled");
                $('input[@name=c_step2a]').attr("disabled","disabled");
                $('#c_registration]').attr("disabled","disabled");
                 $('#c_save').attr("disabled","disabled");         
            } 
        }
    });
    $(function(){
        $('#dodaci').click(function(){
            if ($('.dodaci').css("display") == 'none') {
                $('.dodaci').fadeIn('fast');
            }else{
				$('.dodaci').fadeOut('fast');
			}
        });
        $('#fakturacni').click(function(){
            if ($('.fakturacni').css("display") == 'none') {
				$('.fakturacni').fadeIn('fast');
			}else {
				$('.fakturacni').fadeOut('fast');
			}
        });
    });
    

	 $("#c_registration").click(function(){    
            var name_first 		= $('#name_first');
			var name_second 	= $('#name_second');
			var email 			= $('#email');
            var telefon         = $('#telefon');
			var reglogname 		= $('#reglogname');
			var regpass 		= $('#regpass');
			var regpassre 		= $('#regpassre');
            
            var ulice   =   $("#ulice");
            var cp      =   $("#cp");  
            var mesto   =   $("#mesto");
            var psc     =   $("#psc");
            var co      =   $("#co");
            
            //var sco     =   $("#sco");
            var cs     =   $("#cislo_smlouvy");    
            if(co.val()==1){cs.val('ne');}       
            var agree   =   $("#agree");
            
            if($("#loged").val()=='loged'){
                    $("#OrderForm").submit();
                    return false;
            }else{
                if($("input[@type=radio][@checked]").val()=='registration' || $("input[@type=hidden][@name=c_registration]").val()=="reg"){
		                $('.helpBox').hide();
                        $.ajax({
					            url: "/inc/registrace.check.inc.php",
					            type: 'POST',
					            data: {
						            name_first: $('#name_first').val(),
						            name_second: $('#name_second').val(),
						            email: $('#email').val(),
                                    telefon: $('#telefon').val(),        
						            reglogname: $('#reglogname').val(),
						            regpass: $('#regpass').val(),
						            regpassre: $('#regpassre').val(),
                                    
                                    ulice   :   $("#ulice").val(),
                                    cp      :   $("#cp").val(),
                                    mesto   :   $("#mesto").val(),
                                    psc     :   $("#psc").val(),
                                    co      :   $("#co").val(),
                                    //sco     :   $("#sco").val(),
                                    //cs     :   $("#cislo_smlouvy").val(),
                                    
                                    agree	:	$('#agree:checked').val()  
					            }, 
					            error: function(){alert('error');}, 
					            success: function (txt){   
						            if(txt==""){$('.helpBox').hide(); $("#OrderForm").submit();return false;}else{                                       
							            //$('#OrderForm').find.each($(':input')).removeClass('readyNo');
							            switch(txt){
								            case '1':
										            $('.helpBox > p').html('Hesla se neshodují');
                                                    $('.helpBox').fadeIn(); 
										           
									            break;
								            case '2':
										            $('.helpBox > p').html('Uživatel se stejným emailem je již registrován');
                                                    $('.helpBox').fadeIn();     
										            
									            break;
								            case '3':
										            $('.helpBox > p').html('Email je ve špatném formátu');
                                                    $('.helpBox').fadeIn();       
										           
									            break;
								            case '4':
										            $('.helpBox > p').html('Uživatelské jméno je již použito');
                                                    $('.helpBox').fadeIn();      
										           
									            break;
								            case '5':
										            $('.helpBox > p').html('Nekterá pole jsou prázdná');
                                                    $('.helpBox').fadeIn(); 
                                                    if($('#agree').attr('checked')){agree.removeClass('readyNo');}else{agree.addClass('readyNo');} 
									            break;
							            }
						            }
					            }// end of the success
			            });// end of the .ajax  
                }else{
                    $('.helpBox > p').hide();
                    $("#OrderForm").submit();
                    return false;
                }
            }
     });    
	/**
	 * komentare
	 */
	$(function(){
		$('.CommentsButton').click(function(){
			var prvek = $('#CommentsForm');	
            
			if ($(this).val()=='REAGOVAT'){
				var place = $(this).parent();	
				var title = place.parent().parent().children('h3').html();
				var idComment = place.parent().children('.id_comment').val();
				//alert(title + ' - ' + idComment);
				prvek.children('#commenttext').val("");	
				prvek.children().children('#headlinetext').val('Re: ' + title);
				
			}else{
				
				$('#headlinetext').val("");
				$('#commenttext').val("");
				$('#idcomment').val("0");		
				var place = $(this).parent();	
				
			}
			if(prvek.css("display") == 'none') {
				prvek.children().children('#idcomment').val(idComment);
				place.after(prvek);
				prvek.fadeIn('fast');
            }else{
				prvek.fadeOut('fast');
			}
			
		});
	});
	
	$('#SendComment').click(function(){
			
		var headline	=	$('#headlinetext').val();
		var commenttext	=	$('#commenttext').val();
		var idcomment 	= 	$('#idcomment').val();
		var idprodukt 	= 	$('#idproduct').val();
		var reguser 	= 	$('#iduser').val();
		var product_title   =   $('h1').text(); 
		
		if($("#showemail").attr("checked")==true){
			var showemail 	= 0;
		}else{
			var showemail 	= 1;
		}
		$.post("/inc/comments.inc.php",
		{headline: headline, commenttext:commenttext,produktnazev:product_title,  produkt: idprodukt, id_reguser:reguser,idcomment:idcomment,showemail:showemail},
		   function(data){
		   	var message;
		   	switch(data){
				case 'produkt':
						message = "Není znám produkt, který bude komentován\n";
					break;
				case 'user':
						message = message + "Není znám uživatel, který bude komentován\n";
					break;
				case 'headline':
						message = message + "Není znám nadpis komentáře\n";
					break;
				case 'comment':
						message = message + "Není znám text komentáře\n";
					break;
			}
			 $("#CommentsForm").fadeOut("fast");
			 //$("#CommentsForm").after('<div id="commentMessage"><h1>Úspěch</h1><p>Váš komentář byl přidán.</p></div>');
			 //$("#commentMessage").fadeOut("slow");
			
			 $.post("/inc/comments.inc.php",
			 	{show_comment: 1,produkt: idprodukt,produktnazev:product_title},
			   function(data){
				 
				 window.location.href=window.location.pathname + '#tab-comments';
                 window.location.reload(true);        
                 //window.location.replace('/objednavka.htm'); 
			 });
		   }
		 );
	});

    $('#co').change(function(){
             $.post("/inc/jquery.inc.php",
                    {idcableoperator: $(this).val()},
               function(data){
                $("#sco").html(data);
                $("#sco").focus();
             });
    });
     $('#co2').change(function(){
             $.post("/inc/jquery.inc.php",
                    {idcableoperator: $(this).val()},
               function(data){
                $("#sco2").html(data);
                $("#sco2").focus();
             });
    });
    
             $.post("/inc/jquery.inc.php",
                    {idcableoperator: $('#co').val()},
               function(data){
                $("#sco").html(data);
                $("#sco").focus();
             });

    $(".productItem").click(function(){
    		if($(this).children(".detail").css("display") == 'none') {
				$(this).children(".detail").fadeIn('fast');
            }else{
				$(this).children(".detail").fadeOut('fast');
			}
	});
	

});
