if( typeof $j == "undefined" )
    $j = jQuery.noConflict();

/////////////////////////////////////
// Initialisation des FAQs
//////////////////////
function initFAQ()
{
	//showhide('faqLoading',0);
	//showhide('faqBody',1);
}
//window.onload = initFAQ;

/////////////////////////////////////
// Send form
//////////////////////
function sendform(topic)
{
	document.contact_in_form.Topic.value = topic;
	document.contact_in_form.submit();
}

/////////////////////////////////////
// Gestion des DIVs
//////////////////////
function getlyr(elemId)
{
	return((document.getElementById)? document.getElementById(elemId) : document.all(elemId));
}

function showhide(elemId,dp)
{
	var elemObj = $j("#"+elemId);
	//dp	? elemObj.show() : elemObj.hide();
	dp	? elemObj.slideDown() : elemObj.slideUp();
	
	//var elemObj = getlyr(elemId);
	//elemObj.style.display = (dp)? 'block' : 'none';
}

/////////////////////////////////////
// Engine
//////////////////////
var curQuest = 0;
var curRep = 0;

function disp(elemId,dp)
{
	showhide(elemId,dp);
	// gestion de la puce
	setimg(elemId);
}

function expandCollapse(elemId,typeRep,numQuest)
{
	var elemObj = getlyr(elemId);
	var elemNum = elemId.substring(1,elemId.length);
	var dp = (elemObj.style.display == 'none')? 1 : 0;
	// gestion des div ouverts
	// si une R différente est ouverte, la fermer
	if(curRep && (curRep != elemId))
	{
		disp(curRep,0);
		curRep = 0;
	}
	// s'il s'agit d'une R
	if(typeRep)
	{
		// assigne curRep
		curRep = (dp)? elemId : 0;
	}
	// s'il s'agit d'une Q
	else
	{
		// si une Q différente est ouverte, la fermer
		if(curQuest && (curQuest != elemId))
		{
			disp(curQuest,0);
			curQuest = 0;
		}
		// assigne curQuest
		curQuest = (dp)? elemId : 0;
		//document.contact_in_form.Topic[numQuest-1].checked = true;
		document.contact_in_form.TopicLabel.value = subStr[elemNum][1];
	}
	// ouvre ou ferme le DIV
	disp(elemId,dp);
}

/////////////////////////////////////
// Gestion des puces
//////////////////////
var finon = '3';

function setimg(elemImg)
{
	elemImg = eval('document.im'+arguments[0]);
	if(elemImg.src.lastIndexOf(finon) == -1) {fnsrc = '3'} else {fnsrc = '1'}
	elemImg.src = lnkimgmainpath+'/common/smallArrow'+fnsrc+'.gif';
}

/////////////////////////////////////
// Gestion de la numérotation
//////////////////////
var n = 0;
var m = 0;

function faqnum(){
	var num;
	switch(arguments[0])	{
		case 0:
			n++;
			m = 0;
			num = n+' ';
			break;
		case 1:
			m++;
			num = n+'.'+m+' ';
			break;
	}
	return num;
}

/////////////////////////////////////
// write DATAs
//////////////////////
function nl2br(str){
	return str.replace(/\n/g,'<br />');
}

function Treplace(){
	var str = arguments[0]
	for(i=1;i<arguments.length;i++){
		str = str.replace(new RegExp(arguments[i][0],"g"),arguments[i][1]);
	}
	return str;
}

function templateH2(lnkimgmainpath, i, txt, Qnum, forceArrow){
	//var display 		= Qnum == 9 ? 'block' : 'none';
	//var smallArrowNum	= Qnum == 9 ? '3' : '1';
	var display 		= 'none';
	var smallArrowNum	= typeof forceArrow == 'undefined' || forceArrow == false ? '1' : '3' ;
	var txthtm=
	'<div class="vipQuestionsGroup"><h2>'+
	'<img src="{{PATH}}/common/smallArrow{{NUM}}.gif" name="imQ{{I}}" id="imQ{{I}}" class="vipSmallArrow">'+
	'<a id="AQ{{I}}" href="javascript:expandCollapse(\'Q{{I}}\',0,{{QNUM}})" class="vipLink3b">'+
		'{{QNUM}}&nbsp;{{TXT}}'+
	'</a>'+
	'</h2></div>';
	
	return txthtm
				.replace(/{{PATH}}/g,lnkimgmainpath)
				.replace(/{{NUM}}/g,smallArrowNum)
				.replace(/{{I}}/g,i)
				.replace(/{{QNUM}}/g,Qnum)
				.replace(/{{TXT}}/g,txt)
	
	//return Treplace(txthtm, ['{{NUM}}',display], ['{{PATH}}',lnkimgmainpath], ['{{I}}',i], ['{{QNUM}}',Qnum], ['{{TXT}}',txt])
}

