﻿var camp = {
    AdID: 0,
    sGuid: '',
    Type: 0,
    sName: '',
    sHeadline: '',
    sDescription: '',
    sWebsite: escape('http://'),
    sDestinationURL: escape('http://'),
    iLogoType: 0,
    sLogo: 'http://s3.amazonaws.com/Reollie/logo3.gif',
    sTerm: '',
    sQualification: '',
    sType: 0,

    iScreenOption: 0,
    iProfileOptions: 0,
   

    CategoryType: 0,
    Categorys: '',

    LocationType: 0,
    Locations: '0',
    State: '',

    KeyowrdsType: 0,
    Keyowrds: '',

    DailyBudget: 0,
    MaxCPC: 0,
    ibaMaxCPC: 0,
    ibaDailyBudget: 0,
    iPayMent: 0,

    iIsLocalType: 1,
    iLocalID: '0',
    iStateID: '0',

    Ad: [],
    iAdCustomable: 1,
    iAdTemplateID: 1,
     
    UploadedImgs: [],

    GetAd: function(tid, sid) {
        return this.Ads.GetAd(tid, sid);
    },

    Ads: {
        Data: [],

        GetAd: function(id, sid) {
            var _a = null;
            $.each(this.Data, function() {
                if ((this.ID == id) && (this.SerialID == sid)) { _a = this; }
            });
            return _a;
        },

        Add: function(_a) {
            this.Data.push(_a);
        },

        Clear: function() {
            this.Data.length = 0;
        },

        ToXmlData: function() {
            var _d = "";
            $.each(this.Data, function(i) {
                if (this.H == 0) {
                    _d += "<AdData>";
                    _d += "<TemplateID><![CDATA[" + this.ID + "]]></TemplateID>";
                    _d += "<SerialID><![CDATA[" + this.SerialID + "]]></SerialID>";
                    _d += "<Content><![CDATA[" + escape(this.Content) + "]]></Content>";
                    _d += "</AdData>";
                }
            });

            return _d;
        }
    },

    //should the post data in xml format? 
    ToPostData: function() {
        if (this.MaxCPC == "")
            this.MaxCPC = 0;
        if (this.DailyBudget == "")
            this.DailyBudget = 0;
        if (this.ibaMaxCPC == "")
            this.ibaMaxCPC = 0;
        if (this.ibaDailyBudget == "")
            this.ibaDailyBudget = 0;
        this.sName = $("#inptName").attr("value");

        var _onclik = "try{if(flag==1){gotopage(this);}else {alert('editing......');return false;} }catch(e){alert('editing......');return false;}";

        //var ad = "<div style='font-size:12px;margin-bottom:5px;'>"+this.Ad[0].Content+"</div><br /><span style='margin-top:5px;'><a onclick=\""+_onclik+"\" href=\"javascript:void(0);\">"+this.Ad[0].Site+"</a></span>";
        var ad = this.Ad[0].Content;

        var _sMeta = "<SMeta><Content><![CDATA[" + this.Ad[0].Content + "]]></Content><Site><![CDATA[" + this.Ad[0].Site + "]]></Site></SMeta>";

        var _xml = "<?xml version='1.0' encoding='utf-8' ?>";
        _xml += "<Campaign>";
        _xml += "<Ad AType='" + this.Type + "' >";
        ////////////////////////////////////////////////////////////////////// post ads one Step
        _xml += "<Name><![CDATA[" + handlechar(this.sName.replace(/<br>/g, '\n')) + "]]></Name>";
        _xml += "<Headline><![CDATA[" + handlechar(this.sHeadline.replace(/<br>/g, '\n')) + "]]></Headline>";
        _xml += "<Description><![CDATA[" + handlechar(this.sDescription.replace(/<br>/g, '\n')) + "]]></Description>";
        _xml += "<Website><![CDATA[" + handlechar(this.sWebsite.replace(/<br>/g, '\n')) + "]]></Website>";
        _xml += "<Logotype><![CDATA[" + this.iLogoType + "]]></Logotype>";
        _xml += "<Logourl><![CDATA[" + handlechar(this.sLogo.replace(/<br>/g, '\n')) + "]]></Logourl>";
        _xml += "<Term><![CDATA[" + handlechar(this.sTerm.replace(/<br>/g, '\n')) + "]]></Term>";
        _xml += "<Qualification><![CDATA[" + handlechar(this.sQualification.replace(/<br>/g, '\n')) + "]]></Qualification>";
        _xml += "<Type><![CDATA[" + this.Type + "]]></Type>";
        ////////////////////////////////////////////////////////////////////// post ads Two Step

        _xml += "<AdCustomable><![CDATA[" + this.iAdCustomable + "]]></AdCustomable>";
        _xml += "<Template><![CDATA[" + handlechar(ad) + "]]></Template>";
        _xml += "<sMeta>" + handlechar(_sMeta) + "</sMeta>";
        _xml += "<ScreenOption><![CDATA[" + this.iScreenOption + "]]></ScreenOption>";
        _xml += "<ProfileOptions><![CDATA[" + this.iProfileOptions + "]]></ProfileOptions>";
        _xml += "<DestURL><![CDATA[" + this.sDestinationURL + "]]></DestURL>";
        ////////////////////////////////////////////////////////////////////// post ads Three Step
        _xml += "<CategoryType><![CDATA[" + this.CategoryType + "]]></CategoryType>";
        _xml += "<Categorys><![CDATA[" + this.Categorys + "]]></Categorys>";
        _xml += "<LocationType><![CDATA[" + this.LocationType + "]]></LocationType>";
        _xml += "<Locations><![CDATA[" + this.Locations + "]]></Locations>";
        _xml += "<State><![CDATA[" + handlechar(this.State) + "]]></State>";
        _xml += "<KeyowrdsType><![CDATA[" + this.KeyowrdsType + "]]></KeyowrdsType>";
        _xml += "<Keyowrds><![CDATA[" + handlechar(this.Keyowrds) + "]]></Keyowrds>";
        ////////////////////////////////////////////////////////////////////// post ads Four Step
        _xml += "<PayMent><![CDATA[" + this.iPayMent + "]]></PayMent>";
        _xml += "<MaxCPC><![CDATA[" + this.MaxCPC + "]]></MaxCPC>";
        _xml += "<DailyBudget><![CDATA[" + this.DailyBudget + "]]></DailyBudget>";
        _xml += "<MaxCPM><![CDATA[" + this.ibaMaxCPC + "]]></MaxCPM>";
        _xml += "<DailyBudgetCPM><![CDATA[" + this.ibaDailyBudget + "]]></DailyBudgetCPM>";
        _xml += " <Guid><![CDATA[" + this.sGuid + "]]></Guid>";
        _xml += " <IsLocalType><![CDATA[" + this.iIsLocalType + "]]></IsLocalType>";
        _xml += " <LocalID><![CDATA[" + this.iLocalID + "]]></LocalID>";
        _xml += " <StateID><![CDATA[" + this.iStateID + "]]></StateID>";
        _xml += " <AdTemplateID><![CDATA[" + this.iAdTemplateID + "]]></AdTemplateID>";
        //////////////////////////////////////////////////////////////////////
        _xml += "</Ad>";
        _xml += "</Campaign>";

        return _xml;
    },
    SubmitData: function(t) {
        var _d = this.ToPostData();
        if (camp.KeyowrdsType == 1 && camp.Keyowrds.trim() == "") {
            gotolab(4);
            alert(submsg[10]);
            return false;
        }
        else if (camp.sHeadline.trim() == "") {
            gotolab(1);
            alert(submsg[3]);
            return false;
        }
        else if (camp.sWebsite.trim() == "http://") {
            gotolab(1);
            alert(submsg[11]);
            return false;
        }
        else if (!checkurl(camp.sWebsite)) {
            gotolab(1);
            alert(submsg[12]);
            return false;
        }
        else if (camp.sDestinationURL.trim() == "http://") {
            gotolab(3);
            alert(submsg[4]);
            return false;
        }
        else if (!checkurl(camp.sDestinationURL)) {
            gotolab(3);
            alert(submsg[5]);
            return false;
        }
        else if (!checknumber(camp.MaxCPC)) {
            gotolab(4);
            alert(submsg[6]);
            return false;
        }
        else if (!checknumber(camp.DailyBudget)) {
            gotolab(4);
            alert(submsg[7]);
            return false;
        }
        else if (!checknumber(camp.ibaMaxCPC)) {
            gotolab(4);
            alert(submsg[8]);
            return false;
        }
        else if (!checknumber(camp.ibaDailyBudget)) {
            gotolab(4);
            alert(submsg[9]);
            return false;
        }
        else {
            var url = "/sp/ad/postinghandler.aspx?hidereolliehf=1&ran=" + Math.floor(Math.random() * 10000);
            var param = "adinput=" + _d + "&adid=" + camp.AdID + "&sLogo=" + camp.sLogo + "&iLogoType=" + camp.iLogoType + camp.sLogo + "&iSubmissionType=" + t;

            showAdvaQuery();
            $.post(url, param, function(data) {
                if (camp.AdID == -9) {
                    alert('You do not have advertiser permission');
                }
                else {
                     showAdvaQuery2();
                }
            });
        }
    }
}

