//profileSearchForm
var profileSearchForm = function(){

	var form;
	var productStore, printerRIPStore, printermakeStore, printermodelStore;
	var resultsStore;
	var startMask;

    // return a public interface
    return {
		//startMask:null,
        init : function(){
				startMask = new Ext.LoadMask(document.body,{msg:'Loading',msgCls:'ext-el-mask-msg x-mask-loading',removeMask:false});
				startMask.show('ICC Profile Search Tools...',true);

				function convertName(v){
					return decodeURIComponent(v);
				}

				printermakeStore = new Ext.data.Store({
							baseParams: {action: 'loadmanu'},
							proxy: new Ext.data.HttpProxy({url: 'ajax-load-sort-options.php'}),
							reader: new Ext.data.XmlReader({
							record: 'manufacturer',
							id: 'manid'
							}, ['manid','name'])
					});

					printermodelStore = new Ext.data.Store({
							baseParams: {action: 'loadprinters'},
							proxy: new Ext.data.HttpProxy({url: 'ajax-load-sort-options.php'}),
							reader: new Ext.data.XmlReader({
							record: 'printer',
							id: 'printerid'
							}, ['printerid','name'])
					});

					productStore = new Ext.data.Store({
							baseParams: {action: 'loadproducts'},
							proxy: new Ext.data.HttpProxy({url: 'ajax-icc-profiles-search.php'}),
							reader: new Ext.data.XmlReader({
							record: 'product',
							id: 'productid'
							}, [{name: 'productid'},
								{name: 'name', convert:convertName}])
					});
					
					
					printerRIPStore = new Ext.data.Store({
							baseParams: {action: 'loadrips'},
							proxy: new Ext.data.HttpProxy({url: 'ajax-icc-profiles-search.php'}),
							reader: new Ext.data.XmlReader({
							record: 'rip',
							id: 'ripid'
							}, ['ripid','name'])
					});
							
					resultsStore = new Ext.data.Store({
							baseParams: {action: 'returnProfiles'},
							proxy: new Ext.data.HttpProxy({url: 'ajax-icc-profiles-search.php'}),
							reader: new Ext.data.XmlReader({
							record: 'profile',
							id: 'profileid'
							}, [{name: 'profileid'},
								{name: 'name', convert:convertName},
								{name: 'url'},
								{name: 'productname'},
								{name: 'ripname'},
								{name: 'printermakename'},
								{name: 'printername'}
							])
					});

					resultsStore.on('beforeload', function() {
						//startMask.show("",true);

						resultsStore.baseParams = { // modify the baseParams setting for this request
							action:'returnProfiles',
							product_id: profileSearchForm.returnProduct(),
							printer_manu_id: profileSearchForm.returnPrinterMake(),
							printer_id: profileSearchForm.returnPrinter(),
							rip_id: profileSearchForm.returnRIP()
						};
					});

				//resultsStore.on("load",function(){
					//startMask.hide();
				//});

					productStore.on("load", function(){
						printerRIPStore.load();
					});

					printerRIPStore.on("load", function(){
						printermakeStore.load();
					});

					printermakeStore.on("load", function(){
						profileSearchForm.createForm();
					});

					printermodelStore.on("beforeload", function(){
						 printermodelStore.baseParams = {
							 action: 'loadprinters', 
							 man_id: profileSearchForm.returnPrinterMake()
						};
					});

					printermodelStore.on("load", function(){
						//profileSearchForm.createForm();
					});
				
					productStore.load();

		},
		createForm: function(){
			Ext.QuickTips.init();

			 // turn on validation errors beside the field globally
			Ext.form.Field.prototype.msgTarget = 'side';

			form = new Ext.form.Form({
							name: 'searchProfiles',
							id: 'searchProfiles',
							method: "post"
				});

			var productBox = new Ext.form.ClearableComboBox({
					fieldLabel: 'Product',
					hiddenName:'product_id',
					name: 'product_id',
					store: productStore,
					displayField:'name',
					valueField: 'productid',
					typeAhead: true,
					mode: 'local',
					editable: 'false',
					triggerAction: 'all',
					emptyText:'Select a product...',
					selectOnFocus:true,
					width:200,
					allowBlank: true
				}); 

			var ripBox = new Ext.form.ClearableComboBox({
					fieldLabel: 'RIP or Printer Driver',
					hiddenName:'rip_id',
					name: 'rip_id',
					store: printerRIPStore,
					displayField:'name',
					valueField: 'ripid',
					typeAhead: true,
					mode: 'local',
					editable: 'false',
					triggerAction: 'all',
					emptyText:'Select a RIP or Printer Driver...',
					selectOnFocus:true,
					width:200,
					allowBlank: true
				}); 

			var printerMakeBox = new Ext.form.ClearableComboBox({
					fieldLabel: 'Printer Make',
					hiddenName:'printerMake',
					name: 'printerMake',
					store: printermakeStore,
					displayField:'name',
					valueField: 'manid',
					typeAhead: true,
					mode: 'local',
					editable: 'false',
					triggerAction: 'all',
					emptyText:'Select a make...',
					selectOnFocus:true,
					width:200,
					allowBlank: true,
					listeners:{
									'select' : function(thisfield, newvalue, oldvalue){
										printerModelBox.reset();
										//Ext.get("printerModelHolder").destroy();
										//productView.addFilters(); //add to the filters
										printermodelStore.load();//load the datastore of the printer models
									}
					}
				});

			var printerModelBox = new Ext.form.ClearableComboBox({
					fieldLabel: 'Printer Model',
					hiddenName:'printerModel',
					name: 'printerModel',
					store: printermodelStore,
					displayField:'name',
					valueField: 'printerid',
					typeAhead: true,
					mode: 'local',
					editable: 'false',
					triggerAction: 'all',
					emptyText:'Select a printer...',
					selectOnFocus:true,
					width:200,
					allowBlank: true
				}); 


				form.column({width:255, labelAlign:'top',labelSeparator:':'});
					form.add(productBox);
					form.add(printerMakeBox);
				form.end();

				form.column({width:255, labelAlign:'top',labelSeparator:':'});
					form.add(ripBox);
					form.add(printerModelBox);

				form.end();

				//add the button for search
				form.addButton("Search!", function(){
					profileSearchForm.searchNow();
				});
				
				form.render('iccprofilesearchform');
				startMask.hide();
		},
		searchNow: function(){
			 startMask = new Ext.LoadMask(document.body,{msg:'Searching profiles...',msgCls:'ext-el-mask-msg x-mask-loading',removeMask:false});
			 startMask.show("",true);
			var t = new Ext.Template(
				'<div id="search-result-item">',
				'<a href="{url}">{name}</a>',
				'<br>Product: {productname}; RIP: {ripname}; Printer Make: {printermakename}; Printer: {printername}',
				'</div>');

			resultsStore.on("load",function(){
				Ext.DomHelper.overwrite('iccprofileresults', "<div id='title-container'><h1>Results</h1></div>");
				var ar = resultsStore.getRange();
				
				if(ar.length > 0){
					Ext.each(ar,function(obj, index, allItems){
						//Ext.Msg.alert("",obj.get("name"));
						t.append('iccprofileresults', {id: obj.get("profileid"), url: obj.get("url"), name: obj.get("name"), productname: decodeURIComponent(obj.get("productname")), ripname:obj.get("ripname"), printermakename:obj.get("printermakename"), printername: obj.get("printername")});
					});

					
				} else {
					Ext.DomHelper.append('iccprofileresults', "<div id='search-result-item'>No results found for your search criteria. Please try broadening your search criteria.</div>");
				}

				Ext.DomHelper.append('iccprofileresults', "<div id='search-result-separate'>&nbsp;</div>");
				startMask.hide();
			});

			resultsStore.load();
		},
		returnPrinterMake : function(){
			var formv = form.getValues();
			return formv.printerMake;
		},
		returnProduct: function(){
			var formv = form.getValues();
			return formv.product_id;
		},
		returnPrinter: function(){
			var formv = form.getValues();
			return formv.printerModel;
		},
		returnRIP: function(){
			var formv = form.getValues();
			return formv.rip_id;
		}
	};
}();
Ext.onReady(profileSearchForm.init, profileSearchForm, true);
// end of file
