function loginClient()
{
	var mobile = $("#login_mobile_n").val();
	var pass = $("#login_password").val();
	
	 $.post("/ajax/userLogin",{ mobile: mobile, pass: pass},
            function(data){

            	if(data == "1")
            		window.location.href='/orders';
                else
                	$("#login_form").html(data);
            }
        );
        
    return false;
}

function logoutClient()
{
	 $.post("/ajax/userLogout",{},
            function(data){
           		window.location.reload();
            }
        );
        
    return false;
}

function checkInt(input){
	var re = /^[0-9]*$/;
	if (!re.test(input.value))
	{
		input.value = input.value.replace(/[^0-9]/g,"");
	}
	if (input.value == '0')
	{
		input.value = input.value = 1;
	}
}

function checkMobileAvail()
{
	var mobile = $("#phone_in").val();
	
	if(mobile.length == 9)
	
		$.post("/ajax/checkMobileAvail",{ mobile: mobile},
	            function(data){
	            	if(data == "1")
	            		$("#register_step1").submit();
	                else
	                	$("#phone_in_error").html(data);
	            }
	        );
}
function init()
{
     $(".mya_input").focus(function() {
       	$(this).css('background', 'transparent url(/images/active_input.gif) no-repeat');
    });

     $(".mya_input").blur(function() {
	$(this).css('background', 'transparent url(/images/mya_in.gif) no-repeat');
    });
}

window.onload = init;

function getAddress()
{
	if($('[name=edit_invoice]').val() == 1)
	{
	if (($('#edit_nroute_a').val()).length > 1)
	{
	    $('#edit_nroute_e').html('');
	    $('#edit_nroute_i').val($('#edit_nroute_a').val());
	    $('#edit_nroute_i').css('background', 'transparent url(/images/mya_in.gif) no-repeat');
	   
	}
	else
	{
	     $('#edit_nroute_e').html('Nazwa ulicy od 2 do 32 znaków');
	     $('#edit_nroute_i').css('background', 'transparent url(/images/err_input.gif) no-repeat');
	     $err=1;
	}

	if (($('#edit_padrs_a').val()).length > 0)
	{
	     $('#edit_padrs_e').html('');
	     $('#edit_padrs_i').val($('#edit_padrs_a').val());
	     $('#edit_padrs_i').css('background', 'transparent url(/images/mya_in.gif) no-repeat');
	     
	}
	else
	{
	    $('#edit_padrs_e').html('Podaj poprawny numer ulicy');
	    $('#edit_padrs_i').css('background', 'transparent url(/images/err_input.gif) no-repeat');
	    $err=1;
	}

	if (($('#edit_nflat_a').val()).length > 0)
	{
	     
	     $('#edit_nflat_i').val($('#edit_nflat_a').val());
	}

	if (($('#edit_ncity_a').val()).length > 1)
	{
	     $('#edit_ncity_e').html('');
	     $('#edit_ncity_i').val($('#edit_ncity_a').val());
	     $('#edit_ncity_i').css('background', 'transparent url(/images/mya_in.gif) no-repeat');
	    
	}
	else
	{
	    $('#edit_ncity_e').html('Nazwa miasta od 2 do 24 znaków');
	     $('#edit_ncity_i').css('background', 'transparent url(/images/err_input.gif) no-repeat');
	     $err=1;
	}
	
	if (($('#edit_cmail_a').val()).length == 6)
	{
	     $('#edit_cmail_e').html('');
	     $('#edit_cmail_i').val($('#edit_cmail_a').val());
	     $('#edit_cmail_i').css('background', 'transparent url(/images/mya_in.gif) no-repeat');
	}
	else
	{
	    $('#edit_cmail_e').html('Podaj poprawny kod pocztowy');
	    $('#edit_cmail_i').css('background', 'transparent url(/images/err_input.gif) no-repeat');
	    $err=1;
	}
	}
	   	
}

