﻿var http = createRequestObject();
var lastUrl = '';
var current_url = '';
var field = '';
var interval = '';
var loading=0;

//BACK TREN IE
var br_s = navigator.userAgent.toLowerCase();
	if (br_s.indexOf('opera')!= -1)
		browser='op';
	else if (br_s.indexOf('netscape')!= -1)
		browser='ns';
	else if (br_s.indexOf('firefox')!= -1)
		browser='ff';
	else
		browser='ie';
//END	

function createRequestObject() {
	var xmlhttp;
	try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
	xmlhttp=new XMLHttpRequest();
  }
	return  xmlhttp;
}

function popup(url,wdname,width,height)
{
	if (width == null)  { width  = 200; }   // default width
	if (height == null) { height = 400; }   // default height
	newwin=window.open(url,wdname,'fullscreen=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+width+',height='+height);
	if (document.all)
	{
		newwin.moveTo(0,0);
		newwin.focus();
	}
}

function  Replaces(string) {
string=string.replace(/'/g,"\'");
return string
}

function Title(song,sing)
{
//song=str.replace(/'/i, "\'");
  document.title ='MaWaZiK.Com –––»  '+Replaces(song)+' - : '+Replaces(sing)+' –––';
}
function TitleSing(title)
{
  document.title ='MaWaZiK.Com –––»  '+Replaces(title)+' –––';
}
function TitleAlbum(title)
{
  document.title ='MaWaZiK.Com –––»  '+Replaces(title)+' –––';
}
function TitleSong(title)
{
  document.title ='MaWaZiK.Com –––»  '+Replaces(title)+' –––';
}

function get_comic(){
id = document.getElementById("comic").value;
if (id != '0')
window.location.href='#Comic/'+id+'/1';
}

function sendRequest(current_url,act) {
	try{
		if (act == 'Play' || act == 'Play_Album' || act == 'Play_Singer' || act == 'Play_Playlist' || act == 'Zplay' || act == 'TV' || act == 'Radio')
		field = document.getElementById("playing_field");
		else if (act == 'Download')
			field = document.getElementById("download_field");
		else field = document.getElementById("data_field");
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		current_url = encodeURIComponent(current_url);
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponse;
		http.send('url='+current_url);
	}
	catch(e){}
	finally{}
}

function handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			hide_Loading();
			loading=0;
			response = http.responseText;
			if (current_url.indexOf('Play') != -1) {
				field.style.marginBottom = "10";
			}
			field.innerHTML = response;
			field.style.display = "";
			field.scrollIntoView();
		}
  	}
	catch(e){}
	finally{}
}
// NDK Loading
var ie45,ns6,ns4,dom;
if (navigator.appName=="Microsoft Internet Explorer") ie45=parseInt(navigator.appVersion)>=4;
else if (navigator.appName=="Netscape"){  ns6=parseInt(navigator.appVersion)>=5;  ns4=parseInt(navigator.appVersion)<5;}
dom=ie45 || ns6;

var timershow=false;
var curx=-200;
var cury=200;
var win_w=window.innerWidth ? window.innerWidth : document.body.offsetWidth;
var mid_w=win_w/2;
var timershow1=window.setInterval("stayMiddle()",1);

function getobj(id) {
el = document.all ? document.all[id] :   dom ? document.getElementById(id) :   document.layers[id];
return el;
}

function show_Loading() {
	obj = getobj('LoadingDiv');
	if (timershow) window.clearTimeout(timershow);
	timershow=window.setInterval("nshow()",1);
}

function hide_Loading() {
	obj = getobj('LoadingDiv');
	if (timershow) window.clearTimeout(timershow);
	timershow=window.setInterval("nhide()",1);
}

function moveobj(obj,x,y) {
	obj.style.left=x + "px";
	obj.style.top=y+ "px";
	curx=x;
	cury=y;
}

