/*********************************************************************
Fichier: controle.js
Createur: VDENG (vdeng@ifrance.com)
Date de creation: 13/06/2005

**********************************************************************/

var cALERTE = "#FBF49F";     // couleur de fond pour les champs invalides
var cDERNIER_PUB_H = -2;     // dernière publicité horizontal
var cDERNIER_PUB_V = -2;     // dernière publicité vertical
var cDERNIER_PUB_C = -2;     // dernière publicité carré
var cDERNIER_PUB_I = -2;     // dernière photo du membre

var cINTERVALLE_PUB_H = 50000; // 50 secondes entre 2 publicités
var cINTERVALLE_PUB_V = 40000; // 40 secondes entre 2 publicités
var cINTERVALLE_PUB_C = 30000; // 30 secondes entre 2 publicités
var cINTERVALLE_PUB_I = 3000;  // 3  secondes entre 2 photos du membre

var cALERT_POPUP;       // Popup pour afficher les messages d'alerte
var cCONNEXION_POPUP;   // Popup pour la connexion

/***********************************************************
Redirection
************************************************************/

function SubmitPageUnique(aControl)
{
    if (disabledButton(aControl.name))
        document.forms["REDIRECTION"].submit();
}

/*
 *
 *
 */
function ChangerPageUnique(aControl, id, page, estHREF)
{
	if (disabledButton(aControl.name))
	{
		ChangerPage(id, page, estHREF);
	}
}

//Eviter 2 evenements successifs (propagation evenement)
var isReady = true;
var datetDebut = (new Date()).getTime();

function ChangerPage(id, page, estHREF)
{
	if (isReady || ((new Date()).getTime() - datetDebut) / 1000 > 3)
	{
		isReady = false;
		datetDebut = (new Date()).getTime();
		v_page = page;
		v_id = id;

		//Si la page contient deja les parametres necessaires
		if (v_id == null || v_id == '')
		{
			var chaine = '?PARAMETRE=';
			var pos = v_page.indexOf(chaine);
			if (pos > 0)
			{
				v_id = v_page.substring(pos+chaine.length);
				v_page = v_page.substring(0,pos);
			}
		}

		//Redirection en HREF IE sans les valeurs du formulaire
		if (estHREF == 'HREF')
		{
			if (v_id == null || v_id == '')
				window.location = v_page;
			else
				window.location = v_page+"?PARAMETRE="+v_id;
		}
		else
		{
			document.forms["REDIRECTION"].PARAMETRE.value = v_id;
			document.forms["REDIRECTION"].action = v_page;
			document.forms["REDIRECTION"].submit();
		}
	}
}

/***********************************************************
Redirection dans n secondes
************************************************************/

function ChangerPageAuto(id, page, attente)
{
    setTimeout("ChangerPage('" + id + "','" + page + "');", attente);
}

/***********************************************************
Redirection avec confirmation
************************************************************/

function ChangerPageConfirmer(id, page, message)
{
    if ( confirm(message) )
        ChangerPage(id, page);
}


/***********************************************************
Donner le focus à un champs
************************************************************/

function getFieldByName(myField, exactMatch)
{
    if (!myField)
        return null;

    found = false;
    formElement = null;
    for (var j=0; (j<document.forms.length) && !found; j++)
    {

        var myForm = document.forms[j];
        for (var i=0; ( i < myForm.elements.length) && !found; i++)
        {
            formElement = myForm.elements[i];
            if (formElement.name==myField)
                found = true;
        }
    }
    if (found)
        return formElement;
    else
        return null;
}

function setFocusToField(myField, estClignotement, estFocusInterdit)
{
    if (myField != '')
    {
        // Focus
        theElement = getFieldByName(myField);
        if (theElement != null && !theElement.disabled && estFocusInterdit==null)
            theElement.focus();

        // Sélectionner tout le texte
        if (theElement != null && !theElement.disabled && theElement.type=='text' && estFocusInterdit==null)
            theElement.select();

        // Colorie en jaune si traitement
        if (theElement != null && !theElement.disabled)
            theElement.style.backgroundColor = cALERTE;
    }

    // Message
    if (estClignotement=='O')
    {
        var message = document.forms["REDIRECTION"].MessageErreurCache.value;
        if (message!=null)  // On désactive pour l'instant
        {
            if (myField != '' && (myField=='PSEUDO' || myField=='MOT_DE_PASSE'))
            {
                cCONNEXION_POPUP=dhtmlmodal.open('connexionbox', 'div', 'modalconnexiondiv', 'Connexion au site AsianFever.Fr', 'width=375px,height=175px,center=1,resize=0,scrolling=0', 'recal');

                // Focus
                theElement = getFieldByName('DIV_' + myField);
                if (theElement != null && !theElement.disabled && estFocusInterdit==null)
                    theElement.focus();

                // Sélectionner tout le texte
                if (theElement != null && !theElement.disabled && (theElement.type=='text' || theElement.type=='password') && estFocusInterdit==null)
                    theElement.select();

                // Colorie en jaune si traitement
                if (theElement != null && !theElement.disabled)
                    theElement.style.backgroundColor = cALERTE;
            }
            else if (1==2)
            {
                cALERT_POPUP=dhtmlmodal.open('alertbox', 'div', 'modalalertdiv', 'AsianFever.Fr - Message', 'width=375px,height=175px,center=1,resize=0,scrolling=0', 'recal');
            }
            else
            {
                alert(message);
            }
        }
    }
}

