/******************************************************************************/
/*                                                                            */
/*                                                                            */
/*  CityMagazine                                                              */
/*                                                                            */
/*                                                                            */
/******************************************************************************/



//------------------------------------------------------------------------------
// util                                                                         
//------------------------------------------------------------------------------

function g(id)
{
	return document.getElementById(id);
}

function suite(id)
{
	if (g(id))
	{
		g(id+"_suite").style.display = 'none';
		g(id+"_plus").style.display = 'block';
	}
}

function fermer(id)
{
	if (g(id))
	{
		g(id+"_plus").style.display = 'none';
		g(id+"_suite").style.display = 'block';
	}
}

function plaquette()
{
	if( rep = prompt("Veuillez indiquer le code d'accès.", ""))
		ajax.send(rep);
}

//------------------------------------------------------------------------------
// commande                                                                     
//------------------------------------------------------------------------------

function init(id)
{
	if (!g("c"+id).checked)
		cocherNo(id)
	else
		calculate();
}

function cocherNo(id)
{
	if (g("n"+id))
	{
		g("c"+id).click();
		calculate();
	}
}

function calculate()
{
	numero = new Array();
	var recapitulatif = "";
	for (i=0; i<numeros.length; i++)
	{
		if (g("c"+numeros[i]).checked)
			g("n"+numeros[i]).style.background = '#CEC';
		else
			g("n"+numeros[i]).style.background = '#DDD';
	}
	
	if (g("c0").checked)
		recapitulatif += "Abonnement City Magazine 1 an : 15€";	
	for (i=0; i<numeros.length-1; i++)
		if (g("c"+numeros[i]).checked)
			numero.push(numeros[i]);
	recapitulatif += (numero.length > 0 && recapitulatif.length > 0) ? "<br/>" : "";
	recapitulatif += numero.length > 0 ? "City Magazine "+numero.join(", ")+" : "+(numero.length*5)+"€" : "";
	
	if (numero.length > 0 || recapitulatif.length > 0) 
	{
		recapitulatif += "<br/>------------------------------<br/>Total : "+
		                  (numero.length*5+g("c0").checked*15)+"€";
		g("suivant").style.display = "block";
	}
	else
		g("suivant").style.display = "none";
	g("recapitulatif").innerHTML = recapitulatif;
}

//------------------------------------------------------------------------------
// marquee                                                                      
//------------------------------------------------------------------------------

function l(el)
{
	return parseInt(el.style.left);
}

var d1, d2, contentWidth;
function populate()
{
	var marquee = g("marquee");
	var content = marquee.innerHTML;
	marquee.innerHTML = "";
	var divTemp = document.createElement("div");
	divTemp.innerHTML = content;
	divTemp.style.whiteSpace = "nowrap";
	divTemp.style.display = "inline";
	g("scroll").insertBefore(divTemp, marquee);
	contentWidth = divTemp.offsetWidth;
	g("scroll").removeChild(divTemp);
	
	if (marquee)
	{
		for (var i=1; i<=2; i++)
		{
			var div = document.createElement("div");
			div.innerHTML = content;
			div.setAttribute("id", "marqueeD"+i);
			marquee.appendChild(div);
			g("marqueeD"+i).style.left = "-10px";
			g("marqueeD"+i).style.position = "absolute";
			g("marqueeD"+i).style.whiteSpace = "nowrap";
			this["d"+i] = g("marqueeD"+i);
		}
		d2.style.left = (l(d1)+contentWidth)+"px";
		
		// setInterval(scrollmarquee, 20);
	}
	g("marquee").style.display = "block";
}

function scrollmarquee()
{
	if (l(d1) < -contentWidth)
		d1.style.left  = (l(d2)+contentWidth)+"px";
	if (l(d2) < -contentWidth)
		d2.style.left  = (l(d1)+contentWidth)+"px";		
	d1.style.left = (l(d1)-1)+"px";
	d2.style.left = (l(d2)-1)+"px";
}

//------------------------------------------------------------------------------
// ajax                                                                       
//------------------------------------------------------------------------------

function getHTTPObject()
{
  var xmlhttp = false;

  /* Compilation conditionnelle d'IE */
  /*@cc_on
  @if (@_jscript_version >= 5)
     try
     {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)
     {
        try
        {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
           xmlhttp = false;
        }
     }
  @else
     xmlhttp = false;
  @end @*/

  /* on essaie de créer l'objet si ce n'est pas déjà fait */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
     try
     {
        xmlhttp = new XMLHttpRequest();
     }
     catch (e)
     {
        xmlhttp = false;
     }
  }
  return xmlhttp;
}

function Ajax()
{
	this.hr = null;
	this.send = function(val)
	{
		this.hr = getHTTPObject();
		var x = this.hr;
		this.hr.onreadystatechange = function()
		{
			if (x.readyState == 4)
				if (x.status == 200)
					ajaxResult(x.responseText);
		}
		this.hr.open("POST", "publicite.php", true);
		this.hr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
		this.hr.send("code="+val);
	}
}

function ajaxResult(response)
{
	if (response == "ok")
		g("zoneDownload").src = "/publicite.php/download";
	else alert("Code incorrect ! \nSi vous ne possédez pas le code d'accès, veuillez contacter Vincent Guerrier");
}

var ajax = new Ajax();
