

/**************************************************************/
/*   Design:   Default                                        */
/*   File:     Common javascript for sgc                      */
/*------------------------------------------------------------*/
/*   Author:   Thot                                           */
/*   Date:     April 11, 2008                                 */
/*   Homepage: wwww.olivier-camard.net                        */
/*------------------------------------------------------------*/
/*   License:  Fully open source without restrictions.        */
/*             Please keep footer credits with a link to      */
/*             wwww.o-c-d.fr. Thank you!                      */
/**************************************************************/

/**************************************************************/
/**************************************************************/
/****************                              ****************/
/****************    common.sgc.ajax.js     ****************/
/****************    16/05/2011 à 01:30:29.    ****************/
/****************                              ****************/
/**************************************************************/
/**************************************************************/

function sgc_class_ajax() {
	this.asyn = true;
	this.data = "";
	this.url = "";
	this.target_id = "" ;
	this.method = "GET";
	this.returnFormat = "txt";
	this.xhr;
	this.init();
	
}
sgc_class_ajax.prototype.init = function() {
	this.xhr = null; 
	if (window.XMLHttpRequest) {
		this.xhr = new XMLHttpRequest(); 
	} else if (window.ActiveXObject) {
		var ieversions = ['Msxml2.XMLHTTP','Microsoft.XMLHTTP','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0']; 
		for(var i=0; !this.obj && i<ieversions.length; i++) {
			try {
				this.xhr = new ActiveXObject(ieversions[i]); 
			} catch(e) { } 
		}
	}
}
sgc_class_ajax.prototype.onComplete = function (response) {
	var element = document.getElementById(this.target_id) ;
	if(response.substr(0,5)=='<?php') response = '<pre>'+response+'</pre>';
	if(element.tagName=='input') {
		element.value = response ;
	} else {
		element.innerHTML = response ;
	}
}
sgc_class_ajax.prototype.onFailure = function (errorCode) {
	var element = document.getElementById(this.target_id) ;
	element.innerHTML="Error code " + errorCode;
}
sgc_class_ajax.prototype.execute = function () {
	this.xhr.open(this.method,this.url,this.asyn); 
	if (this.method == "POST") {
		this.xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
	}
	if (this.asyn) {
		_tempAJAX_Reference_ = this; // Crée une copie de l'objet AJAX courant pour pouvoir le récupérer après // 
		this.xhr.onreadystatechange = function () { 
			if (_tempAJAX_Reference_.xhr.readyState == 4 && _tempAJAX_Reference_.xhr.status == 200) {
				if(_tempAJAX_Reference_.returnFormat=='xml') response = _tempAJAX_Reference_.xhr.responseXml ;
				else response = _tempAJAX_Reference_.xhr.responseText ;
				if (typeof _tempAJAX_Reference_.onComplete == "string") {
					eval (_tempAJAX_Reference_.onComplete); 
				} else {
					_tempAJAX_Reference_.onComplete(response); 
				}
			} else if (_tempAJAX_Reference_.xhr.readyState == 4) {
				errorCode = _tempAJAX_Reference_.xhr.status; 
				if (typeof _tempAJAX_Reference_.onFailure == "string") {
					eval(_tempAJAX_Reference_.onFailure); 
				} else {
					_tempAJAX_Reference_.onFailure(errorCode); 
				}
			}
		}
	} else {
		if (this.xhr.status == "200") {
			if(this.returnFormat=='xml') response = this.xhr.responseXml ;
			else response = this.xhr.responseText ;
			if (typeof this.onComplete == "string") {
				eval (this.onComplete); 
			} else {
				this.onComplete(response); 
			}
			
		} else {
			errorCode = this.xhr.status; 
			if (typeof this.onFailure == "string") {
				eval(this.onFailure); 
			} else {
				this.onFailure(errorCode); 
			}
			
		}
	}
	this.xhr.send(null);
}

