function eaErrorHandler(desc, page, line)  {
    PublicDwr.jsError(desc, page, line, Vars.client.browser, Vars.client.version, Vars.client.OS, window.location.href);
    return false;
}
//window.onerror = eaErrorHandler;

function disableButton(btnId, text) {
    var btn = $(btnId);
    disableOnclick(btn);
    disable(btn);
    if (text)
        btn.value = text;
    else
        btn.value = "please wait...";
}

function enableButton(btnId, text) {
    var btn = $(btnId);
    btn.value = text;
    enable(btn);
    enableOnclick(btn);
}

function dwrErrorHandler(msg, ex) {
    dump(ex);
    var desc = "";
    if (ex.javaClassName == "org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException") {
        desc += "Well, it would seem that someone else beat you to the punch and saved their changes before\r\n";
        desc += "you. What are the chances of that happening?!!! This is unfortunate, because now you have\r\n";
        desc += "to reload the page and make your changes again. Sorry about that. Chin up!";
    }
    else if (ex.javaClassName == "com.ea.web.dwr.util.DwrSecurityException") {
        //desc += "Security Exception! Your session may have timed out.";
        // Ignore. Just try to reload the page.
        window.location.reload();
    }
    else if (ex.javaClassName == "com.serotonin.ShouldNeverHappenException") {
        dump(ex);
        desc += "Yikes, this shouldn't have happened: "+ msg;
        eaErrorHandler(msg);
    }
    else {
        dump(ex);
        desc += "An error has occurred.\r\n";
        desc += msg;
        eaErrorHandler(msg);
    }
    
    if (desc != "")
        alert(desc);
}
dwr.engine.setErrorHandler(dwrErrorHandler);

function togglePreview() {
    PublicDwr.togglePreview(function() { location.reload(true); });
}

function preview(targetUrl) {
    PublicDwr.preview(window.location.href, function() { window.location = targetUrl; });
}

function clipImage(mediaId) {
    PublicDwr.clipImage(mediaId, _clipCommon);
    destroyMyProjects();
    return false;
}

function clipProduct(productId) {
    PublicDwr.clipProduct(productId, _clipCommon);
    destroyMyProjects();
    return false;
}

function clipFabric(fabricId) {
    PublicDwr.clipFabric(fabricId, _clipCommon);
    destroyMyProjects();
    return false;
}

function _clipCommon() {
    var self = this;
    var showMyProjectsLink = true;
    
    var copy = 'This item has been added to "my projects"';
    if (Vars.employeeType == "designer")
    {
        copy += ' (designer account)';
        showMyProjectsLink = false;
    }
    else if (Vars.employeeType == "admin")
    {
        copy += ' (corporate boards)';
        showMyProjectsLink = false;
    }
    
    var content = '\
        <div class="form" style="width:auto;">\
          <div class="popup-silo">'+ copy +'</div>\
          <br/><br/><br/>\
          <div class="popup-silo-form">\
            <input id="_mp_closeBtn" type="button" value="close"/>\
            '+ (showMyProjectsLink ? '<input id="_mp_gotoBtn" type="button" class="inverse" value="go to &quot;my projects&quot;"/>' : '') +'\
          </div>\
        </div>';
    
    this.dialog = new Overlay({
        w:325, h:145,
        modal:true, canClose:true, destroyOnClose:true,
        title:'add to my projects', content: content
    });
    
		if(Vars.client.isIE6)
			Form.Dynamic.prettyEverything();
    
		Event.observe($("_mp_closeBtn"), 'click', function() { self.dialog.hide(); });
		if(showMyProjectsLink) {
	    Event.observe($("_mp_gotoBtn"), 'click', function() {
	        self.dialog.hide();
	        launchMyProject();
	    });
	  }
}

function fancyAlert(message, title) {
    if (typeof(title) == "undefined")
        title = 'Please note...';
    new Overlay({
        w:325, h:125,
        modal:true, canClose:true, destroyOnClose:true,
        title: title, content: message
    });
    if (Vars.client.isIE6)
        Form.Dynamic.prettyEverything();
}

