// Function to be able to place overlay in the view - even i scrolled down
function getMouseY(e) {
	var scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
	    // Netscape compliant
		scrOfY = window.pageYOffset;
	} else if( document.body && document.body.scrollTop ) {
	    // DOM compliant
		scrOfY = document.body.scrollTop;
	} else if( document.documentElement && document.documentElement.scrollTop ) {
	    // IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	}
	if ( scrOfY < 0 ) scrOfY = 0;
	if ( document.getElementById('realImage') != null) document.getElementById('realImage').style.top = scrOfY + 350 + 'px';
	if ( document.getElementById('imgShow') != null) document.getElementById('imgShow').style.top = scrOfY + 350 + 'px';
	return true;
}


// jQuery functions and definitions
$(document).ready(function() {

	// POPULAR PRODUCTS: Functions and parameters
	$(".carouselPopular").jCarouselLite({
    	btnNext: ".popularNext",
 		btnPrev: ".popularPrev",
 		scroll: 3,
 		circular: false
	});

	// LAST VISITED PRODUCTS: Functions and parameters
	$(".carouselLastVisited").jCarouselLite({
    	btnNext: ".lastVisitedNext",
 		btnPrev: ".lastVisitedPrev",
 		scroll: 3,
 		circular: false
	});

	// CUSTOMER ADDRESS: Funtions for customer data input
	$('#Name').click(function(){
		$('#Name').focus();
	});
	$('#showDeliveryInfo').click(function(){
	 	$('#showDeliveryInfo').fadeOut();
	 	$('#deliveryInfo').fadeIn();
	 	$('#deliveryHeader').html('Din bestilling betales af:');
	});
	$('#hideDeliveryInfo').click(function(){
	 	document.getElementById('Name2').value='';
	 	document.getElementById('Address2').value='';
	 	document.getElementById('City2').value='';
	 	$('#deliveryInfo').fadeOut();
	 	$('#showDeliveryInfo').fadeIn();
	 	$('#deliveryHeader').html('Betales af og leveres til:');
		updatePurchase( 0 , 'updateDelivery' );
	});

	// SEARCH: Fade out suggestions box on blur
	$('#inputString').blur(function(){
	 	this.value='Skriv søgeord';
	 	$('#suggestions').fadeOut();
	 });
	 
	 // Product image zoom
	 $(".jqzoom").jqzoom();

 });

// SEARCH: Handle Search query
function lookup(inputString) {
	if(inputString.length == 0) {
		// Hide the suggestions box if no query
		$('#suggestions').fadeOut();
	} else {
		// AJAX call if query
		$.post("/includes/search.php", {queryString: ""+inputString+""}, function(data) {
			$('#suggestions').fadeIn();		// Show suggestions box
			$('#suggestions').html(data);	// Fill suggestions box with html
		});
	}
}

// Show Aigle Video
function AigleVideo() {
	var realTop = (($(window).height() - 550) / 2)+$(document).scrollTop();
	var imgTop = $(document).scrollTop();
	$('#basketPop')
		.html('<div id="aigleVideo"><a onclick="javascript:AigleVideoClose()" id="videoClose">Luk</a><object width="853" height="505"><param name="movie" value="http://www.youtube.com/v/tCtBNvHv-uI&hl=da_DK&fs=1&rel=0&color1=0x3a3a3a&color2=0x999999"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/tCtBNvHv-uI&hl=da_DK&fs=1&rel=0&color1=0x3a3a3a&color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="853" height="505"></embed></object></div><div id="videoBackground" ></div>')
		.find('#aigleVideo')
		.css('top', realTop);
	$('#aigleVideo').css('top', realTop);
	$('#videoBackground')
		.css({'top': imgTop, 'opacity': '0'})
		.animate({'opacity': '0.75'}, 'slow');
		
}
function AigleVideoClose() {
	$('#basketPop').css({'display': 'none'}).html('').css({'display': 'block'})
}



// CUSTOMER ADDRESS: Validating input and adding help text
function isNumeric( numberId ) {
    return document.getElementById( numberId ).value.match(/^[0-9]+$/);
}

