var sitePath = '';

jQuery(document).ready(function () {


	menu();
	
	tooltip();
	
	if( jQuery("#overzicht_cart").is("div") )
	{
		
		jQuery("#left").css("width","313px");
		jQuery(".str").css("width","250px");
		jQuery("#left").css("overflow","hidden");

	}
	
	checkBoxAll();
	
	check_newsletter_form();

// end
});

function updateForm(value){
	
	jQuery("#gender1").removeAttr('disabled');
	jQuery("#gender2").removeAttr('disabled');
	jQuery("#buisnessname").removeAttr('disabled');
	jQuery("#first_name").removeAttr('disabled');
	jQuery("#last_name").removeAttr('disabled');
	jQuery("#telephone").removeAttr('disabled');
	
	if (value != '') {
		
		jQuery.ajax({
			type: 'GET',
			url: sitePath+'/ajax.php',
			data: 'email=' + value,
			success: function(data){
					
					jQuery(".book_category").removeAttr('checked');
					var object = eval("("+data+")");

					if(object.jest == 'tak'){
						 jQuery.each(object.lista, function(i, item){
						 		var idd = "#book_category-" + item;
						 		jQuery(idd).attr('checked', 'checked');
						 });
						 jQuery("#gender1").attr('disabled', 'disabled');
						 jQuery("#gender2").attr('disabled', 'disabled');
						 jQuery("#buisnessname").attr('disabled', 'disabled');
						 jQuery("#first_name").attr('disabled', 'disabled');
						 jQuery("#last_name").attr('disabled', 'disabled');
						 jQuery("#telephone").attr('disabled', 'disabled');
					}
				}
		});
		
	} else {
		
		jQuery(".book_category").removeAttr('checked');
		jQuery("#form_newsletter_mode").attr('value', 'yes');
	}
	
}


function checkBoxAll(){
	
	jQuery("#select_all").click(function(){
		
		
		if(jQuery("#select_all:checked").length == 1){
			
			
			jQuery(".book_category").attr('checked', 'checked');
			
		}
		
	});
	
	jQuery(".book_category").click(function(){
			
		jQuery("#select_all").removeAttr('checked');	
		
	});
	
};


function menu()
{

	jQuery("#menu").superfish({ 
            pathClass:  'current' 
        });
        
	jQuery("#menu li").hover(
      function () {
        jQuery(this).addClass("sfHover");
      }, 
      function () {
        jQuery(this).removeClass("sfHover");
      }
    );

    
	jQuery("#menu li li").hover(
		function () {
			jQuery(this).parents("li:first").removeClass("active");
			jQuery(this).parents("li:first").removeClass("sfHover");
	},
		function () {
			jQuery(this).parents("li:first").addClass("sfHover");
	});
	
}

function tooltip()
{

	if( jQuery.browser.version == '6.0' || jQuery.browser.version == '7.0' )
		var fix = 15;
	else
		var fix = 5;

	var title = '';

	jQuery(".tooltip").hover(
      function () {
        title = jQuery(this).find("span.tip-text").html();
        jQuery(this).removeAttr("title");
        jQuery(this).append($("<span class='tip'>" + title + "</span>"));
        
		var position = jQuery(this).position();
		
		//alert( width + ':' + height);
        
  		$(".tooltip").mousemove(function(e){
			jQuery(".tip").css("left", ( e.pageX - position.left ) + fix );
			jQuery(".tip").css("top", ( e.pageY - position.top ) + fix );
		});

      }, 
      function () {
        jQuery(this).find("span.tip").remove();
      }
    );

}

/* FORM CHECK */

function IsEmailCorrect(email) {

  return email.match(/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,3})$/)!=null;

}

function check_form( idk )
{
	
	var and_what = 0;
	
	jQuery(".check").each(function ( loop ) {
													
			//alert (jQuery(this).attr("type") + ' === ' + jQuery(this).val() );
			
			
			if( jQuery(this).val() == '' || ( jQuery(this).hasClass("email") && !IsEmailCorrect( jQuery(this).val() ) ) )
			{
				jQuery(this).addClass("error");
				and_what = 1;
			}
			else
			{
				jQuery(this).removeClass("error");
			}
			
	});
	
	if(and_what == 1)
	{
		alert("Vul de verplichte velden.");
		return false;
	}
	else
	{
		jQuery( "#"+idk ).submit();
	}
	
return false;
	
}