function templateH3(lnkimgmainpath, i, j, txt_arr, Qnum){
	var display 		= 'Q'+i+'x'+j == 'Q132x99' ? 'block' : 'none';
	var smallArrowNum	= 'Q'+i+'x'+j == 'Q132x99' ? '3' : '1';

	var txthtm=
	'<h3><img src="{{PATH}}/common/smallArrow{{NUM}}.gif" name="imQ{{I}}x{{J}}" id="imQ{{I}}R{{J}}" class="vipSmallArrow">'+
	'<a id="AR{{I}}" href="javascript:expandCollapse(\'Q{{I}}x{{J}}\',1)" class="vipLink3nu">'+
		'{{LINK}}'+
	'</a></h3>'+
	'<div id="Q{{I}}x{{J}}" class="vipFaqAnswer" style="display:{{SHOW}}">'+
		'{{TXT}}'+
	'</div>';

	return txthtm.replace(/{{PATH}}/g,lnkimgmainpath)
				.replace(/{{NUM}}/g,smallArrowNum)
				.replace(/{{I}}/g,i)
				.replace(/{{J}}/g,j)
				.replace(/{{LINK}}/g,faqnum(1)+txt_arr[0])
				.replace(/{{SHOW}}/g,display)
				.replace(/{{TXT}}/g,txt_arr[1])
}

function generateFaq( displayType )
{
	var txthtm = '';
	var divId = '';
	var counter = 0;
    // [ 0 : faq_log, 1 : faq_ex, 2:pending]
    var displayType = typeof(displayType)== 'undefined'? 0 : displayType;
	
    var isDisplayOff = displayType ? 0 : 1; 
    
	//for(var i=0;i<subStr.length;i++)
	for(var i in subStr)
	{
        
		var divId = 'Q'+i;
		var Qnum = faqnum(0);
		
		forceArrow = counter == 0 ? true :false;

		txthtm += templateH2(lnkimgmainpath, i, subStr[i][1], Qnum, forceArrow);

		divOption = counter == 0 ? '' : "style='display:none;'" ;
		txthtm +='<div id="'+divId+'" '+divOption+'>'

		var RR = (subStr[i].length>2)? RepRapide : noRepRapide;
		
		//if(Qnum != 9) txthtm += '<p>'+RR+'</p>';
		txthtm += '<p>'+RR+'</p>';

		for(var j=3;j<subStr[i].length;j++)
			if(subStr[i][j])
				txthtm+=templateH3(lnkimgmainpath, i, j, subStr[i][j], Qnum);
		
        //ciflib_bug_str = ciflib_bug.replace(/@@TOPIC@@/g,subStr[i][0]);
        ciflib_str = ciflib.replace(/@@TOPIC@@/g,subStr[i][0]);
        
        switch( displayType ){
            
            case 0:     // hide 1st & last
                        showFormLink = ( counter==0 || i==(subStr.length-1) ) ? false : true;
            break;
            
            case 1:     // show last
                        showFormLink = (i== subStr.length-1) ? true :false;
            break;
            
            case 2:
            default:    showFormLink = true;
            break;
        }
        // on affiche que le dernier pour !isDisplayOff
        if( showFormLink )
		    txthtm += '<br /><img src="'+lnkimgmainpath+'/common/smallArrow5.gif" class="vipSmallArrow">'+ciflib_str;
        
        txthtm += '</div>';
        
        /*
        if( true ){
        //txthtm += '<br /><img src="'+lnkimgmainpath+'/common/ultBulletType5.gif" class="imgMiddle">&nbsp;';
        //txthtm += '<a href="#" onclick="sendform(\''+subStr[i][0]+'\'); return false;" class="titleQuestionQ">-';
        txthtm += Qnum == 9 ? ciflib_bug :ciflib;
        //txthtm += '</a>';
        }
        */
		if( isDisplayOff )
			counter++;
	}
	return nl2br(txthtm);
}

function writefaq( isDisplayOff )
{
	var txthtmfinal = generateFaq( isDisplayOff );
	document.write(txthtmfinal);
	//$j('#vipFaq').html(txthtmfinal);
}



/*********************************************
 * 
 *      CONTACT_EX
 * 
 *********************************************/
function checkMail(elemobj)
{
	var verif = /^[a-zA-Z0-9\-\_]{1,}[a-zA-Z0-9\.\-\_]*[a-zA-Z0-9\-\_]{1,}@[a-zA-Z0-9]{1,}[a-zA-Z0-9\.\-\_]*[a-zA-Z0-9]{1,}[.][a-zA-Z]{2,4}$/;
	if(verif.test(elemobj.value)) return true;
	else return false;
}

