//function SondageOutput(section_name, sous_section) 
//{
//   if (section_name && sous_section)
//   {
//     popupWin2("Sondage", "/sondage/includes/"+section_name+"/"+sous_section+"/"+sous_section+".html", 780, 600);
//   }
//}

function afficheResultats(link, format, width, height) 
{
	if (format == 'popup')
	{
		if (width == null) {
			width = 780;
		}

		if (height == null) {
			height = 600;
		}

		popupWin2("Resultat", link, width, height);
	}
	else 
	{
		top.location = (link);
	}
}

//////////////////////
/*	participerSondage
/	Permet de participer à un sondage
/	Appel le script de participation en lui envoyant les infos nécessaires
/
/	IN:
/		sondageForm: Le formulaire de la question sondage
/
/	OUT:
/		AUCUN
*/
/////////////////////////////////////////////////

// ------------------------------------------------------------
// Révision : 2008-05-29 - Ellis Antaya - 5e parametre 'reponse' (Que feriez-vous pour 1000 piasses?)
// Révision : 2007-10-31 - Ellis Antaya
// Auteur   : ????-??-?? - ?
// Objectif : inscrire la réponse dans la bd
// ------------------------------------------------------------
function participerSondage(sondageForm, format, width, height, reponse, megahorriblepatch_canoef) {

	var id_question = sondageForm.id_question.value;
	var url_result  = sondageForm.url_result.value;
	var url_merci   = sondageForm.url_merci.value;
	var id_region   = null;

	var choixItem;
	var choixList;
        if (sondageForm.id_region) {
           id_region = sondageForm.id_region.value;
        }

	// si on a deja une valeur reponse
	if(reponse)
	{
		choixItem = reponse;
	}
	else
	{
		choixList = sondageForm.reponse;
		for (var i = 0; i < choixList.length; i++) {
			if (choixList[i].checked) {
				choixItem = choixList[i].value
			}
		}
	}
	

// ancienne version
// var link = "http://scripts.canoe.ca/sondage/repondre_sondage.cgi?id_question="+escape(id_question)+"&url_result="+escape(url_result)+"&url_merci="+escape(url_merci)+"&reponse="+escape(choixItem);

// version finale ...
	var link = "http://scripts.canoe.ca/sondage/repondre_sondage.cgi?id_question="+escape(id_question)+"&url_result="+escape(url_result)+"&url_merci="+escape(url_merci)+"&reponse="+escape(choixItem) + "&id_region="+escape(id_region);
	
// version de test
//	var link = "http://scripts.canoe.ca/sondage/repondre_sondage.RV.cgi?id_question="+escape(id_question)+"&url_result="+escape(url_result)+"&url_merci="+escape(url_merci)+"&reponse="+escape(choixItem) + "&id_region="+escape(id_region);


	// Ellis Antaya - 2008-08-25
	// Refonte TVA
	// le nom de la variable dit tout ;)
	if (megahorriblepatch_canoef)	{
		// alert ('megahorriblepatch_canoef');
		window.open(link);
		return;
	}

	if (format == 'popup') {
		if (width == null) {
			width = 780;
		}

		if (height == null) {
			height = 600;
		}

		popupWin2("Resultat", link, width, height);
	} 
	else {
		top.location = (link);
	}
}


function popupWin2(popupName,url,popupWidth,popupHeight)
{
   win = window.open(url, popupName,"width=" + popupWidth + ",height=" + popupHeight + ",resizable=yes,scrollbars=yes,toolbar=no,status=no,location=no,directories=no");
   //bug de NN4 quand focus trop vite, attend 0,25 sec
   setTimeout('win.focus();',250);
   //win.location.reload(true);
   //return win;
}


function responseIsEmpty(nomCookie, form, lang, id_question)
{
   var radioLenth = form.reponse;
   var value = false;

   for(var i=0; i< radioLenth.length; i++)
   {
      if(radioLenth[i].checked)
      {
         value = true;
      }
   }
   if(!value)
      if (lang == 'E')
         alert("You didn't vote!");
	  else
         alert("Vous avez omis de voter!");
   else
      value = getCookie(nomCookie, lang, id_question);

   return value;
}

function responseIsEmpty_noRadio(nomCookie, form, lang, id_question)
{
   var reponse = form.reponse;
   var value = false;

   if(!reponse.value)
      if (lang == 'E')
         alert("You didn't vote!");
          else
         alert("Vous avez omis de voter!");
   else
      value = getCookie(nomCookie, lang, id_question);

   return value;
}

function readCookie (cookieName)
{
    if(document.cookie)
    {
       index = document.cookie.indexOf(cookieName)
       if (index != -1)
       {
           cookieStart = (document.cookie.indexOf("=", index) + 1)
           cookieStop = document.cookie.indexOf(";", index)
           if (cookieStop == -1)
           {
              cookieStop = document.cookie.length
           }

                   return unescape(document.cookie.substring(cookieStart, cookieStop));
       }
    }
    return null;
}

function getCookie(cookieName, lang, id_question)
{
        var presentDay = new Date();

        // LF 2006-03-07
        // now we will pass the id_question
        // (1 vote per question)
        // if we want the old method, then we set id_question = 0 from id_question_cookie !

        var count = readCookie (cookieName + '_' + id_question)

        incCookie(cookieName + '_' + id_question, count, id_question);

        if ((id_question > 0 && count == id_question) || (id_question == 0 && count == presentDay.getDate() ))
        //if (count == presentDay.getDate())
        {
	   if(id_question > 0)
	   {
             if (lang == 'E')
		alert('Only one vote per poll is permitted. Thanks for participating.')
             else
           	alert('Une seule participation est permise. Merci d\'avoir particip\351.')
	   }
	   else
	   {
             if (lang == 'E')
		alert('Only one vote per day is permitted. Thanks for participating.')
             else
           	alert('Une seule participation par jour est permise. Merci d\'avoir particip\351.')
	   }
	   return false;
        }
        else
        {
		//document.location  = "http://scripts.canoe.ca/sondage/resultat_sondage.cgi?id_question="+id_question;
		return true
        }
}
    

function incCookie(cookieName, count, id_question)
{
   var windowprops = "width=400,height=370,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes";

   var presentDay = new Date();

   if(id_question > 0)
   {
     count = id_question;
   }
   else
   {
     if (count)
     {
        count = presentDay.getDate();
     }
     else
     {
        count = presentDay.getDate();
     }
   }

  /* 2006/06/06 ngranger 
   * Add the path to force it to be "/"
   */
  /* document.cookie=cookieName + "=" + escape(count) + "; expires=Tuesday, 01-JAN-2100 08:00:00 GMT" */
   document.cookie=cookieName + "=" + escape(count) + "; path=/" + "; expires=Tuesday, 01-JAN-2100 08:00:00 GMT"



/*  Pour les tests
 * document.cookie=cookieName + "=" + escape(count) + "; expires=Monday, 01-JAN-2001 11:11:00 GMT"
 */
}

