﻿/************************************************************************************************************
(C) www.dhtmlgoodies.com, March 2006

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Version:
	1.0	Released	March. 3rd 2006

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/

//document.writeln('nom : '++' version : '+);
var version = navigator.appVersion ;
var reg1 = new RegExp("MSIE 7.0","g");
var flyingSpeed = 150 ;
var url_addProductToBasket = 'addProduct.php';
var url_addProductToBasket2 = 'addProduct2.php' ;
var url_removeProductFromBasket = 'removeProduct.php';
var txt_totalPrice = 'Total: ';
var langue = Cookie.read("langue");


var shopping_cart_div = false;
var flyingDiv = false;
var currentProductDiv = false;

var shopping_cart_x = false;
var shopping_cart_y = false;

var slide_xFactor = false;
var slide_yFactor = false;

var diffX = false;
var diffY = false;

var currentXPos = false;
var currentYPos = false;

var ajaxObjects = new Array();


function shoppingCart_getTopPos(inputObj){		
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

function shoppingCart_getLeftPos(inputObj){
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}
	

function addToBasket(productId, type){
	
	if (!type) {
	   type = '0';
	}
	
	if(!shopping_cart_div)shopping_cart_div = document.getElementById('shopping_cart');
	if(!flyingDiv){
		flyingDiv = document.createElement('DIV');
		flyingDiv.style.position = 'absolute';
		document.body.appendChild(flyingDiv);
	}
	
	shopping_cart_x = shoppingCart_getLeftPos(shopping_cart_div);
	shopping_cart_y = shoppingCart_getTopPos(shopping_cart_div);

	currentProductDiv = document.getElementById('slidingProduct' + productId);
	currentProductDiv.style.display = 'block';
	
	currentXPos = shoppingCart_getLeftPos(currentProductDiv);
	currentYPos = shoppingCart_getTopPos(currentProductDiv);
	
	diffX = shopping_cart_x - currentXPos;
	diffY = shopping_cart_y - currentYPos;
	
	
	var shoppingContentCopy = currentProductDiv.cloneNode(true);
	shoppingContentCopy.id='';
	flyingDiv.innerHTML = '';
	flyingDiv.style.left = currentXPos + 'px';
	flyingDiv.style.top = currentYPos + 'px';
	flyingDiv.appendChild(shoppingContentCopy);
	flyingDiv.style.display='block';
	flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
	flyToBasket(productId, type);
	currentProductDiv.style.display = 'none';	
}

function flyToBasket(productId, type){
	if (!type) {
	   var type = '0';
	}

	var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
	var moveX = (diffX / maxDiff) * flyingSpeed;;
	var moveY = (diffY / maxDiff) * flyingSpeed;	
	
	currentXPos = currentXPos + moveX;
	currentYPos = currentYPos + moveY;
	
	flyingDiv.style.left = Math.round(currentXPos) + 'px';
	flyingDiv.style.top = Math.round(currentYPos) + 'px';	
	
	
	if(moveX>0 && currentXPos > shopping_cart_x){
		flyingDiv.style.display='none';		
	}
	if(moveX<0 && currentXPos < shopping_cart_x){
		flyingDiv.style.display='none';		
	}
		
	if (flyingDiv.style.display == 'block'){
		setTimeout('flyToBasket("' + productId + '", "'+type+'")',10); 
	}else {
		ajaxAddProduct(productId, type);	
	}
}

function ajaxAddProduct(productId, type, catID){
	/* ajout pour récupérer les cractéristiques et la quantité */
	//validationEngine();
	var maliste = '' ;
	var caracteristiques = document.getElementById('listes').value;
	var montableaudecaracteristiques=caracteristiques.split(',');
	var x;
	for (x=1; x<montableaudecaracteristiques.length; x++) {
		var caractvalid = document.getElementById('lst_'+montableaudecaracteristiques[x]).options[document.getElementById('lst_'+montableaudecaracteristiques[x]).selectedIndex].value ;
		maliste += '&lst_'+montableaudecaracteristiques[x]+'='+caractvalid;
	}
	
	if(type != '1' && type != '2') {
		
		var elems = new Array("lst_changediam_weight", "lst_changediam_color", "lst_changediam_clarity", "lst_changediam_cut");
		for(x=0; x<elems.length;x++) {
			if(document.getElementById(elems[x]))
			maliste += '&'+elems[x]+'='+document.getElementById(elems[x]).options[document.getElementById(elems[x]).selectedIndex].value;
		}
		
	}
	
	var qte = document.getElementById('qte').value;
	if (document.getElementById('saisie'))
		var saisie = encodeURIComponent(document.getElementById('saisie').value);
	else
		var saisie = '' ;
	/* ajout pour récupérer les cractéristiques et la quantité */

	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	
	var reg = new RegExp("(&)", "g");
	saisie = saisie.replace(reg,"etcommercial");	
	//alert(encodeURI(url_addProductToBasket + '?productId=' + productId + '&saisie=' + saisie + '&qte=' + qte + maliste));
	//document.write(url_addProductToBasket + '?productId=' + productId + '&saisie=' + saisie + '&qte=' + qte + maliste);
	
	if (type == '1')
		ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket2 + '?artid=' + productId + '&action=monture&saisie=' + saisie + '&qte=' + qte + maliste; // Saving product in this file
		//ajaxObjects[ajaxIndex].requestFile = encodeURI(url_addProductToBasket2 + '?productId=' + productId + '&saisie=' + saisie + '&qte=' + qte + maliste); // Saving product in this file
	else if (type == '2')
		ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket2 + '?artid=' + productId + '&action=diamant'; // Saving product in this file
		//ajaxObjects[ajaxIndex].requestFile = encodeURI(url_addProductToBasket2 + '?productId=' + productId + '&saisie=' + saisie + '&qte=' + qte + maliste); // Saving product in this file
	else
		ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket + '?productId=' + productId + '&saisie=' + saisie + '&qte=' + qte + maliste; // Saving product in this file
