/*   TO JEST DO OBSŁUGI KOSZYKA NA AJAX  */
//var host = 'http://www.euromega.eu/';
function addToCart(cart_idp) {
  var qty = 0;
  if(document.getElementById('cart_qty_'+cart_idp)) {
    qty = document.getElementById('cart_qty_'+cart_idp).value;
  }

  advAJAX.get({

     url: host+"/ajax_response.php?mod=shopping_cart&act=ajaxAdd&add_to_cart=1&cart_idp="+cart_idp+"&qty="+qty,
     /* url: host+"/ajax_response.php?mod=shopping_cart&act=ajaxAdd&add_to_cart=1",

      parameters : {
      "cart_idp" : cart_idp
      },*/

      onSuccess : function(obj) {

         $.blockUI({
           message: 'Dodano produkt do koszyka',
           css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: '.9',
            color: '#A9DD3B'
        } });

        setTimeout($.unblockUI, 500);

        if(document.getElementById('contentbox')) {
          document.getElementById('contentbox').innerHTML =obj.responseText;
        }
      }
  });
}
function delFromCart(rm_cart_idp) {

  advAJAX.get({

      url: host+"/ajax_response.php?mod=shopping_cart&act=ajaxDeleteBox&rm_cart=1&rm_cart_idp="+rm_cart_idp,

      onSuccess : function(obj) {


         $.blockUI({
           message: 'Usunięto produkt z koszyka',
           css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: '.9',
            color: '#A9DD3B'
        } });

        setTimeout($.unblockUI, 500);


        if(document.getElementById('contentbox')) {
          document.getElementById('contentbox').innerHTML =obj.responseText.toString();
        }
      }
  });
}

function delFromShoppingCart(rm_cart_idp) {

  advAJAX.get({

      url: host+"/ajax_response.php?mod=shopping_cart&act=ajaxDelete&rm_cart=1&rm_cart_idp="+rm_cart_idp,

      onSuccess : function(obj) {


         $.blockUI({
           message: 'Usunięto produkt z koszyka',
           css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: '.9',
            color: '#A9DD3B'
        } });

        setTimeout($.unblockUI, 500);


        if(document.getElementById('shoppingCartDetailed')) {
          document.getElementById('shoppingCartDetailed').innerHTML =obj.responseText.toString();
        }
      }
  });
}

function qtyInc(id) {
  if(document.getElementById(id)) {
    document.getElementById(id).value = parseInt(document.getElementById(id).value)+1;
  }
}

function qtyDec(id) {
  if(document.getElementById(id)) {
    var result = parseInt(document.getElementById(id).value)-1;
    if(result <= 0) {
      result = 0;
    }
    document.getElementById(id).value = parseInt(result);
  }
}

function  switchEventFunc(id,idp,state) {
  if(state==false) {
    delFromCart(idp);
  } else {
    addToCart(idp);
  }
}

//summ_gross_basket_box
function setSummaryPrice() {
    // musimy ustalic cene

  if(document.getElementById('accessory_summary_gross') && document.getElementById('summ_gross_basket_box')) {
 document.getElementById('accessory_summary_gross').innerHTML =  document.getElementById('summ_gross_basket_box').innerHTML;
  }
}

//function setSummaryPrice() {
//    // musimy ustalic cene
//  var value = getCookie('accessory_summary_price');
//  if(document.getElementById('accessory_summary_gross')) {
//    document.getElementById('accessory_summary_gross').innerHTML=value.toString();
//  }
//}

 function getCookie( name ) {

    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
    return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ';', len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}
//
//function showHide(c_name) {
//
//      $("."+c_name+':hidden').show(1000);
//
//      $("."+c_name+':visible').hide(300);
//
//}

function showHide(c_name) {
	 if ($("."+c_name+':hidden').is(":hidden")) {
      $("."+c_name+':hidden').show(500);
	 } else {
      $("."+c_name+':visible').hide(300);
	 }
}


//Twoja cena
function yourPrice(idp) {
	var valid_mail=true;
	var valid_price=true;
	$("#client_price").val( $("#client_price").val().replace(',','.') );
	if(false == /^[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[A-Za-z0-9._%-]{2,4}$/.test( $("#client_price_mail").val() )){
		$("#client_price_mail").addClass("invalid");
		valid_mail=false;
	}else{
		$("#client_price_mail").removeClass("invalid");
		valid_mail=true;
	}	
	
	if(false == /^[0-9]+[\.]?[0-9]+$/.test( $("#client_price").val() )){
		$("#client_price").addClass("invalid");
		valid_price=false;
	}else{
		$("#client_price").removeClass("invalid");
		valid_price=true;
	}
	
	if(valid_mail && valid_price && idp>0){
		addYourPrice( $("#client_price").val(), idp, $("#client_price_mail").val() )
	}
	
}
function addYourPrice(price, id_product, mail) {
	advAJAX.get({
     url: host+"ajax_response.php?mod=availability&act=addPrice&price="+price+"&id_product="+id_product+"&mail="+mail,
	 
	   onInitialization : function(obj) {
		$.blockUI({
           message: 'Wysyłanie...',
           css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: '.9',
			timeout: 0,
            color: 'red'
        } });		
		},

      onSuccess : function(obj) {
        /* $.blockUI({
           message: 'Wysłano propozycję ceny',
           css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: '.9',
            color: '#A9DD3B'
        } });*/
		$(".blockMsg").html('Wysłano propozycję ceny');
		$(".blockMsg").css({color: '#A9DD3B'});

        setTimeout($.unblockUI, 1000);

      }
  });
	return false;
}

