var img_to_preload = new Array();
var FuncOL = new Array();
var curs = {'x': 0, 'y': 0};
var frm_proc = false;

String.prototype.stripHTML = function(){
	var reg = /<(?:.|\s)*?>/g;
	return this.replace(reg, '');
}

String.prototype.trim = function(){
	var reg = /(?:^\s+|\s+$)/g;
	return this.replace(reg, '');
}

function $(e) { return get_element(e); }

function my_round(rnum, rlength){
	return Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
}

function format_price(n, d){
	var price = my_round(n, d).toString();

	var a = '';
	var b = '';

	var e = price.split(/\./);
	if(e.length==2){
		a = e[0];
		b = e[1];
	}else{
		a = price;
	}

	if(d>0){
		a += '.';
		a += b;
		for(var i=b.length; i<d; i++){
			a += '0';
		}
	}

	return a;
}

function is_array(input){
	return typeof(input)=='object'&&(input instanceof Array);
}

function array_filter(array, fn, that){
	var result = [];

	if(!Array.prototype.filter){
		for(var i = 0, length = array.length; i < length; i++){
			if(i in array){
				var value = array[i];
				if(fn.call(that, value, i, array)) result.push(value);
			}
		}
	}else{
		result = array.filter(fn);
	}

	return result;
}

function set_input_type(obj, stype){
	var oldObject = get_element(obj);
	if(!oldObject || oldObject.type==stype) return false;

	var newObject = document.createElement('input');

	newObject.type = stype;

	if(oldObject.size) newObject.size = oldObject.size;
	if(oldObject.value) newObject.value = oldObject.value;
	if(oldObject.name) newObject.name = oldObject.name;
	if(oldObject.id) newObject.id = oldObject.id;
	if(oldObject.onlick) newObject.onlick = oldObject.onlick;
	if(oldObject.onfocus) newObject.onfocus = oldObject.onfocus;
	if(oldObject.onblur) newObject.onblur = oldObject.onblur;
	if(oldObject.onkeypress) newObject.onkeypress = oldObject.onkeypress;
	if(oldObject.className) newObject.className = oldObject.className;
	if(oldObject.readonly) newObject.readonly = oldObject.readonly;

	oldObject.parentNode.replaceChild(newObject,oldObject);
}

function sleep(delay){
	var start = new Date().getTime();
	while(new Date().getTime() < start + delay);
}

function pause(iMilliseconds){
	var sDialogScript = 'window.setTimeout( function () { window.close(); }, ' + iMilliseconds + ');';
	window.showModalDialog('javascript:document.writeln("<script>' + sDialogScript + '<' + '/script>");');
}

function preload_imgs(){
	for(var i=0;i<arguments.length;i++){
		img_to_preload[img_to_preload.length] = arguments[i];
	}
}

function exec_preload_imgs(){
	if(!document.images) return false;

	var tmp = new Array();
	for(var i=0;i<img_to_preload.length;i++){
		tmp[i] = new Image();
		tmp[i].src = img_to_preload[i];
	}
}

function need_quit_confirm(){
	window.onbeforeunload = function(e){ if(quit_confirm_msg) return quit_confirm_msg; }
}

function remove_quit_confirm(){
	window.onbeforeunload = function(e){  }
}

function check_datas(array1, array2, unlock){
	if(!unlock && frm_proc) return false;

	var incomplete = new Array();

	if(!array1 || array1.length<1) return false;
	if(!array2 || array2.length<1) array2 = array1;
	if(array1.length!=array2.length) return false;

	for(var k in array1){
		var tmp1 = array1[k];
		var tmp2 = array2[k];

		if(is_element(tmp1)){
			if(get_element(tmp1).value==''){
				incomplete[incomplete.length] = tmp2;
			}
		}
	}

	if(incomplete.length>0){
		var text = alert_champs_inc+'\n\n';
		for(var k in incomplete){
			if(get_element(incomplete[k]).title) text += ' - '+get_element(incomplete[k]).title+'\n';
			else text += ' - '+incomplete[k]+'\n';
		}
		text += '\n'+alert_make_modif;

		alert(text);

		return false;
	}

	if(!unlock) frm_proc = true;
	return true;
}