function stayMiddle() {
	if (document.documentElement && document.documentElement.scrollTop)
		var pY =  document.documentElement.scrollTop;
	else if (document.body)
		var pY =  document.body.scrollTop;

	obj = getobj('LoadingDiv');
	newy = cury+((pY-cury)/16)+12;
	moveobj(obj,curx, newy);
}

function nshow() {
	obj = getobj('LoadingDiv');
	newx = curx+((mid_w-curx)/16)-7;
	moveobj(obj,newx, cury);
}
function nhide() {
	obj = getobj('LoadingDiv');
	newx = curx+((0-curx)/16)-15;
	moveobj(obj,newx, cury);
}
// End
function getVar(url,cnt)
{
	url=url+'#';
	url=url.split('#');
	if (!url[1]) window.location.href = '#Home';
	url=url[1];
	url=url+'/';
	url=url.split('/');
	if (url[0] == 'Logout')
		window.location.href = '?refresh=1';
	if (url[0] == 's')
		window.location.href = '#'+url[1];
	if (cnt != -1) {
		url=url[cnt];
		if (!url) return '';
	}
	return url;
}

function showComment(media_id) {
	try {
		document.getElementById("comment_field").innerHTML = loadingText;
		document.getElementById("comment_field").style.display = "block";
		http.open('POST',  'comment.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("comment_field").innerHTML = http.responseText;
			}
		}
		http.send('showcomment=1&media_id='+media_id);
	}
	catch(e){}
	finally{}
	return false;
}

function loadPage() {
	act = getVar(window.location.href,0);
	if (act) {
		current_url=window.location.href;
		current_url=current_url+'#';
		current_url=current_url.split('#');
		current_url=current_url[1];
		if (current_url) sendRequest(current_url,act);
	}
}

function urlCheck()
{
	url=window.location.href;
	if (url.indexOf('?') != -1 ) {
		url_temp = url.split('?');
		clearInterval(interval);
		window.location.href = url_temp[0];
		
	}
	if (url != '' && url!=lastUrl)
	{
		loadPage();
		url_hs = window.location.href;
		url_hs = url_hs.split('#');
		url_hs = url_hs[1];
		if (browser=='ie') document.getElementById("ieHistory").src='history.php?history='+url_hs;	
		lastUrl=url;
	}
	
}

function startLoad() {
	interval = setInterval('urlCheck()',100);
}

function copy(text2copy) {
  if (window.clipboardData) {
	window.clipboardData.setData("Text",text2copy);
  }
  alert("Presse-papiers pour copier et utiliser Ctrl + V pour coller");
}

function alertBrokenLink(id) {
	if (confirm("Etes-vous sure que c'est lient mort?")) {
		try{
			http.open('POST',  'index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = BrokenResponse;
			http.send('url=Broken/'+id);
		}
		catch(e){}
		finally{}
	}
}

function BrokenResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			response = http.responseText;
			if (response == 1) alert("Un rapport a été envoyé, merci .");
			else alert("Erreur. On Vous invite à essayer à nouveau.");
		}
  	}
	catch(e){
		alert("Erreur. On Vous invite à essayer à nouveau.");
	}
	finally{}
}

