﻿function createObjectCallback(obj, fn) {
  return function() { fn.apply(obj, arguments); };
}

var SAVEOLOGY = {
  SearchWidget: function(dvContainer) {
    var private = {
      dvSearchWidget: document.createElement('div'),
      dvSearchFields: document.createElement('div'),
      dvSearchButton: document.createElement('div'),
      Fields: {
        ulFields: document.createElement('ul'),
        txtAddress: document.createElement('input'),
        txtAddress2: document.createElement('input'),
        txtZip: document.createElement('input'),

        divNewCustomer: document.createElement("div"),
        moveDateQuestion: document.createElement("span"),
        chkIsNewCustomer: document.createElement("input"),
        newCustomerMoveDate: document.createElement("div"),
        txtMoveDate: document.createElement("input"),

        btnSubmit: document.createElement('input'),
        keypressed: function(e) {
          var charactercode;
          if (e && e.which) { //if which property of event object is supported (NN4)
            e = e
            characterCode = e.which //character code is contained in NN4's which property
          }
          else {
            e = event
            characterCode = e.keyCode //character code is contained in IE's keyCode property
          }

          if (characterCode == '13') { this.onsubmit(); return false; }
        },
        Init: function() {
          this.btnSubmit.className = "clsSearchSubmit";
          this.btnSubmit.type = "button";
          this.chkIsNewCustomer.type = "checkbox";
          this.divNewCustomer.className = "clsWidgetFields";
          this.btnSubmit.onclick = createObjectCallback(this, this.onsubmit);
          document.onkeypress = createObjectCallback(this, this.keypressed);
          this.ulFields.innerHTML

          this.txtAddress.className = "clsSearchAddress1";
          this.txtAddress2.className = "clsSearchAddress2";
          this.txtZip.className = "clsSearchZip";

          this.Add('Address: ', this.txtAddress);
          this.Add('Apt #: ', this.txtAddress2);
          this.Add('Zip Code: ', this.txtZip);

          //this.AddMoveDateSection();//Regular Add

          /*begin Add for aafes or cox*/
          var cox_or_aafes = ["cox", "buycox", "aafes", "exchangehomeservices", "shop.aafes"];
          var hn = window.location.hostname;
          if (hn != null) {
            for (var x in cox_or_aafes) {
              var name = cox_or_aafes[x];
              if (hn.indexOf(name) >= 0) {
                this.AddMoveDateSection();
                break;
              }
            }
          }
          /*END Add for aafes or cox*/

          private.dvSearchFields.appendChild(this.ulFields);
          private.dvSearchButton.appendChild(this.btnSubmit);
          private.dvSearchWidget.appendChild(private.dvSearchFields);
          private.dvSearchWidget.appendChild(private.dvSearchButton);
        },
        AddMoveDateSection: function() {
          this.moveDateQuestion.setAttribute('id', 'moveDateQuestion');
          this.newCustomerMoveDate.setAttribute('id', 'newCustomerMoveDate');
          this.newCustomerMoveDate.setAttribute('style', 'display:none');
          this.chkIsNewCustomer.type = "checkbox";

          var blankDiv = document.createElement("div");
          blankDiv.innerHTML = '<br><p>&nbsp;</p><br>';
          this.ulFields.appendChild(blankDiv);

          var lblMoveDate = document.createElement('lblMoveDate');
          lblMoveDate.setAttribute('id', 'lblMoveDate');
          lblMoveDate.innerHTML = 'Have you recently moved or plan to move to this address?';
          this.moveDateQuestion.appendChild(lblMoveDate);

          this.chkIsNewCustomer.setAttribute('id', 'chkIsNewCustomer');
          //this.chkIsNewCustomer.setAttribute('onclick', 'MoveDateVisibility(\'newCustomerMoveDate\', \'hidden\'');
          this.moveDateQuestion.appendChild(this.chkIsNewCustomer);
          this.divNewCustomer.appendChild(this.moveDateQuestion);

          var lblSelectMoveDate = document.createElement('lblSelectMoveDate');
          lblSelectMoveDate.setAttribute('id', 'lblSelectMoveDate');
          lblSelectMoveDate.innerHTML = 'Select move date: ';
          this.newCustomerMoveDate.appendChild(lblSelectMoveDate);

          this.txtMoveDate.setAttribute('id', 'txtMoveDate');
          this.newCustomerMoveDate.appendChild(this.txtMoveDate);

          this.divNewCustomer.appendChild(this.newCustomerMoveDate);

          this.ulFields.appendChild(this.divNewCustomer);
        },
        Add: function(name, field) {
          var myDate = new Date;
          var uniqueID = myDate.getTime();

          var ListItem = document.createElement('li');

          var Label = document.createElement('label');
          Label.setAttribute('for', uniqueID);
          Label.innerHTML = name;

          field.setAttribute('id', uniqueID);

          ListItem.appendChild(Label);
          ListItem.appendChild(field);

          this.ulFields.appendChild(ListItem);
        },
        onsubmit: function() {
          if (private.Fields.txtAddress.value.length == 0) {
            alert('Address is a required field');
            return false;
          }
          if (private.Fields.txtZip.value.length == 0) {
            alert('Zip code is a required field');
            return false;
          }
          if (isNaN(parseInt(private.Fields.txtZip.value))) {
            alert('Zip code must be a 5 digit number');
            return false;
          }
          if (private.Fields.txtZip.value.length != 5) {
            alert('Zip code must be a 5 digit number');
            return false;
          }

          /*begin Add for aafes or cox*/
          var newcust = false;
          if (private.Fields.txtMoveDate.value.length >= 10) { newcust = true; }
          var mvDate = private.Fields.txtMoveDate.value;
          /*end Add for aafes or cox*/

          //alert(public.RedirectURL);

          public.RedirectURL = (public.RedirectURL === undefined || public.RedirectURL == '') ? '/CompareOffers/default.aspx' : public.RedirectURL;

          if (newcust == true) {
            url = public.RedirectURL + '?a=' + private.Fields.txtAddress.value + ',' + private.Fields.txtAddress2.value + ',,,' + private.Fields.txtZip.value + '&IsNewCustomer=' + newcust + '&MoveDate=' + mvDate + '&c=39&p=995037&nt=Category&nv=' + public.selectedCategory + '&sc=' + public.selectedCategory;
          }
          else {
            url = public.RedirectURL + '?a=' + private.Fields.txtAddress.value + ',' + private.Fields.txtAddress2.value + ',,,' + private.Fields.txtZip.value + '&c=39&p=995037&nt=Category&nv=' + public.selectedCategory + '&sc=' + public.selectedCategory;
          }
          //url = '/CompareOffers/default.aspx?a=' + private.Fields.txtAddress.value + ',' + private.Fields.txtAddress2.value + ',,,' + private.Fields.txtZip.value + '&c=39&p=995037&sch=0&nt=Category&nv=' + '39' + '&sc=' + '39' + trackingVar;

          if (opener != null) {
            opener.window.location = url
            window.close();
          }
          else {
            window.location = url;
          }
        },
        onchange: function(radioButton) {
          this.selectedCategory = radioButton.Value;
        }
      }
    };
    var public = {
      selectedCategory: null,
      RedirectURL: '/CompareOffers/default.aspx',
      Init: function(dvContainer) {
        private.Fields.Init();
        dvContainer.appendChild(private.dvSearchWidget);
      },
      Submit: function() {
      }
    };
    return public;
  },
  SearchWidgetEs: function(dvContainer) {
      var private = {
          dvSearchWidget: document.createElement('div'),
          dvSearchFields: document.createElement('div'),
          dvSearchButton: document.createElement('div'),
          Fields: {
              ulFields: document.createElement('ul'),
              txtAddress: document.createElement('input'),
              txtAddress2: document.createElement('input'),
              txtZip: document.createElement('input'),

              divNewCustomer: document.createElement("div"),
              moveDateQuestion: document.createElement("span"),
              chkIsNewCustomer: document.createElement("input"),
              newCustomerMoveDate: document.createElement("div"),
              txtMoveDate: document.createElement("input"),

              btnSubmit: document.createElement('input'),
              keypressed: function(e) {
                  var charactercode;
                  if (e && e.which) { //if which property of event object is supported (NN4)
                      e = e
                      characterCode = e.which //character code is contained in NN4's which property
                  }
                  else {
                      e = event
                      characterCode = e.keyCode //character code is contained in IE's keyCode property
                  }

                  if (characterCode == '13') { this.onsubmit(); return false; }
              },
              Init: function() {
                  this.btnSubmit.className = "clsSearchSubmit";
                  this.btnSubmit.type = "button";
                  this.chkIsNewCustomer.type = "checkbox";
                  this.divNewCustomer.className = "clsWidgetFields";
                  this.btnSubmit.onclick = createObjectCallback(this, this.onsubmit);
                  document.onkeypress = createObjectCallback(this, this.keypressed);
                  this.ulFields.innerHTML

                  this.txtAddress.className = "clsSearchAddress1";
                  this.txtAddress2.className = "clsSearchAddress2";
                  this.txtZip.className = "clsSearchZip";

                  this.Add('Dirección: ', this.txtAddress);
                  this.Add('Apto #: ', this.txtAddress2);
                  this.Add('Código Postal: ', this.txtZip);

                  //this.AddMoveDateSection();//Regular Add

                  /*begin Add for aafes or cox*/
                  var cox_or_aafes = ["cox", "buycox", "aafes", "exchangehomeservices", "shop.aafes"];
                  var hn = window.location.hostname;
                  if (hn != null) {
                      for (var x in cox_or_aafes) {
                          var name = cox_or_aafes[x];
                          if (hn.indexOf(name) >= 0) {
                              this.AddMoveDateSection();
                              break;
                          }
                      }
                  }
                  /*END Add for aafes or cox*/

                  private.dvSearchFields.appendChild(this.ulFields);
                  private.dvSearchButton.appendChild(this.btnSubmit);
                  private.dvSearchWidget.appendChild(private.dvSearchFields);
                  private.dvSearchWidget.appendChild(private.dvSearchButton);
              },
              AddMoveDateSection: function() {
                  this.moveDateQuestion.setAttribute('id', 'moveDateQuestion');
                  this.newCustomerMoveDate.setAttribute('id', 'newCustomerMoveDate');
                  this.newCustomerMoveDate.setAttribute('style', 'display:none');
                  this.chkIsNewCustomer.type = "checkbox";

                  var blankDiv = document.createElement("div");
                  blankDiv.innerHTML = '<br><p>&nbsp;</p><br>';
                  this.ulFields.appendChild(blankDiv);

                  var lblMoveDate = document.createElement('lblMoveDate');
                  lblMoveDate.setAttribute('id', 'lblMoveDate');
                  lblMoveDate.innerHTML = 'Have you recently moved or plan to move to this address?';
                  this.moveDateQuestion.appendChild(lblMoveDate);

                  this.chkIsNewCustomer.setAttribute('id', 'chkIsNewCustomer');
                  //this.chkIsNewCustomer.setAttribute('onclick', 'MoveDateVisibility(\'newCustomerMoveDate\', \'hidden\'');
                  this.moveDateQuestion.appendChild(this.chkIsNewCustomer);
                  this.divNewCustomer.appendChild(this.moveDateQuestion);

                  var lblSelectMoveDate = document.createElement('lblSelectMoveDate');
                  lblSelectMoveDate.setAttribute('id', 'lblSelectMoveDate');
                  lblSelectMoveDate.innerHTML = 'Select move date: ';
                  this.newCustomerMoveDate.appendChild(lblSelectMoveDate);

                  this.txtMoveDate.setAttribute('id', 'txtMoveDate');
                  this.newCustomerMoveDate.appendChild(this.txtMoveDate);

                  this.divNewCustomer.appendChild(this.newCustomerMoveDate);

                  this.ulFields.appendChild(this.divNewCustomer);
              },
              Add: function(name, field) {
                  var myDate = new Date;
                  var uniqueID = myDate.getTime();

                  var ListItem = document.createElement('li');

                  var Label = document.createElement('label');
                  Label.setAttribute('for', uniqueID);
                  Label.innerHTML = name;

                  field.setAttribute('id', uniqueID);

                  ListItem.appendChild(Label);
                  ListItem.appendChild(field);

                  this.ulFields.appendChild(ListItem);
              },
              onsubmit: function() {
                  if (private.Fields.txtAddress.value.length == 0) {
                      alert('Dirección es requerido');
                      return false;
                  }
                  if (private.Fields.txtZip.value.length == 0) {
                      alert('Código Postal es requerido');
                      return false;
                  }
                  if (isNaN(parseInt(private.Fields.txtZip.value))) {
                      alert('El código postal debe ser un número de 5 dígitos');
                      return false;
                  }
                  if (private.Fields.txtZip.value.length != 5) {
                      alert('El código postal debe ser un número de 5 dígitos');
                      return false;
                  }

                  /*begin Add for aafes or cox*/
                  var newcust = false;
                  if (private.Fields.txtMoveDate.value.length >= 10) { newcust = true; }
                  var mvDate = private.Fields.txtMoveDate.value;
                  /*end Add for aafes or cox*/

                  //alert(public.RedirectURL);

                  public.RedirectURL = (public.RedirectURL === undefined || public.RedirectURL == '') ? '/CompareOffers/default.aspx' : public.RedirectURL;

                  if (newcust == true) {
                      url = public.RedirectURL + '?a=' + private.Fields.txtAddress.value + ',' + private.Fields.txtAddress2.value + ',,,' + private.Fields.txtZip.value + '&IsNewCustomer=' + newcust + '&MoveDate=' + mvDate + '&c=39&p=995037&nt=Category&nv=' + public.selectedCategory + '&sc=' + public.selectedCategory;
                  }
                  else {
                      url = public.RedirectURL + '?a=' + private.Fields.txtAddress.value + ',' + private.Fields.txtAddress2.value + ',,,' + private.Fields.txtZip.value + '&c=39&p=995037&nt=Category&nv=' + public.selectedCategory + '&sc=' + public.selectedCategory;
                  }
                  //url = '/CompareOffers/default.aspx?a=' + private.Fields.txtAddress.value + ',' + private.Fields.txtAddress2.value + ',,,' + private.Fields.txtZip.value + '&c=39&p=995037&sch=0&nt=Category&nv=' + '39' + '&sc=' + '39' + trackingVar;

                  if (opener != null) {
                      opener.window.location = url
                      window.close();
                  }
                  else {
                      window.location = url;
                  }
              },
              onchange: function(radioButton) {
                  this.selectedCategory = radioButton.Value;
              }
          }
      };
      var public = {
          selectedCategory: null,
          RedirectURL: '/CompareOffers/default.aspx',
          Init: function(dvContainer) {
              private.Fields.Init();
              dvContainer.appendChild(private.dvSearchWidget);
          },
          Submit: function() {
          }
      };
      return public;
  }
};