function addText(txt, id){
	var obj = get_element(id), sel;
	obj.focus();

	if(document.selection && document.selection.createRange){
		sel = document.selection.createRange();
		if(sel.parentElement()==obj) sel.text = sel.text+txt;
	}else if(String(typeof obj.selectionStart)!='undefined'){
		sel = obj.selectionStart;
		obj.value = (obj.value).substring(0,sel) + txt + (obj.value).substring(sel,obj.value.length);
	}else obj.value+=txt;

	obj.focus();
}

function getText(id){
	var obj = get_element(id), sel, selection = '';
	obj.focus();

	if(document.selection && document.selection.createRange){
		sel = document.selection.createRange();
		if(sel.parentElement()==obj) selection = sel.text;
	}else if(String(typeof obj.selectionStart)!='undefined'){
		var longueur= parseInt(obj.textLength);
		var selStart = obj.selectionStart;
		var selEnd = obj.selectionEnd;
		//if(selEnd == 2 || selEnd == 1) selEnd = longueur;

		selection = (obj.value).substring(selStart,selEnd);
	}

	return selection;
}

function init(where){
	where = where || 'site';

	StkFunc(exec_preload_imgs);

	if(where=='site'){
		StkFunc(is_mobile_client);
		StkFunc(start_afficher);
	}else if(where=='ec'){
		StkFunc(is_mobile_client);
		StkFunc(update_credit);
	}

	//if(window.addEvent) window.addEvent('load', ExcFunc());
	//else window.document.onload = ExcFunc();

	addEvent(window, 'load', ExcFunc);
}

function StkFunc(Obj){
	FuncOL[FuncOL.length] = Obj;
}

function ExcFunc(){
	for(i=0;i<FuncOL.length;i++){
		if(window.FuncOL[i] && typeof(window.FuncOL[i])=='function') FuncOL[i]();
		else eval(FuncOL[i]);
	}
}

function addEvent(obj, type, fn){
	if(obj.addEventListener){
		obj.addEventListener(type, function(event){ return fn.call(obj, event); }, false);
	}else if(obj.attachEvent){
		obj.attachEvent("on"+type, function(e){ if(!e) var e = window.event; return fn.call(obj, e); });
	}
}

function is_mobile_client(){
	var larg = 0;
	var viewport = self.innerWidth;

	if(document.body && document.body.clientWidth) larg = document.body.clientWidth;
	else if(window.innerWidth) larg = window.innerWidth;

	if(larg<=480 || navigator.userAgent.match(/iP(hone|od)/i)){
		show_element('mobile-version', 'block');
	}
}

function setOpacity(obj, opacite){
	if(!obj) return false;

	if(obj.filters && obj.filters[0]){
		obj.filters[0].opacity = opacite*10;
	}else if(obj.style){
		if(obj.style.filter != undefined){
			obj.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+(opacite*10)+');';
			obj.style.filter = 'alpha(opacity='+(opacite*10)+');';
		}else if(obj.style.MozOpacity != undefined){
			obj.style.MozOpacity = opacite/10;
		}else if(obj.style.KHTMLOpacity != undefined){
			obj.style.KHTMLOpacity = opacite/10;
		}else if(obj.style.opacity != undefined){
			obj.style.opacity = opacite/10;
		}
	}

	if(opacite==10 && obj.style.removeAttribute) obj.style.removeAttribute('filter');
}

function checkCode(obj){
	if(obj.value.length!=8){
		alert(alert_code_allopass);
		return false;
	}
	return true;
}

function is_ip_v4(ip){
	if(ip.match(/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i)) return true;

	return false;
}

function set_radio_value(i, v, f){
	var f = f || 0;

	var radio = document.forms[f].elements[i];

	for(var i=0;i<radio.length;i++){
		if(radio[i].value==v) radio[i].checked = true;
	}
}