/*function do_search_keyword() {
	var frm = document.frm_search;
	kw = frm.keyword.value;
	if (kw == "")
		alert('Bạn chưa nhập từ khóa');
	else {
		kw = encodeURIComponent(kw);		
		for(var i = 0; i < frm.elements.length; i++) {
			if(frm.elements[i].type == 'radio' && frm.elements[i].checked == true) {
				type = frm.elements[i].value ;
				switch (type) {
					case 'song' : type = 1; break;
					case 'singer' : type = 2; break;
					case 'album' : type = 3; break;
					case 'audio' : type = 4; break;
				}
				last_url = '';
				if (type==4) 
				window.location.href = '#Zget/'+kw;
				else
				window.location.href = '#Search/'+type+'/'+kw+'/1';
						
			}		
		}
	}
	return false;
}
*/
function do_search() {
	var frm = document.frm_search;
	kw = frm.keyword.value;
	if (!kw) alert("Vous n'avez pas entré le mot-clé");
	else {
		kw = encodeURIComponent(kw);
		for(var i = 0; i < frm.elements.length; i++) {
			if(frm.elements[i].type == 'radio' && frm.elements[i].checked == true) {
				type = frm.elements[i].value ;
		switch (type) {
			case 'song' : type = 1; break;
			case 'singer' : type = 2; break;
			case 'album' : type = 3; break;
			case 'audio' : type = 4; break;
		}
		last_url = '';
		if (type==4)
		window.location.href = '#Zget/'+kw;
		else
		window.location.href = '#Search/'+type+'/'+kw+'/1';
			}		
		}
	}
	return false;

}
function do_search2() {
	kw = document.getElementById("keyword2").value;
	if (!kw) alert("Vous n'avez pas entré le mot-clé");
	else {
		kw = encodeURIComponent(kw);
		s_type = document.getElementById("searchType2");
		type = s_type.options[s_type.selectedIndex].value;
		switch (type) {
			case 'song' : type = 1; break;
			case 'singer' : type = 2; break;
			case 'album' : type = 3; break;
			case 'audio' : type = 4; break;
		}
		last_url = '';
		if (type==4)
		window.location.href = '#Zget/'+kw;
		else
		window.location.href = '#Search/'+type+'/'+kw+'/1';
	}
	return false;

}
function do_search_tab() {
	kw = document.getElementById("keyword").value;
	if (!kw) alert("Vous n'avez pas entré le mot-clé");
	else {
		kw = encodeURIComponent(kw);
		type = document.getElementById("t").value;
		last_url = '';
		if (type==4)
		window.location.href = '#Zget/'+kw;
		else
		window.location.href = '#Search/'+type+'/'+kw+'/1';
	}
	return false;

}

// + ---------------------- +
// |        PLAYLIST        |
// + ---------------------- +

function reloadPlaylist(add_id,remove_id) {
	try{
		document.getElementById("playlist_field").innerHTML = loadingText;
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = playlist_handleResponse;
		http.send('reloadPlaylist=1&add_id='+add_id+'&remove_id='+remove_id);
	}
	catch(e){}
	finally{}
}

function playlist_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			document.getElementById("playlist_field").innerHTML = response;
		}
  	}
	catch(e){}
	finally{}
}

function addToPlaylist(song_id)
{
	reloadPlaylist(song_id,0);
}
function removeFromPlaylist(song_id)
{
	reloadPlaylist(0,song_id);
}

/*------------------------------------------------------*/


function trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}

// + ------------------- +
// |        LOGIN        |
// + ------------------- +

function login_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("login_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("login_loading").innerHTML = response;
				document.getElementById("login_loading").style.display = "block";
			}
			else window.location.href = '?refresh=1';
						
		}
  	}
	catch(e){}
	finally{}
}

function login(form) {
	name = eval('encodeURIComponent('+form+'.name.value);');
	pwd = eval('encodeURIComponent('+form+'.pwd.value);');
//	remember = document.getElementById("remember").checked; +'&remember='+remember
	if(	trim(name) == "" ||	trim(pwd) == "")
		alert("Bạn chưa nhập đầy đủ thông tin");
	else {
		try{
			document.getElementById("login_loading").innerHTML = loadingText;
			document.getElementById("login_loading").style.display = "block";
			http.open('POST',  'index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = login_handleResponse;
			http.send('login=1&name='+name+'&pwd='+pwd);  
			
		}
		catch(e){}
		finally{}
	}
	return false;
}

// + ---------------------- +
// |        REGISTER        |
// + ---------------------- +

function reg_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("reg_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("reg_loading").innerHTML = response;
				document.getElementById("reg_loading").style.display = "block";
				document.getElementById("submit").disabled=false;document.getElementById("submit").value="Re-entrer";
			}
			else window.location.href = '#Login';
			
		}
  	}
	catch(e){}
	finally{}
}

