function addtocart(productcode, type){
//alert("Koop product: " + productcode);
//alert($("#" + productcode).val());
//aantal = $("#" + productcode).val();
//if(aantal == ''){
//	addjustone(productcode);
//}else{
$.post("/clientcart.php", {action: "atc", code: productcode, type: type}, function(data){
  if(data == "ok"){
	//alert('Product is toegevoegd.');
	msgbox('Product is toegevoegd.', 750);
	//$("#" + productcode).val('');
	load_cart();
  }else{
	alert('Product toevoegen is mislukt.');
  }
});
//}
}

function addjustone(productcode){
//alert("Koop product: " + productcode);
//alert($("#" + productcode).val());
$.post("/clientcart.php", {action: "atc", code: productcode, aantal: "1"}, function(data){
  if(data == "ok"){
	//alert('Product is toegevoegd.');
	msgbox('Product is toegevoegd.', 750);
	load_cart();
  }else{
	alert('Product toevoegen is mislukt.');
  }
});
}

function msgbox(bericht, interval){
$("#msgbox").css({'display' : 'block'});
$("#msgbox").html('<p class="cf msgbox">'+bericht+'</p>');
window.setTimeout('hide_msgbox()', interval);
}
function hide_msgbox(){
$("#msgbox").css({'display' : 'none'});
}

function bigmsgbox(bericht, interval){
$("#bigmsgbox").css({'display' : 'block'});
$("#bigmsgbox").html('<p class="cf bigmsgbox">'+bericht+'</p>');
window.setTimeout('hide_bigmsgbox()', interval);
}
function hide_bigmsgbox(){
$("#bigmsgbox").css({'display' : 'none'});
}

function delfromcart(productcode, type){
//alert("Koop product: " + productcode);
//alert($("#" + productcode).val());
$.post("/clientcart.php", {action: "rfc", code: productcode, type: type}, function(data){
  if(data == "ok"){
	//alert('Product is verwijderd.');
	msgbox('Product is verwijderd.', 1500);
	load_cart();
  }else{
	alert('Product verwijderen is mislukt.');
  }
});
}

function load_cart(){
$.post("/clientcart.php", {action: "loadcart"}, function(data){
  //alert(data);
  $("#cart").html(data);
});
}

function focus_login(field_id, username){
	$("#usrname").val(username);
	$("#pssword").val('');
	$("#"+field_id).focus();
}

function validate(obj, indicator){
txt = $(obj).val();
//$("#test").text(txt);

if(txt == ''){
$("#"+indicator).html('<!--<img src="/img/gri.jpg"/>-->');
}else{
$("#"+indicator).html('<img src="/img/validating.gif"/>');
}

rules = obj.className.split(' ');
required = rules[1];
type = rules[2];

$.post("/form_validator.php", {vtxt:txt, req:required, vtype:type, ind:indicator}, function(data){
  //alert(data+' '+indicator);
if(txt == ''){}else{
if(data == 'ok'){
	$("#"+indicator).html('<img src="/img/gro.jpg"/>');
	$("#verder_btn").html('');
}else if(data == 'pok'){
	$("#"+indicator).html('<img src="/img/gro.jpg"/>');
	$("#verder_btn").html('<div class="cf btn pcode_check" onclick="check_postcode();"><p class="cf btn">Verder</p></div>');
	}else{
	$("#"+indicator).html('<img src="/img/not-valid.jpg"/>');
	$("#verder_btn").html('');
	}
}
  //$("#test").text(data);
  
});
return 'done';
}

function revalidate_form(number){
var item = '';
$("td.indicator").each(function(i){
var indicator_id = this.id;
item += indicator_id+' ';
});
veld = item.split(' ');

//i = 2;
//for (var i=0;i<veld.length;i++){
//field[] = '';

indicator = veld[number];
field = document.getElementsByName(veld[number]);
object = field[0];

if(validate(object, indicator) == 'done'){
number++;
//$("#commentaar").css({'display' : 'block'});
//$("#commentaar").text(object+" - "+indicator+" - "+number);
setTimeout("revalidate_form("+number+")",400);
}

//}
}

