
Event.observe(window, 'load', function() {
    ionMxCustom = {};
    ionMxCustom.CCF = {};
    var currentDay = 1;
    var currentMonth = 0;
    var currentYear = 2008;
    var RowCount = 4;
    var InsertIndex = 0;
    var badForm = false;
    var q = window.location.search.toQueryParams();

    ionMxCustom.CCF.LpAction = function() {
        return {
            initialize: function() {
                var form = $('form1').serialize(true);
                form.request = 'populate';
                form.tID = q["t"];
                form.pID = q["p"];
                form.cID = q["c"];
                if (form.cID == null)
                    form.cID = "1";

                //ajaxStatus.showMessage({message:'Coupon Creating...', minimumDisplayTimeInSeconds:1});

                if (form.tID != null) {
                    new Ajax.Request('ajax/lpAction.ashx', {
                        method: 'get',
                        parameters: form,
                        onSuccess: function(r) {
                            //alert('Response is: ' + r.responseText);
                            if (r.responseText.startsWith('Error')) {
                                if (r.responseText.indexOf('not match') > 0) {
                                    $('FullName').innerHTML = "";
                                    $('PercentOff').innerHTML = "Coupon is not valid";
                                    $('PrintCoupon').removeAttribute("href");
                                    $('CouponPrint').removeAttribute("href");
                                    $('PrintCoupon').onclick = function() { return; };
                                    $('CouponPrint').onclick = function() { return; };
                                    $('BarCodeImage').src = "http://img.ionmx.com/ionmxweb2/MXOrgs/6008/Content/images/coupons/fesg/barCode_void.gif";
                                    alert('ERROR:  Cannot create coupon with given information');
                                    badForm = true;
                                }
                                else if (r.responseText.indexOf('expire') > 0) {
                                    $('FullName').innerHTML = "";
                                    $('PercentOff').innerHTML = "Coupon is not valid";
                                    $('PrintCoupon').removeAttribute("href");
                                    $('CouponPrint').removeAttribute("href");
                                    $('PrintCoupon').onclick = function() { return; };
                                    $('CouponPrint').onclick = function() { return; };
                                    $('BarCodeImage').src = "http://img.ionmx.com/ionmxweb2/MXOrgs/6008/Content/images/coupons/fesg/barCode_void.gif";
                                    alert('This Coupon Offer has expired');
                                    badForm = true;
                                }
                                else {
                                    $('FullName').innerHTML = "";
                                    $('PercentOff').innerHTML = "The System is Busy";
                                    $('PrintCoupon').removeAttribute("href");
                                    $('CouponPrint').removeAttribute("href");
                                    $('PrintCoupon').onclick = function() { return; };
                                    $('CouponPrint').onclick = function() { return; };
                                    $('BarCodeImage').src = "http://img.ionmx.com/ionmxweb2/MXOrgs/6008/Content/images/coupons/fesg/barCode_void.gif";
                                    alert('We cannot render your coupon at this time.  Please try again later.');
                                    badForm = true;
                                }
                            }
                            else if (r.responseText.startsWith('date')) {
                                //alert('Date is: ' + r.responseText);
                                var timeArray = new Array();
                                timeArray = r.responseText.split(':');
                                currentMonth = timeArray[1] - 1;
                                currentDay = timeArray[2];
                                currentYear = timeArray[3];
                            }
                            else {
                                //alert('Populate ' + r.responseText);
                                var memberArray = r.responseText.split(':');
                                var theURL = "http://www.rewardyourselfatriteaid.com/BarCode/NewImage.aspx?BarCode=" + memberArray[0] + "&BarCodeType=Code-128";
                                $('FirstName').innerHTML = memberArray[1] + ",";
                                var theFirstName = memberArray[1];
                                if ((memberArray[1].length + memberArray[2].length) > 30)
                                    theFirstName = memberArray[1].substring(0, 1);
                                if (memberArray[2].endsWith("s")) {
                                    $('FullName').innerHTML = theFirstName + " " + memberArray[2] + "' ";
                                }
                                else {
                                    $('FullName').innerHTML = theFirstName + " " + memberArray[2] + "'s ";
                                }

                                $('BarCodeImage').src = theURL;

                                // Set the correct Title
                                if ((form.cID == "3") || (form.cID == "5"))
                                    $('PercentOff').innerHTML = "20% One-Time Use Savings Pass";
                                else if ((form.cID == "4") || (form.cID == "6"))
                                    $('PercentOff').innerHTML = "20% One-Time Use Health-Related Savings Pass";
                                else
                                    $('PercentOff').innerHTML = "20% Shopping Pass";

                                var interval = 28;
                                var today = new Date();
                                var theDate = new Date(today.getYear(), today.getMonth(), today.getDate() - 0 + interval);
                                var theExpiration = (theDate.getMonth() + 1) + "/" + theDate.getDate() + "/";
                                if (theDate.getFullYear() == 2010)
                                    theExpiration += "10.";
                                else
                                    theExpiration += "09.";
                                $('Expiration').innerHTML = theExpiration;
                                //$('steelCurtain').hide(); 
                                //ajaxStatus.hideMessage();   
                            }
                        },
                        onFailure: function(r) {
                            alert(r.responseText);
                        },
                        onComplete: function(r) {
                        }
                    });
                }
                else {
                    //$('FullName').innerHTML = "";
                    //$('PercentOff').innerHTML = "Coupon is not valid";
                    //$('PrintCoupon').removeAttribute("href");
                    //$('CouponPrint').removeAttribute("href");
                    //$('PrintCoupon').onclick = function() { return; };
                    //$('CouponPrint').onclick = function() { return; };
                    //alert('Please click on the link in your email to access your coupon');
                    badForm = true;
                }
            },
            lpActionCallBack: function(text) { alert('CallBack ' + text); },
            doIt: function() {
                var theEmail = $('EmailTextBox').value;
                var theAgree = $('AgreeCheckBox').checked;
                if (!theAgree) {
                    //alert('Please Check the box');
                    $('CheckBoxError').style.display = '';
                }
                if (!ionMxCustom.CCF.LpAction.IsEmailValid(theEmail)) {
                    //alert('Bad Email');
                    $('EmailError').style.display = '';
                }
                else if (theAgree) {

                    //var form = { 'request': 'signup', 'email': theEmail };

                    new Ajax.Request('ajax/lpAction.ashx',
                    {
                        method: 'get',
                        parameters: { 'request': 'signup', 'email': theEmail },
                        onSuccess: function(r) {
                        //alert('Success: ' + r.responseText);
                        window.location.href = 'gos_ty.html';
                        },
                        onFailure: function(r) {
                            alert('The system is busy at this time.  Please try again later');
                        },
                        onComplete: function(r) {
                        }
                    });
                }
            },
            moreInfo: function() {
                //alert('Got Here');
                //Lightbox.show('testDiv');
                $('infoBorder').style.display = '';
                $('infoWindow').style.display = '';
            },
            closeInfo: function(e) {
                //alert('Got Here');
                //Lightbox.hide('testDiv');
                $('infoWindow').style.display = 'none';
                $('infoBorder').style.display = 'none';
                //var myWindow = $('infoWindow');
                //var x = e.clientX;
                //var y = e.clientY;
                //alert('x: ' + x + ' y: ' + y);
            },
            clearIt: function() {
                //alert('Got Here');
                $('EmailTextBox').value = '';
                $('EmailError').style.display = 'none';
            },
            checkIt: function() {
                //alert('Got Here');
            var theAgree = $('AgreeCheckBox').checked;
                if (theAgree)
                    $('CheckBoxError').style.display = 'none';
            },
            IsEmailValid: function(Email) {
                return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w{2,}$/.test(Email);
            },
            ValidateForm: function() {
                var Invalid = true;
                return Invalid;
            }
        }
    } ();
    //ionMxCustom.CCF.LpAction.initialize();
});