/* FORMULARZ */

function check_form2(form, text, textarea, checkbox, radio){
	if (text!=''){
		var pola_text=text.split(" ");
		for (var i=0; i<pola_text.length; i++){
			var pole=document.getElementById("pole_"+pola_text[i]);
			if (IsEmpty(pole.value)) {
				alert('Graag alle velden invullen.');
				//Focus(pole);
				return false;
			}
		}
	}
	if (textarea!=''){
		var pola_textarea=textarea.split(" ");
		for (var i=0; i<pola_textarea.length; i++){
			var pole=document.getElementById("pole_"+pola_textarea[i]);
			if (IsEmpty(pole.value)) {
				alert('Graag alle velden invullen.');
				//Focus(pole);
				return false;
			}
		}
	}
	if (checkbox!=''){
		var pola_checkbox=checkbox.split(" ");
		for (var j=0; j<pola_checkbox.length; j++){
			var pole='pole_'+pola_checkbox[j]+'[]';
			var zaznaczony = 0;
			var elements = form.elements;
			for (var i = 0;i<elements.length ;i++ )
				if (elements[i].name == pole)
				{
					var numer=i;
					if (elements[i].checked)
						var zaznaczony = 1;
				}
			if (zaznaczony!=1) {
				alert('Graag alle velden invullen.');
				//Focus(document.getElementById('pole_'+pola_checkbox[j]+"_0"));
				return false;
			}
		}
	}
	if (radio!=''){
		var pola_radio=radio.split(" ");
		for (var j=0; j<pola_radio.length; j++){
			var pole='pole_'+pola_radio[j]+'[]';
			var zaznaczony_r = 0;
			var elements = form.elements;
			for (var i = 0;i<elements.length ;i++ )
				if (elements[i].name == pole)
					if (elements[i].checked)
						var zaznaczony_r = 1;
			if (zaznaczony_r!=1) {
				alert('Graag alle velden invullen.');
				//Focus(document.getElementById('pole_'+pola_radio[j]+"_0"));
				return false;
			}
		}
	}
	jQuery('#ContactForm').submit();
	return true;
}

function IsEmpty(pole) {

  if (pole=='') 

  	return true

	else

	return false

}

function check_search_form( idk )
{

	if( jQuery("#search_word").val() != '' && jQuery("#search_word").val() != 'Typ uw zoekwoord' )
	{
		jQuery( "#"+idk ).submit();
		return true;
	}
	
	var loop = jQuery(".ch_tag:checked").length;
	
	if( loop <= 3 && loop > 0 )
	{
	
		jQuery( "#"+idk ).submit();
		return true;
	
	}
	else if( loop > 3 )
	{
	
		alert('Maximaal 3 tags selecteren');
	
	}
	else
	{
	
		alert('Geef uw zoekterm op of vink minimaal één vakje aan.');
	
	}

}

/* END FORM CHECK */

function add_to_cart( form_id )
{
	
	var book_id = jQuery( "#" + form_id + " .book-id").val();
	var amount = jQuery( "#" + form_id + " .book-amount").val();
	var title = jQuery( "#" + form_id + " .book-title").val();
	var price = jQuery( "#" + form_id + " .book-price").val();
	
	//alert( book_id + ' = ' + amount );
	
	$.get(sitePath+"/ajax/ajax.php5", { func: "add_to_cart", book_id: book_id, amount: amount, title: title, price: price },
	function(data){
		alert("Publicatie is toegevoegd aan het winkelmandje");
		jQuery("#cart-items").html(data);
	});

	
}

function cart( id , amount , price , tab_id )
{
	
	price = price.toString();
	price = price.replace( "," , "." );
	price = price*1;
	var new_price = ( (amount*price)*100)/100;
	new_price = new_price.toString();

	var patt =/\./gi;
	/* var ifTrue = patt.test(new_price); */
	var ifTrue = new_price.match(patt);
	
	if( ifTrue )
	{
		new_price = new_price.replace( "." , "," );
	}
	else
	{
		new_price = new_price + ",00";
	}
	
	
	jQuery( "#" + id + " .value" ).html( new_price );
	
	var input = jQuery( "#cart_item_" + tab_id ).val();
	input = input.split("||");
	jQuery( "#cart_item_" + tab_id ).val( input[0] + "||" + input[1] + "||amount::" + amount + "||price::" + new_price );
	
	$.get(sitePath+"/ajax/ajax.php5", { func: "change_amount", id: tab_id, amount: amount });
	
	cart_sum();
	
}

