$(document).ready(function() {

	$('.hovernl').hover(
		function() { 		
			$('.hover').each(function(index) {
				$(this).hide();
			});
			$('#onhovernl').show(); 		
		}
	);
	
	$('.hoveren').hover(
		function() { 
			$('.hover').each(function(index) {
				$(this).hide();
			});
			$('#onhoveren').show(); 
		}
	);
	
	$('.hoverfr').hover(
		function() { 
			$('.hover').each(function(index) {
				$(this).hide();
			});
			$('#onhoverfr').show(); 
		}
	);

	
	$(".scrollable").scrollable({circular: true}).autoscroll({interval: 4000});
	
	try {
        $(".prijsaanvraag").fancybox({overlayShow: false, hideOnContentClick:false, width:400, height:525, autoDimensions:false, type:"iframe"});
    
		$.validator.messages.required = "*";
		$.validator.messages.email = "*";
	
	}catch(e){
		// Prevents an JS error (error: Running the script and not waiting if JQuery is ready.)
	}
	
	try {
        $(".loginpop").fancybox({overlayShow: false, hideOnContentClick:false, width:400, height:518, autoDimensions:false, type:"iframe"});
    
		$.validator.messages.required = "*";
		$.validator.messages.email = "*";
	
	}catch(e){
		// Prevents an JS error (error: Running the script and not waiting if JQuery is ready.)
	}
	
	/*
	$('#registratieform').submit(function() {
		$("#registratieform").validate();
		return $("#registratieform").valid();
	});
	*/
	
	$('#registratieform2').submit(function() {
		$("#registratieform2").validate();
		return $("#registratieform2").valid();
	});
	
	/*
	$('#contactform').submit(function() {
		$("#contactform").validate();
		return $("#contactform").valid();
	});
	*/
	
	$('#loginform2').submit(function() {
		$("#loginform2").validate();
		return $("#loginform2").valid();
	});
	
	$('#update').click(function() {
		$('#oper').val("update");
		$('#basketform').submit();
		
	});


	
});


function doFilter(){
	this.document.filterform.submit();
}

function doAddToWishlist(){
	var msg;
	msg = "";
	var error = 0;
	var theForm = this.document.productform;
	
	for (i = 0; i<theForm.elements.length; ++ i) {	
		if (theForm.elements[i].name.substring(0,11)=="basketItems") {
			if(theForm.elements[i].checked) {
				error++;
			}
		}
		error = 0;
	}
	if (error == 0) {
		msg = this.document.productform.jserror1.value;
	}
	msg = "";
	if (msg == ""){
		this.document.productform.oper.value = "addtowishlist";
		this.document.productform.submit();
	} else {
		alert(msg);
	}
}

function doSubmit(){
	
		this.document.productform.oper.value = "addtowishlist";
		this.document.productform.submit();

}

function doAddToBasket(){
	var msg;
	msg = "";
	var x = 0;
	var x2 = 0;
	var theForm = this.document.productform;
	for (i = 0; i<theForm.elements.length; ++ i) {	
		if (theForm.elements[i].name.substring(0,16)=="basketQuantities") {
			if (theForm.elements[i].value != "") {
				if (msg == "" && (isNaN(theForm.elements[i].value) || theForm.elements[i].value == 0)){
					msg = this.document.productform.jserror1.value;
				}
				if (msg == "" && theForm.elements[i].value*1 < this.document.productform.minquantity.value*1){
					msg = this.document.productform.jserror2.value + this.document.productform.minquantity.value + ".";
				}	
			}
			else {
				x++;
			}
			x2++;
		}
	}	
	if (x2 == x) {
		msg = this.document.productform.jserror1.value;
	}
	if (msg == ""){
		this.document.productform.oper.value = "addtobasket";
		this.document.productform.submit();
	} else {
		alert(msg);
	}
}

function doClearSearch(){
	if (this.document.searchform.q_search.value == this.document.searchform.q_search_default.value){
		this.document.searchform.q_search.value = "";
	}
}

function doResetSearch(){
	this.document.searchform.q_search.value = this.document.searchform.q_search_default.value;
}

function doSearch(){
	if (this.document.searchform.q_search.value != this.document.searchform.q_search_default.value && this.document.searchform.q_search.value != ""){
		this.document.searchform.submit();
	}
}

