var clientPC = navigator.userAgent.toLowerCase();
var clientVer = parseInt(navigator.appVersion);

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_moz = 0;

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);

function storeCaret(textEl)
{
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2)
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd);
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}

function insertexte(pretexte, postexte, champ, mode)
{
	if (champ)
	{
		if (mode && mode == 'id')
			var txtarea = $(champ);
		else
			var txtarea = document.post.eval(champ);
	}
	else
		var txtarea = document.post.text;
	
	
	if (postexte == "")
	{
		if (txtarea.createTextRange && txtarea.caretPos)
		{
			var caretPos = txtarea.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + pretexte + ' ' : caretPos.text + pretexte;
		}
		else if ( txtarea.selectionStart || txtarea.selectionStart == 0 )
		{
			var before = (txtarea.value).substring(0, txtarea.selectionStart);
			var after  = (txtarea.value).substring(txtarea.selectionEnd, txtarea.textLength);
			txtarea.value = before + pretexte + after;
		}
		else
		{
			txtarea.value  += pretexte;
		}
	}
	else
	{
		if ((clientVer >= 4) && is_ie && is_win)
		{
			theSelection = document.selection.createRange().text;
			if (!theSelection)
			{
				txtarea.value += pretexte + postexte;
				txtarea.focus();
				return;
			}
			document.selection.createRange().text = pretexte + theSelection + postexte;
			txtarea.focus();
			return;
		}
		else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
		{
			mozWrap(txtarea, pretexte, postexte);
			return;
		}
		else
		{
			txtarea.value += pretexte + postexte;
			txtarea.focus();
		}
		storeCaret(txtarea);
	}
}


function lien(champ, target, mode)
{
	if (champ)
	{
		if (mode && mode == 'id')
			var txtarea = $(champ);
		else
			var txtarea = document.post.eval(champ);
	}
	else
		var txtarea = document.post.text;
	
	if (champ == null) champ = 'text';
	if (target == null || target == '') target = 'link';
	
	var saisie = prompt("Saisissez l'adresse du lien", "http://")
	if (saisie!=null)
	{
		saisietmp="[" + target + "=" + saisie + "]";
		var saisie2 = prompt("Saisissez le nom du lien (optionnel)", "")
		if (saisie2!=null)
		{
			if (saisie2=="")
			{
				txtarea.value+=saisietmp + saisie + "[/" + target + "]";
			}
			else
			{
				txtarea.value+=saisietmp + saisie2 + "[/" + target + "]";
			}
		}
	}
}

function smiley(text, champ, mode, container)
{
	if (champ)
	{
		if (mode && mode == 'id')
		{
			if (container == 'popup')
				var txtarea = opener.$(champ);
			else
				var txtarea = $(champ);
		}
		else
		{
			if (container == 'popup')
				var txtarea = opener.document.post.eval(champ);
			else
				var txtarea = document.post.eval(champ);
		}
	}
	else
	{
		if (container == 'popup')
			var txtarea = opener.document.post.text;
		else
			var txtarea = document.post.text;
	}
	text = ' ' + text + ' ';
	
	if (txtarea.createTextRange && txtarea.caretPos)
	{
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
	}
	else if ( txtarea.selectionStart || txtarea.selectionStart == 0 )
	{
		var before = (txtarea.value).substring(0, txtarea.selectionStart);
		var after  = (txtarea.value).substring(txtarea.selectionEnd, txtarea.textLength);
		txtarea.value = before + text + after;
	}
	else
	{
		txtarea.value  += text;
	}

	txtarea.focus();
}

function smiley_pop(text, champ, mode)
{
	smiley(text, champ, mode, 'popup');
}

function popup_profil(num)
{
	window.open('/view_profil.php?var=accueil&profil_id='+num, '', 'HEIGHT=500,resizable=yes,WIDTH=900,scrollbars=yes');
}

function popup_smileys(champ,mode)
{
	window.open('/smileys.php?champ='+champ+'&mode='+mode, '', 'HEIGHT=600,resizable=yes,WIDTH=700,scrollbars=yes');
}

function popup(page, titre)
{
	if (titre == null)
		titre = 'popup';
	window.open(page, titre, 'HEIGHT=400,resizable=yes,WIDTH=600,scrollbars=yes');
}

