/*********************
	Home page product sort
*********************/

//prodSortForm
var prodSortForm = function(){

	 var iconPath = '../imgs/icons/';
    
    // define some private variables
    var form1, form2, form3;
    var appStore, appCheckboxArray;
	var userClassStore, userclassRadioArray;
	

    // return a public interface
    return {
        init : function(){
			//ARRAYS
			appCheckboxArray = new Array();
			userclassRadioArray = new Array();

			userClassStore = new Ext.data.Store({
					baseParams: {action: 'returnClassifications'},
					proxy: new Ext.data.HttpProxy({url: 'ajax-load-sort-options.php'}),
					reader: new Ext.data.XmlReader({
					record: 'classification',
					id: 'classid'
					}, ['name', 'classid'])
			});
		
			appStore = new Ext.data.Store({
					baseParams: {action: 'loadapps'},
					proxy: new Ext.data.HttpProxy({url: 'ajax-load-sort-options.php'}),
					reader: new Ext.data.XmlReader({
					totalRecords: 'totalCount',
					record: 'application',
					id: 'appid'
					}, ['name', 'indoor','outdoor'])
			});

			userClassStore.on("load",function(ds, ecords,opts){
				//create checkboxes and put them in checkbox array
				for(i=0; i< ds.getCount(); i++){
					var curItem_id = ds.getAt(i).id;
					var curItem_name =  ds.getAt(i).get("name");
					var checked = false;
					if(curItem_id == 3){
						checked = true;
					} else {
						checked = false;
					}

					var thisRadioButton = new Ext.form.Radio({
						boxLabel: curItem_name,
						checked: checked,
						name: 'userClassification',
						inputValue: curItem_id,
						id: curItem_name,
						listeners:{
							'check' : function(thisbox, newval){
								//record selection in hitslink
								prodSortForm.saveUserClassification(thisbox.getId());
							}
						}
					});
					userclassRadioArray.push(thisRadioButton);
				}
				//end make checkbox

				appStore.load();
			});


			appStore.on("load",function(ds, ecords,opts){
				var max_per_row = 7;
				var total_num = ds.getCount();
				var num_rows = Math.ceil(total_num/max_per_row);
				var count = 1;
				var temparray = new Array();
				
				//create checkboxes and put them in checkbox array
				for(i=0; i< ds.getCount(); i++){

					var curItem_id = ds.getAt(i).id;
					var curItem_name =  ds.getAt(i).get("name");

					var thisCheckBox = new Ext.form.Checkbox({
						boxLabel: curItem_name,
						checked: false,
						name: 'productApp',
						inputValue: curItem_id,
						id: curItem_name,
						listeners:{
							'check' : function(thisbox, newval){
								numView.addFilters();
								prodSortForm.saveUserChecked(thisbox.getId(), newval);
							}
						}
					});
					temparray.push(thisCheckBox);
					count++;

					if(count > max_per_row){
						appCheckboxArray.push(temparray);
						count = 1;
						temparray = Array();
					}
				}

				appCheckboxArray.push(temparray);
				
				//end make checkbox
			
				//load the form
				prodSortForm.makeForm();
			});

			userClassStore.load();

		},
	makeForm: function(){
			
			Ext.QuickTips.init();

			 // turn on validation errors beside the field globally
			Ext.form.Field.prototype.msgTarget = 'side';

			form1 = new Ext.form.Form({
							name: 'selectuserClassForm',
							id: 'selectuserClassForm',
						//	url: 'process_savepage.php',
							method: "post",
							labelWidth: 1 //to hide the labels
				});

				form1.column({id:'userclasspanel', width:160, labelAlign:'right', labelSeparator:''});
					//user classification fieldset
				//	form1.fieldset({id:'app-panel', name:'panel1', labelSeparator:'', legend:''});
						for(var i=0; i<userclassRadioArray.length; i++){
							form1.add(userclassRadioArray[i]);
						}
				//	form1.end();
				form1.end();
				
				form1.render('find-box-1-form');


			form2 = new Ext.form.Form({
							name: 'selectAppForm',
							id: 'selectAppForm',
						//	url: 'process_savepage.php',
							method: "post",
							labelWidth: 1 //to hide the labels
				});
			//	form2.column({id:'app-panel', width:380, labelAlign:'right', labelSeparator:''});
						for(var i=0; i<appCheckboxArray.length; i++){
							form2.column({width:'135',labelAlign:'right', labelSeparator:''});
								for(var j=0; j<appCheckboxArray[i].length;j++){
									form2.add(appCheckboxArray[i][j]);
								}
							form2.end();
						}
		//		form2.end();
				
				form2.render('find-box-2-form');

				numView.init();

		},
	returnApps : function(){
			var formv = form2.getValues();
			return Ext.encode(formv.productApp);
	},
	returnClassifications : function(){
		var formv = form1.getValues();
		return Ext.encode(formv.userClassification);
	},
	saveUserClassification: function(classificationname){
		wa_account="magichits"; wa_location=24;
		wa_pageName = 'I am a ' +classificationname;  // you can customize the page name here
		document.cookie='__support_check=1';wa_hp='http';
		wa_rf=document.referrer;wa_sr=window.location.search;
		wa_tz=new Date();if(location.href.substr(0,6).toLowerCase()=='https:')
		wa_hp='https';wa_data='&an='+escape(navigator.appName)+ 
		'&sr='+escape(wa_sr)+'&ck='+document.cookie.length+
		'&rf='+escape(wa_rf)+'&sl='+escape(navigator.systemLanguage)+
		'&av='+escape(navigator.appVersion)+'&l='+escape(navigator.language)+
		'&pf='+escape(navigator.platform)+'&pg='+escape(wa_pageName);
		wa_data=wa_data+'&cd='+
		screen.colorDepth+'&rs='+escape(screen.width+ ' x '+screen.height)+
		'&tz='+wa_tz.getTimezoneOffset()+'&je='+ navigator.javaEnabled();
		wa_img=new Image();wa_img.src=wa_hp+'://counter.hitslink.com/statistics.asp'+
		'?v=1&s='+wa_location+'&acct='+wa_account+wa_data+'&tks='+wa_tz.getTime();
		//document.getElementById('wa_u').src=wa_hp+'://counter.hitslink.com/track.js';
		wa_hp+'://counter.hitslink.com/track.js';
	},
	saveUserChecked: function(appname, newval){
		var resText = "";
		if(newval != true){
			resText = "(unchecked) I would like to print: " + appname;
		}else{
			resText = "(checked) I would like to print: " + appname;
		}
		wa_account="magichits"; wa_location=24;
		wa_pageName = resText;  // you can customize the page name here
		document.cookie='__support_check=1';wa_hp='http';
		wa_rf=document.referrer;wa_sr=window.location.search;
		wa_tz=new Date();if(location.href.substr(0,6).toLowerCase()=='https:')
		wa_hp='https';wa_data='&an='+escape(navigator.appName)+ 
		'&sr='+escape(wa_sr)+'&ck='+document.cookie.length+
		'&rf='+escape(wa_rf)+'&sl='+escape(navigator.systemLanguage)+
		'&av='+escape(navigator.appVersion)+'&l='+escape(navigator.language)+
		'&pf='+escape(navigator.platform)+'&pg='+escape(wa_pageName);
		wa_data=wa_data+'&cd='+
		screen.colorDepth+'&rs='+escape(screen.width+ ' x '+screen.height)+
		'&tz='+wa_tz.getTimezoneOffset()+'&je='+ navigator.javaEnabled();
		wa_img=new Image();wa_img.src=wa_hp+'://counter.hitslink.com/statistics.asp'+
		'?v=1&s='+wa_location+'&acct='+wa_account+wa_data+'&tks='+wa_tz.getTime();
		//document.getElementById('wa_u').src=wa_hp+'://counter.hitslink.com/track.js';
		wa_hp+'://counter.hitslink.com/track.js';

	}
	};
}();