function doRecalculate(){
	var msg;
	msg = "";
	
	for (i = 0; i<document.basketform.elements.length; ++ i) {
		if (document.basketform.elements[i].name.substring(0,9)=="quantity_"){
			if (isNaN(document.basketform.elements[i].value)){
				msg = document.basketform.jserror3.value;
			} else {
				if (document.basketform.elements[i].value < 0){
					msg = document.basketform.jserror3.value;
				}
			}
		}
	}
	if (msg == ""){
		this.document.basketform.oper.value = "recalculatebasket";
		this.document.basketform.submit();
	} else {
		alert(msg);
	}
}

function doChangedQuantity(){
	this.document.basketform.changedquantity.value = "Y";
}

function doConfirmOrder(){
	var msg;
	msg = "";
	
	if (this.document.basketform.changedquantity.value == "Y"){
		alert(document.basketform.jserror4.value);
	} else {
		
		for (i = 0; i<document.basketform.elements.length; ++ i) {
			if (document.basketform.elements[i].name.substring(0,13)=="stockchecker_"){
				if (document.basketform.elements[i].value == "overflow"){
					msg = document.basketform.jserror5.value;
				}
			}
		}
		
		if (msg != ""){
			alert(msg);
		} else {
			var min = parseFloat(this.document.basketform.total.value);
			var max = parseFloat(this.document.basketform.mintotal.value);
			if (min < max){
				alert(document.basketform.jserror6.value + this.document.basketform.mintotal.value + " euro");
			} else {
				this.document.basketform.next_oper.value = "step2";
				this.document.basketform.submit();
			}
		}
	}
}

function doRemove(field){
	for (i = 0; i<document.basketform.elements.length; ++ i) {
		if (document.basketform.elements[i].name == field){
			document.basketform.elements[i].value = "0";
		}
	}
	doRecalculate();
}

function doCheckRegistratie() {
	var msg;
	msg = "";
	
	if (document.registratieform.firstname.value == ""){
		msg += "- " + document.registratieform.jserror2.value + "\n";
	}
	if (document.registratieform.lastname.value == ""){
		msg += "- " + document.registratieform.jserror3.value + "\n";
	}
	if (document.registratieform.address1.value == ""){
		msg += "- " + document.registratieform.jserror7.value + "\n";
	}
	if (document.registratieform.postal.value == "" || document.registratieform.town.value == ""){
		msg += "- " + document.registratieform.jserror8.value + "\n";
	}
	if (document.registratieform.country.value == ""){
		msg += "- " + document.registratieform.jserror9.value + "\n";
	}
	if (document.registratieform.tel.value == ""){
		msg += "- " + document.registratieform.jserror5.value + "\n";
	}
	if (document.registratieform.email.value == ""){
		msg += "- " + document.registratieform.jserror4.value + "\n";
	}
	if (msg == ""){
		return true;
	} else {
		msg = document.registratieform.jserror6.value + " :\n" + msg;
		alert(msg);
		return false;
	}
}

function doCheckRegistratie2() {
	var msg;
	msg = "";
	
	if (document.registratieform.company.value == ""){
		msg += "- " + document.registratieform.jserror1.value + "\n";
	}
	if (document.registratieform.firstname.value == ""){
		msg += "- " + document.registratieform.jserror2.value + "\n";
	}
	if (document.registratieform.lastname.value == ""){
		msg += "- " + document.registratieform.jserror3.value + "\n";
	}
	if (document.registratieform.address1.value == ""){
		msg += "- " + document.registratieform.jserror7.value + "\n";
	}
	if (document.registratieform.postal.value == "" || document.registratieform.town.value == ""){
		msg += "- " + document.registratieform.jserror8.value + "\n";
	}
	if (document.registratieform.country.value == ""){
		msg += "- " + document.registratieform.jserror9.value + "\n";
	}
	if (document.registratieform.tel.value == ""){
		msg += "- " + document.registratieform.jserror5.value + "\n";
	}
	if (document.registratieform.btw.value == ""){
		msg += "- " + document.registratieform.jserror11.value + "\n";
	}
	if (document.registratieform.email.value == ""){
		msg += "- " + document.registratieform.jserror4.value + "\n";
	}
	if (document.registratieform.password.value == ""){
		msg += "- " + document.registratieform.jserror10.value + "\n";
	}
	if (msg == ""){
		return true;
	} else {
		msg = document.registratieform.jserror6.value + " :\n" + msg;
		alert(msg);
		return false;
	}
}




function doSendOrder() {
	if (doCheckRegistratie()) {
		document.registratieform.submit();	
	}
}