function reg_check_values() {
	ok = false;
	name = encodeURIComponent(document.getElementById("reg_name").value);
	pwd = encodeURIComponent(document.getElementById("reg_pwd").value);
	pwd2 = encodeURIComponent(document.getElementById("reg_pwd2").value);
	email = encodeURIComponent(document.getElementById("reg_email").value);
	u_area = document.getElementById("u_area");
	area = u_area.options[u_area.selectedIndex].value;
	agree = document.getElementById("agree").checked;
	sec_num = document.getElementById("sec_num").value;  
	
	s = document.getElementsByName("reg_sex");
	if (s[0].checked) sex = s[0].value;
	if (s[1].checked) sex = s[1].value;
	
	if(	trim(name) == "" ||	trim(pwd) == "" ||	trim(pwd2) == "" ||	trim(email) == "" )
		alert("Vous n'avez pas entré suffisamment d'informations");
	else
		if (pwd != pwd2) alert("Incorrecte Confirmation du mot de passe");
		else if (!agree) alert("Vous devez accepter les règles et termes ");
		else {
			try{
				document.getElementById("reg_loading").innerHTML = loadingText;
				document.getElementById("reg_loading").style.display = "block";
				http.open('POST',  'index.php');
				http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
				http.onreadystatechange = reg_handleResponse;
				http.send('reg=1&name='+name+'&pwd='+pwd+'&email='+email+'&sex='+sex+'&area='+area+'&sec_num='+sec_num);
				document.getElementById("submit").disabled=true;document.getElementById("submit").value="Attendre...";
			}
			catch(e){}
			finally{}
		}
	return ok;
}


// + ----------------------------- +
// |        FORGOT PASSWORD        |
// + ----------------------------- +

function forgot_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			document.getElementById("forgot_loading").style.display = "none";
			var response = http.responseText;
			if (response) {
				document.getElementById("forgot_loading").innerHTML = response;
				document.getElementById("forgot_loading").style.display = "block";
				document.getElementById("submit").disabled=false;document.getElementById("submit").value="Re-entrer";
			}
		}
  	}
	catch(e){}
	finally{}
}

function forgot() {
	email = encodeURIComponent(document.getElementById("u_email").value);
	if(	trim(email) == "" )	alert("Vous n'avez pas entré l'adresse e-mail");
	else {
		try{
			document.getElementById("forgot_loading").innerHTML = loadingText;
			document.getElementById("forgot_loading").style.display = "block";
			http.open('POST',  'index.php');
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = forgot_handleResponse;
			http.send('forgot=1&email='+email);
			document.getElementById("submit").disabled=true;document.getElementById("submit").value="Attendre...";
		}
		catch(e){}
		finally{}
	}
	return false;
}

function request_check_values() {
    ok = false;
    title_request = encodeURIComponent(document.getElementById("title_request").value);
    singer_request = encodeURIComponent(document.getElementById("singer_request").value);
    author_request = encodeURIComponent(document.getElementById("author_request").value);
    ym_request = encodeURIComponent(document.getElementById("ym_request").value);
    email_request = encodeURIComponent(document.getElementById("email_request").value);
    info_request = encodeURIComponent(document.getElementById("info_request").value);
   
    if(    trim(title_request) == "" ||    trim(email_request) == "" )
        alert("Vous n'avez pas entré suffisamment d'informations");
    else {
        try{
            document.getElementById("reg_loading").innerHTML = loadingText;
            document.getElementById("reg_loading").style.display = "block";
            http.open('POST',  'index.php');
            http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            http.onreadystatechange = reg_handleResponse;
            http.send('request=1&title_request='+title_request+'&author_request='+author_request+'&singer_request='+singer_request+'&ym_request='+ym_request+'&email_request='+email_request+'&info_request='+info_request);
           
        }
        catch(e){}
        finally{}
    }
    return ok;
}

function ntxNext(){
    if(document.getElementById("current_id"))
       n = document.getElementById("current_id").value;
  	   window.location.href = '#Play/'+n+'/Auto-Play';
}

