var scriptlocation = "/includes/_shop.php";

/* Show Password recovery form */
function pgPasswordForm() {
	klapin();
	// remove tickbox if ther is one
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";

	
    jQuery.ajax({
        type: "POST",
        url: scriptlocation,
        data: {'function':"pgPasswordForm"},
        success: function(result){

			// Zoek resultaat..
			jQuery("#zoekresultaat").hide().html(result).slideDown('slow');

        }
    });

    return false;
}


/* Recover password */
function pgPasswordRecover(taal) {

	var email = jQuery("#finish [name='email']").val();

	if(email==''){
		if(taal=='eng'){
			alert('Please fill email address!');
		}else{
			alert('Vul uw email adres in!');
		}
	}else{

	    jQuery.ajax({
	        type: "POST",
	        url: scriptlocation,
	        data: {'function':"pgPasswordRecover", 'email': email},
	        success: function(result){

				// Zoek resultaat..
				jQuery("#zoekresultaat").hide().html(result).slideDown('slow');

	        }
	    });

	}

    return false;
}


/* Offerte Aanvragen bestaande user */
function pgAanvragen(taal) {

	var email = jQuery("#finish [name='email']").val();
	var password1 = jQuery("#finish [name='password1']").val();

	if(email=='' || password1==''){
		if(taal=='eng'){
			alert('Please fill email address and password!');
		}else{
			alert('Vul uw email adres en wachtwoord in!');
		}
	}else{

	    jQuery.ajax({
	        type: "POST",
	        url: scriptlocation,
	        data: {'function':"pgAanvragen", 'email': email, 'password1': password1},
	        success: function(result){

				// Zoek resultaat..
				jQuery("#zoekresultaat").hide().html(result).slideDown('slow');

	        }
	    });

	}

    return false;
}


/* Offerte Aanvragen + Aanmelden nieuwe user */
function pgAanmelden_oud(taal) {

	var email = jQuery("#finish [name='email']").val();
	var password1 = jQuery("#finish [name='password1']").val();
	var password2 = jQuery("#finish [name='password2']").val();
	var bedrijfsnaam = jQuery("#finish [name='bedrijfsnaam']").val();
	var tav = jQuery("#finish [name='tav']").val();
	var adres = jQuery("#finish [name='adres']").val();
	var pc = jQuery("#finish [name='pc']").val();
	var plaats = jQuery("#finish [name='plaats']").val();
	var telefoon = jQuery("#finish [name='telefoon']").val();

	if(email=='' || password1=='' || password2=='' || bedrijfsnaam=='' || tav=='' || adres=='' || pc=='' || plaats=='' || telefoon==''){
		if(taal=='eng'){
			alert('Please fill all the form fields!');
		}else{
			alert('U moet alle velden invullen!');
		}
	}else{

		if(password1!=password2){
			if(taal=='eng'){
				alert('Passwords are not the same!');
			}else{
				alert('Wachtwoorden zijn niet hetzelfde!');
			}
		}else{
		
		    jQuery.ajax({
		        type: "POST",
		        url: scriptlocation,
		        data: {'function':"pgAanmelden", 'email': email, 'password1': password1, 'password2': password2, 'bedrijfsnaam': bedrijfsnaam, 'tav': tav, 'adres': adres, 'pc': pc, 'plaats': plaats, 'telefoon': telefoon},
		        success: function(result){
	
					// Zoek resultaat..
					jQuery("#zoekresultaat").hide().html(result).slideDown('slow');
	
		        }
		    });
	    
		}

	}

    return false;
}


/* Offerte Aanvragen */
function pgAanmelden(taal) {

	var email = jQuery("#finish [name='email']").val();
	var bedrijfsnaam = jQuery("#finish [name='bedrijfsnaam']").val();
	var sex = jQuery("#finish [name='sex']:checked").val();
	var voornaam = jQuery("#finish [name='voornaam']").val();
	var tussenvoegsel = jQuery("#finish [name='tussenvoegsel']").val();
	var achternaam = jQuery("#finish [name='achternaam']").val();
	var adres = jQuery("#finish [name='adres']").val();
	var pc = jQuery("#finish [name='pc']").val();
	var plaats = jQuery("#finish [name='plaats']").val();
	var telefoon = jQuery("#finish [name='telefoon']").val();
	var opmerkingen = jQuery("#finish [name='opmerkingen']").val();
	var catalogus = jQuery("#finish [name='catalogus']:checked").val();
	var mailinglist = jQuery("#finish [name='mailinglist']:checked").val();
	var afspraak = jQuery("#finish [name='afspraak']:checked").val();

	if(email==''){
		if(taal=='eng'){
			alert('Please fill email address!');
		}else{
			alert('Vul uw email adres in!');
		}
	}else{

	    jQuery.ajax({
	        type: "POST",
	        url: scriptlocation,
	        data: {'function':"pgAanmelden", 
	        		'email': email, 
	        		'bedrijfsnaam': bedrijfsnaam, 
	        		'sex': sex, 
	        		'voornaam': voornaam, 
	        		'tussenvoegsel': tussenvoegsel, 
	        		'achternaam': achternaam,  
	        		'adres': adres, 
	        		'pc': pc, 
	        		'plaats': plaats, 
	        		'telefoon': telefoon,
	        		'opmerkingen': opmerkingen,
	        		'catalogus': catalogus,
	        		'mailinglist': mailinglist,
	        		'afspraak': afspraak
	        },
	        success: function(result){
	
				// Zoek resultaat..
				jQuery("#zoekresultaat").hide().html(result).slideDown('slow');

	        }
	    });
	    

	}

    return false;
}


