function utf8_encode(a) {
	a = a.replace(/\r\n/g, "\n");
	var b = "";
	var d, end;
	d = end = 0;
	for (var n = 0; n < a.length; n++) {
		var c = a.charCodeAt(n);
		var e = null;
		if (c < 128) {
			end++
		} else if ((c > 127) && (c < 2048)) {
			e = String.fromCharCode((c >> 6) | 192) + String.fromCharCode((c & 63) | 128)
		} else {
			e = String.fromCharCode((c >> 12) | 224) + String.fromCharCode(((c >> 6) & 63) | 128) + String.fromCharCode((c & 63) | 128)
		}
		if (e != null) {
			if (end > d) {
				b += a.substring(d, end)
			}
			b += e;
			d = end = n + 1
		}
	}
	if (end > d) {
		b += a.substring(d, a.length)
	}
	return b
}
function md5(j) {
	var l = function (a, b) {
		return (a << b) | (a >>> (32 - b))
	};
	var m = function (a, b) {
		var c, lY4, lX8, lY8, lResult;
		lX8 = (a & 0x80000000);
		lY8 = (b & 0x80000000);
		c = (a & 0x40000000);
		lY4 = (b & 0x40000000);
		lResult = (a & 0x3FFFFFFF) + (b & 0x3FFFFFFF);
		if (c & lY4) {
			return (lResult ^ 0x80000000 ^ lX8 ^ lY8)
		}
		if (c | lY4) {
			if (lResult & 0x40000000) {
				return (lResult ^ 0xC0000000 ^ lX8 ^ lY8)
			} else {
				return (lResult ^ 0x40000000 ^ lX8 ^ lY8)
			}
		} else {
			return (lResult ^ lX8 ^ lY8)
		}
	};
	var F = function (x, y, z) {
		return (x & y) | ((~x) & z)
	};
	var G = function (x, y, z) {
		return (x & z) | (y & (~z))
	};
	var H = function (x, y, z) {
		return (x ^ y ^ z)
	};
	var I = function (x, y, z) {
		return (y ^ (x | (~z)))
	};
	var n = function (a, b, c, d, x, s, e) {
		a = m(a, m(m(F(b, c, d), x), e));
		return m(l(a, s), b)
	};
	var o = function (a, b, c, d, x, s, e) {
		a = m(a, m(m(G(b, c, d), x), e));
		return m(l(a, s), b)
	};
	var p = function (a, b, c, d, x, s, e) {
		a = m(a, m(m(H(b, c, d), x), e));
		return m(l(a, s), b)
	};
	var q = function (a, b, c, d, x, s, e) {
		a = m(a, m(m(I(b, c, d), x), e));
		return m(l(a, s), b)
	};
	var r = function (a) {
		var b;
		var c = a.length;
		var d = c + 8;
		var e = (d - (d % 64)) / 64;
		var f = (e + 1) * 16;
		var g = Array(f - 1);
		var h = 0;
		var i = 0;
		while (i < c) {
			b = (i - (i % 4)) / 4;
			h = (i % 4) * 8;
			g[b] = (g[b] | (a.charCodeAt(i) << h));
			i++
		}
		b = (i - (i % 4)) / 4;
		h = (i % 4) * 8;
		g[b] = g[b] | (0x80 << h);
		g[f - 2] = c << 3;
		g[f - 1] = c >>> 29;
		return g
	};
	var t = function (a) {
		var b = "",
		WordToHexValue_temp = "",
		lByte, lCount;
		for (lCount = 0; lCount <= 3; lCount++) {
			lByte = (a >>> (lCount * 8)) & 255;
			WordToHexValue_temp = "0" + lByte.toString(16);
			b = b + WordToHexValue_temp.substr(WordToHexValue_temp.length - 2, 2)
		}
		return b
	};
	var x = Array();
	var k, AA, BB, CC, DD, a, b, c, d;
	var u = 7,
	S12 = 12,
	S13 = 17,
	S14 = 22;
	var v = 5,
	S22 = 9,
	S23 = 14,
	S24 = 20;
	var w = 4,
	S32 = 11,
	S33 = 16,
	S34 = 23;
	var A = 6,
	S42 = 10,
	S43 = 15,
	S44 = 21;
	j = utf8_encode(j);
	x = r(j);
	a = 0x67452301;
	b = 0xEFCDAB89;
	c = 0x98BADCFE;
	d = 0x10325476;
	for (k = 0; k < x.length; k += 16) {
		AA = a;
		BB = b;
		CC = c;
		DD = d;
		a = n(a, b, c, d, x[k + 0], u, 0xD76AA478);
		d = n(d, a, b, c, x[k + 1], S12, 0xE8C7B756);
		c = n(c, d, a, b, x[k + 2], S13, 0x242070DB);
		b = n(b, c, d, a, x[k + 3], S14, 0xC1BDCEEE);
		a = n(a, b, c, d, x[k + 4], u, 0xF57C0FAF);
		d = n(d, a, b, c, x[k + 5], S12, 0x4787C62A);
		c = n(c, d, a, b, x[k + 6], S13, 0xA8304613);
		b = n(b, c, d, a, x[k + 7], S14, 0xFD469501);
		a = n(a, b, c, d, x[k + 8], u, 0x698098D8);
		d = n(d, a, b, c, x[k + 9], S12, 0x8B44F7AF);
		c = n(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1);
		b = n(b, c, d, a, x[k + 11], S14, 0x895CD7BE);
		a = n(a, b, c, d, x[k + 12], u, 0x6B901122);
		d = n(d, a, b, c, x[k + 13], S12, 0xFD987193);
		c = n(c, d, a, b, x[k + 14], S13, 0xA679438E);
		b = n(b, c, d, a, x[k + 15], S14, 0x49B40821);
		a = o(a, b, c, d, x[k + 1], v, 0xF61E2562);
		d = o(d, a, b, c, x[k + 6], S22, 0xC040B340);
		c = o(c, d, a, b, x[k + 11], S23, 0x265E5A51);
		b = o(b, c, d, a, x[k + 0], S24, 0xE9B6C7AA);
		a = o(a, b, c, d, x[k + 5], v, 0xD62F105D);
		d = o(d, a, b, c, x[k + 10], S22, 0x2441453);
		c = o(c, d, a, b, x[k + 15], S23, 0xD8A1E681);
		b = o(b, c, d, a, x[k + 4], S24, 0xE7D3FBC8);
		a = o(a, b, c, d, x[k + 9], v, 0x21E1CDE6);
		d = o(d, a, b, c, x[k + 14], S22, 0xC33707D6);
		c = o(c, d, a, b, x[k + 3], S23, 0xF4D50D87);
		b = o(b, c, d, a, x[k + 8], S24, 0x455A14ED);
		a = o(a, b, c, d, x[k + 13], v, 0xA9E3E905);
		d = o(d, a, b, c, x[k + 2], S22, 0xFCEFA3F8);
		c = o(c, d, a, b, x[k + 7], S23, 0x676F02D9);
		b = o(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A);
		a = p(a, b, c, d, x[k + 5], w, 0xFFFA3942);
		d = p(d, a, b, c, x[k + 8], S32, 0x8771F681);
		c = p(c, d, a, b, x[k + 11], S33, 0x6D9D6122);
		b = p(b, c, d, a, x[k + 14], S34, 0xFDE5380C);
		a = p(a, b, c, d, x[k + 1], w, 0xA4BEEA44);
		d = p(d, a, b, c, x[k + 4], S32, 0x4BDECFA9);
		c = p(c, d, a, b, x[k + 7], S33, 0xF6BB4B60);
		b = p(b, c, d, a, x[k + 10], S34, 0xBEBFBC70);
		a = p(a, b, c, d, x[k + 13], w, 0x289B7EC6);
		d = p(d, a, b, c, x[k + 0], S32, 0xEAA127FA);
		c = p(c, d, a, b, x[k + 3], S33, 0xD4EF3085);
		b = p(b, c, d, a, x[k + 6], S34, 0x4881D05);
		a = p(a, b, c, d, x[k + 9], w, 0xD9D4D039);
		d = p(d, a, b, c, x[k + 12], S32, 0xE6DB99E5);
		c = p(c, d, a, b, x[k + 15], S33, 0x1FA27CF8);
		b = p(b, c, d, a, x[k + 2], S34, 0xC4AC5665);
		a = q(a, b, c, d, x[k + 0], A, 0xF4292244);
		d = q(d, a, b, c, x[k + 7], S42, 0x432AFF97);
		c = q(c, d, a, b, x[k + 14], S43, 0xAB9423A7);
		b = q(b, c, d, a, x[k + 5], S44, 0xFC93A039);
		a = q(a, b, c, d, x[k + 12], A, 0x655B59C3);
		d = q(d, a, b, c, x[k + 3], S42, 0x8F0CCC92);
		c = q(c, d, a, b, x[k + 10], S43, 0xFFEFF47D);
		b = q(b, c, d, a, x[k + 1], S44, 0x85845DD1);
		a = q(a, b, c, d, x[k + 8], A, 0x6FA87E4F);
		d = q(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0);
		c = q(c, d, a, b, x[k + 6], S43, 0xA3014314);
		b = q(b, c, d, a, x[k + 13], S44, 0x4E0811A1);
		a = q(a, b, c, d, x[k + 4], A, 0xF7537E82);
		d = q(d, a, b, c, x[k + 11], S42, 0xBD3AF235);
		c = q(c, d, a, b, x[k + 2], S43, 0x2AD7D2BB);
		b = q(b, c, d, a, x[k + 9], S44, 0xEB86D391);
		a = m(a, AA);
		b = m(b, BB);
		c = m(c, CC);
		d = m(d, DD)
	}
	var B = t(a) + t(b) + t(c) + t(d);
	return B.toLowerCase()
}
/*

*/
var sp_no_corr = '<em>Pas de corrections</em>';
var sp_no_sugg = '<em>Pas de suggestions</em>';