function popupSimple(page,titre)
{
	window.open(page, titre, 'HEIGHT=400,resizable=yes,scrollbars=yes,WIDTH=600');
}

function popupmi(page)
{
	window.open(page, 'titre', 'HEIGHT=500,resizable=yes,WIDTH=1000');
}

function evopopup(page, height, width)
{
	window.open(page, '', 'HEIGHT='+height+',WIDTH='+width+',resizable=yes,scrollbars=yes');
}

function test_champs(champ)
{
	if (champ.value=='')
	{
		alert("Vous devez saisir un sujet");
		return false;
	}
	return true;
}


var tab_ajout = new Array();
var tab_retrait = new Array();
var ia=0;
var ir=0;


function passage(combo1,combo2,sens)	//PERMET DE FAIRE PASSER LES MODERATEURS D UNE COMBO A UNE AUTRE
{
	var existence="";
	if (combo1.selectedIndex != -1)	//TEST AUCUN NICK SELECTIONNé
	{
		nomOption = new Option(combo1.options[combo1.selectedIndex].text, combo1.options[combo1.selectedIndex].value);
		combo2.options[combo2.options.length]=nomOption;
		
		if (sens=="ajout")
		{
			for (j=0;j<tab_retrait.length;j++)
			{
				if (combo1.options[combo1.selectedIndex].value == tab_retrait[j])
				{
					tab_retrait[j]=-1;
					existence="true";
				}
			}
			
			if (existence!="true")
			{
				tab_ajout[ia]=combo1.options[combo1.selectedIndex].value;
				ia++;
			}
		}
		
		if (sens=="retrait")
		{
			for (j=0;j<tab_ajout.length;j++)
			{
				if (combo1.options[combo1.selectedIndex].value == tab_ajout[j])
				{
					tab_ajout[j]=-1;
					existence="true";
				}
			}
			
			if (existence!="true")
			{
				tab_retrait[ir]=combo1.options[combo1.selectedIndex].value;
				ir++;
			}
		}
		
		var serial_tab_ajout = "";
		var serial_tab_retrait = "";
		
		for (z=0;z<tab_ajout.length;z++)
		{
			if (tab_ajout[z] != -1)
			{
				serial_tab_ajout += tab_ajout[z] + ",";
			}
		}
		
		for (z=0;z<tab_retrait.length;z++)
		{
			if (tab_retrait[z] != -1)
			{
				serial_tab_retrait += tab_retrait[z] + ",";
			}
		}
		
		document.form.h_ajout.value = serial_tab_ajout;
		document.form.h_retrait.value = serial_tab_retrait;
		
		combo1.options[combo1.selectedIndex]=null;
	}
}


//hack boutique

var tab = new Array();

function calcul(num,prix)
{
	t_num="quantite_total_"+num;
	s_num="quantite_"+num;
	
	
	document.forms["formu"].elements[t_num].value = Math.round((eval(prix)*document.forms["formu"].elements[s_num].value)*100)/100;
	
	tab[num]=eval(prix)*document.forms["formu"].elements[s_num].value;
	
	
	var total_final = 0;
	var tour = 0;	//pour sauter un champs sur 2
	for (var l = 0; l < document.forms["formu"].elements.length; l++)
	{
		if (document.forms["formu"].elements[l].name != "totaltext")
		{
			if (tour == 1)
			{
				valeur=document.forms["formu"].elements[l].value;
				total_final += eval(valeur);
				
				tour = 0;
			}
			else
			{
				tour =1;
			}
		}
	}
	document.forms["formu"].totaltext.value = Math.round(total_final*100)/100;
}

function test_champs_boutique()
{
	if (document.forms["form"].elements["nom"].value == '')
	{
		alert("Vous devez saisir votre nom");
		return false;
	}
	if (document.forms["form"].elements["prenom"].value == '')
	{
		alert("Vous devez saisir votre prénom");
		return false;
	}
	if (document.forms["form"].elements["adresse"].value == '')
	{
		alert("Vous devez saisir votre adresse");
		return false;
	}
	if (document.forms["form"].elements["cp"].value == '')
	{
		alert("Vous devez saisir cotre code postal");
		return false;
	}
	if (document.forms["form"].elements["ville"].value == '')
	{
		alert("Vous devez saisir votre ville");
		return false;
	}
	if (document.forms["form"].elements["mail"].value == '')
	{
		alert("Vous devez saisir votre mail");
		return false;
	}
	return true;
}