/* Offerte */
function pgOfferte() {

	// remove tickbox if ther is one
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	
    jQuery.ajax({
        type: "POST",
        url: scriptlocation,
        data: {'function':"pgOfferte"},
        success: function(result){

			// Zoek resultaat..
			jQuery("#zoekresultaat").hide().html(result).slideDown('slow');

        }
    });

    return false;
}


/* Show Cart */
function pgShowCart() {
	klapin();
	// remove tickbox if ther is one
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";

	
    jQuery.ajax({
        type: "POST",
        url: scriptlocation,
        data: {'function':"pgShowCart"},
        success: function(result){

			// Zoek resultaat..
			jQuery("#zoekresultaat").hide().html(result).slideDown('slow');

        }
    });

    return false;
}


/* Add to / update / remove from Cart */
function pgCart(action,uid,taal) {
	
	if( uid == null ) var uid = jQuery("#cart [name='uid']").val();
	var qty = jQuery("#cart [name='qty_"+uid+"']").val();

	//alert(taal);
	
    jQuery.ajax({
        type: "POST",
        url: scriptlocation,
        data: {'function':"pgCart", 'action': action, 'uid': uid, 'qty': qty},
        success: function(result){

			// resultaat..
			if(action=="add") {
				jQuery("#product").hide().html(result).slideDown('slow');
				//jQuery("#product").hide().html(result).slideDown('slow').animate({opacity: 1.0}, 5000).slideUp('slow');
			} else {
				//alert("uw winkelwagen is aangepast");
				if(taal=="eng"){
					tb_show("aangepast", "tekst1_eng.html?height=90&width=200&modal=true", "");
				}else{
					tb_show("aangepast", "tekst1_nl.html?height=90&width=200&modal=true", "");
				}
				jQuery("#zoekresultaat").hide().html(result).slideDown('slow');
			}
			
        }
    });

    return false;
}


/* offset search */
function pgZoekNav(product,groep,category,offset) {

    jQuery.ajax({
        type: "POST",
        url: scriptlocation,
        data: {'function':"pgZoek", 'product': product, 'groep': groep, 'category': category, 'offset': offset},
        success: function(result){

			// Zoek resultaat..
			jQuery("#zoekresultaat").hide().html(result).slideDown('slow');

        }
    });

    return false;
}



/* product search */
function pgZoek(taal) {
	klapin();
    var product = jQuery("#searchbox [name='product']").val();
    var groep = jQuery("#searchbox [name='groep']").val();

    if (product=='vul een zoekterm in' || product=='search term') {
    	product = '';
    }
    
    if (product.length<2 && groep.length<1) {
    	if(taal=='eng'){
			alert('Please fill a search term and/or category!');
		}else{
			alert("Vul een zoekterm en/of categorie in!");
		}
		return;
    }

    /*if (groep.length<1) {
        alert("Je moet wel een categorie kiezen!");
        return;
    }*/

    jQuery.ajax({
        type: "POST",
        url: scriptlocation,
        data: {'function':"pgZoek", 'product': product, 'groep':groep },
        success: function(result){

			// Zoek resultaat..
			jQuery("#zoekresultaat").hide().html(result).slideDown('slow');

        }
    });

    return false;
}


/* product_lookup */
function findValue(li) {
	if( li == null ) return alert("Geen match!");

	// if coming from an AJAX call, let's use this as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("Test: " + sValue);
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return row[0];
}

function lookupAjax(){
	var oSuggest = $("#product_lookup")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

$(document).ready(function() {
	$("#product_lookup").autocomplete(
		"includes/_autocomp_products.php",
		{
			delay:10,
			minChars:3,
			matchSubset:1,
			matchContains:1,
			cacheLength:1,
			maxItemsToShow:15,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:false
		}
	);
});
