﻿var OriginalSEO_URL = "";
var isSEO_URL_Valid = false;
var Proxy = new serviceProxy("/Services/SunlightTruthSiteService.asmx");
$(document).ready(onPageLoaded);

function onPageLoaded()
{
    if (document.location.hash == "#Print")
        window.print();

 $(".DateTextBox").datepicker({numberOfMonths: 3,showCurrentAtPos: 2,showButtonPanel: true});

 $('.txtSEO_URL').blur(function () { isSEO_URL_Available(); });

    if ($('#mode').length > 0)
    {
        OriginalSEO_URL = $('.txtSEO_URL').val();
        if (OriginalSEO_URL != '')
            isSEO_URL_Valid = true;
    }

   
}

function Submit_Clicked()
{

    if (Page_ClientValidate())
    {
        if ($('.txtSEO_URL').val() != '')
        {
            if ($('.hfApprovalRequired').val() == 'true')
            {
                alert("Thank you for your submission, administrators of SunlightTruth have been notified to review your article before it will be publicly posted. Please allow 48 hours from the time of posting before contacting an admin if you do not see your article on the site.");
            }
            return isSEO_URL_Valid;
        }
        else
        {
            alert("Please enter a Sunlight Truth URL!");
            $('#SEO_Required').show();
            return false;
        }
    }
    else
        return false;
}

function isSEO_URL_Available() {
    isSEO_URL_Valid = false;
    if ($('.txtSEO_URL').val() != "")
    {
        if (OriginalSEO_URL != $('.txtSEO_URL').val())
        {
            Proxy.invoke("isAvailableSEO_URL", { SEO_URL: $('.txtSEO_URL').val() }, onCompleteSEO_Check, onerror);

//             SunlightTruth.SunlightTruthSiteService.isAvailableSEO_URL($('.txtSEO_URL').val(), onCompleteSEO_Check, onError);
//           $.ajax({
//                url: "/Services/SunlightTruthSiteService.asmx/isAvailableSEO_URL",
//                data: "{SEO_URL:" + $('.txtSEO_URL').val()+ "}",
//                success: onCompleteSEO_Check, 
//                error: onError,
//                type: "POST",
//                contentType: "application/json; charset=utf-8",
//                dataType: "json"
// });
        }
        else {
            isSEO_URL_Valid = true;
        }
    }
    else
        $('#SEO_Required').show();
}

function onCompleteSEO_Check(result) {
    $('#SEO_Required').hide();
    isSEO_URL_Valid = result;
    if (isSEO_URL_Valid)
        $('#SEO_ULR_InUse').hide();
    else
        $('#SEO_ULR_InUse').show();
}

function onError(e) {
    alert(e);
}