/*
 * nbsp, AF | 07.07.2009 | Globales Kontaktformular
 * 
 */

var contactForm;

Ext.namespace('Ext.ContactStore');

function doContactForm(aForm){
 	 
 /*****************************************************************/ 

	var store = new Ext.data.SimpleStore({
        fields: ['PK', 'TITLE'],
        data : Ext.ContactStore.subjects
    });
	
	
	// Betreff
	var subject_combo = "";
	var mand = "";
	var sLabel = "";

	
	if(aForm['subject']['showinclient'] == 1) {
				
		if(aForm['subject']['ismandatoryfield'] == 1)
			mand = false;
		else
			mand = true;
		
		if(mand)
			sLabel = getLanguageText('contactform.form.subject.label');
		else
			sLabel = getLanguageText('contactform.form.subject.label') + "*";
		
		subject_combo = new Ext.form.ComboBox({
			width:333,
	        store: store,
	        typeAhead: true,
	        mode: 'local',
			id: 'subjects',
			hiddenName: 'subject',
	        forceSelection: true,
	        triggerAction: 'all',
	        emptyText: getLanguageText('contactform.form.subject.label.choose',unescape('Bitte ausw%E4hlen')),
	        selectOnFocus:true,        
			displayField:'TITLE',
			valueField:'PK',		
			fieldLabel: sLabel,
			labelSeparator: '', 		
	 		labelStyle: 'width:210px;',
			ctCls: 'cb pad0',
			allowBlank: mand,
			msgTarget: 'under',
			blankText: getLanguageText('contactform.form.subject.error'),
			maxLength: aForm['subject']['maxlength'] 		
	    });
	}
	else
		subject_combo = new Ext.form.Hidden({			
			id: 'subjects'					
	    });
	
	// Email
	var sEmail = "";
	
	if(aForm['email']['showinclient'] == 1) {
		
		if(aForm['email']['ismandatoryfield'] == 1)
			mand = false;
		else
			mand = true;
		
		if(mand)
			sLabel = getLanguageText('contactform.form.email.label');
		else
			sLabel = getLanguageText('contactform.form.email.label') + "*";
		
		sEmail = new Ext.form.TextField({
 			fieldLabel: sLabel,
 			labelSeparator: '',
 			id: 'email',
 			vtype: 'email',
 			width: 333,
 			validationEvent: 'blur',
			//value: aVals['email'],
 			labelStyle: 'width:210px;',
			ctCls: 'cb pad0',
			allowBlank: mand,
 			msgTarget: 'under',
 			blankText: getLanguageText('contactform.form.email.error'),
 			maxLength: aForm['email']['maxlength'] 
					
	    });
	}
	else
		sEmail = new Ext.form.Hidden({			
			id: 'email'	
	    });
	
	
	//Name
	var sNames = "";
	
	if(aForm['names']['showinclient'] == 1)  {
		
		if(aForm['names']['ismandatoryfield'] == 1)
			mand = false;
		else
			mand = true;
		
		if(mand)
			sLabel = getLanguageText('contactform.form.names.label');
		else
			sLabel = getLanguageText('contactform.form.names.label') + "*";
		
		sNames = new Ext.form.TextField({
			fieldLabel: sLabel,
			labelSeparator: '',
			id: 'name',
			width: 333,
			//value: aVals['name'],
			labelStyle: 'width:210px;',
			ctCls: 'cb pad0',
			allowBlank: mand,
			msgTarget: 'under',
			blankText: getLanguageText('contactform.form.names.error'),
			maxLength: aForm['names']['maxlength'] 
		});
	}
	else
		sNames = new Ext.form.Hidden({			
			id: 'name'
	    });
	
	//Firma
	
	var sCompany = "";
	
	if(aForm['company']['showinclient'] == 1) {
		
		if(aForm['company']['ismandatoryfield'] == 1)
			mand = false;
		else
			mand = true;
		
		if(mand)
			sLabel = getLanguageText('contactform.form.company.label');
		else
			sLabel = getLanguageText('contactform.form.company.label') + "*";
		
		sCompany = new Ext.form.TextField({
			fieldLabel: sLabel,
			labelSeparator: '',
			id: 'company',
			width: 333,
			labelStyle: 'width:210px;',
			ctCls: 'cb pad0',
			allowBlank: mand,
			msgTarget: 'under',
			blankText: getLanguageText('contactform.form.company.error'),
			maxLength: aForm['company']['maxlength'] 
		});
	}
	else
		sCompany = new Ext.form.Hidden({			
			id: 'company'
	    });
	
	//Telefon
	
	var sPhone = "";
	
	if(aForm['phone']['showinclient'] == 1) {
		
		if(aForm['phone']['ismandatoryfield'] == 1)
			mand = false;
		else
			mand = true;
		
		if(mand)
			sLabel = getLanguageText('contactform.form.phone.label');
		else
			sLabel = getLanguageText('contactform.form.phone.label') + "*";
		
		sPhone = new Ext.form.TextField({
			fieldLabel: sLabel,
			labelSeparator: '',
			id: 'phone',
			width: 333,
			labelStyle: 'width:210px;',
			ctCls: 'cb pad0',
			allowBlank: mand,
			msgTarget: 'under',
			blankText: getLanguageText('contactform.form.phone.error'),
			maxLength: aForm['phone']['maxlength'] 
		});
	}
	else
		sPhone = new Ext.form.Hidden({			
			id: 'phone'
	    });	
	
	// Abo 
	
	var sAbo = "";
	
	if(aForm['abo']['showinclient'] == 1) {
		
		if(aForm['abo']['ismandatoryfield'] == 1)
			mand = false;
		else
			mand = true;
		
		if(mand)
			sLabel = getLanguageText('contactform.form.abo.label');
		else
			sLabel = getLanguageText('contactform.form.abo.label') + "*";
		
		sAbo = new Ext.form.TextField({
			fieldLabel: sLabel,
			labelSeparator: '',
			id: 'abo',
			width: 333,
			labelStyle: 'width:210px;',
			ctCls: 'cb pad0',
			allowBlank: mand,
			regex: RegExp('^[0-9]{0,1000}$'),
			regexText: getLanguageText('contactform.form.abo.error.justnumbers','Bitte gib nur Zahlen ein'), 
			msgTarget: 'under',
			blankText: getLanguageText('contactform.form.abo.error'),
			maxLength: aForm['abo']['maxlength'] 
		});
	}
	else
		sAbo = new Ext.form.Hidden({			
			id: 'abo'
	    });		
	
	// Nachricht 
	
	var sMessage = "";
	
	if(aForm['message']['showinclient'] == 1) {
		
		if(aForm['message']['ismandatoryfield'] == 1)
			mand = false;
		else
			mand = true;
		
		if(mand)
			sLabel = getLanguageText('contactform.form.message.label');
		else
			sLabel = getLanguageText('contactform.form.message.label') + "*";
		
		sMessage = new Ext.form.TextArea({
			xtype: 'textarea',
			fieldLabel: sLabel,
			labelSeparator: '',
			id: 'message',
			width: 608,
			height: 100,
			labelStyle: 'width:210px;',
			ctCls: 'cb pad0',
			allowBlank: mand,
			msgTarget: 'under',
			blankText: getLanguageText('contactform.form.message.error'),
			maxLength: aForm['message']['maxlength'] 
		});
	}
	else
		sMessage = new Ext.form.Hidden({			
			id: 'message'
	    });
	
	
	// Captcha
	var sCaptcha = "";
	
	if(!(aVals['pkglobalusers'] > 0)) {

		sCaptcha = new Ext.form.TextField({
 			style: 'margin-top:5px;',
 			fieldLabel: getLanguageText('contactform.form.insertcode'),
 			labelSeparator: '',
 			id: 'verificoderepeat',
 			width: 333,
 			labelStyle: 'width:210px;',
			ctCls: 'cb pad0',
 			allowBlank: false,
 			msgTarget: 'under',
 			blankText: getLanguageText('contactform.form.captcha.error')
					
	    });
		
		bCaptcha = "";
	}
	else {
		sCaptcha = new Ext.form.Hidden({			
			id: 'verificoderepeat',
			value: 'nocaptchaneeded'
	    });
		
		bCaptcha = "display:none;";
	}
	
  	contactForm = new Ext.FormPanel({
 		renderTo: 'contact_div',
 		id: 'frm_contact',
		frame:true,
 		autoWidth: true,
 		labelWidth: 198,
		url: '/index.cfm?event=contactform.send',
 		items: [
		{
			width: 608,
			layout: 'form',
			border: false,
			items: [{
				html: '<h2>' + getLanguageText('contactform.form.header') + '</h2>',
				style: 'color:#999999;'				
			}]
		},
		
		{
			width: 608,
			layout: 'form',
			border: false,
			items: [{
				html: '<strong>' + getLanguageText('contactform.form.subheader') + '</strong>',
				style: 'color:#333333;font-family:Arial,Helvetica,sans-serif;font-size:12px;line-height:17px;padding-bottom:16px;'				
			}]
		},
		sEmail,		
		sNames,
		sCompany,
		sPhone,
		subject_combo,
		sAbo,
		sMessage,				
		{
			width: 608,
			layout: 'form',
			border: false,
			items: [{
				html: getLanguageText('contactform.form.copyhint'),
				style: 'color:#333333;font-family:Arial,Helvetica,sans-serif;font-size:12px;line-height:17px;margin-bottom:20px;'
				
			}]
		},		
		{
 			xtype: 'hidden',
 			id: 'captchacheckstring',
 			value: ''
 		}		
		,{
			width: 306,
			layout: 'form',
			border: false,
			items: [{
				html: '<span style="' + bCaptcha + '"><span class="fll">' + getLanguageText('contactform.form.securitycode') + '</span> <span class="flr"><a href="javascript:void(0);" onclick="Ext.getCmp(\'captchaimg\').fireEvent(\'render\',Ext.getCmp(\'captchaimg\'))">' + getLanguageText('contactform.form.getsecuritycode') + '</a></span></span>',
				style: 'font-size: 12px;margin-right: 5px;'
			}]
		},{
			xtype: 'panel',
			id: 'captchaimg',				
        	listeners: {
        		render: function(pThis){
					if(!(aVals['pkglobalusers'] > 0)) {
						
	        			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="302" height="50" border="0" alt="Captcha" style="margin-bottom:14px;" />'
	        					);
	        					cTpl.overwrite(pThis.body,cData);							
	        					Ext.getCmp('captchacheckstring').setValue(cData.captchacheckstring);
	        				}					
	        			});	
					}
        		}
        	}
		},sCaptcha
		,{
			
			layout: 'form',
			border: false,
			items: [{
				html: '<div class="line_dotted mt16"><!--leer--></div>'
            		+ '<span class="fll small mt10">' + getLanguageText('contactform.form.mandatoryfield')+ '</span><div align="right" class="pt8 flr ma-buttonbox clearfix">'
					+ '	<a class="ma-button v1 wrp form-a" href="#" onclick="contactForm.doAbort(); return false;"><span class="size">'+getLanguageText('defaults.cancel')+'</span></a>  '
					+ '	<a  class="ma-button v1 form-b" href="#" onclick="contactForm.doSubmit(); return false;"><span class="size">'+getLanguageText('defaults.send')+'</span></a> '
					+ '</div>',
				style: 'font-size: 12px;margin-right: 5px;'				
			}]						
		}
		
				
		],
		 
		key: {
			key: [10, 13],
			fn: function(){
				contactForm.doSubmit();
			}
		},
		// Submit:
		doSubmit: function(){
			if (contactForm.validate()) {
				contactForm.getForm().submit({
						success: function (pForm, pAction){
							if(pAction){
								Ext.MessageBox.alert(getLanguageText('defaults.hint.goodtoknow'), getLanguageText('contactform.send.successfulsent'),function(){contactForm.doAbort();});
							}else{
								Ext.MessageBox.alert(getLanguageText('defaults.error'),getLanguageText('contactform.form.errors.errorwhilesaving','Fehler beim Speichern!'));
							} 
						},
						failure: function(pForm, pAction){
							if(pAction.result){
								if(pAction.result.successnumber == 1){
									Ext.MessageBox.alert(getLanguageText('defaults.error') ,getLanguageText('contactform.send.captcha.error'));
								}
								
								if(pAction.result.successnumber == 2){
									Ext.MessageBox.alert(getLanguageText('defaults.error'),getLanguageText('contactform.form.errors.errorwhilesaving','Fehler beim Speichern!') +'<br>'+getLanguageText('contactform.form.errors.errorwhilesending','Fehler beim versenden des Formulars!<br>'+unescape("Bitte versuchen Sie es sp%E4ter noch einmal%21")));
								}
							}else{
								Ext.MessageBox.alert(getLanguageText('defaults.error'),getLanguageText('contactform.form.errors.errorwhilesaving','Fehler beim Speichern!') +'<br>'+ getLanguageText('contactform.form.error.mandatoryfields',unescape("F%FCllen Sie bitte alle Pflichtfelder aus%21")));
							}
						}
					}
				);
			}
		},
		// Abort:
		doAbort: function(){
			window.location.href = '/index.cfm';
		},
		
		
		// Validierung:
		validate: function(){
			return true;
		},
		showError: function(fieldName, Msg){
			Ext.MessageBox.alert('Fehler', Msg, function(){
				contactForm.getForm().findField(fieldName).focus();
			});
		}		
		
 	})
 }