function gotolab(type) {

    var ifm = $("#frmEditor").attr("contentWindow");
    if (ifm.FillData)
        ifm.FillData();
    if (type == 1)
        $("#frmEditor").attr("src", "/ads/service.aspx");
    else if (type == 2)
        $("#frmEditor").attr("src", "/ads/targeting.aspx");
    else if (type == 3)
        $("#frmEditor").attr("src","/ads/designad.aspx");
    else if (type == 4)
        $("#frmEditor").attr("src", "/ads/publishing.aspx");
    else if (type == 5)
        $("#frmEditor").attr("src", "/ads/confirm.aspx");

    var objImgID = null;
    for (var i = 1; i < 6; i++) {
        var ele = $("#imgfrmId" + i.toString());
        var e2  = $("#fnId" + i.toString());
        if (type == i)
        {
            ele.attr("src", "/images/button/" + i.toString() + ".gif");
              e2.css("color", "#70A183");
        }
        else
        {
            ele.attr("src", "/images/button/" + i.toString() + "_h.gif");
            e2.css("color", "#a3a3a3");
        }
    }
    $("#divDesigner").attr("class", "DivQueryHidden");
}


function AdjustWindow(_h) {
    var h = (_h > 375) ? _h: 375;
    
    var ifm = $("#frmEditor").css("height", h + "px"); 
}

