/** ******************************************************************
* JavaScript per il controllo dello shop
*
* Elenco delle funzioni:
* - ct_shop_articoli	articoli della categoria
* - ct_shop_add			aggiunge l'articolo al carrello
* - ct_shop_remove		rimuove l'articolo dal carrello
* - ct_shop_detail		dettaglio articolo
* - ct_shop_zoom		zoom immagine
* - ct_shop_promo		promo
* - ct_shop_search		cerca
* - ct_shop_mail		invoca l'invio di una mail
* - ct_shop_empty		svuota il carrello
* - ct_shop_update		aggiorna il carrello
* - ct_shop_register	richiesta registrazione
* - ct_shop_sondaggio	sondaggio
* - ct_shop_login		registrazione e login
* - ct_shop_buy			acquista
* - ct_shop				ritorna all'acquisto
* - ct_shop_basket		ritorna al carrello
* - ct_shop_pay			paga
*
* @package	ct.shop
* @author	ConsulTes info@consultes.it
* ***************************************************************** */

	/** ******************************************************************
	* Articoli della categoria
	* @param	int		$p_categoria_id		categoria
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_articoli($p_categoria_id)
	{
		if($p_categoria_id) {
			document.location='articoli.php?categoria_id='+$p_categoria_id;
		}
	}

	/** ******************************************************************
	* Aggiunge l'articolo al carrello
	* @param	object	$p_button		bottone
	* @param	int		$p_articolo_id	articolo
	* @param	int		$p_variante_id	variante
	* @param	string	$p_email		email esercente
	* @param	string	$p_messaggio	messaggio di richiesta
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_add($p_button, $p_articolo_id, $p_variante_id, $p_email, $p_message)
	{
		// Richiesta
		if ($p_email) {
			ct_shop_mail($p_email, 'Info = '+$p_message);
			return false;
		}
		// Aggiunge articolo/variante
		$form = document.forms.namedItem($p_button.form.name);
		$form.action = 'carrello.php?action=AA';
		$form.articolo_id.value = $p_articolo_id;
		$form.variante_id.value = $p_variante_id;
		$form.submit();
	}

	/** ******************************************************************
	* Rimuove l'articolo dal carrello
	* @param	string	$p_hash				hash
	* @param	int		$p_categoria_id		categoria
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_remove($p_hash, $p_categoria_id)
	{
		document.location='articoli.php?action=RA&hash='+$p_hash+'&categoria_id='+$p_categoria_id;
	}

	/** ******************************************************************
	* Dettaglio articolo
	* @param	int		$p_articolo_id	articolo
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_detail($p_articolo_id)
	{
		document.location='articolo.php?articolo_id='+$p_articolo_id;
	}

	/** ******************************************************************
	* Zoom immagine
	* @param	int		$p_articolo_id	articolo
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_zoom($p_articolo_id)
	{
		$url = '../../shop/pages/zoom.php?articolo_id='+$p_articolo_id;
		// FIXME: Variare le misure della finestra
		//window.open($url,'zoom','scrollbars=yes,width=700,height=500')
		document.location=$url;
	}

	/** ******************************************************************
	* Promo
	* @param	string	$p_nome		nome
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_promo($p_nome)
	{
		// Attributi
		this.nome = $p_nome;
		if ($promo) {
			for ($attribute in $promo) {
				this[$attribute] = $promo[$attribute];
			}
			this.elementi = this.articolo_id.length - 1;
		} else {
			this.elementi = 0;
		}
		this.elemento = 0;
		$promo = 0;

		// Metodi
		this.show     = ct_shop_promo_show;
		this.immagine = ct_shop_promo_immagine;
		this.testo    = ct_shop_promo_testo;
		this.campo    = ct_shop_promo_campo;
	}

	// Visualizza il promo
	function ct_shop_promo_show()
	{
		if (this.elementi == 0) return false;
		$id = ++this.elemento;
		document.getElementById(this.campo("articolo_id")).value = this.articolo_id[$id];
		this.immagine("articolo_immagine",        this.articolo_immagine[$id],        this.articolo_descrizione[$id]);
		this.immagine("articolo_immagine_grande", this.articolo_immagine_grande[$id], this.articolo_descrizione[$id]);
		this.testo("articolo_id",          $id);
		this.testo("articolo_codice",      this.articolo_codice[$id]);
		this.testo("articolo_descrizione", this.articolo_descrizione[$id]);
		this.testo("articolo_prezzo",      this.articolo_prezzo[$id]);
		if (this.elemento == this.elementi) this.elemento = 0;
		setTimeout("$"+this.nome+".show()", this.delay);
	}

	// Inserisce l'immagine
	function ct_shop_promo_immagine($p_campo, $p_immagine, $p_descrizione) {
		document.getElementById(this.campo($p_campo)).src   = this.path_immagine+$p_immagine;
		document.getElementById(this.campo($p_campo)).href  = this.path_immagine+$p_immagine;
		document.getElementById(this.campo($p_campo)).title = $p_descrizione;
	}

	// Inserisce il testo
	function ct_shop_promo_testo($p_campo, $p_testo) {
		document.getElementById(this.campo($p_campo)).innerHTML = $p_testo;
	}

	// Nome del campo
	function ct_shop_promo_campo($p_campo) {
		return this.nome+'_'+$p_campo;
	}

	/** ******************************************************************
	* Cerca
	* @param	NULL
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_search()
	{
		document.cerca.submit();
	}

	/** ******************************************************************
	* Invoca l'invio di una mail
	* @param	string	$p_email	e-mail esercente
	* @param	string	$p_message	messaggio
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_mail($p_email, $p_message)
	{
		document.location='mailto:'+$p_email+'?subject='+$p_message;
	}

	/** ******************************************************************
	* Svuota il carrello
	* @param	NULL
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_empty()
	{
		document.carrello.action = 'index.php?action=SC';
		document.carrello.submit();
	}

	/** ******************************************************************
	* Aggiorna il carrello
	* @param	NULL
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_update()
	{
		// Step in esecuzione
		$step = document.carrello.step.value;

		switch ($step) {
			case 'box_carrello':
			case 'carrello':
				document.carrello.action = '?action=MC';
				document.carrello.submit();
				break;
			case 'cliente':
				document.carrello.action = '?action=AC';
				if (chk_carrello()) document.carrello.submit();
				break;
			case 'nazione':
				document.carrello.submit();
				break;
		}
	}

	/** ******************************************************************
	* Richiesta registrazione
	* @param	NULL
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_register()
	{
		if (chk_cliente()) document.carrello.submit();
	}

	/** ******************************************************************
	* Sondaggio
	* @param	NULL
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_sondaggio()
	{
		if (chk_carrello()) document.sondaggio.submit();
	}

	/** ******************************************************************
	* Registrazione e login
	* @param	string	$p_register	registrazione
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_login($p_register)
	{
		if ($p_register) {
			$url = 'register';
		} else {
			$url = 'login';
		}
		document.location = '../../login/pages/'+$url+'.php';
	}

	/** ******************************************************************
	* Acquista
	* @param	NULL
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_buy()
	{
		// Step in esecuzione
		$step = document.carrello.step.value;

		switch ($step) {
			case 'box_carrello':
				// Dal box carrello passa al carrello
				document.location = 'carrello.php';
				break;
			case 'carrello':
				if (document.carrello.user_id.value == 0) {
					// Dal carrello passa all'utente
					document.carrello.action = 'utente.php?action=AC';
				} else {
					// Dal carrello passa al cliente
					document.carrello.action = 'cliente.php?action=AC';
				}
				if (chk_carrello()) document.carrello.submit();
				break;
			case 'utente':
				// Dall'utente passa al cliente
				document.carrello.action = 'cliente.php?action=AC';
				document.carrello.submit();
				break;
			case 'cliente':
				// Dal cliente passa al pagamento, che passa alla cassa
				document.carrello.action = 'pagamento.php?action=AC';
				if (chk_carrello()) document.carrello.submit();
				break;
			case 'pagato':
				// Dal pagato passa al pagamento per ritentare
				$ordine_id     = document.carrello.ordine_id.value;
				$ordine_codice = document.carrello.ordine_codice.value;
				document.carrello.action = 'pagamento.php?ordine_id='+$ordine_id+'&ordine_codice='+$ordine_codice;
				document.carrello.submit();
				break;
		}
	}

	/** ******************************************************************
	* Ritorna all'acquisto
	* @param	NULL
	* @return	NULL
	* ***************************************************************** */
	function ct_shop()
	{
		document.carrello.action = 'index.php?action=AC';
		document.carrello.submit();
	}

	/** ******************************************************************
	* Ritorna al carrello
	* @param	NULL
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_basket()
	{
		document.carrello.action = 'carrello.php?action=AC';
		document.carrello.submit();
	}

	/** ******************************************************************
	* Paga
	* @param	NULL
	* @return	NULL
	* ***************************************************************** */
	function ct_shop_pay()
	{
		if (document.modulo.action) {
			if (chk_carrello()) document.modulo.submit();
		}
	}

