﻿//Replace images
function movepic(img_name,img_src) {
document[img_name].src=img_src;
}

//Search page
function search_page(id)
{
	str = "inget=inget";
	
	Update_div('search_1', id, str);
}

//Get send mesage form
function get_form(id, to)
{
	e = document.getElementById(id);

	if(e.style.height == '225px')
		clear_div(id);
	else
	{
		e.style.height = '225px';
		e.style.padding = '5px';
		e.style.background = '#EEEEFF';

		str = "to="+to+"&div="+id+"&browser="+navigator.appName;
	
		Update_div('get_msgform', id, str);
	}
}

//Update the users settings in my page
function update_settings()
{
	if(document.settings.o_pass.value.length != 0)
	{
		str="o_pass="+document.settings.o_pass.value;
		
		if(document.settings.n_pass.value.length != 0)
			str=str+"&n_pass="+document.settings.n_pass.value;
		else
			str=str+"&n_pass=x";
		
		str=str+"&mail="+document.settings.mail.value;
		str=str+"&presentation="+document.settings.presentation.value.replace(new RegExp( "\\n", "g" ), "Lxq9_43");
		
		Update_div('update_settings', 'setting_setup', str);
	}
	else
		alert("Inget lösenord angivet");
}

//Mail check
function checkMail(x)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x))
		return true;
	else 
		return false;
}

//sign_up validation.
function check_sign_up()
{
	if(checkMail(document.sign_up.mail_1.value))
		if(document.sign_up.mail_1.value == document.sign_up.mail_2.value)
			if(document.sign_up.username.value.length >= 3)
				if(document.sign_up.pass.value.length >= 3)
					return true;
				else
					alert ('Lösenordet måste vara minst 3 tecken.');
			else
				alert ('Användarnamnet måste vara minst 3 tecken.');
		else
			alert ('E-mailadresserna överensstämmer ej');
	else
		alert ('Ogiltig e-mailadress');
	
	return false;
}

//Result of user search when a button has been pressed in the search field
function show_users(search,id)
{
	if(search == "" || search == null)
		document.getElementById(id).innerHTML="";
	else
	{
		str= "str="+search;

		Update_div('return_user', id, str)
	}
}

//Result of wordlist search when a button has been pressed in the search field
function show_wordlists(search,id)
{
	if(search == "" || search == null || search.length < 3)
		document.getElementById(id).innerHTML="";
	else
	{
		str= "str="+search;

		Update_div('return_wordlists', id, str)
	}
}

//Return a list of words in a specific wordlist
function show_words(wordlist_id, div_id)
{
	e = document.getElementById(div_id);
	
	if(( e.innerHTML == "") || (e.innerHTML == null))
	{
		str= "wordlist="+wordlist_id;
	
		Update_div('show_words', div_id, str);
	}
	else
		e.innerHTML = "";
}

//Show the correct searchform for selected radiobutton
function show_search(div_id, str)
{
		str = "typ="+str;
		Update_div('search_2', div_id, str);
		clear_div('results');
}

//Remove a selected word if the user is correct
function delete_word(wordlist_id, div_id, del_word)
{
		str= "wordlist="+wordlist_id+"&del_word="+del_word;
	
		Update_div('show_words', div_id, str);
}

//List wordlists by owner
function owner_wordlists(Wordlist_owner, div_id)
{
		str= "owner="+Wordlist_owner+"&browser="+navigator.appName;
		
		Update_div('owner_wordlists', div_id, str);
}

//My page
function my_page(multi, div_id)
{
	str= "multi="+multi;
	
	Update_div('my_page', div_id, str);
}

//practise update
function practise_update(ok, div_id)
{
	str= "ok="+ok;
	
	Update_div('practise', div_id, str);
}

//Delete_entire_wordlist
function delete_wordlist(wordlist_id, wordlist_name)
{
	str='Vill du verkligen tabort ordlistan:\n' + wordlist_name;
	if(confirm(str))
	{
		str = 'grp_operation=delete';
		str = str + '&wordlist_id=' + wordlist_id;
		Update_div('my_wordlists', 'info_layer', str);		
	}
}

//Add new wordlist
function add_wordlist(info)
{
	if(info.lesson.value.length >= 3)
	{	
		for (count = 0; count <11; count++) {
			if (info.level[count].checked)
				break;
			}
		str = 'grp_operation=add' +
		'&lesson=' + info.lesson.value +
		'&level=' + info.level[count].value +
		'&comment=' + info.comment.value.replace(new RegExp( "\\n", "g" ), "Lxq9_43") +
		'&folder=' + info.folder.value +
		'&category=';
		
		//'&lang=' + info.lang.options[info.lang.selectedIndex].value +
		
		
		for(i=0;i<10;i++)
		{
			if(info.kattegorier[i].checked)
				tmp = '1_';
			else
				tmp = '0_';
				
			str = str + tmp;
		}

		Update_div('my_wordlists', 'info_layer', str);
	}
	else
	{
		alert ('För kort ordlist-namn');
	}
}

