$(document).ready(function(){
	$('#region-button').bind('click',function(){
		window.location.href=$('#region-select').val();
	})
	$('#gyartok').bind('click',function(){
        changeTipus();
	});
	$('#tipus').bind('change',function(){
        var tsel = false;
		$("#tipus option:selected").each(function (i,selected) {
              if ( i != 0 ) {
            	  $(selected).attr('selected','');
              }
              else {
            	  tsel = true;
              }
        });
		if ( tsel === false ) {
			$('#tipus option[value=0]').attr('selected','selected');
		}
	});
	$('#evjarat').bind('change',function(){
        var esel = false;
		$("#evjarat option:selected").each(function (i,selected) {
              if ( i != 0 ) {
            	  $(selected).attr('selected','');
              }
              else {
            	  esel = true;
              }
        });
		if ( esel === false ) {
			$('#evjarat option[value=0]').attr('selected','selected');
		}
	});
	$('#elhelyezkedes').bind('change',function(){
        var elsel = false;
		$("#elhelyezkedes option:selected").each(function (i,selected) {
              if ( i != 0 ) {
            	  $(selected).attr('selected','');
              }
              else {
            	  elsel = true;
              }
        });
		if ( elsel === false ) {
			$('#elhelyezkedes option[value=0]').attr('selected','selected');
		}
	});
	/*$('#search_button').bind('click',function(){
		sendsearchform();
	});*/
	$('#gyartok').keypress(function(event){
		if (event.keyCode == '13') {sendsearchform();}
	});
	$('#tipus').keypress(function(event){
		if (event.keyCode == '13') {sendsearchform();}
	});
	$('#evjarat').keypress(function(event){
		if (event.keyCode == '13') {sendsearchform();}
	});
	$('#elhelyezkedes').keypress(function(event){
		if (event.keyCode == '13') {sendsearchform();}
	});
	/*$("#frm_button").bind('click',function(){
		
	});*/
	$("#sendbuy").bind('click',function(){
		if ( $('#b_name').val() == ''){
			$('#buymsg').html('<div class="error">'+b_name+'</div>');
			$('#footer').css('position','relative');
		}
		else if ( $('#b_email').val() == ''){
			$('#buymsg').html('<div class="error">'+b_email+'</div>');
			$('#footer').css('position','relative');
		}
		else if ( !isValidEmailAddress($('#b_email').val() ) ){
			$('#buymsg').html('<div class="error">'+bad_email+'</div>');
			$('#footer').css('position','relative');
		}
		else if ( $('#b_tel').val() == ''){
			$('#buymsg').html('<div class="error">'+b_tel+'</div>');
			$('#footer').css('position','relative');
		}
		else if ( $('#b_szab:checked').val() != 1){
			$('#buymsg').html('<div class="error">'+b_szab+'</div>');
			$('#footer').css('position','relative');
		}
		else {
			$('#buyform').submit();
		}
	});
});

function sendsearchform(){
	if ( $("#gyartok option:selected").val() == undefined ) {
		$("#gyartok option:first-child").attr('selected','selected')
	}
	if ( $("#telepules option:selected").val() != undefined ){
		if ( $("#telepules option:selected").val() == 0 ) {
			$($('#noconty')).html('<div class="error">'+no_cnt+'</div>');
		}
		else {
			$('#searchform').submit();
		}
	}
	else {
		$('#searchform').submit();
	}
}

function sendcontactform(){
	if ( $('#cont_name').val() == '' ){
		 $('#showmailmessage').html('<div class="error">'+no_name+'</div>');
		 $('#footer').css('position','relative');
	}
	else if ( $('#cont_email').val() == '' ) {
		$('#showmailmessage').html('<div class="error">'+no_mail+'</div>');
		$('#footer').css('position','relative');
	}
	else if ( !isValidEmailAddress($('#cont_email').val() ) ){
		$('#showmailmessage').html('<div class="error">'+bad_email+'</div>');
		$('#footer').css('position','relative');
	}
	else $('#contactform').submit();
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function changeTipus(){
	var gysel = false;
	$("#gyartok option:selected").each(function (i,selected) {
          if ( i != 0 ) {
        	  $(selected).attr('selected','');
          }
          else {
        	  gysel = true;
          }
    });
	if ( gysel === false ) {
		$('#gyartok option[value=0]').attr('selected','selected');
		return;
	}
    $.post("seacrhfilter.php", { gyarto: $("#gyartok option:selected").val() },
    	function(data){
    		$('#tipus').html(data);
    	}
    );
}