function clignotement(estDernier)
{
    theElement = document.getElementById("MessageErreur");
    if (theElement != null)
    {
        if (estDernier || theElement.style.visibility == 'hidden')
            theElement.style.visibility = 'visible';
        else
            theElement.style.visibility = 'hidden';
    }
}

/***********************************************************
Activer la touche ENTREE
************************************************************/

NS4 = (document.layers) ? true : false;

	/**
	 * Intercepte la saisie de la touche 'Retour Chariot' et appelle la fonction ChangerPage()
	 */
	function checkEnter(event, id, page)
	{
		var code = 0;
		var controle;
		
		//Syntaxe JS differente en fonction du navigateur
		if (NS4)
		{
			code = event.which;
			controle = event.target;
		}
		else
		{
			code = event.keyCode;
			controle = event.srcElement;
		}
		
		//Si la touche 'Retour Chariot' est saisie
		if (code == 13)
		{
			ChangerPage(id, page);
		}
	}

	/**
	 * Intercepte la saisie de la touche 'Retour Chariot', desactive la touche et appelle la fonction ChangerPage()
	 */
	function checkEnter1(event, id, page, aBouton)
	{
		var code = 0;
		var controle;
		
		//Syntaxe JS differente en fonction du navigateur
		if (NS4)
		{
			code = event.which;
			controle = event.target;
		}
		else
		{
			code = event.keyCode;
			controle = event.srcElement;
		}
		
		//Si la touche 'Retour Chariot' est saisie
		if (code == 13)
		{
	        if (disabledButton(aBouton))
	        {
	        	ChangerPage(id, page);
	        }
		}
	}

function submitEnter(event, pControl)
{
	var code = 0;
	var controle;
	if (NS4)
	{
		code = event.which;
		controle = event.target;
	}
	else
	{
		code = event.keyCode;
		controle = event.srcElement;
	}
	if (code==13)
	{
	    var theElement = getFieldByName(pControl);
	    if (theElement != null)
	        theElement.disabled = true;

	    document.forms["REDIRECTION"].submit();
	}
}

/***********************************************************
Ouvre un popup unique, focus, et centré SPECIAL IMAGE
************************************************************/
function ImageMax(chemin, largeur, hauteur)
{
    if (largeur == null)
        largeur = 640;
    if (hauteur == null)
        hauteur = 480;

    var topPosition  = (screen.height-hauteur)/2;
    var leftPosition = (screen.width-largeur)/2;
    var proprieteEcran = 'toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=0,top=' + topPosition + ',left=' + leftPosition + ',width=' + largeur + ',height=' + hauteur;
    html = '<html><head><title>CityNight.Fr</title></head><body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" onBlur="top.close()"><IMG src="'+chemin+'" width="'+largeur+'" height="'+hauteur+'" border="0"></body></html>';
    popupImage = window.open('','_blank',proprieteEcran);
    popupImage.document.open();
    popupImage.document.write(html);
    popupImage.document.close();
}

/***********************************************************
Ouvre un popup unique, focus, et centré
************************************************************/
function popupnr(mylink, largeur, hauteur)
{
    if (largeur == null)
        largeur = 400;
    if (hauteur == null)
        hauteur = 210;

    var mywin, href;
    var topPosition  = (screen.height-hauteur)/2;
    var leftPosition = (screen.width-largeur)/2;
    var proprieteEcran = 'top=' + topPosition + ',left=' + leftPosition + ',width=' + largeur + ',height=' + hauteur +',scrollbars=yes';

    if (typeof(mylink) == 'string')
        href=mylink;
    else
        href=mylink.href;
    mywin = window.open('', 'CITYNIGHT', proprieteEcran);

    // on ferme l'ancienne popup
    if (!(
       mywin.closed ||
       (! mywin.document.URL) ||
       (mywin.document.URL.indexOf("about") == 0)
       ))
    {
        // On ferme l'ancien popup
        if (mywin.document)
            mywin.close();

        mywin = window.open('', 'CITYNIGHT', proprieteEcran);
    }

    mywin.location=href;

    // Focus
    mywin.focus();

    return false;
}