function yourOpinion(idp) {
	var valid_name=true;
	var valid_content=true;
	
	if(!$("#opinion_name").val() && $("#opinion_name").val() == ''){
		$("#opinion_name").addClass("invalid");
		valid_name=false;
	}else{
		$("#opinion_name").removeClass("invalid");
		valid_name=true;
	}	
	//alert($("#opinion_content").html());
	if( !$("#opinion_content").val() || $("#opinion_content").val() == ''){
		$("#opinion_content").addClass("invalid");
		valid_content=false;
	}else{
		$("#opinion_content").removeClass("invalid");
		valid_content=true;
	}
	
	if(valid_name && valid_content && idp>0){
		advAJAX.post({
    		url: host+'ajax_response.php?mod=product&act=ajax_captcha',
	 		parameters : {
      			uid : $("#uid_js").val(),
      			cid : $("#cid_js").val()
     		},
	 		onSuccess : function(obj) {
				if(eval(obj.responseText) ){
					$("#uid_js").removeClass("invalid");
					addYourOpinion( $("#opinion_name").val(), idp, $("#opinion_content").val() );
				}else{
					$("#uid_js").addClass("invalid");
					$("#uid_js").select();
				}
      		}
  		});		
	}else{
		advAJAX.post({
    		url: host+'ajax_response.php?mod=product&act=ajax_captcha',
	 		parameters : {
      			uid : $("#uid_js").val(),
      			cid : $("#cid_js").val()
     		},
	 		onSuccess : function(obj) {
				if(eval(obj.responseText) ){
					$("#uid_js").removeClass("invalid");
				}else{
					$("#uid_js").addClass("invalid");
				}
      		}
  		});
	}
	
}
function addYourOpinion(name, id_product, comment) {
	advAJAX.post({
     url: host+"ajax_response.php?mod=product_opinion&act=addOpinion",
	 
	  parameters : {
      name : name,
      id_product : id_product,
	  comment : comment
     },
	 
	 
	   onInitialization : function(obj) {
		$.blockUI({
           message: 'Wysyłanie...',
           css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: '.9',
			timeout: 0,
            color: 'red'
        } });		
		},

      onSuccess : function(obj) {
		$("#add_opinion_h3").html('Opinia została dodana');
		$("#opinion_form").html('');
				
		var today=new Date();
		month = today.getMonth() +1 ;
		if(month <10) month='0'+month;
		day = today.getDate();
		if(day <10) day='0'+day;	
		year = today.getYear();
		
		$("#new_opinion_table").html('<table width="100%" border="0" class="opnia" style="margin:0 0 5px 0; border-bottom:1px dashed #e0e0e0; padding:3px 0;"><tr><td style="font-weight:bold; color:#848484;">autor: <span style="color:#585858;">'+name+'</span></td><td style="text-align:right; font-weight:bold; color:#848484;">data: <span style="color:#585858;">'+day+'.'+month+'.'+year+'</span></td></tr><tr><td colspan="2" style="text-align:justify;">'+comment+'</td></tr></table>');
		
		$("#new_opinion_table").show();
		$(".blockMsg").html('Opinia została dodana');
		$(".blockMsg").css({color: '#A9DD3B'});
        setTimeout($.unblockUI, 1000);
      }
  });
	return false;
}


//dostepnosc produktu
function productAvailability(idp) {
	var valid_mail=true;
	
	if(false == /^[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[A-Za-z0-9._%-]{2,4}$/.test( $("#client_availability_mail").val())){
		$("#client_availability_mail").addClass("invalid");
		valid_mail=false;
	}else{
		$("#client_availability_mail").removeClass("invalid");
		valid_mail=true;
	}	
	
	if(valid_mail && idp>0){
		addProductAvailability( idp, $("#client_availability_mail").val() )
	}	
}

function addProductAvailability(id_product, mail) {
	advAJAX.get({
     url: host+"ajax_response.php?mod=availability&act=addAvailability&id_product="+id_product+"&mail="+mail,	 
	 
	    onInitialization : function(obj) {
		$.blockUI({
           message: 'Wysyłanie...',
           css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: '.9',
			timeout: 0,
            color: 'red'
        } });		
		},

      onSuccess : function(obj) {
         /*$.blockUI({
           message: 'Wysłano zgłoszenie',
           css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: '.9',
			fadeIn:  0,
            color: '#A9DD3B'
        } });*/		 
		$(".blockMsg").html('Wysłano zgłoszenie');
		$(".blockMsg").css({color: '#A9DD3B'});
        setTimeout($.unblockUI, 1000);
      }
  });
	return false;
}


function remember_password(){
	if(false == /^[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[A-Za-z0-9._%-]{2,4}$/.test( $("#remember_email").val() )){
		$("#remember_email").addClass("invalid");
		return false;
	}else{
		$("#remember_email").removeClass("invalid");
		document.forms.rejestracja.submit();
		return true;
	}
}


function reset_search( ) {
  
	advAJAX.get({
     url: host+'ajax_response.php?mod=product&act=ajax_reset_search'
  });
  
 /*  alert(href_val);
   document.location.href=href_val;*/

}