var ajax = new sack();
var ajaxContentObj;
var valueStr;
var ajaxElemName;

function showContent() {
	var resp = ajax.response;
	ajaxContentObj.innerHTML = resp;	// ajax.response is a variable that contains the content of the external file
	//alert(resp);
	var num = resp.indexOf("ok [begin_id]");
	var num2 = resp.indexOf("[end_id]");
	if (num > -1) {
		var idn = resp.substr(num + 13, num2 - num - 13);
		//alert(idn);
		closeBox(idn);
		loadBoxDati(idn);
	}
	if (resp.indexOf("loadAjaxForm") > -1) {
		updateSpese()
	}
	if (resp.indexOf("updateSpese") > -1) {
		updateSpese()
	}
	if (resp.indexOf("updatePrezzoTotale") > -1) {
		updatePrezzoTotale()
	}
	if (resp.indexOf("dati_cliente") > -1) {
		//alert(resp);
		if (resp.indexOf("registrati") > -1) {
			window.location = "?action=ok";
		} else if (resp.indexOf("dati_personali") > -1) {
			window.location = "area_riservata.htm";
		} else {
			window.location = "?action=dati_cliente";
		}
	}
	if (resp.indexOf("copia_saggio") > -1) {
		window.location = "?action=ok";
	}
	if (resp.indexOf("catalogo_gratuito") > -1) {
		var num = resp.indexOf("gratuito[");
		var num2 = resp.indexOf("]");
		var idn = resp.substr(num + 9, num2 - num - 9);
		window.location = "?action=ok&id=" + idn;
	}
	var num_e = resp.indexOf("ecard [");
	var num_e2 = resp.indexOf("]");
	if (num_e > -1) {
		var idn = resp.substr(num_e + 7, num_e2 - num_e - 7);
		//alert(idn);
		//loadBoxDati(idn);
		Lightview.show({
			href: 'ecard_preview.asp?id=' + idn
			, options: {
   			   width: 650,
			   height: 400
			}
		});
		//alert(idn);
	}
	var num_g = resp.indexOf("goto [");
	var num_g2 = resp.indexOf("]");
	if (num_g > -1) {
		var idn = resp.substr(num_g + 6, num_g2 - num_g - 6);
		//alert(idn);
		window.location = idn;

	}
}

function showWaitMessage() {
	ajaxContentObj.innerHTML = '<img src="img/ajax-loader.gif" alt="" style="display: inline; margin: 10px 0; vertical-align: middle;" /> <span style="font-size: 11px;">LOADING...</span>';
	//ajaxContentObj.innerHTML = "";
}

function getAjaxFile(fileName) {
	ajax.requestFile = fileName;	// Specifying which file to get
	ajax.onCompletion = showContent;	// Specify function that will be executed after file has been found
	ajax.onLoading = showWaitMessage;	// Action when AJAX is loading the file
	ajax.runAJAX();		// Execute AJAX function	
}

function updateSpese() {
	ajaxContentObj = document.getElementById("box_spese_spedizione");
	getAjaxFile("ajax/box_spese_spedizione.asp");
}
function updatePrezzoTotale() {
	//ajaxContentObj = document.getElementById("box_prezzo_totale");
	//getAjaxFile("ajax/box_prezzo_totale.asp");
	
	new Ajax.Request('ajax/box_prezzo_totale.asp', {
		onSuccess: function(t) {
			$("box_prezzo_totale").update(t.responseText);
			$("box_prezzo_top").update(t.responseText);
		}
	});

	
}

function ajax_gift_card(codice, importo) {
	ajaxContentObj = document.getElementById("ajax_gift_card");
	getAjaxFile("ajax/gift_card.asp?codice=" + codice + "&importo=" + importo);
}

function loadAjaxForm(id) {
	var first_num = id.indexOf("_") + 1;
	var second_num = id.indexOf("__");
	var idn = id.substring(first_num, second_num);
	//alert("*" + idn + "*");
	ajaxContentObj = document.getElementById("box_container_" + idn);
	getAjaxFile("ajax/form_address.asp?id=" + idn);
}

function loadBoxDati(idn) {
	/*var first_num = id.indexOf("_") + 1;
	var second_num = id.indexOf("__");
	var idn = id.substring(first_num, second_num);*/
	//alert("*" + idn + "*");
	ajaxContentObj = document.getElementById("dati_container_" + idn);
	getAjaxFile("ajax/box_dati.asp?id=" + idn);
}

function ajaxSetRegalo(idn, which) {
	ajaxContentObj = document.getElementById("box_container_" + idn);
	getAjaxFile("ajax/set_regalo.asp?action=" + which + "&id=" + idn);
}
function ajaxSetRinnovo(idn, which) {
	ajaxContentObj = document.getElementById("box_container_" + idn);
	getAjaxFile("ajax/set_rinnovo.asp?action=" + which + "&id=" + idn);
}


