// AJAX

function getXhr_ajax(){
                                var xhr = null; 
				if(window.XMLHttpRequest) // Firefox et autres
				   xhr = new XMLHttpRequest(); 
				else if(window.ActiveXObject){ // Internet Explorer 
				   try {
			                xhr = new ActiveXObject("Msxml2.XMLHTTP");
			            } catch (e) {
			                xhr = new ActiveXObject("Microsoft.XMLHTTP");
			            }
				}
				else { // XMLHttpRequest non supporté par le navigateur 
				   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
				   xhr = false; 
				} 
                                return xhr;
			}



function ajax_update_select(id_product, name_select, sid){
				var xhr = getXhr_ajax();
				// On défini ce qu'on va faire quand on aura la réponse
				xhr.onreadystatechange = function(){
					// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
					if(xhr.readyState == 4 && xhr.status == 200){var nouveau_href = xhr.responseText;
						// On se sert de innerHTML pour rajouter les options a la liste
						nouveau_href = xhr.responseText;
						document.getElementById('a_href_article_'+id_product).innerHTML = nouveau_href;
					}
				}

				// Ici on va voir comment faire du post
				xhr.open("POST","update_session.php",true);
				// ne pas oublier ça pour le post
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				// ne pas oublier de poster les arguments
				sel = document.getElementById('product_'+id_product+'_'+name_select);
				option_update = sel.options[sel.selectedIndex].value; // homme p-ex
				//        sexe        =   homme
				xhr.send(name_select+"="+option_update+"&id_p="+id_product+"&sid="+sid);
			}

function ajax_update_select_new(id_product, name_select, desc, src_img, tablurlbigf, tablurlbigm){ // update des var de sessions
				var xhr = getXhr_ajax();
				// On défini ce qu'on va faire quand on aura la réponse
				xhr.onreadystatechange = function(){
					// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
					if(xhr.readyState == 4 && xhr.status == 200){var nouveau_href = xhr.responseText;
						// On se sert de innerHTML pour rajouter les options a la liste
							update_sticker(id_product);
						nouveau_href = xhr.responseText;
						if (nouveau_href != 'noupdate') {
							document.getElementById('product_g_id_'+id_product).innerHTML = nouveau_href;
						}
					}
				}

				// Ici on va voir comment faire du post
				xhr.open("POST","update_session.php",true);
				// ne pas oublier ça pour le post
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				// ne pas oublier de poster les arguments
				sel = document.getElementById('product_'+id_product+'_'+name_select);
				option_update = sel.options[sel.selectedIndex].value; // homme p-ex				
				//        sexe        =   homme				
				
				xhr.send(name_select+"="+option_update+"&id_p="+id_product+"&desc="+desc+"&src="+src_img);
				
				if (name_select == 'Sexe') {
					if (option_update == 'Femme') {
  					document.getElementById('a_loupe_'+id_product).href = vm_image_path+tablurlbigf[id_product];
					} else {
  					document.getElementById('a_loupe_'+id_product).href = vm_image_path+tablurlbigm[id_product];
					}
				}
				
			}
			
			

function update_a_href(id_product, name_select){ // solution de rechange
	// non terminée => utiliser JS pour updater le href d'ajout dans le caddie.
	// nouvelle solution => server_side
	
				sel = document.getElementById('product_'+id_product+'_'+name_select);
				option_update = sel.options[sel.selectedIndex].value;
				
	output = '<a href = "http://127.0.0.1/qoqa/templates/qshirts/plus.php?article='+id_product+'&'+name_select+'='+option_update+'"><img src = "templates/qshirts/images/empty4343.gif" border = 0 tag = "Ajouter au caddie"></a>';
	document.getElementById('a_href_article_'+id_product).innerHTML = output;
			}
			
function update_caddie_index_old(prix, nombre_total) { // old: static
	//alert(prix+'CHF / '+nombre_total);
	var pluriel = '';
	if (nombre_total > 1) {
		pluriel = 's';
	}		
	var nombre_total_text = '<b>'+nombre_total+'</b> produit'+pluriel+' dans le panier';
	var prix_text = prix+'.-';
	
	document.getElementById('price_index_caddie').innerHTML = prix_text;
	document.getElementById('articles_index_caddie').innerHTML = nombre_total_text;
}