//	alert(ajaxObjects[ajaxIndex].requestFile);
		//ajaxObjects[ajaxIndex].requestFile = encodeURI(url_addProductToBasket + '?productId=' + productId + '&saisie=' + saisie + '&qte=' + qte + maliste); // Saving product in this file	
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex, type,catID); };	// Specify function that will be executed after file has been found
	
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function

}

function showAjaxBasketContent(ajaxIndex, type,catID){
	// Getting a reference to the shopping cart items table
	var itemBox = document.getElementById('shopping_cart_items');
	
	var resultat = ajaxObjects[ajaxIndex].response;	// Breaking response from Ajax into tokens
	
	//alert(resultat)
	
	if (type == '1') {
		myresultat = resultat.split('|||');
		
				
		if (myresultat[0] == 'OK'){
			if (myresultat[3]){
				//alert(myresultat[3]);
				window.location.replace("cde1.php");
			}else if (myresultat[1] == 'OUI'){
//				window.location.replace("detail-du-montage-"+myresultat[3]+".htm");
				window.location.replace("rechercher-un-diamant.htm");
			}else if (myresultat[1] == 'NON'){
				// A product with this id is allready in the basket - just add number items
				//window.location.replace("cde1.php");
				if (langue=="FR") {
					alert('Un article identique existe déjà dans votre panier, la quantité a été augmentée.');
				}else{
					alert('An identical item already exists in your cart, the quantity was increased.');
				}
			}else if (myresultat[1] == 'STOCK'){
				if (langue=="FR") {
					alert('Le stock n est pas assez important pour honorer votre commande.');	
				}else{
					alert('Not enough stock to fulfill your order.');
				}
			}
			
			if (myresultat[2] != ''){
				document.getElementById('montantpanier').value = myresultat[2] ;
			}
			
			//alert('Article ajouté au panier.');
		}else if (myresultat[1] == 'caractval0') {
			
			if (langue=="FR") {
				//alert('Vous devez sélectionner les caractéristiques de votre bijou avant de le commander.');
			}else{
				//alert('You must select characteristics of your jewelry before ordering it.');
			}
		}else {
			if (langue=="FR") {
				alert('Une erreur s\'est produite pendant l\'ajout au panier.');
			}else{
				alert('An error occurred while adding to cart.');
			}
		} 
	}else if (type == '2') {
		myresultat = resultat.split('|||');
		if (myresultat[0] == 'OK'){
			if (myresultat[1] == 'OUI'){
				
				if(myresultat[3])
				{
				window.location.replace("cde1.php");
				}
				else if(catID==4 || catID==5)
				{
					window.location.replace("cde1.php");
				}
				else
				{
				window.location.replace("rechercher-un-diamant.htm");
				}
			}else if (myresultat[1] == 'NON'){
				// A product with this id is allready in the basket - just add number items
				//window.location.replace("cde1.php");
				if (langue=="FR") {
					alert('Un Diamant identique existe déjà dans votre panier.');
				}else{
					alert('This Diamond already exists in your basket.');
				}
			}else if (myresultat[1] == 'STOCK'){
				if (langue=="FR") {
					alert('Le stock n est pas assez important pour honorer votre commande.');	
				}else{
					alert('Not enough stock to fulfill your order.');
				}
			}
			
			if (myresultat[2] != ''){
				document.getElementById('montantpanier').value = myresultat[2] ;
			}
			
			//alert('Article ajouté au panier.');
		}else if (myresultat[1] == 'caractval0') {
			
			if (langue=="FR") {
			//	alert('2 Vous devez sélectionner les caractéristiques de votre bijou avant de le commander.');
			}else{
				//alert('You must select characteristics of your jewelry before ordering it.');
			}
		}else {
			if (langue=="FR") {
				alert('Une erreur s\'est produite pendant l\'ajout au panier.');
			}else{
				alert('An error occurred while adding to cart.');
			}
		} 
	}else{
		myresultat = resultat.split('|||');
		if (myresultat[0] == 'OK'){
			if (myresultat[1] == 'OUI'){
				window.location.replace("cde1.php");
			}else if (myresultat[1] == 'NON'){
				// A product with this id is allready in the basket - just add number items
				//window.location.replace("cde1.php");
				if (langue=="FR") {
					alert('Un article identique existe déjà dans votre panier, la quantité a été augmentée.');
				}else{
					alert('An identical item already exists in your cart, the quantity was increased.');
				}
			}else if (myresultat[1] == 'STOCK'){
				if (langue=="FR") {
					alert('Le stock n est pas assez important pour honorer votre commande.');	
				}else{
					alert('Not enough stock to fulfill your order.');
				}	
			}else{
				if (langue=="FR") {
					alert('Une erreur s\'est produite pendant l\'ajout au panier.');
				}else{
					alert('An error occurred while adding to cart.');
				}
			}
			if (myresultat[2] != ''){
				document.getElementById('montantpanier').value = myresultat[2] ;
			}
			//alert('Article ajouté au panier.');
		}else if (myresultat[1] == 'caractval0') {
			
			if (langue=="FR") {
				//alert('Vous devez sélectionner les caractéristiques de votre bijou avant de le commander.');
			}else{
			//	alert('You must select characteristics of your jewelry before ordering it.');
			}
		}else {
			if (langue=="FR") {
				alert('Une erreur s\'est produite pendant l\'ajout au panier.');
			}else{
				alert('An error occurred while adding to cart.');
			}
		} 
	}
	ajaxObjects[ajaxIndex] = false;		
}


