var sajax_request_type = "GET";
var sajax_target_id = "";
var sajax_failure_redirect = "";

function sajax_init_object() {
	var A;
	var msxmlhttp = new Array(
		'Msxml2.XMLHTTP.5.0',
		'Msxml2.XMLHTTP.4.0',
		'Msxml2.XMLHTTP.3.0',
		'Msxml2.XMLHTTP',
		'Microsoft.XMLHTTP');
	for (var i = 0; i < msxmlhttp.length; i++) {
		try {
			A = new ActiveXObject(msxmlhttp[i]);
		} catch (e) {
			A = null;
		}
	}

	if(!A && typeof XMLHttpRequest != "undefined")
		A = new XMLHttpRequest();
	return A;
}

var sajax_requests = new Array();

function sajax_cancel() {
	for (var i = 0; i < sajax_requests.length; i++)
		sajax_requests[i].abort();
}

function sajax_do_call(func_name, args) {
	var i, x, n;
	var uri;
	var post_data;
	var target_id;
	
	target_id = sajax_target_id;
	if (typeof(sajax_request_type) == "undefined" || sajax_request_type == "")
		sajax_request_type = "GET";
	
	uri = "modules/boutique/include/real_ajax_options_produit.php";
		
	if (sajax_request_type == "GET") {

		if (uri.indexOf("?") == -1)
			uri += "?rs=" + escape(func_name);
		else
			uri += "&rs=" + escape(func_name);
		uri += "&rst=" + escape(sajax_target_id);
		uri += "&rsrnd=" + new Date().getTime();

		for (i = 0; i < args.length-1; i++)
			uri += "&rsargs[]=" + escape(args[i]);

		post_data = null;
	
	}else if (sajax_request_type == "POST") {
		post_data = "rs=" + escape(func_name);
		post_data += "&rst=" + escape(sajax_target_id);
		post_data += "&rsrnd=" + new Date().getTime();
		for (i = 0; i < args.length-1; i++)
			post_data = post_data + "&rsargs[]=" + escape(args[i]);
	}else{
		alert("Illegal request type: " + sajax_request_type);
	}

	x = sajax_init_object();
	if (x == null) {
		if (sajax_failure_redirect != "") {
			location.href = sajax_failure_redirect;
			return false;
		} else {
			return false;
		}
	} else {
		x.open(sajax_request_type, uri, true);
		//window.open(uri);

		sajax_requests[sajax_requests.length] = x;

		if (sajax_request_type == "POST") {
			x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
			x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}

		x.onreadystatechange = function() {
			if (x.readyState != 4)
				return;

			var status;
			var data;
			var txt = x.responseText.replace(/^\s*|\s*$/g,"");
			status = txt.charAt(0);
			data = txt.substring(2);

			if (status == "") {
				// let's just assume this is a pre-response bailout and let it slide for now
			} else if (status == "-")
				alert("Error: " + data);
			else {
				if (target_id != "")
					document.getElementById(target_id).innerHTML = eval(data);
				else {
					try {
						var callback;
						var extra_data = false;
						if (typeof args[args.length-1] == "object") {
							callback = args[args.length-1].callback;
							extra_data = args[args.length-1].extra_data;
						} else {
							callback = args[args.length-1];
						}
						callback(eval(data), extra_data);
					} catch (e) {
						e = e;
					}
				}
			}
		}
	}
	x.send(post_data);
	delete x;
	return true;
}

// wrapper for gestion_prix_ajax
function x_gestion_prix_ajax() {
	sajax_do_call("gestion_prix_ajax",x_gestion_prix_ajax.arguments);
}


// wrapper for croisements
function x_croisements() {
	sajax_do_call("croisements",x_croisements.arguments);
}

function do_gestion_prix_ajax_callback(result) {
	document.getElementById("prix_prod_" + result[0]).innerHTML = result[1];
	if (document.getElementById("prix_pack_prod_" + result[0]) && result[2] != ""){
		document.getElementById("prix_pack_prod_" + result[0]).innerHTML = result[2];
	}
	if (document.getElementById("aj_ref_prod_" + result[0])){
		document.getElementById("aj_ref_prod_" + result[0]).innerHTML = result[3];
	}
	if (document.getElementById("photos_resume_produit_" + result[0])){
		document.getElementById("photos_resume_produit_" + result[0]).innerHTML = result[5];
	}
	
	document.getElementById("prix_prod_" + result[0]).innerHTML = result[1];
	
	if (document.getElementById("dispodiv_" + result[0])){
		document.getElementById("dispodiv_" + result[0]).innerHTML = result[7];
	}
	
	if (document.getElementById('option_' + result[0] + '_' + (eval(result[6]) + 1)) && document.getElementById('option_' + result[0] + '_' + (eval(result[6]) + 2))){
		do_gestion_prix_ajax(result[0], document.getElementById('option_' + result[0] + '_' + (eval(result[6]) + 1)).value, (eval(result[6]) + 1));
	}
	
	change_photo(result[4]);
	document.getElementById('tab_qte').innerHTML = result[8];
}