// BEGIN RATING
function Rating(media_id,star) {
	try {
		document.getElementById("rate_s").innerHTML = loadingText;
		document.getElementById("rate_s").style.display = "block";
		hide_rating_process();
		http.open('POST',  'rating.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("rating_field").innerHTML = http.responseText;
			}
		}
		http.send('rating=1&media_id='+media_id+'&star='+star);
	}
	catch(e){}
	finally{}
	return false;
}


	// pre-fetch image
	(new Image()).src = RATE_OBJECT_IMG;
	(new Image()).src = RATE_OBJECT_IMG_HALF;
	(new Image()).src = RATE_OBJECT_IMG_BG;

	function show_star(starNum,rate_text) {
		remove_star();
		document.getElementById("rate_text_d").innerHTML = rate_text;
		full_star(starNum);
	}
	
	function full_star(starNum) {
		for (var i=0; i < starNum; i++)
			document.getElementById('star'+ (i+1)).src = RATE_OBJECT_IMG;
	}
	function remove_star() {
		for (var i=0; i < 5; i++)
			document.getElementById('star' + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
	}
	function remove_all_star() {
		for (var i=0; i < 5; i++) document.getElementById('star' + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
		document.getElementById("rate_text_d").innerHTML = 'Commentaires';
	}
	function show_rating_process() {
		if(document.getElementById("rating_process")) document.getElementById("rating_process").style.display = "block";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "none";
	}
	function hide_rating_process() {
		document.getElementById("rating_process").style.display = "none";
		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "block";
	}
// END RATING

function bbcode(icon) {

   document.feeling.comment_content.value =   document.feeling.comment_content.value + icon;

}

function permission(){
      alert("Vous devez être connecté pouvez utiliser cette fonctionnalité");
    return false;
}

// + ---------------------------------- +
// |            PLAY WMP                |
// + ---------------------------------- +
function Playing(media_id) {
    try {
        document.getElementById("quick_play1_"+media_id).innerHTML = loadingText;
        document.getElementById("quick_play1_"+media_id).style.display = "block";
        http.open('POST',  'playing.php');
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = function() {
            if((http.readyState == 4)&&(http.status == 200)){
                document.getElementById("quick_play1_field_"+media_id).innerHTML = http.responseText;
                //document.getElementById("quick_play1_field_"+media_id).innerHTML = '<div id=quick_play1_'+media_id+'></div>';
                document.getElementById("control_quick_play1_"+media_id).innerHTML = '<a href=# onclick="return FPlaying(\''+media_id+'\');" class="a_player" style="color: #ffffff;text-decoration: none;">Cliquez ici pour écouter dans Flash Player</a>';
            }
        }
    http.send('qplay=1&media_id='+media_id);
    }
    catch(e){}
    finally{}
    return false;
}
function FPlaying(media_id) {
       try {
        document.getElementById("ht_play_"+media_id).innerHTML = loadingText;
        document.getElementById("ht_play_"+media_id).style.display = "block";
        http.open('POST',  'playing.php');
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = function() {
            if((http.readyState == 4)&&(http.status == 200)){
                document.getElementById("ht_play_field_"+media_id).innerHTML = http.responseText;
                //document.getElementById("ht_play_field_"+media_id).innerHTML = '<div id=ht_play_'+media_id+'></div>';
                document.getElementById("control_quick_play1_"+media_id).innerHTML = '<a href=# onclick="return Playing(\''+media_id+'\');" class="a_player" style="color: #ffffff;text-decoration: none;">Cliquez ici pour écouter avec Windows Media Player</a>';
            }
        }
    http.send('fplay=1&media_id='+media_id);
    }
    catch(e){}
    finally{}
    return false;
}

// + ----------------------------- +
// |       BEGIN Quick Play		   |
// + ----------------------------- +
function QPlay(media_id) {
    try {
        document.getElementById("quick_play_"+media_id).innerHTML = loadingText;
        document.getElementById("quick_play_"+media_id).style.display = "block";
        http.open('POST',  'qplay.php');
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = function() {
            if((http.readyState == 4)&&(http.status == 200)){
                document.getElementById("quick_play_field_"+media_id).innerHTML = http.responseText;
                document.getElementById("control_quick_play_"+media_id).innerHTML = '<a href=# onclick="return CQPlay(\''+media_id+'\');"><img src="images/qplaycol.gif" border="0"></a>';
            }
        }
    http.send('qplay=1&media_id='+media_id);
    }
    catch(e){}
    finally{}
    return false;
}

function CQPlay(media_id) {
        document.getElementById("quick_play_field_"+media_id).innerHTML = '<div id=quick_play_'+media_id+'></div>';
        document.getElementById("control_quick_play_"+media_id).innerHTML = '<a href="#" onclick="return QPlay(\''+media_id+'\');"><img src="images/qplayex.gif" border="0"></a>';
    return false;
}
// + ----------------------------- +
// |       END Quick Play		   |
// + ----------------------------- +

function killErrors() {
return true;
}
window.onerror = killErrors;

function addText(elname, wrap1, wrap2) {
	if (document.selection) { // for IE 
		var str = document.selection.createRange().text;
		document.forms['add'].elements[elname].focus();
		var sel = document.selection.createRange();
		sel.text = wrap1 + str + wrap2;
		return;
	} else if ((typeof document.forms['add'].elements[elname].selectionStart) != 'undefined') { // for Mozilla
		var txtarea = document.forms['add'].elements[elname];
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		var oldScrollTop = txtarea.scrollTop;
		//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 + wrap1 + s2 + wrap2 + s3;
		txtarea.selectionStart = s1.length;
		txtarea.selectionEnd = s1.length + s2.length + wrap1.length + wrap2.length;
		txtarea.scrollTop = oldScrollTop;
		txtarea.focus();
		return;
	} else {
		insertText(elname, wrap1 + wrap2);
	}
}

var submitcount=0;
   function checkSubmit() {

      if (submitcount == 0)
      {
      submitcount++;
      document.Surv.submit();
      }
   }


function wordCounter(field, countfield, maxlimit) {
wordcounter=0;
for (x=0;x<field.value.length;x++) {
      if (field.value.charAt(x) == " " && field.value.charAt(x-1) != " ")  {wordcounter++}  // Counts the spaces while ignoring double spaces, usually one in between each word.
      if (wordcounter > 250) {field.value = field.value.substring(0, x);}
      else {countfield.value = maxlimit - wordcounter;}
      }
   }

function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit)
      {field.value = field.value.substring(0, maxlimit);}
      else
      {countfield.value = maxlimit - field.value.length;}
  }
