dojo.require("dojo.parser");
dojo.require("dijit.Dialog");
dojo.require('dijit.layout.ContentPane');
dojo.require('dijit.layout.TabContainer');
dojo.require("dijit.layout.AccordionContainer");

function giftAnim(node,msg) {
		
	var fadeInMsg = dojo.fadeIn({
		node: node,
		duration: 1000,
		beforeBegin: function() {
			dojo.byId(node).innerHTML = msg;
			var y = dojo.coords(dojo.body()).y;
			var ny = -y;
			dojo.byId(node).style.top = ny+'px';
			dojo.byId(node).style.display = 'block';
		}
	});
	
	var fadeOutMsg = dojo.fadeOut({
		node: node,
		duration: 4500,
		onEnd: function() {
			dojo.byId(node).style.display = 'none';
		}
	});
	
	dojo.fx.chain([fadeInMsg,fadeOutMsg]).play();
}

function addGift(idgift,imagix) {
	imagix = imagix? true : false;
	
	if (imagix) {
		var giftInfo = dojo.byId('giftInfo');
		if (dojo.query('*[name="optiongift"]',giftInfo)[0].getAttribute('type') == 'hidden') {
			var option = dojo.query('*[name="optiongift"]',giftInfo)[0].value;
		} else {
			var option = dijit.getEnclosingWidget(dojo.query('*[name="optiongift"]',giftInfo)[0]).attr('value');
		}
		var num = dojo.query('*[name="number"]',giftInfo)[0].value;
	} else {
		if (document.getElementById('optiongift'+idgift).getAttribute('type') == 'hidden') {
			var option = dojo.byId('optiongift'+idgift).value;
		} else {
			var option = dijit.byId('optiongift'+idgift).attr('value');
		}
		var num = dojo.byId('qtgift['+idgift+']').value;
	}
	
	if(num == 0 || num == '') {
		return false;
	}
	
	// if the gift is custom(=1) the setPhoto must be != 0
	/*if(custom == 1) {
		// verify that the setPhoto is diferent than 0
		if(setPhoto == 0) {
			giftAnim("popAlertBad",haveUploadPhoto);
			return false;
		} else {
			var idphoto = setPhoto;
			console.log('idphoto set for the gifts: ' + idphoto);
		}				
	} else {
		var idphoto = setPhoto;
		console.log('idphoto set for the gifts: ' + idphoto);
	}*/
	
	dojo.xhrPost({
		url: 'order.backend.php',
		load: function(data) {
			if(data == 1) {
				// show the popconsole.log
				giftAnim("popAlertGood",gadgetAdded);
				// call the update cart
				successMsg();
			} else {
				console.log('Impossible to add gift to your order.');
			}		
		},
		error: function(e) {console.log('error in addGift(): '+e);},
		content: {action : 'addGift', figift : idgift, dtcount : num, option : option},
		sync: true
	});
	
}

function placePhoto(idgift) {
	var num = dojo.byId('qtgift['+idgift+']').value;

	if (num == 0 || num == '') {
		return false;
	}
	
	if (document.getElementById('optiongift'+idgift).getAttribute('type') == 'hidden') {
		var option = dojo.byId('optiongift'+idgift).value;
	} else {
		var option = dijit.byId('optiongift'+idgift).attr('value');
	}
	
	location.href = '?action=gift_imagix&id='+idgift+'&option='+option+'&num='+num;
}

function addNewPrice() {
	console.log('addNewPrice');
	
	dojo.xhrPost({
		url:'order.backend.php',
		load:function(result) {
			console.log(result);
		},
		error:function(data) {console.log('error in addNewPrice() '+data);},
		form:"priceform",
		sync:true
	});	
	
	dijit.byId('priceDialog').hide();
}

function removeGift(idline) {
	//console.log('the idline to remove: ' + idline);
	dojo.xhrPost({
		url: 'order.backend.php',
		load: function(data) {
			if(data == 1) {
				loader.loadModule_checkout();
			} else {
				console.log('Impossible to remove from Cart!');
			}
		},
		error: function(e) {console.log('error in removeGift(): ' + e);},
		content: {action : 'removeGift', idline : idline},
		sync: true
	});
}

