// JavaScript Document


function montreCacheDiv(id,etat){
	
	obj = document.getElementById(id);
	
	if(etat==1){
		obj.style.display = 'block';
	}else{
		obj.style.display = 'none';
	}
	
}


function strip_tags(str, allowed_tags) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Luke Godfrey
    // +      input by: Pul
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +      input by: Alex
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Marc Palau
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brettz9.blogspot.com)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Eric Nagel
    // +      input by: Bobby Drake
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i><b>');
    // *     returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>'
    // *     example 2: strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>');
    // *     returns 2: '<p>Kevin van Zonneveld</p>'
    // *     example 3: strip_tags("<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>", "<a>");
    // *     returns 3: '<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>'
    // *     example 4: strip_tags('1 < 5 5 > 1');
    // *     returns 4: '1 < 5 5 > 1'
 
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';
 
    var replacer = function(search, replace, str) {
        return str.split(search).join(replace);
    };
 
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
    }
  
    str += '';
 
    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);
 
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }
 
        // Save HTML tag
        html = matches[key].toString();
 
        // Is tag not in allowed list? Remove from str!
        allowed = false;
 
        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;
 
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
 
            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }
 
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
 
    return str;
}



function supprimerStyle(id,style){

	htmlListe = '<br><p><a><h1><h2><h3><h4><h5><h6><pre><b><u><i><em><strike><strong><ul><li><ol><div><td><tr><table><sup><sub><script><area><font><param><object><img><map>';
	
	html = id;

	if(style == 4){
		html = strip_tags(html,'<img><p><br><a>');
	}	
	
	if(style == 1){
		htmlListe.replace('<span>', '');
		html = strip_tags(html,htmlListe.replace('<span>', ''));
	}
	
	if(style == 2){
		htmlListe.replace('<em>', '');
		htmlListe.replace('<b>', '');
		htmlListe.replace('<i>', '');
		htmlListe.replace('<u>', '');
		htmlListe.replace('<strike>', '');
		htmlListe.replace('<strong>', '');
		html = strip_tags(html,htmlListe);
	}
	if(style == 3){
		htmlListe.replace('<h1>', '');
		htmlListe.replace('<h2>', '');
		htmlListe.replace('<h3>', '');
		htmlListe.replace('<h4>', '');
		htmlListe.replace('<h5>', '');
		htmlListe.replace('<h6>', '');
		htmlListe.replace('<pre>', '');
		htmlListe.replace('<address>', '');
		htmlListe.replace('<div>', '');
		html = strip_tags(html,htmlListe);
	}
	alert(html);
	id = html;
	
}

function antis(){
	
	if(document.getElementById('texte')){
		document.getElementById('as').value = document.getElementById('texte').value;
	}
	
}


function avertissement_paypal(){
		
		window.open('avertissement_paypal.html','avertissements','width=480,height=300');
	
}

// ----- Checkbox Check All
function checkAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}



var formblock;
var forminputs;
 
function prepare() {
	if(document.getElementById('courrier')){
		//formblock= document.getElementById('courrier');
		//forminputs = formblock.getElementsByTagName('input');
		forminputs = document.getElementsByTagName('input');
	}
	
}
 
function select_all(name, value) {
  for (i = 0; i < forminputs.length; i++) {
    // regex here to check name attribute
    var regex = new RegExp(name, "i");
    if (regex.test(forminputs[i].getAttribute('name'))) {
      	if (value == 1) {
		  if(forminputs[i].disabled == false){
        	forminputs[i].checked = true;
		  }
      	} else {
        	forminputs[i].checked = false;
  		}
		
		if(select_all.arguments[2]){
			aL(forminputs[i].value);
		}
		
    }
  }
}
 
if (window.addEventListener) {
  window.addEventListener("load", prepare, false);
} else if (window.attachEvent) {
  window.attachEvent("onload", prepare)
} else{ 
  window.onload = prepare;
}


// ----- Checkbox Check All

function connexion(){
	
	document.getElementById('connexionMessage').innerHTML = 'Connexion en cours...';
	
	var xhr; 
	try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
	catch (e) 
	{
		try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
		catch (e2) 
		{
		  try {  xhr = new XMLHttpRequest();     }
		  catch (e3) {  xhr = false;   }
		}
	 }
 
	xhr.onreadystatechange  = function()
	{ 
		 if(xhr.readyState  == 4)
		 {
				if(xhr.status  == 200){ 
					
					if(xhr.responseText!=''){
						
						if(xhr.responseText>=1){
						
							document.getElementById('connexionMessage').innerHTML = 'Connexion réussie';
							
							if(xhr.responseText==2){
								document.location = 'index.php?p=profil';
							}else{
								document.location = 'index.php?p=gestion';
							}
							
						}else{
							
							
							if(xhr.responseText==-1){
								document.getElementById('connexionMessage').innerHTML = 'Veuillez inscrire votre nom d\'utilisateur et votre mot de passe.';
							}else{
								document.getElementById('connexionMessage').innerHTML = 'Veuillez vérifier votre nom d\'utilisateur et votre mot de passe.';
							}

						}
						

						
					}else{
						
						document.getElementById('connexionMessage').innerHTML = 'Erreur de connexion.';
						
					}

				}
				
		 }
		 
	}; 

   xhr.open( "POST", "fonction/connexion.php",  true); 
   xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   xhr.send("u="+document.getElementById('nick').value+"&p="+document.getElementById('pw').value); 
   
}


function Set_Cookie( name, value, expires, path, domain, secure ){
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
