
/*
Usage:
Ext.onReady(function(){
	var emailThis  = new ASC.Fad.EmailThisPage(); 
	Ext.get('id_Email_Element').on('click',emailThis.showEmailWindow );
	
});
*/
ASC.namespace('ASC.Fad.EmailThisPage');
ASC.Fad.EmailThisPage = ASC.extend(Ext.util.Observable,{
	submitUrl : null,
	
	constructor: function (config)
	{
		ASC.Fad.EmailThisPage.superclass.constructor.apply(this);
		ASC.apply(this, config);	
		this.emailForm = null;
		this.emailWindow = null;
	},
	
	/*
	 *  This function will show send email window
	 */
	showEmailWindow : function (){
		Ext.QuickTips.init();
	    Ext.form.Field.prototype.msgTarget = 'under';
	    
	    if(!this.emailForm){
	    	var emailsRegExp = /^([\w]+)(.[\w]+)*@([\w-]+\.){1,5}([A-Za-z]){2,4}[\s]*(,[\s]*([\w]+)(.[\w]+)*@([\w-]+\.){1,5}([A-Za-z]){2,4}[\s]*)*$/;
	    	this.emailForm = new Ext.FormPanel({
	    		labelWidth: 75, 
	            url: this.submitUrl,
	            buttonAlign:'right',
	            defaults: { labelSeparator: ' ' },
	            frame:true,
	            bodyStyle:'padding:10px 5px 0; text-align:left;',
	
	            items: [{
		            	layout:'form',
		            	width: 'auto',
		            	defaultType: 'textfield',
		            	items:[{
		                	anchor:'95%',
			                fieldLabel: 'To* ',
			                name: 'emailTo',
			                allowBlank:false,
			                regex : emailsRegExp,
			                regexText : ' Please verify the email address and separate multiple email addresses with a comma'
			             },{
			            	 anchor:'95%',
			                fieldLabel: 'From* ',
			                name: 'emailFrom',
			                allowBlank:false,
			                vtype:'email',
			                vtypeText:' Please verify the email address'
			             },{
			            	 anchor:'95%',
			            	 fieldLabel: 'Subject',
			            	 name: 'subject'
			             }]
	                },{
	                	labelAlign: 'top',
		            	layout:'form',
		            	width: 'auto',
		            	defaultType: 'textfield',
		            	items:[
		            		 new Ext.form.TextArea({
		            			 id : '_EmailThisPage_Message',
				            	 fieldLabel: 'Message',
				            	 anchor:'95%',
				            	 name: 'message',
				            	 height: 150
			            	     })
			             ]
	                }
	            ],
	          buttons: [{
		             text: 'SEND',
		             formBind :true,
		             type: 'submit',
		             
		             handler: function(){
		             	ASC.Chrysler.trackLinkMetrics('email_form','email_submit');
	        	  		this.emailForm.getForm().doAction('submit',{
		                    method:'POST', 
		                    waitMsg :'Sending data...',
							success:function(form,action){
							   if (action.result.result=='SUCCESS') {
								   this.emailWindow.hide();
								   Ext.Msg.show({
									   title: 'Success',
									   msg: 'Your email has been send.',
									   width: 480,
									   buttons: {ok:'CLOSE WINDOW'},
									   icon: Ext.MessageBox.INFO
									});
							   } else {
								   Ext.Msg.show({
									   title: 'Failed',
									   msg: 'email failed to be sended.',
									   width: 480,
									   buttons: {ok:'CLOSE WINDOW'},
									   icon: Ext.MessageBox.WARNING 
									});
							   }
							}.createDelegate(this),
							failure:function(form,action){
								if( action.failureType == Ext.form.Action.CLIENT_INVALID  ){
									   Ext.Msg.show({
										   title: 'error',
										   msg: 'client side validation Failed,please verify your input.',
										   width: 480,
										   buttons: {ok:'CLOSE WINDOW'},
										   icon: Ext.MessageBox.WARNING 
										});
								}else{
									this.emailWindow.hide();
									   Ext.Msg.show({
										   title: 'error',
										   msg: 'server side error,you may try again later.',
										   width: 480,
										   buttons: {ok:'CLOSE WINDOW'},
										   icon: Ext.MessageBox.WARNING
										});
								}
							}.createDelegate(this)  
						});
		              }.createDelegate(this)                                                    
		          }]
	       });   
		}
	    if(!this.emailWindow){
	    	this.emailWindow = new Ext.Window({
	    		title:"Email This Page",
	    		layout:'fit',    
	    		width:480,
	    		height:350,
	    		resizable :false,
				constrainHeader: true,
				draggable: true,
	    		modal:true, 
	    		border : true,
	    		plain:true,
	    		maximizable:false, 
	    		closeAction:'hide',
	    		plain: true,
	    		items: this.emailForm 
	    	});
	    	this.emailWindow.on('hide', function () {
             	ASC.Chrysler.trackLinkMetrics('email_form','email_close');
	    	});
	    }
	    this.emailWindow.show(); 
	},
	getMessage : function(){
		return this.emailForm.findById("_EmailThisPage_Message").getValue();
	},
	setMessage : function(msg){
		this.emailForm.findById("_EmailThisPage_Message").setValue(msg);
	}
});
