Record = {
    cartItem:[
        {name:'idproduct',xtype:'int'},
        {name:'customid'},
        {name:'name'},
        {name:'price',xtype:'float'},
        {name:'discount',xtype:'float'},
        {name:'quantity',xtype:'int'},
        {name:'total',xtype:'float'}
    ],
    address:[
        {name:'iduser_address'},
        {name:'name'},
        {name:'company'},
        {name:'address1'},
        {name:'address2'},
        {name:'city'},
        {name:'state'},
        {name:'state_string'},
        {name:'zip'},
        {name:'country',defaultValue:223},
        {name:'country_string'},
        {name:'phone'},
        {name:'user'},
        {name:'email'},
        {name:'stringified'}
    ],
    states:[
        {name:'idlocation_state',xtype:'int'},
        {name:'name'}
    ],
    countries:[
        {name:'idlocation_country', xtype:'int'},
        {name:'name'}
    ],
    pageRevision:[
        {name:'idpage_revision',xtype:'int'},
        {name:'website_page_id',xtype:'int'},
        {name:'url'},
        {name:'title'},
        {name:'description'},
        {name:'keywords'},
        {name:'content'},
        {name:'created_on',xtype:'date'},
        {name:'created_by'},
        {name:'created_by_string'},
        {name:'approved_on',xtype:'date'},
        {name:'approved_by'},
        {name:'approved_by_string'},
        {name:'release_on',xtype:'date'},
        {name:'changes'}
    ],
    page:[
        {name:'idwebsite_page',xtype:'int'},
        {name:'url'},
        {name:'title'},
        {name:'description'},
        {name:'keywords'},
        {name:'content'},
        {name:'last_update',xtype:'date'},
        {name:'current_revisions',xtype:'int'},
        {name:'filedate',xtype:'date'}
    ]
};

BuyWindow = Ext.extend(Ext.Window,{
    constructor:function(config){
        Ext.apply(this,config);
        BuyWindow.superclass.constructor.apply(this);
    },
    initComponent:function(){
        Ext.apply(this,{
            height:136,
            width:300,
            ctCls:'BuyWindow',
            items:{
                xtype:'form',
                border:false,
                frame:true,
                buttonAlign:'center',
                items:[
                    {xtype:'panel',border:'false',html:"<h5 style='text-align:center'>How many would you like to add to your cart?</h5>"},
                    {xtype:'textfield',name:'quantity',fieldLabel:'Quantity',value:this.pq}
                ],
                buttons:[
                    {text:'Proceed To Cart',handler:function(button){
                        var fp = button.findParentByType('form');
                        var form = fp.getForm();
                        var win = button.findParentByType(BuyWindow);
                        form.submit({
                            url:"/index.php?action=Shop&op=addToCart&quantity="+form.findField('quantity').getValue()+"&id="+win.pid,
                            waitMsg:'Adding to cart...',
                            success:function(){
                                window.location.href = "/index.php?action=Shop&op=cart";
                            },
                            failure:function(){
                                Ext.Msg.alert("Error","Internal Error - something bad happened that was beyond your control. Try refreshing the page.");
                            }
                        })
                    }},
                    {text:'Keep Shopping',handler:function(button){
                        var fp = button.findParentByType('form');
                        var form = fp.getForm();
                        var win = button.findParentByType(BuyWindow);
                        form.submit({
                            url:"/index.php?action=Shop&op=addToCart&quantity="+form.findField('quantity').getValue()+"&id="+win.pid,
                            waitMsg:'Adding to cart...',
                            success:function(){
                                win.close();
                            },
                            failure:function(){
                                Ext.Msg.alert("Error","Internal Error - something bad happened that was beyond your control. Try refreshing the page.");
                            }
                        })
                    }}
                ]
            }
        });
        BuyWindow.superclass.initComponent.apply(this);
    }
});


Ext.onReady(function(){
    Ext.BLANK_IMAGE_URL = "/3rdparty/ext/resources/images/default/s.gif";

    

//TODO: .buyButton, #cartButton, .hideme, .noscript, .zebraMe

    Ext.select(".buyButton").on('click',function(e,t,o){
        var buywin = new BuyWindow({
            pid:t.getAttribute('bbpid'),
            pq:t.getAttribute('bbpq'),
            htmlel:t
        });
        buywin.show();
    });

});



