﻿
var UpdateContestAdsBoxControl = {
    adgroupid: 0,
    contestupdate: false,
    champheadline: '',
    champline1: '',
    champline2: '',
    champdisplayurl: '',
    champdestinationurl: '',
    challheadline: '',
    challline1: '',
    challline2: '',
    challdisplayurl: '',
    challdestinationurl: '',

    init: function() { },

    getAdBlock: function(displayurl, headline, line1, line2, trialadwordstatus) {
        var sb = "";
        sb += "<div class='adblock' style='width: 280px;'> ";
        sb += "<a class='main' href='";
        sb += displayurl + "'>";
        sb += headline + "</a> ";
        sb += "<p>";
        sb += line1 + "<br />" + line2 + "</p> ";
        sb += "<a class='site' href='";
        sb += displayurl + "'>";
        sb += displayurl + "</a> ";
        sb += "</div>";
        return sb;
    },

    updateContest: function() {

        if (this.contestupdate == false) {
            //spell checking ===================
            //Since spellchecker is designed to check 1 field at a time, multiple field spellcheck is done
            //using the callback function of the previous check
            myChecker = new SpellChecker();
            tb_remove();
            myChecker.checkSpelling($('#editchampheadline'), function(status) {
                if (status)
                    myChecker.checkSpelling($('#editchampline1'), function(status) {
                        if (status)
                            myChecker.checkSpelling($('#editchampline2'), function(status) {
                                if (status)
                                    myChecker.checkSpelling($('#editchallheadline'), function(status) {
                                        if (status)
                                            myChecker.checkSpelling($('#editchallline1'), function(status) {
                                                if (status)
                                                    myChecker.checkSpelling($('#editchallline2'), function(status) {
                                                        if (status) {
                                                            $("#btnEdit").click();
                                                            $('#UpdateBoxAjax').removeClass("nodisplay");
                                                            $('#dvEditAds').addClass("ajaxContainer");
                                                            $("#btnUpdateContest").removeClass("contestgreenbutton");
                                                            $("#btnUpdateContest").addClass("contestgraybutton");
                                                            this.contestupdate = true;

                                                            this.champheadline = $('#editchampheadline').val();
                                                            this.champline1 = $('#editchampline1').val();
                                                            this.champline2 = $('#editchampline2').val();
                                                            this.champdisplayurl = $('#editchampurldisplay').val();
                                                            this.champdestinationurl = $('#editchampurldestination').val();
                                                            this.challheadline = $('#editchallheadline').val();
                                                            this.challline1 = $('#editchallline1').val();
                                                            this.challline2 = $('#editchallline2').val();
                                                            this.challdisplayurl = $('#editchallurldisplay').val();
                                                            this.challdestinationurl = $('#editchallurldestination').val();
                                                            this.adgroupid = $('#hdnAdGroupID').val();

                                                            //This is the code that saves the ad
                                                            $.post("/Advertiser/UpdateContest",
                                                                   {
                                                                       AdGroupID: this.adgroupid,
                                                                       ChampionHeadline: this.champheadline,
                                                                       ChampionLine1: this.champline1,
                                                                       ChampionLine2: this.champline2,
                                                                       ChampionDisplayURL: this.champdisplayurl,
                                                                       ChampionDestinationURL: this.champdestinationurl,
                                                                       ChallengerHeadline: this.challheadline,
                                                                       ChallengerLine1: this.challline1,
                                                                       ChallengerLine2: this.challline2,
                                                                       ChallengerDisplayURL: this.challdisplayurl,
                                                                       ChallengerDestinationURL: this.challdestinationurl
                                                                   },
                                                                   function(data) { UpdateContestAdsBoxControl.updateContest_callback(data); }, "json");
                                                        }
                                                    })
                                            })
                                    })
                            })
                    })
            })
        }
    },

    updateContest_callback: function(response) {
        $("#btnUpdateContest").removeClass("contestgraybutton");
        $("#btnUpdateContest").addClass("contestgreenbutton");
        this.contestupdate = false;

        if (response.ErrorList.length > 0) {
            $("#lblUpdateErrorMsg").attr("innerHTML", response.ErrorList[0].ErrorMessage);
            $('#updateerrorsummary').removeClass("nodisplay");
            setTimeout("$('#updateerrorsummary').addClass('nodisplay');", 5000);
            $('#UpdateBoxAjax').addClass("nodisplay");
            $('#dvEditAds').removeClass("ajaxContainer");
            return;
        }
        else if (response.IsRedirect) {
            window.location.href = response.Redirect;
            alert('Contest updated successfully');
        }

        $('#updateerrorsummary').addClass('nodisplay');
        $('#UpdateBoxAjax').addClass("nodisplay");
        $('#dvEditAds').removeClass("ajaxContainer");
        tb_remove();
    }
}