function validate_form(){
var item = '';
err = 0;

$("td.indicator").each(function(i){
var indicator_id = this.id;
item += indicator_id+' ';
});

console.log( item );

veld = item.split(' ');

for (var i=0;i<(veld.length-1);i++){
status = $("#" + veld[i]).html();

console.log(status);

if( status === '' || status === '<img src="img/gri.jpg">' || status === '<img src="/img/ora.jpg">' || status === '<img src="/img/not-valid.jpg">'){
err++;
}

if(err == 1){
field = document.getElementsByName(veld[i]);
field[0].focus();
}

if(veld[i] == 'pass'){
field = document.getElementsByName(veld[i]);
pass1 = field[0].value;
//$("#commentaar").css({'display' : 'block'});
//$("#commentaar").text(pass1);
}

if(veld[i] == 'hhpass'){
field = document.getElementsByName(veld[i]);
pass2 = field[0].value;
}

}

if( ( pass1 !== '' && pass2 !== '' ) && ( pass1 === pass2 ) ){
ww = 'good';
}else{
ww = 'bad';
}

if(err == 0){
if(ww == 'good'){
$("#commentaar").css({'display' : 'block'});
$("#commentaar").html('Uw aanvraag wordt nu verwerkt.<br/>Een ogenblik geduld a.u.b.');
setTimeout("document.ajaxvalform.submit();",2400);
}else{
$("#commentaar").css({'display' : 'block'});
$("#commentaar").html('Het controle wachtwoord is niet het zelfde als het wachtwoord.');
field = document.getElementsByName('hhpass');
field[0].value = '';
field = document.getElementsByName('pass');
field[0].value = '';
field[0].focus();
}
}else{
$("#commentaar").css({'display' : 'block'});
if(err == 1){
$("#commentaar").text('U heeft '+err+' veld nog niet correct ingevuld.');
}else if(err > 1){
$("#commentaar").text('U heeft '+err+' velden nog niet correct ingevuld.');
}
}

//$("#test").text(err);
}

function validate_npform(){
field = document.getElementsByName("pass");
pass1 = field[0].value;

field = document.getElementsByName("hhpass");
pass2 = field[0].value;


if(pass1 == pass2){
ww = 'good';
if(!pass1){
ww = 'empty';
}
}else{
ww = 'bad';
}

if(ww == 'good'){
setTimeout("document.ajaxvalform.submit();",200);
}else if(ww == 'empty'){
bigmsgbox('Een of meerdere textvelden zijn leeg.', 1800);
}else{
bigmsgbox('De ingevulde wachtwoorden komen niet overeen.', 1800);
}

}

function check_postcode(status){
//status = $("#ipcode").html();
//pcode_veld = document.getElementById("pcode_veld").innerHTML;
//alert(status);
//if(status == '<img src="img/gro.jpg">'){
//alert('alles is goed');
setTimeout("document.pcodeform.submit();",200);
//}else if(pcode_veld == ''){
//bigmsgbox('Vul a.u.b. een postcode in voordat u verder gaat.', 2300);
//return false;
//}else{
//bigmsgbox('De ingevulde postcode valt (nog) niet binnen ons bezorggebied.', 2300);
//return false;
//}
}

function show_lightbox(pic_url, titel){
//$("#flashheader").html('');
//$("#lightbox").css({'display' : 'block'});
//lightbox_width = screen.availWidth;
//lightbox_height = screen.availHeight;
//$("#lightbox").css({'height' : lightbox_height});

showLightbox('product', pic_url, titel);
//$("#foto").html('<img src="'+pic_url+'"/>');

//render_pic(pic_url);
}