function cart_sum()
{
	var sum = 0;
	jQuery(".cart-item .value").each(function(){
	
		var val = jQuery(this).html();
		val = val.replace( "," , "." );
		val = val*1;
		sum = sum+val;
	
	});
	
	var patt =/\./gi;
	var vat = (((6/100)*sum)*100)/100;
	vat = roundNumber( vat , 2 );
	sum = roundNumber( sum , 2 );
	//alert( vat + ' / ' + sum);
	var total = parseFloat(vat) + parseFloat(sum);
	//total = Math.round(total*100)/100;
	total = roundNumber( total , 2 );
	
	total = total.toString();
	var ifTrue_1 = total.match(patt);
	if( ifTrue_1 )
	{
		total = total.replace( "." , "," );
		totalSplit = total.split(',');
		if( totalSplit[1].length == 1 )
		{
			total = total + "0";
		}
	}
	else
	{
		total = total + ",00";
	}

	vat = vat.toString();
	var ifTrue_2 = vat.match(patt);
	if( ifTrue_2 )
	{
		vat = vat.replace( "." , "," );
		vatSplit = vat.split(',');
		if( vatSplit[1].length == 1 )
		{
			vat = vat + "0";
		}
	}
	else
	{
		vat = vat + ",00";
	}
	
	sum = sum.toString();
	var ifTrue_3 = sum.match(patt);
	if( ifTrue_3 )
	{
		sum = sum.replace( "." , "," );
		sumSplit = sum.split(',');
		if( sumSplit[1].length == 1 )
		{
			sum = sum + "0";
		}
	}
	else
	{
		sum = sum + ",00";
	}
	
	jQuery("#sum").html(sum);
	jQuery("#input_subtotaal").val(sum);
	jQuery("#vat").html(vat);
	jQuery("#input_btw").val(vat);
	jQuery("#total").html(total);
	jQuery("#input_totaal").val(total);
}

function delete_from_cart( id )
{

	$.get(sitePath+"/ajax/ajax.php5", { func: "delete_from_cart", id: id },
	function(data){
		window.location.reload();
	});

}
function delete_from_cart2( id )
{

	$.get(sitePath+"/ajax/ajax.php5", { func: "delete_from_cart2", id: id },
	function(data){
		jQuery("#cart-items").html(data);
	});

}
function roundNumber(number,decimals) {
	var newString;// The new rounded number
	decimals = Number(decimals);
	if (decimals < 1) {
		newString = (Math.round(number)).toString();
	} else {
		var numString = number.toString();
		if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
			numString += ".";// give it one at the end
		}
		var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
		var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
		var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
		if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
			if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
				while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
					if (d1 != ".") {
						cutoff -= 1;
						d1 = Number(numString.substring(cutoff,cutoff+1));
					} else {
						cutoff -= 1;
					}
				}
			}
			d1 += 1;
		} 
		if (d1 == 10) {
			numString = numString.substring(0, numString.lastIndexOf("."));
			var roundedNum = Number(numString) + 1;
			newString = roundedNum.toString() + '.';
		} else {
			newString = numString.substring(0,cutoff) + d1.toString();
		}
	}
	if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
		newString += ".";
	}
	var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
	for(var i=0;i<decimals-decs;i++) newString += "0";
	//var newNumber = Number(newString);// make it a number if you like
	return newString; // Output the result to the form field (change for your purposes)
}

function check_newsletter_form() {
	
	$("#NewsletterForm").validate({
		rules: {
			email: {
				required: true,
				email: true
			},
			first_name: "required",
			last_name: "required",
			buisnessname: "required",
			"book_category[]": "required"
		},
		messages: {
			email: "Deze velden zijn verplicht",
			first_name: "Deze velden zijn verplicht",
			last_name: "Deze velden zijn verplicht",
			"book_category[]": "Deze velden zijn verplicht"
		}
	});
	
}