function Add_favourites(info)
{
	str = "fav_operation=add&wordlist_id=";
	
	if(info.max.value == 1)
	{
		if(info.wordlist_id.checked)
		{
			tmp = info.wordlist_id.value+'_';
			str = str + tmp;
			
		}
	}
	else
	{
		for(i=0;i<info.max.value;i++)
		{
			if(info.wordlist_id[i].checked)
			{
				tmp = info.wordlist_id[i].value+'_';
				str = str + tmp;
			}
		}
	}

	Update_div('my_wordlists', 'info_layer', str);
}

//Edit wordlist
function edit_wordlist(info)
{
	if(info.lesson.value.length >= 3)
	{	
		for (count = 0; count <11; count++) {
			if (info.level[count].checked)
				break;
			}
		str = 'grp_operation=update'+
		'&wordlist_id=' + info.wordlist_id.value +
		'&lesson=' + info.lesson.value +
		'&level=' + info.level[count].value +
		'&comment=' + info.comment.value.replace(new RegExp( "\\n", "g" ), "Lxq9_43") +
		'&folder=' + info.folder.value +
		'&category=';
		
		//'&lang=' + info.lang.options[info.lang.selectedIndex].value +
		
		for(i=0;i<10;i++)
		{
			if(info.kattegorier[i].checked)
				tmp = '1_';
			else
				tmp = '0_';
				
			str = str + tmp;
		}
//alert(str);
		Update_div('my_wordlists', 'info_layer', str);
	}
	else
	{
		alert ('För kort ordlist-namn');
	}
}

function go_practise(info)
{
	str = "initiate=";

	if(info.max_grp.value)
	{
		if(info.max_grp.value == 1)
		{
			if(info.wordlist_id_grp.checked)
			{
				tmp = info.wordlist_id_grp.value+'_';
				str = str + tmp;
			}
		}
		else
		{
			for(i=0;i<(info.max_grp.value);i++)
			{
				if(info.wordlist_id_grp[i].checked)
				{
					tmp = info.wordlist_id_grp[i].value+'_';
					str = str + tmp;
				}
			}
		}
	}
	
	if(info.max_fav.value)
	{
		if(info.max_fav.value == 1)
		{
			if(info.wordlist_id_fav.checked)
			{
				tmp = info.wordlist_id_fav.value+'_';
				str = str + tmp;
			}
		}
		else
		{
			for(i=0;i<(info.max_fav.value);i++)
			{
				if(info.wordlist_id_fav[i].checked)
				{
					tmp = info.wordlist_id_fav[i].value+'_';
					str = str + tmp;
				}
			}
		}
	}

	for (count = 0; count <2; count++) {
	if (info.start_language[count].checked)
		break;
	}
	
	str = str + "&start_language=" + info.start_language[count].value;
	str = str + "&ok=x";

	Update_div('practise', 'info_layer', str);
}

function admin_word(obj) {
	if(document.getElementById("se").value != '')
	{
		if(document.getElementById("jp1").value != '')
		{
		  var poststr = "Wordlist_ID=" + encodeURI( document.getElementById("Wordlist_ID").value ) +
		                "&word_operation=" + encodeURI( document.getElementById("word_operation").value) +
		                "&se=" + encodeURI( document.getElementById("se").value) +
		                "&jp1=" + encodeURI( document.getElementById("jp1").value );
		  if(document.getElementById("jp2").value != '')
		  	poststr = poststr + "&jp2=" + encodeURI( document.getElementById("jp2").value );
		  	
		  if(document.getElementById("Word_ID").value != '')
		  	poststr = poststr + "&Word_ID=" + encodeURI( document.getElementById("Word_ID").value );
		  	
		  makePOSTRequest('./ajax/add_word.php','info_layer', poststr);
		}
		else
			alert('Hiragana-fältet är tomt');
	}
	else
		alert('Svenska-fältet är tomt');
}

function send_message(obj) {
		  var poststr = "subject=" + encodeURI( document.getElementById("subject").value ) +
		                "&message=" + encodeURI( document.getElementById("message").value.replace(new RegExp( "\\n", "g" ), "Lxq9_43")) +
		                "&to=" + encodeURI( document.getElementById("to").value );
		  makePOSTRequest('./ajax/my_page.php','info_layer', poststr);
}

function send_post(obj,table_id,thread_id) {
  if(document.getElementById("subject_exist").value == 1)
  {
		if(document.getElementById("subject").value.length >= 3)
		{
	  	poststr = "subject=" + encodeURI( document.getElementById("subject").value ) + "&";
	  }
  	else
			alert ('Förkort subject');
  }
  else
  {
  	poststr = '';
  }
	if(document.getElementById("message").value.length >= 1)
	{
	  poststr = poststr +
	                "message=" + encodeURI( document.getElementById("message").value.replace(new RegExp( "\\n", "g" ), "Lxq9_43")) +
	                "&Table_ID=" + table_id +
	                "&Thread_ID=" + thread_id +
	                "&forum_operation=add";
	  makePOSTRequest('./ajax/forum_list.php','info_layer', poststr);
	}
	else
		alert ('Inget meddelande');
}

//Clears a div
function clear_div (id)
{
	e = document.getElementById(id);
	e.innerHTML = "";
	e.style.background = '#ffffff';
	e.style.height = '0px';
	e.style.padding = '0px';
}