function postForm(formId, idn, no) {
	ajaxContentObj = document.getElementById("ajax_form_text");
	var elemArray = document.getElementById(formId);
	var valueStr = "";
	for (var i = 0; i < elemArray.length; i++) {
        var element = elemArray[i];
		if (element.type) {
			var elemType = element.type.toUpperCase();
			var elemName = element.name;
			//var valueStr = "";
			if (element.disabled == false) {
				if (elemName) {
					var typesStr = "|TEXT|TEXTAREA|PASSWORD|BUTTON|RESET|SUBMIT|FILE|IMAGE|HIDDEN|";
					if (typesStr.indexOf("|" + elemType + "|") != -1) {
						valueStr = GetElemValue(elemName, element.value);
					} else if (elemType == "CHECKBOX") {
						if (element.checked) {
							valueStr = GetElemValue(elemName, element.value ? element.value : "On");
						} else if (ajaxElemName != elemName) {
							valueStr = "";
						}
					} else if (elemType == "RADIO") {
						if (element.checked) {
							valueStr = GetElemValue(elemName, element.value);
						} else {
							//valueStr = "";
						}
					} else if (elemType.indexOf("SELECT") != -1) {
						for (var j = 0; j < element.options.length; j++) {
							var option = element.options[j];
							if (option.selected) {
								//valueStr = GetElemValue(elemName, option.value ? option.value : option.text);
								valueStr = GetElemValue(elemName, option.value);
							}
						}
					}
				}
				//alert(elemName + "|" + valueStr);
				ajax.setVar(elemName, valueStr);
			}
		}	
    }
	
	ajax.setVar("rndval", "");
	if (formId == "ajaxFormClienti") {
		getAjaxFile('ajax/form_clienti.asp?action2=save');
	} else if (formId == "ajaxFormClientiModifica") {
		getAjaxFile('ajax/form_clienti.asp?action2=update');
	} else if (formId == "ajaxFormClientiRegistrati") {
		getAjaxFile('ajax/form_clienti.asp?action2=save&action=registrati');
	} else if (formId == "ajaxFormClientiAR") {
		getAjaxFile('ajax/form_clienti.asp?action2=update&action=dati_personali');
	} else if (formId == "ajaxCopiaSaggio") {
		getAjaxFile('ajax/form_copia_saggio.asp?action2=save');
	} else if (formId == "ajaxCatalogoGratuito") {
		getAjaxFile('ajax/form_catalogo_gratuito.asp?action2=save');
	} else if (formId == "ajaxRichiesteAziende") {
		getAjaxFile('ajax/form_richieste_aziende.asp?action2=save');
	} else if (formId == "ajaxPartner") {
		getAjaxFile('ajax/form_partner.asp?action2=save');
	} else if (formId == "ajaxEcard") {
		//alert(idn);
		var str_no = (no) ? "action2=no&" : "";
		if (idn == "0") {
			getAjaxFile('ajax/ecard_preview.asp?' + str_no);
		} else if (idn.indexOf("cliente") != -1) {
			getAjaxFile('ajax/ecard_preview.asp?' + str_no + 'action=' + idn);
		} else {
			getAjaxFile('ajax/ecard_preview.asp?' + str_no + 'id=' + idn);
		}
	} else  {
		getAjaxFile('ajax/form_address.asp?action2=save&id=' + idn);
	}
}

function GetElemValue(el_name, value) {
	var comma = "";
	if (ajaxElemName != el_name) {
		valueStr = "";
	} else {
		comma = ", ";
	}
	//alert(ajaxElemName + "|" + el_name + "|" + value +  "|" +  valueStr);
    valueStr += comma + escape((value ? value : "")).replace(/%0A/g, "<br>");
	// escape(value ? value : "").replace(/\+/g, "%2B");
	//+ escape(value ? value : "").replace(/\n/g, "%0D");
	//if (valueStr) alert(valueStr);
	ajaxElemName = el_name;
	return valueStr;
}

function ajaxProvince(regione) {
	var provincia = document.getElementById("provincia");
	//contentObj = provincia;
	num_option = provincia.options.length;
	for (a=num_option;a>=0;a--) {
		provincia.options[a]=null;
	}
	//alert(regione);
	ajax.requestFile = "ajax/form_province.asp?regione=" + regione;	// Specifying which file to get
	ajax.onCompletion = function() {
		var provStr = ajax.response;
		//alert(provStr);
		var provArray = provStr.split("|");
		for (a=0;a<provArray.length;a++) {
			var thisProv = provArray[a];
			var thisProvArray = thisProv.split(",");
			var provValue = thisProvArray[0];
			var provName = thisProvArray[1];
			provincia.options[a]=new Option(provName, provValue, false, false);
		}
	};	// Specify function that will be executed after file has been found
	ajax.onLoading = function() {
		provincia.options[0] = new Option("Elenco province in caricamento...", "", true, false);
	};	// Action when AJAX is loading the file
	ajax.onError = function() {
		provincia.options[0] = new Option("Si è verificato un errore!", "", true, false);
	};	// Action when AJAX is loading the file
	ajax.runAJAX();		// Execute AJAX function
}
/*function showNews(id) {
	getAjaxFile("ajax_news.asp?id=" + id);
}

function ajaxInit() {
	ajaxContentObj = document.getElementById("newsContent");
	var tags = document.getElementById("newsLinks").getElementsByTagName("a");
	for (i=0; i<tags.length; i++) {
		var tag = tags[i];
		tag.onclick = function() {
			var thisId = this.id;
			thisId = thisId.replace("news_", "");
			showNews(thisId);
		}
	}
	
}*/