function render_pic(pic_url, titel){

$.post("/get_image_size.php", {pic_url: pic_url}, function(data){
	//msgbox('Image size ontvangen', 1500);
	pic_size = data.split(',');
	//alert(data);
	$("#jquery-lightbox").css('width', pic_size[0]+'px');
	$("#jquery-lightbox").css('height', (pic_size[1])+'px');
	$("#titel").html(titel);
  
});

}


function showLightbox(dialogName, param, titel) {
	
	$('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lbContent"><img id="loading-image" src="/img/lightbox-loader.gif"/></div><div class="cf" id="nav"><h3 id="titel" class="titel" style="font-size:17px;padding-left:15px;padding-top:9px;color:#666666">...</h3><img id="btnClose" src="/img/lightbox-btn-close.gif"/></div></div>');	
	
	render_pic(param, titel);
	
	var arrPageScroll = getPageScroll();
	// Calculate top and left offset for the jquery-lightbox div object and show it
	
	
	// Style overlay and show it
	$('#jquery-overlay').css({
		width: $(window).width(),
		height:	$(document).height()
	}).fadeIn(400, function(){
		$('#loading-image').css({
		left: (($('#jquery-lightbox').width()/2) - ($('#loading-image').width())) + 'px',
		top: (($('#jquery-lightbox').height()/2) - ($('#loading-image').height())) + 'px'
		}).fadeIn(600);
		
		$('#lbContent').css({
		height: ($('#jquery-lightbox').height()-40) + 'px'
		}).fadeIn(850);
		
		$('#btnClose').fadeIn(600);
	});
	
	$('#jquery-overlay').click(function() {
		closeLightbox();
	});
	$('#btnClose').click(function() {
		closeLightbox();
	});
	
	$(document).keydown(function(objEvent) {
		if ( objEvent == null ) {
			keycode = event.keyCode;
			escapeKey = 27;
		// To Mozilla
		} else {
			keycode = objEvent.keyCode;
			escapeKey = objEvent.DOM_VK_ESCAPE;
		}
		
		if (keycode == 27) {
			closeLightbox();
		}
		
	});
		
	$(window).resize(function() {
		// Style overlay and show it
		$('#jquery-overlay').css({
			width:		$(window).width(),
			height:		$(document).height()
		});
		// Get page scroll
		var arrPageScroll = getPageScroll();
		// Calculate top and left offset for the jquery-lightbox div object and show it 
		$('#jquery-lightbox').css({
			top:	($(window).height() - ($('#jquery-lightbox').height()+20))/2 + arrPageScroll[1]
		});
	});
	
	$(window).scroll(function() {
		
		// Get page scroll
		var arrPageScroll = getPageScroll();
		// Calculate top and left offset for the jquery-lightbox div object and show it
		$('#jquery-lightbox').css({
			top:	($(window).height() - ($('#jquery-lightbox').height()+20))/2 + arrPageScroll[1]
		});
	});
	
	$('#jquery-lightbox #lbContent').html('<img class="cf" src="'+param+'"/>');
	//alert($('#jquery-lightbox').height()+$('#jquery-lightbox #lbContent').height());
	$('#jquery-lightbox').css({
		top:	arrPageScroll[1] + 20
	});
	
}

function closeLightbox(){
	$('#jquery-lightbox').fadeOut(500, function(){
		$(this).remove();
	});
	$('#jquery-overlay').fadeOut(500, function(){
		$(this).remove();
	});
	
}

function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
}

function tooltip(target){
//alert($('#tooltip').length);
if($('#tooltip').length == 0){
$(target).append(getTooltipText($(target).attr('class')));
$('#tooltip').hide();
$('#tooltip').fadeIn('fast');
}else{
$('#tooltip').remove();
}
}

function getTooltipText(classes){
var output = '';

//alert(classes);

switch (classes){
case "tooltip pc":
  output = '<div class="cf cb" id="tooltip"><img alt="Klik hier om direct te bestellen." title="Klik hier om direct te bestellen." src="/img/postcodegebied_sugoi_sushi.jpg"/></div>';
  break;
default:
  output = '';
}

return(output);
}
