

	function makeJsRequestForm(){
		requestForm = new RequestForm();
		requestForm.init();
	}


	function RequestForm() {
		var self = this;
		var form = null;
		var submitButton = null;
		var requestUrl = '';
		var formType = null;
		var otherForm = false;
		var validator = null;
		var otherEventTypeDiv = null;
		var otherEventTypeDivVisible = false;
		
		
		//callbackVars
		var showMore = null;
		var moreDiv = null;
		var moreIsVisible = false;
		
		//emailVars
		var emailConfirmDiv = null;
		var callbackDiv = null;
		var emailOption = null;
		var callbackOption = null;
		var emailConfirmVisible = false;
		var callbackVisible = false;
		
		
		/*
		*	true for debug output to console
		*/
		var showDebug = false;
		
		this.init = function(){
			this.form = $('#popup').find('form');
			if(this.form.length == 0){
				this.form = $('#emailUsForm');
				this.otherForm = true;
			}
			this.setFormType();

			this.debug('formType: '+this.getFormType());
			if(!this.getFormType())		//SETZ DEN FORMTYPE RICHTIG!
				return false;
			
			
			this.requestUrl = this.form.attr('action');
			this.submitButton = this.form.find('input.popup-submit-send-enquiry');
			this.otherEventTypeDiv = this.form.find('#otherEventTypeDiv');
			this.otherEventTypeDiv.hide();
			this.addSubmitEvent();
			this.addEventTypeChangeEvent();
			
			this.addFormTypeSpecificStuff();
			this.addFormValidation();
			this.debug("requestUrl: "+this.requestUrl);
			this.debug('init done!!');
		}

		this.addFormTypeSpecificStuff = function(){
			
			if(this.formType == 'callback'){
				this.moreDiv = this.form.find('.more');
				this.showMore = this.form.find('.popup-tell-us-more');
				this.addShowMoreEvent();
				this.moreDiv.hide();
				this.debug('request-a-callback stuff added!');
			}
			if(this.formType == 'email'){
				this.emailConfirmDiv = this.form.find('.emailConfirm');
				this.callbackDiv = this.form.find('.phoneCallback');
				$('#popup').css('min-height','1450px');
				
				this.emailConfirmDiv.hide();
				this.callbackDiv.hide();
				this.emailOption = this.form.find('#radio-email');
				this.callbackOption = this.form.find('#radio-callback');
				this.addOptionEvents();
				this.debug('email-us stuff added!');

			}
		
		}
				
		
		this.addSubmitEvent = function(){
			this.submitButton.addClass('submitButton');
			this.submitButton.click(function(){
				self.sendData();
				return false;
			});
		}
		
		this.sendData = function(){
			var dataToSend = this.gatherDataToSend();
		
			
			if(!this.form.valid())
			{
				//GoogleAnalytics
				var errors = self.validator.invalid;
				for(key in errors)
				{
					var formtype = this.getFormType();
					var outputFormType = formtype.substr(0,1).toUpperCase() + formtype.substr(1,formtype.length)+'form';
					
					pageTracker._trackEvent('Error, '+outputFormType+' error', errors[key]);
				}
				
				//don't submit on having errors
				return;
			}

		
			if(!this.otherForm){
				$('#popup .popup-content').load(this.requestUrl+ " #content", dataToSend, function(){
					findSelectsAndInitJsDropDown();
	    			makeJsInput();
					self.init();
				});
			}
			else
			{
				this.form.load(this.requestUrl+ " #content", dataToSend, function(){
					findSelectsAndInitJsDropDown();
	    			makeJsInput();
					self.init();
				});
			}
			
		}
		
		this.addEventTypeChangeEvent = function (){
			this.form.find('#eventType').change(function(){
				if($(this).val() == 7){   //7 == other
					if(!self.otherEventTypeDivVisible)
						self.showOtherEventTypeInput();
				}
				else{
					if(self.otherEventTypeDivVisible)
						self.hideOtherEventTypeInput();
				}
			});
			
		}
		
		this.showOtherEventTypeInput = function(){
			this.otherEventTypeDiv.slideDown();
			this.otherEventTypeDivVisible = true;
			if(this.formType == 'email'){
				this.addOtherEventTypeRule();
			}	
		}
		this.hideOtherEventTypeInput = function(){
			this.otherEventTypeDiv.slideUp();
			this.otherEventTypeDivVisible = false;
			if(this.formType == 'email'){
				this.removeOtherEventTypeRule();
			}

		}
		
		
		/*
		*	finds the input fields excluding non edited or even empty fields
		*/
		this.gatherDataToSend = function(){
			var a = [];
			
			//find inputs to send
			this.form.find('input').each(function(index, element){
				var field = $(element);
				if(!field.hasClass('submitButton') && field.hasClass('edited'))
					a.push(field.attr('name')+"="+field.val());
			});
			
			//find selects to send
			this.form.find('select').each(function(index, element){
				var field = $(element);
				if(field.val()!='')
					a.push(field.attr('name')+"="+field.val());
			});
			
			//find textareas to send
			this.form.find('textarea').each(function(index, element){
				var field = $(element);
				if(field.val()!='')
					a.push(field.serialize());
			});
			
			//build the post parameter string
			var dataToSend = a.join("&");
			return dataToSend;
		}
		
		this.debug = function(msg){
			if(showDebug)
				console.log(msg);
		}
		
		
		
		//REQUEST-A-CALLBACK Stuff
		
		this.toggleShowMore = function(){
			if(this.moreIsVisible){
				this.moreDiv.slideUp();
				this.moreIsVisible = false;
				$('.popup-tell-us-more').html('Tell us more about your event');
			}
			else{
				this.moreDiv.slideDown();
				this.moreIsVisible = true;
				$('.popup-tell-us-more').html('Return to short form');
			}
		}
		
		this.addShowMoreEvent = function(){
			this.showMore.click(function(){
				self.toggleShowMore();
			});
		}
		
		//EMAIL-US Stuff
		
		this.addOptionEvents = function(){
			
			
			this.emailOption.click(function(){
				if(!$(this).hasClass('option-selected'))
					self.showEmailDiv();
			});
			
			this.callbackOption.click(function(){
				if(!$(this).hasClass('option-selected'))
					self.showCallbackDiv();
			})
		
		}
		
		this.setEmailSelected = function(){
			this.form.find('#optionValue').val('email');
			this.emailOption.addClass('option-selected');
			$('#radioSelect').val('1');
			this.form.validate().element("#radioSelect");
			this.callbackOption.removeClass('option-selected');
			this.debug('email selected');
		}
		
		this.setCallbackSelected = function(){
			this.form.find('#optionValue').val('callback');
			this.callbackOption.addClass('option-selected');
			$('#radioSelect').val('1');
			this.form.validate().element("#radioSelect");
			this.emailOption.removeClass('option-selected');
			this.debug('callback selected');		
		}
		
		this.showEmailDiv = function(){
			this.setEmailSelected();
			if(this.callbackVisible)
				this.callbackDiv.slideUp();
				
			this.emailConfirmDiv.slideDown();
			this.emailConfirmVisible = true;
			this.callbackVisible = false;
			this.addEmailConfirmRule();
			this.removeCallbackRule();
		}
	
		this.showCallbackDiv = function(){
			this.setCallbackSelected();
			if(this.emailConfirmVisible)
				this.emailConfirmDiv.slideUp();
				
			this.callbackDiv.slideDown();
			this.callbackVisible = true;
			this.emailConfirmVisible = false;
			this.removeEmailConfirmRule();
			this.addCallbackRule();	
		}
		
		
		
		//MISC
		
		this.addFormValidation = function(){
		
			//Adds additional method to check if user has changed the default value
			jQuery.validator.addMethod('mydefault', function(value, element){
				var toCheck = $(element);
				return this.optional(element) || true == toCheck.hasClass('edited');
			});
			
			jQuery.validator.addMethod('oneOfTwo', function(value, element){
				var toCheck = $(element);
				return this.optional(element) || value != 0;
			});
			
			jQuery.validator.addMethod('tele', function(value,element){
				var arr = value.split(" ");
				var toCheck = arr.join("");
				return this.optional(element) || /^\d+$/.test(toCheck);
			});
		
			this.validator = this.form.validate({
				errorLabelContainer: "#errorLabelContainer",
   				wrapper: "em",
   				submitHandler: function() { alert("Submitted!")},
				highlight: function(element, errorClass, validClass) {
				     $(element).addClass(errorClass).removeClass(validClass);
				     $(element.form).find("label[for=" + element.id + "]")
				                    .addClass(errorClass);
				     try{
				     	if($(element).hasClass('oneOfTwo'))
				     		$('#contactMeByLabel').css('color','red');
				     
				     	if(!$(element).hasClass('select'))
				     		return;
				     	for(var select in selects){
				     		var muh = selects[select];
				     		if(muh.getName() == element.id+"JS"){
				     			muh.makeMeError();
				     			break;
				     		}
				     	}
				     }catch(e){}
				                    
				},
				unhighlight: function(element, errorClass, validClass) {
				     $(element).removeClass(errorClass).addClass(validClass);
				     $(element.form).find("label[for=" + element.id + "]")
				                    .removeClass(errorClass);
				     try{
				     
				     	if($(element).hasClass('oneOfTwo'))
				     		$('#contactMeByLabel').css('color','#6B2646');
				     
				     	if(!$(element).hasClass('select'))
				     		return;
				     	for(var select in selects){
				     		var muh = selects[select];
				     		if(muh.getName() == element.id+"JS"){
				     			muh.makeMeNoError();
				     			break;
				     		}
				     	}
				     }catch(e){}
				},
				messages:{
					firstname:"Please enter your first name",
					surname: "Please enter your surname",
					company: "Please enter your company",
					contactTelephoneNo: "Please enter a contact telephone number",
					eventType: "Please select an event type",
					locationRequired: "Please fill in location required",
					phonenumber: "Please provide your phone number",
					callback: "Please choose a callback time",
					email: "Please enter your email address",
					eventDate: "Please supply an event date",
					source: "Please select a source",
					radioSelect: "Please select email or telephone"
				
				}
			});
			
		/*
			this.validator.showErrors = function(errors){
				if(errors) {
					$.extend(this.errorMap,errors);
					this.errorList=[];
					for(var name in errors) {
						this.errorList.push({
																	message:errors[name],
																	element:this.findByName(name)[0]
															});
					}
					this.successList=$.grep(this.successList,function(element){
																											return!(element.name in errors);
																											});
				}
				this.settings.showErrors?this.settings.showErrors.call(this,this.errorMap,this.errorList):this.defaultShowErrors();
				
				console.log('errors', this.errorList.lenght);
			}
			*/	
			
		}
		
		this.addOtherEventTypeRule = function(){
			this.debug("add OtherEventTypeRule");
			this.form.find('#otherEventtype').rules('add',{
				required: true,
				mydefault: true,
				messages:{
					required: "Please enter Other Event Type",
					mydefault: "Please enter Other Event Type"
				}
			});
		}
		
		this.removeOtherEventTypeRule = function(){
			this.debug("remove OtherEventTypeRule");
			this.form.find('#otherEventtype').rules('remove');
		}
		
		this.addEmailConfirmRule = function(){
			this.form.find('#confirmemail').rules('add',{
				required : true,
				mydefault: true,
				email: true,
				equalTo: '#email',
				messages: {
					required: "Please confirm your email",
					mydefault: "Please confirm your email",
					euqalTo: "Please confirm your email",
					email: "Please confirm your email"
				}
			});
		}
		
		this.removeEmailConfirmRule = function(){
			this.form.find('#confirmemail').rules('remove');
		}
		
		
		
		
		this.addCallbackRule = function(){
			
			this.form.find('#confirmPhonenumber').rules('add',{
				required : true,
				mydefault: true,
				tele: true,
				equalTo: '#contactTelephoneNo',
				messages: {
					required: "Please confirm your contact phone number",
					mydefault: "Please confirm your contact phone number",
					equalTo: "Please confirm your contact phone number",
					tele: "Please confirm your contact phone number"
				}
			});
			
			this.form.find('#callback').rules('add',{
				required : true
			});

		}
		
		this.removeCallbackRule = function(){
			
			this.form.find('#confirmPhonenumber').rules('remove');
			this.form.find('#callback').rules('remove');

		}
		
		this.setFormType = function(){
			var formName = this.form.attr('name');
			this.debug('formName: ' + formName);
			if(formName == 'request-a-callback')
				this.formType = 'callback';
			if (formName == 'email-enquiry')
				this.formType = 'email';
			
		}
		
		this.getFormType = function(){
			if(!this.formType)
				return false;
			else
				return this.formType;
		}
	}