function update_caddie_index() { // updated: dynamic
var xhr = getXhr_ajax();
				xhr.onreadystatechange = function(){
					if(xhr.readyState == 4 && xhr.status == 200){
						prixarticle = xhr.responseText;
						arrayprixarticle = prixarticle.split('/');
						prix = arrayprixarticle[0];
						nombre_total = arrayprixarticle[1];
						
						
						var pluriel = '';
						if (nombre_total > 1) {
							pluriel = 's';
						}		
						var nombre_total_text = '<b>'+nombre_total+'</b> produit'+pluriel+' dans le panier';
						var prix_text = prix+'.-';
						
						document.getElementById('price_index_caddie').innerHTML = prix_text;
						document.getElementById('articles_index_caddie').innerHTML = nombre_total_text;
						
					}
				}

				xhr.open("POST","update_caddie.php",true);
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				xhr.send('check=b944');
}


function update_login() { // updated: dynamic
var xhr = getXhr_ajax();
				xhr.onreadystatechange = function(){
					if(xhr.readyState == 4 && xhr.status == 200){
						logrep = xhr.responseText;
						if (logrep!='') {
						document.getElementById('top_left_bt3').innerHTML = logrep;						
						}
					}
				}

				xhr.open("POST","update_login.php",true);
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				xhr.send('check=b944');
}


function saveScrollPositions(theForm) {
if(theForm) {
	var scrolly = typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement.scrollTop;
	var scrollx = typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement.scrollLeft;
	theForm.scrollx.value = scrollx;
	theForm.scrolly.value = scrolly;
	}
}

function formpost(formID, command, id_article) {
	var formieux = document.getElementById(formID)  	
  saveScrollPositions(formieux);
  if (command == '1') {
  	formieux.addone.value = id_article;
	} else {
  	formieux.minusone.value = id_article;
  }
  formieux.submit();
}



// --------------------------------------------------------------------------------------------------------
function updateSex(attr, tabl, tabldesc, tableurlhomme, tableurlfemme, tableurlhommebig, tableurlfemmebig){	
  tablurl = new Array;
  	if (attr=='Homme') {
  		tablurl = tableurlhomme;
  		tablurlbig = tableurlhommebig;
  		var to_select = 0;
  	} else {
  		tablurl = tableurlfemme;
  		tablurlbig = tableurlfemmebig;
  		var to_select = 1;
  	}
  		
  for(i=0;i<tabl.length;i++) {
  	//new_div = 'betatest';
  	//document.getElementById('product_g_id_'+tabl[i]).innerHTML = new_div;
  	var id_product = tabl[i];  	
  		document.getElementById('product_g_img_'+tabl[i]).src = vm_image_path+tablurl[id_product];  	
  		var nom_form = 'form_product_'+tabl[i];
  		var nom_select = 'product_'+tabl[i]+'_Sexe';
  		//document.nom_form.nom_select.selectedIndex = to_select;
  		document.getElementById(nom_select).selectedIndex = to_select;
  		
  		// loupe
  		document.getElementById('a_loupe_'+tabl[i]).href = vm_image_path+tablurlbig[id_product];
  		
  		// update var session + image de gauche
  		updateItem(tabl[i], attr) // update de Sexe=
  		updateattr2sex(tabl[i], 'Sexe', attr, 'Taille'); // Update de Taille=
  }
}