function EmailThisPage() {
    var self = this;
    this.toAddrs = new Array();
    this.toAddrs.push({name:"",email:""});
    
    var content = '\
        <div class="form">\
          <div class="content-silo medium" style="margin-top:0px;">\
            <div id="_etp_errorMessage" class="errorMessage" style="display:none;">\
              <h3>please check the following</h3>\
            </div>\
            <h3>to</h3>\
            <div id="_etp_toAddrsDiv">\
            </div>\
            <div class="formbreakshort" style="width:500px;"></div>\
            <h3>from</h3>\
            <div class="input-box floater">\
              <label for="_etp_fromFirstName">your first name</label>\
              <input type="text" id="_etp_fromFirstName" />\
            </div>\
            <div class="input-box floater">\
              <label for="_etp_fromLastName">your last name</label>\
              <input type="text" id="_etp_fromLastName" />\
            </div>\
            <div class="input-box floater">\
              <label for="_etp_fromEmail">your email address</label>\
              <input type="text" id="_etp_fromEmail" />\
            </div>\
            <br/>\
            <div class="formbreakshort" style="width:500px;"></div>\
            <h3>enter your message</h3>\
            <div class="textarea-box">\
              <label for="_etp_message">message</label>\
              <textarea id="_etp_message"></textarea>\
            </div>\
            <div class="check-box floater">\
              <label for="_etp_subscribe">Subscribe to email updates</label>\
              <input type="checkbox" id="_etp_subscribe"/>\
            </div>\
            <div class="input-box floater" id="_etp_subscribeFields" style="display:none">\
              <label for="_etp_postalCode">zip</label>\
              <input type="text" id="_etp_postalCode" />\
            </div>\
            <div class="formbreakshort" style="width:500px;"></div>\
            <input type="button" value="send" id="_etp_sendBtn"/>\
          </div>\
        </div>';
    
    this.dialog = new Overlay({
        w:825, h:525,
        modal:true, canClose:true, destroyOnClose:true,
        title:'email this page',
        content: content
    });
    
    this.drawToAddresses = function() {
        // Create the content
        var content = "";
        for (var i=0; i<this.toAddrs.length; i++) {
            content += '\
                <div class="input-box floater">\
                  <label for="_etp_toEmail'+ i +'">your friend\'s email address</label>\
                  <input type="text" id="_etp_toEmail'+ i +'" value="'+ escapeDQuotes(this.toAddrs[i].email) +'"/>\
                </div>';
            if (i > 0)
                content += '<a href="#" id="_etp_removeToAddrBtn'+ i +'">Remove this friend</a><br/>';
            content += '<div class="formbreakshort" style="width:500px;"></div>';
        }
        
        if (this.toAddrs.length < 3)
            content += '<a href="#" id="_etp_addToAddrBtn">Add another friend</a>';
        
        $set("_etp_toAddrsDiv", content);
        
        // Hook up the events
        var btn;
        for (var i=1; i<this.toAddrs.length; i++) {
            btn = $("_etp_removeToAddrBtn"+ i);
            btn.removeIndex = i;
            Event.observe(btn, 'click', function() { self.removeToAddr(this.removeIndex); return false; });
        }
        if (this.toAddrs.length < 3)
            Event.observe($("_etp_addToAddrBtn"), 'click', this.addToAddr );
        
        Form.Dynamic.prettyEverything();
    };
    
    this.removeToAddr = function(index) {
        self.getToAddressValues();
        self.toAddrs.splice(index, 1);
        self.drawToAddresses();
    };
    
    this.addToAddr = function() {
        self.getToAddressValues();
        self.toAddrs.push({name:"",email:""});
        self.drawToAddresses();
        return false;
    };
    
    this.subscribeClicked = function() {
        display("_etp_subscribeFields", $get("_etp_subscribe"));
        Form.Dynamic.prettyEverything();
    };
    
    this.getToAddressValues = function() {
        for (var i=0; i<this.toAddrs.length; i++)
            this.toAddrs[i].email = $get("_etp_toEmail"+ i);
    };
    
    this.sendEmail = function() {
        disableButton("_etp_sendBtn", "please wait...");
        
        self.getToAddressValues();
        
        var email2 = null, email3 = null;
        if (self.toAddrs.length > 1)
            email2 = self.toAddrs[1].email;
        if (self.toAddrs.length > 2)
            email3 = self.toAddrs[2].email;
        
        PublicDwr.emailThisPage("_etp_", window.location.href, document.title, self.toAddrs[0].email, email2, email3,
                $get("_etp_fromFirstName"), $get("_etp_fromLastName"), $get("_etp_fromEmail"), $get("_etp_message"),
                $get("_etp_subscribe"), $get("_etp_postalCode"), function(response) {
            enableButton("_etp_sendBtn", "send");
            if (response.hasMessages)
                showDwrMessages(response.messages, "_etp_errorMessage");
            else {
                self.dialog.hide();
                
                new Overlay({
                    w:325, h:125,
                    modal:true, canClose:true, destroyOnClose:true,
                    title:'email this page',
                    content: "Your message has been sent. We hope they love it as much as you do!"
                });
            }
        });
    };
    
    Form.Dynamic.prettyEverything();
    Event.observe($("_etp_sendBtn"), 'click', this.sendEmail);
    Event.observe($("_etp_subscribe"), 'click', this.subscribeClicked);
    this.drawToAddresses();
}