function do_croisements_callback(result) {
	var option = new Array();
	var id_prod = result[0];
	var croisement = result[2];
	var num_option = result[1];
	var option_sel = result[3];
	var bool_test = false;
	
	croisement = croisement.split("#");
	
	// Mise à jour des autres select / radio en fonction de l'option sélectionnée
	for (i=0; i<10; i++){
		if (i > num_option){
			if (document.getElementById("option_" + id_prod + "_" + i)){
				if ((document.getElementById("option_" + id_prod + "_" + i).type) == "select-one"){
					for (k=0; k<document.getElementById("option_" + id_prod + "_" + i).length; k++){
						val_opt = document.getElementById("option_" + id_prod + "_" + i).options[k].value;
						val_opt = val_opt.split("#");
						for (j=0; j<croisement.length; j++){
							if (croisement[j] == val_opt[2]){
								bool_test = true;
								break;
							}
						}
						if (!bool_test){
							document.getElementById("option_" + id_prod + "_" + i).options[k].disabled = true;
							document.getElementById("option_" + id_prod + "_" + i).options[k].style.color = "#CCCCCC";
							if (document.getElementById("option_" + id_prod + "_" + i).selectedIndex == k){
								trouve = false;
								for (l=0; l<document.getElementById("option_" + id_prod + "_" + i).length; l++){
									if (document.getElementById("option_" + id_prod + "_" + i).options[l].disabled == false){
										document.getElementById("option_" + id_prod + "_" + i).selectedIndex = l;
										trouve = true;
										break;
									}
								}
								if (trouve == false){
									document.getElementById("option_" + id_prod + "_" + i).selectedIndex = k + 1;
								}
							}
						}else{
							document.getElementById("option_" + id_prod + "_" + i).options[k].disabled = false;
							document.getElementById("option_" + id_prod + "_" + i).options[k].style.color = "black";
							bool_test = false;
						}
					}
				}
			}
		}
	}
	
	for (i=0; i<10; i++){
		if (document.getElementById("option_" + id_prod + "_" + i)){
			if ((document.getElementById("option_" + id_prod + "_" + i).type) == "select-one"){
				val_id_art_ref_option = document.getElementById("option_" + id_prod + "_" + i).value;
				val_id_art_ref_option = val_id_art_ref_option.split("#");
				val_id_art_ref_option = val_id_art_ref_option[2];
				indice = document.getElementById("num_option_ref_" + id_prod +"_" + val_id_art_ref_option).value;
				option[indice-1] = document.getElementById("option_" + id_prod + "_" + i).value;
				if (document.getElementById("option_" + id_prod + "_" + i).disabled == true){
					document.getElementById("option_" + id_prod + "_" + i).disabled = false;
				}
			}else if ((document.getElementById("option_" + id_prod + "_" + i).type) == "radio"){
				val_id_art_ref_option = document.getElementById("valeur_option_" + id_prod + "_" + i).value;
				val_id_art_ref_option = val_id_art_ref_option.split("#");
				val_id_art_ref_option = val_id_art_ref_option[2];
				indice = document.getElementById("num_option_ref_" + id_prod +"_" + val_id_art_ref_option).value;
				option[indice-1] = document.getElementById("valeur_option_" + id_prod + "_" + i).value;
			}else{
				option[i] = 0;
			}
		}else{
			option[i] = 0;
		}
	}
	x_gestion_prix_ajax(id_prod, option[0], option[1], option[2], option[3], option[4], option[5], option[6], option[7], option[8], option[9], result[1], do_gestion_prix_ajax_callback);
}


function do_gestion_prix_ajax(id_prod, option, cpt) {
	if (option != "0"){
		if (document.getElementById("option_" + id_prod + "_" + cpt)){
			if ((document.getElementById("option_" + id_prod + "_" + cpt).type) == "select-one"){
				if (document.getElementById("option_" + id_prod + "_" + cpt).options[document.getElementById("option_" + id_prod + "_" + cpt).selectedIndex].style.color == "#cccccc"){
					libelle_option = option.split("#");
					if (cpt > 0){
						if ((document.getElementById("option_" + id_prod + "_" + (cpt - 1)).type) == "select-one"){
							option_parente = document.getElementById("option_" + id_prod + "_" + (cpt - 1)).value;
							libelle_option_parente = option_parente.split("#");
							alert("\"" + libelle_option[1] + "\" n'est pas disponible en \"" + libelle_option_parente[1] + "\"");
						}else if ((document.getElementById("option_" + id_prod + "_" + (cpt - 1)).type) == "radio"){
							option_parente = document.getElementById("valeur_option_" + id_prod + "_" + (cpt - 1)).value;
							libelle_option_parente = option_parente.split("#");
							alert("\"" + libelle_option[1] + "\" n'est pas disponible en \"" + libelle_option_parente[1] + "\"");
						}else{
							alert("\"" + libelle_option[1] + "\" n'est pas disponible avec votre sélection de coloris ou taille.");
						}
					}else{
						alert("\"" + libelle_option[1] + "\" n'est pas disponible avec votre sélection de coloris ou taille.");
					}
					
					trouve = false;
					for (l=0; l<document.getElementById("option_" + id_prod + "_" + cpt).length; l++){
						if (document.getElementById("option_" + id_prod + "_" + cpt).options[l].disabled == false){
							document.getElementById("option_" + id_prod + "_" + cpt).selectedIndex = l;
							trouve = true;
							break;
						}
					}
					if (trouve == false){
						document.getElementById("option_" + id_prod + "_" + cpt).selectedIndex = k + 1;
					}
					return false;
				}
			}
			for (i=0; i<10; i++){
				if (document.getElementById("option_" + id_prod + "_" + i)){
					if ((document.getElementById("option_" + id_prod + "_" + i).type) == "select-one" && document.getElementById("option_" + id_prod + "_" + i).value != "0" && document.getElementById("option_" + id_prod + "_" + i).value != ""){
						document.getElementById("option_" + id_prod + "_" + i).disabled = true;
					}
				}
			}
		}
		x_croisements(id_prod, option, cpt, do_croisements_callback);
	}
}