/* This script by $criptMaster BB Duall 2008-2011 */
/* JQuery 1.2.6 needed */
/* www.duall.be */

$(document).ready(function() {
	
	DUALLblurAnchors();
	maproll(1,"-330px");
	maproll(2,"-660px");
	maproll(3,"-990px");
	maproll(4,"-1320px");
	maproll(5,"-1650px");
	maproll(6,"-1980px");
	maproll(7,"-2310px");
	maproll(8,"-2640px");
	maproll(9,"-2970px");
	maproll(10,"-3300px");
	subnavroll(1);
	subnavroll(2);
	subnavroll(3);
	subnavroll(4);
	subnavroll(5);
	subnavroll(6);
	subnavroll(7);
	subnavroll(8);
	subnavroll(9);
	ETgoHome();
	DUALLvalidate("#contactForm")
	showMap();
	setupSearch();
});

function DUALLblurAnchors() {
	if(document.getElementsByTagName) {
		var a = document.getElementsByTagName("a");
		for(var i = 0; i < a.length; i++){
			a[i].onfocus = function(){this.blur();};
		}
	}
};

function showMap(){
		$('a#ShowMap').click(function () {
	      $("#Googlemapswrapper").show();
	    });
		$('a#closeMaps').click(function () {
	      $("#Googlemapswrapper").hide();
	    });
}

function maproll(idCntr,deltaH) {
// kaart
	$("#locatiekaart li#bedrijvencentrum"+idCntr+" a, .bedrijvencentra li a#tblcntr"+idCntr+".centrumcontainer").hover(
			function(){
				$("#locatiekaart").css("background-position","0 "+deltaH);
				$(".bedrijvencentra li a#tblcntr"+idCntr+".centrumcontainer").addClass("over");
			},
			function () {
				$("#locatiekaart").css("background-position","0 0");
				$(".bedrijvencentra li a#tblcntr"+idCntr+".centrumcontainer").removeClass("over");
		    }
	);
	return false;
} 

function subnavroll(idCntr) {
// kaart
	$("#subnav li#bedrijvencentrum"+idCntr+" a").hover(
			function () {
				$("#subnav li#bedrijvencentrum"+idCntr).addClass("over");
			},
			function () {
				$("#subnav li#bedrijvencentrum"+idCntr).removeClass("over");
		    }
	);
	return false;
}

function ETgoHome() {
	$("h1").hover(
		function(){
			$("h1").css({ cursor:"pointer" });
		},
		function () {
			$("h1").css({ cursor:"auto" });
		}),
	$("h1").click(function(){
		window.open('index.html','_self');
	});
}

//function setimage(strID, strImage, strFotoCopyright){
//	if (document.getElementById)
//	{
//		document.getElementById(strID).src=strImage;
//	}
//}

function setimage(strID, strImage, strFotoCopyright){
		$("#"+strID).attr("src",strImage);
		$('p.FotoCopyright').text(strFotoCopyright);
}


/*$.validator.setDefaults({
	submitHandler: function() { alert("Bedankt, het formulier werd verstuurd!"); }
});*/


// validate signup form on keyup and submit
function DUALLvalidate(FORM) {
	var $contactForm = $('#contactForm');
	
	if($('#contactForm').size() > 0){

		if($('#oia_member_extra_category_id').val() == ''){
			$('div#extraCat').hide();
			$('a#extraCatLink').show();
		}
		else{
			$('div#extraCat').show();
			$('a#extraCatLink').hide();			
		}


		$('#oia_member_request_type_1').click(_setup_contact_form_1);
		$('#oia_member_request_type_1:checked').each(_setup_contact_form_1);
		
		$('#oia_member_request_type_2').click(_setup_contact_form_2);
		$('#oia_member_request_type_2:checked').each(_setup_contact_form_2);
		
		$('#oia_member_request_type_3').click(_setup_contact_form_3);
		$('#oia_member_request_type_3:checked').each(_setup_contact_form_3);
		
		$('a#extraCatLink').click(function(e){
			
			$('div#extraCat').show();
			$('a#extraCatLink').hide();
			
			e.preventDefault();
		});
		
		_setup_contact_form();
	}
}

