<?php
defined('_JEXEC') or die('Direct Access to this location is not allowed.');
JHTML::_('behavior.modal');
?>
<script
	type="text/javascript"
	src="<?php echo JURI::root() . 'administrator/components/com_facileforms/libraries/jquery/' ;?>jquery.js"></script>
<script
	type="text/javascript"
	src="<?php echo JURI::root() . 'administrator/components/com_facileforms/libraries/jquery/' ;?>jquery-ui.min.js"></script>
<script
	type="text/javascript"
	src="<?php echo JURI::root() . 'administrator/components/com_facileforms/libraries/jquery/plugins/' ;?>base64.js"></script>
<script
	type="text/javascript"
	src="<?php echo JURI::root() . 'administrator/components/com_facileforms/libraries/jquery/plugins/' ;?>json.js"></script>
<script
	type="text/javascript"
	src="<?php echo JURI::root() . 'administrator/components/com_facileforms/libraries/jquery/plugins/' ;?>md5.js"></script>
<script type="text/javascript">
var $jq = jQuery.noConflict();
var app = null;
	
function BF_EasyModeApp()
{
		// to app scope
		var appScope = this;
		// the opacity for each draggable element
		this.opacity = 0.5;
		// the areaList contains all elements into all available areas. Structure: [ { area : droppableArea_instance, elements : [ Object_instance , ... ] } , ... ]
		this.areaList = [];
		// temp area list for the trashcan
		this.trashcanAreaList = [];
		// the actually selected element to change its options for
		this.optionElement = null;
		// all available element scripts
		this.elementScripts = <?php echo Zend_Json::encode($elementScripts)?>
		
		this.captchaAdded = 0;
		
		this.pixelRaster = 1;
		
		this.setElementResizable = function(element){
			$jq('#'+element.id).resizable({
				grid: [appScope.pixelRaster,appScope.pixelRaster],
				//knobHandles : true,
				handles: "se",
				//minWidth: 5, 
				//minHeight: 5, 
				//ghost: true,
				//transparent: true
				start: function(){
					if($jq('#'+element.id).parent().attr('class') == 'ui-wrapper'){
						$jq('#'+element.id).parent().css('padding-bottom', '0px');
						if($jq('#'+element.id).parent().children('.ui-resizable-se')[0]){
						  $jq($jq('#'+element.id).parent().children('.ui-resizable-se')[0]).click(appScope.resizableElement);
						}
					}
					$jq(this).css('border', '1px dotted #000');
				},
				stop: function(){
					//alert($jq(this).attr('class'));
					$jq(this).css('border', 'none');
				}
			});
			
			if($jq('#'+element.id).parent().attr('class') == 'ui-wrapper'){
				$jq('#'+element.id).parent().css('padding-bottom', '0px');
				if($jq('#'+element.id).parent().children('.ui-resizable-se')[0]){
					$jq($jq('#'+element.id).parent().children('.ui-resizable-se')[0]).click(appScope.resizableElement);
				}
			}
		};
		
		this.refreshTemplateBox = function(){
			$jq('#bfTemplateBox').val( $jq.trim($jq('#bfTemplate').get(0).innerHTML) );
		};
		
		// first turn all pages on sucht that they can be initially processed by jquery
		$jq('.bfPage').css('display','');
		
		<?php if(isset($callbackParams['areas']) && trim($callbackParams['areas']) != ''){ ?>

		var tmpAreas = <?php echo $callbackParams['areas'] ?>;

		for(var i = 0; i < tmpAreas.length;i++){
			if($jq("#"+tmpAreas[i].id).get(0)){
				if(!tmpAreas[i].pixelRaster){ // compat 686
					tmpAreas[i]['pixelRaster'] = 1;
				}
				if(i == 0){
					appScope.pixelRaster = tmpAreas[i].pixelRaster;
					$jq('#bfPixelRaster').val(appScope.pixelRaster);
				}
				this.areaList.push( { area: $jq("#"+tmpAreas[i].id).get(0), elements : tmpAreas[i].elements, pixelRaster: tmpAreas[i].pixelRaster } );
			}
		}
		
		for(var i = 0; i < appScope.areaList.length; i++){
				for(var j = 0; j < appScope.areaList[i].elements.length;j++){
				
					var element = appScope.areaList[i].elements[j];
					
					$jq('#'+element.id).get(0).removeAttribute('onclick');
					$jq('#'+element.id).get(0).removeAttribute('onblur');
					$jq('#'+element.id).get(0).removeAttribute('onchange');
					$jq('#'+element.id).get(0).removeAttribute('onfocus');
					$jq('#'+element.id).get(0).removeAttribute('onselect');
					$jq('#'+element.id).get(0).removeAttribute('onmouseover');
					$jq('#'+element.id).get(0).removeAttribute('onmouseout');
					$jq('#'+element.id).get(0).removeAttribute('tabindex');
					$jq('#'+element.id).get(0).removeAttribute('checked');
					
					if(typeof element.tabIndex == 'undefined'){ // compat 687
						element.tabIndex = -1;
					}
					
					$jq('#'+element.id).css('border','none');
				
					if(element.internalType == 'bfCaptcha'){
						appScope.captchaAdded++;
					}
				
					var prevm = $jq('#'+element.id).css('margin');
					var prevp = $jq('#'+element.id).css('padding');
					var prevElementWidth = $jq('#'+element.id).css('width');
					var prevElementHeight = $jq('#'+element.id).css('height');
					if(element.internalType != 'bfHidden'){
						appScope.setElementResizable(element);
					}
					$jq('#'+element.id).css('margin', prevm);
					$jq('#'+element.id).css('padding', prevp);
					$jq('#'+element.id).css('width', prevElementWidth);
					$jq('#'+element.id).css('height', prevElementHeight);
					
					if(element.internalType == 'bfIcon'){
						$jq( $jq('#'+element.id).children('img')[0] ).get(0).removeAttribute('onmouseover');
						$jq( $jq('#'+element.id).children('img')[0] ).get(0).removeAttribute('onmouseout');
					}
					
					var labelsResult = $jq('#'+element.id).parent().parent().children('.ff_label');
					if(labelsResult.length != 0){
						for(var k = 0; k < labelsResult.length;k++){
							prevm = $jq('#'+labelsResult[k].id).css('margin');
							prevp = $jq('#'+labelsResult[k].id).css('padding');
							prevElementWidth = $jq('#'+labelsResult[k].id).css('width');
							prevElementHeight = $jq('#'+labelsResult[k].id).css('height');
							appScope.setElementResizable(labelsResult[k]);
							$jq('#'+labelsResult[k].id).css('margin', prevm);
							$jq('#'+labelsResult[k].id).css('padding', prevp);
							$jq('#'+labelsResult[k].id).css('width', prevElementWidth);
							$jq('#'+labelsResult[k].id).css('height', prevElementHeight);
					
							var batchLabel = document.createElement('option');
							$jq(batchLabel).val(labelsResult[k].id);
							$jq(batchLabel).text(stripHTML( $jq('#'+labelsResult[k].id).html() ) );
							$jq('#bfBatchLabels').append(batchLabel);
						}
					}
					labelsResult = $jq('#'+element.id).parent().children('.ff_label');
					if(labelsResult.length != 0){
						for(var k = 0; k < labelsResult.length;k++){
							prevm = $jq('#'+labelsResult[k].id).css('margin');
							prevp = $jq('#'+labelsResult[k].id).css('padding');
							prevElementWidth = $jq('#'+labelsResult[k].id).css('width');
							prevElementHeight = $jq('#'+labelsResult[k].id).css('height');
							appScope.setElementResizable(labelsResult[k]);
							$jq('#'+labelsResult[k].id).css('margin', prevm);
							$jq('#'+labelsResult[k].id).css('padding', prevp);
							$jq('#'+labelsResult[k].id).css('width', prevElementWidth);
							$jq('#'+labelsResult[k].id).css('height', prevElementHeight);
							
							var batchLabel = document.createElement('option');
							$jq(batchLabel).val(labelsResult[k].id);
							$jq(batchLabel).text(stripHTML( $jq('#'+labelsResult[k].id).html() ) );
							$jq('#bfBatchLabels').append(batchLabel);
						}
					}
				
					if(element.internalType != 'bfHidden'){
						var batchElement = document.createElement('option');
						$jq(batchElement).val(element.id);
						$jq(batchElement).text(element.name);
						$jq('#bfBatchElements').append(batchElement);
					}
				}
		}
		<?php } ?>
		
		appScope.refreshTemplateBox();
		if(parseInt(document.adminForm.page.value) < 1){
			document.adminForm.page.value = 1;
		}
		$jq('#bfCurrentPage').get(0).innerHTML = document.adminForm.page.value + "/" + (parseInt(document.adminForm.pages.value) == 0 ? 1 : document.adminForm.pages.value);
		$jq('.bfPage').css('display','none');
		$jq('#bfPage'+document.adminForm.page.value).css('display','');
		
		for(var i = 1; i <= parseInt(document.adminForm.pages.value);i++){
		
			var option = document.createElement('option');
			$jq(option).val(i);
			$jq(option).text(i);
			
			var option2 = document.createElement('option');
			$jq(option2).val(i);
			$jq(option2).text(i);
			
			$jq('#bfGoToPage').append(option);
			$jq('#bfMoveThisPageTo').append(option2);
		}
		
		this.populateHiddenFieldsOptions = function(){
			
			$jq('#bfHiddenFieldsOptions').empty();
			
			var element = null;
			var br      = null;
			var hiddenFields = appScope.getElementsByType('bfHidden');
			
			for(var i=0;i < hiddenFields.length;i++){
			
				// element name header
				element = document.createElement('span');
				element.innerHTML = '<?php echo BFText::_('Name') ?>:';
				$jq('#bfHiddenFieldsOptions').append(element);
				br = document.createElement('br');
				$jq('#bfHiddenFieldsOptions').append(br);
				
				// element name
				element = document.createElement('input'); 
				element.setAttribute('type', 'text');
				element.setAttribute('id', 'name__HIDDEN__' + hiddenFields[i].id);
				element.setAttribute('class', 'bfHiddenOptionsName');
				element.setAttribute('value', hiddenFields[i].name);
				element.style.width = '100%';
				br = document.createElement('br');
				$jq('#bfHiddenFieldsOptions').append(element);
				$jq('#bfHiddenFieldsOptions').append(br);
				
				// element title header
				element = document.createElement('span');
				element.innerHTML = 'Title:';
				$jq('#bfHiddenFieldsOptions').append(element);
				br = document.createElement('br');
				$jq('#bfHiddenFieldsOptions').append(br);
				
				// element title
				element = document.createElement('input'); 
				element.setAttribute('type', 'text');
				element.setAttribute('id', 'title__HIDDEN__' + hiddenFields[i].id);
				element.setAttribute('class', 'bfHiddenOptionsTitle');
				element.setAttribute('value', hiddenFields[i].title);
				element.style.width = '100%';
				br = document.createElement('br');
				$jq('#bfHiddenFieldsOptions').append(element);
				$jq('#bfHiddenFieldsOptions').append(br);
				
				// element value header
				element = document.createElement('span');
				element.innerHTML = 'Value:';
				$jq('#bfHiddenFieldsOptions').append(element);
				br = document.createElement('br');
				$jq('#bfHiddenFieldsOptions').append(br);
				
				// element value
				element = document.createElement('input'); 
				element.setAttribute('type', 'text');
				element.setAttribute('id', 'value__HIDDEN__' + hiddenFields[i].id);
				element.setAttribute('class', 'bfHiddenOptionsValue');
				element.setAttribute('value', hiddenFields[i].options.value);
				element.style.width = '100%';
				br = document.createElement('br');
				$jq('#bfHiddenFieldsOptions').append(element);
				$jq('#bfHiddenFieldsOptions').append(br);
				
				element = document.createElement('span');
				element.innerHTML = 'Remove? ';
				$jq('#bfHiddenFieldsOptions').append(element);
				element = document.createElement('input'); 
				element.setAttribute('type', 'checkbox');
				element.setAttribute('id', 'remove__HIDDEN__' + hiddenFields[i].id);
				element.setAttribute('class', 'bfHiddenOptionsDelete');
				//element.style.width = '100%';
				$jq('#bfHiddenFieldsOptions').append(element);
				
				br = document.createElement('hr');
				$jq('#bfHiddenFieldsOptions').append(br);
			}
			
			if(hiddenFields.length != 0){
			
				// update button
				br = document.createElement('br');
				$jq('#bfHiddenFieldsOptions').append(br);
				
				element = document.createElement('input'); 
				element.setAttribute('type', 'submit');
				element.setAttribute('value', 'update');
				element.style.width = '100%';
				$jq(element).click(
					function(){
						
						var children = $jq('#bfHiddenFieldsOptions').children('.bfHiddenOptionsValue');
						for( var i = 0; i < children.length; i++ ){
							var id = children[i].id.split('__HIDDEN__');
							if(id.length == 2){
								var element = appScope.getElementById(id[1]);
								element.options.value = $jq('#value__HIDDEN__'+id[1]).val();
								$jq('#'+element.id).get(0).setAttribute('value', element.options.value);
							}
						}
						
						var children = $jq('#bfHiddenFieldsOptions').children('.bfHiddenOptionsTitle');
						for( var i = 0; i < children.length; i++ ){
							var id = children[i].id.split('__HIDDEN__');
							if(id.length == 2){
								var element = appScope.getElementById(id[1]);
								element.title = $jq('#title__HIDDEN__'+id[1]).val();
							}
						}
						
						children = $jq('#bfHiddenFieldsOptions').children('.bfHiddenOptionsName');
						for( var i = 0; i < children.length; i++ ){
						var id = children[i].id.split('__HIDDEN__');
							if(id.length == 2){
								var element = appScope.getElementById(id[1]);
								element.name = $jq('#name__HIDDEN__'+id[1]).val();
							}
						}
						
						var children = $jq('#bfHiddenFieldsOptions').children('.bfHiddenOptionsDelete');
						for( var i = 0; i < children.length; i++ ){
							if(children[i].checked){
								var id = children[i].id.split('__HIDDEN__');
								if(id.length == 2){
									var element = appScope.getElementById(id[1]);
									$jq('#'+id[1]).remove();
									appScope.removeElementFromAreaList(element);
								}
							}
						}
						
						appScope.populateHiddenFieldsOptions();
						
						$jq('#bfHiddenFieldsOptions').get(0).innerHTML = '<?php echo BFText::_('Options saved! To finally save your form, click "save" on the top right button!') ?><br/><br/>' + $jq('#bfHiddenFieldsOptions').get(0).innerHTML;
					}
				);
				
				$jq('#bfHiddenFieldsOptions').append(element);
			}
		}
		
		/**
		* Return the elements of a droppableArea
		*/
		this.getElementsArray = function (area){
			for(var i = 0; i < appScope.areaList.length; i++){
				if(appScope.areaList[i].area && appScope.areaList[i].area == area){
					return appScope.areaList[i].elements;
				}
			}

			return null;
		};

		/**
		* Checks if the droppableArea exists
		*/
		this.hasArea = function (area){
			for(var i = 0; i < appScope.areaList.length; i++){
				if(appScope.areaList[i].area && appScope.areaList[i].area == area){
					return true;
				}
			}

			return false;
		};
		
		this.getElementById = function(id) {
			for(var i = 0; i < appScope.areaList.length; i++){
				for(var j = 0; j < appScope.areaList[i].elements.length; j++){
					if(id == appScope.areaList[i].elements[j].id){
						return appScope.areaList[i].elements[j];
					}
				}
			}

			return null;
		};

		this.getElementsByType = function(type) {
			var foundElements = new Array();
			for(var i = 0; i < appScope.areaList.length; i++){
				for(var j = 0; j < appScope.areaList[i].elements.length; j++){
					if(type == appScope.areaList[i].elements[j].internalType){
						foundElements.push(appScope.areaList[i].elements[j]);
					}
				}
			}

			return foundElements;
		};
	
		this.removeElementFromAreaList = function(element){
			for(var i = 0; i < appScope.areaList.length; i++){
				var newElements = new Array();
				for(var j = 0; j < appScope.areaList[i].elements.length; j++){
					if(element.id != appScope.areaList[i].elements[j].id){
						newElements.push(appScope.areaList[i].elements[j]);
					}
				}
				appScope.areaList[i].elements = newElements;
			}
		};

		this.removeElementFromTrashcanAreaList = function(element){
			var newElements = new Array();
			for(var i = 0; i < appScope.trashcanAreaList.length; i++){
				if(element.id != appScope.trashcanAreaList[i].id){
					newElements.push(appScope.trashcanAreaList[i]);
				}
			}
			appScope.trashcanAreaList = newElements;
		};

		/**
		* prepares the data for the form before the submit
		*/
		this.prepareForSave = function(){

			if(appScope.captchaAdded > 1){
				alert('<?php echo BFText::_('You added more than one captcha. Please remove the element in order to be able to save!') ?>');
				return;
			}

			$jq('ui-resizable-handle').remove();

			var areas = new Array();
			
			for(var i = 0; i < appScope.areaList.length; i++){
				areas.push( { id : appScope.areaList[i].area.id, elements: appScope.areaList[i].elements, pixelRaster: appScope.pixelRaster } );
				
				for(var j = 0; j < appScope.areaList[i].elements.length;j++){
				
					var element = appScope.areaList[i].elements[j];
					
					if( (element.internalType == 'bfTextfield' || element.internalType == 'bfSelect') && element.options && !element.options.mailback){ // compat 690
						element.options['mailback'] = false;
					}
					
					if(element.internalType == 'bfFile' && element.options && !element.options.allowedFileExtensions){ // compat 690
						element.options['allowedFileExtensions'] = '';
						element.data2 = '';
					} else {
						if(element.internalType == 'bfFile' && element.options){
							element.data2 = element.options.allowedFileExtensions.toLowerCase();
						}
					}
					
					if(element.internalType == 'bfFile' && element.options && !element.options.attachToAdminMail){ // compat 691
						element.options['attachToAdminMail'] = false;
					}
					
					if(element.internalType == 'bfFile' && element.options && !element.options.attachToUserMail){ // compat 691
						element.options['attachToUserMail'] = false;
					}
					
					if(!element.mailbackfile){
						element['mailbackfile'] = '';
					}
					
					if(!element.orderNumber){
						element['orderNumber'] = -1;
					}
					
					if(!element.tabIndex){ // compat 687
						element['tabIndex'] = -1;
					}
					
					if(!element.mailbackAccept){ // compat 686
						element['mailbackAccept'] = false;
					}
					
					if(!element.mailbackAcceptConnectWith){ // compat 686
						element['mailbackAcceptConnectWith'] = '';
					}
					
					if(!element.mailbackAsSender){ // compat 687
						element['mailbackAsSender'] = false;
					}
					
					if(element.internalType == 'bfFile' && element.options.attachToAdminMail){
						var attachToAdminMail = document.createElement('input');
						attachToAdminMail.setAttribute('type', 'hidden');
						attachToAdminMail.setAttribute('id', 'attachToAdminMail_'+element.name);
						attachToAdminMail.setAttribute('name', 'attachToAdminMail['+element.name+']');
						attachToAdminMail.setAttribute('class', 'attachToAdminMail');
						attachToAdminMail.setAttribute('value', 'true');
						$jq('#bfTemplate').append(attachToAdminMail);
					}
					
					if(element.internalType == 'bfFile' && element.options.attachToUserMail){
						var attachToUserMail = document.createElement('input');
						attachToUserMail.setAttribute('type', 'hidden');
						attachToUserMail.setAttribute('id', 'attachToUserMail_'+element.name);
						attachToUserMail.setAttribute('name', 'attachToUserMail['+element.name+']');
						attachToUserMail.setAttribute('class', 'attachToUserMail');
						attachToUserMail.setAttribute('value', 'true');
						$jq('#bfTemplate').append(attachToUserMail);
					}
					
					if(element.internalType == 'bfTextfield' && element.mailbackAsSender){
						var mailbackSender = document.createElement('input');
						mailbackSender.setAttribute('type', 'hidden');
						mailbackSender.setAttribute('id', 'mailbackSender_'+element.name);
						mailbackSender.setAttribute('name', 'mailbackSender['+element.name+']');
						mailbackSender.setAttribute('class', 'mailbackSender');
						mailbackSender.setAttribute('value', 'true');
						$jq('#bfTemplate').append(mailbackSender);
					}
					
					if(element.internalType == 'bfCheckbox'){
						if(element.options.checked){
							$jq('#'+element.id).get(0).setAttribute('checked','checked');
						}
						if(element.mailbackAccept){
							var connectWith = document.createElement('input');
							connectWith.setAttribute('type', 'hidden');
							connectWith.setAttribute('id', 'mailbackConnectWith_'+element.mailbackAcceptConnectWith);
							connectWith.setAttribute('name', 'mailbackConnectWith['+element.mailbackAcceptConnectWith+']');
							connectWith.setAttribute('class', 'mailbackConnectWith');
							connectWith.setAttribute('value', 'true_'+element.name);
							$jq('#bfTemplate').append(connectWith);
						}
					}
					
					if(element.internalType == 'bfRadio'){
						if(element.options.checked){
							$jq('#'+element.id).get(0).setAttribute('checked','checked');
						}
					}
					
					if(element.internalType == 'bfCalendar' || element.internalType == 'bfCaptcha' || element.internalType == 'bfHidden' || element.internalType == 'bfStaticText' || element.internalType == 'bfIcon'){
						// leave out, only necessary for input elements
					} else {
						// move input elements out of their ui-wrappers
						$jq('#'+element.id).parent().before($jq($jq('#'+element.id).parent().prev()).get(0)); // moving the label
						$jq('#'+element.id).parent().before($jq('#'+element.id).get(0)); // moving the element itself
					}

					if(element.script2flag1 == 1){
						var payment = "";
						if(element.internalType == 'bfPayPal'){
							payment = "document.getElementById('bfPaymentMethod').value='PayPal';";
						}
						else if(element.internalType == 'bfSofortueberweisung'){
							payment = "document.getElementById('bfPaymentMethod').value='Sofortueberweisung';";
						}
						$jq('#'+element.id).get(0).setAttribute('onclick', payment + element.functionNameScript2 + '(this,\'click\');');
					}
					if(element.script2flag2 == 1){
						$jq('#'+element.id).get(0).setAttribute('onblur', element.functionNameScript2 + '(this,\'onblur\')');
					}
					if(element.script2flag3 == 1){
						$jq('#'+element.id).get(0).setAttribute('onchange', element.functionNameScript2 + '(this,\'onchange\')');
					}
					if(element.script2flag4 == 1){
						$jq('#'+element.id).get(0).setAttribute('onfocus', element.functionNameScript2 + '(this,\'onfocus\')');
					}
					if(element.script2flag5 == 1){
						$jq('#'+element.id).get(0).setAttribute('onselect', element.functionNameScript2 + '(this,\'onselect\')');
					}
					
					if(element.internalType == 'bfTooltip'){
					 $jq('#'+element.id).get(0).setAttribute('onmouseover', "return overlib('"+expstring(element.options.text)+"',CAPTION,'"+element.name+"',BELOW,RIGHT)");
					 $jq('#'+element.id).get(0).setAttribute('onmouseout', "return nd()");
					} else
					if(element.internalType == 'bfIcon'){
						$jq($jq('#'+element.id).children('img')[0]).get(0).setAttribute('onmouseover', "this.src='"+element.data3+"'");
					 	$jq($jq('#'+element.id).children('img')[0]).get(0).setAttribute('onmouseout', "this.src='"+element.data1+"'");	
					 	$jq('#'+element.id).get(0).setAttribute('onmouseover',"this.style.cursor = 'pointer'");
					 	$jq('#'+element.id).get(0).setAttribute('onmouseout',"this.style.cursor = ''");
					} else
					if(element.internalType == 'bfCalendar'){
						$jq('#'+element.id).get(0).setAttribute('onclick',"showCalendar(ff_getElementByName('"+element.options.connectWith+"').id, '"+element.options.format+"');");
						
					}
					if($jq('#'+element.id).parents('.bfPage')[0]){
						var elPage = $jq('#'+element.id).parents('.bfPage')[0].id.split('bfPage');
						element.page = parseInt(elPage[1]); 
					} else {
						element.page = 1;
					}
					if(element.internalType != 'bfHidden' && parseInt(element.tabIndex) != -1){
						$jq('#'+element.id).get(0).setAttribute('tabindex', element.tabIndex);
					}
				}
			}
			
			$jq('.ff_div').css('border','');
			$jq('.ff_elem').css('border','');
			$jq('.ff_label').css('border','');
			$jq('.ff_div').css('border-left','');
			$jq('.ff_elem').css('border-left','');
			$jq('.ff_label').css('border-left','');
			$jq('.ff_div').children('.ui-wrapper').remove();
			$jq('.ff_div').children('.ui-resizable-handle').remove();
			$jq('.ff_div').children('.ff_label').children('.ui-resizable-handle').remove();
			$jq('.ff_div').children('.ff_elem').children('.ui-resizable-handle').remove();
			$jq('.ff_label').removeClass('ui-resizable');
			$jq('.ff_label').removeClass('ui-resizable-disabled');
			$jq('.ff_label').removeClass('ui-state-disabled');
			$jq('.ff_elem').removeClass('ui-resizable');
			$jq('.ff_elem').removeClass('ui-resizable-disabled');
			$jq('.ff_elem').removeClass('ui-state-disabled');
			$jq('.droppableArea').removeClass('ui-sortable');
			$jq('.droppableArea').removeClass('ui-droppable');
			$jq('#bfTemplate').children('.ui-wrapper').remove();
			
			$jq('.bfPage').css('display','none');
			$jq('#bfPage1').css('display','');
			
			var rep = $jq('#bfTemplate').get(0).innerHTML.replace(/border-width: initial; /g,"");
			rep = rep.replace(/border-color: initial; /g,"");
			
			var result =  
			{ 
				templateCode : $jq.base64Encode(rep),
				areas        : $jq.base64Encode($jq.toJSON(areas))
			};

			return result;
		};
		
		this.createElementBeside = function(element, type){
				
				var elements = appScope.getElementsArray($jq('#'+element.area).get(0));
				
				if(elements != null && type != ''){
				
							var ffListItem = null;
							
							if($jq('#' + element.id).parent().parent().parent().hasClass('ff_listItem')){
								ffListItem = $jq('#' + element.id).parent().parent().parent();
							} else if($jq('#' + element.id).parent().parent().hasClass('ff_listItem')){
								ffListItem = $jq('#' + element.id).parent().parent();
							} else {
								return;
							}
							
							$jq(ffListItem).children('.ff_appender').remove();
							
							var info = appScope.getElementById(element.id);
							var rndId = $jq.md5(Math.random() + info.appElementOrderId + info.appElementId + info.area);
							
							var besideElement = null;
							
							var wrapper = document.createElement('div');
							wrapper.setAttribute('id', 'ff_div' + rndId);
							wrapper.setAttribute('class', 'ff_div');
							
							if(type != 'bfStaticText'){
								
								var label = document.createElement('div');
								label.setAttribute('id', 'ff_elemLabel' + rndId);
								label.setAttribute('class', 'ff_label');
														
								label.style.verticalAlign = 'top';
								label.style.width = '50px';
								label.style.height = '10px';
								label.innerHTML = 'Label...';
								
								$jq(label).click(appScope.resizableElement);
														
								$jq(wrapper).append(label);
								
								var mybr = document.createElement('div');
								mybr.setAttribute('id', 'ff_break' + rndId);
								mybr.setAttribute('class', 'ff_break');
								$jq(mybr).css('display','none');
													
								$jq(wrapper).append(mybr);
							}
							
							besideElement = appScope.createElementByType(type, rndId);
							
							if(besideElement != null){
							
								$jq(wrapper).append(besideElement.element);
								ffListItem.append(wrapper);
								
								var appender = document.createElement('div');
								appender.setAttribute('class', 'ff_appender');
								$jq(appender).css('clear','both');
								$jq(ffListItem).append(appender);
								
								elements.push(
									{
										id                 : besideElement.element.id,
										dbId               : 0,
										rndId              : rndId,
										name               : rndId, // default name until changed by user
										title              : 'title_' + besideElement.element.id, // default title until changed by user
										type               : besideElement.element.type ? besideElement.element.type : '',
										internalType       : type,
										bfType             : besideElement.bfType,
										elementType        : besideElement.elementType,
										area               : element.area,
										appElementId       : type,
										appElementOrderId  : elements.length,
										wrapperId          : wrapper.id,
										labelId            : label ? label.id : '', 
										listItemId         : element.listItemId,
										data1              : besideElement.data1,
										data2              : besideElement.data2,
										data3              : besideElement.data3,
										script1cond        : besideElement.script1cond,
										script1id          : besideElement.script1id,
										script1code        : besideElement.script1code,
										script1flag1       : besideElement.script1flag1,
										script1flag2       : besideElement.script1flag2,
										script2cond        : besideElement.script2cond,
										script2id          : besideElement.script2id,
										script2code        : besideElement.script2code,
										script2flag1       : besideElement.script2flag1,
										script2flag2       : besideElement.script2flag2,
										script2flag3       : besideElement.script2flag3,
										script2flag4       : besideElement.script2flag4,
										script2flag5       : besideElement.script2flag5,
										script3cond        : besideElement.script3cond,
										script3id          : besideElement.script3id,
										script3code        : besideElement.script3code,
										script3msg         : besideElement.script3msg,
										functionNameScript1: besideElement.functionNameScript1,
										functionNameScript2: besideElement.functionNameScript2,
										functionNameScript3: besideElement.functionNameScript3,
										flag1              : besideElement.flag1,
										flag2              : besideElement.flag2,
										mailback           : besideElement.mailback,
										mailbackfile       : besideElement.mailbackfile,
										mailbackAsSender   : false,
										mailbackAccept     : false,
										mailbackAcceptConnectWith: '', 
										orderNumber        : -1,
										tabIndex           : -1,
										page               : parseInt(document.adminForm.page.value),
										options            : besideElement.options
									}
								);
							
								if(type == 'bfCaptcha'){
									appScope.captchaAdded++;
								}
							
								appScope.populateHiddenFieldsOptions();
								if(type != 'bfHidden'){
									appScope.setElementResizable(besideElement.element);
								}
								var labelsResult = $jq('#'+besideElement.element.id).parent().parent().children('.ff_label');
								if(labelsResult.length != 0){
									for(var k = 0; k < labelsResult.length;k++){
										appScope.setElementResizable(labelsResult[k]);
									}
								}
								labelsResult = $jq('#'+besideElement.element.id).parent().children('.ff_label');
								if(labelsResult.length != 0){
									for(var k = 0; k < labelsResult.length;k++){
										appScope.setElementResizable(labelsResult[k]);
									}
								}
								appScope.initMouseOvers();
							}
				}
			
		};
		
		this.createElementBesideByType = function(element, selector){
			this.createElementBeside( element, $jq(selector).val() );
		};
		
		this.disableElementsDetails = function(){
			$jq('.bfOptionsWrapper').css('display','none');
			$jq('.bfOptions').css('display','none');
			$jq('.bfOptions').css('visibility','hidden');
			$jq('#bfActions').css('display','none');
			$jq('#bfActions').css('visibility','hidden');
			$jq('#bfGlobalOptions').css('display','none');
			$jq('#bfGlobalOptions').css('visibility','hidden');
			$jq('#bfSaveOptionsButton').css('display','none');
			$jq('#bfSaveOptionsButton').css('visibility','hidden');
			$jq('#bfRemoveLabelButton').css('display','none');
			$jq('#bfRemoveLabelButton').css('visibility','hidden');
			$jq('#bfOptionsSaveMessage').css('display','none');
			$jq('#bfOptionsSaveMessage').css('visibility','hidden');
			$jq('#bfBesideCreationButton').css('display','none');
			$jq('#bfBesideCreationButton').css('visibility','hidden');
			$jq('#bfElementRemoveButton').css('display','none');
			$jq('#bfElementRemoveButton').css('visibility','hidden');
			$jq('#bfElementMoveLeft').css('display','none');
			$jq('#bfElementMoveRight').css('display','none');
			$jq('#bfInitScript').css('display','none');
			$jq('#bfInitScript').css('visibility','hidden');
			$jq('#bfActionScript').css('display','none');
			$jq('#bfActionScript').css('visibility','hidden');
			$jq('#bfValidationScript').css('display','none');
			$jq('#bfValidationScript').css('visibility','hidden');
			$jq('.bfScriptsSaveMessage').css('display','none');
		};
		
		this.handleLabel = function (label){
		
			appScope.disableElementsDetails();
			appScope.optionElement = label;
			
			if(appScope.optionElement != null){
			
				$jq('#bfOptionsWrapper').css('display','');
				$jq('#bfGlobalOptions').css('display','');
				$jq('#bfGlobalOptions').css('visibility','visible');
			
				$jq('#bfSaveOptionsButton').css('display','');
				$jq('#bfSaveOptionsButton').css('visibility','visible');
			
				$jq('#bfRemoveLabelButton').css('display','');
				$jq('#bfRemoveLabelButton').css('visibility','visible');
			
				$jq('#bfLabelOptions').css('display','');
				$jq('#bfLabelOptions').css('visibility','visible');
				
				$jq(label).resizable('destroy');
				
				$jq('#bfLabelContent').get(0).value = $jq(label).get(0).innerHTML;
				
				appScope.setElementResizable($jq(label).get(0));
				
				if($jq(label).parent().children('.ff_break')[0].style.display != 'none'){
					$jq('#bfLabelOnTop').attr('checked', true);
				} else {
					$jq('#bfLabelOnTop').attr('checked', false);
				}
				
				$jq('#bfLabelWidth').get(0).value  = $jq(label).get(0).style.width;
				$jq('#bfLabelHeight').get(0).value = $jq(label).get(0).style.height;
				$jq('#bfOptionsPadding').get(0).value = $jq(label).get(0).style.padding;
				$jq('#bfOptionsMargin').get(0).value = $jq(label).get(0).style.margin;
			}
		};
			
		this.populateElementValidationScript = function(){
			
			$jq('#bfValidationScript').css('display','');
			$jq('#bfValidationScript').css('visibility','visible');

			$jq('#bfValidationScriptSelection').empty();
			for(var i = 0; i < appScope.elementScripts.validation.length;i++){
				var option = document.createElement('option');
				$jq(option).val(appScope.elementScripts.validation[i].id);
				$jq(option).text(appScope.elementScripts.validation[i].package + '::' + appScope.elementScripts.validation[i].name); 
				if(appScope.elementScripts.validation[i].id == appScope.optionElement.script3id){
					//$jq(option).attr('selected', 'selected');
					$jq(option).get(0).setAttribute('selected', 'selected');
				}
				$jq('#bfValidationScriptSelection').append(option);
			}
			
			$jq('#script3msg').val(appScope.optionElement.script3msg);
			$jq('#script3code').val(appScope.optionElement.script3code);
			
			switch(appScope.optionElement.script3cond){
				case 1:
					$jq('.bfValidationType').attr('checked','');
					$jq('#bfValidationTypeLibrary').attr('checked','checked');
					$jq('#bfValidationScriptLibrary').css('display','');
					$jq('#bfValidationScriptCustom').css('display','none');
					$jq('#bfValidationScriptFlags').css('display','');
					$jq('#bfValidationScriptLibrary').css('display','');
					$jq('#bfValidationScriptCustom').css('display','none');
					break;
				case 2:
					$jq('.bfValidationType').attr('checked','');
					$jq('#bfValidationTypeCustom').attr('checked','checked');
					$jq('#bfValidationScriptFlags').css('display','');
					$jq('#bfValidationScriptLibrary').css('display','none');
					$jq('#bfValidationScriptCustom').css('display','');
					break;
				default:
					$jq('.bfValidationType').attr('checked','');
					$jq('#bfValidationTypeNone').attr('checked','checked');
					$jq('#bfValidationScriptFlags').css('display','none');
					$jq('#bfValidationScriptLibrary').css('display','none');
					$jq('#bfValidationScriptCustom').css('display','none');
			}
		};
				
		this.populateElementInitScript = function(){
			
			$jq('#bfInitScript').css('display','');
			$jq('#bfInitScript').css('visibility','visible');

			$jq('#bfInitScriptSelection').empty();
			for(var i = 0; i < appScope.elementScripts.init.length;i++){
				var option = document.createElement('option');
				$jq(option).val(appScope.elementScripts.init[i].id);
				$jq(option).text(appScope.elementScripts.init[i].package + '::' + appScope.elementScripts.init[i].name); 
				if(appScope.elementScripts.init[i].id == appScope.optionElement.script1id){
					//$jq(option).attr('selected', 'selected');
					$jq(option).get(0).setAttribute('selected', 'selected');
				}
				$jq('#bfInitScriptSelection').append(option);
			}
			
			if(appScope.optionElement.script1flag1 == 1){
				$jq('#script1flag1').get(0).checked = true;
			} else {
				$jq('#script1flag1').get(0).checked = false;
			}
			
			if(appScope.optionElement.script1flag2 == 1){
				$jq('#script1flag2').get(0).checked = true;
			} else {
				$jq('#script1flag2').get(0).checked = false;
			}
			
			$jq('#script1code').val(appScope.optionElement.script1code);
			
			switch(appScope.optionElement.script1cond){
				case 1:
					$jq('.bfInitType').attr('checked','');
					$jq('#bfInitTypeLibrary').attr('checked','checked');
					$jq('#bfInitScriptLibrary').css('display','');
					$jq('#bfInitScriptCustom').css('display','none');
					$jq('#bfInitScriptFlags').css('display','');
					$jq('#bfInitScriptLibrary').css('display','');
					$jq('#bfInitScriptCustom').css('display','none');
					break;
				case 2:
					$jq('.bfInitType').attr('checked','');
					$jq('#bfInitTypeCustom').attr('checked','checked');
					$jq('#bfInitScriptFlags').css('display','');
					$jq('#bfInitScriptLibrary').css('display','none');
					$jq('#bfInitScriptCustom').css('display','');
					break;
				default:
					$jq('.bfInitType').attr('checked','');
					$jq('#bfInitTypeNone').attr('checked','checked');
					$jq('#bfInitScriptFlags').css('display','none');
					$jq('#bfInitScriptLibrary').css('display','none');
					$jq('#bfInitScriptCustom').css('display','none');
			}
		};
		
		this.populateElementActionScript = function(){
			
			$jq('#bfActionScript').css('display','');
			$jq('#bfActionScript').css('visibility','visible');
			
			if(appScope.optionElement.internalType == 'bfSofortueberweisung' || appScope.optionElement.internalType == 'bfPayPal' || appScope.optionElement.internalType == 'bfIcon' || appScope.optionElement.internalType == 'bfImageButton' || appScope.optionElement.internalType == 'bfSubmitButton'){
				$jq('.script2flag').css('display','none');
				$jq('.script2flagLabel').css('display','none');
				$jq('#script2flag1').css('display','');
				$jq('#script2flag1Label').css('display','');
			} else {
				$jq('.script2flag').css('display','');
				$jq('.script2flagLabel').css('display','');
			}
			
			$jq('#bfActionsScriptSelection').empty();
			
			for(var i = 0; i < appScope.elementScripts.action.length;i++){
			
				var option = document.createElement('option');
				
				$jq(option).val(appScope.elementScripts.action[i].id);
				$jq(option).text(appScope.elementScripts.action[i].package + '::' + appScope.elementScripts.action[i].name); 
				
				if(appScope.elementScripts.action[i].id == appScope.optionElement.script2id){
					
					$jq(option).get(0).setAttribute('selected', 'selected');
				}
				
				$jq('#bfActionsScriptSelection').append(option);
			}
			
			if(appScope.optionElement.script2flag1 == 1){
				$jq('#script2flag1').get(0).checked = true;
			} else {
				$jq('#script2flag1').get(0).checked = false;
			}
			
			if(appScope.optionElement.script2flag2 == 1){
				$jq('#script2flag2').get(0).checked = true;
			} else {
				$jq('#script2flag2').get(0).checked = false;
			}
			
			if(appScope.optionElement.script2flag3 == 1){
				$jq('#script2flag3').get(0).checked = true;
			} else {
				$jq('#script2flag3').get(0).checked = false;
			}
			
			if(appScope.optionElement.script2flag4 == 1){
				$jq('#script2flag4').get(0).checked = true;
			} else {
				$jq('#script2flag4').get(0).checked = false;
			}
			
			if(appScope.optionElement.script2flag5 == 1){
				$jq('#script2flag5').get(0).checked = true;
			} else {
				$jq('#script2flag5').get(0).checked = false;
			}
			
			$jq('#script2code').val(appScope.optionElement.script2code);
			
			switch(appScope.optionElement.script2cond){
				case 1:
					$jq('.bfActionType').attr('checked','');
					$jq('#bfActionTypeLibrary').attr('checked','checked');
					$jq('#bfActionScriptLibrary').css('display','');
					$jq('#bfActionScriptCustom').css('display','none');
					$jq('#bfActionScriptFlags').css('display','');
					$jq('#bfActionScriptLibrary').css('display','');
					$jq('#bfActionScriptCustom').css('display','none');
					break;
				case 2:
					$jq('.bfActionType').attr('checked','');
					$jq('#bfActionTypeCustom').attr('checked','checked');
					$jq('#bfActionScriptFlags').css('display','');
					$jq('#bfActionScriptLibrary').css('display','none');
					$jq('#bfActionScriptCustom').css('display','');
					break;
				default:
					$jq('.bfActionType').attr('checked','');
					$jq('#bfActionTypeNone').attr('checked','checked');
					$jq('#bfActionScriptFlags').css('display','none');
					$jq('#bfActionScriptLibrary').css('display','none');
					$jq('#bfActionScriptCustom').css('display','none');
			}
		};
		
		/**
		* handles the selected element to show its options in the accordion menu
		*/
		this.handleElement = function(element){
			
			appScope.disableElementsDetails();
			
			if(element != null){
				
				appScope.optionElement = element;
			
				$jq('#bfOptionsWrapper').css('display','');
				$jq('#bfActions').css('display','');
				$jq('#bfActions').css('visibility','visible');
				$jq('#bfGlobalOptions').css('display','');
				$jq('#bfGlobalOptions').css('visibility','visible');
				$jq('#bfSaveOptionsButton').css('display','');
				$jq('#bfSaveOptionsButton').css('visibility','visible');
				$jq('#bfElementRemoveButton').css('display','');
				$jq('#bfElementRemoveButton').css('visibility','visible');
				$jq('#bfElementMoveLeft').css('display','');
				$jq('#bfElementMoveLeft').css('visibility','visible');
				$jq('#bfElementMoveRight').css('display','');
				$jq('#bfElementMoveRight').css('visibility','visible');
				$jq('#bfBesideCreationButton').css('display','');
				$jq('#bfBesideCreationButton').css('visibility','visible');
			
				switch(element.internalType){
					case 'bfStaticText':
						$jq('#bfStaticTextOptions').css('display','');
						$jq('#bfStaticTextOptions').css('visibility','visible');
						$jq('#bfStaticTextTitle').get(0).value   = element.title;
						$jq('#bfStaticTextWidth').get(0).value  = $jq('#'+element.id).get(0).style.width;
						$jq('#bfStaticTextHeight').get(0).value = $jq('#'+element.id).get(0).style.height;
						$jq('#'+element.id).resizable('destroy');
						
						$jq('#bfStaticTextContent').get(0).value = $jq('#'+element.id).get(0).innerHTML;
						appScope.setElementResizable($jq('#'+element.id).get(0));
						break;
					case 'bfIcon':
						$jq('#bfIconOptions').css('display','');
						$jq('#bfIconOptions').css('visibility','visible');
						$jq('#bfIconCaption').get(0).value  = $jq('#'+element.id).children('#ff_iconCaption'+element.rndId)[0].innerHTML;
						$jq('#bfIconWidth').get(0).value  = $jq('#'+element.id).get(0).style.width;
						$jq('#bfIconHeight').get(0).value = $jq('#'+element.id).get(0).style.height;
						$jq('#bfIconImage').get(0).value = $jq('#'+element.id).attr('src');
						$jq('#bfIconImage').get(0).value = element.data1;
						$jq('#bfIconImageOver').get(0).value = element.data3;
						appScope.populateElementActionScript();
						break;
					case 'bfCalendar':
						$jq('#bfCalendarOptions').css('display','');
						$jq('#bfCalendarOptions').css('visibility','visible');
						$jq('#bfCalendarFormat').get(0).value  = element.options.format;
						$jq('#bfCalendarConnectWith').get(0).value = element.options.connectWith;
						$jq('#'+element.id).resizable('destroy');
						$jq('#bfCalendarText').get(0).value = $jq('#'+element.id).get(0).innerHTML.replace('','');
						appScope.setElementResizable($jq('#'+element.id).get(0));
						break;
					case 'bfCaptcha':
						$jq('#bfCaptchaOptions').css('display','');
						$jq('#bfCaptchaOptions').css('visibility','visible');
						$jq('#bfCaptchaWidth').get(0).value  = $jq('.ff_captcha')[0].style.width;
						$jq('#bfCaptchaHeight').get(0).value = $jq('.ff_captcha')[0].style.height;
						break;
					case 'bfTextfield':
						$jq('#bfTextfieldOptions').css('display','');
						$jq('#bfTextfieldOptions').css('visibility','visible');
						$jq('#bfTextfieldTitle').get(0).value  = element.title;
						$jq('#bfTextfieldName').get(0).value   = element.name;
						$jq('#bfTextfieldValue').get(0).value = element.options.value;
						
						$jq('#bfTextfieldWidth').get(0).value  = $jq('#'+element.id).get(0).style.width;
						$jq('#bfTextfieldHeight').get(0).value = $jq('#'+element.id).get(0).style.height;
						$jq('#bfTextfieldMaxlength').get(0).value = $jq('#'+element.id).attr('maxlength') > -1 ? $jq('#'+element.id).attr('maxlength') : '';
						if(element.options.readonly){
							$jq('#bfTextfieldDisable').attr('checked', true);
							$jq('#'+element.id).get(0).setAttribute('readonly','readonly');
							
						} else {
							$jq('#bfTextfieldDisable').attr('checked', false);
							$jq('#'+element.id).get(0).setAttribute('readonly','');
							$jq('#'+element.id).get(0).removeAttribute('readonly');
						}
						if(element.options.password){
							$jq('#bfTextfieldPassword').attr('checked', true);
							$jq('#'+element.id).get(0).setAttribute('type','password');
							
						} else {
							$jq('#bfTextfieldPassword').attr('checked', false);
							$jq('#'+element.id).get(0).setAttribute('type', 'text');
						}
						if(element.options.mailback){
							$jq('#bfTextfieldMailback').attr('checked', true);
							
						} else {
							$jq('#bfTextfieldMailback').attr('checked', false);
						}
						if(element.mailbackAsSender){
							$jq('#bfTextfieldMailbackAsSender').attr('checked', true);
							
						} else {
							$jq('#bfTextfieldMailbackAsSender').attr('checked', false);
						}
						$jq('#bfTextfieldMailbackfile').get(0).value = element.mailbackfile;
						appScope.populateElementActionScript();
						appScope.populateElementInitScript();
						appScope.populateElementValidationScript();
						break;
					case 'bfTextarea':
						$jq('#bfTextareaOptions').css('display','');
						$jq('#bfTextareaOptions').css('visibility','visible');
						$jq('#bfTextareaTitle').get(0).value  = element.title;
						$jq('#bfTextareaName').get(0).value  = element.name;
						$jq('#bfTextareaValue').get(0).value = element.options.value;
						$jq('#bfTextareaWidth').get(0).value  = $jq('#'+element.id).get(0).style.width;
						$jq('#bfTextareaHeight').get(0).value = $jq('#'+element.id).get(0).style.height;
						if(element.options.readonly){
							$jq('#bfTextareaDisable').attr('checked', true);
							$jq('#'+element.id).get(0).setAttribute('readonly','readonly');
							
						} else {
							$jq('#bfTextareaDisable').attr('checked', false);
							$jq('#'+element.id).get(0).setAttribute('readonly','');
							$jq('#'+element.id).get(0).removeAttribute('readonly');
						}
						appScope.populateElementInitScript();
						appScope.populateElementActionScript();
						appScope.populateElementValidationScript();
						break;
					case 'bfCheckbox':
						$jq('#bfCheckboxOptions').css('display','');
						$jq('#bfCheckboxOptions').css('visibility','visible');
						$jq('#bfCheckboxChecked').get(0).checked = element.options.checked;
						$jq('#bfCheckboxValue').get(0).value = element.options.value;
						$jq('#bfCheckboxValue').get(0).setAttribute('value', element.options.value);
						$jq('#bfCheckboxTitle').get(0).value  = element.title;
						$jq('#bfCheckboxName').get(0).value  = element.name;
						$jq('#bfCheckboxWidth').get(0).value  = $jq('#'+element.id).get(0).style.width;
						$jq('#bfCheckboxHeight').get(0).value = $jq('#'+element.id).get(0).style.height;
						if(element.options.readonly){
							$jq('#bfCheckboxDisable').attr('checked', true);
							$jq('#'+element.id).get(0).setAttribute('readonly','readonly');
							
						} else {
							$jq('#bfCheckboxDisable').attr('checked', false);
							$jq('#'+element.id).get(0).setAttribute('readonly','');
							$jq('#'+element.id).get(0).removeAttribute('readonly');
						}
						if(element.mailbackAccept){
							$jq('#bfCheckboxMailbackAccept').attr('checked', true);
							
						} else {
							$jq('#bfCheckboxMailbackAccept').attr('checked', false);
						}						
						$jq('#bfCheckboxMailbackAcceptConnectWith').get(0).value = element.mailbackAcceptConnectWith;
						appScope.populateElementInitScript();
						appScope.populateElementActionScript();
						appScope.populateElementValidationScript();
						break;
					case 'bfRadio':
						$jq('#bfRadioOptions').css('display','');
						$jq('#bfRadioOptions').css('visibility','visible');
						$jq('#bfRadioChecked').get(0).checked = element.options.checked;
						$jq('#bfRadioValue').get(0).value = element.options.value;
						$jq('#bfRadioValue').get(0).setAttribute('value', element.options.value);
						$jq('#bfRadioTitle').get(0).value = element.title;
						$jq('#bfRadioName').get(0).value  = element.name;
						$jq('#bfRadioWidth').get(0).value  = $jq('#'+element.id).get(0).style.width;
						$jq('#bfRadioHeight').get(0).value = $jq('#'+element.id).get(0).style.height;
						if(element.options.readonly){
							$jq('#bfRadioDisable').attr('checked', true);
							$jq('#'+element.id).get(0).setAttribute('readonly','readonly');
							
						} else {
							$jq('#bfRadioDisable').attr('checked', false);
							$jq('#'+element.id).get(0).setAttribute('readonly','');
							$jq('#'+element.id).get(0).removeAttribute('readonly');
						}
						appScope.populateElementInitScript();
						appScope.populateElementActionScript();
						appScope.populateElementValidationScript();
						break;
					case 'bfSelect':
						$jq('#bfSelectOptions').css('display','');
						$jq('#bfSelectOptions').css('visibility','visible');
						$jq('#bfSelectTitle').get(0).value = element.title;
						$jq('#bfSelectName').get(0).value  = element.name;
						
						if(element.options.multiple){
							$jq('#bfSelectMultipleYes').attr('checked', true);
							$jq('#bfSelectMultipleNo').attr('checked', false);
							
						} else {
							$jq('#bfSelectMultipleNo').attr('checked', true);
							$jq('#bfSelectMultipleYes').attr('checked', false);
						}
						
						$jq('#bfSelectOpts').get(0).value = element.options.options;
						
						$jq('#bfSelectWidth').get(0).value  = $jq('#'+element.id).get(0).style.width;
						$jq('#bfSelectHeight').get(0).value = $jq('#'+element.id).get(0).style.height;
						if(element.options.readonly){
							$jq('#bfSelectDisable').attr('checked', true);
							$jq('#'+element.id).get(0).setAttribute('readonly','readonly');
							
						} else {
							$jq('#bfTextfieldDisable').attr('checked', false);
							$jq('#'+element.id).get(0).setAttribute('readonly','');
							$jq('#'+element.id).get(0).removeAttribute('readonly');
						}
						if(element.options.mailback){
							$jq('#bfSelectMailback').attr('checked', true);
							
						} else {
							$jq('#bfSelectMailback').attr('checked', false);
						}
						appScope.populateElementInitScript();
						appScope.populateElementActionScript();
						appScope.populateElementValidationScript();
						break;
					case 'bfFile':
						$jq('#bfFileOptions').css('display','');
						$jq('#bfFileOptions').css('visibility','visible');
						$jq('#bfFileTitle').get(0).value = element.title;
						$jq('#bfFileName').get(0).value  = element.name;
						
						if(element.options.timestamp){
							$jq('#bfFileTimestamp').attr('checked', true);
						} else {
							$jq('#bfFileTimestamp').attr('checked', false);
						}
						
						if(element.options.attachToAdminMail){
							$jq('#bfFileAttachToAdminMail').attr('checked', true);
						} else {
							$jq('#bfFileAttachToAdminMail').attr('checked', false);
						}
						
						if(element.options.attachToUserMail){
							$jq('#bfFileAttachToUserMail').attr('checked', true);
						} else {
							$jq('#bfFileAttachToUserMail').attr('checked', false);
						}
						
						$jq('#bfFileUploadDirectory').get(0).value  = element.options.uploadDirectory;
						if(!element.options.allowedFileExtensions) element.options['allowedFileExtensions'] = -1; // compat 690
						$jq('#bfFileAllowedFileExtensions').get(0).value  = element.options.allowedFileExtensions;
						$jq('#bfFileWidth').get(0).value  = $jq('#'+element.id).get(0).style.width;
						$jq('#bfFileHeight').get(0).value = $jq('#'+element.id).get(0).style.height;
						if(element.options.readonly){
							$jq('#bfFileDisable').attr('checked', true);
							$jq('#'+element.id).get(0).setAttribute('readonly','readonly');
							
						} else {
							$jq('#bfFileDisable').attr('checked', false);
							$jq('#'+element.id).get(0).setAttribute('readonly','');
							$jq('#'+element.id).get(0).removeAttribute('readonly');
						}
						appScope.populateElementInitScript();
						appScope.populateElementActionScript();
						appScope.populateElementValidationScript();
						break;
					case 'bfImageButton':
						$jq('#bfImageButtonOptions').css('display','');
						$jq('#bfImageButtonOptions').css('visibility','visible');
						$jq('#bfImageButtonTitle').get(0).value = element.title;
						$jq('#bfImageButtonName').get(0).value  = element.name;
						$jq('#bfImageButtonValue').get(0).value = element.options.value;
						$jq('#bfImageButtonWidth').get(0).value  = $jq('#'+element.id).get(0).style.width;
						$jq('#bfImageButtonHeight').get(0).value = $jq('#'+element.id).get(0).style.height;
						$jq('#bfImageButtonImage').get(0).value = $jq('#'+element.id).get(0).getAttribute('src');
						if(element.options.readonly){
							$jq('#bfImageButtonDisable').attr('checked', true);
							$jq('#'+element.id).get(0).setAttribute('readonly','readonly');
							
						} else {
							$jq('#bfImageButtonDisable').attr('checked', false);
							$jq('#'+element.id).get(0).setAttribute('readonly','');
							$jq('#'+element.id).get(0).removeAttribute('readonly');
						}
						appScope.populateElementActionScript();
						break;
					case 'bfSubmitButton':
						$jq('#bfSubmitButtonOptions').css('display','');
						$jq('#bfSubmitButtonOptions').css('visibility','visible');
						$jq('#bfSubmitButtonTitle').get(0).value = element.title;
						$jq('#bfSubmitButtonName').get(0).value  = element.name;
						$jq('#bfSubmitButtonValue').get(0).value = element.options.value;
						$jq('#bfSubmitButtonWidth').get(0).value  = $jq('#'+element.id).get(0).style.width;
						$jq('#bfSubmitButtonHeight').get(0).value = $jq('#'+element.id).get(0).style.height;
						if(element.options.readonly){
							$jq('#bfSubmitButtonDisable').attr('checked', true);
							$jq('#'+element.id).get(0).setAttribute('readonly','readonly');
							
						} else {
							$jq('#bfSubmitButtonDisable').attr('checked', false);
							$jq('#'+element.id).get(0).setAttribute('readonly','');
							$jq('#'+element.id).get(0).removeAttribute('readonly');
						}
						appScope.populateElementActionScript();
						break;
					case 'bfTooltip':
						$jq('#bfTooltipOptions').css('display','');
						$jq('#bfTooltipOptions').css('visibility','visible');
						$jq('#bfTooltipTitle').get(0).value = element.title;
						$jq('#bfTooltipName').get(0).value  = element.name;
						if(element.options.type == 'info'){
							$jq('#bfTooltipTypeInfo').get(0).checked = true;
							$jq('#bfTooltipTypeWarning').get(0).checked = false;
							$jq('#bfTooltipTypeCustom').get(0).checked = false;
						} else if(element.options.type == 'warning'){
							$jq('#bfTooltipTypeInfo').get(0).checked = false;
							$jq('#bfTooltipTypeWarning').get(0).checked = true;
							$jq('#bfTooltipTypeCustom').get(0).checked = false;
						} else if(element.options.type == 'custom'){
							$jq('#bfTooltipTypeInfo').get(0).checked = false;
							$jq('#bfTooltipTypeWarning').get(0).checked = false;
							$jq('#bfTooltipTypeCustom').get(0).checked = true;
							$jq('#bfTooltipCustomImage').get(0).value = element.options.image;
						}
						$jq('#bfTooltipText').get(0).value = element.options.text;
						$jq('#bfTooltipWidth').get(0).value  = $jq('#'+element.id).get(0).style.width;
						$jq('#bfTooltipHeight').get(0).value = $jq('#'+element.id).get(0).style.height;
						break;
						
					case 'bfPayPal':
					
						$jq('#bfPayPalOptions').css('display','');
						$jq('#bfPayPalOptions').css('visibility','visible');
						
						$jq('#bfPayPalTitle').get(0).value = element.title;
						$jq('#bfPayPalName').get(0).value  = element.name;
						
						if(element.options.testaccount){
							$jq('#bfPayPalTestaccountYes').attr('checked', 'checked');
							$jq('#bfPayPalTestaccountNo').attr('checked', '');
						} else {
							$jq('#bfPayPalTestaccountYes').attr('checked', '');
							$jq('#bfPayPalTestaccountNo').attr('checked', 'checked');
						}
						
						if(element.options.downloadableFile){
							$jq('#bfPayPalDownloadableFileYes').attr('checked', 'checked');
							$jq('#bfPayPalDownloadableFileNo').attr('checked', '');
						} else {
							$jq('#bfPayPalDownloadableFileYes').attr('checked', '');
							$jq('#bfPayPalDownloadableFileNo').attr('checked', 'checked');
						}
						
						$jq('#bfPayPalFilepath').get(0).value  = element.options.filepath;
						$jq('#bfPayPalFileDownloadTries').get(0).value  = element.options.downloadTries;
						$jq('#bfPayPalBusiness').get(0).value  = element.options.business;
						$jq('#bfPayPalToken').get(0).value  = element.options.token;
						$jq('#bfPayPalTestBusiness').get(0).value  = element.options.testBusiness;
						$jq('#bfPayPalTestToken').get(0).value  = element.options.testToken;
						$jq('#bfPayPalItemname').get(0).value  = element.options.itemname;
						$jq('#bfPayPalItemnumber').get(0).value  = element.options.itemnumber;
						$jq('#bfPayPalAmount').get(0).value  = element.options.amount;
						$jq('#bfPayPalTax').get(0).value  = element.options.tax;
						$jq('#bfPayPalThankYouPage').get(0).value  = element.options.thankYouPage;
						$jq('#bfPayPalLocale').get(0).value  = element.options.locale;
						$jq('#bfPayPalCurrencyCode').get(0).value  = element.options.currencyCode;
						$jq('#bfPayPalImage').get(0).value  = $jq('#'+element.id).attr('src');
						$jq('#bfPayPalWidth').get(0).value  = $jq('#'+element.id).get(0).style.width;
						$jq('#bfPayPalHeight').get(0).value  = $jq('#'+element.id).get(0).style.height;
						
						appScope.populateElementActionScript();
						
						break;
						
					case 'bfSofortueberweisung':
					
						$jq('#bfSofortueberweisungOptions').css('display','');
						$jq('#bfSofortueberweisungOptions').css('visibility','visible');
						
						$jq('#bfSofortueberweisungTitle').get(0).value = element.title;
						$jq('#bfSofortueberweisungName').get(0).value  = element.name;
						
						if(element.options.downloadableFile){
							$jq('#bfSofortueberweisungDownloadableFileYes').attr('checked', 'checked');
							$jq('#bfSofortueberweisungDownloadableFileNo').attr('checked', '');
						} else {
							$jq('#bfSofortueberweisungDownloadableFileYes').attr('checked', '');
							$jq('#bfSofortueberweisungDownloadableFileNo').attr('checked', 'checked');
						}
						
						if(element.options.mailback){
							$jq('#bfSofortueberweisungMailbackYes').attr('checked', 'checked');
							$jq('#bfSofortueberweisungMailbackNo').attr('checked', '');
						} else {
							$jq('#bfSofortueberweisungMailbackYes').attr('checked', '');
							$jq('#bfSofortueberweisungMailbackNo').attr('checked', 'checked');
						}
						
						$jq('#bfSofortueberweisungFilepath').get(0).value  = element.options.filepath;
						$jq('#bfSofortueberweisungFileDownloadTries').get(0).value  = element.options.downloadTries;
						$jq('#bfSofortueberweisungUserId').get(0).value  = element.options.user_id;
						$jq('#bfSofortueberweisungProjectId').get(0).value  = element.options.project_id;
						$jq('#bfSofortueberweisungProjectPassword').get(0).value  = element.options.project_password;
						$jq('#bfSofortueberweisungReason1').get(0).value  = element.options.reason_1;
						$jq('#bfSofortueberweisungReason2').get(0).value  = element.options.reason_2;
						$jq('#bfSofortueberweisungAmount').get(0).value  = element.options.amount;
						$jq('#bfSofortueberweisungThankYouPage').get(0).value  = element.options.thankYouPage;
						$jq('#bfSofortueberweisungLanguageId').get(0).value  = element.options.language_id;
						$jq('#bfSofortueberweisungCurrencyId').get(0).value  = element.options.currency_id;
						$jq('#bfSofortueberweisungImage').get(0).value  = $jq('#'+element.id).attr('src');
						$jq('#bfSofortueberweisungWidth').get(0).value  = $jq('#'+element.id).get(0).style.width;
						$jq('#bfSofortueberweisungHeight').get(0).value  = $jq('#'+element.id).get(0).style.height;
						
						appScope.populateElementActionScript();
						
						break;
				}
				
				
				$jq('#bfOptionsPadding').get(0).value = $jq('#'+element.id).get(0).style.padding;
				$jq('#bfOptionsMargin').get(0).value = $jq('#'+element.id).get(0).style.margin;
				$jq('#bfOptionsOrder').get(0).value = element.orderNumber;
				$jq('#bfOptionsTabIndex').get(0).value = element.tabIndex;
			}
		};
		
		this.resizableElement = function(){
		
			var internalElement = null;
			var clazz = $jq(this).attr('class');
			
			if(clazz.substr(0, 8) != 'ff_label'){
				if(clazz == 'ui-resizable-handle ui-resizable-se' || clazz == 'ui-resizable-handle ui-resizable-se '){
					internalElement = appScope.getElementById( $jq( $jq(this).parent().children('.ff_elem')[0] ).attr('id') );
				} else {
					internalElement = appScope.getElementById($jq(this).attr('id'));
				}
				
				appScope.handleElement(internalElement);
			} else {
				internalElement = this;
				appScope.handleLabel(internalElement);
			}
		};
		
		this.cleanResizables = function(){
			$jq('.ff_elem').resizable('disable');
			$jq('.ff_label').resizable('disable');
		}
		
		this.destroyResizables = function(){
			$jq('.ff_elem').resizable('disable');
			$jq('.ff_label').resizable('disable');
			$jq('.ff_elem').resizable('destroy');
			$jq('.ff_label').resizable('destroy');
		}
		
		this.removeElement = function(element){
			$jq('.bfOptionsWrapper').css('display','none');
			$jq('.bfOptions').css('display','none');
			$jq('.bfOptions').css('visibility','hidden');
			$jq('#bfActions').css('display','none');
			$jq('#bfActions').css('visibility','hidden');
			$jq('#bfSaveOptionsButton').css('display','none');
			$jq('#bfSaveOptionsButton').css('visibility','hidden');
			$jq('#bfOptionsSaveMessage').css('display','none');
			$jq('#bfOptionsSaveMessage').css('visibility','hidden');
			$jq('#bfBesideCreationButton').css('display','none');
			$jq('#bfBesideCreationButton').css('visibility','hidden');
			$jq('#bfElementRemoveButton').css('display','none');
			$jq('#bfElementRemoveButton').css('visibility','hidden');
			$jq('#bfInitScript').css('display','none');
			$jq('#bfInitScript').css('visibility','hidden');
			$jq('#bfActionScript').css('display','none');
			$jq('#bfActionScript').css('visibility','hidden');
			$jq('#bfValidationScript').css('display','none');
			$jq('#bfValidationScript').css('visibility','hidden');
			$jq('.bfScriptsSaveMessage').css('display','none');
			$jq('#bfElementMoveLeft').css('display','none');
			$jq('#bfElementMoveRight').css('display','none');
		
			$jq('#'+element.wrapperId).remove();
			if(element.internalType == 'bfCaptcha'){
				appScope.captchaAdded--;
			}
			if($jq('#'+element.listItemId).children('.ff_div').length == 0){
				$jq('#'+element.listItemId).remove();
			}
			appScope.optionElement = null;
			appScope.removeElementFromAreaList(element);
		};
		
		this.moveElement = function(element, direction){
			if(direction != 'prev' && direction != 'next'){
				return;
			}
			
			if(direction == 'next'){
				if(typeof $jq('#'+element.wrapperId + ' + .ff_div') != 'undefined'){
					var nextElement = $jq('#'+element.wrapperId + ' + .ff_div');
					$jq('#'+element.wrapperId).insertAfter(nextElement);
				}
				if(typeof $jq('#'+element.wrapperId + ' + .ui-wrapper') != 'undefined'){
					var nextElement = $jq('#'+element.wrapperId + ' + .ui-wrapper');
					$jq('#'+element.wrapperId).insertAfter(nextElement);
				}
			} else if(direction == 'prev'){
				
				if(typeof $jq('#'+element.wrapperId).prev() != 'undefined' && $jq('#'+element.wrapperId).prev().attr('class') == 'ff_div'){
					var prevElement = $jq('#'+element.wrapperId).prev();
					$jq('#'+element.wrapperId).insertBefore(prevElement);
				}
			}
		}
		
		this.createElementByType = function(typeName, rndId){
		
			var element = null;
			var bfType = '';
			var elementType = '';
			var options = {};
			var data1 = '';
			var data2 = '';
			var data3 = '';
			var script1cond  = 0;
			var script1id    = 0;
			var script1code  = '';
			var script1flag1 = 0;
			var script1flag2 = 0;
			var script2cond  = 0;
			var script2id    = 0;
			var script2code  = '';
			var script2flag1 = 0;
			var script2flag2 = 0;
			var script2flag3 = 0;
			var script2flag4 = 0;
			var script2flag5 = 0;
			var script3cond  = 0;
			var script3id    = 0;
			var script3code  = '';
			var script3msg   = '';
			var functionNameScript1 = '';
			var functionNameScript2 = '';
			var functionNameScript3 = '';
			var flag1 = 0;
			var flag2 = 0;
			var mailback = 0;
			var mailbackfile = '';
					
			switch(typeName){

				case 'bfCalendar':
				
					bfType = 'Calendar';
					elementType = 'div';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('class', 'ff_elem');
					element.innerHTML = 'pick date';
					
					$jq(element).click(appScope.resizableElement);
					
					options = { format: 'y-mm-dd', connectWith: '' };
					
					break;

				case 'bfCaptcha':
				
					bfType = 'Captcha';
					elementType = 'div';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('class', 'ff_elem');
					
					var imageHolder = document.createElement('img');
					imageHolder.setAttribute('id', 'ff_capimgValue');
					imageHolder.setAttribute('class', 'ff_capimg');
					imageHolder.setAttribute('src', '<?php echo JURI::root() ?>ff_secimage.php?option=com_facileforms&bfCaptcha=true');
				
					elementType = 'br';
					var br = document.createElement(elementType);
				
					elementType = 'input';
					var cap = document.createElement(elementType);
					cap.setAttribute('id',   'bfCaptchaEntry');
					cap.setAttribute('name', 'bfCaptchaEntry'); 
					cap.setAttribute('type', 'text');
					cap.setAttribute('class', 'ff_captcha');
					$jq(cap).css('width','150px');
					$jq(cap).css('float','left');
					var reload = document.createElement('div');
					$jq(reload).css('float','left');
					$jq(reload).css('padding-left','5px');
					reload.innerHTML = '<a href="#" onclick="document.getElementById(\'bfCaptchaEntry\').value=\'\';document.getElementById(\'bfCaptchaEntry\').focus();document.getElementById(\'ff_capimgValue\').src = \'<?php echo JURI::root() ?>ff_secimage.php?option=com_facileforms&bfCaptcha=true?\' + Math.random(); return false"><img src="<?php echo JURI::root() ?>components/com_facileforms/images/captcha/refresh-captcha.png" border="0" /></a>';
				
					$jq(element).append(imageHolder);
					$jq(element).append(br);
					$jq(element).append(cap);
					$jq(element).append(reload);
					
					$jq(element).click(appScope.resizableElement);
					
					break;

				case 'bfIcon':
												
					var iconImage = '<?php echo JURI::root() ?>administrator/images/next.png';
												
					bfType = 'Icon';
					elementType = 'div';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('class', 'ff_elem');
					element.setAttribute('align', 'center');
					
					var imageHolder = document.createElement('img');
					imageHolder.setAttribute('id', 'ff_icon' + rndId);
					imageHolder.setAttribute('class', 'ff_icon');
					imageHolder.setAttribute('src', iconImage);
													
					var textHolder = document.createElement('div');
					textHolder.setAttribute('id', 'ff_iconCaption' + rndId);
					textHolder.setAttribute('class', 'ff_iconCaption');
					textHolder.innerHTML = '<div align="center">Next</div>';
													
					$jq(element).append(imageHolder);
					$jq(element).append(textHolder);
													
					$jq(element).click(appScope.resizableElement);
											
					data3 = '<?php echo JURI::root() ?>administrator/images/next_f2.png',
					data1 = iconImage;
													
					break;

				case 'bfTextfield':

					bfType = 'Text';
					elementType = 'input';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('name', 'ff_nm_' + rndId + '[]'); 
					element.setAttribute('type', 'text');
					element.setAttribute('class', 'ff_elem');
					$jq(element).focus(appScope.resizableElement);
					$jq(element).css('width','170px');
					$jq(element).css('height','18px');
					
					options = {value : '', readonly: false, password: false, mailback: false};
		
				break; 

					case 'bfStaticText':

					bfType = 'Static Text/HTML';
					elementType = 'div';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('class', 'ff_elem');
					element.innerHTML = 'Enter Text here...';
					element.style.width = '150px';
					element.style.height = '20px';
					$jq(element).click(appScope.resizableElement);
													
				break;

					case 'bfTextarea':

					bfType = 'Textarea';
					elementType = 'textarea';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('name', 'ff_nm_' + rndId + '[]'); 
					element.setAttribute('type', 'textarea');
					element.setAttribute('class', 'ff_elem');
													
					$jq(element).focus(appScope.resizableElement);
					
					options = {value : '', readonly: false};
												
					break;

				case 'bfCheckbox':

					bfType = 'Checkbox';
					elementType = 'input';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('name', 'ff_nm_' + rndId + '[]'); 
					element.setAttribute('type', 'checkbox');
					element.setAttribute('class', 'ff_elem');
					$jq(element).click(appScope.resizableElement);
					
					options = {checked : false, value : 'cb', readonly: false};
					
					break;  

				case 'bfRadio':

					bfType = 'Radio Button';
					elementType = 'input';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('name', 'ff_nm_' + rndId + '[]'); 
					element.setAttribute('type', 'radio');
					element.setAttribute('class', 'ff_elem');
					$jq(element).click(appScope.resizableElement);
					
					options = {checked : false, value : 'on', readonly: false};
					
					break;


				case 'bfSelect':

					bfType = 'Select List';
					elementType = 'select';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('name', 'ff_nm_' + rndId + '[]');
					element.setAttribute('class', 'ff_elem');
					$jq(element).focus(appScope.resizableElement);
					$jq(element).css('width','110px');
					$jq(element).css('height','25px');
					
					options = {multiple : false, options : "1;Select Color;\n0;Red;red\n0;Green;green\n0;Blue;blue", readonly: false, mailback: false};
					
					data1 = 1;
					data2 = "1;Select Color;\n0;Red;red\n0;Green;green\n0;Blue;blue";
					
					break;
													
				case 'bfFile':

					bfType = 'File Upload';
					elementType = 'input';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('name', 'ff_nm_' + rndId + '[]'); 
					element.setAttribute('type', 'file');
					element.setAttribute('class', 'ff_elem');
					
					$jq(element).css('height','25px');
					
					$jq(element).click(appScope.resizableElement);
						
					options = { 
						uploadDirectory: '{ff_uploads}', 
						timestamp: false, 
						readonly: false, 
						allowedFileExtensions: 'zip,rar,pdf,doc,xls,ppt,jpg,jpeg,gif,png',
						attachToUserMail: false,
						attachToAdminMail: false 
					};
													
					data1 = '{ff_uploads}';
					break;

				case 'bfSubmitButton':

					bfType = 'Regular Button';
					elementType = 'input';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('name', 'ff_nm_' + rndId + '[]');
					element.setAttribute('type', 'submit');
					element.setAttribute('class', 'ff_elem');
					$jq(element).click(appScope.resizableElement);
					$jq(element).css('padding','0');
					$jq(element).css('width','176px');
					$jq(element).css('height','26px');
					
					options = { readonly: false, value : '' };
					
					break;
													
				case 'bfImageButton':
				
					var iconImage = '<?php echo JURI::root() ?>administrator/images/next.png';
					bfType = 'Graphic Button';
					elementType = 'input';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('name', 'ff_nm_' + rndId + '[]');
					element.setAttribute('type', 'image');
					element.setAttribute('class', 'ff_elem');
					element.setAttribute('src', iconImage);
					data1 = iconImage;
					$jq(element).click(appScope.resizableElement);
					
					options = { readonly: false, value : ''  };
					
					break;  
												
				case 'bfHidden':

					bfType = 'Hidden Input';
					elementType = 'input';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('name', 'ff_nm_' + rndId + '[]'); 
					element.setAttribute('type', 'hidden');
					element.setAttribute('class', 'ff_elem');
					
					options = { value: '' };
		
					break;
					
				case 'bfTooltip':

					bfType = 'Tooltip';
					elementType = 'img';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('name', 'ff_nm_' + rndId + '[]');
					element.setAttribute('class', 'ff_elem');
					element.setAttribute('src', '<?php echo JURI::root() ?>includes/js/ThemeOffice/tooltip.png');
					$jq(element).click(appScope.resizableElement);
					
					options = { text: 'Some <em>hint</em> or <strong/>warning</strong> for the user', image: '<?php echo JURI::root() ?>includes/js/ThemeOffice/tooltip.png', type: 'info' };
					data2 = 'Some <em>hint</em> or <strong/>warning</strong> for the user';
					break;  
					
				case 'bfPayPal':
				
					var iconImage = 'http://www.paypal.com/en_US/i/btn/btn_paynowCC_LG.gif';
					bfType = 'PayPal';
					elementType = 'input';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('name', 'ff_nm_' + rndId + '[]');
					element.setAttribute('type', 'image');
					element.setAttribute('class', 'ff_elem');
					element.setAttribute('src', iconImage);
					$jq(element).css('width','144px');
					$jq(element).css('height','47px');
					data1 = iconImage;
					$jq(element).click(appScope.resizableElement);
					
					options = { 
						testaccount: false, 
						downloadableFile: false,
						filepath: '',
						downloadTries: 1,
						business: '',
						token: '',
						testBusiness: '',
						testToken: '',
						itemname: '',
						itemnumber: '',
						amount: '',
						tax: '',
						thankYouPage: '<?php echo JURI::root()?>',
						locale: 'us',
						currencyCode: 'USD',
						image: iconImage
					};
					
					break;
					
				case 'bfSofortueberweisung':
				
					var iconImage = '<?php echo JURI::root()?>components/com_facileforms/images/200x65px.png';
					bfType = 'Sofortueberweisung';
					elementType = 'input';
					element = document.createElement(elementType);
					element.setAttribute('id', 'ff_elem' + rndId);
					element.setAttribute('name', 'ff_nm_' + rndId + '[]');
					element.setAttribute('type', 'image');
					element.setAttribute('class', 'ff_elem');
					element.setAttribute('src', iconImage);
					$jq(element).css('width','144px');
					$jq(element).css('height','47px');
					data1 = iconImage;
					$jq(element).click(appScope.resizableElement);
					
					options = {
						downloadableFile: false,
						filepath: '',
						downloadTries: 1,
						user_id: '',
						project_id: '',
						project_password: '',
						reason_1: '',
						reason_2: '',
						amount: '',
						thankYouPage: '<?php echo JURI::root()?>',
						language_id: 'DE',
						currency_id: 'EUR',
						image: iconImage,
						mailback : false
					};
					
					break;
													
			}
			
			
			$jq(element).css('border','none');
			
			return { element: element, 
			         bfType: bfType, 
			         elementType: elementType, 
			         data1: data1, 
			         data2: data2, 
			         data3: data3,
			         script1cond: script1cond,
					 script1id: script1id,
					 script1code: script1code,
					 script1flag1: script1flag1,
					 script1flag2: script1flag2,
					 script2cond: script2cond,
					 script2id: script2id,
					 script2code: script2code,
					 script2flag1: script2flag1,
					 script2flag2: script2flag2,
					 script2flag3: script2flag3,
					 script2flag4: script2flag4,
					 script2flag5: script2flag5,
					 script3cond: script3cond,
					 script3id: script3id,
					 script3code: script3code,
					 script3msg: script3msg,
					 functionNameScript1: functionNameScript1,
					 functionNameScript2: functionNameScript2,
					 functionNameScript3: functionNameScript3,
					 flag1 : flag1,
					 flag2 : flag2,
					 mailback: mailback,
					 mailbackfile: mailbackfile,
			         options: options 
			       };
		
		};
		
		this.initMouseOvers = function(){
		
		
			$jq('.ff_div').mouseover(function(){
				$jq('.ff_div').css('border','none');
				$jq(this).css('border', '1px dotted #000');
				
				$jq('.ff_div').children('.ff_label').children('.ui-resizable-se').css('display', 'none');
				$jq('.ff_div').children('.ui-wrapper').children('.ui-resizable-se').css('display', 'none');
				$jq('.ff_div').children('.ff_elem').children('.ui-resizable-se').css('display', 'none');
				
				if($jq(this).children('.ff_label').length != 0){
					var label = $jq($jq(this).children('.ff_label')[0]);
					var labres  = $jq(label.children('.ui-resizable-se')[0]);
					labres.css('display', '');
				}
				
				if($jq(this).children('.ui-wrapper').length != 0){
					var wrapper = $jq($jq(this).children('.ui-wrapper')[0]);
					var res     = $jq(wrapper.children('.ui-resizable-se')[0]);
					res.css('display', 'block');
					
					wrapper.css('padding-bottom', '-13px');
					
					if(wrapper.children('.ff_elem').length != 0){
						var elem = $jq(wrapper.children('.ff_elem')[0]);
						//elem.css('padding','0');
						elem.css('border','none');
					}
					
				} else if($jq(this).children('.ff_elem').length != 0){
					var elem = $jq($jq(this).children('.ff_elem')[0]);
					elem.children('.ui-resizable-se').css('display', '');
					elem.css('border','none');
				}
			});
			
			$jq('.ff_div').bind('mouseleave',function(){
			
				$jq(this).css('border', 'none');
			
				if($jq(this).children('.ff_label').length != 0){
					var label = $jq($jq(this).children('.ff_label')[0]);
					var labres  = $jq(label.children('.ui-resizable-se')[0]);
					labres.css('display', 'none');
				}
				
				if($jq(this).children('.ui-wrapper').length != 0){
					var wrapper = $jq($jq(this).children('.ui-wrapper')[0]);
					var res     = $jq(wrapper.children('.ui-resizable-se')[0]);
					res.css('display', 'none');
					
					wrapper.css('padding-bottom', '0px');
					
					if(wrapper.children('.ff_elem').length != 0){
						var elem = $jq(wrapper.children('.ff_elem')[0]);
						//elem.css('padding','0');
						elem.css('border','none');
					}
				} else if($jq(this).children('.ff_elem').length != 0){
					var elem = $jq($jq(this).children('.ff_elem')[0]);
					elem.children('.ui-resizable-se').css('display', 'none');
					elem.css('border','none');
				}
				
			});
			
			$jq('.ui-wrapper').mouseover(function(){
				$jq(this).css('border', '1px dotted #000');
			});
			
			$jq('.ui-wrapper').mouseout(function(){
				$jq(this).css('border', 'none');
			});
			
			$jq('.ff_label').mouseover(function(){
				$jq(this).css('border', '1px dotted #000');
			});
			
			$jq('.ff_label').mouseout(function(){
				$jq(this).css('border', 'none');
			});
		
		};
		
		this.refreshBatchOptions = function(){
			$jq('#bfBatchLabels').empty();
			$jq('#bfBatchElements').empty();
			for(var i = 0; i < appScope.areaList.length; i++){
			
				for(var j = 0; j < appScope.areaList[i].elements.length;j++){
					var element = appScope.areaList[i].elements[j]
					
					var labelsResult = $jq('#'+element.id).parent().parent().children('.ff_label');
					if(labelsResult.length != 0){
						for(var k = 0; k < labelsResult.length;k++){
							var batchLabel = document.createElement('option');
							$jq(batchLabel).val(labelsResult[k].id);
							$jq(batchLabel).text(stripHTML( $jq('#'+labelsResult[k].id).html() ) );
							$jq('#bfBatchLabels').append(batchLabel);
						}
					}
					
					labelsResult = $jq('#'+element.id).parent().children('.ff_label');
					if(labelsResult.length != 0){
						for(var k = 0; k < labelsResult.length;k++){
							var batchLabel = document.createElement('option');
							$jq(batchLabel).val(labelsResult[k].id);
							$jq(batchLabel).text(stripHTML( $jq('#'+labelsResult[k].id).html() ) );
							$jq('#bfBatchLabels').append(batchLabel);
						}
					}		
					
					if(element.internalType != 'bfHidden'){
						var batchElement = document.createElement('option');
						$jq(batchElement).val(element.id);
						$jq(batchElement).text(element.name);
						$jq('#bfBatchElements').append(batchElement);
					}
				}
			}
		};
		
		/**
		* JQuery initalizations
		*/  
		appScope.initMouseOvers();
		
		$jq('.attachToAdminMail').remove();
		$jq('.attachToUserMail').remove();
		$jq('.mailbackConnectWith').remove();
		$jq('.mailbackSender').remove();
		
		$jq('#bfUpdateTemplateButton').click(function(){
			$jq('#bfTemplate').get(0).innerHTML = $jq.trim($jq('#bfTemplateBox').val());
			submitbutton('save');
		});
		
		$jq('.ff_elem').css('border','none');
		$jq('.ff_div').children('.ff_label').children('.ui-resizable-se').css('display', 'none');
		$jq('.ff_div').children('.ui-wrapper').children('.ui-resizable-se').css('display', 'none');
		$jq('.ff_div').children('.ff_elem').children('.ui-resizable-se').css('display', 'none');
		$jq('.ff_div').children('.ui-wrapper').css('padding-bottom','0px');
		
		$jq('.ff_elem').click(this.resizableElement);
		$jq('.ff_elem').focus(this.resizableElement);
		$jq('.ui-resizable-se').click(this.resizableElement);
		$jq('.ff_label').click(this.resizableElement);
		
		$jq("#menutab").tabs( { select: function(e, ui){ } } );
		$jq("#nestedaccordion").accordion({autoHeight:false, collapsible: true, change: function(e, ui){ 
			appScope.refreshTemplateBox(); 	
		}});
		$jq("#nestedaccordion2").accordion({autoHeight:false, collapsible: true, change: function(e, ui){ 
			appScope.refreshTemplateBox(); 	
		}});
		$jq("#nestedaccordion3").accordion({autoHeight:false, collapsible: true, change: function(e, ui){ 
			appScope.refreshTemplateBox(); 	
		}});
		
		$jq('#bfBatchButton').click(function(){
			
			    var width   = $jq.trim($jq('#bfBatchWidth').val());
				var height  = $jq.trim($jq('#bfBatchHeight').val());
			    var padding = $jq.trim($jq('#bfBatchPadding').val());
				var margin  = $jq.trim($jq('#bfBatchMargin').val());
				
				$jq('#bfBatchLabels :selected').each(function(i, selected){
				   var label   = $jq('#'+$jq(selected).val());
				   if(width != ''){
				  	label.css('width', width);
				   }
				   if(height != ''){
				  	label.css('height', height);
				   }
				   if(padding != ''){
				  	label.css('padding', padding);
				   }
				   if(margin != ''){
				  	label.css('margin', margin);
				   }
				});
				$jq('#bfBatchElements :selected').each(function(i, selected){
				
					var element = $jq('#'+$jq(selected).val());
				   	    
					if(width != ''){
						element.css('width', width);
					}
					if(height != ''){
						element.css('height', height);
					}
					if(padding != ''){
						element.css('padding', padding);
					}
					if(margin != ''){
						element.css('margin', margin);
					}
				
				   if($jq('#'+$jq(selected).val()).parent().attr('class') == 'ui-wrapper'){
				   	    
				   	   element = $jq('#'+$jq(selected).val()).parent();
				   	    
					   if(width != ''){
					  	element.css('width', width);
					   }
					   if(height != ''){
					  	element.css('height', height);
					   }
					   if(padding != ''){
					  	element.css('padding', padding);
					   }
					   if(margin != ''){
					  	element.css('margin', margin);
					   }
				   }
			});
		});
		
		$jq('#bfUpdatePixelRaster').click(
			function(){
				appScope.pixelRaster = parseInt($jq('#bfPixelRaster').val());
				submitbutton('save');
			}
		);
		
		$jq('#bfGoToPage').change(
			function(){
				if(parseInt($jq(this).val()) > 0){
					document.adminForm.page.value = $jq(this).val();
					submitbutton('save');
				}
			}
		);
		
		$jq('#bfMoveThisPageTo').change(
			function(){
				
				if(parseInt($jq(this).val()) > 0){
					
					if(parseInt($jq(this).val()) == 1){
						$jq('#bfPage'+$jq(this).val()).before( $jq('#bfPage'+document.adminForm.page.value).get(0) );
					} else {
						$jq('#bfPage'+$jq(this).val()).after( $jq('#bfPage'+document.adminForm.page.value).get(0) );
					}
					
					var children = $jq('#bfTemplate').children('.bfPage');
					var clength = children.length;
					for(var i = 0; i < clength;i++){
						$jq(children[i]).get(0).setAttribute('id', 'bfPage' + (i+1));
					}
					
					var idbag = {};
					for(var i = 0; i < $jq('#bfTemplate').children('.bfPage').length;i++){
						var page = $jq('#bfTemplate').children('.bfPage')[i];
						for(var j = 0; j < $jq(page).find('.ff_elem').length;j++){
							idbag[$jq(page).find('.ff_elem')[j].id] = (i+1);
						}
					}
					
					for(var i = 0; i < appScope.areaList.length; i++){
						for(var j = 0; j < appScope.areaList[i].elements.length;j++){
							if( idbag[appScope.areaList[i].elements[j].id] ){
								appScope.areaList[i].elements[j].page = idbag[appScope.areaList[i].elements[j].id];
								break;
							}
						}
					}
					
					document.adminForm.page.value = parseInt($jq(this).val());
					submitbutton('save');
				};
			}
		);
		
		$jq('#bfDeleteThisPage').click(
			function(){
			
				if(!confirm('<?php echo BFText::_('Are you sure you want to delete this page?') ?>')){
					return;
				}
				
				var areaToRemove = null;
				for(var i = 0; i < appScope.areaList.length; i++){
					for(var j = 0; j < appScope.areaList[i].elements.length;j++){
						if(appScope.areaList[i].elements[j].page == document.adminForm.page.value){
							areaToRemove = appScope.areaList[i];
							break;
						}
					}
				}
				
				if(areaToRemove != null){
					var newAreaList = new Array();
					for(var i = 0; i < appScope.areaList.length; i++){
						if(areaToRemove.area.id != appScope.areaList[i].area.id){
							newAreaList.push(appScope.areaList[i]);	
						}
					}
					appScope.areaList = newAreaList;
				}
				
				$jq('#bfPage'+document.adminForm.page.value).remove();
				
				var children = $jq('#bfTemplate').children('.bfPage');
				var clength = children.length;
				for(var i = 0; i < clength;i++){
					$jq(children[i]).get(0).setAttribute('id', 'bfPage' + (i+1));
				}
				
				var idbag = {};
				for(var i = 0; i < $jq('#bfTemplate').children('.bfPage').length;i++){
					var page = $jq('#bfTemplate').children('.bfPage')[i];
					for(var j = 0; j < $jq(page).find('.ff_elem').length;j++){
						idbag[$jq(page).find('.ff_elem')[j].id] = (i+1);
					}
				}
					
				for(var i = 0; i < appScope.areaList.length; i++){
					for(var j = 0; j < appScope.areaList[i].elements.length;j++){
						if( idbag[appScope.areaList[i].elements[j].id] ){
							appScope.areaList[i].elements[j].page = idbag[appScope.areaList[i].elements[j].id];
							break;
						}
					}
				}
					
				document.adminForm.page.value = 1;
				document.adminForm.pages.value = parseInt(document.adminForm.pages.value) - 1;
				submitbutton('save');
			}
		);
		
		$jq('#bfCreatePage').click(
			function(){
				
				if($jq('#bfTemplate').children('.bfPage').length == 0){
				
					var page = document.createElement('span');
					page.setAttribute('class', 'bfPage');
					page.setAttribute('id', 'bfPage1');
					
					if($jq('#bfTemplate').children('*').length == 0){
						var area = document.createElement('ul');
						area.setAttribute('class', 'droppableArea');
						area.setAttribute('id', 'drop1');
						$jq(page).append(area);
						$jq('#bfTemplate').append(page);
						appScope.areaList.push( { area : $jq(area).get(0), elements : [] } );
						document.adminForm.pages.value = 1;
						submitbutton('save');
					} else {
						var children = $jq('#bfTemplate').children('*');
						var tlength = children.length;
						for(var i = 0; i<tlength;i++ ){
							$jq(page).append(children[i]);
						}
						$jq('#bfTemplate').append(page);
						
						document.adminForm.pages.value = 1;
					}
					
				} else {
					//rearranging the page numbers
					var children = $jq('#bfTemplate').children('.bfPage');
					var clength = children.length;
					for(var i = 0; i < clength;i++){
						$jq(children[i]).get(0).setAttribute('id', 'bfPage' + (i+1));
					}
					
					var page = document.createElement('span');
					page.setAttribute('class', 'bfPage');
					page.setAttribute('id', 'bfPage'+(clength+1));
					var area = document.createElement('ul');
					appScope.areaList.push( { area : $jq(area).get(0), elements : [] } );
					area.setAttribute('class', 'droppableArea');
					
					if(!$jq('#'+'drop'+($jq('.droppableArea').length + 1)).get(0)){
						area.setAttribute('id', 'drop'+($jq('.droppableArea').length + 1));
					} else {
						// trying random drop name if the id already exists
						var rndId = $jq.md5("rnd" + Math.random() + (clength+1));
						area.setAttribute('id', 'drop'+rndId);
					}
					
					$jq(page).append(area);
					$jq('#bfTemplate').append(page);
					
					document.adminForm.pages.value = clength + 1;
					submitbutton('save');
				}
			}
		);
		
		$jq('#bfRemoveLabelButton').click(
			function(){
				$jq($jq('#'+appScope.optionElement.id).parent().children('.ff_break')[0]).remove();
				$jq('#'+appScope.optionElement.id).remove();
				appScope.disableElementsDetails();
			}
		);
		
		$jq('#bfInitButton').click(
			function(){
				
				if($jq('#script1flag1').get(0).checked){
					appScope.optionElement.script1flag1 = 1;
				} else {
					appScope.optionElement.script1flag1 = 0;
				}
				
				if($jq('#script1flag2').get(0).checked){
					appScope.optionElement.script1flag2 = 1;
				} else {
					appScope.optionElement.script1flag2 = 0;
				}
				
				appScope.optionElement.script1id = $jq('#bfInitScriptSelection').val();
				appScope.optionElement.script1code = $jq('#script1code').val();
				
				if($jq('#bfInitTypeLibrary').get(0).checked){
					appScope.optionElement.script1cond = 1;
					for(var i = 0; i < appScope.elementScripts.init.length;i++){
						if(appScope.elementScripts.init[i].id == $jq('#bfInitScriptSelection').val()){
							appScope.optionElement.functionNameScript1 = appScope.elementScripts.init[i].name;
							break;
						}
					}
				} else if($jq('#bfInitTypeCustom').get(0).checked){
					appScope.optionElement.script1cond = 2;
					appScope.optionElement.functionNameScript1 = 'ff_' + appScope.optionElement.name + '_init';
				} else {
					appScope.optionElement.script1cond = 0;
				}
				
				$jq('.bfScriptsSaveMessage').css('display','');
				$jq('.bfScriptsSaveMessage').empty().append('<?php echo BFText::_('Script saved! To finally save your form, click "save" on the top right button!') ?><br/><br/>');
			}
		);
		
		$jq('#bfActionButton').click(
			function(){
				
				if($jq('#script2flag1').get(0).checked){
					appScope.optionElement.script2flag1 = 1;
				} else {
					appScope.optionElement.script2flag1 = 0;
				}
				
				if($jq('#script2flag2').get(0).checked){
					appScope.optionElement.script2flag2 = 1;
				} else {
					appScope.optionElement.script2flag2 = 0;
				}
				
				if($jq('#script2flag3').get(0).checked){
					appScope.optionElement.script2flag3 = 1;
				} else {
					appScope.optionElement.script2flag3 = 0;
				}
				
				if($jq('#script2flag4').get(0).checked){
					appScope.optionElement.script2flag4 = 1;
				} else {
					appScope.optionElement.script2flag4 = 0;
				}
				
				if($jq('#script2flag5').get(0).checked){
					appScope.optionElement.script2flag5 = 1;
				} else {
					appScope.optionElement.script2flag5 = 0;
				}
				
				appScope.optionElement.script2id = $jq('#bfActionsScriptSelection').val();
				appScope.optionElement.script2code = $jq('#script2code').val();
				
				if($jq('#bfActionTypeLibrary').get(0).checked){
					appScope.optionElement.script2cond = 1;
					for(var i = 0; i < appScope.elementScripts.action.length;i++){
						if(appScope.elementScripts.action[i].id == $jq('#bfActionsScriptSelection').val()){
							appScope.optionElement.functionNameScript2 = appScope.elementScripts.action[i].name;
							break;
						}
					}
				} else if($jq('#bfActionTypeCustom').get(0).checked){
					appScope.optionElement.script2cond = 2;
					appScope.optionElement.functionNameScript2 = 'ff_' + appScope.optionElement.name + '_action';
				} else {
					appScope.optionElement.script2cond = 0;
				}
				
				$jq('.bfScriptsSaveMessage').css('display','');
				$jq('.bfScriptsSaveMessage').empty().append('<?php echo BFText::_('Script saved! To finally save your form, click "save" on the top right button!') ?><br/><br/>');
			}
		);
		
		$jq('#bfValidationButton').click(
			function(){
				
				appScope.optionElement.script3id = $jq('#bfValidationScriptSelection').val();
				appScope.optionElement.script3code = $jq('#script3code').val();
				appScope.optionElement.script3msg = $jq('#script3msg').val();
				
				if($jq('#bfValidationTypeLibrary').get(0).checked){
					appScope.optionElement.script3cond = 1;
					for(var i = 0; i < appScope.elementScripts.validation.length;i++){
						if(appScope.elementScripts.validation[i].id == $jq('#bfValidationScriptSelection').val()){
							appScope.optionElement.functionNameScript3 = appScope.elementScripts.validation[i].name;
							break;
						}
					}
				} else if($jq('#bfValidationTypeCustom').get(0).checked){
					appScope.optionElement.script3cond = 2;
					appScope.optionElement.functionNameScript3 = 'ff_' + appScope.optionElement.name + '_validation';
				} else {
					appScope.optionElement.script3cond = 0;
				}
				
				$jq('.bfScriptsSaveMessage').css('display','');
				$jq('.bfScriptsSaveMessage').empty().append('<?php echo BFText::_('Script saved! To finally save your form, click "save" on the top right button!') ?><br/><br/>');
			}
		);
		
		$jq('#bfSaveOptionsButton').click(
			function(){
				var error = '';
				
				if(appScope.optionElement.internalType){
				
					switch(appScope.optionElement.internalType){
						case 'bfCalendar':
							appScope.optionElement.options.format       = $jq.trim($jq('#bfCalendarFormat').get(0).value);
							appScope.optionElement.options.connectWith  = $jq.trim($jq('#bfCalendarConnectWith').get(0).value);
							$jq('#'+appScope.optionElement.id).resizable('destroy');
							$jq('#'+appScope.optionElement.id).get(0).innerHTML = $jq.trim($jq('#bfCalendarText').get(0).value);
							appScope.setElementResizable($jq('#'+appScope.optionElement.id).get(0));
							break;
						case 'bfCaptcha':
							$jq('.ff_captcha')[0].style.width  = $jq.trim($jq('#bfCaptchaWidth').get(0).value);
							$jq('.ff_captcha')[0].style.height = $jq.trim($jq('#bfCaptchaHeight').get(0).value);
							break;
						case 'bfStaticText':
							if($jq.trim($jq('#bfStaticTextTitle').get(0).value) == ''){
								error = '<?php echo BFText::_('Title must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfStaticTextContent').get(0).value) == ''){
								error = 'Content must not be empty!';
							}
							if(error == ''){
								appScope.optionElement.title = $jq.trim($jq('#bfStaticTextTitle').get(0).value);
								$jq('#'+appScope.optionElement.id).resizable('destroy');
								$jq('#'+appScope.optionElement.id).get(0).innerHTML = $jq.trim($jq('#bfStaticTextContent').get(0).value);
								appScope.setElementResizable($jq('#'+appScope.optionElement.id).get(0));
								$jq('#'+appScope.optionElement.id).get(0).style.width  = $jq.trim($jq('#bfStaticTextWidth').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.height = $jq.trim($jq('#bfStaticTextHeight').get(0).value);
							} 
						break;
						case 'bfIcon':
							if($jq.trim($jq('#bfIconCaption').get(0).value) == ''){
								error = '<?php echo BFText::_('Caption must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfIconImage').get(0).value) == ''){
								error = '<?php echo BFText::_('Icon Image must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfIconImageOver').get(0).value) == ''){
								error = '<?php echo BFText::_('Icon Image Over must not be empty!') ?>';
							}
							if(error == ''){
								appScope.optionElement.title = $jq.trim($jq('#bfIconCaption').get(0).value);
								$jq('#'+appScope.optionElement.id).children('.ff_iconCaption')[0].innerHTML = $jq.trim($jq('#bfIconCaption').get(0).value);
								
								$jq('#'+appScope.optionElement.id).get(0).style.width  = $jq.trim($jq('#bfIconWidth').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.height = $jq.trim($jq('#bfIconHeight').get(0).value);
								$jq('#'+appScope.optionElement.id).children('.ff_icon')[0].setAttribute('src', $jq.trim($jq('#bfIconImage').get(0).value));
							} 
						break;
						case 'bfTextfield':
							if($jq.trim($jq('#bfTextfieldTitle').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Title must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfTextfieldName').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Name must not be empty!') ?>';
							}
							if(error == ''){
								appScope.optionElement.title = $jq.trim($jq('#bfTextfieldTitle').get(0).value);
								appScope.optionElement.name = $jq.trim($jq('#bfTextfieldName').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).setAttribute('value', $jq.trim($jq('#bfTextfieldValue').get(0).value));
								$jq('#'+appScope.optionElement.id).get(0).value = $jq.trim($jq('#bfTextfieldValue').get(0).value);
								appScope.optionElement.options.value = $jq.trim($jq('#bfTextfieldValue').get(0).value);
								
								$jq('#'+appScope.optionElement.id).get(0).style.width  = $jq.trim($jq('#bfTextfieldWidth').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.height = $jq.trim($jq('#bfTextfieldHeight').get(0).value);
								
								if($jq.trim($jq('#bfTextfieldMaxlength').get(0).value) == ''){
									$jq('#'+appScope.optionElement.id).get(0).removeAttribute('maxlength');
								} else {
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('maxlength', $jq.trim($jq('#bfTextfieldMaxlength').get(0).value));
								}
								if($jq('#bfTextfieldDisable').get(0).checked){
									appScope.optionElement.flag2 = 1;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', 'readonly');
									appScope.optionElement.options.readonly = true;
									
								} else {
									appScope.optionElement.flag2 = 0;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', '');
									$jq('#'+appScope.optionElement.id).get(0).removeAttribute('readonly');
									appScope.optionElement.options.readonly = false;
								}
								if($jq('#bfTextfieldPassword').get(0).checked){
									appScope.optionElement.flag1 = 1;
									appScope.optionElement.options.password = true;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('type', 'password');
									
								} else {
									appScope.optionElement.options.password = false;
									appScope.optionElement.flag1 = 0;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('type', 'text');
								}
								if($jq('#bfTextfieldMailback').get(0).checked){
									appScope.optionElement.mailback = 1;
									appScope.optionElement.options.mailback = true;
								} else {
									appScope.optionElement.options.mailback = false;
									appScope.optionElement.mailback = 0;
								}
								if($jq('#bfTextfieldMailbackAsSender').get(0).checked){
									appScope.optionElement.mailbackAsSender = true;
								} else {
									appScope.optionElement.mailbackAsSender = false;
								}
								appScope.optionElement.mailbackfile = $jq('#bfTextfieldMailbackfile').get(0).value;
							}
						break;
						case 'bfTextarea':
							if($jq.trim($jq('#bfTextareaTitle').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Title must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfTextareaName').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Name must not be empty!') ?>';
							}
							if(error == ''){
								appScope.optionElement.title = $jq.trim($jq('#bfTextareaTitle').get(0).value);
								appScope.optionElement.name = $jq.trim($jq('#bfTextareaName').get(0).value);
								appScope.optionElement.options.value = $jq.trim($jq('#bfTextareaValue').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).value = $jq.trim($jq('#bfTextareaValue').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).defaultValue = $jq.trim($jq('#bfTextareaValue').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.width  = $jq.trim($jq('#bfTextareaWidth').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.height = $jq.trim($jq('#bfTextareaHeight').get(0).value);
								if($jq('#bfTextareaDisable').get(0).checked){
									appScope.optionElement.flag2 = 1;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', 'readonly');
									appScope.optionElement.options.readonly = true;
									
								} else {
									appScope.optionElement.flag2 = 0;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', '');
									$jq('#'+appScope.optionElement.id).get(0).removeAttribute('readonly');
									appScope.optionElement.options.readonly = false;
								}
							}
						break;
						case 'bfCheckbox':
							if($jq.trim($jq('#bfCheckboxTitle').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Title must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfCheckboxName').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Name must not be empty!') ?>';
							}
							if(error == ''){
								appScope.optionElement.title = $jq.trim($jq('#bfCheckboxTitle').get(0).value);
								appScope.optionElement.name = $jq.trim($jq('#bfCheckboxName').get(0).value);
								appScope.optionElement.options.checked = $jq('#bfCheckboxChecked').get(0).checked;
								$jq('#'+appScope.optionElement.id).get(0).checked = $jq('#bfCheckboxChecked').get(0).checked;
								if($jq('#bfCheckboxChecked').get(0).checked){
									appScope.optionElement.options.checked = true;
									appScope.optionElement.flag1 = 1;
									$jq('#'+appScope.optionElement.id).attr('checked', true);
								} else {
									appScope.optionElement.flag1 = 0;
									$jq('#'+appScope.optionElement.id).attr('checked', false);
									appScope.optionElement.options.checked = false;
								}
								appScope.optionElement.options.value = $jq.trim($jq('#bfCheckboxValue').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).value = $jq.trim($jq('#bfCheckboxValue').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.width  = $jq.trim($jq('#bfCheckboxWidth').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.height = $jq.trim($jq('#bfCheckboxHeight').get(0).value);
								if($jq('#bfCheckboxDisable').get(0).checked){
									appScope.optionElement.flag2 = 1;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', 'readonly');
									appScope.optionElement.options.readonly = true;
									
								} else {
									appScope.optionElement.flag2 = 0;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', '');
									$jq('#'+appScope.optionElement.id).get(0).removeAttribute('readonly');
									appScope.optionElement.options.readonly = false;
								}
								if($jq('#bfCheckboxMailbackAccept').get(0).checked){
									appScope.optionElement.mailbackAccept = true;
								} else {
									appScope.optionElement.mailbackAccept = false;
								}
								
								appScope.optionElement.mailbackAcceptConnectWith = $jq('#bfCheckboxMailbackAcceptConnectWith').val();
							}
						break;
						case 'bfRadio':
							if($jq.trim($jq('#bfRadioTitle').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Title must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfRadioName').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Name must not be empty!') ?>';
							}
							if(error == ''){
								appScope.optionElement.title = $jq.trim($jq('#bfRadioTitle').get(0).value);
								appScope.optionElement.name = $jq.trim($jq('#bfRadioName').get(0).value);
								appScope.optionElement.options.checked = $jq('#bfRadioChecked').get(0).checked;
								$jq('#'+appScope.optionElement.id).get(0).checked = $jq('#bfRadioChecked').get(0).checked;
								if($jq('#bfRadioChecked').get(0).checked){
									appScope.optionElement.flag1 = 1;
									$jq('#'+appScope.optionElement.id).attr('checked', true);
								} else {
									appScope.optionElement.flag1 = 0;
									$jq('#'+appScope.optionElement.id).attr('checked', false);
								}
								appScope.optionElement.options.value = $jq.trim($jq('#bfRadioValue').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).value = $jq.trim($jq('#bfRadioValue').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.width  = $jq.trim($jq('#bfRadioWidth').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.height = $jq.trim($jq('#bfRadioHeight').get(0).value);
								if($jq('#bfRadioDisable').get(0).checked){
									appScope.optionElement.flag2 = 1;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', 'readonly');
									appScope.optionElement.options.readonly = true;
									
								} else {
									appScope.optionElement.flag2 = 0;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', '');
									$jq('#'+appScope.optionElement.id).get(0).removeAttribute('readonly');
									appScope.optionElement.options.readonly = false;
								}
							}
						break;
						case 'bfSelect':
							if($jq.trim($jq('#bfSelectTitle').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Title must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfSelectName').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Name must not be empty!') ?>';
							}
							if(error == ''){
								appScope.optionElement.title = $jq.trim($jq('#bfSelectTitle').get(0).value);
								appScope.optionElement.name = $jq.trim($jq('#bfSelectName').get(0).value);
								
								if($jq('#bfSelectMultipleYes').get(0).checked){
									appScope.optionElement.flag1 = 1;
									appScope.optionElement.options.multiple = true;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('multiple', 'multiple');
								} else {
									appScope.optionElement.options.multiple = false;
									appScope.optionElement.flag1 = 0;
									$jq('#'+appScope.optionElement.id).get(0).removeAttribute('multiple');
								}
								
								$jq('#'+appScope.optionElement.id).empty();
								
								var raw = $jq.trim($jq('#bfSelectOpts').get(0).value);
								raw = raw.replace(/\r/g,"");
								appScope.optionElement.data2 = raw;
								
								var lines = raw.split("\n");
								
								for(var i = 0; i < lines.length; i++){
									
									var line = lines[i].split(';');
									if(line.length == 3){
										var option = document.createElement('option');
										option.setAttribute('value', $jq.trim(line[2]));
										option.innerHTML = $jq.trim(line[1]);
										if($jq.trim(line[0]) == '1'){
											option.setAttribute('selected', 'selected');
										}
									}
									
									$jq('#'+appScope.optionElement.id).append(option);	
								}
								
								appScope.optionElement.options.options = $jq.trim($jq('#bfSelectOpts').get(0).value);
								
								$jq('#'+appScope.optionElement.id).get(0).style.width  = $jq.trim($jq('#bfSelectWidth').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.height = $jq.trim($jq('#bfSelectHeight').get(0).value);
								
								if($jq('#bfSelectDisable').get(0).checked){
									appScope.optionElement.flag2 = 1;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', 'readonly');
									appScope.optionElement.options.readonly = true;
									
								} else {
									appScope.optionElement.flag2 = 0;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', '');
									$jq('#'+appScope.optionElement.id).get(0).removeAttribute('readonly');
									appScope.optionElement.options.readonly = false;
								}
								if($jq('#bfSelectMailback').get(0).checked){
									appScope.optionElement.mailback = 1;
									appScope.optionElement.options.mailback = true;
								} else {
									appScope.optionElement.options.mailback = false;
									appScope.optionElement.mailback = 0;
								}
							}
						break;
						case 'bfFile':
							if($jq.trim($jq('#bfFileTitle').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Title must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfFileName').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Name must not be empty!') ?>';
							}
							if(error == ''){
								appScope.optionElement.title = $jq.trim($jq('#bfFileTitle').get(0).value);
								appScope.optionElement.name = $jq.trim($jq('#bfFileName').get(0).value);
								if(!appScope.optionElement.options.allowedFileExtensions) appScope.optionElement.options['allowedFileExtensions'] = -1; 
								appScope.optionElement.options.allowedFileExtensions = $jq.trim($jq('#bfFileAllowedFileExtensions').get(0).value);
								appScope.optionElement.options.uploadDirectory = $jq.trim($jq('#bfFileUploadDirectory').get(0).value);
								appScope.optionElement.data1 = $jq.trim($jq('#bfFileUploadDirectory').get(0).value);
								appScope.optionElement.options.timestamp = $jq('#bfFileTimestamp').get(0).checked;
								if($jq('#bfFileTimestamp').get(0).checked){
									appScope.optionElement.flag1 = 1;
									$jq('#'+appScope.optionElement.id).attr('checked', true);
								} else {
									appScope.optionElement.flag1 = 0;
									$jq('#'+appScope.optionElement.id).attr('checked', false);
								}
								if(!appScope.optionElement.options.attachToAdminMail) appScope.optionElement.options['attachToAdminMail'] = false;
								if($jq('#bfFileAttachToAdminMail').get(0).checked){
									appScope.optionElement.options.attachToAdminMail = true;
								} else {
									appScope.optionElement.options.attachToAdminMail = false;
								}
								if(!appScope.optionElement.options.attachToUserMail) appScope.optionElement.options['attachToUserMail'] = false;
								if($jq('#bfFileAttachToUserMail').get(0).checked){
									appScope.optionElement.options.attachToUserMail = true;
								} else {
									appScope.optionElement.options.attachToUserMail = false;
								}
								$jq('#'+appScope.optionElement.id).get(0).style.width  = $jq.trim($jq('#bfFileWidth').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.height = $jq.trim($jq('#bfFileHeight').get(0).value);
								if($jq('#bfFileDisable').get(0).checked){
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', 'readonly');
									appScope.optionElement.options.readonly = true;
									
								} else {
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', '');
									$jq('#'+appScope.optionElement.id).get(0).removeAttribute('readonly');
									appScope.optionElement.options.readonly = false;
								}
							}
						break;
						case 'bfImageButton':
							if($jq.trim($jq('#bfImageButtonTitle').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Title must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfImageButtonName').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Name must not be empty!') ?>';
							}
							if(error == ''){
								appScope.optionElement.title = $jq.trim($jq('#bfImageButtonTitle').get(0).value);
								appScope.optionElement.name = $jq.trim($jq('#bfImageButtonName').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.width  = $jq.trim($jq('#bfImageButtonWidth').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.height = $jq.trim($jq('#bfImageButtonHeight').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).setAttribute('src', $jq.trim($jq('#bfImageButtonImage').get(0).value));
								$jq('#'+appScope.optionElement.id).get(0).setAttribute('value', $jq.trim($jq('#bfImageButtonValue').get(0).value));
								$jq('#'+appScope.optionElement.id).get(0).value = $jq.trim($jq('#bfImageButtonValue').get(0).value);
								appScope.optionElement.options.value = $jq.trim($jq('#bfImageButtonValue').get(0).value);
								if($jq('#bfImageButtonDisable').get(0).checked){
									appScope.optionElement.flag2 = 1;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', 'readonly');
									appScope.optionElement.options.readonly = true;
									
								} else {
									appScope.optionElement.flag2 = 0;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', '');
									$jq('#'+appScope.optionElement.id).get(0).removeAttribute('readonly');
									appScope.optionElement.options.readonly = false;
								}
							}
						break;
						case 'bfSubmitButton':
							if($jq.trim($jq('#bfSubmitButtonTitle').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Title must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfSubmitButtonName').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Name must not be empty!') ?>';
							}
							if(error == ''){
								appScope.optionElement.title = $jq.trim($jq('#bfSubmitButtonTitle').get(0).value);
								appScope.optionElement.name = $jq.trim($jq('#bfSubmitButtonName').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.width  = $jq.trim($jq('#bfSubmitButtonWidth').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.height = $jq.trim($jq('#bfSubmitButtonHeight').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).setAttribute('value', $jq.trim($jq('#bfSubmitButtonValue').get(0).value));
								$jq('#'+appScope.optionElement.id).get(0).value = $jq.trim($jq('#bfSubmitButtonValue').get(0).value);
								appScope.optionElement.options.value = $jq.trim($jq('#bfSubmitButtonValue').get(0).value);
								if($jq('#bfSubmitButtonDisable').get(0).checked){
									appScope.optionElement.flag2 = 1;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', 'readonly');
									appScope.optionElement.options.readonly = true;
									
								} else {
									appScope.optionElement.flag2 = 0;
									$jq('#'+appScope.optionElement.id).get(0).setAttribute('readonly', '');
									$jq('#'+appScope.optionElement.id).get(0).removeAttribute('readonly');
									appScope.optionElement.options.readonly = false;
								}
							}
						break;
						case 'bfTooltip':
							if($jq.trim($jq('#bfTooltipTitle').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Title must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfTooltipName').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Name must not be empty!') ?>';
							}
							if(error == ''){
								appScope.optionElement.title = $jq.trim($jq('#bfTooltipTitle').get(0).value);
								appScope.optionElement.name = $jq.trim($jq('#bfTooltipName').get(0).value);
								if($jq('#bfTooltipTypeInfo').get(0).checked){
									appScope.optionElement.flag1 = 0;
									appScope.optionElement.options.image = '<?php echo JURI::root(); ?>includes/js/ThemeOffice/tooltip.png';
									appScope.optionElement.options.type = 'info';
								} else if($jq('#bfTooltipTypeWarning').get(0).checked){
									appScope.optionElement.flag1 = 1;
									appScope.optionElement.options.image = '<?php echo JURI::root(); ?>includes/js/ThemeOffice/warning.png';
									appScope.optionElement.options.type = 'warning';
								} else if($jq('#bfTooltipTypeCustom').get(0).checked){
									appScope.optionElement.options.image = $jq.trim($jq('#bfTooltipCustomImage').get(0).value);
									appScope.optionElement.options.type = 'custom';
								}
								$jq('#'+appScope.optionElement.id).get(0).setAttribute('src', appScope.optionElement.options.image);
								appScope.optionElement.options.text = $jq.trim($jq('#bfTooltipText').get(0).value);
								appScope.optionElement.data2 = $jq.trim($jq('#bfTooltipText').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.width  = $jq.trim($jq('#bfTooltipWidth').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.height = $jq.trim($jq('#bfTooltipHeight').get(0).value);
								
							}
						break;
						
						case 'bfPayPal':
						
							if($jq.trim($jq('#bfPayPalTitle').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Title must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfPayPalName').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Name must not be empty!') ?>';
							}
						
							if(error == ''){
							
								appScope.optionElement.title = $jq.trim($jq('#bfPayPalTitle').get(0).value);
								appScope.optionElement.name = $jq.trim($jq('#bfPayPalName').get(0).value);
							
								if($jq('#bfPayPalTestaccountYes').get(0).checked){
									appScope.optionElement.options.testaccount = true;
								} else {
									appScope.optionElement.options.testaccount = false;
								}
						
								if($jq('#bfPayPalDownloadableFileYes').get(0).checked){
									appScope.optionElement.options.downloadableFile = true;
								} else {
									appScope.optionElement.options.downloadableFile = false;
								}
								
								appScope.optionElement.options.filepath = $jq.trim($jq('#bfPayPalFilepath').get(0).value);
								appScope.optionElement.options.downloadTries = $jq.trim($jq('#bfPayPalFileDownloadTries').get(0).value);
								appScope.optionElement.options.business = $jq.trim($jq('#bfPayPalBusiness').get(0).value);
								appScope.optionElement.options.token = $jq.trim($jq('#bfPayPalToken').get(0).value);
								appScope.optionElement.options.testBusiness = $jq.trim($jq('#bfPayPalTestBusiness').get(0).value);
								appScope.optionElement.options.testToken = $jq.trim($jq('#bfPayPalTestToken').get(0).value);
								appScope.optionElement.options.itemname = $jq.trim($jq('#bfPayPalItemname').get(0).value);
								appScope.optionElement.options.itemnumber = $jq.trim($jq('#bfPayPalItemnumber').get(0).value);
								appScope.optionElement.options.amount = $jq.trim($jq('#bfPayPalAmount').get(0).value);
								appScope.optionElement.options.tax = $jq.trim($jq('#bfPayPalTax').get(0).value);
								appScope.optionElement.options.thankYouPage = $jq.trim($jq('#bfPayPalThankYouPage').get(0).value);
								appScope.optionElement.options.locale = $jq.trim($jq('#bfPayPalLocale').get(0).value);
								appScope.optionElement.options.currencyCode = $jq.trim($jq('#bfPayPalCurrencyCode').get(0).value);
								$jq('#'+appScope.optionElement.id).attr('src', $jq.trim($jq('#bfPayPalImage').get(0).value));
								$jq('#'+appScope.optionElement.id).get(0).style.width  = $jq.trim($jq('#bfPayPalWidth').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.height = $jq.trim($jq('#bfPayPalHeight').get(0).value);
							}
						
						break;
						
						
						case 'bfSofortueberweisung':
						
							if($jq.trim($jq('#bfSofortueberweisungTitle').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Title must not be empty!') ?>';
							}
							if($jq.trim($jq('#bfSofortueberweisungName').get(0).value) == '')
							{
								error = '<?php echo BFText::_('Name must not be empty!') ?>';
							}
						
							if(error == ''){
							
								appScope.optionElement.title = $jq.trim($jq('#bfSofortueberweisungTitle').get(0).value);
								appScope.optionElement.name = $jq.trim($jq('#bfSofortueberweisungName').get(0).value);
						
								if($jq('#bfSofortueberweisungDownloadableFileYes').get(0).checked){
									appScope.optionElement.options.downloadableFile = true;
								} else {
									appScope.optionElement.options.downloadableFile = false;
								}
								
								if($jq('#bfSofortueberweisungMailbackYes').get(0).checked){
									appScope.optionElement.options.mailback = true;
								} else {
									appScope.optionElement.options.mailback = false;
								}
								
								appScope.optionElement.options.filepath = $jq.trim($jq('#bfSofortueberweisungFilepath').get(0).value);
								appScope.optionElement.options.downloadTries = $jq.trim($jq('#bfSofortueberweisungFileDownloadTries').get(0).value);
								appScope.optionElement.options.user_id = $jq.trim($jq('#bfSofortueberweisungUserId').get(0).value);
								appScope.optionElement.options.project_id = $jq.trim($jq('#bfSofortueberweisungProjectId').get(0).value);
								appScope.optionElement.options.project_password = $jq.trim($jq('#bfSofortueberweisungProjectPassword').get(0).value);
								appScope.optionElement.options.reason_1 = $jq.trim($jq('#bfSofortueberweisungReason1').get(0).value);
								appScope.optionElement.options.reason_2 = $jq.trim($jq('#bfSofortueberweisungReason2').get(0).value);
								appScope.optionElement.options.amount = $jq.trim($jq('#bfSofortueberweisungAmount').get(0).value);
								appScope.optionElement.options.thankYouPage = $jq.trim($jq('#bfSofortueberweisungThankYouPage').get(0).value);
								appScope.optionElement.options.language_id = $jq.trim($jq('#bfSofortueberweisungLanguageId').get(0).value);
								appScope.optionElement.options.currency_id = $jq.trim($jq('#bfSofortueberweisungCurrencyId').get(0).value);
								$jq('#'+appScope.optionElement.id).attr('src', $jq.trim($jq('#bfSofortueberweisungImage').get(0).value));
								$jq('#'+appScope.optionElement.id).get(0).style.width  = $jq.trim($jq('#bfSofortueberweisungWidth').get(0).value);
								$jq('#'+appScope.optionElement.id).get(0).style.height = $jq.trim($jq('#bfSofortueberweisungHeight').get(0).value);
							}
						
						break;
						
					}
					
					if($jq('#'+appScope.optionElement.id).parent().attr('class') == 'ui-wrapper'){
				   	    
				   	   element = $jq('#'+appScope.optionElement.id).parent();
				   	    
					   if($jq('#'+appScope.optionElement.id).get(0).style.width != ''){
					  	element.css('width', $jq('#'+appScope.optionElement.id).get(0).style.width);
					   }
					   if($jq('#'+appScope.optionElement.id).get(0).style.height != ''){
					  	element.css('height', $jq('#'+appScope.optionElement.id).get(0).style.height);
					   }
				   }
					
				} else {
					// must be a label then
					$jq(appScope.optionElement).resizable('destroy');
					$jq(appScope.optionElement).get(0).innerHTML    = $jq.trim($jq('#bfLabelContent').get(0).value);
					appScope.setElementResizable($jq(appScope.optionElement).get(0));
					$jq(appScope.optionElement).get(0).style.width  = $jq.trim($jq('#bfLabelWidth').get(0).value);
					$jq(appScope.optionElement).get(0).style.height = $jq.trim($jq('#bfLabelHeight').get(0).value);
					if($jq('#bfLabelOnTop').get(0).checked){
						$jq(appScope.optionElement).parent().children('.ff_break')[0].style.display = '';
						$jq(appScope.optionElement).parent().children('.ff_label')[0].style.cssFloat = 'none';
					} else {
						$jq(appScope.optionElement).parent().children('.ff_break')[0].style.display = 'none';
						$jq(appScope.optionElement).parent().children('.ff_label')[0].style.cssFloat = 'left';
					}	
				}
				
				
				$jq('#'+appScope.optionElement.id).get(0).style.padding = $jq.trim($jq('#bfOptionsPadding').get(0).value);
				$jq('#'+appScope.optionElement.id).get(0).style.margin = $jq.trim($jq('#bfOptionsMargin').get(0).value);
				appScope.optionElement.orderNumber = $jq.trim($jq('#bfOptionsOrder').get(0).value);
				appScope.optionElement.tabIndex = $jq.trim($jq('#bfOptionsTabIndex').get(0).value);
				
				if(error == ''){
					$jq('#bfOptionsSaveMessage').get(0).innerHTML = '<?php echo BFText::_('Options saved! To finally save your form, click "save" on the top right button!') ?><br/><br/>';
				} else {
					$jq('#bfOptionsSaveMessage').get(0).innerHTML = error + '<br/><br/>';
				}
				
				$jq('#bfOptionsSaveMessage').css('display','');
				$jq('#bfOptionsSaveMessage').css('visibility','visible');
			}
		);

		this.droppableAreaSortableInit = {
					  cursor:"pointer",
				      handle : '.ff_dragBox',
				      tolerance : 'pointer',
				      connectWith : ['#trashcan', '.droppableArea'],
				      start: function(e, ui){
				      		
				      },
				      receive: function(e, ui){
				      	
				      	// removing the moved element from other areas and assigning to new one.
				      	// this keeps the internal areaList intact
				      
				      	var foundElements = new Array();
				      	
				      	if(!appScope.hasArea($jq(this).get(0))){
							appScope.areaList.push( { area : $jq(this).get(0), elements : [] } );	
						}
				      	
						if(appScope.hasArea($jq(this).get(0))){
							
							// try to find the elements to be moved on all areas
							for(var i=0;i < appScope.areaList.length;i++){
								if(appScope.areaList[i].area != $jq(this).get(0)){
									for(var j=0;j < appScope.areaList[i].elements.length;j++){
										for(var k=0;k < $jq(ui.item).children('.ff_div').children('.ff_elem').length;k++){
											if(appScope.areaList[i].elements[j].id == $jq(ui.item).children('.ff_div').children('.ff_elem')[k].id){
												foundElements.push(appScope.areaList[i].elements[j]);
												break;
											}
										}
										for(var k=0;k < $jq(ui.item).children('.ff_div').children('.ui-wrapper').length;k++){
											var wrappedElement = $jq(ui.item).children('.ff_div').children('.ui-wrapper')[k];
											if($jq(wrappedElement).children('.ff_elem').length == 1){
												var id = $jq(wrappedElement).children('.ff_elem')[0].id;
												if(appScope.areaList[i].elements[j].id == id){
													foundElements.push(appScope.areaList[i].elements[j]);
													break;
												}
											}
										}
									}
								}
							}
						
							// try to find them in the trashcan
							for(var i=0;i < appScope.trashcanAreaList.length;i++){
								for(var k=0;k < $jq(ui.item).children('.ff_div').children('.ff_elem').length;k++){
									if(appScope.trashcanAreaList[i].id == $jq(ui.item).children('.ff_div').children('.ff_elem')[k].id){
										foundElements.push(appScope.trashcanAreaList[i]);
										break;
									}
								}
											
								for(var k=0;k < $jq(ui.item).children('.ff_div').children('.ui-wrapper').length;k++){
									var wrappedElement = $jq(ui.item).children('.ff_div').children('.ui-wrapper')[k];
									if($jq(wrappedElement).children('.ff_elem').length == 1){
										var id = $jq(wrappedElement).children('.ff_elem')[0].id;
										if(appScope.areaList[i].elements[j].id == id){
											foundElements.push(appScope.trashcanAreaList[i]);
											break;
										}
									}
								}
							}
						
							if(foundElements.length > 0){
								for(var i=0;i < foundElements.length ;i++){
									// remove the element from all areas and the trashcan...
									appScope.removeElementFromAreaList(foundElements[i]);
									appScope.removeElementFromTrashcanAreaList(foundElements[i]);
									// ...and add it to the newly selected area
									foundElements[i].area = $jq(this).get(0).id;
									var elements = appScope.getElementsArray($jq(this).get(0));
									elements.push(foundElements[i]);
								}
							}	
						}
				      }
				    };
		$jq(".droppableArea").sortable(appScope.droppableAreaSortableInit);

		$jq("#trashcan").sortable(
				{
					  cursor:"pointer",
				      handle : '.ff_dragBox',
				      connectWith : ['.droppableArea'],
				      receive: function(e, ui){
				      
				      		var foundElements = new Array();
				      	
					      	for(var i=0;i < appScope.areaList.length;i++){
								if(appScope.areaList[i].area != $jq(this).get(0)){for(var j=0;j < appScope.areaList[i].elements.length;j++){
										for(var k=0;k < $jq(ui.item).children('.ff_div').children('.ff_elem').length;k++){
											if(appScope.areaList[i].elements[j].id == $jq(ui.item).children('.ff_div').children('.ff_elem')[k].id){
												foundElements.push(appScope.areaList[i].elements[j]);
												break;
											}
										}
										for(var k=0;k < $jq(ui.item).children('.ff_div').children('.ui-wrapper').length;k++){
											var wrappedElement = $jq(ui.item).children('.ff_div').children('.ui-wrapper')[k];
											if($jq(wrappedElement).children('.ff_elem').length == 1){
												var id = $jq(wrappedElement).children('.ff_elem')[0].id;
												if(appScope.areaList[i].elements[j].id == id){
													foundElements.push(appScope.areaList[i].elements[j]);
													break;
												}
											}
										}
									}
								}
							}
							
							if(foundElements.length > 0){
								for(var i=0;i < foundElements.length ;i++){
									appScope.removeElementFromAreaList(foundElements[i]);
									appScope.trashcanAreaList.push(foundElements[i]);
								}
							}
				      	}
				  }
				);
		
		$jq(".draggableElement").draggable(
									{
										revert: 'invalid',
										helper : 'clone',
										opacity: appScope.opacity
																				
									}
								);
		
		this.droppableAreaDroppableInit = {
										accept: '.draggableElement',
										activeClass: 'droppable-active',
										hoverClass: 'droppable-hover',
										drop: function(e, ui) {
										
											var element = null;
											
											if(typeof $jq(this).get(0).id == "undefined" || $jq(this).get(0).id == ""){
												alert('One of your areas has no id, please fix that!');
												return;
											}
											
											if(!appScope.hasArea($jq(this).get(0))){
												appScope.areaList.push( { area : $jq(this).get(0), elements : [] } );	
											}

											var elements = appScope.getElementsArray($jq(this).get(0));
											var rndId = $jq.md5(Math.random() + elements.length + $jq(ui.draggable).attr('id') + $jq(this).get(0).id);
											var internalType = $jq(ui.draggable).attr('id');
											
											element = appScope.createElementByType(internalType, rndId);

											if(element != null){

                                              if($jq(ui.draggable).attr('id') != 'bfHidden'){
											 	
												var wrapper = document.createElement('div');
												wrapper.setAttribute('id', 'ff_div' + rndId);
												wrapper.setAttribute('class', 'ff_div');
												
												if($jq(ui.draggable).attr('id') != 'bfStaticText'){
												
													var label = document.createElement('div');
													label.setAttribute('id', 'ff_label' + rndId);
													label.setAttribute('class', 'ff_label');
													
													label.style.verticalAlign = 'top';
													label.style.width = '50px';
													label.style.height = '10px';
													label.style.cssFloat = 'left';
													label.innerHTML = 'Label...';
													$jq(label).click(appScope.resizableElement);
													
													$jq(wrapper).append(label);
													
													var mybr = document.createElement('div');
													mybr.setAttribute('id', 'ff_break' + rndId);
													mybr.setAttribute('class', 'ff_break');
													$jq(mybr).css('display','none');
													
													$jq(wrapper).append(mybr);
												}
												
												$jq(wrapper).append(element.element);
												
												var listItem = document.createElement('li');
												listItem.setAttribute('id', 'ff_listItem' + rndId);
												listItem.setAttribute('class', 'ff_listItem');
												
												var dragBox = document.createElement('span');
												dragBox.setAttribute('id', 'ff_dragBox' + rndId);
												dragBox.setAttribute('class', 'ff_dragBox');
												dragBox.innerHTML = "&nbsp;";
												
												$jq(listItem).append(dragBox);
												$jq(listItem).append(wrapper);
												
												var appender = document.createElement('div');
												appender.setAttribute('class', 'ff_appender');
												$jq(appender).css('clear','both');
												$jq(listItem).append(appender);
												
												$jq(this).append(listItem);
												
											  } else {
											  
											  	$jq('#bfTemplate').append(element.element);
											  
											  }
												// push the element into the internal representation
											  if(elements != null){
													elements.push(
															{
																id                 : element.element.id,
																dbId               : 0,
																rndId              : rndId,
																name               : rndId, // default name until changed by user
																title              : 'title_' + element.element.id, // default title until changed by user
																type               : element.element.type ? element.element.type : '',
																internalType       : internalType,
																bfType             : element.bfType,
																elementType        : element.elementType,
																area               : $jq(this).get(0).id,
																appElementId       : $jq(ui.draggable).attr('id'),
																appElementOrderId  : elements.length,
																wrapperId          : wrapper ? wrapper.id : '',
																labelId            : label ? label.id : '',
																listItemId         : listItem ? listItem.id : '',
																data1		 	   : element.data1,
																data2              : element.data2,
																data3              : element.data3,
																script1cond        : element.script1cond,
															    script1id          : element.script1id,
															    script1code        : element.script1code,
														   	    script1flag1       : element.script1flag1,
															    script1flag2       : element.script1flag2,
															    script2cond        : element.script2cond,
															    script2id          : element.script2id,
															    script2code        : element.script2code,
															    script2flag1       : element.script2flag1,
															    script2flag2       : element.script2flag2,
															    script2flag3       : element.script2flag3,
															    script2flag4       : element.script2flag4,
															    script2flag5       : element.script2flag5,
															    script3cond        : element.script3cond,
															    script3id          : element.script3id,
															    script3code        : element.script3code,
															    script3msg         : element.script3msg,
															    functionNameScript1: element.functionNameScript1,
															    functionNameScript2: element.functionNameScript2,
															    functionNameScript3: element.functionNameScript3,
															    flag1              : element.flag1,
															    flag2              : element.flag2,
															    mailback           : element.mailback,
															    mailbackfile       : element.mailbackfile,
															    mailbackAsSender   : element.mailbackAsSender,
															    mailbackAccept     : false,
															    mailbackAcceptConnectWith: '',     
															    orderNumber        : -1,
															    tabIndex           : -1,
															    page               : parseInt(document.adminForm.page.value),
																options            : element.options
															}
													);
													
													if(internalType == 'bfCaptcha'){
														appScope.captchaAdded++;
													}
													
													appScope.populateHiddenFieldsOptions();
													if(internalType != 'bfHidden'){
														appScope.setElementResizable(element.element);
													}
													var labelsResult = $jq('#'+element.element.id).parent().parent().children('.ff_label');
													if(labelsResult.length != 0){
														for(var k = 0; k < labelsResult.length;k++){
															appScope.setElementResizable(labelsResult[k]);
														}
													}
													labelsResult = $jq('#'+element.element.id).parent().children('.ff_label');
													if(labelsResult.length != 0){
														for(var k = 0; k < labelsResult.length;k++){
															appScope.setElementResizable(labelsResult[k]);
														}
													}
													appScope.initMouseOvers();
											  }
											}
										}
									};
		$jq(".droppableArea").droppable(appScope.droppableAreaDroppableInit);
} 

$jq(document).ready(function() {
		app = new BF_EasyModeApp();
		app.populateHiddenFieldsOptions();
});
	
function submitbutton(pressbutton)
{
		var form = document.adminForm;
		
		switch (pressbutton) {
			case 'integrate':
				form.task.value = 'list';
				form.act.value = 'integrate';
				submitform(pressbutton);
				break;
			case 'save':
				var prepared = app.prepareForSave();
				form.task.value = 'save';
				form.act.value = 'easymode';
				form.templateCode.value = prepared.templateCode;
				form.areas.value = prepared.areas;
				submitform(pressbutton);
				break;
			case 'editform':
				SqueezeBox.initialize({});               
			         
			    SqueezeBox.loadModal = function(modalUrl,handler,x,y) {
			    		this.initialize();      
			      		var options = $merge(options || {}, Json.evaluate("{handler: \'" + handler + "\', size: {x: " + x +", y: " + y + "}}"));      
						this.setOptions(this.presets, options);
						this.assignOptions();
						this.setContent(handler,modalUrl);
			   	};
			         
			    SqueezeBox.loadModal("index3.php?option=com_facileforms&task=editform&act=editpage&form=<?php echo $formId ?>&pkg=EasyModeForms","iframe",820,400);
			case 'preview':
				SqueezeBox.initialize({});               
			         
			    SqueezeBox.loadModal = function(modalUrl,handler,x,y) {
			    		this.initialize();      
			      		var options = $merge(options || {}, Json.evaluate("{handler: \'" + handler + "\', size: {x: " + x +", y: " + y + "}}"));      
						this.setOptions(this.presets, options);
						this.assignOptions();
						this.setContent(handler,modalUrl);
			   	};
			         
			    SqueezeBox.loadModal("<?php echo JURI::root()?>index2.php?option=com_facileforms&ff_form=<?php echo $formId ?>&ff_page=<?php echo $page ?>","iframe",820,400);
				break; 
			case 'preview_site':
				SqueezeBox.initialize({});               
			         
			    SqueezeBox.loadModal = function(modalUrl,handler,x,y) {
			    		this.initialize();      
			      		var options = $merge(options || {}, Json.evaluate("{handler: \'" + handler + "\', size: {x: " + x +", y: " + y + "}}"));      
						this.setOptions(this.presets, options);
						this.assignOptions();
						this.setContent(handler,modalUrl);
			   	};
			         
			    SqueezeBox.loadModal("<?php echo JURI::root()?>index.php?option=com_facileforms&ff_form=<?php echo $formId ?>&ff_page=<?php echo $page ?>","iframe",820,400);
				break;        
			default:
				break;
		}
		
		
} // submitbutton

function expstring(text)
{
	text = $jq.trim(text);
	var i;
	var o = '';
	for(i = 0; i < text.length; i++) {
		c = text.charAt(i);
		switch(c) {
			case '&' : o += '\\x26'; break;
			case '<' : o += '\\x3C'; break;
			case '>' : o += '\\x3E'; break;
			case '\'': o += '\\x27'; break;
			case '\\': o += '\\x5C'; break;
			case '"' : o += '\\x22'; break;
			case '\n': o += '\\n'; break;
			case '\r': o += '\\r'; break;
			default: o += c;
		} // switch
	} // for
	return o;
} // expstring

function createActionCode(element)
{
			form = document.bfForm;
			name = element.name;
			if (name=='') {
				alert('Please enter the element name first.');
				return;
			} // if
			if (!confirm("<?php echo BFText::_('ELEMENTS_CREAACTION'); ?>\n<?php echo BFText::_('ELEMENTS_EXISTAPP'); ?>")) return;
			code =
				"function ff_"+name+"_action(element, action)\n"+
				"{\n"+
				"    switch (action) {\n";
			if (form.script2flag1)
				if (form.script2flag1.checked)
					code +=
						"        case 'click':\n"+
						"            break;\n";
			if (form.script2flag2)
				if (form.script2flag2.checked)
					code +=
						"        case 'blur':\n"+
						"            break;\n";
			if (form.script2flag3)
				if (form.script2flag3.checked)
					code +=
						"        case 'change':\n"+
						"            break;\n";
			if (form.script2flag4)
				if (form.script2flag4.checked)
					code +=
						"        case 'focus':\n"+
						"            break;\n";
			if (form.script2flag5)
				if (form.script2flag5.checked)
					code +=
						"        case 'select':\n"+
						"            break;\n";
			code +=
				"        default:;\n"+
				"    } // switch\n"+
				"} // ff_"+name+"_action\n";
				
			oldcode = form.script2code.value;
			if (oldcode != '')
				form.script2code.value =
					code+
					"\n// -------------- <?php echo BFText::_('ELEMENTS_OLDBELOW'); ?> --------------\n\n"+
					oldcode;
			else
				form.script2code.value = code;
} // createActionCode

function createInitCode(element)
{
			form = document.bfForm;
			name = element.name;
			if (name=='') {
				alert('Please enter the element name first.');
				return;
			} // if
			if (!confirm("<?php echo BFText::_('ELEMENTS_CREAINIT'); ?>\n<?php echo BFText::_('ELEMENTS_EXISTAPP'); ?>")) return;
			code =
				"function ff_"+name+"_init(element, condition)\n"+
				"{\n"+
				"    switch (condition) {\n";
			if (form.script1flag1.checked)
				code +=
					"        case 'formentry':\n"+
					"            break;\n";
			if (form.script1flag2.checked)
				code +=
					"        case 'pageentry':\n"+
					"            break;\n";
			code +=
				"        default:;\n"+
				"    } // switch\n"+
				"} // ff_"+name+"_init\n";
			oldcode = form.script1code.value;
			if (oldcode != '')
				form.script1code.value =
					code+
					"\n// -------------- <?php echo BFText::_('ELEMENTS_OLDBELOW'); ?> --------------\n\n"+
					oldcode;
			else
				form.script1code.value = code;
} // createInitCode
		
function createValidationCode(element)
{
			form = document.bfForm;
			name = element.name;
			if (name=='') {
				alert('Please enter the element name first.');
				return;
			} // if
			if (!confirm("<?php echo BFText::_('ELEMENTS_CREAVALID'); ?>\n<?php echo BFText::_('ELEMENTS_EXISTAPP'); ?>")) return;
			code =
				"function ff_"+name+"_validation(element, message)\n"+
				"{\n"+
				"    if (element_fails_my_test) {\n"+
				"        if (message=='') message = element.name+\" faild in my test.\\n\"\n"+
				"        ff_validationFocus(element.name);\n"+
				"        return message;\n"+
				"    } // if\n"+
				"    return '';\n"+
				"} // ff_"+name+"_validation\n";
			oldcode = form.script3code.value;
			if (oldcode != '')
				form.script3code.value =
					code+
					"\n// -------------- <?php echo BFText::_('ELEMENTS_OLDBELOW'); ?> --------------\n\n"+
					oldcode;
			else
				form.script3code.value = code;
} // createValidationCode

function stripHTML(string) { 
    return string.replace(/<(.|\n)*?>/g, ''); 
}
</script>