﻿$(document).ready(function() {

    //Set opacity on each span to 0%
    $(".caseimg").css({ 'opacity': '0.6' });

    $('.caseimg').hover(
		function() {
		    $(this).fadeTo("slow", 1.0);
		},
		function() {
		    $(this).fadeTo("slow", 0.6);
		}
	)

    $("#msg").keyup(function() {
        var box = $(this).val();
        var smscount = Math.ceil(box.length / 160);

        $("#charscount").val((160 * smscount) - box.length);
        $("#smscount").val(smscount);
    });

    $("#keyword").keyup(function() {
        membercount();
    });

    $("#login").keyup(function() {
        membercount();
    })

    function membercount() {

        if ($("#keyword").val() != "" && $("#login").val() != "") {
            $.ajax({
                url: "http://login.quickinfo.dk/main/num_membs",
                data: "keyword=" + $("#keyword").val() + "&title=" + $("#login").val(),
                cache: false,
                dataType: "html",
                success: function(html) {
                    $("#membercounter").html("");
                    if (html != "0" && html != "Bad Request") {
                        $("#membercounter").val(html);
                    }
                    else {
                        $("#membercounter").val("0");
                    }


                },
                error: function(xml, error) { alert(error); }
            });
        }
    }

    $("#qc2_keyword").keyup(function() {
        qc2_membercount();
    });

    $("#qc2_login").keyup(function() {
        qc2_membercount();
    })
    $("#qc2_password").keyup(function() {
        qc2_membercount();
    })

    function qc2_membercount() {

        if ($("#qc2_keyword").val() != "" && $("#qc2_login").val() != "" && $("#qc2_password").val() != "") {
            $.ajax({
                url: "http://admin.quickinfo.dk/ajax/quickclub.php",
                data: { username: "user" },
                data: "username=" + $("#qc2_keyword").val() + "&clubname=" + $("#qc2_login").val() + "&password=" + $("#qc2_password").val(),
                cache: "false",
                async: "false",
                type: "POST",
                dataType: "json",
                success: function(data) {
                    if (data.status == "1") {
                        $("#qc2_membercounter").val(data.members);
                        $("#qc2_submitter").show();
                        $("#solutionid").val(data.solutionid);
                    }
                    if (data.status == "0") {
                        $("#qc2_submitter").hide();
                        $("#solutionid").val("");
                    }

                },
                error: function(data) { alert(data); }
            });
        }
    }

    $("#qc2_submitter").click(function() {
        var totalmsg = ($("#qc2_membercounter").val() * $("#smscount").val());
        if (confirm("Tryk OK for at sende " + totalmsg + " beskeder til kundeklubben.")) {
            $.ajax({
                url: "http://admin.quickinfo.dk/ajax/ajax.php",
                data: { type: "quickclubsend", post_username: $("#qc2_keyword").val(), solutionid: $("#solutionid").val(), post_password: $("#qc2_password").val(), message: $("#msg").val() },
                //                data: "type=quickclubsend&post_username=" + $("#qc2_keyword").val() + "&solutionid=" + $("#solutionid").val() + "&post_password=" + $("#qc2_password").val() + "&message=" + $("#msg").val(),
                cache: "false",
                async: "false",
                type: "POST",
                dataType: "json",
                success: function(data) {
                    if (data.status == "ok") {
                        alert("Beskeden blev sendt. Din konto har nu " + data.credits + " beskeder tilbage");
                        $("#msg").val($("#origmsg").val());
                        $("#qc2_password").val("");
                        $("#qc2_submitter").hide();
                    }
                },
                error: function(data) { alert(data); }
            });
        }
        return false;
    });

});


