/*
 * Aunt Bertha Interview
 * Developed by: Erine A. Gray
 * 4/24/201
 * 
 * 
 */

Ext.BLANK_IMAGE_URL = '../extjs/resources/images/default/s.gif';

Ext.namespace('searchBertha');

Ext.onReady(function(){
    Ext.History.init();	
    var tokenDelimiter = ':';

    Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

    Ext.apply(Ext.form.VTypes,{ 
        phoneText: "Not a valid phone number.  Must be in the format 123-4567 or 123-456-7890.", 
        phoneMask: /[\d-]/, 
        phoneRe: /^(\d{3}[-]?){1,2}(\d{4})$/, 
        phone : function (v) { 
            return this.phoneRe.test(v); 
        }, 
    
        numericText: "Only numbers are allowed.", 
        numericMask: /[0-9]/, 
        numericRe: /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/, 
        numeric :function (v) { 
            return this.numericRe.test(v); 
        } , 
    
        decNumText: "Only decimal numbers are allowed.", 
        decNumMask: /(\d|\.)/, 
        decNumRe: /\d+\.\d+|\d+/, 
        decNum : function (v) { 
            return this.decNumRe.test(v); 
        } 
    }); 

    Ext.apply(Ext.form.VTypes, {
    	email_verification: function(val, field) {
    		if (field.initialEmailField) {
    			var email = Ext.getCmp(field.initialEmailField);
    			
    			if (val == "knownlogin@auntbertha.com"){
    				return true;
    			}
    			else {
    				return (val == email.getValue());
    			}
    		}
    		return true;
    	},
    	email_verificationText: 'The email addresses entered do not match!'
    });

	// Build Registration Cards
    var termsMessage = new Ext.Panel ({
        id		  : 'terms_message',
        renderTo  : 'terms',
        bodyStyle : 'padding:5px 5px 0; padding-top: 2px;padding-bottom: 4px;margin-left: 20px;font-family:Helvetica, Arial, sans-serif; font-size: 12px;text-align: center;',
    	html      : 'By continuing, you agree to Aunt Bertha\'s <a href="terms" target="_blank">terms of service</a>.',
    	hidden	  : false	
    });

	var postalCodeForm = new Ext.FormPanel({
    	width		: 450,
    	//xtype		: 'fieldset',
    	labelAlign  : 'left',
    	bodyStyle	: 'padding: 4px;padding-bottom: 0px;color: #5B5B5B;padding:0; font-family: Helvetica, Arial, sans-serif; font-size: 18px;',          				
        layout		: 'table',
        layoutConfig:{
            columns	: 3
	    },
	    defaults:{
	        cellCls:'center-align' // in stylesheet: .center-align {vertical-align:bottom;}
	    },
	    items		: [ {
	    					xtype			: 'textfield',
	    					name           	: 'function',
			                colspan			: 3,
			                hidden	 		: true,
							value 			: 'update_interview'
			          	},{
			          		xtype			: 'textfield',
			          		name           	: 'form',
			                colspan			: 3,
			                hidden	 		: true,
							value 			: 'location'
			          	},{
	        				xtype			: 'displayfield',
	        				colspan			: 1,
	        				columnWidth		: 10,
	        				//hideLabel		: true,
	        				html			: 'Zip:',
			                style			: 'padding-right: 16px;padding-left: 10px;text-align: right; font-family: helvetica, arial, sans-serif; font-size: 36px;'
			          	},{
			                id				: 'postal_code',
			                xtype			: 'textfield',
			                colspan			: 1,
			                style			: 'font-size: 42px;height: 48px;padding-top: 6px;margin-left: 10px;margin-right: 10px;',
			                validationEvent	: false,
			                name           	: 'postal',
			                width			: 140,
							vtype	       	: 'numeric',
			                minLength		: 5,
			                minLengthText	: "Please enter a 5-digit zip code.",
			                maxLength		: 5,
			                maxLengthText	: "Please enter a 5-digit zip code.",	                					
			                allowBlank     	: false
			          	}, new Ext.Button({
			                text		: '<span style="font-weight: bold;font-size: 18px;">Search</span>',
		                	//html		: '<p style="font-size: 30px;">a huge button</p>',
			                height		: 56,
		                	width		: 200,
		                	style		: 'padding-left: 6px;font-size: 48px;',
		                	//ctCls		: 'green-btn',
			                formBind	: true,	 
			                // Function that fires when user clicks the button 
			                handler:function(){ 
			                	postalCodeForm.getForm().submit({						    		                	
				                	clientValidation 	:  true,
								    url              	: 'rpcprif',
								    scripts				: 'true',						    
								    method				: 'POST',
								    success				: function(form, action) {
											                // Evaluate success
									    					var redirect = 'programs'; 
											                window.location = redirect;													                						    
								    },
								    failure				: function(form, action) {
													        switch (action.failureType) {
													            case Ext.form.Action.CLIENT_INVALID:
													            	if (Ext.isIE){
													            		alert ('Aww, Shucks! Please enter a valid zipcode (5 digits)!');
													            	}
													            	else {
													            		Ext.Msg.alert('Aww, Shucks!', 'Please enter a valid zipcode (5 digits)!');	
													            	}					                
													                break;
													            case Ext.form.Action.CONNECT_FAILURE:
													                Ext.Msg.alert('Aww, Shucks!', 'Ajax communication failed');
													                break;
													            case Ext.form.Action.SERVER_INVALID:
									                                obj = Ext.util.JSON.decode(action.response.responseText); 
									                                Ext.Msg.alert('Submission Failed!', obj.errors.reason); 
													       }
								    } 
			                	});
			                }         		    	     		   
			          	})	   
    	     		 ],
    	            keys: [
	    	                { 
	    	                	key: [Ext.EventObject.ENTER], handler: function() {
				                	postalCodeForm.getForm().submit({						    		                	
					                	clientValidation 	:  true,
									    url              	: 'rpcprif',
									    scripts				: 'true',						    
									    method				: 'POST',
									    success				: function(form, action) {
												                // Evaluate success
										    					var redirect = 'programs'; 
												                window.location = redirect;													                						    
									    },
									    failure				: function(form, action) {
														        switch (action.failureType) {
														            case Ext.form.Action.CLIENT_INVALID:
														            	if (Ext.isIE){
														            		alert ('Aww, Shucks! Please enter a valid zipcode (5 digits)!');
														            	}
														            	else {
														            		Ext.Msg.alert('Aww, Shucks!', 'Please enter a valid zipcode (5 digits)!');	
														            	}					                
														                break;
														            case Ext.form.Action.CONNECT_FAILURE:
														                Ext.Msg.alert('Aww, Shucks!', 'Ajax communication failed');
														                break;
														            case Ext.form.Action.SERVER_INVALID:
										                                obj = Ext.util.JSON.decode(action.response.responseText); 
										                                Ext.Msg.alert('Submission Failed!', obj.errors.reason); 
														       }
									    } 
				                	});
	    	                    }
	    	                }
	    	              ]
	});    
    
    var postalCodePanel = new Ext.Panel({
    	renderTo	: 'question_set',
    	labelWidth	: 170, // label settings here cascade unless overridden
    	width		: 450,
    	xtype		: 'fieldset',
    	bodyStyle	: 'padding: 4px;color: #5B5B5B;padding:0; font-family: Helvetica, Arial, sans-serif; font-size: 18px;',  
        items		: [
             		/*{
        				xtype			: 'displayfield',
        				colspan			: 2,
        				hideLabel		: true,
        		        style			: 'line-height: 1.5;padding-top: 0px;font-size: 20px;padding-bottom: 30px;font-family: Helvetica, Arial, sans-serif;text-align: normal;',
        				html			: 'Aunt Bertha picks up where Uncle Sam leaves off by making it easy to find food, health, housing and education programs.<br/><br/>'
        			},*/postalCodeForm
        			]		                   	
    });
	//Ext.getCmp('postal_code').focus('', 10);

});

//Google +
(function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
//Facebook
(function(d, s, id) {
	  var js, fjs = d.getElementsByTagName(s)[0];
	  if (d.getElementById(id)) return;
	  js = d.createElement(s); js.id = id;
	  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=226542604103475";
	  fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));
//Twitter

!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");

