/**
 *
 * @fileoverview Gestisce l'account
 *
 * @filename account.js

 * @created 2008-05-07
 * @original author Giampaolo Bellavite
 * @copyright Kemen s.r.l.
 * @license Mozilla Public License Version 1.1
 * @version 0.1
 * @$LastChangedDate:: 2011-02-07#$
 * $LastChangedRevision: 4084 $
 * $LastChangedBy: faser $
*/
if(typeof(HOL) == 'undefined') {var HOL = {}};

var oConn;	

HOL.account = {};


/*** Public methods ***/

/**
 * Elimina un indirizzo di spedizione
 * 
 */
HOL.account.deleteAddress = function(id){
	var target = Dom.get('tdAddress'+id);
	var callback = {
		success: function(oResult){
			Dom.removeClass('pageTitle','loader');
			var fade = new Effects.Fade(target, {
				seconds: 0.3
			});
		},
		failure: function(oResult){
			HOL.panels.showError(oResult);
			Dom.removeClass('pageTitle','loader');
		}
	};
	
	Dom.addClass('pageTitle','loader');
	
	var oConn = Conn.asyncRequest('POST', 
								  '/_ajax.account.cfm?_isXHR=true&method=deleteAddress&id='+id, 
								  callback);
	return true;

};

/*
 * Mostra la finestra per modificare i dati di accesso
 */
HOL.account.showEdit = function(){
	HOL.windowBox.showDialogContent('Modifica i dati di accesso', 
										   '/_ajax.account.cfm?_isXHR=true&method=showEdit',
								         	 true,null,400,250);
};

/*
 * Modifica un account dal panel
 */
HOL.account.edit = function(form) {
	var callback = {
		success: function(oResult){
			var parsedResponse = JSON.parse(oResult.responseText);
			if (parsedResponse.CFValidationError) {
				HOL.forms.enableSubmit(form);
				HOL.forms.catchValidationError(parsedResponse.CFValidationError, 'edit-errore', form);
			}
			else {
				location.reload();
			}
		},
		failure: function(oResult){			
			HOL.forms.enableSubmit(form);
			HOL.panels.showError(oResult);
		}
	};
	if(oConn) {
		// Termina l'eventuale connessione esistente
		Conn.abort(oConn);
	};
	HOL.forms.cleanValidationError(form,'edit-errore');
	HOL.forms.disableSubmit(form);	
	Conn.setForm(form);
	oConn = Conn.asyncRequest('POST', 
								  '/_ajax.account.cfm?_isXHR=true&method=edit', 
								  callback);
	// reset the form state after the submit
	Conn.resetFormState(); 
	return false;
};

HOL.account.orders = {};

/*
 * Carica la tabella degli ordini
 */
HOL.account.loadOrders = function(page) {
	var tbody = Dom.get('tbody_orders');
	var tableElement = Dom.get('paginatedTable');
		tableElement.removeChild(tbody);
	var navigator = Dom.get('navigator');
	var idDoc = ow.form.getSelectValues('filterIdDoc');
	var byDate = ow.form.getSelectValues('filterByDate');
	var byStatus = ow.form.getSelectValues('filterByStatus');
	var kw = Dom.get('inputKW').value;
	
	var callback = {
		success: function(oResult){
			var parsedResponse = JSON.parse(oResult.responseText);

			// mmh hack per risolvere il problema con ie secondo cui innerHTML e' readonly per le table
			var tempTBody = "<table><tbody id='tbody_orders'>" + parsedResponse.tbody + "</tbody></table>";
			var tempDiv = document.createElement("DIV");
			tempDiv.innerHTML = tempTBody;
			tableElement.appendChild(tempDiv.firstChild.firstChild);

			navigator.innerHTML = parsedResponse.navigator;
			Dom.setStyle('tbody_loading','display', 'none');
			Dom.setStyle('tbody_orders','display', '');
			Dom.get('filterIdDoc').disabled=Dom.get('filterByDate').disabled=Dom.get('filterByStatus').disabled=false;
		},
		failure: function(oResult){
			Dom.get('filterIdDoc').disabled=Dom.get('filterByDate').disabled=Dom.get('filterByStatus').disabled=false;
			HOL.panels.showError(oResult);
			Dom.removeClass('tbody_loading_image', 'loader');
			Dom.get('tbody_loading_text').innerHTML = "&nbsp;";
		}
	};
	
	if(oConn) {
		// Termina l'eventuale connessione esistente
		Conn.abort(oConn);
	};
	
	Dom.addClass('tbody_loading_image', 'loader');
	Dom.get('tbody_loading_text').innerHTML = "Ricerca in corso...";
	Dom.get('filterIdDoc').disabled=Dom.get('filterByDate').disabled=Dom.get('filterByStatus').disabled=true;
	Dom.setStyle('tbody_loading','display', '');
	Dom.setStyle('tbody_orders','display', 'none');

	oConn = Conn.asyncRequest('POST', 
								  '/_ajax.account.cfm?_isXHR=true&method=loadOrders&page='+page+'&filterIdDoc='+idDoc+'&filterByStatus='+byStatus+'&filterByDate='+byDate+'&kw='+kw, 
								  callback);
	return false;
};