function ContactUs(cmtContent) {
    var self = this;
    this.cmtContent = cmtContent;
    
    var content = '\
        <div class="form" style="width:auto;">\
          <div class="popup-silo" style="padding-right:30px;">'+ cmtContent.copy +'\
            <h3>'+ cmtContent.userFieldHeader +'</h3>\
            <div class="input-box">\
              <label for="_cu_name">your name</label>\
              <input type="text" id="_cu_name" />\
            </div>\
            <div class="input-box">\
              <label for="_cu_email">your email address</label>\
              <input type="text" id="_cu_email" />\
            </div>\
            <div id="_cu_errorMessage" class="errorMessage" style="display:none;">\
              <h3>please check the following</h3>\
            </div>\
          </div>\
          <div class="popup-silo-form">\
            <h3>'+ cmtContent.topicFieldHeader +'</h3>\
            <div class="select-box">\
              <label for="_cu_topic">topic</label>\
              <select id="_cu_topic"><option value="0">Select one</option></select>\
            </div>\
            <h3>'+ cmtContent.messageFieldHeader +'</h3>\
            <div class="textarea-box" style="width:240px">\
              <label for="_cu_message">message</label>\
              <textarea id="_cu_message"></textarea>\
            </div>\
            <div class="check-box">\
              <label for="_cu_subscribe">subscribe to email updates</label>\
              <input type="checkbox" id="_cu_subscribe"/>\
            </div>\
            <div class="input-box" id="_cu_subscribeFields" style="display:none">\
              <label for="_cu_postalCode">zip</label>\
              <input type="text" id="_cu_postalCode" />\
            </div>\
            <div class="formbreakshort" style="width:240px;"></div>\
            <input type="button" value="send" id="_cu_sendBtn"/>\
          </div>\
        </div>';
    
    this.dialog = new Overlay({
        w:575, h:500,
        modal:true, canClose:true, destroyOnClose:true,
        title: cmtContent.title,
        content: content
    });
    
    this.subscribeClicked = function() {
        display("_cu_subscribeFields", $get("_cu_subscribe"));
        Form.Dynamic.prettyEverything();
    };
    
    this.sendEmail = function() {
        PublicDwr.submitContactUs($get("_cu_name"), $get("_cu_email"), $get("_cu_topic"), $get("_cu_message"),
                $get("_cu_subscribe"), $get("_cu_postalCode"), function(response) {
            if (response.hasMessages)
                showDwrMessages(response.messages, "_cu_errorMessage");
            else {
                self.dialog.hide();
                
                new Overlay({
                    w:325, h:125,
                    modal:true, canClose:true, destroyOnClose:true,
                    title: self.cmtContent.title,
                    content: self.cmtContent.confirmationMessage
                });
            }
        });
    };
    
    // Populate the select box.
    dwr.util.addOptions("_cu_topic", cmtContent.topics, "contactUsTopicId", "label");
    
    Form.Dynamic.prettyEverything();
    Event.observe($("_cu_sendBtn"), 'click', this.sendEmail);
    Event.observe($("_cu_subscribe"), 'click', this.subscribeClicked);
}

