var post_url = '/ajax_request.php';
var new_store_logo_box = '';

function distanceCalculatorSubmit() {
	//$("#distanceCalculatorButton").attr("disabled", "true");
	//$("#distanceCalculatorResult").html('');
	var request = $("#distanceCalculatorForm").serialize();
	$.post(post_url + '?module=SearchLocal&type=requestGetDistance', request, function(data) {
		distanceCalculatorResponse(data);
	});	
}

	 
function distanceCalculatorResponse(resp_data) {
	//$("#distanceCalculatorButton").attr("disabled", "false");
	$("#distanceCalculatorResult").html(resp_data);
	
}

function searchLocalSubmit() {
	//$("#searchLocalButton").attr("disabled", "true");
	//$("#distanceCalculatorResult").html('');
	var request = $("#searchLocalForm").serialize();
	$.post(post_url + '?module=SearchLocal&type=requestGetRadius', request, function(data) {
		searchLocalResponse(data);
	});	
}

function setNewPosition(lat, lng) {
    var request = 'lat=' + lat + '&lng=' + lng;
    $.post(post_url + '?com=MapAPI&type=saveNewPosition', request, function(data) {
        newPositionResponse(data);
    });
}

function newPositionResponse(resp_data) {
    $("#setPositionResult").html(resp_data);
}

	 
function searchLocalResponse(resp_data) {
	//$("#searchLocalButton").attr("disabled", "false");
	$("#searchLocalResult").html(resp_data);
	
}

function storeRegistrationSubmit() {
    var valid = false
    if ($("#storeRegistrationForm").valid()) {
        valid = true;
    } else {
        warnUser("Prosze poprawić dane w formularzu.");
    }

    return valid;

}

function couponSubmit() {
    var valid = false
    if ($("#couponForm").valid()) {
        valid = true;
    } else {
        warnUser("Prosze poprawić informacje w formularzu.");
    }

    return valid;

}

function userLoginSubmit() {
    var valid = false
    if ($("#userLoginForm").valid()) {
        valid = true;
    } else {
        warnUser("Prosze poprawić dane w formularzu.");
    }

    return valid;

}

function userLostPassSubmit() {
    var valid = false
    if ($("#userLostPassForm").valid()) {
        valid = true;
    } else {
        warnUser("Prosze poprawić dane w formularzu.");
    }

    return valid;

}

function mapRefreshSubmit() {
    org_address_street = $("#store_register_address_street").val();
	org_address_code = $("#store_register_address_code").val();
	org_address_city = $("#store_register_address_city").val();
	org_address_state = $("#store_register_address_state").val();
	$("#mapRefreshButton").attr("disabled", "true");
	
	var address = 'address=' + $("#store_register_address_street").val() + ' ' + $("#store_register_address_code").val() + ' ' + $("#store_register_address_city").val();
	$.post(post_url + '?com=MapAPI&type=requestGetMapURL', address, function(data) {
		mapRefreshResponse(data);
	});
}

function mapRefreshResponse(resp_data) {

    var JSONobject = $.json.decode(resp_data);
    if (JSONobject.error == 0) {
        infoUser(JSONobject.message);

        $("#google_map_url").attr({
            src: JSONobject.map_url
        });

        $("#store_register_address_latitude").val(JSONobject.latitude);
        $("#store_register_address_longitude").val(JSONobject.longitude);

    } else {
        warnUser(JSONobject.message);
    }



}

function couponVote(vote_type, coupon_id) {
    var request = 'type=' + vote_type + '&coupon_id=' + coupon_id;
    $.post(post_url + '?com=CouponVote', request, function(data) {
        couponVoteResponse(data);
    });
}


function couponVoteResponse(resp_data, data_type) {
    if (data_type == 'json_object') {
        JSONobject = resp_data;
    } else {
        JSONobject = $.json.decode(resp_data);
    }

    var id = JSONobject.coupon_id;

    if (JSONobject.error == 1) {
        warnUser(JSONobject.message);
    } else {
        var percentage = JSONobject.percentage > 0 ? JSONobject.percentage : 0 ;
        $("#coupon_vote_text_" + id).html(percentage + '%');
        $("#coupon_vote_count_" + id).html(JSONobject.total_votes);

        $("#coupon_vote_bg_" + id).addClass("coupon_vote_" + JSONobject.vote_color);
        $("#coupon_vote_text_" + id).addClass("coupon_vote_text" + JSONobject.vote_color);

        if (JSONobject.vote_color != 1 && $("#coupon_vote_bg_" + id).hasClass("coupon_vote_1")) {
            $("#coupon_vote_bg_" + id).removeClass("coupon_vote_1");
            $("#coupon_vote_text_" + id).removeClass("coupon_vote_text1");
        }
        if (JSONobject.vote_color != 2 && $("#coupon_vote_bg_" + id).hasClass("coupon_vote_2")) {
            $("#coupon_vote_bg_" + id).removeClass("coupon_vote_2");
            $("#coupon_vote_text_" + id).removeClass("coupon_vote_text2");
        }
        if (JSONobject.vote_color != 3 && $("#coupon_vote_bg_" + id).hasClass("coupon_vote_3")) {
            $("#coupon_vote_bg_" + id).removeClass("coupon_vote_3");
            $("#coupon_vote_text_" + id).removeClass("coupon_vote_text3");
        }
        if (JSONobject.vote_color != 4 && $("#coupon_vote_bg_" + id).hasClass("coupon_vote_4")) {
            $("#coupon_vote_bg_" + id).removeClass("coupon_vote_4");
            $("#coupon_vote_text_" + id).removeClass("coupon_vote_text4");
        }
    }
}