function addGadget(idgadget,custom,setPhoto) {
	console.log('gadget id: '+idgadget);
	// get the quantity of the gift
	var num = dojo.byId('qtgadget['+idgadget+']').value;
	console.log('quantity of gadgets: '+num);
	// if the quantity is empty return false
	if(num == 0 || num == '') {
		return false;
	}
	
	if (document.getElementById('optiongadget['+idgadget+']').getAttribute('type') == 'hidden') {
		var option = dojo.byId('optiongadget['+idgadget+']').value;
	} else {
		var option = dijit.byId('optiongadget['+idgadget+']').attr('value');
	}
	console.log('option of the gadget: '+option);
	
	
	// if the gift is custom(=1) the setPhoto must be != 0
	if(custom == 1) {
		// verify that the setPhoto is diferent than 0
		if(setPhoto == 0) {
			return false;
		} else {
			var idphoto = setPhoto;
			console.log('idphoto set for the gadgets: ' + idphoto);
		}				
	} else {
		var idphoto = setPhoto;
		console.log('idphoto set for the gadgets: ' + idphoto);
	}
	
	dojo.xhrPost({
		url: 'order.backend.php',
		load: function(data) {
			if(data == 1) {
				// show the popconsole.log
				giftAnim("popAlertGood",accsAdded);
				// call the update cart
				successMsg();
			} else {
				console.log('Impossible to add gadget to your order.');
			}		
		},
		error: function(e) {console.log('error in addGadget(): '+e);},
		content: {action : 'addGadget', figadget : idgadget, dtcount : num, option : option, fiphoto : idphoto},
		sync: true
	});
	
}

function removeGadget(idline) {
	//console.log('the idline to remove: ' + idline);
	dojo.xhrPost({
		url: 'order.backend.php',
		load: function(data) {
			if(data == 1) {
				loader.loadModule_checkout();
			} else {
				console.log('Impossible to remove from Cart!');
			}
		},
		error: function(e) {console.log('error in removeGadget(): ' + e);},
		content: {action : 'removeGadget', idline : idline},
		sync: true
	});
}

function removeGreeting(idline) {
	//console.log('the idline to remove: ' + idline);
	dojo.xhrPost({
		url: 'order.backend.php',
		load: function(data) {
			if(data == 1) {
				loader.loadModule_checkout();
			} else {
				console.log('Impossible to remove from Cart!');
			}
		},
		error: function(e) {console.log('error in removeGreet(): ' + e);},
		content: {action : 'removeGreeting', idline : idline},
		sync: true
	});
}

function listPhotos(val, ID) {
	var a = dojo.query("."+ID);
	
	dojo.forEach(a, function(x) {
		if(dojo.hasClass(x, "photosList")) {
			dojo.removeClass(x, "photosList");
			dojo.addClass(x, "photosListShow");
		} else {
			dojo.removeClass(x, "photosListShow");
			dojo.addClass(x, "photosList");
		}
	});
}

function removePhotos(idline) {
	console.log('the idline that you want to remove from the photos is :'+idline);
	dojo.xhrPost({
		url: 'photo.backend.php',
		load: function(data) {
			if(data == 1) {
				loader.loadModule_checkout();
			} else {
				console.log('We can not remove your photo.');
			}
		},
		error: function(e) {console.log('error in removePhotos(): '+e);},
		content: {action : 'removePhotosList', idline : idline},
		sync: true
	});
}

function listPhotosAlbums(val, ID) {
	var a = dojo.query("."+ID);
	
	dojo.forEach(a, function(x) {
		if(dojo.hasClass(x, "photosAlbumList")) {
			dojo.removeClass(x, "photosAlbumList");
			dojo.addClass(x, "photosAlbumListShow");
		} else {
			dojo.removeClass(x, "photosAlbumListShow");
			dojo.addClass(x, "photosAlbumList");
		}
	});
}

function removePhotosAlbums(idline) {
	console.log('the idline that you want to remove from the photosalbums is :'+idline);
	dojo.xhrPost({
		url: 'photo.backend.php',
		load: function(data) {
			if(data == 1) {
				loader.loadModule_checkout();
			} else {
				console.log('We can not remove your photo.');
			}
		},
		error: function(e) {console.log('error in removePhotosAlbums(): '+e);},
		content: {action : 'removePhotosAlbums', idline : idline},
		sync: true
	});
}

function removePoster(idline) {
	//console.log('the idline to remove: ' + idline);
	console.log('remove poster with idline: '+idline);
	
	/*dojo.xhrPost({
		url: 'order.backend.php',
		load: function(data) {
			if (data == 1) {
				loader.loadModule_checkout();
			} else {
				console.log('Impossible to remove from Cart!');
			}
		},
		error: function(e) {console.log('error in removeGift(): ' + e);},
		content: {action : 'removeGift', idline : idline},
		sync: true
	});*/
}