$.validator.addMethod("iban", function(value, element, enabled) {
			if(enabled){
				var stripped = element.value.replace(/\s/g, '');
				
				if(matched = stripped.match(/^([A-z]{2})([0-9]{14,})$/)){
					element.value = matched[1].toUpperCase() + matched[2].substr(0, 2) + ' ' + matched[2].substr(2, 4) + ' ' + matched[2].substr(6, 4) + ' ' + matched[2].substr(10, matched[2].length);
				}
	
        return element.value.match(/^[A-z]{2}\d{2}( \d{4}){3}$/);				
			}
			else{
				return true;
			}
    },
    "Gelieve een correct IBAN nummer in te vullen (BExx xxxx xxxx xxxx)"
);

$.validator.addMethod("vat_number", function(value, element) {
				var stripped = element.value.replace(/\s/g, '');
				var modulus = 1;
				var checksum = -1;
				
				if(matched = stripped.match(/^([A-z]{2})([0-9]{10,})$/)){
					element.value = matched[1].toUpperCase() + ' ' + matched[2].substr(0, 4) + ' ' + matched[2].substr(4, 3) + ' ' + matched[2].substr(7, 3);
					
					number = parseInt(matched[2].substr(0, 8), 10);
					checksum = parseInt(matched[2].substr(8,2), 10);
					modulus = (97 - (number % 97));
					
//					console.log(number + ' ' + checksum + ' ' + modulus);
				}
	
        return (element.value.match(/^[A-z]{2} \d{4}( \d{3}){2}$/) && (modulus == checksum));
    },
    "Gelieve een correct en geldig ondernemingsnummer in te vullen (BE xxxx xxx xxx)"
);

function _setup_contact_form(){
	$('#contactForm').validate({
		rules: {
			'oia_member[company]': "required",
			'oia_member[name]': "required",
			'oia_member[activity]': "required",
			'oia_member[vat_number]': {
				vat_number: true
			},
			'oia_member[zipcode]': {
				required: true,
				number: true,
				minlength: 4
			},
			'oia_member[email]': {
				required: true,
				email: true
			},
			'oia_member[website]': {
				required: false,
				url: true
			},
			'oia_member[bank_account_number]': {
				iban: function(element){
	        return !$('#oia_member_request_type_2').is(':checked');
	      }
			},
			'oia_member[terms_and_conditions]': "required"
		},
		// the errorPlacement has to take the table layout into account 
		errorPlacement: function(error, element) { 
			if ( element.is("#oia_member_zipcode") ) {
				error.insertAfter( element.parent() );
				} else {
					error.appendTo( element.parent() ); }
				}, 
		messages: {
			'oia_member[company]': "Gelieve uw bedrijf in te geven",
			'oia_member[name]': "Gelieve de naam van de zaakvoerder in te geven",
			'oia_member[activity]': "Gelieve uw activiteit in te geven",
			'oia_member[zipcode]': {
				required: "Gelieve uw postcode in te geven",
				number: "Gelieve enkel cijfers te gebruiken",
				minlength: "Geef minstens 4 cijfers in"
			},
			'oia_member[email]': {
				required: "Gelieve uw e-mailadres correct in te geven",
				email: "Geef je e-mailadres correct in aub",
			},
			'oia_member[website]': {
				required: "Gelieve uw webadres correct in te geven",
				url: "Geef je url correct in aub, start met http://",
			},
			'oia_member[terms_and_conditions]': "Gelieve u akkoord te verklaren met ons reglement"
		}
	});
}

function _setup_contact_form_1(){
	$('div.oia_row_2').hide();
	$('div.oia_row_1, div.oia_row_3').show();
}

function _setup_contact_form_2(){
	$('div.oia_row_1').hide();
	$('div.oia_row_2, div.oia_row_3').show();
}

function _setup_contact_form_3(){
	$('div.oia_row_1, div.oia_row_2, div.oia_row_3').show();
}

function setupSearch(){
	$('#wat').focus(function(){
		var $this = $(this);
		if($this.val() == 'Postcode, Naam of zoekterm'){
			$this.val('');
		}
	});
	
	$('#searchForm').submit(function(){
		var $wat = $('#wat');
		if($wat.val() == 'Postcode, Naam of zoekterm'){
			$wat.val('');
		}
	});
	
	$('#zipcode_form select#postnummer').change(function(){
		$('#zipcode_form').submit();
	});
}