var AnswerBoxControl = {
    init: function() { },
    postId: 0,
    answerResponse: null,

    setCurrentPost: function(postid) {
        this.postId = postid;
    },

    reset: function(resp) {
        $("#txtAnswerMessage").attr('value', 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.');
        this.answerResponse = resp;
        this.postId = 0;
        tb_remove();
        $('#AnswerAdAjax').addClass("nodisplay");       //the ajax box
        $('#dvAnswerAdd').removeClass("ajaxContainer"); //the container you want to be gray
    },

    answerQuestion: function() {
        $('#AnswerAdAjax').removeClass("nodisplay"); //the ajax box
        $('#dvAnswerAdd').addClass("ajaxContainer"); //the container you want to be gray
        $.post("/Advertiser/AnswerQuestion",
                {
                    PostID: this.postId,
                    Message: $("#txtAnswerMessage").val()
                },
                function(data) { AnswerBoxControl.answerQuestion_callback(data); }, "json");
    },

    answerQuestion_callback: function(resp) {
        this.reset(resp);
    }
}

var AdChallengeBatchControl = {
    adChallengeIdList: '',
    adChallengeBatchResponse: '',

    init: function() {
        $('#chkSelectorAll').click(this.checkAll);
    },

    checkAll: function() {
        var checked_status = this.checked;
        $("input[type='checkbox'][name='chkSelector']").click();
        $("input[type='checkbox'][name='chkSelector']").attr('checked', checked_status);
        AdChallengeBatchControl.adChallengeIdList = '';
    },

    checkItem: function(checked, selectid) {
        if (checked) {
            AdChallengeBatchControl.addChallenge(selectid);
        }
        else {
            AdChallengeBatchControl.removeChallenge(selectid);
        }
    },

    addChallenge: function(challengeid) {
        if (this.adChallengeIdList.search(challengeid) == -1) {
            this.adChallengeIdList += challengeid + ",";
        }
    },

    removeChallenge: function(challengeid) {
        if (this.adChallengeIdList.search(challengeid) == -1) { return; } //doesn't exist return
        if (this.adChallengeIdList.search(challengeid + ",") == -1) {
            this.adChallengeIdList = this.adChallengeIdList.replace(challengeid, "");
        }
        else {
            this.adChallengeIdList = this.adChallengeIdList.replace(challengeid + ",", "");
        }
    },

    getAdChallengeList: function(adchallengestatus, isreject, isapprove) {
        $('#AdChallengeBatchAjax').removeClass("nodisplay");  //the ajax box
        $('#AdChallengeBatchBox').addClass("ajaxContainer");    //the container you want to be gray
        $.get("/PartialView/AdChallengeBatch",
               {
                   AdChallengeStatus: adchallengestatus,
                   IsReject: isreject,
                   IsApprove: isapprove
               },
               function(data) { AdChallengeBatchControl.getAdChallengeList_callback(data); });
    },

    getAdChallengeList_callback: function(resp) {
        this.reset(resp);
    },

    approveAdList: function() {
        $('#AdChallengeBatchAjax').removeClass("nodisplay");  //the ajax box
        $('#AdChallengeBatchBox').addClass("ajaxContainer");    //the container you want to be gray
        $.post("/PartialView/ApproveAdChallengeList",
               {
                   AdChallengeIdList: this.adChallengeIdList
               },
               function(data) { AdChallengeBatchControl.approveAdList_callback(data); }, "json");
    },

    approveAdList_callback: function(resp) {
        this.reset(resp);
    },

    rejectAdList: function() {
        if (this.adChallengeIdList == '') {
            alert('No items selected.');
            return;
        }
        $('#AdChallengeBatchAjax').removeClass("nodisplay");  //the ajax box
        $('#AdChallengeBatchBox').addClass("ajaxContainer");    //the container you want to be gray

        if ($("#txtBatchReasonDescription").val() == "Your reason for rejection will be visible to future writers so please provide detailed feedback")
            $("#txtBatchReasonDescription").val("");
        else
            $("#txtBatchReasonDescription").val($("#txtBatchReasonDescription").val());

        $.post("/PartialView/RejectAdChallengeList",
                {
                    AdChallengeIdList: this.adChallengeIdList,
                    Reason: $('input[name=batchreason]:checked').val(),
                    ReasonDescription: $("#txtBatchReasonDescription").val()
                },
                function(data) { AdChallengeBatchControl.rejectAd_callback(data); }, "json");
    },

    rejectAdList_callback: function(resp) {
        this.reset(resp);
    },

    reset: function(resp) {
        $("#txtBatchReasonDescription").attr('value', 'Your reason for rejection will be visible to future writers so please provide detailed feedback');
        this.adChallengeIdList = '';
        tb_remove();
        $('#AdChallengeBatchAjax').addClass("nodisplay");       //the ajax box
        $('#AdChallengeBatchBox').removeClass("ajaxContainer");   //the container you want to be gray
    },
    
    resetReason: function() {
        if ($("#txtBatchReasonDescription").attr('value') == "")
            $("#txtBatchReasonDescription").attr('value', "");
    }    
}

var RejectAdBoxControl = {
    init: function() { },
    adChallengeId: 0,
    adTrialId: 0,
    rejectResponse: null,

    setCurrentChallenge: function(adchallengeid) {
        this.adChallengeId = adchallengeid;
    },

    setCurrentTrial: function(adtrialid) {
        this.adTrialId = adtrialid;
    },

    reset: function(resp) {
        $("#txtReasonDescription").attr('value', 'Your reason for rejection will be visible to future writers so please provide detailed feedback');
        this.rejectResponse = resp;
        this.adChallengeId = 0;
        tb_remove();
        $('#RejectAdAjax').addClass("nodisplay");       //the ajax box
        $('#dvRejectAdd').removeClass("ajaxContainer"); //the container you want to be gray
    },

    rejectAd: function() {
        $('#RejectAdAjax').removeClass("nodisplay"); //the ajax box
        $('#dvRejectAdd').addClass("ajaxContainer"); //the container you want to be gray
        
        if($("#txtReasonDescription").val() == "Your reason for rejection will be visible to future writers so please provide detailed feedback")
            $("#txtReasonDescription").val("");
        else
            $("#txtReasonDescription").val($("#txtReasonDescription").val());

        $.post("/Advertiser/RejectAd",
                {
                    AdTrialId: this.adTrialId,
                    AdChallengeId: this.adChallengeId,
                    Reason: $('input[name=reason]:checked').val(),
                    ReasonDescription: $("#txtReasonDescription").val()
                },
                function(data) { RejectAdBoxControl.rejectAd_callback(data); }, "json");
    },

    rejectAd_callback: function(resp) {
        this.reset(resp);
    },
    
    onclickReasonDescription: function() {
       if($("#txtReasonDescription").attr('value') == "Your reason for rejection will be visible to future writers so please provide detailed feedback")
            $("#txtReasonDescription").attr('value',"");
    },
    
    resetReason: function() {
       if($("#txtReasonDescription").attr('value') == "")
            $("#txtReasonDescription").attr('value',"Your reason for rejection will be visible to future writers so please provide detailed feedback");
    }    
}

var AddToQueueBoxControl = {
    init: function() { },
    adChallengeIds: '',
    addToQueueResponse: null,

    setCurrentChallenge: function(adchallengeid) {
        this.adChallengeIds = adchallengeid;
    },

    addChallenge: function(adchallengeid) {
        if (this.adChallengeIds.search(adchallengeid) == -1) {
            this.adChallengeIds += adchallengeid + ",";
        }
    },

    removeChallenge: function(adchallengeid) {
        if (this.adChallengeIds.search(adchallengeid) == -1) { return; } //doesn't exist return
        if (this.adChallengeIds.search(adchallengeid + ",") == -1) {
            this.adChallengeIds = this.adChallengeIds.replace(adchallengeid, "");
        }
        else {
            this.adChallengeIds = this.adChallengeIds.replace(adchallengeid + ",", "");
        }
    },

    reset: function(resp) {
        this.addToQueueResponse = resp;
        this.adChallengeIds = '';
        tb_remove();
        $('#AddToQueueAjax').addClass("nodisplay");
        $('#dvAddToQueue').removeClass("ajaxContainer");
    },

    addToQueue: function() {
        if (this.adChallengeIds == '') {
            alert('No items selected.');
            return;
        }
        $('#AddToQueueAjax').removeClass("nodisplay"); //the ajax box
        $('#dvAddToQueue').addClass("ajaxContainer"); //the container you want to be gray        
        $.post("/Advertiser/AddToQueue",
                {
                    AdChallengeIdList: this.adChallengeIds
                },
                function(data) { AddToQueueBoxControl.addToQueue_callback(data); }, "json");
    },

    addToQueue_callback: function(resp) {
        this.reset(resp);
    }
}

var ActivateContestBoxControl = {
    init: function() { },
    adGroupIds: '',
    groupResponse: null,

    setCurrentContest: function(adgroupid) {
        this.adGroupIds = adgroupid;
    },

    addContest: function(adgroupid) {
        if (this.adGroupIds.search(adgroupid) == -1) {
            this.adGroupIds += adgroupid + ",";
        }
    },

    removeContest: function(adgroupid) {
        if (this.adGroupIds.search(adgroupid) == -1) { return; } //doesn't exist return
        if (this.adGroupIds.search(adgroupid + ",") == -1) {
            this.adGroupIds = this.adGroupIds.replace(adgroupid, "");
        }
        else {
            this.adGroupIds = this.adGroupIds.replace(adgroupid + ",", "");
        }
    },

    reset: function(resp) {
        this.groupResponse = resp;
        this.adGroupIds = '';
        tb_remove();
        $('#ActivateAjax').addClass("nodisplay");
        $('#dvActivate').removeClass("ajaxContainer");        
    },
    
    activate: function() {
        if (this.adGroupIds == '') {
            alert('No items selected.');
            return;
        }
        $('#ActivateAjax').removeClass("nodisplay"); //the ajax box
        $('#dvActivate').addClass("ajaxContainer"); //the container you want to be gray        
        $.post("/Advertiser/ActivateContest",
                {
                    AdGroupIdList: this.adGroupIds
                },
                function(data) { ActivateContestBoxControl.activate_callback(data); }, "json");
    },

    activate_callback: function(resp) {
        this.reset(resp);
    }
}

var RemoveAdBoxControl = {
    init: function() { },
    adTrialId: 0,
    trialResponse: null,

    setCurrentTrial: function(adtrialid) {
        this.adTrialId = adtrialid;
    },

    reset: function(resp) {
        this.trialResponse = resp;
        this.adTrialId = 0;
        tb_remove();
        $('#RemoveAdAjax').addClass("nodisplay");
        $('#dvRemoveAdd').removeClass("ajaxContainer");        
        $('.ajaxContainer').removeClass("ajaxContainer");
        $('.ajaxMainProcessing').addClass("nodisplay");
    },

    removeAd: function() {
        if (this.adTrialId == 0) {
            alert('No item selected.');
            return;
        }
        $('#RemoveAdAjax').removeClass("nodisplay"); //the ajax box
        $('#dvRemoveAdd').addClass("ajaxContainer"); //the container you want to be gray
        $.post("/Advertiser/RemoveAd",
                {
                    AdTrialId: this.adTrialId
                },
                function(data) { RemoveAdBoxControl.removeAd_callback(data); }, "json");
    },

    removeAd_callback: function(resp) {
        this.reset(resp);
    }
}

ApproveAdBoxControl = {
     init: function() { },
     adChallengeId: 0,
     challengeResponse: null,

     setCurrentChallenge: function(adchallengeid) {
        this.adChallengeId = adchallengeid;
     },

     reset: function(resp) {
        this.challengeResponse = resp;
        this.adChallengeId = 0;
        tb_remove();
        $('#ApproveAdAjax').addClass("nodisplay");
        $('#dvApproveAdd').removeClass("ajaxContainer");
        $('.ajaxContainer').removeClass("ajaxContainer");
        $('.ajaxMainProcessing').addClass("nodisplay");
     },

     approveAd: function() {
        if (this.adChallengeId == 0) {
             alert('No item selected.');
             return;
        }
        $('#ApproveAdAjax').removeClass("nodisplay"); //the ajax box
        $('#dvApproveAdd').addClass("ajaxContainer"); //the container you want to be gray        
        $.post("/Advertiser/ApproveChallenger",
               {
                   AdChallengeId: this.adChallengeId
               },
               function(data) { ApproveAdBoxControl.approveAd_callback(data); }, "json");
     },

     approveAd_callback: function(resp) {
        this.reset(resp);
     }
 }

ResubmitAdBoxControl = {
     init: function() { },
     adTrialId: 0,
     adChallengeId: 0,
     challengeResponse: null,

     setCurrentChallenge: function(adtrialid, adchallengeid) {
        this.adTrialId = adtrialid;
        this.adChallengeId = adchallengeid;
     },

     reset: function(resp) {
        this.challengeResponse = resp;
        this.adTrialId = 0;
        this.adChallengeId = 0;
        tb_remove();
        $('#ReSubmitAdAjax').addClass("nodisplay");
        $('#dvReSubmitAdd').removeClass("ajaxContainer");
        $('.ajaxContainer').removeClass("ajaxContainer");
        $('.ajaxMainProcessing').addClass("nodisplay");     
     },

     resubmitAd: function() {
        if (this.adChallengeId == 0) {
             alert('No item selected.');
             return;
        }
        $('#ReSubmitAdAjax').removeClass("nodisplay"); //the ajax box
        $('#dvReSubmitAdd').addClass("ajaxContainer"); //the container you want to be gray
        $.post("/Advertiser/ResubmitChallenge",
               {
                   AdChallengeId: this.adChallengeId
               },
               function(data) { ResubmitAdBoxControl.resubmitAd_callback(data); }, "json");
     },

     resubmitAd_callback: function(resp) {
        this.reset(resp);
     }
 }

var DeleteAdBoxControl = {
    init: function() { },
    adChallengeId: 0,
    adTrialId: 0,
    adId:0,
    deletetype:'',
    rejectResponse: null,

    setCurrentChallenge: function(adtrialid,adchallengeid,adid,deltype) {
        $("#txtDeleteReason").attr('value', 'Please enter additional details. These will be viewable by future writers.');
        this.adChallengeId = 0;
        this.adTrialId = 0;
        this.adId = 0;
        this.deletetype = '';
    
        this.adChallengeId = adchallengeid;
        this.adTrialId = adtrialid;
        this.adId = adid;
        this.deletetype = deltype;

        $("#titleDeleteType").text(deltype);
        if(deltype == "Champion") {
            $("#titleMessage").text("Deleting the Champion ad will result in the Challenger ad winning this challenge. You will have to pay the prize associated with this Challenge.");
            $("#alertnote").removeClass("nodisplay");
        }
        else if(deltype == "Challenger"){
            $("#titleMessage").text("Deleting the Challenger ad will result in the Champion ad winning this challenge.");
            $("#alertnote").addClass("nodisplay");
        }
    },

    setCurrentTrial: function(adtrialid) {
        this.adTrialId = adtrialid;
    },

    reset: function(resp) {
        if(resp == "Sucessfull")
        {
            $("#txtDeleteReason").attr('value', 'Please enter additional details. These will be viewable by future writers.');
            this.rejectResponse = resp;
            this.adChallengeId = 0;
            this.adTrialId = 0;
            this.adId = 0;
            this.deletetype = '';
            tb_remove();
            window.location.reload();
        }
        else
        {
            $("#errMessage").attr("innerHTML","Error: " + resp + "<br/>");
            $("#errMessage").removeClass("nodisplay");
            setTimeout("$('#errMessage').addClass('nodisplay');",7000);
        }
        $('#DeleteAdAjax').addClass("nodisplay");       //the ajax box
        $('#dvDeleteAd').removeClass("ajaxContainer"); //the container you want to be gray
    },

    deleteAd: function() {
        $('#DeleteAdAjax').removeClass("nodisplay"); //the ajax box
        $('#dvDeleteAd').addClass("ajaxContainer"); //the container you want to be gray

        if($("#txtDeleteReason").val() == "Please enter additional details. These will be viewable by future writers.")
            $("#txtDeleteReason").val("");
        else
            $("#txtDeleteReason").val(", " + $("#txtDeleteReason").val());

        $.post("/Advertiser/DeleteAd",
                {
                    AdTrialId: this.adTrialId,
                    AdChallengeId: this.adChallengeId,
                    AdId: this.adId,
                    DeleteType: this.deletetype,
                    Reason: $('input[name=reason]:checked').val(),
                    ReasonDescription: $("#txtDeleteReason").val()
                },
                function(data) { DeleteAdBoxControl.deleteAd_callback(data); }, "json");
    },

    deleteAd_callback: function(resp) {
        this.reset(resp);
    },
    
    onclickReasonDescription: function() {
       if($("#txtDeleteReason").attr('value') == "Please enter additional details. These will be viewable by future writers.")
            $("#txtDeleteReason").attr('value',"");
    },
    
    resetReason: function() {
       if($("#txtDeleteReason").attr('value') == "")
            $("#txtDeleteReason").attr('value',"Please enter additional details. These will be viewable by future writers.");
    }
}

WriterResubmitAd = {
     adTrialId: 0,
     adChallengeId: 0,
     adDestinationUrl: '',
     adHeadline:'',
     adLine1:'',
     adLine2:'',
     adDisplayUrl:'',
     challengeResponse: null,

     init: function() {
        $("#txtheadline").bind("keyup", function() { var strVal = $("#txtheadline").val(); $("#lblheadline").text(25 - parseInt(strVal.length)); WriterResubmitAd.getAddBlock(); });
        $("#txtline1").bind("keyup", function() { var strVal = $("#txtline1").val(); $("#lblline1").text(35 - parseInt(strVal.length)); WriterResubmitAd.getAddBlock(); });
        $("#txtline2").bind("keyup", function() { var strVal = $("#txtline2").val(); $("#lblline2").text(35 - parseInt(strVal.length)); WriterResubmitAd.getAddBlock(); });
        $("#txturldisplay").bind("keyup", function() { var strVal = $("#txturldisplay").val(); $("#lblurldisplay").text(35 - parseInt(strVal.length)); WriterResubmitAd.getAddBlock(); });
        $("#txtheadline").attr("maxlength", 25);
        $("#txtline1").attr("maxlength", 35);
        $("#txtline2").attr("maxlength", 35);
        $("#txturldisplay").attr("maxlength", 35);
        return true;
      },
      
     setCurrentChallenge: function(adtrialid, adchallengeid, addesturl, adheadline, adline1, adline2, addispurl) {
        this.adTrialId = adtrialid;
        this.adChallengeId = adchallengeid;
        this.adDestinationUrl = addesturl.split("`").join("'");
        this.adHeadline = adheadline.split("`").join("'");
        this.adLine1 = adline1.split("`").join("'");
        this.adLine2 = adline2.split("`").join("'");
        this.adDisplayUrl = addispurl.split("`").join("'");
        $("#txtheadline").val(this.adHeadline);
        $("#txtline1").val(this.adLine1);
        $("#txtline2").val(this.adLine2);
        $("#txturldisplay").val(this.adDisplayUrl);
        WriterResubmitAd.getAddBlock();
     },

    getAddBlock: function() {
        var sb = "";
        sb += "<div class='adblock leftalign'> ";
        sb += "<a class='main' href='";
        sb += $("#txturldisplay").val() + "'>";
        sb += $("#txtheadline").val() + "</a> ";
        sb += "<p>";
        sb += $("#txtline1").val() + "<br />" + $("#txtline2").val() + "</p> ";
        sb += "<a class='site' href='";
        sb += $("#txturldisplay").val() + "'>";
        sb += $("#txturldisplay").val() + "</a> ";
        sb += "</div>";
        $("#addBox").attr("innerHTML", sb);
    },

     resubmitAd: function() {
        if (this.adChallengeId == 0) {
             alert('No item selected.');
             return;
        }
        if(this.adHeadline == $("#txtheadline").val() && this.adLine1 == $("#txtline1").val() && this.adLine2 == $("#txtline2").val() && this.adDisplayUrl == $("#txturldisplay").val())
        {
            $("#fstError").attr("innerHTML","You must change at least one character to resubmit");
            $("#errorsummary").show(500);
            setTimeout("$('#errorsummary').hide(500)",5000);
            return;
        }
        $('#ReSubmitAdAjax').removeClass("nodisplay"); //the ajax box
        $('#dvReSubmitAdd').addClass("ajaxContainer"); //the container you want to be gray
        $.post("/Writer/ResubmitChallenge",
               {
                   AdChallengeId: this.adChallengeId, Headline: $("#txtheadline").val(), Line1: $("#txtline1").val(), Line2:$("#txtline2").val(), DisplayURL: $("#txturldisplay").val()
               },
               function(data) { WriterResubmitAd.resubmitAd_callback(data); }, "json");
     },

     resubmitAd_callback: function(resp) {
        try{
              if (resp.ErrorList.length > 0) {
                    $("#fstError").attr("innerHTML",resp.ErrorList[0].ErrorMessage);
                    $("#errorsummary").show(500);
                    setTimeout("$('#errorsummary').hide(500)",5000);
                    $('#ReSubmitAdAjax').addClass("nodisplay");
                    $('#dvReSubmitAdd').removeClass("ajaxContainer");
                    $('.ajaxContainer').removeClass("ajaxContainer");
                    $('.ajaxMainProcessing').addClass("nodisplay");
                    return;                    
              }
              else
              {
                    $("#tr" + this.adChallengeId).addClass("nodisplay");
              }
         }catch(e) { }

        var count = parseInt($("#lblReSubmitAds").text()); //set submission tab count
        $("#lblReSubmitAds").text(count - 1);
        this.challengeResponse = resp;
        this.adTrialId = 0;
        this.adChallengeId = 0;
        tb_remove();
        $('#ReSubmitAdAjax').addClass("nodisplay");
        $('#dvReSubmitAdd').removeClass("ajaxContainer");
        $('.ajaxContainer').removeClass("ajaxContainer");
        $('.ajaxMainProcessing').addClass("nodisplay");
     }
 }

WriterEditAd = {
     adTrialId: 0,
     adChallengeId: 0,
     adDestinationUrl: '',
     adHeadline:'',
     adLine1:'',
     adLine2:'',
     adDisplayUrl:'',
     challengeResponse: null,

     init: function() {
        //$("#editheadline").bind("keyup", function() { var strVal = $("#editheadline").val(); $("#lbleditheadline").text(25 - parseInt(strVal.length)); WriterEditAd.getAddBlock(); });
        $("#editheadline").bind("keyup", function() {
            var strVal = $("#editheadline").val();
            if(strVal.toLowerCase().indexOf("{keyword:") >= 0) {
                if(strVal.indexOf("}") > strVal.toLowerCase().indexOf("{keyword:")) {
                    strVal = strVal.substring(0, strVal.toLowerCase().indexOf("{keyword:")) + strVal.substring(strVal.toLowerCase().indexOf("{keyword:") + 9, strVal.indexOf("}")) + strVal.substring(strVal.indexOf("}") + 1, strVal.length);
                    $("#lbleditheadline").text(25 - parseInt(strVal.length));
                }
            }
            else
                $("#lbleditheadline").text(25 - parseInt(strVal.length)); 
            WriterEditAd.getAddBlock();
        });        
        
        $("#editline1").bind("keyup", function() { var strVal = $("#editline1").val(); $("#lbleditline1").text(35 - parseInt(strVal.length)); WriterEditAd.getAddBlock(); });
        $("#editline2").bind("keyup", function() { var strVal = $("#editline2").val(); $("#lbleditline2").text(35 - parseInt(strVal.length)); WriterEditAd.getAddBlock(); });
        $("#editurldisplay").bind("keyup", function() { var strVal = $("#editurldisplay").val(); $("#lblediturldisplay").text(35 - parseInt(strVal.length)); WriterEditAd.getAddBlock(); });
        $("#editheadline").attr("maxlength", 35);
        $("#editline1").attr("maxlength", 35);
        $("#editline2").attr("maxlength", 35);
        $("#editurldisplay").attr("maxlength", 35);
        return true;
      },
      
     setCurrentChallenge: function(adtrialid, adchallengeid, addesturl, adheadline, adline1, adline2, addispurl) {
        this.adTrialId = adtrialid;
        this.adChallengeId = adchallengeid;
        this.adDestinationUrl = addesturl.split("`").join("'");
        this.adHeadline = adheadline.split("`").join("'");
        this.adLine1 = adline1.split("`").join("'");
        this.adLine2 = adline2.split("`").join("'");
        this.adDisplayUrl = addispurl.split("`").join("'");
        $("#editheadline").val(this.adHeadline);
        $("#lbleditheadline").text(25 - parseInt($("#editheadline").val().replace("{keyword:","").replace("}","").length)); 
        $("#editline1").val(this.adLine1);
        $("#lbleditline1").text(35 - parseInt($("#editline1").val().length));
        $("#editline2").val(this.adLine2);
        $("#lbleditline2").text(35 - parseInt($("#editline2").val().length));
        $("#editurldisplay").val(this.adDisplayUrl);
        $("#lblediturldisplay").text(35 - parseInt($("#editurldisplay").val().length));
        WriterEditAd.getAddBlock();
     },

    getKeywordfilter: function(strVal) {
        if(strVal.toLowerCase().indexOf("{keyword:") >= 0) {
            if(strVal.indexOf("}") > strVal.toLowerCase().indexOf("{keyword:")) {
                strVal = strVal.substring(0, strVal.toLowerCase().indexOf("{keyword:")) + strVal.substring(strVal.toLowerCase().indexOf("{keyword:") + 9, strVal.indexOf("}")) + strVal.substring(strVal.indexOf("}") + 1, strVal.length);
                return strVal;
            }
        }
        return strVal;
    },

    getAddBlock: function() {
        var sb = "";
        sb += "<div class='adblock leftalign'> ";
        sb += "<a class='main' href='";
        sb += $("#editurldisplay").val() + "'>";
        sb += this.getKeywordfilter($("#editheadline").val()) + "</a> ";
        sb += "<p>";
        sb += $("#editline1").val() + "<br />" + $("#editline2").val() + "</p> ";
        sb += "<a class='site' href='";
        sb += $("#editurldisplay").val() + "'>";
        sb += $("#editurldisplay").val() + "</a> ";
        sb += "</div>";
        $("#editadBox").attr("innerHTML", sb);
        return sb;
    },

     editAd: function() {
        if (this.adChallengeId == 0) {
             alert('No item selected.');
             return;
        }

        if(this.adHeadline == $("#editheadline").val() && this.adLine1 == $("#editline1").val() && this.adLine2 == $("#editline2").val() && this.adDisplayUrl == $("#editurldisplay").val())
        {
            $("#editfstError").attr("innerHTML","You must change at least one character before submit");
            $("#editerrorsummary").show(500);
            setTimeout("$('#editerrorsummary').hide(500)",5000);
            return;
        }
        
        myChecker = new SpellChecker();
        tb_remove();
        myChecker.checkSpelling($('#editheadline'), function(status) {
            if (status)
                myChecker.checkSpelling($('#editline1'), function(status) {
                    if (status)
                        myChecker.checkSpelling($('#editline1'), function(status) {
                            if (status)
                                myChecker.checkSpelling($('#editurldisplay'), function(status) {
                                    if (status) {
                                        $("#lnkEditShow").click();
                                        this.adHeadline = $("#editheadline").val();
                                        this.adLine1 = $("#editline1").val();
                                        this.adLine2 = $("#editline2").val();
                                        this.adDisplayUrl = $("#editurldisplay").val();
                                    
                                        $('#EditAdAjax').removeClass("nodisplay"); //the ajax box
                                        $('#dvEditAdd').addClass("ajaxContainer"); //the container you want to be gray

                                        $.post("/Writer/EditAdChallenge",
                                               {
                                                   AdChallengeId: WriterEditAd.adChallengeId, Headline: $("#editheadline").val(), Line1: $("#editline1").val(), Line2:$("#editline2").val(), DisplayURL: $("#editurldisplay").val()
                                               },
                                               function(data) { WriterEditAd.editAd_callback(data); }, "json");
                                    }
                              })
                        })
                })
        })
     },

     editAd_callback: function(resp) {
        try{
              if (resp.ErrorList.length > 0) {
                    $("#editfstError").attr("innerHTML",resp.ErrorList[0].ErrorMessage);
                    $("#editerrorsummary").show(500);
                    setTimeout("$('#editerrorsummary').hide(500)",5000);
                    $('#EditAdAjax').addClass("nodisplay");
                    $('#dvEditAdd').removeClass("ajaxContainer");
                    $('.ajaxContainer').removeClass("ajaxContainer");
                    $('.ajaxMainProcessing').addClass("nodisplay");
                    return;
              }
              else
              {
                    var instr = "<a id='btnEdit" + this.adChallengeId + "' href='#TB_inline?height=320&width=500&inlineId=editad-modal&modal=true' onclick='WriterEditAd.setCurrentChallenge(" + this.adTrialId + "," + this.adChallengeId + ",\"" + this.adDestinationUrl.split("'").join("`") + "\",\"" + this.adHeadline.split("'").join("`") + "\",\"" + this.adLine1.split("'").join("`") + "\",\"" + this.adLine2.split("'").join("`") + "\",\"" + this.adDisplayUrl.split("'").join("`") + "\");' class='thickbox mbutton mbutton-green'>Edit</a>";
                    $("#link" + this.adChallengeId).attr("innerHTML",instr);
                    $("#editad" + this.adChallengeId).attr("innerHTML", WriterEditAd.getAddBlock());
                    tb_init('a.thickbox, area.thickbox, input.thickbox');
              }
         }catch(e) { }

        this.challengeResponse = resp;
        this.adTrialId = 0;
        this.adChallengeId = 0;
        tb_remove();
        $('#EditAdAjax').addClass("nodisplay");
        $('#dvEditAdd').removeClass("ajaxContainer");
        $('.ajaxContainer').removeClass("ajaxContainer");
        $('.ajaxMainProcessing').addClass("nodisplay");
     }
 }

var SignupModal = {
    isAdvertiser:true,
    isValid:true,
    
    init: function() {        
        $("input[type='text']").change(function() { if($(this).val() != "") { $(this).removeClass("errorfield"); $("#spn" + this.id).addClass("nodisplay"); } });
        $("#Industry").change(function() { if($(this).val() != "") { $(this).removeClass("errorfield"); $("#spn" + this.id).addClass("nodisplay"); } });
        $("#UserName").blur(function() { SignupModal.checkUserExist(); });
         
        $("input[vtype='email']").change(function(event) {
            if(!FiledValidation.IsValidEmail(this)) {
                $("#spn" + this.id).html("Invalid!");
                $("#spn" + this.id).removeClass("nodisplay");
                $("#" + this.id).addClass("errorfield");
            } else {
                $("#spn" + this.id).adClass("nodisplay");
            }            
        });
        
//        $("input[vtype='nospchar']").change(function(event) {
//            if(FiledValidation.IsNoSpecialChar(this)) {
//                $("#spn" + this.id).html("Invalid!");
//                $("#spn" + this.id).removeClass("nodisplay");
//                $("#" + this.id).addClass("errorfield");
//            } else {
//                $("#spn" + this.id).adClass("nodisplay");
//            }            
//        });

        $("input[vtype='char']").change(function(event) {
            if(!FiledValidation.IsChar(this)) {
                $("#spn" + this.id).html("Invalid!");
                $("#spn" + this.id).removeClass("nodisplay");
                $("#" + this.id).addClass("errorfield");
            } else {
                $("#spn" + this.id).adClass("nodisplay");
            }
        });
        
        $("input[vtype='phonenumber']").change(function(event) {
            if(!FiledValidation.IsPhoneNumber(this)) {
                $("#spn" + this.id).html("000-000-0000");
                $("#spn" + this.id).removeClass("nodisplay");
                $("#" + this.id).addClass("errorfield");
            } else {
                $("#spn" + this.id).adClass("nodisplay");
            }
        });
        
        $("input[vtype='weburl']").change(function(event) {
            if(!FiledValidation.IsWebURL(this)) {
                $("#spn" + this.id).html("Invalid!");
                $("#spn" + this.id).removeClass("nodisplay");
                $("#" + this.id).addClass("errorfield");
            } else {
                $("#spn" + this.id).adClass("nodisplay");
            }
        });
    },

    accountTypechange:function() {
        if($("#AccountType").val() == "1") {
            SignupModal.isAdvertiser = true;
            $("#advertiserfield").css("display","inline");
            $("#writerfiled").css("display","none");
        } else {
            SignupModal.isAdvertiser = false;
            $("#advertiserfield").css("display","none");
            $("#writerfiled").css("display","inline");
        }
    },

    submitUserData:function() {
        $('#CreateAccAjax').removeClass("nodisplay"); //the ajax box
        $('#dvCreateAcc').addClass("ajaxContainer"); //the container you want to be gray

        SignupModal.isValid = SignupModal.validation();
        if(SignupModal.isValid) {

        var Languages = "";
        if($("#AccountType").val() == "2") {
            $("input[name='writerlanguage']:checked").each(function() {
                Languages += (Languages == "" ? this.id : "," + this.id);
            });

            if($("#otherlanguage:checked").val() != undefined)
                Languages += (Languages == "" ? $("#txtotherlanguage").val() : "," + $("#txtotherlanguage").val());
        }
        
        $.post("/Account/frontsignup",
                {
                    Industry:$("#Industry").val(),
                    Email:$("#Email").val(), 
                    FirstName:$("#FirstName").val(), 
                    LastName:$("#LastName").val(), 
                    Phone:$("#Phone").val(),
                    AccountType:$("#AccountType").val(), 
                    JobTitle:$("#JobTitle").val(), 
                    Company:$("#Company").val(), 
                    WebSite:$("#Website").val(), 
                    UserName:$("#UserName").val(), 
                    Password:$("#Password").val(), 
                    Terms:$("#Terms").val(),
                    State:$("#State").val(),
                    languages:Languages
                },
                function(data) { SignupModal.submitUserData_callback(data); }, "json");
        }
        else
        {
            $('#CreateAccAjax').addClass("nodisplay"); //the ajax box
            $('#dvCreateAcc').removeClass("ajaxContainer"); //the container you want to be gray
        }
    },

    submitUserData_callback: function(resp) {
        if(resp == "Incorrect Data") {
            SignupModal.validation();
            $('#CreateAccAjax').addClass("nodisplay"); //the ajax box
            $('#dvCreateAcc').removeClass("ajaxContainer"); //the container you want to be gray
        }
        else if(resp == "Successfull") {
            window.location.href = "/Home/CompleteSignup";
        }
        else {
            SignupModal.validation();
            $('#CreateAccAjax').addClass("nodisplay"); //the ajax box
            $('#dvCreateAcc').removeClass("ajaxContainer"); //the container you want to be gray
        }
    },
    
    checkUserExist:function() {
        $.post("/Account/checkUserExist",
        {
            UserName:$("#UserName").val()
        },
        function(data) { SignupModal.checkUserExist_callback(data); }, "json");
    },

    checkUserExist_callback: function(resp) {
        if(resp == "Exists" || resp == "Error") {
            $("#spnUserNameExist").removeClass("nodisplay");
            SignupModal.isValid = false;
        }
        else
            $("#spnUserNameExist").addClass("nodisplay");
    },    

    validation:function() {
        SignupModal.isValid = true;
        //SignupModal.checkUserExist();
        
        if($("#otherlanguage:checked").val() != undefined && $("#txtotherlanguage").val() == "") {
            $("#txtotherlanguage").addClass("errorfield");
            SignupModal.isValid = false;
        }
        
        if($("input[name='writerlanguage']:checked").val() == undefined && $("#otherlanguage:checked").val() == undefined && SignupModal.isAdvertiser == false)
        {
            $("#spnLanguages").removeClass("nodisplay");
            SignupModal.isValid = false;
        }
        else
            $("#spnLanguages").addClass("nodisplay");

        if($("#Email").val() == "" || !FiledValidation.IsValidEmail($("#Email"))) {
            if(!FiledValidation.IsValidEmail($("#Email")))
                $("#spnEmail").html("Invalid!");
            else
                $("#spnEmail").html("Required");
            $("#spnEmail").removeClass("nodisplay");
            $("#Email").addClass("errorfield");
            SignupModal.isValid = false;
        }
        else
            $("#spnEmail").addClass("nodisplay");
            
//        if($("#UserName").val() == "" || !FiledValidation.IsNoSpecialChar($("#UserName"))) {
//            if(!FiledValidation.IsNoSpecialChar($("#UserName")))
//                $("#spnUserName").html("Invalid!");
//            else
//                $("#spnUserName").html("Required");                
//            $("#spnUserName").removeClass("nodisplay");
//            $("#UserName").addClass("errorfield");
//            SignupModal.isValid = false;
//        }
//        else
//            $("#spnUserName").addClass("nodisplay");
//            
//        if($("#Password").val() == "") {
//            $("#spnPassword").html("Required");
//            $("#spnPassword").removeClass("nodisplay");
//            $("#Password").addClass("errorfield");
//            SignupModal.isValid = false;
//        }
//        else
//            $("#spnPassword").addClass("nodisplay");

//        if($("#Password").val() != "" && $("#Password").val().length < 6) {
//            $("#spnLenPassword").html("Password should Minimum 6 charcters long");
//            $("#spnLenPassword").removeClass("nodisplay");
//            $("#Password").addClass("errorfield");
//            SignupModal.isValid = false;
//        }
//        else
//            $("#spnLenPassword").addClass("nodisplay");

        if($("#FirstName").val() == "" || !FiledValidation.IsChar($("#FirstName"))) {
            if(!FiledValidation.IsChar($("#FirstName")))
                $("#spnFirstName").html("Invalid!");
            else
                $("#spnFirstName").html("Required");
            
            $("#spnFirstName").removeClass("nodisplay");
            $("#FirstName").addClass("errorfield");
            SignupModal.isValid = false;
        }
        else
            $("#spnFirstName").addClass("nodisplay");
            
        if($("#LastName").val() == "" || !FiledValidation.IsChar($("#LastName"))) {
            if(!FiledValidation.IsChar($("#LastName")))
                $("#spnLastName").html("Invalid!");
            else
                $("#spnLastName").html("Required");
        
            $("#spnLastName").removeClass("nodisplay");
            $("#LastName").addClass("errorfield");
            SignupModal.isValid = false;
        }
        else
            $("#spnLastName").addClass("nodisplay");

        if($("#Phone").val() == "" || !FiledValidation.IsPhoneNumber($("#Phone"))) {
            if(!FiledValidation.IsPhoneNumber($("#Phone")))
                $("#spnPhone").html("000-000-0000");
            else
                $("#spnPhone").html("Required");        
        
            $("#spnPhone").removeClass("nodisplay");
            $("#Phone").addClass("errorfield");
            SignupModal.isValid = false;
        }
        else
            $("#spnPhone").addClass("nodisplay");

        if($("#JobTitle").val() == "" && SignupModal.isAdvertiser == true) {
            $("#spnJobTitle").removeClass("nodisplay");
            $("#JobTitle").addClass("errorfield");
            SignupModal.isValid = false;
        }
        else
            $("#spnJobTitle").addClass("nodisplay");

        if($("#Company").val() == "" && SignupModal.isAdvertiser == true) {
            $("#spnCompany").removeClass("nodisplay");
            $("#Company").addClass("errorfield");
            SignupModal.isValid = false;
        }
        else
            $("#spnCompany").addClass("nodisplay");

        if($("#State").val() == "" && SignupModal.isAdvertiser == true) {
            $("#spnState").removeClass("nodisplay");
            $("#State").addClass("errorfield");
            SignupModal.isValid = false;
        }
        else
            $("#spnState").addClass("nodisplay");

        if(($("#Website").val() == "" || !FiledValidation.IsWebURL($("#Website"))) && SignupModal.isAdvertiser == true) {
            if(!FiledValidation.IsWebURL($("#Website")))
                $("#spnWebSite").removeClass("Invalid!");
            else
                $("#spnWebSite").html("Required");
                
            $("#spnWebSite").removeClass("nodisplay");
            $("#Website").addClass("errorfield");
            SignupModal.isValid = false;
        }
        else
            $("#spnWebSite").addClass("nodisplay");

        if($("#Industry").val() == "" && SignupModal.isAdvertiser == true) {
            $("#spnIndustry").removeClass("nodisplay");
            $("#Industry").addClass("errorfield");
            SignupModal.isValid = false;
        }
        else
            $("#spnIndustry").addClass("nodisplay");


//        if($("#Terms").is(':checked') == false) {
//            $("#spnTerms").removeClass("nodisplay");
//            $("#Terms").addClass("errorfield");
//            SignupModal.isValid = false;
//        }
//        else
//            $("#spnTerms").addClass("nodisplay");
        return SignupModal.isValid;
    }
}


var WriterSignupModal = {
    isValid:true,
    
    init: function() {        
        $("input[type='text']").change(function() { if($(this).val() != "") { $(this).removeClass("errorfield"); $("#spn" + this.id).addClass("nodisplay"); } });
        $("#Password").blur(function() { if($(this).val() != "") { $(this).removeClass("errorfield"); $("#spn" + this.id).addClass("nodisplay"); } });
        $("#UserName").blur(function() { WriterSignupModal.checkUserExist(); });
    },
    
    submitUserData:function() {
        $('#CreateAccAjax').removeClass("nodisplay"); //the ajax box
        $('#dvCreateAcc').addClass("ajaxContainer"); //the container you want to be gray

        WriterSignupModal.isValid = WriterSignupModal.validation();
        if(WriterSignupModal.isValid) {
        
        var Languages = "";
//        $("input[name='writerlanguage']:checked").each(function() {
//            Languages += (Languages == "" ? $(this).val() : "," + $(this).val());
//        });

//        if($("#otherlanguage:checked").val() != undefined)
//            Languages += (Languages == "" ? $("#txtotherlanguage").val() : "," + $("#txtotherlanguage").val());

        $.post("/Account/writerSignup",
                {
                    Email:$("#Email").val(), 
                    FirstName:$("#FirstName").val(), 
                    LastName:$("#LastName").val(), 
                    Phone:$("#Phone").val(),
                    UserName:$("#UserName").val(), 
                    Password:$("#Password").val(), 
                    Languages:Languages, 
                    Terms:$("#Terms").val()
                },
                function(data) { WriterSignupModal.submitUserData_callback(data); }, "json");
        }
        else
        {
            $('#CreateAccAjax').addClass("nodisplay"); //the ajax box
            $('#dvCreateAcc').removeClass("ajaxContainer"); //the container you want to be gray
        }
    },

    submitUserData_callback: function(resp) {
        if(resp == "Incorrect Data") {
            WriterSignupModal.validation();
            $('#CreateAccAjax').addClass("nodisplay"); //the ajax box
            $('#dvCreateAcc').removeClass("ajaxContainer"); //the container you want to be gray
        }
        else if(resp == "Successfull") {
            window.location.href = "/Writer/Dashboard";
        }
        else {
            WriterSignupModal.validation();
            $('#CreateAccAjax').addClass("nodisplay"); //the ajax box
            $('#dvCreateAcc').removeClass("ajaxContainer"); //the container you want to be gray
        }
    },
    
    checkUserExist:function() {
        $.post("/Account/checkUserExist",
        {
            UserName:$("#UserName").val()
        },
        function(data) { WriterSignupModal.checkUserExist_callback(data); }, "json");
    },

    checkUserExist_callback: function(resp) {
        if(resp == "Exists" || resp == "Error") {
            $("#spnUserNameExist").removeClass("nodisplay");
            WriterSignupModal.isValid = false;
        }
        else
            $("#spnUserNameExist").addClass("nodisplay");
    },    

    validation:function() {
        WriterSignupModal.isValid = true;
        WriterSignupModal.checkUserExist();
        
        if($("#Email").val() == "") {
            $("#spnEmail").removeClass("nodisplay");
            $("#Email").addClass("errorfield");
            WriterSignupModal.isValid = false;
        }
        else
            $("#spnEmail").addClass("nodisplay");
            
        if($("#UserName").val() == "") {
            $("#spnUserName").html("Required");
            $("#spnUserName").removeClass("nodisplay");
            $("#UserName").addClass("errorfield");
            WriterSignupModal.isValid = false;
        }
        else
            $("#spnUserName").addClass("nodisplay");
            
        if($("#Password").val() == "") {
            $("#spnPassword").html("Required");
            $("#spnPassword").removeClass("nodisplay");
            $("#Password").addClass("errorfield");
            WriterSignupModal.isValid = false;
        }
        else
            $("#spnPassword").addClass("nodisplay");

        if($("#Password").val() != "" && $("#Password").val().length < 6) {
            $("#spnLenPassword").html("<div class='cl'>&nbsp;</div><label>&nbsp;</label>Password should Minimum 6 charcters long");
            $("#spnLenPassword").removeClass("nodisplay");
            $("#Password").addClass("errorfield");
            WriterSignupModal.isValid = false;
        }
        else
            $("#spnLenPassword").addClass("nodisplay");

        if($("#FirstName").val() == "") {
            $("#spnFirstName").removeClass("nodisplay");
            $("#FirstName").addClass("errorfield");
            WriterSignupModal.isValid = false;
        }
        else
            $("#spnFirstName").addClass("nodisplay");
            
        if($("#LastName").val() == "") { 
            $("#spnLastName").removeClass("nodisplay");
            $("#LastName").addClass("errorfield");
            WriterSignupModal.isValid = false;
        }
        else
            $("#spnLastName").addClass("nodisplay");

        if($("#Phone").val() == "") {
            $("#spnPhone").removeClass("nodisplay");
            $("#Phone").addClass("errorfield");
            WriterSignupModal.isValid = false;
        }
        else
            $("#spnPhone").addClass("nodisplay");

//        if($("#otherlanguage:checked").val() != undefined && $("#txtotherlanguage").val() == "") {
//            $("#txtotherlanguage").addClass("errorfield");
//            WriterSignupModal.isValid = false;
//        }

//        if($("input[name='writerlanguage']:checked").val() == undefined && $("#otherlanguage:checked").val() == undefined)
//        {
//            $("#spnLanguages").removeClass("nodisplay");
//            WriterSignupModal.isValid = false;
//        }
//        else
//            $("#spnLanguages").addClass("nodisplay");

        if($("#Terms").is(':checked') == false) {
            $("#spnTerms").removeClass("nodisplay");
            $("#Terms").addClass("errorfield");
            WriterSignupModal.isValid = false;
        }
        else
            $("#spnTerms").addClass("nodisplay");
            
        return WriterSignupModal.isValid;
    }
}

var AffiliateModal = {
    isValid:true,
    
    init: function() {        
        $("input[type='text']").change(function() { if($(this).val() != "") { $(this).removeClass("errorfield"); $("#spn" + this.id).addClass("nodisplay"); } });
    },
    
    submitAffiliateData:function() {
        $('#CreateAffiliateAjax').removeClass("nodisplay"); //the ajax box
        $('#dvCreateAffiliate').addClass("ajaxContainer"); //the container you want to be gray

        AffiliateModal.isValid = AffiliateModal.validation();
        if(AffiliateModal.isValid) {
        
        $.post("/Account/affiliatesignup",
                {
                    Email:$("#AffiliateEmail").val(), 
                    FirstName:$("#AffiliateFirstName").val(), 
                    LastName:$("#AffiliateLastName").val(), 
                    Phone:$("#AffiliatePhone").val(),
                    Terms:$("#AffiliateTerms").val()
                },
                function(data) { AffiliateModal.submitAffiliateData_callback(data); }, "json");
        }
        else
        {
            $('#CreateAffiliateAjax').addClass("nodisplay"); //the ajax box
            $('#dvCreateAffiliate').removeClass("ajaxContainer"); //the container you want to be gray
        }
    },

    submitAffiliateData_callback: function(resp) {
        if(resp == "Incorrect Data") {
            AffiliateModal.validation();
            $('#CreateAffiliateAjax').addClass("nodisplay"); //the ajax box
            $('#dvCreateAffiliate').removeClass("ajaxContainer"); //the container you want to be gray
        }
        else if(resp == "Successfull") {
            window.location.href = "/Home/CompleteSignup";
        }
        else {
            AffiliateModal.validation();
            $('#CreateAffiliateAjax').addClass("nodisplay"); //the ajax box
            $('#dvCreateAffiliate').removeClass("ajaxContainer"); //the container you want to be gray
        }
    },
    
    validation:function() {
        AffiliateModal.isValid = true;
       
        if($("#AffiliateEmail").val() == "") {
            $("#spnAffiliateEmail").removeClass("nodisplay");
            $("#AffiliateEmail").addClass("errorfield");
            AffiliateModal.isValid = false;
        }
        else
            $("#spnAffiliateEmail").addClass("nodisplay");
            
        if($("#AffiliateFirstName").val() == "") {
            $("#spnAffiliateFirstName").removeClass("nodisplay");
            $("#AffiliateFirstName").addClass("errorfield");
            AffiliateModal.isValid = false;
        }
        else
            $("#spnAffiliateFirstName").addClass("nodisplay");
            
        if($("#AffiliateLastName").val() == "") { 
            $("#spnAffiliateLastName").removeClass("nodisplay");
            $("#AffiliateLastName").addClass("errorfield");
            AffiliateModal.isValid = false;
        }
        else
            $("#spnAffiliateLastName").addClass("nodisplay");

        if($("#AffiliatePhone").val() == "") {
            $("#spnAffiliatePhone").removeClass("nodisplay");
            $("#AffiliatePhone").addClass("errorfield");
            AffiliateModal.isValid = false;
        }
        else
            $("#spnAffiliatePhone").addClass("nodisplay");

        if($("#AffiliateTerms").is(':checked') == false) {
            $("#spnAffiliateTerms").removeClass("nodisplay");
            $("#AffiliateTerms").addClass("errorfield");
            AffiliateModal.isValid = false;
        }
        else
            $("#spnAffiliateTerms").addClass("nodisplay");

        return AffiliateModal.isValid;
    }
}