// ACH Foam Customizations
// V1.0
// CLARK 8/2011
//
_tag.dcsCustom = function () {

    // the intention here is to put together a mini CRm system
    // using WTOD as the data colleciton tool
    // we could send this data as seperate payloads and build some nice report
    // but the end-game is to pull it into excel using rest
    // so to minimize the cost (server calls) it makes sense to sent the data all
    // as one payload, understanding that the drawback is flexibility with reports
    // in WTOD
    // look up the plain text labels for the input box ctl codes
    getLabel = function (tag) {

        labels = document.getElementsByTagName('label');
        for (var c = 0; c < labels.length; c++) {
            if (labels[c].htmlFor == tag) return labels[c].innerHTML;
        }
        return null;
    }

    // this is an event function ths is bound to the global onClick event for the page
    // the bubble order is insignificant
    // we call this when any on click event occurs on the page
    OnSubmitClick = function (event) {

        event = event || window.event;
        var e = event.target || event.srcElement;

        var track = new Array();

        // look to see if its an INPUT event, and if so the ContactSend submit button
        if (e.nodeName == 'INPUT' && (e.id.indexOf('ContactSend', 0) > -1 || e.id.indexOf('WorkshopSend', 0) > -1 || e.value == 'Sign Up')) {

            // this is a table of the input tag id elements
            // and their corresponding webtrends tag names
            // we also use this to transcent into the DOM to pull the
            // form values out
            //
            // if the site changes these values will need to be updated
            // the are in the form of Webtrends tag name : HTML Tag name
            //
            var formValues = {
                contact: {
                    firstName: 'ctl00$ContentPlaceHolder1$txtFName',
                    lastName: 'ctl00$ContentPlaceHolder1$txtLName',
                    email: 'ctl00$ContentPlaceHolder1$txtEmail',
                    phone: 'ctl00$ContentPlaceHolder1$txtPhone',
                    company: 'ctl00$ContentPlaceHolder1$txtCompany',
                    title: 'ctl00$ContentPlaceHolder1$txtTitle',
                    address: 'ctl00$ContentPlaceHolder1$txtAddress',
                    city: 'ctl00$ContentPlaceHolder1$txtCity',
                    state: 'ctl00$ContentPlaceHolder1$ddState',
                    zip: 'ctl00$ContentPlaceHolder1$txtZip',
                    comments: 'ctl00$ContentPlaceHolder1$txtComments'
                },
                infoRequest: {
                    Packaging: {
                        FabEPS: 'ctl00$ContentPlaceHolder1$cbxPackagingFabricatedEPS',
                        CustomMoldEPS: 'ctl00$ContentPlaceHolder1$cbxPackagingCustomMoldedEPS',
                        CustomMoldEPE: 'ctl00$ContentPlaceHolder1$cbxPackagingCustomMoldedEPE',
                        Containers: 'ctl00$ContentPlaceHolder1$cbxPackagingDuraThermInsulatedShippingContainers',
                        WineLoc: 'ctl00$ContentPlaceHolder1$cbxWineLocWinePackaging',
                        BoxLiners: 'ctl00$ContentPlaceHolder1$cbxBoxLiners',
                        Peanuts: 'ctl00$ContentPlaceHolder1$cbxPackagingLooseFillPeanuts',
                        FlexibleFoam: 'ctl00$ContentPlaceHolder1$cbxPackagingFlexibleFoam',
                        FoamFoundry: 'ctl00$ContentPlaceHolder1$cbxPackagingLostFoamFoundryPatterns',
                        RVInsulation: 'ctl00$ContentPlaceHolder1$cbxPackagingRVInsulation',
                        DoorInsulation: 'ctl00$ContentPlaceHolder1$cbxPackagingGarageDoorInsulation',
                        Other: 'ctl00$ContentPlaceHolder1$cbxPackagingOther'
                    },
                    Geofoam: {
                        Products: 'ctl00$ContentPlaceHolder1$cbxProductsGeofoam',
                        RoofInstall: 'ctl00$ContentPlaceHolder1$cbxProductsRoofInsulation',
                        Panels: 'ctl00$ContentPlaceHolder1$cbxProductsStructuralInsulatedPanels',
                        Forms: 'ctl00$ContentPlaceHolder1$cbxProductsInsulatedConcreteForms',
                        BelowGrade: 'ctl00$ContentPlaceHolder1$cbxProductsBelowGrade',
                        Underlayment: 'ctl00$ContentPlaceHolder1$cbxProductsShethingSidingUnderlayment',
                        ProductEIFS: 'ctl00$ContentPlaceHolder1$cbxProductsEIFS',
                        Architectural: 'ctl00$ContentPlaceHolder1$cbxProductsArchitectural',
                        WallApplication: 'ctl00$ContentPlaceHolder1$cbxProductsWallApplication',
                        Other: 'ctl00$ContentPlaceHolder1$cbxProductsOther'
                    },
                    Education: {
                        About_ACH: 'ctl00$ContentPlaceHolder1$cbxContactWorkshop1',
                        EPS_FrostLine_Roof: 'ctl00$ContentPlaceHolder1$cbxContactWorkshop2',
                        Geofoam: 'ctl00$ContentPlaceHolder1$cbxContactWorkshop3',
                        Roof_Insulation: 'ctl00$ContentPlaceHolder1$cbxContactWorkshop4',
                        Structural_Insulation: 'ctl00$ContentPlaceHolder1$cbxContactWorkshop5',
                        Insulated_Concrete: 'ctl00$ContentPlaceHolder1$cbxContactWorkshop6',
                        Wather_Absorption: 'ctl00$ContentPlaceHolder1$cbxContactWorkshop7',
                        Green_Building: 'ctl00$ContentPlaceHolder1$cbxContactWorkshop8',
                        EPS_Packaging: 'ctl00$ContentPlaceHolder1$cbxContactWorkshop9',
                        Sustainable_Packaging: 'ctl00$ContentPlaceHolder1$cbxContactWorkshop10',
                        Other: 'ctl00$ContentPlaceHolder1$cbxContactWorkshop11'
                    },
                    NewsLetter: {
                        Construction: 'ctl00$ContentPlaceHolder1$cbxNewsletterConstruction',
                        Packaging: 'ctl00$ContentPlaceHolder1$cbxNewsletterPackaging',
                        Vinyards: 'ctl00$ContentPlaceHolder1$cbxNewsletterVineyards'
                    }
                },
                sidebar: {
                    firstName: 'sidebarNewsletterSignup_FirstName',
                    lastName: 'sidebarNewsletterSignup_LastName',
                    email: 'sidebarNewsletterSignup_Email'
                },
                sidebarTopic: {
                    Construction: 'sidebarNewsletterSignup_NewsletterTopic0',
                    Packaging: 'sidebarNewsletterSignup_NewsletterTopic1',
                    Vinyards: 'sidebarNewsletterSignup_NewsletterTopic2'
                },
                contentWorkshops: {
                     contact: {
	                    firstName: 'ctl00$ContentPlaceHolder1$txtWorkshopFName',
	                    lastName: 'ctl00$ContentPlaceHolder1$txtWorkshopLName',
	                    email: 'ctl00$ContentPlaceHolder1$txtWorkshopEmail',
	                    phone: 'ctl00$ContentPlaceHolder1$txtWorkshopPhone',
	                    company: 'ctl00$ContentPlaceHolder1$txtWorkshopCompany',
	                    title: 'ctl00$ContentPlaceHolder1$txtWorkshopTitle',
	                    address: 'ctl00$ContentPlaceHolder1$txtWorkshopAddress',
	                    city: 'ctl00$ContentPlaceHolder1$txtWorkshopCity',
	                    state: 'ctl00$ContentPlaceHolder1$ddWorkshopState',
	                    zip: 'ctl00$ContentPlaceHolder1$txtWorkshopZip',
	                    comments: 'ctl00$ContentPlaceHolder1$txtWorkshopComments'
                },
										info: {
	                      About_ACH: 'ctl00$ContentPlaceHolder1$cbxWorkshop1',
                        EPS_FrostLine_Roof: 'ctl00$ContentPlaceHolder1$cbxWorkshop2',
                        Geofoam: 'ctl00$ContentPlaceHolder1$cbxWorkshop3',
                        Roof_Insulation: 'ctl00$ContentPlaceHolder1$cbxWorkshop4',
                        Structural_Insulation: 'ctl00$ContentPlaceHolder1$cbxWorkshop5',
                        Insulated_Concrete: 'ctl00$ContentPlaceHolder1$cbxWorkshop6',
                        Wather_Absorption: 'ctl00$ContentPlaceHolder1$cbxWorkshop7',
                        Green_Building: 'ctl00$ContentPlaceHolder1$cbxWorkshop8',
                        EPS_Packaging: 'ctl00$ContentPlaceHolder1$cbxWorkshop9',
                        Sustainable_Packaging: 'ctl00$ContentPlaceHolder1$cbxWorkshop10',
                        Other: 'ctl00$ContentPlaceHolder1$cbxWorkshop11'
                }
              }

            }
            if (e.value == 'Sign Up') {
                var myContact = '';
                for (val in formValues.sidebar) {
                    a = document.getElementsByName(formValues.sidebar[val]);
                    if (a.length > 0 && a.item(0).value != '' && val != 'comments') {
                        a.item(0).value.replace(":", "");
                        a.item(0).value.replace(";", "");
                        if (myContact == '') myContact = val + ':' + a.item(0).value;
                        else myContact = myContact + ";" + val + ":" + a.item(0).value;
                    }
                }
                myContact += ";date:" + new Date().toMMDDYYYYString();

                track.push('WT.frm_contact');
                track.push(myContact);

                for (val in formValues.sidebarTopic) {
                    a = document.getElementsByName(formValues.sidebarTopic[val]);
                    if (a.length > 0 && a.item(0).checked == true) {
                        track.push('WT.frm_NewsLetter_' + val);
                        track.push('1');
                    }
                }
                // don't record this as a page view -- but as a form post (submit) event
                track.push('WT.dl');
                track.push('27');

                // sent it all to webtrends
                _tag.dcsMultiTrack.apply(_tag, track);

            }


						if (e.id.indexOf('ContactSend', 0) > -1) {
                // create our key from email addy
                a = document.getElementsByName(formValues.contact['email']);
                if (a.length > 0 && a.item(0).value != '') {
                    track.push('WT.frm_email');
                    track.push(a.item(0).value);
                }

                // aggragate the rest of the data as the second dimension
                // we'll parse this back out in excel
                var myContact = '';
                for (val in formValues.contact) {
                    a = document.getElementsByName(formValues.contact[val]);
                    if (a.length > 0 && a.item(0).value != '' && val != 'comments') {
                        a.item(0).value.replace(":", "");
                        a.item(0).value.replace(";", "");
                        if (myContact == '') myContact = val + ':' + a.item(0).value;
                        else myContact = myContact + ";" + val + ":" + a.item(0).value;
                    }
                }
                myContact += ";date:" + new Date().toMMDDYYYYString();

                track.push('WT.frm_contact');
                track.push(myContact);

                // Create the comments record
                // we'll parse this back out in excel
                var myComments = '';
                for (val in formValues.contact) {
                    a = document.getElementsByName(formValues.contact[val]);
                    if (a.length > 0 && a.item(0).value != '' && (val == 'comments' || val == 'email')) {
                        a.item(0).value.replace(":", "");
                        a.item(0).value.replace(";", "");
                        if (myComments == '') myComments = val + ':' + a.item(0).value;
                        else myComments = myComments + ";" + val + ":" + a.item(0).value;
                    }
                }
                myComments += ";date:" + new Date().toMMDDYYYYString();

                track.push('WT.frm_comments');
                track.push(myComments);

                // queue up all of the infoRequest
                for (area in formValues.infoRequest) {
                    for (val in formValues.infoRequest[area]) {
                        a = document.getElementsByName(formValues.infoRequest[area][val]);
                        if (a.length > 0 && a.item(0).checked == true) {
                            track.push('WT.frm_' + area + '_' + val);
                            track.push('1');
                        }
                    }
                }

                // don't record this as a page view -- but as a form post (submit) event
                track.push('WT.dl');
                track.push('27');

                // sent it all to webtrends
                _tag.dcsMultiTrack.apply(_tag, track);

            }


						if (e.id.indexOf('WorkshopSend', 0) > -1) {
                // create our key from email addy
                a = document.getElementsByName(formValues.contentWorkshops.contact['email']);
                if (a.length > 0 && a.item(0).value != '') {
                    track.push('WT.frm_email');
                    track.push(a.item(0).value);
                }

                // aggragate the rest of the data as the second dimension
                // we'll parse this back out in excel
                var myContact = '';
                for (val in formValues.contact) {
                    a = document.getElementsByName(formValues.contentWorkshops.contact[val]);
                    if (a.length > 0 && a.item(0).value != '' && val != 'comments') {
                        a.item(0).value.replace(":", "");
                        a.item(0).value.replace(";", "");
                        if (myContact == '') myContact = val + ':' + a.item(0).value;
                        else myContact = myContact + ";" + val + ":" + a.item(0).value;
                    }
                }
                myContact += ";date:" + new Date().toMMDDYYYYString();

                track.push('WT.frm_contact');
                track.push(myContact);

                // Create the comments record
                // we'll parse this back out in excel
                var myComments = '';
                for (val in formValues.contact) {
                    a = document.getElementsByName(formValues.contentWorkshops.contact[val]);
                    if (a.length > 0 && a.item(0).value != '' && (val == 'comments' || val == 'email')) {
                        a.item(0).value.replace(":", "");
                        a.item(0).value.replace(";", "");
                        if (myComments == '') myComments = val + ':' + a.item(0).value;
                        else myComments = myComments + ";" + val + ":" + a.item(0).value;
                    }
                }
                myComments += ";date:" + new Date().toMMDDYYYYString();

                track.push('WT.frm_comments');
                track.push(myComments);

                // queue up all of the infoRequest
                for (val in formValues.contentWorkshops.info) {
                    a = document.getElementsByName(formValues.contentWorkshops.info[val]);
                    if (a.length > 0 && a.item(0).checked == true) {
                        track.push('WT.frm_contentWorkshops_' + val);
                        track.push('1');
                    }
                }
                // don't record this as a page view -- but as a form post (submit) event
                track.push('WT.dl');
                track.push('27');

                // sent it all to webtrends
                _tag.dcsMultiTrack.apply(_tag, track);

            }
            
            
            
        }
    }

    // bind to the events
    var e = (navigator.appVersion.indexOf("MSIE") != -1) ? "click" : "mousedown";
    this.dcsBind(e, OnSubmitClick);

    Date.prototype.toMMDDYYYYString = function () {
        return isNaN(this) ? 'NaN' : [this.getMonth() > 8 ? this.getMonth() + 1 : '0' + (this.getMonth() + 1), this.getDate() > 9 ? this.getDate() : '0' + this.getDate(), this.getFullYear()].join('/')
    }

}

_tag.dcsCollect();