function test_champs_recherche()
{
    if (document.forms["form1"].elements["nom"].value == '')
	{
		alert("Vous devez saisir un mot clé.");
		return false;
	}
	return true;
}

function popupOriginalCover(url,num_cover)
{
	window.open('http://www.cinewax.com/site/popup_img.php?' + num_cover + '=' + url,'CoverViewer','width=320,height=240,resizable=yes,scrollbars=yes');
}

function popupOriginalfond(url,num_fd)
{
	window.open('http://www.cinewax.com/site/popup_img_fond.php?' + num_fd + '=' + url,'WallpaperViewer','width=320,height=240,resizable=yes,scrollbars=yes');
}


function pop_mi()
{
	var pop = document.getElementById('popmi');
	
	if (pop)
	{
		pop.style.display = 'block';
	}
	if (pop)
	{
		pop.onclick = function() {this.style.display = 'none';};
	}
}

function soumettre(formulaire, champ)
{
	if(document.forms[formulaire].elements[champ].value == '')
	{
		alert('Veuillez saisir un nom de jaquette');
		return false;
	}
	else if(document.forms[formulaire].elements[champ].value.length <= 2)
	{
		alert('Veuillez saisir plus de 2 caractères');
		return false;
	}
	else
		return true;
}


function display(swf, hauteur, largeur, alternatif)
{
	document.write('<object type="application/x-shockwave-flash" data="'+swf+'" width="'+hauteur+'" height="'+largeur+'">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="'+swf+'" />');
	document.write(alternatif);
	document.write('</object>');
}

function displayFlash(swf, hauteur, largeur, alternatif)
{
	document.write('<object type="application/x-shockwave-flash" data="'+swf+'" width="'+hauteur+'" height="'+largeur+'">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="'+swf+'" />');
	document.write(alternatif);
	document.write('</object>');
}


function onOff(id,bool)
{
	var element = document.getElementById(id);
	if(element)
	{
		if(bool)
		{
			element.style.display = '';
		}
		else
		{
			element.style.display = 'none';
		}
	}
}

function onOf(id)
{
	var element = document.getElementById(id);
	if(element)
	{
		if(element.style.display == 'none')
		{
			element.style.display = 'block';
		}
		else
		{
			element.style.display = 'none';
		}
	}
}

function menuDeroulantMontre(id) 
{
	var d = document.getElementById(id);
	for (var i = 1; i<=10; i++) 
	{
		if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
	}
	if (d)
	{
		d.style.display='block';
	}
}

function openFelicitation()
{
	$('felicitation').style.display = '';
}




// Menus dynamiques
var timerID = null;
var timerOn = false;
var timecount = 500;
var menudown =0;

function findObj(theObj, theDoc)
{
  var p, i, foundObj;  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length){
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);}
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  return foundObj;
}

function show1calq(calque){
	var obj;
	if ((obj = findObj(calque)) != null){
		if (obj.style){
    	  obj = obj.style;
				obj.visibility='visible';}
		else obj.visibility='show';
	}
	else
		alert('ezr');
}
function hide1calq(calque){
	var obj;
	if ((obj = findObj(calque)) != null){
		if (obj.style){
    	  obj = obj.style;
				obj.visibility='hidden';}
		else obj.visibility='hide';
	}
}

function showMenu(nummenu){
	stopTim();
	closeMenu();
	menudown=nummenu;
	show1calq(menudown);
}
function closeMenu(){
	if (menudown){hide1calq(menudown);menudown=0;}
}
function startTim() {if (timerOn == false) {timerID=setTimeout( "closeMenu()" , timecount);timerOn = true;}}
function stopTim() {if (timerOn) {clearTimeout(timerID);timerID = null;timerOn = false;}}

function nl2br (str, is_xhtml)
{
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');    // *     returns 1: 'Kevin\nvan\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '\nOne\nTwo\n\nThree\n'    
	var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '' : '<br>';
 
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}