/***********************************************************
Permet de faire un calcul coté serveur sans recharger la page
************************************************************/

var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try
{
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
    try
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (E)
    {
        xmlhttp = false;
    }
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
    xmlhttp = new XMLHttpRequest();
}

function loadFragmentInToElement(fragment_url, pControl)
{
    var aControl = getFieldByName(pControl);
    aControl.value = 'Loading ...';
    xmlhttp.open("GET", fragment_url);
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            aControl.value = xmlhttp.responseText;
        }
    }
    xmlhttp.send(null);
}

function ControleServeur(pFonction, pControl)
{
  var aControl = getFieldByName(pControl);
  var sUrl = 'controle_serveur.php?CONTROLE=' + pFonction + '&VALEUR=' + aControl.value;
  xmlhttp.open("GET", sUrl);
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      aControl.value = xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
}

/**********************************
Nombre aléatoire entre 0 et n-1
***********************************/
function rand_number(n)
{
    var x;
    x=Math.round(Math.random()*100);
    x%=n;
    return x;
}

/**********************************
Tags Smart Adserver pour CityNight.fr
***********************************/
sas_tmstp=Math.round(Math.random()*10000000000);sas_masterflag=1;
function SmartAdServer(sas_pageid,sas_formatid,sas_target) {
 if (sas_masterflag==1) {sas_masterflag=0;sas_master='M';} else {sas_master='S';};
 document.write('<SCR'+'IPT SRC="http://www.smartadserver.com/call/pubj/' + sas_pageid + '/' + sas_formatid + '/'+sas_master + '/' + sas_tmstp + '/' + escape(sas_target) + '?"></SCR'+'IPT>');
}

/**********************************
Empêcher de faire plusieurs SUBMIT
***********************************/
var counter = 0;
function submitOnce(aBouton)
{
    counter++;
    if (counter>1)
    {
        return false;
    }
    else
    {
        // Désactiver le bouton de soumission
        disabledButton(aBouton);
        return true;
    }
}

/*
 * Disabled submit button
 *
 */
function disabledButton(aBouton)
{
	var retour = false;
	var bouton = null;

	if (aBouton != null)
	{
		//Desactiver le bouton pour eviter plusieurs soumissions
		bouton = getFieldByName(aBouton);
		if (bouton != null)
		{
			retour = !bouton.disabled;
			bouton.disabled = true;
			bouton.className = 'button_disabled';
		}
	}

	//Desactiver INPUT btnConnexion
	if (bouton != null && bouton.name != 'btnConnexion')
	{
		aControl = getFieldByName('PSEUDO');
		if (aControl != null)
		{
			aControl.disabled = true;
		}

		aControl = getFieldByName('MOT_DE_PASSE');
		if (aControl != null)
		{
			aControl.disabled = true;
		}

			aControl = getFieldByName('AUTO');
		if (aControl != null)
		{
			aControl.disabled = true;
		}
	}

	//Desactiver INPUT btnDivConnexion
	if (bouton != null && bouton.name != 'btnDivConnexion')
	{
		aControl = getFieldByName('DIV_PSEUDO');
		if (aControl != null)
		{
			aControl.disabled = true;
		}
		
		aControl = getFieldByName('DIV_MOT_DE_PASSE');
		if (aControl != null)
		{
			aControl.disabled = true;
		}
		
		aControl = getFieldByName('DIV_AUTO');
		if (aControl != null)
		{
			aControl.disabled = true;
		}
	}

	//Desactiver la newsletter
	if (bouton != null && bouton.name != 'btnPubOK')
	{
		aControl = getFieldByName('PUB_EMAIL');
		if (aControl != null)
		{
			aControl.disabled = true;
		}
	}
	
	return retour;
}

/*************************************************************************************************/

function selectAllCheckboxes(obj)
{
	var checkboxes = document.getElementsByTagName('input');
	for(var i=0; i<checkboxes.length; i++)
	{
		if(checkboxes[i].type == 'checkbox' && checkboxes[i].name == obj.name + '[]')
		{
			checkboxes[i].checked = obj.checked;
		}
	}
}
