
Ext.onReady(function(){
	Ext.debugconsole("Showhomes Franchisee","console");
	
	this.appStatus.sid = Ext.getUrlParam('sid');
	
	var parserobj = new XMLParser();
	var parser = new XMLParser();
	var office_id = Ext.getUrlParam('office_id');
	var noevents = Ext.getUrlParam('noevents'); 
	var url = 'activepages/pages/mainrequest.php';
	var home_id = Ext.getUrlParam('home_id');
	var conn = new Ext.data.Connection(); 
	var parsetxt;
	var regionname;

	if(this.appStatus.sid == "" && office_id == "" && window.document.activeElement.baseURI.search("locations.php") == -1){
		//window.location = "http://"+window.location.host; // this was causing problems with urls like: http://www.showhomesnashville.com with no office id
	}
	
	//Ext.QuickTips.init();
	
	
	
	Ext.debugconsole("Calling mainrequest","console");
	//alert(this.appStatus.inited);
	//alert(this.appStatus.page);
	conn.request({
		url: url, // the page and the sid
		method: 'POST',
		params: {
			sid:this.appStatus.sid,
			office_id:office_id,
			home_id:home_id,
			noevents:noevents
		},
		success: function(responseObject){
			Ext.debugconsole("Response: "+responseObject.responseText,"console");
			//alert("project.js (38)");

			parserobj.XMLParse(responseObject.responseText);
			if(parserobj.XMLTagExists('Result') && parserobj.XMLValue('Result') == "success") {
				//alert("project.js (42)");
				if(parserobj.XMLTagExists('sid')) {
					this.appStatus.sid = parserobj.XMLValue('sid');
				}
				if(parserobj.XMLTagExists('page')) {
					this.appStatus.page = parserobj.XMLValue('page');
				}
				
				
				// populate the page
				
				// all pages
				regionname = "";
				
				/*if(parserobj.XMLTagExists('fran_region_name')) {
	
					regionname = parserobj.XMLValue('fran_region_name');
					var shlocation = Ext.get('shlocation');
					if(shlocation){
						//shlocation.dom.innerHTML = Ext.replaceall("shlocation","<!--  [franchise_name] -->","Showhomes - "+regionname);
						var temptag = "shlocation";
						shlocation.dom.innerHTML = Ext.replaceall(temptag,"<!--  [franchise_name] -->","Showhomes - "+regionname);
						 
					}
				}*/
				
				
				// #all pages
			
				// specific pages
				//Ext.Msg.alert("Debugging",window.location.host);
				switch(this.appStatus.page){
					case "index.php":
						
						// intro
						if(regionname != ""){
							//Ext.get('intro').dom.innerHTML = Ext.replaceall("intro","<!-- [franchise_name] -->",regionname);
						}
						// #intro
						
						// upcoming events
						if(parserobj.XMLTagExists('events')) {
							parsetxt = parserobj.XMLValue('events');
							if(parsetxt != ""){
								//Ext.Msg.alert("Debugging",parsetxt);
								var divObj = Ext.get('sidebar');
								var divObj2 = Ext.get('home_col1');
								var divObj3 = Ext.get('home_col2');
								var divObj4 = Ext.get('sidebar_content');
								
								divObj4.dom.innerHTML = parsetxt;
								divObj.setStyle('width', '220px');
								divObj2.setStyle('width', '305px');
								divObj3.setStyle('width', '305px');
								divObj.show();
							} 
						} 
						// #upcoming events
						/*
						// Paragraph tags
						if(parserobj.XMLTagExists('meta_hdr1')) {
							parsetxt = parserobj.XMLValue('meta_hdr1');
							Ext.get('home_col1').dom.innerHTML = Ext.replaceall("home_col1","<!-- [meta_hdr1] -->",parsetxt);
						}
						if(parserobj.XMLTagExists('meta_hdr2')) {
							parsetxt = parserobj.XMLValue('meta_hdr2');
							Ext.get('home_col2').dom.innerHTML = Ext.replaceall("home_col2","<!-- [meta_hdr2] -->",parsetxt);
						}
						if(parserobj.XMLTagExists('meta_locale_para')) {
							parsetxt = parserobj.XMLValue('meta_locale_para');
							Ext.get('home_col2').dom.innerHTML = Ext.replaceall("home_col2","<!-- [meta_locale_para] -->",parsetxt);
						}
						if(parserobj.XMLTagExists('meta_footer')) {
							parsetxt = parserobj.XMLValue('meta_footer');
							Ext.get('home_col2').dom.innerHTML = Ext.replaceall("home_col2","<!-- [meta_footer] -->",parsetxt);
						}
						// paragraph tags
						*/
						break;
					case "homeowner.php":
					case "realtor.php":
						break;
					
					case "homemanager.php":
						break;
						
					case "aboutus.php":
						break;
						
						
					case "press.php":
						if(parserobj.XMLTagExists('movie_switcher')) {
							parsetxt = parserobj.XMLValue('movie_switcher');
							//Ext.get('movie_switcher').dom.innerHTML = parsetxt;
						}
						
						if(parserobj.XMLTagExists('articles')) {
							parsetxt = parserobj.XMLValue('articles');
							//Ext.get('articles').dom.innerHTML = parsetxt;
						}
						
						if(parserobj.XMLTagExists('press')) {
							parsetxt = parserobj.XMLValue('press');
							//Ext.get('press').dom.innerHTML = parsetxt;
						}
						break;
						
					case "careers.php":
						if(regionname != ""){
							Ext.get('employment_ops').dom.innerHTML = Ext.replaceall("employment_ops","<!--  [franchise_name] -->"," - "+regionname);
						}
						if(parserobj.XMLTagExists('careers')) {
							parsetxt = parserobj.XMLValue('careers');
							//Ext.get('career_list').dom.innerHTML = parsetxt;
						}
						break;
						
					case "testimonials.php":
						if(parserobj.XMLTagExists('testimonial_list')) {
							parsetxt = parserobj.XMLValue('testimonial_list');
							//Ext.get('testimonial_list').dom.innerHTML = parsetxt;
						}
						break;
						
					case "contactus.php":
						if(parserobj.XMLTagExists('general_info')) {
							parsetxt = parserobj.XMLValue('general_info');
							//Ext.get('general_info').dom.innerHTML = parsetxt;
						}
						if(parserobj.XMLTagExists('social_info')) {
							parsetxt = parserobj.XMLValue('social_info');
							//Ext.get('social_info').dom.innerHTML = parsetxt;
						}
						if(parserobj.XMLTagExists('badges')) {
							parsetxt = parserobj.XMLValue('badges');
							//Ext.get('badges').dom.innerHTML = parsetxt;
						}
						break;
						
					case "viewshowhomes.php":
						
/*						
						var homesdata = [
							['1','one','Gilmour','x'],
							['2','two','Waters','x'],
							['3','three','Write','x'],
						]
						
						var store = new Ext.data.JsonStore({
							data: homesdata,
							fields: [
								{name: 'id', type: 'int'},
								'name', 'position', 'ambition'
							]
						});
					  
						// load data from the url ( data.php )
						store.load();
						
						var test = store.getAt(1);
						var test2 = test.data['position'];
						var test3 = test.position
*/					
						if(parserobj.XMLTagExists('homessort_default')) {
							parsetxt = parserobj.XMLValue('homessort_default');
							//Ext.get('showhome_list').dom.innerHTML = parsetxt;
						}
						
						Ext.fetchpages("beds");		
												
						// build the array
						
						// sort the array
						
						// build the innerHTML
						
						break;
					case "viewshowhomedetails.php":
						
						break;
						
				}
				// #specific pages
				
				
				// #populate the page	
			}
		}
	})

	/*
	Ext.get('shlocation').dom.innerHTML = Ext.replaceall("shlocation","<!--  [franchise_name] -->","Showhomes - Whatever");
	
	// header/footer
	Ext.get('navigation').dom.innerHTML = Ext.replaceall("navigation","<a href=\"aboutus.php\">","<a href=\"aboutus.php?sid=123456\">");
	Ext.get('footer').dom.innerHTML = Ext.replaceall("footer","<a href=\"aboutus.php\">","<a href=\"aboutus.php?sid=123456\">");

	
	// tabs
	Ext.get('tabs').dom.innerHTML = Ext.replaceall("tabs","href=\"homemanager.php\"","href=\"homemanager.php?sid=1234567\"");
	*/
	//var locateshowhomes = Ext.get('locateshowhomes');
	Ext.form.VTypes.nameVal  = /^([A-Z]{1})[A-Za-z\-]+ ([A-Z]{1})[A-Za-z\-]+/;
	Ext.form.VTypes.nameMask = /[A-Za-z\- ]/;
	Ext.form.VTypes.nameText = 'In-valid Director Name.';
	Ext.form.VTypes.name 	= function(v){
		return Ext.form.VTypes.nameVal.test(v);
	};
	
	Ext.form.VTypes.zipVal  = /^(\d{5})*$/; // ^([zZ]\d{3})*$
	Ext.form.VTypes.zipMask = /[0-9]/;
	Ext.form.VTypes.zipText = 'In-valid Zip Code. Must be 6 numbers.';
	Ext.form.VTypes.zip 	= function(v){
		return Ext.form.VTypes.zipVal.test(v);
	};
	
	
		
	var win_locate;
	var locate_office = function(){
		var my_values = [
		     ['al','Alabama'],
	            ['ak','Alaska'],
	            ['az','Arizona'],
	            ['ar','Arkansas'],
	            ['ca','California'],
	            ['co','Colorado'],
	            ['ct','Connecticut'],
	            ['dc','D.C.'],
	            ['de','Delaware'],
	            ['fl','Florida'],
	            ['ga','Georgia'],
	            ['hi','Hawaii'],
	            ['id','Idaho'],
	            ['il','Illinois'],
	            ['in','Indiana'],
	            ['ia','Iowa'],
	            ['ks','Kansas'],
	            ['ky','Kentucky'],
	            ['la','Louisiana'],
	            ['me','Maine'],
	            ['md','Maryland'],
	            ['ma','Massachusetts'],
	            ['mi','Michigan'],
	            ['mn','Minnesota'],
	            ['ms','Mississippi'],
	            ['mo','Missouri'],
	            ['mt','Montana'],
	            ['ne','Nebraska'],
	            ['nv','Nevada'],
	            ['nh','New Hampshire'],
	            ['nj','New Jersey'],
	            ['nm','New Mexico'],
	            ['ny','New York'],
	            ['nc','North Carolina'],
	            ['nd','North Dakota'],
	            ['oh','Ohio'],
	            ['ok','Oklahoma'],
	            ['or','Oregon'],
	            ['pa','Pennsylvania'],
	            ['ri','Rhode Island'],
	            ['sc','South Carolina'],
	            ['sd','South Dakota'],
	            ['tn','Tennessee'],
	            ['tx','Texas'],
	            ['ut','Utah'],
	            ['vt','Vermont'],
	            ['va','Virginia'],
	            ['wa','Washington'],
	            ['wv','West Virginia'],
	            ['wi','Wisconsin'],
	            ['wy','Wyoming']
	
		]
		
		var locationcancel = function(button,event) {
		  win_locate.hide();
		  alignwin = true;
		 };
		 
		 var handleCombo = function(){
		 	Ext.debugconsole("handleCombo","console");
		 	//var form = win_locate.getForm();
	        //var comboField = win_locate.findField('state');
	        
	        //var comboValue = comboField.getValue();
		 }
		 
		if(!win_locate){
			var alignwin = true;
			win_locate = new Ext.Window({
				layout: 'form',
				id:'Win_Locate',
				url:'activepages/locate_showhomes.inc',
				labelWidth:60,
				/*layout: {
				    type: 'vbox',
				    padding: '5',
				    align: 'left'
				},*/
				bodyStyle:'padding:0 18px 0 0',
				height:150,
				width:300,
				title: 'Locate a Showhomes\' office' ,
				//x:700,
				//html:'<h1>Oh</h1><p>Hi There Everyone.</p>',
				items:[
					//{xtype: 'textfield', fieldLabel: '&nbsp;&nbsp;Zip Code',width:50},
					new Ext.form.NumberField({
						xtype: 'textfield',
						fieldLabel: '&nbsp;&nbsp;Zip Code',
						width:50,
						vtype: 'zip'
						/*layout: {
						    padding: '5',
						    align: 'left'
						}*/
					}),
					
					
					
					/* vtype: 'name' */
					new Ext.form.Label({
						html: '&nbsp;&nbsp;OR',
						layout: {
						    padding: '5',
						    align: 'left'
						}
					}),
					//new Ext.form.NumericField({fieldLabel: 'Surname'}),
					new Ext.form.ComboBox({
					    fieldLabel: '&nbsp;&nbsp;State',
					    hiddenName: 'state',
					    name: 'state',
					    store: new Ext.data.SimpleStore({
					        fields: ['abbr','state'],
					        data : my_values 
					    }),
					    displayField: 'state',
					    typeAhead: true,
					    forceSelection:true,
					    mode: 'local',
					    triggerAction: 'all',
					    emptyText:'Choose state...',
					    selectOnFocus:true,
					    listeners: {
			                blur:{
			                    fn: handleCombo
			                }
			            }
					   })
		
				],
				buttons: [
		         {text:"Cancel",handler:locationcancel},
		         {text:"Save",
		         handler: function(){
					this.getForm().submit({
					success: function(f,a){
						Ext.Msg.alert('Success', 'It worked');
					},
					failure: function(f,a){
						Ext.Msg.alert('Warning', 'Error');
					}
					});
				}}
		      ]
			});
		}
		
		win_locate.show();
		if(alignwin){
			alignwin = false;
			win_locate.alignTo('additional_links','tr',[-170,-130,10,10]);
		}
		
		
		
	}
	//Ext.get('locateshowhomes').on('click', locate_office());
	var locateshowhomes = Ext.get('locateshowhomes');
	if(locateshowhomes){
		locateshowhomes.on('click',function(){
			//alert("test");
			locate_office();
		});
	}
});


	/*var locationcancel = function(button,event) {
	  alert('You clicked the cancel button!  '+locate_office.w);
	 };*/