function showFeedbackWindow(){
    win = new Ext.Window({
        title:'Page Feedback',
        layout:'fit',
        height:350,
        width:500,
        items:[
            new Ext.FormPanel({
                itemId:'feedbackForm',
                border:'false',
                items:[
                    {fieldLabel:'Your Name', xtype:'textfield', name:'name'},
                    {fieldLabel:'Your Email', xtype:'textfield', name:'email'},
                    {hidden:true, xtype:'hidden', name:'page',value:document.location},
                    {fieldLabel:'Feedback Type', xtype:'combo',
                        name:'type',
                        mode:'local',
                        store:new Ext.data.ArrayStore({
                            idIndex:0,
                            fields:[
                                'name'
                            ],
                            data:[
                                ['General'],
                                ['Graphical Issue'],
                                ['Useability Problem'],
                                ['Site Error']
                            ]
                        }),
                        displayField:'name',
                        valueField:'name',
                        triggerAction:'all',
                        typeAhead:true
                    },
                    {fieldLabel:'Comments', width:300, height:170, xtype:'htmleditor', name:'comments', enableColors:false,enableFont:false,enableFontSize:false,enableSourceEdit:false,enableAlignments:false}
                ]
            })
        ],
        buttons:[
            {text:'Feedback Privacy Statement', handler:function(){

                    win = new Ext.Window({
                        title:'Feedback Privacy',
                        height:300,
                        width:300,
                        layout:'fit',
                        items:[{
                                border:false,
                                data:{},
                                tpl:new Ext.Template([
                                    "Your feedback is very important to us, as is your privacy. Different browsers, system configurations and connection speeds may create wildly different experiences on this site, and therefore we do collect that information. It is not personal data and is only used to re-create your experience of the site. Your email address is optional, and can help if we cannot re-create the issue you were having on the site. If you have any concerns, just email <a href='mailto:cheryl@gettysburgflag.com'>cheryl@gettysburgflag.com</a> or call us at 1-888-697-3524."
                                ])
                        }]
                    });
                    win.show();

            }},
            {text:'Cancel', handler:function(button){
                    button.findParentByType('window').close();
            }},
            {text:'Submit', handler:function(button){
                button.findParentByType('window').getComponent('feedbackForm').getForm().submit({
                    url:'/?action=FeedbackModule&op=submitFeedback',
		    waitMsg:'Sending Feedback, please wait..',
		    waitTitle:'Sending Feedback',
                    success:function(){
                        button.findParentByType('window').close();
                        Ext.Msg.alert('Thank you','Thank you for your feedback, and thank you for shopping with Gettysburg Flag Works!');
                    }
                })
            }}
        ]
    });
    win.show();
}

function zebraTable(){
    $(this).children('tbody').children("tr:even").each(function(){
        $(this).addClass('zebraColor');
    })
    if(!$(this).hasClass("zebraMeNoRoll")){
        $(this).find("tr").each(function(){
            $(this).mouseover(function(){
                $(this).children("td").addClass("zebraColorMouseOver");
            })
            $(this).mouseout(function(){
                $(this).children("td").removeClass("zebraColorMouseOver");
            })
        })
    }
}

function addToCart(){
    if(readCookie("dontshowbuybox")==1){
        $("#theId").val($(this).attr('bbpid'));
        $("#buyQ").val($(this).attr('bbpq'));
        addItemToCart(function(){window.location.href = "/index.php?action=Shop&op=cart";});
    } else {
        var p = $(this).offset();
        $("#addToCartDiv").css("top",p.top - $("#addToCartDiv").height()/2)
        $("#addToCartDiv").css("left",p.left - $("#addToCartDiv").width())
        $("#addToCartDiv").show();
        $("#theId").val($(this).attr('bbpid'));
        $("#buyQ").val($(this).attr('bbpq'));
    }
}

function addItemToCart(f){
    if(f!==null && f!==undefined){
        $.get("/index.php?action=Shop&op=addToCart&quantity="+$("#buyQ").val()+"&id="+$("#theId").val(),null,f);
    } else {
        $.get("/index.php?action=Shop&op=addToCart&quantity="+$("#buyQ").val()+"&id="+$("#theId").val());
    }
}

function showCart(){
    
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

Ext.override(Ext.form.ComboBox, {
    setValue : function(v){
        var text = v;
        if(this.valueField){
            if(this.mode == 'remote' && !Ext.isDefined(this.store.totalLength)){
                this.store.on('load', this.setValue.createDelegate(this, arguments), null, {single: true});
                if(this.store.lastOptions === null){
                    var params = {};
                    if(this.valueParam){
                        params[this.valueParam] = v;
                    }else{
                        params[this.queryParam] = this.allQuery;
                    }
                    this.store.load({params: params});
                }
                return;
            }
            var r = this.findRecord(this.valueField, v);
            if(r){
                text = r.data[this.displayField];
            }else if(this.valueNotFoundText !== undefined){
                text = this.valueNotFoundText;
            }
        }
        this.lastSelectionText = text;
        if(this.hiddenField){
            this.hiddenField.value = v;
        }
        Ext.form.ComboBox.superclass.setValue.call(this, text);
        this.value = v;
    }
});