function editCheckoutAddress(idaddress,who) {
	var t = dojo.byId('form');
	if (!t) {
		var cp = dijit.byId('addNewAddresses');
		var link = dojo.connect(cp, 'onDownloadEnd', function() {
			editCheckoutAddress(idaddress,who);
			dojo.disconnect(link);
		});
		cp.refresh();
		return;
	}
	
	console.log('the id of the address: '+idaddress);
	
	dojo.xhrPost({
		handleAs:'json',
		url: 'customer.backend.php',
		load: function(data) {
			console.log(data.dtlabel);
			if(who == 'noguest') {
				t.dtlabel.value = data.dtlabel;
			}
			if(who == 'guest') {
				t.dtemail2.value = data.dtemail;
			}
			t.dtsendto.value = data.dtsendto;
			t.dtaddress.value = data.dtaddress;
			t.dtzip.value = data.dtzip;
			t.dtlocal.value = data.dtlocal;
			if (data.dttel)
				t.dttel.value = data.dttel;
			
			if (dojo.byId('idaddress'))
				dojo.byId('idaddress').value = idaddress;
			
			dijit.byId('dtcountry').setDisplayedValue(data.dtcountry);
			dijit.byId('orderaddress').show();	
		},
		error: function(e) {console.log('error in editCheckoutAddress ' + e);},
		content: {action : 'alterCheckoutAddress', idaddress : idaddress},
		sync: true
	});
}

function addOrderAddress(iduser,idaddress) {

	if(dojo.byId('dtlabel')) {
		var label   = dojo.byId('dtlabel').value;
	} else {
		var label = 'notempty';
	}
	
	if(dojo.byId('dtemail2')) {
		var email = dojo.byId('dtemail2').value;
	} else {
		var email = 'noneed';
	}
	var country = dojo.byId('idaddress').value;
	var sendto  = dojo.byId('dtsendto').value;
	var address = dojo.byId('dtaddress').value;
	var tel 	= (dojo.byId('dttel'))?dojo.byId('dttel').value:'';
	var zip     = dojo.byId('dtzip').value;
	var local   = dojo.byId('dtlocal').value;
	var country = dojo.byId('dtcountry').value;
	
	if(label.length == 0 ||
	   sendto.length == 0 ||
	   address.length == 0 ||
	   zip.length == 0 ||
	   local.length == 0 ||
	   (tel.length == 0 && dojo.byId('dttel')) ||
	   email.length == 0)
	{
		giftAnim("popAlertBad",mustEnterValues);
		return false;
	}
	
	console.log('ici');
	console.log(country);
	
	dojo.xhrPost({
		url:'order.backend.php',
		load: function(data) {
			if(data == 1) {
				setSessionDestination(country);
				loader.loadModule_checkout();
			}
		},
		error: function(e) {console.log('error in addOrderAddress: ' + e);},
		form:'form',
		content:{action:'addOrderAddress', iduser: iduser, idaddress: idaddress, dtemail : email},
		sync:true
	});
	
	dijit.byId('orderaddress').hide();
}

function updateAddressCheckout(value)
{
	console.log(value);
	if(value == '0') {
		giftAnim("popAlertBad",haveSelectAddress);
		return false;
	}
	
	// url: 'customer.backend.php',
	
	dojo.xhrPost({
		url: 'order.backend.php',
		load: function(data) {
			if(data != 0)
			{
				setSessionDestination(data);
				loader.loadModule_checkout();
			}
		},
		error: function(e) {console.log('error in updateAddressCheckout(): '+e);},
		content: {action : 'setAddressCheckout', idaddress : value},
		sync: true
	});
}

function setSessionDestination(country)
{
	console.log('trigered the function setSessionDestination()');
	var dest = country;
	
	console.log(country);
	
	dojo.xhrPost({
		url:'order.backend.php',
		load:function(data) {
			if(data == 1){
				return true;
			} else {
				console.log('impossible to setSessionDestination');
				return false;
			}
		},
		error: function(e){console.log('error in setSessionDestination(): '+e);},
		content:{action : 'setSessionDestination', dest : dest},
		sync:true
	});
}