function updatePostcodeForAddress(fieldNames) {
	
	var city = $("#"+fieldNames.city)[0].value;
	
	if (city.length > 0) { 
		$.getJSON("/ajax/getPostcodeForAddress/"+ [city, $("#"+fieldNames.street)[0].value, $("#"+fieldNames.streetNo)[0].value].join("/") + "/",{ },
		        function(data){
					
					if (data && data.status && data.postcodes.length == 1) {
						
						$("#" + fieldNames.postcode)[0].value = data.postcodes[0].pna;
					}
		        }
		    );
	}
}

function updateAddressForPostcode(fieldNames) {
	
	$.getJSON("/ajax/getAddressForPostcode/"+$("#"+fieldNames.postcode)[0].value+"/",{ },
	        function(data){
		
				if (data && data.status) {
					if (data.postcodes.length == 1) {
					
						$("#" + fieldNames.street)[0].value = data.postcodes[0].street;
					}
					
					$("#" + fieldNames.city)[0].value = data.postcodes[0].city;
				}
	        }
	    );
}

dropdownSelect = function(id) {
		
	this.id  = id;
	this.element = $("#"+id);
    this.isFocused = false;
    
	var instance = this;
	
	this.options =  this.element.attr('values').split(';');
	this.element.after("<ul style='display:none'><li>" + this.options.join('</li><li>') + "</li></ul>");
	this.ul = this.element.next();
	
	this.itemSelect = function(item) {
		this.element[0].value = item.innerHTML;
		this.element.change();
	}
	
	var pos = $('#filterButton').position();
	
	this.onFocus = function () {
				
		var pos = $('#'+this.id).position();
		
		this.ul.css("position", "absolute");

		var top = pos.top + this.element.outerHeight();
		
		this.ul.css("top", top);
		this.ul.css("left", pos.left);
		this.ul.css("width", this.element.outerWidth());
		this.ul.css("display", "block");
		this.isFocused = true;
		return false;
	}
	
	this.blur = function() {
		this.isFocused = false;
		this.ul.css("display", "none");
	}
	
	this.element.focus(function(e) { e.stopPropagation(); return instance.onFocus();});
    this.element.click(function(e) { if (!instance.isFocused) { instance.element.focus(); }; e.stopPropagation(); });
    
	this.ul.children('li').click( function(event){ return instance.itemSelect(this); });
	$('body').click( function(){ instance.blur() });
}

function showPasswordReminder() {
	
	$('#loginForm').hide();
	$('#reminderForm').show();
}

function handleRequestPasswordReset(response) {
	
	if (!response.status) {
		$('#remider_error').html(response.msg);
		return false;
	}
		
	$('#reminder_done').show();
	$('#reminderForm').hide();	
}

function requestPasswordReset() {
	
	var data = $("#reminderForm").serialize();
	$.post('/ajax/passwordResetRequest',data, handleRequestPasswordReset ,'json');
}

function handleFilterChange(e, norefresh) {
	
	norefresh = (norefresh == undefined) ? false : norefresh;
	
	if (e.value == 'ceny') {
		
		$('#searchFields').children().hide();
		$('#priceFilterOptions').show();
		
	} else if (e.value == 'nowości' || e.value == 'zapowiedzi'){
		
		$('#searchFields').children().hide();
		$('#search_text')[0].value='';
		if (!norefresh) e.form.submit();
	} 	else {
		
		$('#searchFields').children().show();
		$('#priceFilterOptions').hide();
		if (!norefresh) e.form.submit();
	}	
	
	if (!$('#priceFilterOptions').attr("initialized")) {
		$('#priceFilterOptions').attr("initialized",  true);
		var ds1 = new dropdownSelect('priceFilterOptions');
		
		$('#priceFilterOptions').change( function() { $('#search_text')[0].value=''; this.form.submit();});
	}
}

function fireFilterChangeHandler() {
	
	handleFilterChange($('#filterButton')[0], true);
}