function get_radio_value(radioObj){
	if(!radioObj) return '';

	var radioLength = radioObj.length;

	if(radioLength == undefined)
		if(radioObj.checked) return radioObj.value;
		else return '';
	for(var i = 0; i < radioLength; i++){
		if(radioObj[i].checked){
			return radioObj[i].value;
		}
	}

	return '';
}

function show_form_part(obj, val, parts, part){
	var part = part || 'form_part';

	if(!parts || parts.length<1){
		var parts = new Array();
		parts[1] = new Array();
		parts[2] = new Array('1','2');
		parts[3] = new Array('1');
	}

	for(i=1;i<=10;i++) hide_element(part+i);

	if(!parts[val]) return false;

	for(i=0;i<parts[val].length;i++) show_element(part+parts[val][i]);

	if(get_element(part+'_value')) get_element(part+'_value').innerHTML = obj.options[obj.selectedIndex].text;

	return true;
}

function popup(url, w, h, scroll){
	w = w || 400;
	h = h || 400;

	if(scroll!='no') scroll = 1;
	else scroll = 0;

	var n = window.open(url, 'popup_'+w+'x'+h, 'width='+w+', height='+h+', top=100, left=100, toolbar=0, location=0, directories=0, status=0, scrollbars='+scroll+', resizable=0, copyhistory=0, menuBar=0');

	if(n){
		n.focus();
		return false;
	}

	return true;
}

function popup_doc(url){
	return popup(url);
}

function new_window(url){
	var n = window.open(url, '_blank');

	if(n){
		n.focus();
		return false;
	}

	return true;
}

function in_array(needle, haystack) {
    var found = false, key;

    for (key in haystack) {
        if (haystack[key] == needle) {
            found = true;
            break;
        }
    }

    return found;
}

function unset(array, value){
	var output = new Array();
	var i = 0;

	for(var k in array){
		if(array[k]!=value){
			output[i] = array[k];
			i++;
		}
	}

	return output;
}