function changeVal(myform,myelement)
{
	if(myelement.value != -1 && TopicId)
	{
		myform.SubTopic.value   = subStr[TopicId][myelement.value][0];
		myform.Sujet.value      = subStr[TopicId][myelement.value][2];
		myform.text.value       = subStr[TopicId][myelement.value][3];
	}
	else
	{
		myform.SubTopic.value   = '';
		myform.Sujet.value      = '';
		myform.text.value       = '';
	}
}

var TopicId = null;

function majSel(myform,val)
{
	TopicId = val;
	myform.Topic.value = subStr[val][0];
	myform.text.value = '';

	var targObj = document.getElementById('SubTopicChoice');
	var targElement = myform.SubTopicChoice;

	while(targElement.length>0) targElement.options[0] = null;
	targElement.options[0] = new Option(Choose,"-1",false,false);
	for(var i=2;i<subStr[val].length;i++)
	{
		if(subStr[val][i]) targElement.options[targElement.length] = new Option(subStr[val][i][1],i,false,false);
	}
	targObj.disabled = false;

// Init
	myform.FirstName.value = ' ';
	myform.LastName.value = ' ';
	myform.Pseudo.value = ' ';

// show/hide
	if(subStr[val][0] == 'MEMBRE')
	{
		showhide('memDiv',1);
		showhide('visDiv',0);
		myform.Pseudo.value = '';
	}
	else
	{
		showhide('memDiv',0);
		showhide('visDiv',1);
		myform.FirstName.value = '';
		myform.LastName.value = '';
	}
}

function writeSel()
{
	TopicId = subStr.length-1;
	var sel = '<option value="-1">'+Choose+'</option>';
	for(var i=2;i<subStr[TopicId].length;i++)
	{
		if(subStr[TopicId][i])
		{
			sel += '	<option value="'+i+'">'+subStr[TopicId][i][1]+'</option>';
		}
	}
	document.write(sel);
}



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

		CONTACT_IN_FORM

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


$j(document).ready(function(){
	$j('form[name=contact_req_in]').submit(function(){
		
	/*
	var SubTopicChoice  = $j('#SubTopicChoice').val();
	var msg = $j('#vipContactMessageElement').val();
	
	if(  msg  == '' || SubTopicChoice <= 0 ){
		
		message  = msg == '' ? 'veuillez remplir votre message' : '';
		message += SubTopicChoice <= 0 ? 'veuillez remplir votre message' : '';
		
		alert( message );
	    return false;

	}	
	
	
	    return true;

	    */
	    
	    
	    
	});
	
	
	$j("#vipBtnSubmit").click( checkreqinmsg )
			
	$j('#vipContactMessageElement').click( checkreqintitle )
	
});


	/////////////////////////////
	function initForm()
	{
		var targElement = document.getElementById("SubTopicChoice");
		if(!targElement) return false;
		targElement.options[0] = new Option(Choose,"-1",false,false);
		for(var i=0;i<subStr.length;i++)
		{
			if(subStr[i])
				targElement.options[targElement.length] = new Option(subStr[i][1],i,false,false);
		}
		return true;
	}
	
	
	function changeSelectVal(myform,myelement)
	{
		if(myelement.value != -1)
		{
			myform.SubTopic.value = subStr[myelement.value][0];
			myform.Sujet.value = subStr[myelement.value][2];
			myform.text.value = subStr[myelement.value][3];
		}
		else
		{
			myform.SubTopic.value = '';
			myform.Sujet.value = '';
			myform.text.value = '';
		}
	}
	
	function checkreqintitle()
	{
		var myform = document.contact_req_in;
		var letsgo = true;
		var warning = "Attention";
		
		if(myform.SubTopic.value == '')
		{
			letsgo = false;
			warning += "Vous n'avez pas choisi de sujet.";
		}
		
		if(letsgo == false)
		{
			alert(warning);
		}
	}
	
	function checkreqinmsg()
	{
		var myform = document.contact_req_in;
		
		affect(myform);
		var letsgo = 1;
		var warning = "Attention";
		if(myform.SubTopic.value == '')
		{
			letsgo = 0;
			warning += "Vous n'avez pas choisi de sujet.";
		}
		if(myform.text.value == '')
		{
			letsgo = 0;
			warning += "Vous n'avez pas saisi de message.";
		}
		if(letsgo) document.contact_req_in.submit();
		else
		{
			alert(warning);
		}
	}



