var IPOST=(function(){
    function B(F,E,C,D){
        this.formObj=F;
        this.tokenValue=E;
        this.emailField=C;
        this.monetaryField=D;
        this.error="";
        this.getFormValues()
    }
    B.prototype.getFormValues=function(){
        if(typeof (this.formObj.elements)!="undefined"){
            for(var C=0;C<this.formObj.elements.length;C++){
                if(this.emailField&&this.formObj.elements[C].name==this.emailField){this.emailValue=this.formObj.elements[C].value}
                if(this.monetaryField&&this.formObj.elements[C].name==this.monetaryField){this.monetaryValue=this.formObj.elements[C].value}
            }
        }else{
            this.error+="We could not access form elements.\n"
        }
        if(this.emailValue&&this.monetaryValue&&this.tokenValue){
           this.loadScript()
        }else{
            this.error+="There was a problem with either the email value, monetary value, token value.\n"
        }
    };
    B.prototype.loadScript=function(){
        var E="ipostScript";
        var C=document.getElementById(E);
        if(C!=null){
            C.parentNode.removeChild(C);
            delete C
        }
        this.newJs=document.createElement("script");
        //demo
        //this.newJs.src="http://demo.ipost.com/js-autotarget.cgi?email_addr="+encodeURIComponent(this.emailValue)+"&submit=submit&monetary="+encodeURIComponent(this.monetaryValue)+"&token="+encodeURIComponent(this.tokenValue);
        //prod
        this.newJs.src="http://conduit.ipost.com/js-autotarget.cgi?email_addr="+encodeURIComponent(this.emailValue)+"&submit=submit&monetary="+encodeURIComponent(this.monetaryValue)+"&token="+encodeURIComponent(this.tokenValue);
        this.newJs.id=E;
        this.newJs.type="text/javascript";
        var D=document.getElementsByTagName("head")[0];
        D.appendChild(this.newJs)
    };
    function A(H,G,D,F,C,E){
        this.formObj=H;
        this.emailField=D;
        this.error="";
        //Virid - Extra fields to init firstname and last name, in case they are not in the hidden form
        this.firstNameValue;
        this.lastNameValue;
        if(F&&C){
            this.subscribeField=F;
            this.subscribeHas=C
        }else{
            if(F&&F=="_nosubscribefield_"&&!C){
                this.subscribeField=F;
                this.subscribeValue=((E&&E=="optin")?"optin":"optout")
            }
        }
        if(G){this.tokenValue=G}
        if(this.subscribeField&&!this.subscribeValue){
            this.optinDesc=((E&&E=="optin")?"optin":"optout")
        }
        if(this.emailField&&this.subscribeField&&this.tokenValue){
            this.getFormValues()
        }else{
            this.error+="There was a problem with either the email field, subscribe field, token value.\n"
        }
    }
    A.prototype.getFormValues=function(){
        if(typeof (this.formObj.elements)!="undefined"){
            for(var C=0;C<this.formObj.elements.length;C++){
                if(this.emailField&&this.formObj.elements[C].name==this.emailField&&this.formObj.elements[C].type=="hidden"){
                    this.emailValue=this.formObj.elements[C].value
                }
                if(!this.subscribeValue){
                    if(this.subscribeField&&this.formObj.elements[C].name==this.subscribeField){
                        if(this.formObj.elements[C].type=="checkbox"||this.formObj.elements[C].type=="radio"){
                            if(this.formObj.elements[C].value==this.subscribeHas&&this.formObj.elements[C].checked){
                                this.subscribeValue=((this.optinDesc=="optin")?"optin":"optout")
                            }
                        }
                    }
                }
                //Virid Code to capture the first name and last name fields
                //Rather than change the prototype, we are just harcoding the field names -srd
                if(this.formObj.elements[C].name=='first_name'&&this.formObj.elements[C].type=="hidden"){
                    this.firstNameValue=this.formObj.elements[C].value
                }
                if(this.formObj.elements[C].name=='last_name'&&this.formObj.elements[C].type=="hidden"){
                    this.lastNameValue=this.formObj.elements[C].value
                }
            }
        }else{
            this.error+="We could not access form elements.\n"
        }
        if(this.emailValue&&this.subscribeValue&&this.tokenValue){
            this.loadScript()
        }else{
            this.error+="There was a problem with either the email value, subscribe value, token value.\n"
        }
    };
    A.prototype.loadScript=function(){
        var E="ipostScript";
        var C=document.getElementById(E);
        if(C!=null){
            C.parentNode.removeChild(C);
            delete C
        }
        //this.newJs=document.createElement("script");
        this.newJs=document.createElement("iframe");
        //demo
        //this.newJs.src="http://demo.ipost.com/js-subscribe.cgi?email_addr="+encodeURIComponent(this.emailValue)+"&first_name="+encodeURIComponent(this.firstNameValue)+"&last_name="+encodeURIComponent(this.lastNameValue)+"&submit=submit&subscribe="+this.subscribeValue+"&token="+encodeURIComponent(this.tokenValue);
        //prod
        //this.newJs.src="http://conduit.ipost.com/js-subscribe.cgi?email_addr="+encodeURIComponent(this.emailValue)+"&first_name="+encodeURIComponent(this.firstNameValue)+"&last_name="+encodeURIComponent(this.lastNameValue)+"&submit=submit&subscribe="+this.subscribeValue+"&token="+encodeURIComponent(this.tokenValue);
		//prod temp
		this.newJs.src="https://www.ipost.com/bshoes/prefs?email_addr="+this.emailValue+"&first_name="+encodeURIComponent(this.firstNameValue)+"&last_name="+encodeURIComponent(this.lastNameValue)+"&submit=submit&subscribe="+this.subscribeValue;
        this.newJs.id=E;
        this.newJs.type="text/javascript";
        var D=document.getElementsByTagName("head")[0];
        D.appendChild(this.newJs)
    };
    return{Subscribe:A,Autotarget:B}
}
    )();
