﻿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'),
                //btnSubmit: 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.setAttribute('type', 'submit');
                    //this.btnSubmit.setAttribute('value', 'Search For Your Offers');
                    this.btnSubmit.className = "clsSearchSubmit";
                    this.btnSubmit.type = "button";
                    //  this.btnSubmit.value = "Find Offers";
                    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);

                    private.dvSearchFields.appendChild(this.ulFields);
                    private.dvSearchButton.appendChild(this.btnSubmit);
                    private.dvSearchWidget.appendChild(private.dvSearchFields);
                    private.dvSearchWidget.appendChild(private.dvSearchButton);
                    //                    private.dvSearchWidget.appendChild(this.ulFields);
                    //                    private.dvSearchWidget.appendChild(this.btnSubmit);
                },
                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;
                    }
                    //		            if (public.selectedCategory == null)
                    //		            {
                    //			            alert('You must select a category');
                    //			            return false;
                    //		            }

                    url = '\\CompareOffers\\default.aspx?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;

                    if (opener != null) {
                        opener.window.location = url
                        window.close();
                    }
                    else {
                        window.location = url;
                    }
                },
                onchange: function(radioButton) {
                    this.selectedCategory = radioButton.Value;
                }
            }
            //	       Categories:{                
            //                ulCategories: document.createElement('ul'),
            //                Init: function(){
            //                    this.Add('Bundled Offers', 39);
            //		            this.Add('Television', 4);
            //		            this.Add('Internet', 2);
            //		            this.Add('Phone', 1);
            //                },
            //                Add: function(name, value){
            //		            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;

            //		            var RadioButton = document.createElement('input');
            //		            RadioButton.setAttribute('type', 'radio');
            //	                RadioButton.setAttribute('name', 'category');
            //		            RadioButton.setAttribute('value', value);
            //		            RadioButton.setAttribute('id', uniqueID);
            //		            RadioButton.onchange = createObjectCallback(this, 
            //		            function(){this.selectedCategory = RadioButton.value;});
            //    	            
            //		            ListItem.appendChild(RadioButton);
            //		            ListItem.appendChild(Label);

            //		            this.ulCategories.appendChild(ListItem);
            //                 }
            //            }
        };
        var public = {
            selectedCategory: null,
            Init: function(dvContainer) {
                private.Fields.Init();
                //private.Categories.Init();
                //alert(private.dvSearchWidget.innerHTML);
                dvContainer.appendChild(private.dvSearchWidget);
            },
            Submit: function() {

            }
        };
        return public;
    }
};

function callClickOfSubmitButton()
{
    var submitButton = document.getElementById('ctl00_MainContent_ServiceSearch1_SubmitButton');
    submitButton.click();
}
