$(function() {

    function setEV(value) {
        // define new url
        ID_CT_VALUE_32145632    = ID_CT_VALUE_32145632.substr(0, 9) + value;
        var $d      = new Date();
        var $url    = 'http://evisitcs2.com/tag/tagkd.php?'
                    + 'rq='     + encodeURIComponent(location.href) + '&'
                    + 'dc='                                         + '&'
                    + 'userid=' + ID_uid_15243615                   + '&'
                    + 'tt='     + ID_tt_15243615                    + '&'
                    + 'ctid='   + ID_CT_ID_32145632                 + '&'                          
                    + 'ctval='  + ID_CT_VALUE_32145632              + '&' 
                    + 'tc='     + (($d.getTime() - $d.getMilliseconds() ) / 1000 );
        $("#ev").html("<img width=\"1\" height=\"1\" border=\"0\" src=\"" + $url + "\" />");
    }

    $.clipboardReady(function(){
        $("#copyclip").show();

        $("#copyclip").click(function() {
            $.clipboard($("#tweakdomain").val());
            return false;
        });
    }, { 
        swfpath: "http://www.dot.tk/images.v2/jquery.clipboard.swf", 
        debug: false 
    });


    $("#long_url").one("focus", function() {
        $(this).val("");
    });


    $('#long_url').keyup(function(e) {
        if(e.keyCode == 13) {
            $("#tweak").trigger('click');
        }
    });

    // for emailing the password
    $("#emailpassword").click(function() {
        var $v  = $("#email").val();
        if ($v == '') {
            return;
        }

        // otherwise try to rename this.
        $.ajax({
            url:        'http://my.dot.tk/tweak/sendpasswd',
            type:       'GET',
            data:       {
               email:   $v
            },
            cache:      false,
            dataType:   'json',
            timeout:    18000,
            success:    function(data) {
                if (data.success == 1) {
                    $("#passwordsent").show();
                    setEV("PASSWDSENTSUCCESS");
                } else {
                    setEV("PASSWDSENTERROR");
                }
            }
        });
    });



    $("#tweak").click(function() {
        $(this).attr("disabled", "disabled");
        $("#tweakerr").hide();
        $("#tweakerr_already_tweaked").hide();

        $v  = $("#long_url").val();
        if ($v == '' || $v == 'http://') {
            $(this).removeAttr("disabled");
            return;
        }

        // otherwise try to rename this.
        $.ajax({
            url:        'http://my.dot.tk/tweak/shorten',
            type:       'GET',
            data:       {
               long_url:    $v
            },
            cache:      false,
            dataType:   'json',
            timeout:    18000,
            success:    function(data) {
                if (data.success == 1) {
                    $("#tweakintro").hide();
                    $("#tweakinstructions").hide();
                    $("#tweaktechnical").hide();
                    $("#tweakdomain").val(data.domain);
                    $("#tweakeddiv").show();
                    $("#addtoaccountdiv").show();

                    if ($("#tweetit:checked").val() == "yes") {
                        $("#twmsg").val(data.domain);
                        $("#twitterthisdiv").show();
                    }

                    setEV("SHORTENSUCCESS");
                } else {
                    if (data.problem == 'already_tweaked') {
                        $("#tweakerr_already_tweaked").show();
                    } else {
                        $("#tweakerr").show();
                    }
                    $("#tweak").removeAttr("disabled");
                    setEV("SHORTENERROR");
                }
            }
        });
    });

    $("#tweet").click(function() {
        $(this).attr("disabled", "disabled");
        $("#twitterposted_err").hide();
        $("#twitterposted_ok").hide();
        $("#twitterposting").show();

        // get several values
        var $twuser = $("#twname").val();
        var $twpass = $("#twpass").val();
        var $twmsg  = $("#twmsg").val();

        // if no message..
        if ($twmsg == '') {
            $(this).removeAttr("disabled");
            return;
        }

        // otherwise try to rename this.
        $.ajax({
            url:        'http://my.dot.tk/tweak/posttweet',
            type:       'GET',
            data:       {
                twuser:     $twuser,
                twpass:     $twpass,
                twmsg:      $twmsg
            },
            cache:      false,
            dataType:   'json',
            timeout:    18000,
            success:    function(data) {
                $("#twitterposting").hide();
                $("#tweet").removeAttr("disabled");
                if (data.success == 1) {
                    $("#twitterposted_err").hide();
                    $("#twitterposted_ok").show();
                    setEV("TWEETSUCCESS");
                } else {
                    $("#twitterposted_ok").hide();
                    $("#twitterposted_err").show();
                    setEV("TWEETERROR");
                }
            }
        });
    });

    $("#email").click(function() {
        $("#email").removeAttr("readonly");
    });

    $("#email").change(function() { 
        $("#testemail").removeAttr("disabled");
    });



    $("#testemail").click(function() {
        $(this).attr("disabled", "disabled");
        $v  = $("#email").val();
        $("#email").attr("readonly", "readonly");
    
        if ($v == '') {
            $(this).removeAttr("disabled");
            $("#email").removeAttr("readonly");
            return;
        }

        // otherwise try to rename this.
        $.ajax({
            url:        'http://my.dot.tk/tweak/testemail',
            type:       'GET',
            data:       {
               email:    $v
            },
            cache:      false,
            dataType:   'json',
            timeout:    18000,
            success:    function(data) {
                if (data.success == 1) {
                    $("#emailtest_err").hide();
                    $("#whenemailentered").show();
                    if (data.email_status == 'not_known') {
                        $("#registrantknown").hide();
                        $("#forgotpassspan").hide();
                        $("#registrantnotknown").show();
                        $("#pass2span").show(); 
                        setEV("USERNOTKNOWN");
                    } else if (data.email_status == 'known') {
                        $("#registrantnotknown").hide();
                        $("#pass2span").hide(); 
                        $("#forgotpassspan").show();
                        $("#registrantknown").show();
                        setEV("USERKNOWN");
                    }
                } else {
                    // input sucks
                    $("#emailtest_err").show();
                    $("#testemail").removeAttr("disabled");
                }
            }
        });
    });


    $("#addtoaccount").click(function() {
       // $(this).attr("disabled", "disabled");
        var $email  = $("#email").val();
        var $pass1  = $("#pass1").val();
        var $pass2  = $("#pass2").val();
        var $twuser = $("#twname").val();
        var $twpass = $("#twpass").val();

        if ($email == '' || $pass1 == '') {
            $(this).removeAttr("disabled");
            return;
        }

        // otherwise try to rename this.
        $.ajax({
            url:        'http://my.dot.tk/tweak/addtoaccount',
            type:       'POST',
            data:       {
                email:  $email,
                pass1:  $pass1,
                pass2:  $pass2,
                twuser: $twuser,
                twpass: $twpass
            },
            cache:      false,
            dataType:   'json',
            timeout:    18000,
            success:    function(data) {
                $("#addtoaccount_ok").hide(); 
                $("#addtoaccount_ok_new").hide();
                $("#addtoaccount_err_invalidpass").hide(); 
                $("#addtoaccount_err_passproblem").hide();
                $("#addtoaccount_err_other").hide();

                if (data.success == 1) {
                    if (data.problem == 'addtoaccount_ok') {
                        $("#addtoaccount_ok").show(); 
                    } else if (data.problem == 'addtoaccount_ok_new') {
                        $("#addtoaccount_ok_new").show();
                    }
                    setEV("ADDACCOUNTSUCCESS");
                } else {
                    if (data.problem == 'addtoaccount_err_invalidpass') {
                        $("#addtoaccount_err_invalidpass").show(); 
                    } else if (data.problem == 'addtoaccount_err_passproblem') {
                        $("#addtoaccount_err_passproblem").show();
                    } else if (data.problem == 'addtoaccount_err_other') {
                        $("#addtoaccount_err_other").show();
                    }
                    setEV("ADDACCOUNTERROR");
                }
            }
        });
    });
});
