$(document).ready(function() {
	$('.show').click(function(){
		$(this).next('.hide:hidden').slideDown('slow').siblings('.hide:visible').slideUp('slow');
	});
});


//------- KERESÉS ----------

var req = false;
var tartalom;
var save = true;
function search(inputString) {
	if (save) {
		tartalom = $('#content').html();
		save=false;
	}
	$('#content').html('<div id="top_banner"></div><p style="text-align: center;"><img src="images/ajax.gif"  /></p>');
	if(inputString.length == 0) {
		$('#content').html(tartalom); // Hide the suggestions box
		save=true;
	} else {
		if (req) req.abort();
		req = $.ajax({
			type: "GET",
			url: "search.php?searchstring="+inputString,
			datatype: "html",
			success: function(data){
				//$('#right').show(); // Show the suggestions box
				$('#content').html(data); // Fill the suggestions box
			}
		});
	}
}

function newsletter() {
	var user = $('#newsletter #username').attr('value');
	var pass = $('#newsletter #useremail').attr('value');
	if (user!='Név' && checkMail(pass))	{
		$.ajax({
				type: "GET",
				url: "helper.php?user="+user+"&pass="+pass,
				datatype: "html",
				success: function(data){
					if (data=='ok')	{
						$('#newsletter').hide();
						$('#nerror').html('<p>Kedves '+user+'!<br />Sikeresen feliratkozott a hírlevelünkre.</p>');
						$('#nerror').show();
					}	else{
						
					}
				}
		});
	}	else	{
		$('#nerror').html('<p style="color: red;">Nem megfelelő adatok.</p>');
		$('#nerror').show();
	}
	return false;
}



function checkMail(email){
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) {
		return true;
	}
	return false;
}

function checkTel(tel){
	var filter = /^([0-9\+\-\/])+$/;
	if (filter.test(tel.replace(/\s/g, ''))) {
		return true;
	}
	return false;
}

function validateform(formid) {
	error=false;
	$('#'+formid+' :input').each(function()	{
		if (this.value=='' && this.name!='other' && this.name!='company' && this.name!='sendername' && this.name!='recivername' && this.name!='sendingtime')	{
			$(this).addClass("infocus");
			error=true;
		}	else	{
			$(this).removeClass("infocus");
		}
		if (this.name=='usermail')	{
			if (!checkMail(this.value))	{
				$(this).addClass("infocus");
				error=true;
			}
		}
		if (this.name=='usertel')	{
			if (!checkTel(this.value))	{
				$(this).addClass("infocus");
				error=true;
			}
		}
		if (this.name=='password2') {
			if (this.value != $(':input[name=password1]').attr('value') || this.value=='') {
				$(this).addClass("infocus");
				$(':input[name=password1]').addClass("infocus");
				error=true;
			}	else	{
				$(this).removeClass("infocus");
				$(':input[name=password1]').removeClass("infocus");
			}
		}
	});
	
	if (error)	{
		return false;
	}	else	{
		return true;
	}
}

function vocabulary(car) {
	$('#vocabulary').html('<p style="text-align: center;"><img src="images/ajax.gif"  /></p>');
	$.ajax({
		type: "GET",
		url: "szotar.php?car="+car,
		datatype: "html",
		success: function(data){
			$('#vocabulary').html(data);
			$('.show').click(function(){
				$(this).next('.hide:hidden').slideDown('slow').siblings('.hide:visible').slideUp('slow');
			});
		}
	});
}


function login() {
	$('#logerror').hide();
	$.ajax({
		type: "GET",
		url: "login.php?user="+$(':input[name=loginname]').attr('value')+"&pass="+$(':input[name=loginpass]').attr('value'),
		datatype: "html",
		success: function(data){
			if (data!='ok')	{
				$(':input[name=loginname]').addClass("infocus");
				$(':input[name=loginpass]').addClass("infocus");
				$('#logerror').html('<p style="color: red;">Hibás név és/vagy jelszó!</p>');
				$('#logerror').show();
				return false;
			}	else	{
				$.ajax({
				type: "GET",
				url: "setsession.php?user="+$(':input[name=loginname]').attr('value'),
				datatype: "html",
				success: function(data){
					var sURL = unescape(window.location.pathname);
					window.location.href = sURL;
				}
			})
		}
		}
	});
	return false;
}