/*
 * Carica la tabella delle ricariche ordinate
 */
HOL.account.loadSubscriptions = function(page) {
	var tbody = Dom.get('tbody_orders');
	var tableElement = Dom.get('paginatedTable');
		tableElement.removeChild(tbody);
	var navigator = Dom.get('navigator');
	var byDate = ow.form.getSelectValues('filterByDate');
	var byStatus = ow.form.getSelectValues('filterByStatus');
	var callback = {
		success: function(oResult){
			var parsedResponse = JSON.parse(oResult.responseText);
			var tempTBody = "<table><tbody id='tbody_orders'>" + parsedResponse.tbody + "</tbody></table>";
			var tempDiv = document.createElement("DIV");
			tempDiv.innerHTML = tempTBody;
			tableElement.appendChild(tempDiv.firstChild.firstChild);

			Dom.get('filterByDate').disabled=Dom.get('filterByStatus').disabled=false;
			Dom.setStyle('tbody_loading','display', 'none');
			Dom.setStyle('tbody_orders','display', '');
			navigator.innerHTML = parsedResponse.navigator;
		},
		failure: function(oResult){
			HOL.panels.showError(oResult);
			Dom.get('filterByDate').disabled=Dom.get('filterByStatus').disabled=false;
			Dom.removeClass('tbody_loading_image', 'loader');
			Dom.get('tbody_loading_text').innerHTML = "&nbsp;";
		}
	};
	
	if(oConn) {
		// Termina l'eventuale connessione esistente
		Conn.abort(oConn);
	}
	
	Dom.addClass('tbody_loading_image', 'loader');
	Dom.get('tbody_loading_text').innerHTML = "Ricerca in corso...";
	Dom.get('filterByDate').disabled=Dom.get('filterByStatus').disabled=true;
	Dom.setStyle('tbody_loading','display', '');
	Dom.setStyle('tbody_orders','display', 'none');
	
	oConn = Conn.asyncRequest('POST', 
								  '/_ajax.account.cfm?_isXHR=true&method=loadSubscriptions&page='+page+'&filterByStatus='+byStatus+'&filterByDate='+byDate, 
								  callback);
	
	return false;
};

/*
 * Carica la tabella con l'elenco delle fatture
 */
HOL.account.loadFatture = function(page) {
	var tbody = Dom.get('tbody_orders');
	var tableElement = Dom.get('paginatedTable');
		tableElement.removeChild(tbody);
	var navigator = Dom.get('navigator');
	var callback = {
		success: function(oResult){
			var parsedResponse = JSON.parse(oResult.responseText);
			var tempTBody = "<table><tbody id='tbody_orders'>" + parsedResponse.tbody + "</tbody></table>";
			var tempDiv = document.createElement("DIV");
			tempDiv.innerHTML = tempTBody;
			tableElement.appendChild(tempDiv.firstChild.firstChild);

			Dom.setStyle('tbody_loading','display', 'none');
			Dom.setStyle('tbody_orders','display', '');
			navigator.innerHTML = parsedResponse.navigator;
		
		},
		failure: function(oResult){
			HOL.panels.showError(oResult);
			Dom.removeClass('tbody_loading_image', 'loader');
			Dom.get('tbody_loading_text').innerHTML = "&nbsp;";
		}
	};
	if(oConn) {
		// Termina l'eventuale connessione esistente
		Conn.abort(oConn);
	}
	Dom.addClass('tbody_loading_image', 'loader');
	Dom.get('tbody_loading_text').innerHTML = "Ricerca in corso...";
	Dom.setStyle('tbody_loading','display', '');
	Dom.setStyle('tbody_orders','display', 'none');
	oConn = Conn.asyncRequest('POST', 
								  '/_ajax.account.cfm?_isXHR=true&method=loadFatture&page='+page, 
								  callback);
	return false;
};