function RequestToBeRemoved(cmtContent) {
    var self = this;
    this.cmtContent = cmtContent;
    
    var content = '\
        <div class="form" style="width:auto;">\
          <div class="popup-silo" style="padding-right:30px;">'+ cmtContent.copy +'\
            <h3>'+ cmtContent.sampleImageCaption +'</h3><br>\
            <img src="'+ (cmtContent.sampleLabelImage ? s7mediaUrl(cmtContent.sampleLabelImage) : '/logo.gif') +'"/>\
          </div>\
          <div class="popup-silo-form">\
            <h3>'+ cmtContent.fieldHeader +'</h3><br>\
            <div id="_rbr_errorMessage" class="errorMessage" style="display:none;">\
              <h3>please check the following</h3>\
            </div>\
            <div class="input-box">\
              <label for="_rbr_code">code</label>\
              <input type="text" id="_rbr_code" />\
            </div>\
            <div class="input-box">\
              <label for="_rbr_firstName">first name</label>\
              <input type="text" id="_rbr_firstName" />\
            </div>\
            <div class="input-box">\
              <label for="_rbr_lastName">last name</label>\
              <input type="text" id="_rbr_lastName" />\
            </div>\
            <div class="input-box">\
              <label for="_rbr_address">address</label>\
              <input type="text" id="_rbr_address" />\
            </div>\
            <div class="input-box">\
              <label for="_rbr_city">city</label>\
              <input type="text" id="_rbr_city" />\
            </div>\
            <div class="select-box" style="width:120px">\
              <label for="_rbr_state">state/province</label>\
              <select id="_rbr_state"><option></option></select>\
            </div>\
            <div class="input-box" style="width:120px">\
              <label for="_rbr_zip">zip/postal code</label>\
              <input type="text" id="_rbr_zip" />\
            </div>\
            <div class="formbreakshort" style="width:240px;"></div>\
            <input type="button" value="submit" id="_rbr_submitBtn"/>\
          </div>\
        </div>';
    
    this.dialog = new Overlay({
        w:575, h:525,
        modal:true, canClose:true, destroyOnClose:true,
        title: cmtContent.title,
        content: content
    });
    
    this.submit = function() {
        PublicDwr.requestToBeRemoved($get("_rbr_code"), $get("_rbr_firstName"), $get("_rbr_lastName"),
                $get("_rbr_address"), $get("_rbr_city"), $get("_rbr_state"), $get("_rbr_zip"), function(response) {
            if (response.hasMessages)
                showDwrMessages(response.messages, "_rbr_errorMessage");
            else {
                self.dialog.hide();
                
                new Overlay({
                    w:500, h:200,
                    modal:true, canClose:true, destroyOnClose:true,
                    title: self.cmtContent.title,
                    content: self.cmtContent.confirmationMessage
                });
            }
        });
    };
    
    // Populate the select box.
    PublicDwr.requestToBeRemovedShippingRegions(function(regions) {
        dwr.util.addOptions("_rbr_state", regions, "regionId", "name");
        Form.Dynamic.drawSelectOptions($("_rbr_state"), true);
    });
    
    Form.Dynamic.prettyEverything();
    Event.observe($("_rbr_submitBtn"), 'click', this.submit);
}