/**************************************************************/
/**************************************************************/
/****************                              ****************/
/****************    common.sgc.javascript.js     ****************/
/****************    16/05/2011 à 01:30:29.    ****************/
/****************                              ****************/
/**************************************************************/
/**************************************************************/
// Pour lancer une fonction au chargement de la page
function onload_init(fonction) {
	if (window.addEventListener) window.addEventListener('load', fonction, false) ;
	else window.attachEvent('onload', fonction) ;
	// window.addEvent('domready', open_ext_link);
}

// Pour ouvrir tous les liens ayant class externe ou image dans une nouvelle fenetre
function open_ext_link() {
	var liens = document.getElementsByTagName('a');
	// On récupère tous les liens (<a>) du document dans une variable (un array), ici liens.
	// Une boucle qui parcourt le tableau (array) liens du début à la fin.
	for (var i = 0 ; i < liens.length ; ++i)  {
		// Si les liens ont un nom de class égal à externe ou image, alors on agit.
		if (sgc_check_css_class_for_className(liens[i].className,'externe') || sgc_check_css_class_for_className(liens[i].className,'image'))  {
			liens[i].title = liens[i].title+' (S\'ouvre dans une nouvelle fenêtre)';
			// Au clique de la souris.
			liens[i].onclick = function()  {
				window.open(this.href);
				return false; // On ouvre une nouvelle page ayant pour URL le href du lien cliqué et on inhibe le lien réel.
			};
		}
	}
}

	onload_init(open_ext_link) ;

function sgc_make_html_preview() {
	var liens = document.getElementsByTagName('a');
	for (var i = 0 ; i < liens.length ; ++i)  {
		if (sgc_check_css_class_for_className('html_preview',liens[i].className))  {
			var tmp = liens[i].innerHTML ;
			liens[i].innerHTML = tmp+'<div class="html_preview_box"><iframe src="'+liens[i].href+'"></iframe></div>' ;
			liens[i].title = liens[i].title+' (S\'ouvre dans une nouvelle fenêtre)';
			liens[i].onclick = function()  {
				window.open(this.href);
				return false; // On ouvre une nouvelle page ayant pour URL le href du lien cliqué et on inhibe le lien réel.
			};
		}
	}
}
onload_init(sgc_make_html_preview) ;

function sgc_make_ajax() {
	var liens = document.getElementsByTagName('a');
	// On récupère tous les liens (<a>) du document dans une variable (un array), ici liens.
	// Une boucle qui parcourt le tableau (array) liens du début à la fin.
	for (var i = 0 ; i < liens.length ; ++i)  {
		// Si les liens ont un nom de class égal à externe ou image, alors on agit.
		if (sgc_check_css_class_for_className('ajax',liens[i].className))  {
			liens[i].title = 'ajax' ;
			var url = liens[i].href ;
			//var id = liens[i].id ;
			//liens[i].href = 'javascript:sgc_ajax(\''+url+'\',\'+id+\');' ;
			//liens[i].href = 'javascript:void(0);' ;
			liens[i].onclick = function() {
				url = this.href ;
				alert(url);
				return false; 
			};
		}
	}
}
onload_init(sgc_make_ajax) ;

function sgc_ajax(ajax_url,target_id) {
	var ajax = new sgc_class_ajax() ;
	ajax.url = ajax_url ;
	ajax.target_id = target_id ;
	ajax.execute() ;
	return false;
}

function sgc_switch_css_class_for_id(old_class,new_class,element_id) {
	var original_className = document.getElementById(element_id).className ;
	var norm = new RegExp(' ');
	var switch_done = false ;
	if(norm.exec( original_className )!=null ){
		var classes = original_className.split(' ') ;
		for(var i=0;i<classes.length;i++) {
			if(classes[i] == old_class) {
				classes[i] = new_class ;
				switch_done = true ;
			}
		}
		var modified = classes.join(' ') ;
		document.getElementById(element_id).className = modified ;
	} else if(original_className == old_class) {
		document.getElementById(element_id).className = new_class ;
		switch_done = true ;
	}
	return switch_done ;
}