/*
 * Mostra la finestra di dialogo per l'esportazione delle fatture
 */
HOL.account.showExportFatture = function(){
	return HOL.panels.showDialogContent('Esporta fatture', 
										   '/_ajax.account.cfm?_isXHR=true&method=showExportFatture',
								         	 true);
};

/*
 * Mostra la finestra di dialogo per l'esportazione dell'elenco ordini
 */
HOL.account.showExportOrdini = function(){
	return HOL.panels.showDialogContent('Esporta elenco ordini', 
										   '/_ajax.account.cfm?_isXHR=true&method=showExportOrdini',
								         	 true);
};

/*
 * Mostra la finestra di dialogo per l'esportazione dell'elenco ricariche
 */
HOL.account.showExportRicariche = function(){
	return HOL.panels.showDialogContent('Esporta elenco ricariche', 
										   '/_ajax.account.cfm?_isXHR=true&method=showExportRicariche',
								         	 true);
};

/*
 * Invia un messaggio
 */
 HOL.account.sendMessage = function(btn) {
 	var theForm = document.forms.frmMessage;
	HOL.forms.disableSubmit(theForm);

	var onSuccess = function (oResult) {
		location='/account/index.cfm/mailbox/?sent&messageSent';
	};
	
	return HOL.utils.sendMessage(theForm, onSuccess);
 };

/*
 * Invia feedback
 */
 HOL.account.sendFeedback = function(form) {
 	var uri = '/_ajax.common.cfm?_isXHR=true&method=sendFeedback';
    var theForm = document.getElementById(form);
	var callback = {
			success: function(oResult) {
				var parsedResponse = JSON.parse(oResult.responseText);
				if (parsedResponse.CFValidationError) {
					HOL.forms.catchValidationError(parsedResponse.CFValidationError, 'messaggio-errore', theForm);
				}
				else {
					// nascondo i panel in cui puo' trovarsi il messaggio.
					$.fancybox.close();					
				};		
			},
			failure: function(oResult){
				HOL.panels.showError(oResult);
			}
		};
	HOL.forms.cleanValidationError(theForm,'messaggio-errore');
	Conn.setForm(theForm);
	var oConn = Conn.asyncRequest('POST', uri, callback);
	Conn.resetFormState(); 
	return false;
	
 };

/*
 * Invia il tell a friend
 */
 HOL.account.sendTellAFriend = function(form) {
 	HOL.forms.disableSubmit(form);
	var uri = '/_ajax.common.cfm?_isXHR=true&method=sendTellAFriend';
	var form = Dom.get(form);
	var callback = {
			success: function(oResult) {
				HOL.forms.enableSubmit(form);
				var parsedResponse = JSON.parse(oResult.responseText);
				if (parsedResponse.CFValidationError) {
					HOL.forms.catchValidationError(parsedResponse.CFValidationError, 'messaggio-errore', form);
				}
				else {
					// nascondo i panel in cui puo' trovarsi il messaggio.
					$.fancybox.close();					
				};		
			},
			failure: function(oResult){
				HOL.panels.showError(oResult);
				HOL.forms.enableSubmit(form);
			}
		};
	HOL.forms.disableSubmit(form);
	HOL.forms.cleanValidationError(form,'messaggio-errore');
	Conn.setForm(form);	
	var oConn = Conn.asyncRequest('POST', uri, callback);
	Conn.resetFormState(); 
	return false;
	
 };
 