function callClickOfSubmitButton() {
  var submitButton = document.getElementById('ctl00_MainContent_ServiceSearch1_SubmitButton');
  submitButton.click();
}

/********************Search Widget*****************/
var SAVEOLOGY2 = {
  SearchWidget: function(dvContainer) {
    var private = {
      dvSearchWidget: document.createElement('div'),
      dvSearchHeader: document.createElement('div'),
      dvSearchFields: document.createElement('div'),
      Fields: {
        ulFields: document.createElement('ul'),
        txtAddress: document.createElement('input'),
        txtAddress2: document.createElement('input'),
        txtZip: document.createElement('input'),
        txtSMSEmail: document.createElement('input'),

        divNewCustomer: document.createElement("div"),
        moveDateQuestion: document.createElement("span"),
        chkIsNewCustomer: document.createElement("input"),
        newCustomerMoveDate: document.createElement("div"),
        txtMoveDate: document.createElement("input"),

        btnSubmit: document.createElement('div'),
        keypressed: function(e) {
          var evt = (e) ? e : (window.event) ? window.event : null;
          var key = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));

          if (key == '13') { this.onsubmit(); return false; }
        },
        Init: function() {
          private.dvSearchWidget.className = "clsWidgetBox";
          private.dvSearchHeader.className = "clsWidgetHeader";
          this.divNewCustomer.className = "clsWidgetFields";
          this.btnSubmit.className = "clsWidgetButton";
          this.ulFields.className = "clsWidgetFields";
          this.btnSubmit.onclick = createObjectCallback(this, this.onsubmit);
          document.onkeypress = createObjectCallback(this, this.keypressed);

          this.txtAddress.size = 28;
          this.txtZip.size = 5;
          this.txtZip.style.width = "3em";
          this.txtZip.maxLength = 5;
          this.txtAddress2.size = 5;
          this.txtAddress2.style.width = "3em";
          this.txtAddress2.maxLength = 5;
          this.txtAddress.className = "clsSearchAddress1";
          this.txtAddress2.className = "clsSearchAddress2";
          this.txtZip.className = "clsSearchZip";
          this.txtSMSEmail.className = "clsSearchAddress1";
          this.txtSMSEmail.setAttribute('id', 'txtSMSEmail');
          this.ulFields.innerHTML

          this.Add('Address: ', this.txtAddress);
          this.Add('Apt #: ', this.txtAddress2);
          this.Add('Zip Code: ', this.txtZip);

          //this.AddSMSEmail();
          //this.AddMoveDateSection();//Regular Add

          /*begin Add for aafes or cox and PrideInSavings*/
          var pride_in_sav = ["prideinsavings", "prideinsaving"]
          var cox_or_aafes = ["cox", "buycox", "aafes", "exchangehomeservices", "shop.aafes"];
          var hn = window.location.hostname;
          //alert('window.location.hostname:   ' + hn);
          if (hn != null) {
            for (var x in cox_or_aafes) {
              var name = cox_or_aafes[x];
              if (hn.indexOf(name) >= 0) { this.AddMoveDateSection(); break; }
            }

            for (var p in pride_in_sav) {
              var pis = pride_in_sav[p];
              if (hn.indexOf(pis) >= 0) { this.AddSMSEmail(); break; }
            }
          }
          /*END Add for aafes or cox and PrideInSavings*/

          private.dvSearchFields.appendChild(this.ulFields);

          private.dvSearchWidget.appendChild(private.dvSearchHeader);
          private.dvSearchWidget.appendChild(private.dvSearchFields);
          private.dvSearchWidget.appendChild(this.btnSubmit);
          //alert('SAVEOLOGY2.init called');
        },
        AddMoveDateSection: function() {
          this.moveDateQuestion.setAttribute('id', 'moveDateQuestion');
          this.newCustomerMoveDate.setAttribute('id', 'newCustomerMoveDate');
          this.newCustomerMoveDate.setAttribute('style', 'display:none');
          this.chkIsNewCustomer.type = "checkbox";

          var blankDiv = document.createElement("div");
          blankDiv.innerHTML = '<br><p>&nbsp;</p><br>';
          this.ulFields.appendChild(blankDiv);

          var lblMoveDate = document.createElement('lblMoveDate');
          lblMoveDate.setAttribute('id', 'lblMoveDate');
          lblMoveDate.innerHTML = 'Have you recently moved or plan to move to this address?';
          this.moveDateQuestion.appendChild(lblMoveDate);

          this.chkIsNewCustomer.setAttribute('id', 'chkIsNewCustomer');
          //this.chkIsNewCustomer.setAttribute('onclick', 'MoveDateVisibility(\'newCustomerMoveDate\', \'hidden\'');
          this.moveDateQuestion.appendChild(this.chkIsNewCustomer);
          this.divNewCustomer.appendChild(this.moveDateQuestion);

          var lblSelectMoveDate = document.createElement('lblSelectMoveDate');
          lblSelectMoveDate.setAttribute('id', 'lblSelectMoveDate');
          lblSelectMoveDate.innerHTML = 'Select move date: ';
          this.newCustomerMoveDate.appendChild(lblSelectMoveDate);

          this.txtMoveDate.setAttribute('id', 'txtMoveDate');
          this.newCustomerMoveDate.appendChild(this.txtMoveDate);

          this.divNewCustomer.appendChild(this.newCustomerMoveDate);

          this.ulFields.appendChild(this.divNewCustomer);
        },
        AddSMSEmail: function() {
          var ListItem = document.createElement('li');

          var Label = document.createElement('label');
          Label.setAttribute('id', 'lblSMSEmail');
          Label.innerHTML = "Email: (optional)";

          this.txtSMSEmail.setAttribute('id', 'txtSMSEmail');

          ListItem.appendChild(Label);
          ListItem.appendChild(this.txtSMSEmail);

          this.ulFields.appendChild(ListItem);
        },
        Add: function(name, field) {
          var myDate = new Date;
          var uniqueID = myDate.getTime();

          var ListItem = document.createElement('li');

          var Label = document.createElement('label');
          Label.setAttribute('for', uniqueID);
          Label.innerHTML = name;

          field.setAttribute('id', uniqueID);

          ListItem.appendChild(Label);
          ListItem.appendChild(field);

          this.ulFields.appendChild(ListItem);
        },
        onsubmit: function() {
          if (private.Fields.txtAddress.value.length == 0) {
            alert('Address is a required field');
            return false;
          }
          if (private.Fields.txtZip.value.length == 0) {
            alert('Zip code is a required field');
            return false;
          }
          if (isNaN(parseInt(private.Fields.txtZip.value))) {
            alert('Zip code must be a 5 digit number');
            return false;
          }
          if (private.Fields.txtZip.value.length != 5) {
            alert('Zip code must be a 5 digit number');
            return false;
          }

          qs = new Querystring();
          var trackingVar = qs.get();

          /*begin Add for aafes or cox*/
          var newcust = false;
          if (private.Fields.txtMoveDate.value.length >= 10) { newcust = true; }
          var mvDate = private.Fields.txtMoveDate.value;
          /*end Add for aafes or cox*/

          var email = private.Fields.txtSMSEmail.value;

          //alert(public.RedirectURL);

          public.RedirectURL = (public.RedirectURL === undefined || public.RedirectURL == '') ? '/CompareOffers/default.aspx' : public.RedirectURL;

          if (newcust == true) {
            url = public.RedirectURL + '?a=' + private.Fields.txtAddress.value + ',' + private.Fields.txtAddress2.value + ',,,' + private.Fields.txtZip.value + '&IsNewCustomer=' + newcust + '&MoveDate=' + mvDate + '&c=39&p=995037&nt=Category&nv=' + public.selectedCategory + '&sc=' + public.selectedCategory;
          }
          else {
            url = public.RedirectURL + '?a=' + private.Fields.txtAddress.value + ',' + private.Fields.txtAddress2.value + ',,,' + private.Fields.txtZip.value + '&c=39&p=995037&nt=Category&nv=' + public.selectedCategory + '&sc=' + public.selectedCategory;
          }

          url = url + '&email=' + email;

          if (opener != null) {
            opener.window.location = url
            window.close();
          }
          else {
            window.location = url;
          }
        },
        onchange: function(radioButton) {
          this.selectedCategory = radioButton.Value;
        }
      }
    };
    var public = {
      selectedCategory: null,
      Init: function(dvContainer) {
        private.Fields.Init();
        //private.Categories.Init();
        RedirectURL: '/CompareOffers/default.aspx',
                dvContainer.appendChild(private.dvSearchWidget);
      },
      Submit: function() {
      }
    };
    return public;
  }
};