function ShowEditor(id) {
    if (camp.iAdTemplateID!=id)
       camp.Ads.Clear();
    camp.iAdTemplateID = id;
   
    $("#divDesigner").css("height", document.body.offsetHeight + "px");
    var _f = $("#frmDesigner");
    _f.attr("src", "/ads/jeditor.aspx?tid=" + id);
    $("#divDesigner").attr("class", "divDesigner2009");
}

function CloseEditor() {
    var ifm = $("#frmDesigner").attr("contentWindow");
    if (ifm.FillAdData)
        ifm.FillAdData();
    $("#divDesigner").attr("class", "DivQueryHidden");
    return false;
}

//link on the div   
function popupgoto(type) {
    if (type == "advance")
        window.location.href = "/sp/adv/camp/addetail.aspx?adid=" + camp.AdID;
    else if (type == "invite")
        window.location.href = "/search/bstsc.aspx";
    else if (type == "edit")
        window.location.href = "/sp/adv/camp/addetail.aspx?adid=" + camp.AdID;
    else
        window.location.href = "/sp/myaccount/my.aspx";
}
//show div
function showAdvaQuery() {
    if ($("#divQueryCond").attr("class") == "DivQueryHidden") {
        $("#divQueryCond").attr("class", "DivQueryDisplay");
        $("#DivQueryCover").attr("class", "DivQueryBlock");
        $("#divQueryCond").css("left", document.body.offsetWidth / 2 - 200 + "px");
        $("#divQueryCond").css("top", document.documentElement.scrollTop+ 100+ "px");
        $("#DivQueryCover").css("height", document.body.scrollHeight + "px");
         $("#divPostIndicator").show();
        $("#divPostMessage").hide();
    }
}

function showAdvaQuery2(){
    $("#divPostIndicator").hide();
    $("#divPostMessage").show();
}

function closepopup() {
    $("#divQueryCond").attr("class", "DivQueryHidden");
    $("#DivQueryCover").attr("class", "DivQueryHidden");
}    