/* News verschicken*/
var recommendForm;
var aVals;

function doRecommend(pk,dr){
	nCid = getVarFromUrlScope('cid');
	
	cVals = new Ext.data.JsonStore({
		url: '/index.cfm?event=recommend.show&cid='+ nCid +'&rec_pk=' + pk + '&rec_dr=' + dr,
		autoLoad: true,
		root: 'data',
		totalProperty: 'totalcount',
		fields: ['pk','title','description','name','email'],
		listeners: {
			load: showRecommendPopup
		}});
}


function recommendContent(){
	
	var myProfileGeneralLabelStyle = 'width:210px;';
	
	recommendForm = new Ext.FormPanel({
		renderTo: 'recommend_div',
 		id: 'frm_recommend',
		frame:true,
 		autoWidth: true,
 		labelWidth: 198,
		/*
		renderTo: 'recommend_div',
		defaults: {width: 650 },       
	    labelWidth: 225,
	    layoutConfig: {labelSeparator: ''},
		bodyStyle:'background-color:transparent;border:0px none;',
		style: 'margin-top: 10px;',			
				
		id: 'frm_recommend',	
		*/
		url: '/index.cfm?event=ajax.recommend.send',
		items: [
		
			{
				xtype: 'hidden',
				id: 'rec_pk',
				value: aVals.pk
			},
			{
				xtype: 'hidden',
				id: 'rec_dr',
				value: aVals.dr
			},
			
			{
				width: 608,
				layout: 'form',
				border: false,
				items: [{
					html: '<h2>Recommend ' + aVals['typename'] +'</h2>',
					style: 'color:#999999;'				
				}]
			},
			{
				xtype: 'panel',
				border: false,
				style: 'margin-left:0px;',
				html: '<strong>'+ aVals.title+'</strong><br/>'+ aVals.description + '<br/><br/>' 					
			},
			
			{
				xtype: 'textfield',
	 			fieldLabel: '*'+'Your e-mail address',
	 			labelSeparator: '',
	 			id: 'email',
	 			vtype: 'email',
	 			width: 320,
				value: aVals.email,
	 			labelStyle: myProfileGeneralLabelStyle,
				ctCls: 'cb pad0',
	 			allowBlank: false,
	 			msgTarget: 'under',
	 			blankText: 'Mandatory field!'+' '+'This field should contain an e-mail address. Format: "user@domain.com"'
			},
			{
				xtype: 'textfield',
	 			fieldLabel: '*'+'Your name',
	 			labelSeparator: '',
	 			id: 'name',
	 			width: 320,
				value: aVals.name,
	 			labelStyle: myProfileGeneralLabelStyle,
				ctCls: 'cb pad0',
	 			allowBlank: false,
	 			msgTarget: 'under',
	 			blankText: 'Mandatory field!'+' '+'Please enter your name!'				
			},		
			{
				xtype: 'textfield',
	 			fieldLabel: '*'+ unescape("E-mail address of the recipient"),
	 			labelSeparator: '',
	 			id: 'emailto',
	 			vtype: 'email',
	 			width: 320,
	 			labelStyle: myProfileGeneralLabelStyle,
				ctCls: 'cb pad0',
	 			allowBlank: false,
	 			msgTarget: 'under',
	 			blankText: 'Mandatory field!'+' '+'This field should contain an e-mail address. Format: "user@domain.com"'				
			},	
			{
				xtype: 'textarea',
	 			fieldLabel: '*'+'Your message (max. 500 characters)',
	 			labelSeparator: '',
	 			id: 'message',
	 			width: 320,
	 			height: 100,
	 			labelStyle: myProfileGeneralLabelStyle,
				ctCls: 'cb pad0',
	 			allowBlank: false,
	 			msgTarget: 'under',
	 			blankText: 'Mandatory field!'+' '+'Please enter your message!',
	 			maxLength: 500
			},
			
			
			{
 			xtype: 'hidden',
 			id: 'captchacheckstring',
 			value: ''
	 		}
			
			,{
				width: 538,
				layout: 'form',
				border: false,
				items: [{
					html: '<span class="fll">*' + 'Securitycode</span> <span class="flr"><a href="javascript:void(0);" onclick="Ext.getCmp(\'captchaimg\').fireEvent(\'render\',Ext.getCmp(\'captchaimg\'))">Request new code</a></span>',
					style: 'font-size: 12px;margin-right: 5px;'
				}]
			},{
				xtype: 'panel',
				id: 'captchaimg',				
	        	listeners: {
	        		render: function(pThis){
	        			Ext.Ajax.request({
	        				url: '/index.cfm?event=ajax.getContactFormCaptcha&random=' + Math.random(),
	        				success: function(pR,pO){
	        					var cData = Ext.decode(pR.responseText);
	        					var cTpl = new Ext.XTemplate(
	        						'<img src="captchaimages/{img}" width="320" height="50" border="0" alt="Captcha" style="margin-bottom:14px;" />'
	        					);
	        					cTpl.overwrite(pThis.body,cData);							
	        					Ext.getCmp('captchacheckstring').setValue(cData.captchacheckstring);
	        				},
							failure: function(){alert('error');}
	        			});					
	        		}
	        	}
			},{
	 			xtype: 'textfield',
	 			style: 'margin-top:5px;',
	 			fieldLabel: '*'+'Please enter the code!',
	 			labelSeparator: '',
	 			id: 'verificoderepeat',
	 			width: 320,
	 			labelStyle: 'width:210px;',
				ctCls: 'cb pad0',
	 			allowBlank: false,
	 			msgTarget: 'under',
	 			blankText: 'Mandatory field!'+' '+'Please enter the securitycode!'
	 		},
			
			
			
			{
				xtype: 'panel',
				border: false,							
				html: '<div class="line_dotted mt16"><!--leer--></div>'
            		+ '<div align="right" class="pt16">'
					+ '	<a class="submit" href="#" onclick="recommendForm.doSubmit(); return false;">GO</a>'
					+ '	<a class="submit" href="#" onclick="recommendForm.doAbort(); return false;" style="margin-right:10px;">cancel</a>'
					+ '</div>',
				style: 'font-size: 12px;margin-right: 5px;'
			}				
		],
	
		key: {
			key: [10, 13],
			fn: function(){
				recommendForm.doSubmit();
			}
		},
		// Submit:
		doSubmit: function(){
			if (recommendForm.validate()) {
				recommendForm.getForm().submit({
					
						success: function (pForm, pAction){
							if(pAction){
								recommendForm.getForm().reset();
								Ext.MessageBox.alert("Success",aVals['success_text'],function(){recommendForm.doAbort();});								
							}else{
								Ext.MessageBox.alert('Error','Error while sending!');
							} 
						},
						failure: function(pForm, pAction){
							if(pAction.result){
								if(pAction.result.successnumber == 1){
									Ext.MessageBox.alert('Error','Error while sending!'+'<br>'+'Please enter the correct securitycode!');
								}
								
								if(pAction.result.successnumber == 2){
									Ext.MessageBox.alert('Error','Error while sending!'+'<br>'+'');
								}
							}else{
								Ext.MessageBox.alert('Error','Error while sending!'+'<br>'+ unescape("Please fill in all mandatory fields"));
							}
						}						
					}
				);
			}
		},
		// Abort:
		doAbort: function(){
			//recommendForm.getForm().reset();
			//fpRecommendDataFormPopup.close();
			window.location.href = '/index.cfm?event='+ aLocation['lastevent'] + '&cid=' + aLocation['cid'] + '&pk=' + aVals.pk;
		},
		
		
		// Validierung:
		validate: function(){
			return true;
		},
		showError: function(fieldName, Msg){
			Ext.MessageBox.alert('Fehler', Msg, function(){
				recommendForm.getForm().findField(fieldName).focus();
			});
		}
		
		 /*
		 ,listeners: {
			render: function(pThis){
				dmy = Ext.get('recommend_topline');
				dmy.dom.innerHTML = 'Recommend ' + aVals['typename'];
			}
		}
		*/
		
		})
		
}
	
	
	
	