function htmlentities(text){
	if(typeof(text)!='string') text=text.toString();

	text=text.replace(/&/g, '&amp;');
	text=text.replace(/"/g, '&quot;');
	text=text.replace(/</g, '&lt;');
	text=text.replace(/>/g, '&gt;');
	text=text.replace(/'/g, '&#146;');
	text=text.replace(/\n/g, '<br />');

	return text;
}

function ajax_req(methode, uri, query, oid, fid, load, callback, sto){
	var req;
	if(!charset) var charset = 'iso-8859-15';
	var timeout = 5000;
	var dont_set_timeout = sto || false;

	if(methode!='POST' && methode!='GET') return false;

	if(window.XMLHttpRequest){ // Mozilla, Safari,...
		var req = new XMLHttpRequest();
		if(req.overrideMimeType) req.overrideMimeType('text/html');
	}else if(window.ActiveXObject){ // IE
		try{
			var req = new ActiveXObject('Msxml2.XMLHTTP');
		}catch (e){
			try{
				var req = new ActiveXObject('Microsoft.XMLHTTP');
			}catch (e){
				alert(alert_ajax_error+"\n\n"+e.description);
				return false;
			}
		}
	}

	if(!req){
		alert(alert_ajax_error);
		return false;
	}

	if(methode=='GET'){
		req.open(methode, uri+'?'+query+'&rnd='+Math.random(), true);
		req.setRequestHeader('Content-Type', 'text/html; charset='+charset);
		req.send('');
	}else{
		req.open(methode, uri, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset='+charset);
		req.send(query);
	}

	if(!dont_set_timeout) window.setTimeout(function(){ req.abort(); }, timeout);

	if(load && oid && oid!='') loading(oid);

	req.onreadystatechange = function(){
		if(req.readyState==4){
			if(req.status==200){
				if(oid && oid!='') ajax_resp(req, oid);
				else if(fid && fid!='' && window[fid]) window[fid](req);

				if(callback && callback!='') eval(callback);
			}else{
				// error http code
			}
		}
	}

	return req;
}

function ajax_resp(obj, oid){
	if(!is_element(oid)) return false;

	var objet  = get_element(oid);
	var retour = obj.responseText;

	objet.innerHTML = retour;

	if(retour!='') show_element(oid);
	else hide_element(oid);
}

function loading(id, style){
	if(!is_element(id)) return false;

	style = style || '';

	get_element(id).innerHTML = '<div id="loading" style="'+style+'"><img src="/images/icons/ajax-loader.gif" alt="" class="icon"/></div>';
	show_element(id);
}

function go_to_page_top(){
	scrollTo(0,0);
}

function go_to_url(url){
	if(!url || url=='') return false;

	remove_quit_confirm();
	window.location = url;
}

function $(e) { return get_element(e); }

function is_element(e){
	if(document.getElementById(e)) return true;

	return false;
}

function get_element(e){
	if(is_element(e)) return document.getElementById(e);

	return false;
}

function add_element_class(e, c){
	if(is_element(e)){
		var cur = get_element_class(e, c).split(' ');
		if(!in_array(c, cur)){
			cur.push(c);
			set_element_class(e, cur.join(' '));
		}
	}
}

function remove_element_class(e, c){
	if(is_element(e)){
		a = get_element_class(e, c);
		var cur = get_element_class(e, c).split(' ');
		if(in_array(c, cur)){
			cur = unset(cur, c);
			set_element_class(e, cur.join(' '));
		}
		b = cur.join(' ');
	}
}

function set_element_class(e, c){
	if(is_element(e)) document.getElementById(e).className = c;
}

function get_element_class(e, c){
	if(is_element(e)) return document.getElementById(e).className;

	return false;
}

function set_element_style(e, s, v){
	if(is_element(e)) document.getElementById(e).style[s] = v;
}

function get_element_style(e, s){
	if(is_element(e)) return document.getElementById(e).style[s];

	return false;
}

function show_element(e, v){
	var v = v || '';
	set_element_style(e, 'display', v);
}

function hide_element(e){
	set_element_style(e, 'display', 'none');
}

function set_display(a, b, p){
	if(!a) return false;
	for(i=0;i<a.length;i++) hide_element(p+a[i]);

	if(!b) return false;
	for(i=0;i<b.length;i++) show_element(p+b[i]);

	return true;
}

function set_class(a, c, p){
	if(!a) return false;
	for(i=0;i<a.length;i++) set_element_class(p+a[i], c);

	return true;
}

function set_style(a, s, v, p){
	if(!a) return false;
	for(i=0;i<a.length;i++) set_element_style(p+a[i], s, v);

	return true;
}

function reload_parent_window(){
	if(window.opener){
		if(window.opener.remove_quit_confirm){
			window.opener.remove_quit_confirm();
		}

		var loc = window.opener.document.location.href;
		window.opener.document.location.href = strip_anchor(loc);
	}
}

function reload_window(){
	remove_quit_confirm();

	var loc = window.document.location.href;
	window.document.location.href = strip_anchor(loc);
}

function strip_domain(url){
	var reg = new RegExp('http\:\/\/([^/]+)\/', 'i');
	return url.replace(reg, '/');
}

function strip_anchor(url){
	if(url.lastIndexOf('#')>-1) url = url.substring(0, url.lastIndexOf('#'));

	return url;
}

function calc_message_length(obj, can_udh, id){
	var L = obj.value.length;
	var f = obj.form.name;

	var id = id || '';
	var can_udh = can_udh || false;
	var affCar = 'nbCar';
	var affSMS = 'nbSMS';

	if(id){
		affCar = affCar+id;
		affSMS = affSMS+id;
	}

	if(bUDH && is_element('concat')){
		if(L>m_norm && can_udh){
			if(!bdisplayed){
				m = m_udh;

				document.forms[f].elements['udh'].checked = true;

				bdisplayed = true;
			}
			show_element('concat');

			if(is_element('noconcat')){
				hide_element('noconcat');
			}
		}else{
			m = m_norm;

			document.forms[f].elements['udh'].checked = false;
			hide_element('concat');

			if(is_element('noconcat')){
				if(L>m_norm){
					show_element('noconcat');
				}else{
					hide_element('noconcat');
				}
			}

			bdisplayed = false;
		}
	}

	var n = Math.floor(L/m);
	get_element(affCar).innerHTML = m-(L-(n*m));

	n += 1;
	current_nb_msg = n;
	var s = null;

	switch(n){
		case 1:
			s = html_nbsms_1;
			break;
		case 2:
			s = html_nbsms_2;
			break;
		case 3:
			s = html_nbsms_3;
			break;
		default:
			s = html_nbsms_4;
			break;
	}
	get_element(affSMS).innerHTML = n+'<sup>'+s+'</sup>';

	return true;
}

function check_login_format(obj, warn){
	var reg = new RegExp('[^a-z0-9\-_\.]{1}','i');
	var old = obj.value;

	while(obj.value.match(reg)) obj.value = obj.value.replace(reg, '');

	if(obj.value!=old){
		show_element('erreur');
		get_element('erreur').innerHTML = html_login_format;
		window.setTimeout(function(){ hide_element('erreur'); }, 5000);

		if(warn){
			alert(alert_login_format);
			//obj.focus();
		}

		return false;
	}

	//hide_element('erreur');
	return true;
}

function bulle(e, w, a){
	if(a){
		if(a=='open') show_element(e);
		else if(a=='close') hide_element(e);
		else return false;
	}else{
		if(get_element_style(e, 'display')=='none') show_element(e);
		else hide_element(e);
	}

	if(!w || w==0) w = 220;
	set_element_style(e, 'width', w+'px');

	var fw = 872;
	if(window.document.documentElement) var dw = window.document.documentElement.clientWidth;
	else var dw = window.document.body.clientWidth;

	var ml = 0;
	if(dw>fw) ml = (dw-fw)/2;

	set_element_style(e, 'left', (curs['x']+3-ml)+'px');
	set_element_style(e, 'top', (curs['y']+3)+'px');
}

function set_bulle_display(a, b, p){
	if(!a) return false;
	for(i=0;i<a.length;i++) bulle(p+a[i]);

	if(!b) return false;
	for(i=0;i<b.length;i++) bulle(p+b[i], 0, 'close');

	return true;
}

function numeric_input(obj){
	var tmp = obj.value;

	var reg = new RegExp('[^0-9]{1}', 'g');
	tmp = tmp.replace(reg, '');

	if(tmp.length>1 && tmp.substring(0,1)=='0'){
		tmp = tmp.substring(1);
	}

	if(obj.value!=tmp) obj.value = tmp;
}

function alphanumeric_input(obj){
	var tmp = obj.value;

	var reg = new RegExp('[^a-z0-9]{1}', 'gi');
	tmp = tmp.replace(reg, '');

	if(obj.value!=tmp) obj.value = tmp;
}

function get_cursor_pos(e){
	e = e || window.event;
	var xcurs, ycurs;

	if(navigator.appName == 'Netscape'){
		xcurs = e.pageX;
		ycurs = e.pageY;
	}else{
		xcurs = event.clientX;
		ycurs = event.clientY;
		if(window.document.documentElement){
			xcurs += window.document.documentElement.scrollLeft;
			ycurs += window.document.documentElement.scrollTop;
		}else{
			xcurs += window.document.body.scrollLeft;
			ycurs += window.document.body.scrollTop;
		}
	}

	curs = {'x': xcurs, 'y': ycurs};
}

function get_cursor_pos_noscroll(e){
	e = e || window.event;
	var xcurs, ycurs;

	xcurs = e.clientX;
	ycurs = e.clientY;

	curs = {'x': xcurs, 'y': ycurs};
}

if(!document.all){
	window.captureEvents(Event.MOUSEMOVE);
	window.onmousemove = get_cursor_pos;
}else window.document.onmousemove = get_cursor_pos;