function couponVoteResponseWindow(resp_data, data_type) {
    if (data_type == 'json_object') {
        JSONobject = resp_data;
    } else {
        JSONobject = $.json.decode(resp_data);
    }

    var id = JSONobject.coupon_id;

    if (JSONobject.error == 1) {
        warnUser(JSONobject.message);
    } else {
        var percentage = JSONobject.percentage > 0 ? JSONobject.percentage : 0 ;
        window.opener.$("#coupon_vote_text_" + id).html(percentage + '%');
        window.opener.$("#coupon_vote_count_" + id).html(JSONobject.total_votes);

        window.opener.$("#coupon_vote_bg_" + id).addClass("coupon_vote_" + JSONobject.vote_color);
        window.opener.$("#coupon_vote_text_" + id).addClass("coupon_vote_text" + JSONobject.vote_color);

        if (JSONobject.vote_color != 1 && window.opener.$("#coupon_vote_bg_" + id).hasClass("coupon_vote_1")) {
            window.opener.$("#coupon_vote_bg_" + id).removeClass("coupon_vote_1");
            window.opener.$("#coupon_vote_text_" + id).removeClass("coupon_vote_text1");
        }
        if (JSONobject.vote_color != 2 && window.opener.$("#coupon_vote_bg_" + id).hasClass("coupon_vote_2")) {
            window.opener.$("#coupon_vote_bg_" + id).removeClass("coupon_vote_2");
            window.opener.$("#coupon_vote_text_" + id).removeClass("coupon_vote_text2");
        }
        if (JSONobject.vote_color != 3 && window.opener.$("#coupon_vote_bg_" + id).hasClass("coupon_vote_3")) {
            window.opener.$("#coupon_vote_bg_" + id).removeClass("coupon_vote_3");
            window.opener.$("#coupon_vote_text_" + id).removeClass("coupon_vote_text3");
        }
        if (JSONobject.vote_color != 4 && window.opener.$("#coupon_vote_bg_" + id).hasClass("coupon_vote_4")) {
            window.opener.$("#coupon_vote_bg_" + id).removeClass("coupon_vote_4");
            window.opener.$("#coupon_vote_text_" + id).removeClass("coupon_vote_text4");
        }
    }
}

function sendCoupon() {
    var request = $("#coupon_send").serialize();
    $.post(post_url + '?com=CouponSend', request, function(data) {
            sendCouponResponse(data);
    });
}

function sendFeedback() {
    var request = $("#feedback_send").serialize();
    $.post(post_url + '?com=FeedbackSend', request, function(data) {
            sendFeedbackResponse(data);
    });
}

function sendCouponResponse(resp_data) {
    var JSONobject = $.json.decode(resp_data);

    if (JSONobject.error == 1) {
        warnUser(JSONobject.message);
    } else {
        $("#coupon_send_form").html(JSONobject.message);
        setTimeout('window.close()', 3000);
    }
}

function sendFeedbackResponse(resp_data) {
    var JSONobject = $.json.decode(resp_data);

    if (JSONobject.error == 1) {
        warnUser(JSONobject.message);
    } else {
        couponVoteResponseWindow(JSONobject, 'json_object');
        $("#feedback_send_form").html(JSONobject.message);
        setTimeout('window.close()', 3000);
    }
}

function localSearch() {
    var zip_code = $("#search_zip_code").val();
    var distance = $("#search_distance").val();
    var category = $("#search_category").val();

    var url = '/SzukajLokalnie/' + category + '/' + zip_code + '/' + distance;
    window.location.href = url;
}

function netSearch() {
    var category = $("#search_net_category").val();

    var url = '/SzukajInternet/' + category;
    window.location.href = url;
}

function storeSearch() {
    var store = $("#search_store_select").val();

    var url = '/Rabaty/Sklep/' + store;
    window.location.href = url;
}

function barcodePreview() {
    var code = $("#coupon_add_barcode").val();
    if (code.length < 12) {
        warnUser('Kod kreskowy EAN-13 musi zawierać 12 cyfr');
    } else {
        var request = 'code=' + code;
        $.post(post_url + '?com=CouponBarcode', request, barcodePreviewResponse);
    }
}

function barcodePreviewResponse(resp_data) {
    var JSONobject = $.json.decode(resp_data);

    if (JSONobject.error == 1) {
        warnUser(JSONobject.message);
    } else {
        $("#barcode_box").html(JSONobject.image);
    }
}


function getNewStoreLogo() {
    $.post(post_url + '?com=StoreLogoBox', '', hideOldStoreLogo);
}

function hideOldStoreLogo(resp_data) {
    new_store_logo_box = resp_data;
    $("#site_logo_box").hide('slow', showNewStoreLogo);
}

function showNewStoreLogo() {
    $("#site_logo_box").html(new_store_logo_box);
    $("#site_logo_box").show('slow');
}