function isLatinName( numberId ) {
    return document.getElementById( numberId ).value.match(/^[a-zA-Z-.æøåÆØÅ ]+$/);
}

function isAddress( numberId ) {
    return document.getElementById( numberId ).value.match(/^[a-zA-Z-.æøåÆØÅ0-9 ]+$/);
}

function isEmail( numberId ) {
    return document.getElementById( numberId ).value.match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
}

function validateName( helpId, thisId ) {
	var content = document.getElementById( thisId ).value;
	var helpTxt = 'Skriv dit fulde navn eller et firmanavn.<br />Du kan anvende tal, bogstaver, punktum, bindestreg og mellemrum';
	var checkChr = content.substring( content.length-1 , content.length );
	var valid = true;
	var toLong = false;
	
	if ( content.length != 0 ) {
		if ( isAddress( thisId ) == null ) {
			content = content.substring( 0 , content.length-1 );
			document.getElementById( thisId ).value = content;
			valid = false;
		}
		if ( content.length < 2 && content.length !=0 ) {
			helpTxt = '<em>' + helpId +'et skal indeholde mindst 2 tegn</em>';
		} else if ( content.length >= 2 && content.length <= 80 ) {
			helpTxt = '<span>' + helpId +'e-feltet er gyldigt</span>';
		}
		else if ( content.length > 80 ) {
			content = content.substring( 0 , content.length-1 );
			document.getElementById( thisId ).value = content;
			helpTxt = '<span>' + helpId +'e-feltet er gyldigt</span>';
			valid = false;
			toLong = true;
		}
	}
	
	if (!valid) {
		if (toLong) {
			helpTxtShow( '' + helpId + '', '<em>' + helpId +'et kan højst indeholde 80 tegn</em>' );
			toLong = false;
		} else {
			helpTxtShow( '' + helpId + '', '<em>' + checkChr + ' er ikke et gyldigt tegn</em>' );
		}
		valid = true;
		setTimeout( "helpTxtShow('" + helpId + "', '" + helpTxt + "' )", 2000 );
	} else {
		helpTxtShow( '' + helpId + '', helpTxt );
	}
}

function validateAddress( helpId, thisId ) {
	var content = document.getElementById( thisId ).value;
	var Address = helpId.replace(/_/gi, " ");
	var helpTxt = 'Du kan anvende tal og bogstaver samt punktum, bindestreg og mellemrum';
	var checkChr = content.substring( content.length-1 , content.length );
	var valid = true;
	var toLong = false;
	
	if ( content.length != 0 ) {
		if ( isAddress( thisId ) == null ) {
			content = content.substring( 0 , content.length-1 );
			document.getElementById( thisId ).value = content;
			valid = false;
		}
		if ( content.length < 6 && content.length !=0 ) {
			helpTxt = '<em>'+ Address +' skal indeholde mindst 6 tegn</em>';
		} else if ( content.length >= 6 && content.length <= 80 ) {
			helpTxt = '<span>'+ Address +'-feltet er gyldigt</span>';
		}
		else if ( content.length > 80 ) {
			content = content.substring( 0 , content.length-1 );
			document.getElementById( thisId ).value = content;
			helpTxt = '<span>'+ Address +'-feltet er gyldigt</span>';
			valid = false;
			toLong = true;
		}
	}
	
	if (!valid) {
		if (toLong) {
			helpTxtShow( '' + helpId + '', '<em>'+ Address +'-feltet kan højst indeholde 80 tegn</em>' );
			toLong = false;
		} else {
			helpTxtShow( '' + helpId + '', '<em>' + checkChr + ' er ikke et gyldigt tegn</em>' );
		}
		valid = true;
		setTimeout( "helpTxtShow('" + helpId + "', '" + helpTxt + "' )", 2000 );
	} else {
		helpTxtShow( '' + helpId + '', helpTxt );
	}
}