var numView = function(){
	var view;
	var ds_products;

	return {
        init : function(){

			 // create the Data Store
			ds_products = new Ext.data.Store({
				// load using HTTP
				proxy: new Ext.data.HttpProxy({url: 'ajax-find-products.php'}),
				//baseParams
				baseParams: {action:'returnHomeNum'},//{action:'returnHomeNum'},
				// the return will be XML, so lets set up a reader
				reader: new Ext.data.JsonReader({
				    // The property which contains the total dataset size (optional)
					root: "number"               // The property which contains an Array of row objects
					},['numproducts'])
						});

				ds_products.on('beforeload', function() {
					ds_products.baseParams = { // modify the baseParams setting for this request
						action:'returnHomeNum',
						applications: prodSortForm.returnApps(),
						classification: prodSortForm.returnClassifications()
					};
				});


				numView.showNum();
		},
		showNum : function(){
			// create the required templates

			var numTemplate = new Ext.Template(
			'<div class="num-found">{numproducts}</div>'
			);
			numTemplate.compile();	

			view = new Ext.View("found-box-num", numTemplate, {
				singleSelect: true,
				//selectedClass: "ydataview-selected",
				store: ds_products
			});

			ds_products.load();

		},
		addFilters:function(){
			ds_products.reload();
		}
	};
}();

Ext.onReady(prodSortForm.init, prodSortForm, true);
// end of file