function PublicationRequest(publication, cmtContent) {
    var self = this;
    this.publication = publication;
    this.cmtContent = cmtContent;
    
    var content = '\
        <div class="form" style="width:auto;">\
          <div class="popup-silo" style="padding-right:30px;">'+ cmtContent.copy +'\
	          <h3>'+ publication.title +'</h3>\
	          <div>'+ publication.prettyDate +'</div>\
	          <img src="'+ s7mediaUrl(publication.thumbnail) +'?wid=110" width="110" />\
	        </div>\
	        <div class="popup-silo-form">\
		        <h3>'+ cmtContent.fieldHeader +'</h3>\
		        <div id="_pr_errorMessage" class="errorMessage" style="display:none;">\
		          <h3>please check the following</h3>\
		        </div>\
		        <div class="input-box">\
		          <label for="_pr_firstName">first name</label>\
		          <input type="text" id="_pr_firstName" />\
		        </div>\
		        <div class="input-box">\
		          <label for="_pr_lastName">last name</label>\
		          <input type="text" id="_pr_lastName" />\
		        </div>\
		        <div class="input-box">\
		          <label for="_pr_address1">address</label>\
		          <input type="text" id="_pr_address1" />\
		        </div>\
		        <div class="input-box">\
		          <label for="_pr_address2"></label>\
		          <input type="text" id="_pr_address2" />\
		        </div>\
		        <div class="input-box">\
		          <label for="_pr_city">city</label>\
		          <input type="text" id="_pr_city" />\
		        </div>\
		        <div class="select-box" style="width:120px">\
		          <label for="_pr_state">state</label>\
		          <select id="_pr_state"><option></option></select>\
		        </div>\
		        <div class="input-box" style="width:120px">\
		          <label for="_pr_zip">zip</label>\
		          <input type="text" id="_pr_zip" />\
		        </div>\
		        <div class="input-box" style="width:120px">\
		          <label for="_pr_phone">phone</label>\
		          <input type="text" id="_pr_phone" />\
		        </div>\
		        <div class="input-box">\
		          <label for="_pr_email">email</label>\
		          <input type="text" id="_pr_email" />\
		        </div>\
		        <div class="check-box">\
		          <label for="_pr_subscribe">subscribe to email updates</label>\
		          <input type="checkbox" id="_pr_subscribe"/>\
		        </div>\
		        <div class="formbreakshort" style="width:240px;"></div>\
		        <input type="button" value="submit" id="_pr_submitBtn"/>\
		      </div>\
        </div>';
    
    this.dialog = new Overlay({
        w:575, h:575,
        modal:true, canClose:true, destroyOnClose:true,
        title: cmtContent.title,
        content: content
    });
    
    this.submit = function() {
        PublicDwr.publicationRequest(publication.sku, $get("_pr_firstName"), $get("_pr_lastName"),
                $get("_pr_address1"), $get("_pr_address2"), $get("_pr_city"), $get("_pr_state"), $get("_pr_zip"),
                $get("_pr_phone"), $get("_pr_email"), $get("_pr_subscribe"), function(response) {
            if (response.hasMessages)
                showDwrMessages(response.messages, "_pr_errorMessage");
            else {
                self.dialog.hide();
                
                new Overlay({
                    w:325, h:125,
                    modal:true, canClose:true, destroyOnClose:true,
                    title: self.cmtContent.title,
                    content: self.cmtContent.confirmationMessage
                });
            }
        });
    };
    
    // Populate the select box.
    PublicDwr.shippingRegions(function(regions) {
        dwr.util.addOptions("_pr_state", regions, "code", "name");
        Form.Dynamic.drawSelectOptions($("_pr_state"), true);
    });
    
    Form.Dynamic.prettyEverything();
    Event.observe($("_pr_submitBtn"), 'click', this.submit);
}