function validateComment( helpId, thisId ) {
	var content = document.getElementById( thisId ).value;
	var Address = helpId.replace(/_/gi, " ");
	var helpTxt = 'Du kan anvende tal og bogstaver samt punktum, bindestreg og mellemrum';
	var checkChr = content.substring( content.length-1 , content.length );
	var valid = true;
	var toLong = false;
	
	if ( content.length != 0 ) {
		if ( isAddress( thisId ) == null ) {
			content = content.substring( 0 , content.length-1 );
			document.getElementById( thisId ).value = content;
			valid = false;
		}
		if ( content.length < 0 && content.length !=0 ) {
			helpTxt = '<span>'+ Address +'-feltet er gyldigt</span>';
		}
		else if ( content.length > 100 ) {
			content = content.substring( 0 , content.length-1 );
			document.getElementById( thisId ).value = content;
			helpTxt = '<span>'+ Address +'-feltet er gyldigt</span>';
			valid = false;
			toLong = true;
		}
	}
	
	if (!valid) {
		if (toLong) {
			helpTxtShow( '' + helpId + '', '<em>'+ Address +'-feltet kan højst indeholde 40 tegn</em>' );
			toLong = false;
		} else {
			helpTxtShow( '' + helpId + '', '<em>' + checkChr + ' er ikke et gyldigt tegn</em>' );
		}
		valid = true;
		setTimeout( "helpTxtShow('" + helpId + "', '" + helpTxt + "' )", 2000 );
	} else {
		helpTxtShow( '' + helpId + '', helpTxt );
	}
}

function validateEmail( helpId, thisId ) {
	var content = document.getElementById( thisId ).value;
	
	if ( content.length != 0 ) {
		if ( isEmail( thisId ) == null ) {
			helpTxtShow( '' + helpId + '', '<em>E-mail-adressen er endnu ikke gyldig</em>' );
		} else {
			helpTxtShow( '' + helpId + '', '<span>E-mail-adressen er gyldig</span>' );
		}
	} else {
		helpTxtShow( '' + helpId + '', 'Du modtager en e-mail med et track-and-trace nummer ved afsendelse af dine varer, så du kan følge din bestilling hos PostDanmark' );
	}
}

function validateNumber( helpId, thisId, minL, maxL ) {
	var content = document.getElementById( thisId ).value;
	var helpTxt = 'Skriv kun tal uden mellemrum eller andre tegn';
	var valid = true;
	var toLong = false;

	if ( content.length != 0 && isNumeric( thisId ) == null ) {
		content = content.substring( 0 , content.length-1 );
		document.getElementById( thisId ).value = content;
		valid = false;
	}
	if ( content.length < minL && content.length !=0 ) {
		helpTxt = '<em>Nummeret skal indeholde mindst ' + minL + ' tal</em>';
	}
	else if ( content.length >= minL && content.length <= maxL ) {
		helpTxt = '<span>Nummeret er gyldigt</span>';
	}
	else if ( content.length > maxL ) {
		content = content.substring( 0 , content.length-1 );
		document.getElementById( thisId ).value = content;
		helpTxt = '<span>Nummeret er gyldigt</span>';
		valid = false;
		toLong = true;
	}
	
	if (!valid) {
		if (toLong) {
			helpTxtShow( '' + helpId + '', '<em>Nummeret kan højst indeholde ' + maxL + ' tal</em>' );
			toLong = false;
		} else {
			helpTxtShow( '' + helpId + '', '<em>Du kan ikke indtaste andre tegn end tal</em>' );
		}
		valid = true;
		setTimeout( "helpTxtShow('" + helpId + "', '" + helpTxt + "' )", 1500 );
	} else {
		helpTxtShow( '' + helpId + '', helpTxt );
	}
}

function helpTxtShow( showId, text ){
	document.getElementById( showId ).style.display = 'block';
	document.getElementById( showId ).innerHTML = '<div id="topTxt">' + showId.replace(/_/gi, " ") + '</div><div id="botTxt">' + text + '</div>';
}
function helpTxtHide( hideId ){
	document.getElementById( hideId ).style.display = 'none';
}

function changeNumber( prodId , number ) {
	document.getElementById( "addProd"+prodId ).value = number;
}
		