// JavaScript Document
var old_id = 1;
var new_id = 2;
var banner_animate = true;


$(document).ready(function()
{

	$('.slideDown').slideDown("normal");
	menu_active();
	form_inputs();
	elements_height();
	
	var availableTags = $("#json-tags").text();
		availableTags = $.parseJSON( availableTags );
	
	jQuery( "#conversietool-search" ).autocomplete({
			source: availableTags,
			open: function(event, ui) {
				
				jQuery(".ui-autocomplete").prepend('<li class="first"></li>');
				jQuery(".ui-autocomplete").append('<li class="last"></li>');
			
			}
		});
	
});

function elements_height()
{

	var height = 0;
	jQuery(".button-txt td").each(function ()
	{
	
		if ( jQuery(this).height() > height )
			height = jQuery(this).height();
			
			//alert(height);

	});
	
	jQuery(".button-txt td").height(height+10);
	jQuery(".not-first .button-txt td").height(height+20);

}

function form_inputs()
{

	jQuery('.querySelect').click(function() {
		
			var idk = jQuery(this).parent().attr('id');
			var element = ".f-dropdown-" + idk;
			
			jQuery(element).slideDown("slow");
			//jQuery( "#" + jQuery(this).parent().attr('id') + " .f-box" ).mouseout().slideUp("slow");
			
			jQuery(element) .find("li" ).click(function() {
			
				jQuery( "#" + idk + " input" ).val( jQuery(this).html() );
				jQuery(element).slideUp("slow");
			
			});
	
	});


	jQuery('input').each(function() {
	
		if( jQuery(this).val() != '' && !jQuery(this).hasClass("querySelect") && !jQuery(this).hasClass("submit") )
		{
		
			jQuery(this).blur(function() {
			
				if( jQuery(this).val() == '' || jQuery(this).val() == jQuery(this).attr('rel') )
					jQuery(this).val( jQuery(this).attr('rel') );
			
			});
			
			jQuery(this).focus(function() {
			
				if( jQuery(this).val() == jQuery(this).attr('rel') )
					jQuery(this).val('');
			
			});
		
		}
	
	});
	
}

function close_ie6()
{
	jQuery('#_d').hide('slow');
	jQuery('#_l').hide('slow');
	jQuery('html').css("height",'auto');
	jQuery('html').css("overflow","scroll");
}

function menu_active()
{
	
	var element1 = jQuery("#sm_0").html();
	var element2 = '';
	
	jQuery("#menu a").each(function(){
		
		element2 = jQuery(this).html();
		if( element2 == element1 )
		{
			jQuery(this).addClass("action");
		}
								  
	});
	
}

function changePhoto ( ) {
	if ( banner_animate && new_id != old_id ) {
	animate = false;
		$('#li_'+old_id).removeClass('active');
		$('#li_'+new_id).addClass('active');
	
		$('#image_'+old_id).fadeOut('fast' , function () {
			$('#image_'+new_id).fadeIn('fast');	
			old_id = new_id;
			if ( new_id == 3 ) {
				new_id = 1;
			} else {
				new_id++;
			}
			banner_animate = true;
		});
	}
}

var active = 0;

function news_year( year ) {
	if ( year != active ) {
		$('.all_years').slideUp("normal"); 
		$('#'+year).slideDown("normal");
		active = year;
	}
}

var product_box = -1;

function show_product ( id ) {
	
	
		$('.pr_text').animate({ 
			height: '64'
		});
		
		$('.all_links').html( '&raquo; Meer info' );
	
		if ( product_box != id ) {
		
			var height = $('#sub_box'+id).css('height' );
			$('#pr_box'+id).animate({ 
				height: height
			});
			product_box = id;
			$('#link'+id).html( '&raquo; Minder info' );
		} else {
			product_box = -1;
			
		}
}

function sendPharmacy() {
	var value = $('#queryInput').attr('value');
	/*
	if ( value ) {	
		if ( value.match(/^[0-9]{4}\s*[a-zA-Z]{0,2}$/i) ) {
			document.getElementById('pharmactForm').submit();
		} else {
			alert('We need format: nnnn or nnnn ll');
		};
	} else {
		alert('Please fill field');
	}
	*/
	document.getElementById('pharmactForm').submit();
}

function sendPharmacy2() {
	var value = $('#queryInput').attr('value');
	/*
	if ( value ) {	
		if ( value.match(/^[0-9]{4}\s*[a-zA-Z]{0,2}$/i) ) {
			document.getElementById('pharmactForm').submit();
		} else {
			alert('We need format: nnnn or nnnn ll');
		};
	} else {
		alert('Please fill field');
	}
	*/
	document.getElementById('pharmactForm2').submit();
}

function opensublevel ( id ) {
	$("#ul_"+id).slideToggle("normal");
	
	var class_name = $("#a_"+id).attr('class');

	if (class_name == 'open2') {
		$("#a_"+id).attr('class', 'open');
	}
	else {
		$("#a_"+id).attr('class', 'open2');
	}

}

function compare( str , search )
{

	var str = "Visit W3Schools";
	var patt1 = /w3schools/i;
	document.write( str.match( patt1 ) );

}

function check_form( idk )
{
 	
	var and_what = 0;
 
	jQuery("#" + idk + " .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).val() == jQuery(this).attr("rel") )
		{
			jQuery(this).addClass("error");
			and_what = 1;
		}
		else
		{
			jQuery(this).removeClass("error");
		}
 
	});
 	//alert(and_what);
	if(and_what == 1)
	{
		alert("Vul de verplichte velden.");
		return false;
	}
	else
	{
	
		jQuery("#" + idk + " input").each(function () {
		
			if( jQuery(this).val() == jQuery(this).attr("rel") )
				jQuery(this).val('');
				
		
		});
		
		jQuery( "#"+idk ).submit();
		//return true;
	}
 
}

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