function sgc_check_css_class_for_className(target_class,original_className) {
	var norm = new RegExp(' ');
	if(norm.exec( original_className )!=null ){
		var classes = original_className.split(' ') ;
		for(var i=0;i<classes.length;i++) {
			if(classes[i] == target_class) {
				return true ;
			}
		}
	} else if(original_className == target_class) {
		return true ;
	}
	return false ;
}

function sgc_debug_box_switch(id_box) {
	var action = "" ;
	var original = document.getElementById(id_box).className ;
	var norm = new RegExp(' ');
	if(norm.exec( original )!=null ){
		var classes = original.split(' ') ;
		for(i=0;i<classes.length;i++) {
			if(classes[i] == "hidden") {
				classes[i] = "show" ;
				action = "show" ;
			} else if(classes[i] == "show") {
				classes[i] = "hidden" ;
				action = "hide" ;
			}
		}
		var modified = classes.join(' ') ;
		document.getElementById(id_box).className = modified ;
	} else {
		if(original=="hidden") {
			document.getElementById(id_box).className = "show" ;
			action = "show" ;
		} else if(original=="show") {
			document.getElementById(id_box).className = "hidden" ;
			action = "hide" ;
		}
	}
	if(action == "show") {
		document.getElementById(id_box).innerHTML += '<a href="javascript:void(0);" title="Fermer cette fenêtre" onclick="sgc_debug_box_switch(\''+id_box+'\');" class="sgc_debug_close_link">X</a>' ;
	} else if(action == "hide") {
		document.getElementById(id_box).innerHTML += '' ;
	}
	
}

function sgc_hidden_switch(id_select,dir_url) {
	var action = "" ;
	var liste = document.getElementById(id_select) ;
	var valeur = liste.options[liste.selectedIndex].value ;
	var box = document.getElementById(id_select+'_box') ;
	box.innerHTML = '<img src="'+dir_url+valeur+'" title="'+valeur+'" style="width:200px;height:150px;" />' ;
	var original = box.className ;
	var norm = new RegExp(' ');
	if(norm.exec( original )!=null ){
		var classes = original.split(' ') ;
		for(i=0;i<classes.length;i++) {
			if(classes[i] == "hidden") {
				classes[i] = "show" ;
				action = "show" ;
			} else if(classes[i] == "show") {
				classes[i] = "hidden" ;
				action = "hide" ;
			}
		}
		var modified = classes.join(' ') ;
		box.className = modified ;
	} else {
		if(original=="hidden") {
			box.className = "show" ;
			action = "show" ;
		} else if(original=="show") {
			box.className = "hidden" ;
			action = "hide" ;
		}
	}
}



function switch_tab(eltId,tab_id,nb_tab) {
	var arrLinkId = new Array() ;
	var arrBoxId = new Array() ;
	for(i=0;i<nb_tab;i++) {
		arrLinkId[i] = tab_id+"_menu_"+i ;
		arrBoxId[i] = tab_id+"_content_"+i ;
	}
	intNbLinkElt = new Number(arrLinkId.length);
	arrClassLink = new Array("current","ghost");
	for (i=0; i<intNbLinkElt; i++) {
		if ( arrLinkId[i] == eltId ) {
			if(document.getElementById(arrLinkId[i])) document.getElementById(arrLinkId[i]).className = arrClassLink[0];
			if(document.getElementById(arrBoxId[i])) document.getElementById(arrBoxId[i]).className = "on content";
		} else {
			if(document.getElementById(arrLinkId[i])) document.getElementById(arrLinkId[i]).className = arrClassLink[1];
			if(document.getElementById(arrBoxId[i])) document.getElementById(arrBoxId[i]).className = "off content";
		}
	}
	return false ;
}

function sgc_update_value(id_input,value) {
	document.getElementById(id_input).value = value ;
}
function sgc_update_content(id_box,html) {
	id_box = id_box+'' ;
	var box = document.getElementById(id_box) ;
	box.innerHTML = html ;
}


