$(document).ready( function() {

	assignDeleteProvEvents();

	$('input#doSend').click( function() {

		var passedValidation = true;
		var passedCode = true;
		
		$("#contactForm input").each(function() {
			if((this.type != "submit") && ($(this).val().length < 2)) { 
				passedValidation = false;
			} 
		});
			
		$("#contactForm textarea").each(function() {
			if(($(this).val().length < 2)) {
				passedValidation = false;
			}
		});

		if (!passedValidation) {
			alert("Alla fält är inte ifyllda.");
			return;
		}

		var code = $("#contactForm #security_code").val();

		$.post('xmlhttp/checkCaptha.php', { code: code }, function(data){
			if(data == 1){
				$('form#contactForm').submit();
				return;
			}
			alert("Koden stämmer ej.");
		});
	});

});

function assignDeleteProvEvents() {
	$('.deleteProv').click( function() {
		//console.log($(this));
		id = $(this).attr('id');

		$.get('xmlhttp/deleteProv.php', { id: id, type: 'vav' }, function(data) {

			//alert(data);
				
			if(data == 'tom'){
				$('.vavprovbox').hide();
			}else{
				$('.vavprovbox').html(data);
				//Assign click events to .deleteProv links again
				assignDeleteProvEvents();
			}
		});
	});

	$('.deleteLamellProv').click( function() {
		//console.log($(this));
		id = $(this).attr('id');

		$.get('xmlhttp/deleteProv.php', { id: id, type: 'lamell' }, function(data) {

			//alert(data);
				
			if(data == 'tom'){
				$('.lamellprovbox').hide();
			}else{
				$('.lamellprovbox').html(data);
				//Assign click events to .deleteProv links again
				assignDeleteProvEvents();
			}
		});
	});


	$('.deletePlisseProv').click( function() {
		//console.log($(this));
		id = $(this).attr('id');

		$.get('xmlhttp/deleteProv.php', { id: id, type: 'plisse' }, function(data) {

			//alert(data);
				
			if(data == 'tom'){
				$('.plisseprovbox').hide();
			}else{
				$('.plisseprovbox').html(data);
				//Assign click events to .deleteProv links again
				assignDeleteProvEvents();
			}
		});
	});

	
}

//changes main image on product page
function changeProductMainPicture( file, imageID, productID ) {
	$('#productImageLargeTD').html('<img src="'+file+'">');

	price = $('#prod_price').val();

	$('option.'+imageID).attr('selected', 'selected');

	$.get('xmlhttp/checkPrice.php',{ productID: productID, imageID: imageID, price: price }, function (data) {
		$('.price-body').html(data+':-');
	});
}



function popup (url, name, width, height, status, menu, resizable, scrollbars) {
	var prop = 'toolbar=no,location=no,directories=no,copyhistory=no,';
	prop += 'status='+ (status ? 'yes' : 'no') +',';
	prop += 'resizable='+ (resizable ? resizable : 'yes') +',';
	prop += 'menu='+ (menu ? 'yes' : 'no') +',';
	prop += 'scrollbars='+ (scrollbars ? scrollbars : 'yes') +',';
	prop += 'width='+ (width ? width : 500) +',';
	prop += 'height='+ (height ? height : 400);

	var win = window.open(typeof(url) == 'string' ? url : url.href, name, prop);
	win.focus();
	return false;
}





function proceedToPayment() {

	//alert($('form#payForm input#confirmDeal').attr('value'));
	// Insert code here.
	listenForPayment(); 
	custompop('aurigaforward.php', 640, 480, 'yes', 'no')
	
}

function trigPayment() {
	document.forms['orderForm'].submit();
}


function listenForPayment() {
	
	$.get('xmlhttp/aurigacheck.php', null, function (data) {
		if (data == 'OK') {
			alert("Betalningen är godkänd, klicka OK för att slutföra beställning.\n\nPayment accepted, please click OK to complete your order.");
			document.forms['orderForm'].submit();
		}
		else {
			setTimeout ( 'listenForPayment()', 5000 );
		}
	});
}


function custompop(url, width, height, status, menu) {
   win = window.open(url, 'profile_popup', 'toolbar=no,location=no,directories=no,status=' + status +  ',menubar=' + menu + ',scrollbars=yes,resizable=yes,copyhistory=no,width=' + width + ',height=' + height);
   win.focus();
}