//////////////////////////////////////////////////////////////////////////////////////////////

function addToBasket2(productId, type, cat2){
	
	if (!type) {
	   type = '0';
	}
	
	if(!shopping_cart_div)shopping_cart_div = document.getElementById('shopping_cart');
	if(!flyingDiv){
		flyingDiv = document.createElement('DIV');
		flyingDiv.style.position = 'absolute';
		document.body.appendChild(flyingDiv);
	}
	
	shopping_cart_x = shoppingCart_getLeftPos(shopping_cart_div);
	shopping_cart_y = shoppingCart_getTopPos(shopping_cart_div);

	currentProductDiv = document.getElementById('slidingProduct' + productId);
	currentProductDiv.style.display = 'block';
	
	currentXPos = shoppingCart_getLeftPos(currentProductDiv);
	currentYPos = shoppingCart_getTopPos(currentProductDiv);
	
	diffX = shopping_cart_x - currentXPos;
	diffY = shopping_cart_y - currentYPos;
	
	
	var shoppingContentCopy = currentProductDiv.cloneNode(true);
	shoppingContentCopy.id='';
	flyingDiv.innerHTML = '';
	flyingDiv.style.left = currentXPos + 'px';
	flyingDiv.style.top = currentYPos + 'px';
	flyingDiv.appendChild(shoppingContentCopy);
	flyingDiv.style.display='block';
	flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
	flyToBasket2(productId, type, cat2);
	currentProductDiv.style.display = 'none';	
}