function SwatchRequest(fabricArray, cmtContent) {
    var self = this;
    this.fabricArray = fabricArray;
    this.cmtContent = cmtContent;
    
    var content = '\
        <div id="swatchrequest" class="form" style="width:auto;">\
          <div class="popup-silo" style="padding-right:30px;">'+ cmtContent.copy +'\
	          <h3>'+ cmtContent.swatchHeader + (fabricArray.length > 10 ? ' (up to 10)' : '') + '</h3>\
	          <div class="content-silo fabriccontent">\
	            <div class="carousel fabriclist">\
		            <ul><li>';
						    fabricArray.each(function(item, index) {
	                if (index > 0 && (index % 4) == 0)
	                  content += '</li><li>';
	                content += '<div alt="' + item.name + '" title="' + item.name + '"><input type="checkbox" name="_sr_fabricId" value="'+ item.fabricId +'"/> ';
	                content += '<img src="' + s7mediaUrl(item.image) +'?crop=0,0,295,295&size=50,50" width="50" height="50" border="0" /></div>';
	              });
	              content += '</li></ul>\
	            </div>\
              <div class="slidertrack trackhorizontal"><div class="sliderhandle handlehorizontal"></div></div>\
	          </div>\
          </div>\
          <div class="popup-silo-form">\
            <h3>'+ cmtContent.fieldHeader +'</h3>\
            <div id="_sr_errorMessage" class="errorMessage" style="display:none;">\
              <h3>please check the following</h3>\
            </div>\
            <div class="input-box">\
              <label for="_sr_firstName">first name</label>\
              <input type="text" id="_sr_firstName" />\
            </div>\
            <div class="input-box">\
              <label for="_sr_lastName">last name</label>\
              <input type="text" id="_sr_lastName" />\
            </div>\
            <div class="input-box">\
              <label for="_sr_address1">address</label>\
              <input type="text" id="_sr_address1" />\
            </div>\
            <div class="input-box">\
              <label for="_sr_address2"></label>\
              <input type="text" id="_sr_address2" />\
            </div>\
            <div class="input-box">\
              <label for="_sr_city">city</label>\
              <input type="text" id="_sr_city" />\
            </div>\
            <div class="select-box" style="width:120px">\
              <label for="_sr_state">state</label>\
              <select id="_sr_state"><option></option></select>\
            </div>\
            <div class="input-box" style="width:120px">\
              <label for="_sr_zip">zip</label>\
              <input type="text" id="_sr_zip" />\
            </div>\
            <div class="input-box" style="width:120px">\
              <label for="_sr_phone">phone</label>\
              <input type="text" id="_sr_phone" />\
            </div>\
            <div class="input-box">\
              <label for="_sr_email">email</label>\
              <input type="text" id="_sr_email" />\
            </div>\
            <div class="check-box">\
              <label for="_sr_subscribe">subscribe to email updates</label>\
              <input type="checkbox" id="_sr_subscribe"/>\
            </div>\
            <div class="formbreakshort" style="width:240px;"></div>\
            <input type="button" value="submit" id="_sr_submitBtn"/>\
          </div>\
        </div>';

    this.dialog = new Overlay({
        w:620, h:550,
        modal:true, canClose:true, destroyOnClose:true, allowscroll:false,
        title: cmtContent.title,
        content: content
    });
    
    var swatchBrowser = $('swatchrequest');
    swatchBrowser.sliderTrack = swatchBrowser.down('.slidertrack');
    if (fabricArray.length > 16) {
	    swatchBrowser.sliderHandle = swatchBrowser.down('.sliderhandle');
	    swatchBrowser.optionList = swatchBrowser.down('.carousel').down('ul');
	    swatchBrowser.optionListWidth = (swatchBrowser.optionList.childElements().length * 60) - 10; //60 is the width of one thumbnail plus 10 left margin
	    swatchBrowser.slider = new Control.Slider(swatchBrowser.sliderHandle, swatchBrowser.sliderTrack, {
	        axis: "horizontal",
	        //range: $R(0, ul.offsetHeight),
	        //sliderValue: 0,
	        onSlide: function(value) {
	            swatchBrowser.optionList.setStyle('left:' + (Math.floor(value/swatchBrowser.slider.maximum*(230-swatchBrowser.optionListWidth)) + 'px'));
	        },
	        onChange: function(value) {
	            swatchBrowser.optionList.setStyle('left:' + (Math.floor(value/swatchBrowser.slider.maximum*(230-swatchBrowser.optionListWidth)) + 'px'));
	        }
	    });
    }
    else
	    swatchBrowser.sliderTrack.hide();

    this.submit = function() {
        PublicDwr.swatchRequest($get("_sr_fabricId"), $get("_sr_firstName"), $get("_sr_lastName"), $get("_sr_address1"),
                $get("_sr_address2"), $get("_sr_city"), $get("_sr_state"), $get("_sr_zip"), $get("_sr_phone"),
                $get("_sr_email"), $get("_sr_subscribe"), function(response) {
            if (response.hasMessages)
                showDwrMessages(response.messages, "_sr_errorMessage");
            else {
                self.dialog.hide();
                
                new Overlay({
                    w:325, h:125,
                    modal:true, canClose:true, destroyOnClose:true,
                    title: self.cmtContent.title,
                    content: self.cmtContent.confirmationMessage
                });
            }
        });
    };
    
    // Populate the select box.
    PublicDwr.shippingRegions(function(regions) {
        dwr.util.addOptions("_sr_state", regions, "code", "name");
        Form.Dynamic.drawSelectOptions($("_sr_state"), true);
    });
    
    Form.Dynamic.prettyEverything();
    Event.observe($("_sr_submitBtn"), 'click', this.submit);
}

