/*
	Author: Wesley Bos - Label A 2011
*/

//Validate function
if ($('#form').length) jQuery.validator.addMethod('zipcode',function() {
	var zip1 = $('#zipcode_1').val();
	var zip2 = $('#zipcode_2').val();
	if(zip1 == '' || zip2 == '') {
		return false
	}
	return true;
}, 'This field is required.');

//Tooltip
var activateTooltip = function() {
	var tip;
	var spacing = { left: 20, top: 0 }
	$(".tip_trigger").hover(function(){
		tip = $(this).find('.tip').remove();
		$('body').append(tip);
		spacing.top = -tip.height();
		tip.show();
		tip.css({ top: $(this).offset().top + spacing.top, left: $(this).offset().left + spacing.left })
	}, function() {
		tip.hide().remove();
		$(this).append(tip);
	})
}

// Hide google maps
if ($("#navigation-details").length) {
	initialize();
	$('#navigation-details').hide();
	$('#mapdirections').hide();
}

//Page load
$(document).ready(function(){
	activateTooltip();
	if (window.location.hash.length && window.location.hash.substr(1) != "alle") if ($("#brands-filter a." + window.location.hash.substr(1)).length) $("#brands-filter a." + window.location.hash.substr(1)).trigger("click");
});

function removeText(obj){
	obj.value = ''
}

function replaceText(obj, val){
	if(obj.value == ''){
		obj.value = val;
	}
}

function doNext(el){
	if (el.value.length < el.getAttribute('maxlength')) return;

	var f = el.form;
	var els = f.elements;
	var x, nextEl;
	for (var i=0, len=els.length; i<len; i++){
		x = els[i];
		if (el == x && (nextEl = els[i+1])){
			if (nextEl.focus) nextEl.focus();
		}	
	}
}


function checkAge() {
	var day = $('#age_day').val();
	var month = $('#age_month').val() - 1;
	var year = $('#age_year').val();
	
	var birthdate = new Date(year, month, day);
	var now = new Date();
	var age = Math.floor((now.getTime() - birthdate.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
	if (isNaN(age)) {
		$("#error").html("U dient uw volledige geboortedatum op te geven.");
	} else if (age >= 18) {
		setCookie('isoflegalage', 'true', 30);
		return true;
	} else {
		$('#error').html('U moet minimaal 18 jaar zijn om deze website te bezoeken.')
	}
	$('#error').show();
	return false;
	
	/*var queryString =  { age_day: $("#age_day").val(), age_month: $("#age_month").val(), age_year: $("#age_year").val() };	
	if($("#age_day").val() != "" && $("#age_month").val() != "" && $("#age_year").val() != "" && $("#age_day").val() != "DD" && $("#age_month").val() != "MM" && $("#age_year").val() != "YYYY") {	
		$.ajax({
			url : "/hermanjansen/includes/display_objects/agecheck.cfm",
			data : queryString,
			cache : false,
			success :  function(returndata){
				if(returndata.isLegalAge == "true") {
					return true;
				}else{
					$("#error").show();
					return false;
				}
				},
			dataType : "json",
			type : "GET"
		});
	}else{
		$("#error").html("U dient uw volledige geboortedatum op te geven");
		$("#error").css("display","block");
		return false;
	}*/
}

function setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value + '; path=/';
}

function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
	 	x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
	 	y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
	  	if (x==c_name) {
	  		return unescape(y);
	    }
	}
}