function finishCheckout() {
	var request = {action:'verifyCheckout'};
	dojo.xhrPost({
		url:'order.backend.php',
		load:function(data) {
			//dijit.byId('checkoutDialog').show();
			//window.location.href = '?action=checkout';
			loader.loadModule_album('?action=checkout');
		},
		error:function(e) {console.log('error in finishCheckout() '+e);},
		content:request,
		sync:true
	});
}

function proceedToPayment(id) {
    
	/* 
	checkout();
	return;
	*/

	dojo.xhrPost({
		url: 'payment.class.php',
		load: function(data) {
			if(data != 0) {
				dojo.byId('content').innerHTML = data;
				if (dojo.byId('balance')) {
					if (confirm(Payment_Use_Balance)) {
						dojo.byId('balance').checked = true;
						useBalance(dojo.byId('balance'));
					}
				}
			} else {
				console.log('Impossible to load payment!');
			}
		},
		error: function(e) {console.log('error in checkout: '+e)},
		content: {action:'selectPayment',orderid:id},
		sync: true
	});
}

function checkout() {
	var result = confirm(confirmOrderFinal);
	if(result == true) {
		dojo.xhrPost({
			url: 'order.backend.php',
			load: function(data) {
				dojo.byId('content').innerHTML = data;
				/* JEDI
				if(data != 0) {
					
				} else {
					console.log('Impossible to close your order!');
				}
				*/
			},
			error: function(e) {console.log('error in checkout: '+e)},
			content: {action:'finishCheckout'},
			sync: true
		});
		//rightmenu.start(1,1,0,0,0,0,0);
		//successMsg();
	}
}

function loggingIn() {
	dojo.byId('login').style.display = 'block';
}

function signIn() {
	dojo.byId('signin').style.display = 'block';
	dojo.parser.parse(dojo.byId('albn_content'));
}

function finishCheckout() {
	dojo.byId('finish').style.display = 'block';
}

function saveOrderSettings(link) {
	//try to block page reload and block (seems only working in IE)
	window.history.forward(1);
	dojo.connect(document, 'onkeydown', document, function(e) {
		switch (e.keyCode) {
			case 116 : 	
				alert('F5');
				e.returnValue = false;
				e.keyCode = 0;
				window.status = 'We have disabled F5';
				dojo.stopEvent(e);
				break;
		}
	});

	//loading Mask
	var loadingMask = document.createElement('div');
	loadingMask.id = 'loadingMask';
	var loadingImage = document.createElement('div');
	loadingImage.id = 'loadingImage';
	loadingMask.appendChild(loadingImage);
	var loadingText = document.createElement('div');
	loadingText.id = 'loadingText';
	loadingMask.appendChild(loadingText);
	
	dojo.xhrPost({
		url: 'photo.backend.php',
		load: function(data) {
			loadingText.innerHTML = data;
		},
		error: function(e) {console.log('error: '+e)},
		content: {action:'getHTMLCode',page:'payment',position:'loading'},
		sync: true
	});
	
	document.body.appendChild(loadingMask);

	if (document.getElementById('balance')) {
		var useBalance = document.getElementById('balance').checked+'';
	} else {
		var useBalance = false;
	}
	if (document.getElementById('comments')) {
		var comments = document.getElementById('comments').value;
		dojo.xhrPost({
			url: 'order.backend.php',
			load: function(data) {
				console.log('saveOrderSettings: '+data);
			},
			error: function(e) {console.log('error in checkout: '+e)},
			content: {action:'saveOrderSettings',useBalance:useBalance,comments:comments},
			sync: true
		});
	}
	window.location.href = link;	
	
}

function useBalance(obj) {
	document.getElementById('payment').innerHTML = '';
	
	var bc = document.getElementById('bc');
	if (obj.checked == true) {
		bc.style.display = 'block';
	} else {
		bc.style.display = 'none';
	};
	
	dojo.xhrPost({
		url: 'payment.class.php',
		load: function(data) {
			document.getElementById('payment').innerHTML = data;
		},
		error: function(e) {console.log('error in checkout: '+e)},
		content: {action:'getpaymentmethods',useBalance:obj.checked+''},
		sync: true
	});

}

function removeProAccount(user)
{
	console.log('triggered the removeProAccount()');
	
	dojo.xhrPost({
		url:'order.backend.php',
		load: function(data) {
			if(data == 1)
			{
				loader.loadModule_checkout();
			}
		},
		error: function(e) {console.log('error in removeProAccount(): '+e);},
		content:{action : 'removeProAccount', iduser : user},
		sync:true
	});
}

