var nb_kw = 0;
var not_allowed_char = new Array('*','$','%',';','@','--','"', ',');

function change_data(src, id_dest){

	if( !test_kw(src) ){


		if (id_dest == 'desc_id'){
			if(src.value.length >= 150){
				src.value = src.value.substr(0, (src.value.length)-1);
				return;
			}
			else{
				$('char_desc').innerHTML = src.value.length;
				if(src.value.length >= 50){
					$('char_desc').className = 'grey_num';
				}
				else{
					$('char_desc').className = 'red_num';
				}
				document.getElementById(id_dest).innerHTML = src.value;
			}
		}
		else if (id_dest == 'title_id'){
			$('char_title').innerHTML = src.value.length;
			if(src.value.length >= 5){
				$('char_title').className = 'grey_num';
			}
			else{
				$('char_title').className = 'red_num';
			}

			document.getElementById(id_dest).innerHTML = src.value;
		}
		else if (id_dest == 'dispurl_id'){
			$('char_disp').innerHTML = src.value.length;
			if(src.value.length >= 5){
				$('char_disp').className = 'grey_num';
			}
			else{
				$('char_disp').className = 'red_num';
			}
			document.getElementById(id_dest).innerHTML = src.value;
		}

	}
}

function list_keyword(obj, host){
	var len = obj.value.length;
	//alert(len);

	if (len > 2){

		var url = 'http://'+host+'/ajax/ajaxListKw.php';
		var pars = 'keyword='+escape(obj.value);

		/* List of keywords */
		var myAjax = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: pars,
			onComplete: function(originalRequest) {disp_list_kw_found(originalRequest); return true}
		});
	}
}

function disp_list_kw_found(originalRequest){

	if (originalRequest.responseText != 'nothing'){
		var tab = originalRequest.responseText.split('|UU|');
		for(i=0 ; i < tab.length ; i++){
			if (tab[i] != ''){
				alert(tab[i]);
			}
		}
	}
}

function add_kw(){
	var kw_to_add = $('keyword_website').value;

	if( !test_kw($('keyword_website')) ){

		if (kw_to_add.length > 2){

			if (nb_kw < 5){

				var kw_to_add = $('keyword_website').value;
				var tab_kw = $('id_hidden_list_kw').value.split('|UU|');

				if (tab_kw.length > 0){
					for(i=0 ; i < tab_kw.length ; i++){
						if (tab_kw[i] != '' && tab_kw[i] == kw_to_add){
							alert('You have already added this keyword');
							return;
						}
					}
				}

				nb_kw++;
				$('id_list_kw_added').innerHTML = $('id_list_kw_added').innerHTML+'&nbsp;- '+kw_to_add+"<br />";
				$('id_hidden_list_kw').value = $('id_hidden_list_kw').value+'|UU|'+kw_to_add;
				$('keyword_website').value = '';
			}
			else{
				alert('You can\'t add more keywords');
			}
		}
		else{
			alert('The minimum length of the keyword has to be 3 characters');
		}
	}
}

function reset_kw(){
	nb_kw = 0;
	$('id_list_kw_added').innerHTML = '';
	$('id_hidden_list_kw').value = '';
}


function test_kw(obj_text){

	for (i=0 ; i < not_allowed_char.length ; i++){

		//alert(not_allowed_char[i]+': '+src.value.indexOf(not_allowed_char[i]));
		if (obj_text.value.indexOf(not_allowed_char[i]) != -1){
			obj_text.value = obj_text.value.substr(0, (obj_text.value.length)-1);
			alert('Character \''+not_allowed_char[i]+'\' not allowed');
			return false;
		}
	}
}

function open_help(){
	$('top_help').style.display='block';
	$('core_help').style.display='block';
}

function close_help(){
	$('top_help').style.display='none';
	$('core_help').style.display='none';
}

function check_form(){

	var len_title = $('title').value.length;
	var len_desc = $('desc').value.length;
	var len_dispurl = $('dispurl').value.length;
	
	//alert($('dispurl').value.indexOf('.'));

	//alert(len_title);
	//return false;
	//alert('Test: '+len_title);

	if (len_desc < 50){
		alert('The minimum number of character for the description is: 50');
		return false;
	}
	else if (len_title < 5){
		alert('The minimum number of character for the title is: 5');
		return false;
	}
	else if (len_dispurl < 5){
		alert('The minimum number of character for the display url is: 5');
		return false;
	}
	else if ($('dispurl').value.indexOf('.') == -1){
		alert('You should have at least a "." into your display url');
		return false;
	}

	return true;
}
