$(document).ready(function(){
	
	/*Light box special made for softub.com*/
	$(".light_box").Box();
	
	/*Dropdown languages menu-softub*/
	$(function(){
		$('#navigation_horiz').naviDropDown({
			dropDownWidth: '245px'
		});	
	});
	
	//mainly used on admin panel
	$("#selector-change").change(function(){
		var id = $(this).val();
		var content = $(this).attr("name");
		window.location = $.fn.getLocation()+'admin/'+content+'/'+id;
	});

	/*Control fields*/
	
	$("#check-fields").submit(function(){
		alert("s");
		var error = false;
		$.each($(".input-text"),function(i,v){
			if($(this).attr("required")!="required"){return false;}
			if($(this).val().length==0){
				$(this).css("background","#ff9191");
				error = true;
			}else{
				$(this).css("background","#fff");
			}
			//email
			
		})
		
		//if(isValidEmailAddress()){}
		if(error){
			//ALERT IF FIELDS ARE NOT FILLED
			//$("#alert_fields").css({"display":"inline-block","color":"#ff3c3c","font-weight":"bold"});
			return false;
		}
	});

});



/*SHOP*/
$(document).ready(function(){
	
	function isValidEmailAddress(emailAddress) {
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			return pattern.test(emailAddress);
	}
	
	//checking if the attribute has an extra price
	$(".shop-attributes").change(function(){
		var avid = $(this).attr("id").split("_")[1];
		var pid = $("#pid").val();
		var quantity =  $("#shop-quantity").val();
		var aviid = $(this).val();
		$.ajax({
			cache: false,
			url: $.fn.getLocation({main_url:true})+'shop.php?attribute_price=true&aviid='+aviid+'&pid='+pid+'&quantity='+quantity,
			success: function(data) {
				$("#price").html(data);
			}
		});
		
	});
	
	$(".shop-add-product").click(function(){					  
		$("#shop-message").fadeIn();
		//calling php function
		var pid = $("#pid").val();
		var quantity =  $("#shop-quantity").val();
		var attribute = "";
		$.each($(".shop-attributes"),function(i, val){
			attribute = attribute + '$$' + $(this).val();
		});
		//alert($.fn.getLocation({main_url:true})+'shop.php?add_to_cart=true&pid='+pid+'&quantity='+quantity+'&attribute='+attribute);
		$.ajax({
			cache: false,
			url: $.fn.getLocation({main_url:true})+'shop.php?add_to_cart=true&pid='+pid+'&quantity='+quantity+'&attribute='+attribute,
			success: function(data) {
				
				$("#shop-cart-button").html($("#shop-cart-button").attr("title") + ' '+data);
			}
		});		
		return false;
	});
	
	$(".shop-cart-plus").click(function(){
		var pid = $(this).attr("id").split("_")[1];
		var cart_value = parseInt( ($('input[name="temp_update_pid['+pid+']"]').val()).replace(/[^0-9]/g, '') );
		if(cart_value<=50){
			$('input[name="temp_update_pid['+pid+']"]').val((cart_value + 1) + 'x');
		}
		return false;
	});
	$(".shop-cart-minus").click(function(){
		var pid = $(this).attr("id").split("_")[1];
		var cart_value = parseInt( ($('input[name="temp_update_pid['+pid+']"]').val()).replace(/[^0-9]/g, '') );
		if(cart_value>=1){
			$('input[name="temp_update_pid['+pid+']"]').val((cart_value - 1) + 'x');
		}
		return false;
	});
	

	if($("#shop-more-products").attr("id")!=undefined){
	$("#shop-more-products").ready(function(){
		var pid = $("#shop-more-products").attr('softub-attr');
		window.setTimeout(function(){
		$.post($.fn.getLocation({main_url:true})+'callbacks.php?get_more_products=true', {actual_pid:pid}, function(data){
			var data = $.parseJSON(data);
			$("#shop-more-products").html(data.body).hide().fadeIn();
		});
		},2000);
	});
	}
	
	
	$("#shop-form-submit").click(function(){
		var error,error_mail;
		var nr_fields = $(".box").length;
		var nr_check_fields = 0;
		$.each($(".box"),function(i,v){
			if($(this).val().length==0){
				$(this).css("background","#ff9191");
				error = true;
			}else{
				$(this).css("background","#fff");
				nr_check_fields = nr_check_fields + 1;
			}
			//email
			if($(this).attr("name")=="email"){
				if(!isValidEmailAddress($(this).val())){
					$(this).css("background","#ff9191");
					nr_check_fields = nr_check_fields - 1;
				}else{
					$(this).css("background","#fff");
				}
			}
		});
		
		if(nr_fields != nr_check_fields){return false;}
		//$(document).find("form").submit();
		
	});
	
	$("#check-form").submit(function(){
		var error,error_mail;
		var nr_fields = $(".box").length;
		var nr_check_fields = 0;
		$.each($(".box"),function(i,v){
			if($(this).val().length==0){
				$(this).css("background","#ff9191");
				error = true;
			}else{
				$(this).css("background","#fff");
				nr_check_fields = nr_check_fields + 1;
			}
			//email
			if($(this).attr("name")=="email"){
				if(!isValidEmailAddress($(this).val())){
					$(this).css("background","#ff9191");
					nr_check_fields = nr_check_fields - 1;
				}else{
					$(this).css("background","#fff");
				}
			}
		});
		
		if(nr_fields != nr_check_fields){return false;}
	});
	
	$("#shop-form-create-order").submit(function(){
		var error = false;
		$.each($(".box"),function(i,v){
			if($(this).val().length==0){
				$(this).css("background","#ff9191");
				error = true;
			}else{
				$(this).css("background","#fff");
			}
			//email
			if($(this).attr("name")=="delivery_email" || $(this).attr("name")=="invoice_email"){
				if(!isValidEmailAddress($(this).val())){
					$(this).css("background","#ff9191");
					error = true;
				}else{
					$(this).css("background","#fff");
				}
			}
		})
		//GENERAL TERM CONDITIONS
		if(!$("#tc").attr('checked')){
			$("#label_tc").css({"color":"#ff3c3c","font-weight":"bold"});
			error = true;
		}
		
		//if(isValidEmailAddress()){}
		if(error){
			//ALERT IF FIELDS ARE NOT FILLED
			$("#alert_fields").css({"display":"inline-block","color":"#ff3c3c","font-weight":"bold"});
			return false;
		}
	});

	$("#shop-product-search").autocomplete($.fn.getLocation({main_url:true})+'/shop.php?autocomplete=true',{
		selectFirst: false,
		highlight: false,
		scrollHeight: "auto",
		matchContains: true,
		formatItem: function(row, i, max, term) {
			return term.split("$$")[1];
		},
		formatResult: function(data, value){
			return value.split("$$")[0];
		}
		}).result(function(event, i) {
			var val = String(i).split("$$")[2];
			window.location = val;
			//alert(item);
			
	}).click(function(){
		$(this).val("");
	});

	/*add product change container language*/
	$(".shop_change_language").click(function(){
		$(this).index();
		$(".container_zoom").hide();
		$(".container_zoom:eq("+$(this).index()+")").show();
		return false;
	});
	
	$("#add-more-attributes").click(function(){
		var row = '<tr><td><input type="text" class="input-text" name="attributes_values_information[]" /></td><td><input type="text" class="input-text" name="attributes_values_information_extra_price[]" size="4" /></td></tr>';
		$("#container-attributes").append(row);
		return false;
	});
	
	$(".world-selection").click(function(){
		var continent = $(this).html();
		$("#container_index").fadeOut(function(){
			
			$.post($.fn.getLocation({main_url:true})+'/global-selection.php?get_continents_countries=true',{'continent':continent}, function(data){
				$("#container_continents").fadeIn();
				$("#continent_name").html(continent);
				$("#continents_content").html(data);
			})
		});
		
		return false;
	});
	
	
	$("#delete").click(function(){
		return confirm('Are you sure?');
	});
	$(".delete").click(function(){
		return confirm('Are you sure?');
	});
	
	$("#create_permlink").focusout(function(){
		var permalink = $(this).val();
		$.post($.fn.getLocation({main_url:true})+'/callbacks.php?permalink=true', {'permalink':permalink}, function(data){
			$("#set_permalink").val(data);
		});
		
	});
	
	$(".print-tc").click(function(){
		tc = window.open($.fn.getLocation({main_url:true})+"callbacks.php?terms_and_conditions=true", "tc-print", "width=600,height=400,status=yes,scrollbars=yes,resizable=yes");
		tc.focus();
		tc.setTimeout(function(){tc.print();},2000);
		
	});
	
	$("#softub-models-video-trigger").click(function(){
		
		if($("#softub-models-video").css("display")=="none"){
			$("#softub-models-video").fadeIn();
			$("#softub-models-video-trigger").val("Hide Video");
		}else{
			$("#softub-models-video").fadeOut();
			$("#softub-models-video-trigger").val("Show Video");
		}
		
	});
	
	window.setTimeout(function(){
		$("#banner-global-selection").animate({marginTop:"0px"});
		$.post($.fn.getLocation({main_url:true})+'/callbacks.php?looking_distributors=true', {}, function(data){
			//alert(data);
			$("#banner-global-selection").html('<br><span style="font-size:16px; font-weight:bold; padding-left:15px">Interested in our product? Click under contact on the navigation.</span>');
				
		});
	},2000);
	
	//****************************************//
	//QR CODE FOR SOFTUB REGISTRATION
	//****************************************//
	$(".qr-code-generator").change(function(){
		var motor_number = $("#motor_number").val();
		var cfn = $("#customer_first_name").val();
		var cln = $("#customer_last_name").val();
		var purchase_date = $("#purchase_day").val()+'-'+$("#purchase_month").val()+'-'+$("#purchase_year").val();
		var installation_date = $("#installation_day").val()+'-'+$("#installation_month").val()+'-'+$("#installation_year").val();
		var address = $("#address").val();
		var city = $("#city").val();
		var zip_code = $("#zip_code").val();
		var country = $("#country").val();
		var email = $("#email").val();
		var telefone = $("#telefone").val();
		var tub_color = $("#tub_color").val();
		var installation_place = $("#installation_place").val();
		var surround = $("#surround").val();
		var repair_date = $("#motor_repair_day").val()+'-'+$("#motor_repair_month").val()+'-'+$("#motor_repair_year").val();
		var comments = $("#comments").val();
		
		var params = motor_number + '_' + purchase_date + '_' + installation_date + '_' + cfn + '_' + cln + '_' + address + '_' + city + '_' + zip_code + '_' + country + '_' + 'email' + '_' + telefone + '_' + tub_color + '_' + installation_place + '_' + surround + '_' + repair_date + '_' + comments;
		$("#qr-code").attr("src","http://api.qrserver.com/v1/create-qr-code/?data=http%3A%2F%2Fwww.softub.ch%2Fch%2Ftemplate-motor-registration%2F"+encodeURI(params));
	});
	
});