function spParse1(param) {
	var json, text, elem, html, offset, length, corr_len, sugg_len, i, j;
	json = eval('('+param+')');
	html = '<div id="spell_sugg">';
	
	elem = document.getElementById('markItUp');
	text = elem.value;
	
	if (json.error == 0) {
		corr_len = json.spell.length;
		
		if (corr_len > 0) {
			for (i = 0; i < corr_len; i++) {
				offset = json.spell[i].offset;
				length = json.spell[i].length;
				sugg_len = json.spell[i].sugg.length;
				
				html += '<span id="spell_correct_'+i+'">'+"\n"+'<strong>'+atob(json.spell[i].word)+'</strong> : ';
				
				
				if (sugg_len > 0 && json.spell[i].sugg[0].length > 0) {
					for (j = 0; j < sugg_len; j++) {
						html += '<a onclick="javascript:spReplace(\'markItUp\', \''+atob(json.spell[i].word)+'\',\''+atob(json.spell[i].sugg[j])+'\', '+i+');">'+atob(json.spell[i].sugg[j]).replace(/\\'/g,"'")+'</a>';
						if (j+1 < sugg_len)
							html += ', ';
					}
				}
				else
					html += sp_no_sugg;
				
				html += '<span class="spell_ignore"> <a onclick="spHideSugg('+i+')">Ignorer</a>&emsp;&emsp;&emsp;</span>';
				html += '<br /></span>';
			}
		}
		else
			html = sp_no_corr;
	}
	else
		html = 'Erreur reçue';
	
	document.getElementById('form_preview').innerHTML = html+'</div>';
}

function spReplace(id, from_word, to_word, i) {
	var elem, text, text_length, text_before, text_after, word_length, word_start;
	
	elem = document.getElementById(id);
	text = elem.value;
	length = text.length;
	word_length = from_word.length;
	word_start = text.search(from_word);
	
	text_before = text.substring(0, word_start);
	text_after = text.substring((word_start + word_length), length);
	
	elem.value = text_before + to_word + text_after;
	
	spHideSugg(i);
}

function spHideSugg(i) {
	if (document.getElementById('spell_correct_'+i))
		document.getElementById('spell_sugg').removeChild(document.getElementById('spell_correct_'+i));
	if (document.getElementById('spell_sugg')) {
		var elem = document.getElementById('spell_sugg');
		if (elem.innerHTML.length == 0)
			elem.innerHTML = sp_no_corr;
	}
}


function spCheck(text) {
	var xhr = null; 
	 
	if(window.XMLHttpRequest) // Firefox et autres
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
	   try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
	}
	else { // XMLHttpRequest non supporté par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	   xhr = false; 
	}
	
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			alert(xhr.responseText);
		}
	}
	xhr.open("POST", 'spellify.php', true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send('data=maisson');
	
}