/*----------- Dom Image Rollover ---------------/
/												/
/				  KOOLVN.NET					/
/												/
/----------- Dom Image Rollover ---------------*/

function imageholderclass(){
	this.over=new Array();
	this.down=new Array();
	this.src=new Array();
	this.store=store;
	
	function store(src, down, over){
		var AL=this.src.length;
		this.src[AL]=new Image(); this.src[AL].src=src;
		this.over[AL]=new Image(); this.over[AL].src=over;
		this.down[AL]=new Image(); this.down[AL].src=down;
	}
}

var ih = new imageholderclass();
var mouseisdown=0;

function preloader(t){
	for(i=0;i<t.length;i++){
		if(t[i].getAttribute('srcover')||t[i].getAttribute('srcdown')){
			
			storeimages(t[i]);
			var checker='';
			checker=(t[i].getAttribute('srcover'))?checker+'A':checker+'';
			checker=(t[i].getAttribute('srcdown'))?checker+'B':checker+'';
			
			switch(checker){
			case 'A' : mouseover(t[i]);mouseout(t[i]); break;
			case 'B' : mousedown(t[i]); mouseup2(t[i]); break;
			case 'AB' : mouseover(t[i]);mouseout(t[i]); mousedown(t[i]); mouseup(t[i]); break;
			default : return;			
			}
			
			if(t[i].src){t[i].setAttribute("oldsrc",t[i].src);}
		}
	}
}
function mouseup(t){
	var newmouseup;
	if(t.onmouseup){
		return;
	/*	t.oldmouseup=t.onmouseup;
		newmouseup=function(){mouseisdown=0;this.src=this.getAttribute("srcover");this.oldmouseup();}*/

	}
	else{newmouseup=function(){mouseisdown=0;this.src=this.getAttribute("srcover");}}
	t.onmouseup=newmouseup;
}