/*
 * Invia la richeista di affiliazione
 */
 HOL.account.sendRichiestaAffiliazione = function(form) {
 	HOL.forms.disableSubmit(form);
	var uri = '/_ajax.common.cfm?_isXHR=true&method=sendRichiestaAffiliazione';
	var form = Dom.get(form);
	var callback = {
			success: function(oResult) {
				HOL.forms.enableSubmit(form);
				var parsedResponse = JSON.parse(oResult.responseText);
				if (parsedResponse.CFValidationError) {
					HOL.forms.catchValidationError(parsedResponse.CFValidationError, 'messaggio-errore', form);
				}
				else {
					// mostro messaggio di ringraziamento
				   $('#monitor').css({height:"500px"}).html('<p>Grazie per il tuo interesse nel programma di affiliazione di <b>Home On Line</b></p><p>Ti contatteremo nel piu\' breve tempo possibile</p>');					
				};		
			},
			failure: function(oResult){
				HOL.panels.showError(oResult);
				HOL.forms.enableSubmit(form);
			}
		};
	HOL.forms.disableSubmit(form);
	HOL.forms.cleanValidationError(form,'messaggio-errore');
	Conn.setForm(form);	
	var oConn = Conn.asyncRequest('POST', uri, callback);
	Conn.resetFormState(); 
	return false;
	
 };
 
 
 /*
  * Invia un messaggio di richiesta relativo ad un ordine
  */
 
HOL.account.showSendMessage = function(orderID, messageType) {
	var dialogTitle = '';
	
	switch(messageType) {
		case 'request-edit': 
			dialogTitle = 'Richiedi la modifica dei dati inseriti';
		break;
		case 'request-info': 
			dialogTitle = 'Richiedi informazioni sull\'ordine';
		break;
		case 'request-cancel': 
			dialogTitle = 'Annulla l\'ordine';
		break;
		case 'request-fatturazione': 
			dialogTitle = 'Richiedi la fatturazione mensile';
		break;
		case 'request-listino': 
			dialogTitle = 'Richiedi un listino personalizzato';
		break;
		case 'request-scoperto': 
			dialogTitle = 'Richiedi di accreditare fuori dall\'abbonamento';
		break;
		
	};
	return HOL.panels.showDialogContent(dialogTitle, '/_ajax.account.cfm?_isXHR=true&method=showSendMessage&type='+messageType+'&orderID='+orderID, false);
};
 
/*
 * Carica la tabella con l'elenco dei messaggi
 */
HOL.account.loadMailBox = function(page) {
	var tbody = Dom.get('tbody_orders');
	var type = document.forms.frmFilterMailBox.type.value; // sent or received
	var onlyUnread = Dom.get('unread') ? Dom.get('unread').checked : false;
	var tableElement = Dom.get('paginatedTable');
		tableElement.removeChild(tbody);
	var navigator = Dom.get('navigator');
	var callback = {
		success: function(oResult){
			var parsedResponse = JSON.parse(oResult.responseText);
			var tempTBody = "<table><tbody id='tbody_orders'>" + parsedResponse.tbody + "</tbody></table>";
			var tempDiv = document.createElement("DIV");
			tempDiv.innerHTML = tempTBody;
			tableElement.appendChild(tempDiv.firstChild.firstChild);

			Dom.setStyle('tbody_loading','display', 'none');
			Dom.setStyle('tbody_orders','display', '');
			navigator.innerHTML = parsedResponse.navigator;
		
		},
		failure: function(oResult){
			HOL.panels.showError(oResult);
			Dom.removeClass('tbody_loading_image', 'loader');
			Dom.get('tbody_loading_text').innerHTML = "&nbsp;";
		}
	};
	if(oConn) {
		// Termina l'eventuale connessione esistente
		Conn.abort(oConn);
	};
	Dom.addClass('tbody_loading_image', 'loader');
	Dom.get('tbody_loading_text').innerHTML = "Ricerca in corso...";
	Dom.setStyle('tbody_loading','display', '');
	Dom.setStyle('tbody_orders','display', 'none');
	oConn = Conn.asyncRequest('POST', 
								  '/_ajax.account.cfm?_isXHR=true&method=loadMailBox&page='+page+'&type='+type+'&onlyUnread='+onlyUnread, 
								  callback);
	return false;
};