function Registration(cmtContent, registerTask, roomPlannerCall) {
    var self = this;
    this.cmtContent = cmtContent;
    this.registerTask = registerTask;
    this.content = null;
    this.dialog = null;
    this.promo = null;
    
    this.close = function() {
        self.dialog.hide();
    };
    
    this.submit = function() {
        disableButton("_reg_submitBtn", "please wait...");
        
        var subscribe = $get("_reg_subscribe");
        var first = $get("_reg_firstName");
        var last = $get("_reg_lastName");
        
        var promoSignup = false;
        var promoId = null;
        if (self.promo) {
            promoSignup = $get("_reg_promoAccept");
            promoId = self.promo.pointsPromotionId;
        }
        
        SigninDwr.register(first, last, $get("_reg_email"), $get("_reg_confirmEmail"), $get("_reg_regPassword"),
                $get("_reg_confirmPassword"), $get("_reg_subscribe"), $get("_reg_postalCode"), roomPlannerCall,
                promoSignup, promoId, function(response) {
            if (response.hasMessages) {
                enableButton("_reg_submitBtn", "register");
                showDwrMessages(response.messages, "errorMessageRegister");
            }
            else {
                 pageTracker._trackPageview('/vpv/goals/registration/success/');
                var content = '\
                    <div class="form" style="width:auto;">\
                      <div class="popup-silo">\
                        Fabulous! Your account has been created. An email has been sent to you confirming your registration.\
                      </div>\
                      <div class="popup-silo-form">';
                
                if (response.data.loginTask)
                    content += '<input type="button" value="continue" onclick="window.location = \''+ response.data.loginTask +'\'"/>';
                else if (self.registerTask)
                    content += '<input type="button" value="continue" onclick="window.location = \''+ registerTask +'\'"/>';
                else
                    content += '<input type="button" value="close" id="_reg_closeBtn"/>\
                        <input type="button" value="go to &quot;my account&quot;" onclick="window.location = \'/account/account_info\'"/>';
                
                content += '</div>\
                    </div>';
                
                // Open the confirmation layer.
                self.dialog.title("welcome, "+ first +" "+ last);
                self.dialog.setContent(content);
                
                Event.observe($("_reg_closeBtn"), 'click', self.close);
            }
        });
    };
    
    PublicDwr.getAvailablePointsPromotion(function(promo) {
        self.promo = promo;
        
        var content = '\
            <div class="form" style="width:auto;">\
              <div class="popup-silo" style="padding-right:30px;">';
				
        if (promo) {
            content += (cmtContent.promoBanner ? '<img src="'+ s7mediaUrl(cmtContent.promoBanner) +'"/><br/><br/>' : '');
				}

        content += '\
                <h3>'+ cmtContent.header +'</h3>\
                '+ cmtContent.copy;
               
        if (promo) {
            content += '\
                <h3>'+ cmtContent.promoTitle +'</h3>\
                <div class="check-box">\
                  <label for="_reg_promoAccept">'+ cmtContent.promoEnroll +'</label>\
                  <input type="checkbox" id="_reg_promoAccept" checked="checked"/>\
                </div>\
                '+ cmtContent.promoCopy;
        }
        
        content += '\
              </div>\
              <div class="popup-silo-form">\
                <div id="errorMessageRegister" class="errorMessage" style="display:none;">\
                  <h3>please check the following</h3>\
                </div>\
                <div class="input-box">\
                  <label for="firstName">first name</label>\
                  <input type="text" id="_reg_firstName" />\
                </div>\
                <div class="input-box">\
                  <label for="lastName">last name</label>\
                  <input type="text" id="_reg_lastName" />\
                </div>\
                <div class="input-box">\
                  <label for="email">email address</label>\
                  <input type="text" id="_reg_email" />\
                </div>\
                <div class="input-box">\
                  <label for="confirmEmail">confirm email address</label>\
                  <input type="text" id="_reg_confirmEmail" />\
                </div>\
                <div class="input-box">\
                  <label for="regPassword">password</label>\
                  <input type="password" id="_reg_regPassword" name="regPassword" />\
                </div>\
                <div class="input-box">\
                  <label for="confirmPassword">confirm password</label>\
                  <input type="password" id="_reg_confirmPassword" name="confirmPassword" />\
                </div>\
                <div class="input-box" id="subscribeFields">\
                  <label for="postalCode">zip</label>\
                  <input type="text" id="_reg_postalCode" />\
                </div>\
                <div class="check-box">\
                  <label for="_reg_subscribe">Subscribe to email updates</label>\
                  <input type="checkbox" id="_reg_subscribe" checked="checked"/>\
                </div>\
                <div class="formbreakshort" style="width:240px;"></div>\
                <input type="button" value="register" id="_reg_submitBtn"/>\
              </div>\
            </div>';
        
        if (promo && cmtContent.legalCopy)
            content += '<div style="clear:both; padding-top:10px;">'+ cmtContent.legalCopy +'</div>';
        
        self.dialog = new Overlay({
            w:600, h:425,
            modal:true, canClose:true, destroyOnClose:true,
            title: cmtContent.title,
            content: content
        });
    
        Form.Dynamic.prettyEverything();
        Event.observe($("_reg_submitBtn"), 'click', self.submit);
    });
}