function mouseup2(t){
	var newmouseup;
	if(t.onmouseup){
		return;
/*		t.oldmouseup=t.onmouseup;
		newmouseup=function(){mouseisdown=0;this.src=this.getAttribute("oldsrc");this.oldmouseup();}*/
		}
	else{newmouseup=function(){mouseisdown=0;this.src=this.getAttribute("oldsrc");}}
	t.onmouseup = newmouseup;
}

function mousedown(t){
	var newmousedown;
	if(t.onmousedown){
		return;
/*		t.oldmousedown=t.onmousedown;
		newmousedown=function(){if(mouseisdown==0){this.src=this.getAttribute("srcdown");this.oldmousedown();}}*/
	}
	else{newmousedown=function(){if(mouseisdown==0){this.src=this.getAttribute("srcdown");}}}
	t.onmousedown=newmousedown;
}

function mouseover(t){
	var newmouseover;
	if(t.onmouseover){
		return;
/*		t.oldmouseover=t.onmouseover;
		newmouseover=function(){this.src=this.getAttribute("srcover");this.oldmouseover();}*/
	}
	else{newmouseover=function(){this.src=this.getAttribute("srcover");}}
	t.onmouseover=newmouseover;
}

function mouseout(t){

	var newmouseout;
	if(t.onmouseout){
		return;
/*		t.oldmouseout=t.onmouseout;
		newmouseout=function(){this.src=this.getAttribute("oldsrc");this.oldmouseout();}*/
	}
	else{newmouseout=function(){this.src=this.getAttribute("oldsrc");}}
	t.onmouseout=newmouseout;
}

function storeimages(t){
	var s=(t.getAttribute('src'))?t.getAttribute('src'):'';
	var d=(t.getAttribute('srcdown'))?t.getAttribute('srcdown'):'';
	var o=(t.getAttribute('srcover'))?t.getAttribute('srcover'):'';
	ih.store(s,d,o);
}

function preloadimgsrc(){
	if(!document.getElementById) return;
	var it=document.getElementsByTagName('IMG');
	var it2=document.getElementsByTagName('INPUT');
	preloader(it);
	preloader(it2);
}

if(window.addEventListener){window.addEventListener("load", preloadimgsrc, false);} 
else{
	if(window.attachEvent){window.attachEvent("onload", preloadimgsrc);}
	else{if(document.getElementById){window.onload=preloadimgsrc;}}
}
/*------------------------------------------------------------*/

//-----------------------TRA TU DIEN------------------------
function get_quick_meaning(word,dict) {
	try{
		document.getElementById("data_field").align = "left";
		field = document.getElementById("data_field");
		if (loading==0) {
			loading=1;
			show_Loading();
		}
		if((word=='') && (dict=='')){
		word = encodeURIComponent(document.getElementById('word').value);
		s_type = document.getElementById("lstdict");
		dict = encodeURIComponent(s_type.options[s_type.selectedIndex].value);
		}
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponse;
		http.send('tratu=meaning&word='+word+'&dict='+dict);
	}
	catch(e){}
	finally{}
}
function get_sound(word,dict) {
	try{
		field = document.getElementById("sound");
		field.innerHTML = loadingText;
		if((word=='') && (dict=='')){
		word=document.getElementById('word').value;
		word = encodeURIComponent(word);
		s_type = document.getElementById("lstdict");
		dict = s_type.options[s_type.selectedIndex].value;
		}
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponse;
		http.send('tratu=sound&word='+word+'&dict='+dict);
	}
	catch(e){}
	finally{}
}
//-------------------------------END------------------------