function flyToBasket2(productId, type, cat2){
	
	if (!type) {
	   var type = '0';
	}

	var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
	var moveX = (diffX / maxDiff) * flyingSpeed;;
	var moveY = (diffY / maxDiff) * flyingSpeed;	
	
	currentXPos = currentXPos + moveX;
	currentYPos = currentYPos + moveY;
	
	flyingDiv.style.left = Math.round(currentXPos) + 'px';
	flyingDiv.style.top = Math.round(currentYPos) + 'px';	
	
	
	if(moveX>0 && currentXPos > shopping_cart_x){
		flyingDiv.style.display='none';		
	}
	if(moveX<0 && currentXPos < shopping_cart_x){
		flyingDiv.style.display='none';		
	}
		
	if (flyingDiv.style.display == 'block'){
		setTimeout('flyToBasket2("' + productId + '", "'+type+'" , "'+cat2+'")',10); 
	}else {
		ajaxAddProduct2(productId, type, cat2);	
	}
	
}

function ajaxAddProduct2(productId, type, cat2){
	
	/* ajout pour récupérer les cractéristiques et la quantité */
	var maliste = '' ;
	var caracteristiques = document.getElementById('listes').value;
	var montableaudecaracteristiques=caracteristiques.split(',');
	var x;
	for (x=1; x<montableaudecaracteristiques.length; x++) {
		var caractvalid = document.getElementById('lst_'+montableaudecaracteristiques[x]).options[document.getElementById('lst_'+montableaudecaracteristiques[x]).selectedIndex].value ;
		maliste += '&lst_'+montableaudecaracteristiques[x]+'='+caractvalid;
	}
	var qte = document.getElementById('qte').value;
	if (document.getElementById('saisie'))
		var saisie = encodeURIComponent(document.getElementById('saisie').value);
	else
		var saisie = '' ;
	/* ajout pour récupérer les cractéristiques et la quantité */

	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	
	var reg = new RegExp("(&)", "g");
	saisie = saisie.replace(reg,"etcommercial");	
	//alert(encodeURI(url_addProductToBasket + '?productId=' + productId + '&saisie=' + saisie + '&qte=' + qte + maliste));
	//document.write(url_addProductToBasket + '?productId=' + productId + '&saisie=' + saisie + '&qte=' + qte + maliste);
	
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket2 + '?artid=' + productId + '&action=diamant'; // Saving product in this file
	
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent2(ajaxIndex, type, cat2); };	// Specify function that will be executed after file has been found
	
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

function showAjaxBasketContent2(ajaxIndex, type, cat2){
	
	// Getting a reference to the shopping cart items table
	var itemBox = document.getElementById('shopping_cart_items');
	
	var resultat = ajaxObjects[ajaxIndex].response;	// Breaking response from Ajax into tokens
	
	myresultat = resultat.split('|||');
	
	if (myresultat[0] == 'OK'){
		if (myresultat[1] == 'OUI'){
			
			if(cat2=="0")
				window.location.replace("cat2,4,creer-votre-bague.htm");
			if(cat2=="1")
				window.location.replace("cat2,5,creer-votre-collier.htm");
			if(cat2=="2")
				window.location.replace("cat2,6,creer-vos-boucles-d-oreilles.htm");
			if(cat2=="3")
				window.location.replace("cde1.php");
				
		}else if (myresultat[1] == 'NON'){
			// A product with this id is allready in the basket - just add number items
			//window.location.replace("cde1.php");
			if(type== '2') {
				if (langue=="FR") {
					alert('Un Diamant identique existe déjà dans votre panier.');
				}else{
					alert('This Diamond already exists in your basket.');
				}
			}else{
				if (langue=="FR") {
					alert('Un article identique existe déjà dans votre panier, la quantité a été augmentée.');
				}else{
					alert('An identical item already exists in your cart, the quantity was increased.');
				}
			}
		}else if (myresultat[1] == 'STOCK'){
			if (langue=="FR") {
				alert('Le stock n est pas assez important pour honorer votre commande.');	
			}else{
				alert('Not enough stock to fulfill your order.');
			}	
		}
		
		if (myresultat[2] != ''){
			document.getElementById('montantpanier').value = myresultat[2] ;
		}
		
		//alert('Article ajouté au panier.');
	}else if (myresultat[1] == 'caractval0') {
		
		if (langue=="FR") {
			//alert('Vous devez sélectionner les caractéristiques de votre bijou avant de le commander.');
		}else{
			//alert('You must select characteristics of your jewelry before ordering it.');
		}
	}else {
		if (langue=="FR") {
			alert('Une erreur s\'est produite pendant l\'ajout au panier.');
		}else{
			alert('An error occurred while adding to cart.');
		}
	} 
	ajaxObjects[ajaxIndex] = false;		
}