function s7mediaUrl(media) {
    if (Vars.https)
        return media.secureUrl;
    return media.url;
}

function signout() {
    PublicDwr.signout(function() {
        var url = window.location.href;
        var del = "?";
        if (url.indexOf("?") != -1)
            del = "&";
        window.location = url + del +"signout=true";
    });
    return false;
}

function pointsPromoMessage(promotionId) {
    new PointsPromoMessage(null, promotionId, null, true);
}

function PointsPromoMessage(cmtContent, promotionId, redirect, mp) {
    var self = this;
    this.promotionId = promotionId;
    this.redirect = redirect;
    this.mp = mp;
    this.dialog = null;
    
    this.init = function(cmtContent) {
        var content = '\
            <div class="form" style="width:auto;">\
              <div class="popup-silo-form" style="width:350px;">\
							' + (cmtContent.promoBanner ? '<img src="'+ s7mediaUrl(cmtContent.promoBanner) +'"/><br/><br/>' : '') + '\
								' + cmtContent.promoCopy + '\
                <div class="floater">\
                  <input id="promoBtn1" type="button" value="'+ cmtContent.promoYes +'"/>\
                  <input id="promoBtn2" type="button" value="'+ cmtContent.promoNo +'"/>\
                  <input id="promoBtn3" type="button" value="'+ cmtContent.promoLater +'"/>\
                </div>\
              </div>\
            </div>\
            <div style="clear:both; padding-top:10px;">'+ cmtContent.promoLegal +'</div>';
        
        self.dialog = new Overlay({
            w:400, h:450, zIndex:498,
            modal:true, canClose:true, destroyOnClose:true,
            title: cmtContent.promoTitle,
            content: content,
            hidecallback: self.tryRedirect
        });
        Form.Dynamic.prettyEverything();
        
        Event.observe($("promoBtn1"), 'click', function() { self.submit(1); });
        Event.observe($("promoBtn2"), 'click', function() { self.submit(2); });
        Event.observe($("promoBtn3"), 'click', function() { self.submit(3); });
    };
    
    this.submit = function(answer) {
        disableButton("promoBtn1", $("promoBtn1").value);
        disableButton("promoBtn2", $("promoBtn2").value);
        disableButton("promoBtn3", $("promoBtn3").value);
    
        if (answer == 1) { // yes
            PublicDwr.addPointsPromotionCustomer(self.promotionId, true);
            if (!this.redirect)
                this.redirect = "/account/rewards";
        }
        else if (answer == 2) // no
            PublicDwr.addPointsPromotionCustomer(self.promotionId, false);
        // No need to do anything for "ask me later"
        
        self.dialog.hide();
        self.tryRedirect();
    }
    
    this.tryRedirect = function() {
        if (!mp) {
            if (self.redirect)
                window.location = self.redirect;
            else
                window.location = "/account/account_info";
        }
    }
    
    if (cmtContent)
        this.init(cmtContent);
    else {
        PublicDwr.getContent("com.ea.web.model.content.SigninPage", function(result) {
            self.init(result);
        });
    }
}