function updateattr2sex(id_item, a1, a2, cle_next) {
	var xhr = getXhr_ajax();
	
				xhr.onreadystatechange = function(){
					if(xhr.readyState == 4 && xhr.status == 200){
						logrep = xhr.responseText;
						//alert(logrep);
						if (logrep == 'nothing') {
							generatedselect = '<span style = "color: red;">indisponible</span>';
							//alert('nothing');
						} else {
						arep = logrep.split('*');						
						generatedselect = arep[0];
						syst_update = arep[1];
						
						//alert(syst_update);
						s_arr = syst_update.split('=');
						s1 = s_arr[0];
						s2 = s_arr[1];
						
						updateSessionSelected(id_item, s1, s2);
						}
						
						document.getElementById('level2_'+id_item).innerHTML = generatedselect;
						
						//update_sticker(id_item);
					}
				}
	
		
				
		xhr.open("POST","update_sexe.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send('id_p='+id_item+'&'+a1+'='+a2+'&nextkey='+cle_next);				
	
}
// --------------------------------------------------------------------------------------------------------







function updateItem(id_item, attr) {	
var xhr = getXhr_ajax();
				xhr.onreadystatechange = function(){
					if(xhr.readyState == 4 && xhr.status == 200){
						//logrep = xhr.responseText;
						//document.getElementById('top_left_bt3').innerHTML = logrep;			
							update_sticker(id_item);			
						
					}
				}

				xhr.open("POST","update_session.php",true);
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				xhr.send('id_p='+id_item+'&Sexe='+attr);
}

function updateattr2(id_item, selectedselect, cle_appel, cle_next) {
	var xhr = getXhr_ajax();
	
				xhr.onreadystatechange = function(){
					if(xhr.readyState == 4 && xhr.status == 200){
						logrep = xhr.responseText;
						//alert(logrep);
						if (logrep == 'nothing') {
							generatedselect = '<span style = "color: red;">indisponible</span>';
							//alert('nothing');
						} else {
						arep = logrep.split('*');						
						generatedselect = arep[0];
						syst_update = arep[1];
						
						//alert(syst_update);
						s_arr = syst_update.split('=');
						s1 = s_arr[0];
						s2 = s_arr[1];
						
						updateSessionSelected(id_item, s1, s2);
						}
						
						document.getElementById('level2_'+id_item).innerHTML = generatedselect;
						
						
						
						//update_sticker(id_item);
					}
				}
	
  	var ss = document.getElementById(selectedselect);	
		var choiced = ss.options[ss.selectedIndex].value;
		
				
		xhr.open("POST","update_sexe.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send('id_p='+id_item+'&'+cle_appel+'='+choiced+'&nextkey='+cle_next);				
	
}

function update_sticker(id_item) {	//update le sticker en fct de $_SESSION
	//alert('update_sticker');
	var xhr = getXhr_ajax();	
				xhr.onreadystatechange = function(){
					if(xhr.readyState == 4 && xhr.status == 200){
						logrep = xhr.responseText;
						document.getElementById('stock_'+id_item).innerHTML = logrep;
					}
				}
	
		xhr.open("POST","update_sticker.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send('id_p='+id_item);				
	
}

function updateSessionSelected(id_item, s1, s2) {	
var xhr = getXhr_ajax();
				xhr.onreadystatechange = function(){
					if(xhr.readyState == 4 && xhr.status == 200){
						//logrep = xhr.responseText;
						//document.getElementById('top_left_bt3').innerHTML = logrep;			
							update_sticker(id_item);			
						
					}
				}

				xhr.open("POST","update_session.php",true);
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				xhr.send('id_p='+id_item+'&'+s1+'='+s2);
}



function coupon_sub() { // updated: dynamic
var xhr = getXhr_ajax();
				xhr.onreadystatechange = function() {
					if(xhr.readyState == 4 && xhr.status == 200){
						var t_price = document.getElementById('total_price').value;
						
						logrep = xhr.responseText;
						code_answer = parseFloat(logrep) + 0.0;
						//  -1 = inexistant, -2 = épuisé (déjà utilisé), -3 = réduction supérieure au montant total
						if (code_answer > 0) {
							document.getElementById('error_coupon').innerHTML = '<img src = "images/magic.png" height="16" width = "16" style = "margin-bottom: -4px;"><span style = "color: green;"> Merci, votre code coupon est <b>valable</b> !</span>';	
							document.getElementById('red_price').innerHTML = '-'+code_answer;	
							document.getElementById('t_p').innerHTML = t_price-code_answer;	
						
						} else if (code_answer == -1) {
							document.getElementById('error_coupon').innerHTML = '<img src = "images/error.png" width = "16" height = "16" style = "margin-bottom: -4px;"> Ce code coupon n\'existe pas !';
						} else if (code_answer == -2) {
							document.getElementById('error_coupon').innerHTML = '<img src = "images/error.png" width = "16" height = "16" style = "margin-bottom: -4px;"> Ce code coupon est &eacute;puis&eacute; !';
						} else if (code_answer == -3) {
							document.getElementById('error_coupon').innerHTML = '<img src = "images/error.png" width = "16" height = "16" style = "margin-bottom: -4px;"> Ce coupon poss&egrave;de un montant sup&eacute;rieur au total de l\'achat !';
						} else if (code_answer == -4) {
							document.getElementById('error_coupon').innerHTML = '<img src = "images/error.png" width = "16" height = "16" style = "margin-bottom: -4px;"> D&eacute;sol&eacute;, ce coupon n\'est pas valable pour cette vente !';
						} else {							
						}
					}
				}

				var numeroc = document.getElementById('coupon');
				
				if (numeroc.value.length > 3) {
				xhr.open("POST","coupon_redeem.php",true);
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				xhr.send('code='+numeroc.value);
				} 
				
}

function coupon_input() {
	var cp = document.getElementById('coupon');
	if (cp.value == '') {
														cp.style.backgroundImage="url('images/codecoupon.png')";
														cp.style.backgroundPosition = 'center center';
													} else {													
														cp.style.background='white';
													}						
}