function spCheck2(text) {
	var xhr = null;

	if (window.XMLHttpRequest || window.ActiveXObject) {
		if (window.ActiveXObject) {
			try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
		} else {
			xhr = new XMLHttpRequest(); 
		}
	} else {
	return null;
	}
	
	
	xhr.open("POST", "/templates/js/ajax/spellify.php", true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-1");
	xhr.send('data='+text);
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			spParse1(xhr.responseText);
		}
	};

}

function hexa_decode($str) {
	str.replace(/&x([0-9a-f]{1,4});/gi, function (a, b) {
		return String.fromCharCode("0"+b)
	});
	return $str;
}


function utf8_decode ( str_data ) {
	var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0;
	str_data += '';
	while ( i < str_data.length ) {
		c1 = str_data.charCodeAt(i);
		if (c1 < 128) {
			tmp_arr[ac++] = String.fromCharCode(c1);
			i++;
		} else if ((c1 > 191) && (c1 < 224)) {
			c2 = str_data.charCodeAt(i+1);
			tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
			i += 2;
		} else {
			c2 = str_data.charCodeAt(i+1);
			c3 = str_data.charCodeAt(i+2);
			tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}
	}
	return tmp_arr.join('');
}


if (typeof atob == 'undefined') {
	function atob(str) {
		var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
		var invalid = {
			strlen: (str.length % 4 != 0),
			chars:  new RegExp('[^' + chars + ']').test(str),
			equals: (/=/.test(str) && (/=[^=]/.test(str) || /={3}/.test(str)))
		};
		if (invalid.strlen || invalid.chars || invalid.equals)
			throw new Error('Invalid base64 data');
		var decoded = [];
		var c = 0;
		while (c < str.length) {
			var i0 = chars.indexOf(str.charAt(c++));
			var i1 = chars.indexOf(str.charAt(c++));
			var i2 = chars.indexOf(str.charAt(c++));
			var i3 = chars.indexOf(str.charAt(c++));
			var buf = (i0 << 18) + (i1 << 12) + ((i2 & 63) << 6) + (i3 & 63);
			var b0 = (buf & (255 << 16)) >> 16;
			var b1 = (i2 == 64) ? -1 : (buf & (255 << 8)) >> 8;
			var b2 = (i3 == 64) ? -1 : (buf & 255);
			decoded[decoded.length] = String.fromCharCode(b0);
			if (b1 >= 0) decoded[decoded.length] = String.fromCharCode(b1);
			if (b2 >= 0) decoded[decoded.length] = String.fromCharCode(b2);
		}
		return decoded.join('');
	}
}



/* http://www.stringify.com/2007/jun/18/base64/ */

$(document).ready(function()	{
	if ($('#markItUp').length > 0) {
		$('#markItUp').markItUp(mySettings);
		
		$('#preview').click(function () {
			var texte = $('#markItUp').val();
			$.ajax({ 
				type: "POST", 
				data: 'texte='+encodeURIComponent(texte),
				url: '/templates/js/ajax/rcode_apercu_xhr.php?apercu=1', 
				success: function(retour){ 
					$("#form_preview").html(retour); 
				}
			}); 
		});
		$('#ortho').click(function () {
			spCheck2($('#markItUp').val());
		});
	}
});
