
	$(document).ready(function() {
		
		if($('#personalDataFormJS').val() == 1){
			processPersonalData();
		}
		
		if($('#parkingDataFormJS').val() == 1){
			processParkingData();	
		}
		
		if($('#paymentDataFormJS').val() == 1){
			processPaymentData();
		}	
		

		if($('#sumaryDataFormJS').val() == 1){
			processSummaryData();	
		}
		
		if($('#passwordForgottenFormJS').val() == 1){
			processPasswordForgotten();	
		}
				
		//handleGTC();					
		//bankFormActivation(!$('#bank-debit').attr('checked'));
			
		handleErrors();
		

	});
	
	
	
	
	
	function processPersonalData(){
		
		//PERSONALLY		
		
		$('#country').change(function(event){countryChangeHandler();});		
		
		
		
		var url = "?method=jQACompleteProvince&type=5000"; 
		$("#postCode").autocomplete(url, {
				dataType: 'json',
				minChars:3, 
				matchSubset:1, 
				matchContains:1, 
				cacheLength:0, 
				scroll:true,
				selectOnly:1,
				extraParams: {
							'tx_mccustomer_list[postcode]': function() { return $('#postCode').val(); },
							'tx_mccustomer_list[country]': function() { return $("#country option:selected").val(); },
							'tx_mccustomer_list[ajaxAction]': 'autoComp_Postcode'
						},
				parse: function(data) {
					var rows = new Array();
					if(data){
						for(var i=0; i<data.length; i++){
							//rows[i] = { data:data[i], value:data[i].postcode + ',' + data[i].name, result:data[i].postcode + ',' +data[i].city };
							rows[i] = { data:data[i]};
						}
					}

					return rows;
				},
				formatItem: function(row, i, n) {
					return row.postcode + ', ' + row.city;
				}
		});
		
		$('#postCode').result(function(event, data, formatted) {
			setPostcodeCityValues(data.postcode, data.city);
		});		
		
		var url = "?method=jQACompleteProvince&type=5000";
		$("#city").autocomplete(url, {
				dataType: 'json',
				minChars:3, 
				matchSubset:1, 
				matchContains:1, 
				cacheLength:0, 
				scroll:true,
				selectOnly:1,
				extraParams: {
							'tx_mccustomer_list[city]': function() { return $('#city').val(); },
							'tx_mccustomer_list[country]': function() { return $("#country option:selected").val(); },
							'tx_mccustomer_list[ajaxAction]': 'autoComp_Postcode'
						},
				parse: function(data) {
					var rows = new Array();
					for(var i=0; i<data.length; i++){
						//rows[i] = { data:data[i], value:data[i].postcode + ',' + data[i].name, result:data[i].postcode + ',' +data[i].city };
						rows[i] = { data:data[i]};
					}
					return rows;
				},
				formatItem: function(row, i, n) {
					return row.postcode + ', ' + row.city;
				}
		});		
		
		$('#city').result(function(event, data, formatted) {
			setPostcodeCityValues(data.postcode, data.city);
		});		
		
		$('#salutation').change(function(event){ajaxFieldValidation('personally', 'salutation', $("#salutation option:selected").val());});		
		$('#firstName').focusout(function(event){ajaxFieldValidation('personally', 'firstName', $('#firstName').val());});
		$('#lastName').focusout(function(event){ajaxFieldValidation('personally', 'lastName', $('#lastName').val());});
		$('#street').focusout(function(event){ajaxFieldValidation('personally', 'street', $('#street').val());});
		$('#houseNumber').focusout(function(event){ajaxFieldValidation('personally', 'houseNumber', $('#houseNumber').val());});
		$('#postCode').focusout(function(event){ajaxFieldValidation('personally', 'postCode', $('#postCode').val(), $("#country option:selected").val());});
		$('#city').focusout(function(event){ajaxFieldValidation('personally', 'city', $('#city').val());});
		$('#email').focusout(function(event){ajaxFieldValidation('personally', 'email', $('#email').val());});
		$('#userName').focusout(function(event){ajaxFieldValidation('personally', 'userName', $('#userName').val());});
		
		/* No ajaxValidation. Just delete possible errors after submit. */
		$('#bDateDay').change(function(event){clearErrors('bDateDay', 'tx-mccustomer-field-error');});
		$('#bDateMonth').change(function(event){clearErrors('bDateMonth', 'tx-mccustomer-field-error');});
		$('#bDateYear').change(function(event){clearErrors('bDateYear', 'tx-mccustomer-field-error');});
		$('#company').change(function(event){clearErrors('company', 'tx-mccustomer-field-error');});
		
	}
	
	function clearErrors(fieldIdClass, fieldErrorClass){
		$('#field-err-message-' + fieldIdClass).slideUp('slow', function(){$('#field-err-message-' + fieldIdClass).remove();});				
		$('#' + fieldIdClass).removeClass(fieldErrorClass);	
	}
	
	function processParkingData(){		
		//PARKING DATA
		addAjaxEventToLP();
		addEventToMobile();
		createDialogFleetsParkingYes();
		createDialogFleetsParkingNo();
		//init fleets parking message
		initFleetsParkingMessages();
		checkMaxNumberMobilePhone(countMobileSets());
		checkMaxNumberLicencePlate(countLPSets());
		
		$('[id*=country-code-]').each(
				function(){	setMobileDialingCode($("#" + this.id + " option:selected").val(), this.id, true);
							$('#' + this.id).change(									
								function(){
									setMobileDialingCode($("#" + this.id + " option:selected").val(), this.id, false);
									}
							)}
			);		
		
	}
	
	
	function validateBankCode(){
		ajaxFieldValidation('payment', 'bank-code', $('#bank-account-number').val() + '|' + $('#bank-code').val());
		
	}	
	
	function validateBankAccountNumber(){
		ajaxFieldValidation('payment', 'bank-account-number', $('#bank-account-number').val() + '|' + $('#bank-code').val());
	}
	

	function processPaymentData(){

		//Create Message for banknumber check
		createDialogBankNumberCheck();
		
		if($('#error-bank-check-number-hidden').length > 0){
			dialogBankNumberCheck();	
		}
		
		
		$('#bank-code').focusout(function(event){autocompleteBankcode();});
		$('#payment-option-prepaid').click(function(event){paymentResetFields();});
		$('#payment-option-cc').click(function(event){paymentResetFields();});
		$('#bank-debit').click(function(event){paymentButtonNextController();});
		$('#bank-debit').click(function(event){paymentResetFields();});
		
		//PAYMENT BANK
		$('#bank-account-number').focusout(function(event){ validateBankAccountNumber(); });
		//$('#bank-account-number').change(function(event){ validateBankAccountNumber(); });
	
		$('#bank-account-number').click(function(event){bankFormActivation(false);});
		
		$('#bank-code').focusout(function(event){validateBankCode();});
		
		$('#bank-code').click(function(event){bankFormActivation(false);});		
		$('#bank-name').focusout(function(event){ajaxFieldValidation('payment', 'bank-name', $('#bank-name').val());});
		$('#bank-name').click(function(event){bankFormActivation(false);});		
		$('#bank-account-owner-first-name').focusout(function(event){ajaxFieldValidation('payment', 'bank-account-owner-first-name', $('#bank-account-owner-first-name').val());});
		$('#bank-account-owner-first-name').click(function(event){bankFormActivation(false);});		
		$('#bank-account-owner-last-name').focusout(function(event){ajaxFieldValidation('payment', 'bank-account-owner-last-name', $('#bank-account-owner-last-name').val());});
		$('#bank-account-owner-last-name').click(function(event){bankFormActivation(false);});		
		$('#bank-debit-approval').focusout(function(event){ajaxFieldValidation('payment', 'bank-debit-approval', $('#bank-debit-approval').attr('checked'));});
		$('#bank-debit-approval').click(function(event){bankFormActivation(false);});	
		$('#bank-payment-type').change(function(event){setBankPaymentType();});
		
		$('#cc-direct-debit-approval').click(function(event){ccDirectDebitApprovalHandler();});
		$('#cc-aut-debit-approval').click(function(event){ccAutDebitApprovalHandler();});
				
		//PAYMENT CC
		$('#cc-number').focusout(function(event){ajaxFieldValidation('payment', 'cc-number', $('#cc-number').val());});
		$('#cc-number').click(function(event){ccFormActivation(false)});
		$('#cc-provider').click(function(event){ccFormActivation(false)});		
		$('#cc-sec-code').focusout(function(event){ajaxFieldValidation('payment', 'cc-sec-code', $('#cc-sec-code').val());});
		$('#cc-sec-code').click(function(event){ccFormActivation(false)});		
		$('#cc-owner-first-name').focusout(function(event){ajaxFieldValidation('payment', 'cc-owner-first-name', $('#cc-owner-first-name').val());});
		$('#cc-owner-first-name').click(function(event){ccFormActivation(false)});		
		$('#cc-owner-last-name').focusout(function(event){ajaxFieldValidation('payment', 'cc-owner-last-name', $('#cc-owner-last-name').val());});
		$('#cc-owner-last-name').click(function(event){ccFormActivation(false)});		
		$('#cc-first-debit-approval').focusout(function(event){ajaxFieldValidation('payment', 'cc-first-debit-approval', $('#cc-first-debit-approval').attr('checked'));});
		$('#cc-first-debit-approval').click(function(event){ccFormActivation(false)});		
		$('#cc-exp-year').change(function(event){ajaxFieldValidation('payment', 'cc-exp-year', $("#cc-exp-year option:selected").val());});
		$('#cc-exp-month').change(function(event){ajaxFieldValidation('payment', 'cc-exp-month', $("#cc-exp-month option:selected").val());});		
		$('#cc-exp-month').click(function(event){ccFormActivation(false)});
		$('#cc-exp-year').click(function(event){ccFormActivation(false)});		
		$('#cc-provider').change(function(event){ajaxFieldValidation('payment', 'cc-provider', $("#cc-provider option:selected").val());});		
		$('#cc-direct-debit-approval').click(function(event){ccFormActivation(false)});
		$('#cc-aut-debit-approval').click(function(event){ccFormActivation(false)});
						
		//Info text for prepaid payment
		if($('#info-prepaid-wrap').length > 0){
				$('#info-prepaid-wrap').qtip({
				content: $('#info-prepaid').html(),
				show: 'mouseover',
				style: { 
						name: 'green',
						color: 'black'
					},
				position: { 
					target: 'mouse', 
					adjust: { screen: true, scroll: true, mouse: false } 
					},
				show: {
						delay: 200,
						solo: true 
					},
				hide: {
					fixed: true,
					when: 'mouseout'
				}				
			})
		}		
		
		addCCDirctBookingInfoMessage();
		addCCAutoBookingInfoMessage();
		addBankBookingInfoMessages();
		paymentButtonNextController();
		

	}
	
	function processSummaryData(){

		createDialogCheckGTC();

		//Info text for vignette
		if($('#info-vignette-wrap').length > 0){
				$('#info-vignette-wrap').qtip({
				content: $('#info-vignettes').html(),
				show: 'mouseover',
				style: { 
						name: 'green',
						color: 'black'
					},
				position: { 
					target: 'mouse', 
					adjust: { screen: true, scroll: true, mouse: false } 
					},
				show: {
						delay: 200,
						solo: true 
					},
				hide: {
					fixed: true,
					when: 'mouseout'
				}				
			})
		}		
		
	}
	
	function processPasswordForgotten(){
		//PASSWORD FORGOTTEN
		$('#password-forgotten-auth').focusout(function(event){ajaxFieldValidation('sendPassword', 'password-forgotten-auth', $('#password-forgotten-auth').val());});
		$('#row-info-password-forgotten-auth').append($('#password-forgotten-phone-pattern').html());
	}

	function countryChangeHandler(){
		resetFildsOnCountryChange();
	}
	
	function ccDirectDebitApprovalHandler(){
		processDirectBookingAmount();
		$('#cc-aut-debit-approval').attr('checked', '');
		processAutoloadBookingAmount();
		
	}
	
	function ccAutDebitApprovalHandler(){
		$('#cc-direct-debit-approval').attr('checked', '');
		
		//Enable cc autoload amount if type cc autoload is selected only.
		processAutoloadBookingAmount()
		processDirectBookingAmount();
	}
	
	function processDirectBookingAmount(){
		if($('#cc-direct-debit-approval').attr('checked')){
			var amountOptId = -1;
			$('[id*=cc-debit-amount-]').each(
					function(){
						$('#' + this.id).attr('disabled',''); 
						//Pre - Select min. option
						if(amountOptId != -1 ){
							if($('#' + this.id).val() < $('#' + amountOptId).val()){
								amountOptId = $('#' + this.id).val();
							}
						}else{
							amountOptId = this.id;
						}						
					});
			$('#' + amountOptId).attr('checked', true);
			
		}else{
			$('[id*=cc-debit-amount-]').each(function(){$('#' + this.id).attr('disabled','disabled'); });
		}		
	}
	
	function processAutoloadBookingAmount(){
		if($('#cc-aut-debit-approval').attr('checked')){
			$('#cc-autoload-debit-amount').attr('disabled',''); 
			$('#cc-autoload-debit-amount').attr('checked', 'checked');
		}else{
			$('#cc-autoload-debit-amount').attr('disabled','disabled');
			$('#cc-autoload-debit-amount').attr('checked', '');
		}
	}
	
	function initFleetsParkingMessages(){
		$('#message-charged-service-small-wrap').prepend($('#message-charged-service-small-description'));
		$('#message-charged-service-small-description').css('display', 'block');
		$('#message-charged-service-wrap').prepend($('#message-charged-service'));
		$('#message-charged-service').css('display', 'block');
		$('#message-free-service-wrap').prepend($('#message-free-service'));
		$('#message-free-service').css('display', 'block');
		//Add QTIP to fleets parker
		addFleetsInfoMessage();
		addLPPatternInfoMessage();
	}
	
	function modifyRowDoubleFieldIds(fieldId){
		
		if(fieldId == 'street'){		
			fieldId = 'houseNumber';
		}
		
		if(fieldId == 'postCode'){
			fieldId = 'city';
		}
		
		if(fieldId == 'cc-exp-month'){
			fieldId = 'cc-exp-year';
		}
		//Display error message after third field bDateYear for all select fields of date
		if(fieldId == 'bDateDay' || fieldId == 'bDateMonth'){
			fieldId = 'bDateYear';
		}

		return fieldId;
	}


	function ajaxFieldValidation(formId, fieldId, fieldValue, country){

		var fieldErrorClass			= 'tx-mccustomer-field-error';
		var fieldClass				= 'tx-mccustomer-form-row';
		var url;
		
		var params = '';
		params = "&tx_mccustomer_list[fieldId]=" + fieldId;
		params = params + "&tx_mccustomer_list[fieldValue]=" + fieldValue;
		params = params + "&tx_mccustomer_list[formId]=" + formId; 		
		params = params + "&tx_mccustomer_list[country]=" + country;
		
		
		var url = "?type=5000&tx_mccustomer_list[ajaxAction]=formFieldValidation" + params;
		$.getJSON(url, function(data) {
		
			fieldIdClass 	= data.fieldId;
			fieldId 		= modifyRowDoubleFieldIds(data.fieldId);	
					
			if(data && data.message.length > 0){//Validation with error

				fieldIdClass = data.fieldId;
				message = data.message;
				
				//BEGIN Handle special case. If bankSumNumberCheck is failed then remove other error and display this one.
				if(fieldId == 'bank-default-error'){
					fieldId = 'default-error';					
					fieldIdClass = 'default-error';
					
					$('#field-err-message-bank-code').slideUp('slow', function(){$('#field-err-message-bank-code').remove();});				
					$('#bank-code').removeClass(fieldErrorClass);
					
					$('#field-err-message-bank-account-number').slideUp('slow', function(){$('#field-err-message-bank-account-number').remove();});				
					$('#bank-account-number').removeClass(fieldErrorClass);
				}
				//END Handle special case. If bankSumNumberCheck is failed then remove other error and display this one.
				
				
				if($('#field-err-message-' + fieldIdClass).length > 0){//In case if error was not removed

					var divEl = $('#field-err-message-' + fieldIdClass);
					divEl.html(message);
					divEl.slideDown('slow');
					
					
				}else{//Create new error message

					var divEl 	= $('<div></div>');
					divEl.addClass('tx-mccustomer-field-error-message');
					divEl.attr('id', 'field-err-message-' + fieldIdClass);
					divEl.html(message);
					divEl.hide();
					
					$('#' + fieldId).after(divEl);
					divEl.slideDown('slow');

					if(fieldId != 'default-error'){
						$('#' + fieldIdClass).addClass(fieldErrorClass);	
					}
				}

			}else{//Validation was successful
								
				//Remove field errors
				$('#field-err-message-' + fieldIdClass).slideUp('slow', function(){$('#field-err-message-' + fieldIdClass).remove();});				
				$('#' + fieldIdClass).removeClass(fieldErrorClass);
				
				//Remove default errors
				$('#field-err-message-default-error').slideUp('slow', function(){$('#field-err-message-default-error').remove();});					
			
			}			
		});
	}
	
		
	function handleGTC(){
		summaryPostButton();		
	}
	
	function summaryPostButton(){
		
		var gtcIsChecked = $('#gtc-approval').attr('checked');
		if(!gtcIsChecked){
			dialogCheckGTC();
		}else{
			$('#summaryForm').submit();	
		}
		
	}


	var fieldErrorMessageClass 	= 'tx-mccustomer-field-error-message';
	var systemErrorMessageClass	= 'tx-mccustomer-default-error-message';
	var fieldErrorClass			= 'tx-mccustomer-field-error';
	var errId					= '';
	var errHintId				= '';
	var formField				= '';
	var fieldWrap = '';
	
	//Error ids defined in Tx_McCustomer_Domain_Model_Form_Error_Abstract	
	function handleErrors(){
		
				
		$('#form-errors').children().each(function(){
			
			formField = $('#' + this.id).attr('name');			
			var hiddenIndex = formField.indexOf("hidden");
			if(hiddenIndex == -1){ //Check if hidden don't display as default error (used in dialog)
				errHintId = 'field-err-message-' + formField;
				errId = this.id;
				createErrorMessage(errId, errHintId, formField, fieldWrap);	
			}
			
		});
		
		//PARKING DATA ERROR BEGIN
		//Check if errors exists
		if($('[id*=ERROR_PD_LP_NUMBER_DEFAULT]').length > 0){
			for(i = 0; i<=150; i++){
				if($('#ERROR_PD_LP_NUMBER_DEFAULT-' + i).length > 0){
					errId = 'ERROR_PD_LP_NUMBER_DEFAULT-' + i;
					errHintId =  'field-err-message-input-license-plate-' + i;
					formField = 'input-license-plate-' + i;
					fieldWrap = '';//'field-err-message-input-license-plate-' + i;
					createErrorMessage(errId, errHintId, formField, fieldWrap);
				}
			}
		}

		if($('[id*=ERROR_PD_MB_NUMBER_DEFAULT]').length > 0){
			for(n = 0; n<=150; n++){
				if($('#ERROR_PD_MB_NUMBER_DEFAULT-' + n).length > 0){
					errId = 'ERROR_PD_MB_NUMBER_DEFAULT-' + n;
					errHintId =   'field-err-message-input-mobile-' + n;
					formField = 'input-mobile-' + n;
					fieldWrap = '';//'error-mobile-number-message-' + n;
					createErrorMessage(errId, errHintId, formField, fieldWrap);
				}
			}
		}		
		//PARKING DATA ERROR END		
			
		
	}
	
	function createErrorMessage(errId, errHintId, formField, fieldWrap){		
	
		var errMessage = $('#' + errId).html();
		var errObj = $('<div></div>');
		errObj.attr('id', errHintId);

		errObj.hide();
		errObj.html(errMessage);
		
		fieldIdClass 	= formField;
		formField 		= modifyRowDoubleFieldIds(formField);
				
		if(fieldWrap.length > 0){
			$('#' + fieldWrap).append(errObj);
		}else
		{
			//IF ERROR FIELD EXISTS THEN APPEND AFTER IT, ELSE APPEND TO DEFAULT ERROR
			if($('#' + formField).length > 0 && formField != 'default-error'){
				if(formField == 'bDateYear'){
					handleSpecialErrorDate(formField, errObj); //If several interrelated errors should be displayed once.
				}else{
					$('#' + formField).after(errObj);
				}
			}else
			{
				$('#default-error').append(errObj);
				errId = 'ERROR_DEFAULT';
			}
		}
				
		if(errId != 'SYSTEM_ERROR' && errId != 'SESSION_ERROR' && errId != 'ERROR_DEFAULT'){
			errObj.attr('class', fieldErrorMessageClass);
			$('#' + fieldIdClass).addClass(fieldErrorClass);
		}
		
		
		errObj.slideDown('slow');		
	}
	
	function handleSpecialErrorDate(formField, errObj){
		if($('#field-err-message-bDateDay').length == 0 && $('#field-err-message-bDateMonth').length == 0 && $('#field-err-message-bDateYear').length == 0){
			$('#' + formField).after(errObj);
		}	
	}
	
	function bankFormActivation(reset){		
		$('#bank-debit').attr('checked', true);
		resetCCForm();		
	}
	
	function setBankPaymentType(){
		$('#bank-debit').attr('checked', true);
		$('#bank-debit').val($("#bank-payment-type option:selected").val());
		addBankBookingInfoMessages();
	}
	
	function ccFormActivation(reset){
		$('#payment-option-cc').attr('checked', true);
		resetBankForm();
	}

	function resetBankForm(){
		$('#bank-account-number').val('');
		$('#bank-code').val('');
		$('#bank-name').val('');
		//$('#bank-account-owner-first-name').val('');
		//$('#bank-account-owner-last-name').val('');
		$('#bank-debit-approval').attr('checked', false);
		
		$('input[id*=bank-]').removeClass('tx-mccustomer-field-error');
		$('div[id*=bank-]').remove();
	}
	
	function resetCCForm(){
		$('#cc-number').val('');
		$('#cc-sec-code').val('');
		$('#cc-provider').val(" ");
		$('#cc-direct-debit-approval').attr('checked', false);
		$('input[id*=cc-debit-amount-]').attr('disabled', 'disabled').attr('checked', false);
		$('#cc-aut-debit-approval').attr('checked', false);
		$('#cc-exp-month').val(" ");
		$('#cc-exp-year').val(" ");		
		$('#cc-autoload-debit-amount').attr('disabled', 'disabled').attr('checked', false);
		$('input[id*=cc-]').removeClass('tx-mccustomer-field-error');
		$('div[id*=cc-]').remove();
	}
	
	function paymentResetFields(){
				
		if($('#payment-option-cc').attr('checked')){
			resetBankForm();
		}
		if($('#bank-debit').attr('checked')){
			resetCCForm()
		}
		if($('#payment-option-prepaid').attr('checked')){
			resetBankForm();
			resetCCForm()
		}
		
	}
	
	
	function clearPaymentErrors(){
		
		$('div[id*=field-err-message]').remove();
		$('input[id*=bank-]').removeClass('tx-mccustomer-field-error');
		$('input[id*=cc-]').removeClass('tx-mccustomer-field-error');	
		
	}
	
	function paymentButtonNextController(){
		var bankFormActivated = $('#bank-debit').attr('checked');
		var prepaidActivated = $('#payment-option-prepaid').attr('checked');
		var ccActivated = false;
						
		if($('#payment-option-cc').length > 0){
			ccActivated = true;
		}
				
		//bankFormActivation(true);
		
		if(bankFormActivated || prepaidActivated || ccActivated){			
			$('#button-next-payment').attr('disabled', false);
		}
		
		//AFTER CHANGE OF PAYMENT TYPE, RESET THE FORM ERRORS
		clearPaymentErrors();
		
	}

	
	function resetFildsOnCountryChange(){
		$('#selected-country').val($("#country option:selected").val());
		
		/* Don't clear street and house number
		$('#street').val('');
		$('#street').removeClass('tx-mccustomer-field-error');		
		$('#houseNumber').val('');
		$('#houseNumber').removeClass('tx-mccustomer-field-error');
		*/
		
		$('#postCode').val('');
		$('#postCode').removeClass('tx-mccustomer-field-error');
		$('#city').val('');
		$('#city').removeClass('tx-mccustomer-field-error');
		
		$('#field-err-message-city').remove();
		$('#field-err-message-postCode').remove();
		$('#field-err-message-houseNumber').remove();
		$('#field-err-message-street').remove();
	}
	
	function ajaxResetOnCountryChange(){
		var url = "?type=5000&tx_mccustomer_list[ajaxAction]=resetOnCountryChange";	
		$.ajax({url: url, success:function(data) {
			
			}
		});
	
	}
	
	function setSelectedCountry(){
		$('#selected-country').val($("#country option:selected").val());
	}
	
	function resetCountrySelection(){
		$("#country").val($('#selected-country').val());
		var countries = $('#fleetsParkingCountries').val();
		var selectedCountry = $("#country option:selected").val();
		if(countries && countries.length > 0){
			countries = countries.split(',');
			fleetsParkingDisabled = $.inArray(selectedCountry, countries) >= 0 ? false : true;
		}	
	}
	


	function setPostcodeCityValues(pCode, city){
		removePostalCodeList();
		
		$('#field-err-message-postCode').remove();
		$('#field-err-message-city').remove();
				
		$('#postCode').removeClass('tx-mccustomer-field-error');
		$('#city').removeClass('tx-mccustomer-field-error');
		
		if(pCode.length > 0 && city && city.length > 0){
			$('#postCode').val(pCode);
			$('#city').val(city);
			$('#city').focus();
		}else{
			var index = pCode.indexOf(" ");
			var strpc = pCode.substring(0, index);
			var strcity = pCode.substring(index);
			
			$('#postCode').val(strpc);
			$('#city').val(strcity);
			$('#city').focus();
		}
		
		ajaxFieldValidation('personally', 'city', $('#city').val());
		ajaxFieldValidation('personally', 'postCode', $('#postCode').val(), $("#country option:selected").val());			

	}
	
	function removePostalCodeList(){
		$('#postcode-list').remove();
	}
	
	
	function aCompleteBankname(bankcode, bankname){
		$('#bank-name').val(bankname);
		$('#bank-name').removeClass('tx-mccustomer-field-error');
		$('#field-err-message-bank-name').remove();
	}	
	
	
	function autocompleteBankcode(){
		if($('#bank-code').val().length >= 8 ){		
			aCompleteBankcode();
		}
	}

	
	function aCompleteBankcode(){
		var url = "?type=5000&tx_mccustomer_list[ajaxAction]=autoComp_Bankcode&tx_mccustomer_list[bankcode]=" + $('#bank-code').val();
	
		$.getJSON(url, function(data) {
			if(data.length == 1){				
				aCompleteBankname(data[0].bankcode, data[0].bankname);
			}else{
				var divEl 	= $('<div></div>');
				divEl.addClass('tx-mccustomer-bankname-list');
				divEl.attr('id', 'bank-list');
				var ulEl 	= $('<ul></ul>');
				var liEl	= '';
			
				for(i=0; i < data.length; i++){
					liElm = $('<li><span id="bankname-entry">' + data[i].bankcode + ' ' + data[i].bankname + '</span></li>');
					liElm.click({bankcode:data[i].bankcode, bankname: data[i].bankname}, function (e){aCompleteBankname(e.data.bankcode, e.data.bankname);});
					ulEl.append(liElm);
				}			
			
				divEl.append(ulEl);
				divEl.hide();
				if($('#bank-list').length > 0){
					$('#bank-list').remove();
				}	
				$('#bank-name').after(divEl);
				divEl.slideDown('slow');
			}
		});		

	}		
	
	
	function isFleetsParker(){
		if($('#isFleetsParkingServiceActive').val() == '1'){
			return true;
		}
		return false;
	}
	
	
	function dialogFleetsParkingNo(){
		$("#message-free-service-wrap").dialog('open');
	}
	
	function test(){
		alert($('[name="tx_mccustomer_mccustomerdata[paymentData][paymentTypeId][value]"]').val());
	}
	
	function handleConfirmationButton(){
		var dialogButtons = $( "#message-charged-service-wrap" ).dialog("option", "buttons");
		$.each(dialogButtons, function (buttonIndex, button) {		
			if (button.id === "confirm"){
				if ($('#charged-service-confirmation').attr("checked")) {
					button.disabled = false;
				}else{
					button.disabled = true;
				}
			}			
		})
		$("#message-charged-service-wrap").dialog("option", "buttons", dialogButtons);
	}

	function createDialogFleetsParkingNo(){

		var btns = {};
		var ok = $('#MSG_FLEETS_PARKING_OPTION_NO_ID').children('[id=MSG_BTN_OK_ID]').val();
		btns[ok] = function(){	
								$(this).dialog('close');
							};
			
		$("#message-free-service-wrap").dialog({
              modal: true,
              buttons:btns,
			  autoOpen: false,
			  closeOnEscape: false,
			  resizable: false,
			  width: 500			  
        });		
	
	}	
	
	function dialogBankNumberCheck(){
		 $("#MSG_BANK_NUMBER_CHECK_ID").dialog('open');		
	 
	}
		
	
	function createDialogBankNumberCheck(){
		
		var ok = $('#MSG_BANK_NUMBER_CHECK_ID').children('[id=MSG_BTN_OK_ID]').val();
		var cancel = $('#MSG_BANK_NUMBER_CHECK_ID').children('[id=MSG_BTN_CANCEL_ID]').val();
				
		$("#MSG_BANK_NUMBER_CHECK_ID").dialog({
		          modal: true,
		          buttons: [
								{
									id: "confirm",
									text: ok,
									click: function() {
										$('#bank-checksum-validation').val(0);
										$(this).dialog('close');
										$('#paymentForm').submit();
									}
								},
								{
									id: "cancel",
									text: cancel,
									click: function() { $(this).dialog('close');}
								}	
							],
				  autoOpen: false,
				  closeOnEscape: false,
				  resizable: false,
				  width: 380	  
		    });
	}	
	
	function createDialogCheckGTC(){
		
	   //$('body').append('<div id="message-check-gtc"><div id="message"></div></div>');		
		var ok = 'Ok';
	   //$("#message-check-gtc").children().html('TEXT');
	   $("#MSG_CHECK_GTC_ID").dialog({
              modal: true,
              buttons: [
							{
								id: "confirm",
								text: ok,
								click: function() { 
									$(this).dialog('close');
								}
							}							
						],
			  autoOpen: false,
			  closeOnEscape: false,
			  resizable: false,
			  width: 300	  
        });
	}
	
	function dialogCheckGTC(){		
		$("#MSG_CHECK_GTC_ID").dialog('open');	
	}
	
	function createDialogFleetsParkingYes(){
		
		var btns = {};
		var ok = $('#MSG_FLEETS_PARKING_OPTION_YES_ID').children('[id=MSG_BTN_OK_ID]').val();
		var cancel = $('#MSG_FLEETS_PARKING_OPTION_YES_ID').children('[id=MSG_BTN_CANCEL_ID]').val();				
		btns[ok] = function(){$(this).dialog('close'); $('#isFleetYes').attr('checked', true); $('#isFleetNo').attr('checked', false);};
		btns[cancel] = function() { $(this).dialog('close'); $('#isFleetNo').attr('checked', true);};

	   $("#message-charged-service-wrap").dialog({
              modal: true,
              buttons: [
							{
								id: "confirm",
								text: ok,
								click: function() { 
											if($('#charged-service-confirmation').attr("checked")){
												setFleetsParking();
												$(this).dialog('close');
											}
									}
							},							
							{
								id: "cancel",
								text: cancel,
								click: function() { $(this).dialog('close');}
							}
							

						],
			  autoOpen: false,
			  closeOnEscape: false,
			  resizable: false,
			  width: 500	  
        });
	}

	function dialogFleetsParkingYes(){		
		$("#message-charged-service-wrap").dialog('open');
		$('#charged-service-confirmation').attr("checked", false);
		handleConfirmationButton();		
	}	
	
	function getMaxNumberOfNFP(){
		return $('#maxNumberOfMobileNumberNFP').val();	
	}
		
	function getMaxNumberOfMobilePhone(){
		var maxNumber = getMaxNumberOfNFP();
		if($('#maxNumberOfMobileNumberFP') && isFleetsParker()){
			return $('#maxNumberOfMobileNumberFP').val();
		}
		
		if($('#maxNumberOfMobileNumberNFP')){
			return $('#maxNumberOfMobileNumberNFP').val();
		}
		
		return maxNumber;	
		
	}

	function getMaxNumberOfLicencePlate(){
		var maxNumber = getMaxNumberOfNFP();
		
		if($('#maxNumberOfLicencePlateFP') && isFleetsParker()){
			return $('#maxNumberOfLicencePlateFP').val();
		}
		
		if($('#maxNumberOfLicencePlateNFP')){
			return $('#maxNumberOfLicencePlateNFP').val();
		}		
		return maxNumber;	
	}
	
	function checkMaxNumberLicencePlate(nextNum){
		var maxNum = getMaxNumberOfLicencePlate();
		if(nextNum >= maxNum){
			$('#add-licence-plate').attr('disabled', true);
			$('#add-licence-plate').parent().addClass('tx-mccustomer-form-button-left-disabled');
			$('#add-licence-plate').addClass('tx-mccustomer-form-button-right-disabled');
			return false;
		}else{
			$('#add-licence-plate').attr('disabled', false);
			$('#add-licence-plate').parent().removeClass('tx-mccustomer-form-button-left-disabled');
			$('#add-licence-plate').removeClass('tx-mccustomer-form-button-right-disabled');			
			return true;
		}
	}
	
	function checkMaxNumberMobilePhone(nextNum){
		var maxNum = getMaxNumberOfMobilePhone();
		if(nextNum >= maxNum){
			$('#add-mobile').attr('disabled', true);
			$('#add-mobile').parent().addClass('tx-mccustomer-form-button-left-disabled');
			$('#add-mobile').addClass('tx-mccustomer-form-button-right-disabled');			
			return false;
		}else{
			$('#add-mobile').attr('disabled', false);
			$('#add-mobile').parent().removeClass('tx-mccustomer-form-button-left-disabled');
			$('#add-mobile').removeClass('tx-mccustomer-form-button-right-disabled');				
			return true;
		}
	}
	
	function setFleetsParking(){		
		
		if($('#add-mode').val() === 'lp'){
			addLPSetRow();
		}
		if($('#add-mode').val() === 'mb'){
			addMobileSetRow();
		}
		
		//to replace with setting of a fleets parking elemenz
		return true;
		
		var countries = $('#fleetsParkingCountries').val();
		var selectedCountry = $("#country option:selected").val();
		var fleetsParkingDisabled = false;
		
		if(countries && countries.length > 0){
			countries = countries.split(',');
			fleetsParkingDisabled = $.inArray(selectedCountry, countries) >= 0 ? false : true;
		}		
	}
	
	
	function addMobileSet(){
		var countMS = countMobileSets();
		var countLPs = countLPSets();
		var maxNumOfNFP = getMaxNumberOfNFP();
		
		if((countMS == maxNumOfNFP) && (countLPs <= maxNumOfNFP)){
			$('#add-mode').val('mb');
			dialogFleetsParkingYes();
		}else{
			addMobileSetRow();
		}
		
	}
	
	
	function addLPSet(){	
		var countMS = countMobileSets();
		var countLPs = countLPSets();
		var maxNumOfNFP = getMaxNumberOfNFP();
		if((countLPs == maxNumOfNFP) && (countMS <= maxNumOfNFP)){
			$('#add-mode').val('lp');
			dialogFleetsParkingYes();
		}else{
			addLPSetRow();
		}		
	}	
	
	function removeLPSet(id){
		var countMS = countMobileSets();
		var countLPs = countLPSets();
		var maxNumbOfNFP = getMaxNumberOfNFP();		
		if((((countLPs - 1) == maxNumbOfNFP) && (countMS <= maxNumbOfNFP))){
			dialogFleetsParkingNo();
			$('#' + id).remove();
		}else{
			$('#' + id).remove();	
		}
		resortLPSet();
	}
	
	/* MN-SET BEGIN*/
	function removeMobileSet(id){
		var countMS = countMobileSets();
		var countLPs = countLPSets();
		var maxNumbOfNFP = getMaxNumberOfNFP();	
		if((((countMS - 1) == maxNumbOfNFP) && (countLPs <= maxNumbOfNFP))){
			dialogFleetsParkingNo();
			$('#' + id).remove();
		}else{
			$('#' + id).remove();	
		}
		resortMobileSet();
	
	}
	
	function addMobileSetRow(){

		var count 			= countMobileSets();				
		var nextNum 		= count;
		var maxNumbOfNFP 	= getMaxNumberOfNFP();
		
		if(!checkMaxNumberMobilePhone(nextNum)){
			return false;
		}		
		
		var addMobile = $('#mb-0').clone();
		
		/* remove Ajax error message */
		addMobile.children().children('div[id*=field-err-message-input-mobile]').remove();			
		
		var mobileNumberTagName 			= $('#mobileInputTagName').attr('name').replace('#', nextNum);
		var mobileCodeOptionTagName	 		= $('#mobileCodeOptionTagName').attr('name').replace('#', nextNum);
		var mobileCountryCodeOptionTagName 	= $('#mobileCountryCodeOptionTagName').attr('name').replace('#', nextNum);
		var mobileBillingTextTagName  		= $('#mobileBillingTextTagName').attr('name').replace('#', nextNum);
		var mobileOrderTagName 				= $('#mobileOrderTagName').attr('name').replace('#', nextNum);
				
		addMobile.children().children('span[id*=mb-label]').html(nextNum + 1);		
		addMobile.children().children('input[id*=input-mobile-]').val('');		
		addMobile.children().children('input[id*=input-mobile-]').attr('id', 'input-mobile-' + nextNum);	
		addMobile.children().children('input[id*=input-mobile-]').attr('name', mobileNumberTagName);			
		addMobile.children().children('input[id*=input-mobile-]').removeClass('tx-mccustomer-field-error');
		
		addMobile.children().children('[id*=option-mobile-code-]').attr('id', 'option-mobile-code-' + nextNum);		
		addMobile.children().children('[id*=option-mobile-code-]').attr('name', mobileCodeOptionTagName);	
		
		//addMobile.children().children('[id*=country-mobile-code-]').attr('id', 'country-mobile-code-' + nextNum);		
		//addMobile.children().children('[id*=country-mobile-code-]').attr('name', mobileCountryCodeOptionTagName);
		
		addMobile.children().children('[id*=billing-text-mobile-]').attr('id', 'billing-text-mobile-' + nextNum);
		addMobile.children().children('[id*=billing-text-mobile-]').attr('name', mobileBillingTextTagName);
		addMobile.children().children('[id*=billing-text-mobile-]').val('');
	
		
		addMobile.children('[id*=error-mobile-number-message-]').attr('id', 'error-mobile-number-message-' + nextNum);
		addMobile.children('[id*=error-mobile-number-message-]').html('');		
		
		addMobile.children().children('.tx-mccustomer-form-button-left').children('[id*=remove-mobile]').attr('disabled', '');	
		addMobile.children().children('.tx-mccustomer-form-button-left').removeClass('tx-mccustomer-form-button-left-disabled');
		addMobile.children().children('.tx-mccustomer-form-button-left').children('[id*=remove-mobile]').removeClass('tx-mccustomer-form-button-right-disabled');		
		
		//mobile-order
		addMobile.children().children().children('.tx-mccustomer-mobile-order').html(nextNum + 1);
		addMobile.children().children('[id*=order-mobile-]').attr('id', 'order-mobile-' + nextNum);
		addMobile.children().children('[id*=order-mobile-]').attr('name', mobileOrderTagName);				
		addMobile.children().children('[id*=order-mobile-]').val(nextNum + 1);
		
		addMobile.children().children('[id*=country-code-]').attr('id', 'country-code-' + nextNum);
		addMobile.children().children('[id*=country-code-]').attr('name', mobileCountryCodeOptionTagName);
		
		addMobile.attr('id', 'mb-' + nextNum);
		
		
		if(nextNum >= maxNumbOfNFP){
			$(addMobile).addClass('tx-mccustomer-fleets-parker');
		}else{
			$(addMobile).removeClass('tx-mccustomer-fleets-parker');
		}
		
		
		var lastElemId = count - 1 ;
		$('#mb-' + lastElemId).after(addMobile);
		
		addEventToMobile();
		
		
		//Set mobile dialing code dependent on counrty dialing code
		var countryId = addMobile.children().children('[id*=country-code-]').val();
		var rowId = addMobile.children().children('[id*=country-code-]').attr('id');
		setMobileDialingCode(countryId, rowId, false);
		addFleetsInfoMessage();
		
		checkMaxNumberMobilePhone(countMobileSets());
	}

	
	function resortMobileSet(){
		var nextNum = 0;
		var mobileNumberTagName = '';
		var mobileCodeOptionTagName	= '';
		var mobileCountryCodeOptionTagName = '';
		var mobileBillingTextTagName  = '';
		var mobileOrderTagName = '';
		var maxNumbOfNFP = getMaxNumberOfNFP();
		
		$('div[id*=mb-]').each(
			function(){
				mobileNumberTagName = $('#mobileInputTagName').attr('name').replace('#', nextNum);
				mobileCodeOptionTagName = $('#mobileCodeOptionTagName').attr('name').replace('#', nextNum);
				mobileCountryCodeOptionTagName = $('#mobileCountryCodeOptionTagName').attr('name').replace('#', nextNum);
				mobileBillingTextTagName  = $('#mobileBillingTextTagName').attr('name').replace('#', nextNum);
				mobileOrderTagName = $('#mobileOrderTagName').attr('name').replace('#', nextNum);
				
				if(nextNum >= maxNumbOfNFP){
					$(this).addClass('tx-mccustomer-fleets-parker');
				}else{
					$(this).removeClass('tx-mccustomer-fleets-parker');
				}
				
				$('#' + this.id).children().children('span[id*=mb-label]').html(nextNum + 1);
				$('#' + this.id).children().children('input[id*=input-mobile-]').attr('id', 'input-mobile-' + nextNum);
				$('#' + this.id).children().children('input[id*=input-mobile-]').attr('name', mobileNumberTagName);
				$('#' + this.id).children().children('[id*=option-mobile-code-]').attr('id', 'option-mobile-code-' + nextNum);
				$('#' + this.id).children().children('[id*=option-mobile-code-]').attr('name', mobileCodeOptionTagName);	
				//$('#' + this.id).children().children('[id*=country-mobile-code-]').attr('name', mobileCountryCodeOptionTagName);
				//$('#' + this.id).children().children('[id*=country-mobile-code-]').attr('id', 'country-mobile-code-' + nextNum);
				
				
				$('#' + this.id).children().children('[id*=billing-text-mobile-]').attr('id', 'billing-text-mobile-' + nextNum);
				$('#' + this.id).children().children('[id*=billing-text-mobile-]').attr('name', mobileBillingTextTagName);				
			
				
				$('#' + this.id).children().children('div[id*=field-err-message-input-mobile-]').attr('id', 'field-err-message-input-mobile-' + nextNum);
				//$('#' + this.id).children('[id*=error-mobile-number-message-]').attr('id', 'error-mobile-number-message-' + nextNum);				
				
				//mobile-order		
				if((nextNum + 1) == 1){
					$('#' + this.id).children().children().children('.tx-mccustomer-mobile-order').html(nextNum + 1 + ' *');
				}else{
					$('#' + this.id).children().children().children('.tx-mccustomer-mobile-order').html(nextNum + 1);
				}
				$('#' + this.id).children().children('[id*=order-mobile-]').attr('id', 'order-mobile-' + nextNum);
				$('#' + this.id).children().children('[id*=order-mobile-]').attr('name', mobileOrderTagName);
				$('#' + this.id).children().children('[id*=order-mobile-]').val(nextNum + 1);				
				
				$('#' + this.id).children().children('[id*=country-code-]').attr('id', 'country-code-' + nextNum);
				$('#' + this.id).children().children('[id*=country-code-]').attr('name', mobileCountryCodeOptionTagName);
				
				$('#' + this.id).attr('id', 'mb-' + nextNum);
				nextNum ++;
			}
		)
		checkMaxNumberMobilePhone(nextNum - 1);
		addEventToMobile();
	}
	
	function countMobileSets(){
		var count = 0;		
		$('div[id*=mb-]').each(
			function(){
				count ++;
				}
		);
		return count;
	}
	/* MN-SET END*/
		
	
	
	/* LP-SET BEGIN*/
	function resortLPSet(){
		var nextNum = 0;
		var licencePlateTagName = '';
		var licencePlateCountryCodeTagName = '';
		var licensePlateBillingTextTagName = '';
		var licencePlateOrderTagName = '';
		var maxNumbOfNFP = getMaxNumberOfNFP();
				
		$('div[id*=lp-item-]').each(
			function(){			
				licencePlateTagName 			= $('#licencePlateTagName').attr('name').replace('#', nextNum);
				licencePlateCountryCodeTagName 	= $('#licencePlateCountryCodeTagName').attr('name').replace('#', nextNum);
				licensePlateBillingTextTagName 	= $('#licensePlateBillingTextTagName').attr('name').replace('#', nextNum);				
				licencePlateOrderTagName 		= $('#licencePlateOrderTagName').attr('name').replace('#', nextNum);				
			
				if(nextNum >= maxNumbOfNFP){
					$(this).addClass('tx-mccustomer-fleets-parker');
				}else{
					$(this).removeClass('tx-mccustomer-fleets-parker');
				}
			
				$('#' + this.id).children().children('[id*=input-license-plate-]').attr('name', licencePlateTagName);				
				$('#' + this.id).children().children('input[id*=input-license-plate-]').attr('id', 'input-license-plate-' + nextNum);
				$('#' + this.id).children().children('[id*=option-country-license-plate-]').attr('id', 'option-country-license-plate-' + nextNum);				
				$('#' + this.id).children().children('[id*=option-country-license-plate-]').attr('name', licencePlateCountryCodeTagName);				
				$('#' + this.id).children().children('[id*=billing-text-license-plate-]').attr('id', 'billing-text-license-plate-' + nextNum);
				$('#' + this.id).children().children('[id*=billing-text-license-plate-]').attr('name', licensePlateBillingTextTagName);				
				
				//$('#' + this.id).children('[id*=field-err-message-input-license-plate-]').attr('id', 'field-err-message-input-license-plate-' + nextNum);
				
				/* Ajax error message */
				$('#' + this.id).children().children('div[id*=field-err-message-input-license-plate-]').attr('id', 'field-err-message-input-license-plate-' + nextNum);
				
				//order
				if((nextNum + 1) == 1){
					$('#' + this.id).children().children().children('.tx-mccustomer-licence-plate-order').html(nextNum + 1 + ' *');
				}else{
					$('#' + this.id).children().children().children('.tx-mccustomer-licence-plate-order').html(nextNum + 1);
				}
		
				$('#' + this.id).children().children('[id*=order-license-plate-]').attr('id', 'order-license-plate-' + nextNum);
				$('#' + this.id).children().children('[id*=order-license-plate-]').attr('name', licencePlateOrderTagName);
				$('#' + this.id).children().children('[id*=order-license-plate-]').val(nextNum + 1);
				
				$('#' + this.id).attr('id', 'lp-item-' + nextNum);
				nextNum ++;	
			}
		)
		checkMaxNumberLicencePlate(nextNum - 1);
		addAjaxEventToLP();
	}
	


	function addLPSetRow(){
	
		var count = countLPSets();			
		var nextNum = count;
		var maxNumbOfNFP = getMaxNumberOfNFP();
		
		if(!checkMaxNumberLicencePlate(nextNum)){
			return false;
		}
		
		var licencePlateTagName 			= $('#licencePlateTagName').attr('name').replace('#', nextNum);
		var licencePlateCountryCodeTagName 	= $('#licencePlateCountryCodeTagName').attr('name').replace('#', nextNum);
		var licensePlateBillingTextTagName 	= $('#licensePlateBillingTextTagName').attr('name').replace('#', nextNum);
		var licencePlateOrderTagName 		= $('#licencePlateOrderTagName').attr('name').replace('#', nextNum);
				
		var addLp = $('#lp-item-0').clone();
		
		/* remove Ajax error message */
		addLp.children().children('div[id*=field-err-message-input-license-plate-]').remove();	
				
				
		addLp.children().children('span[id*=lp-label]').html(nextNum + 1);		
		
		addLp.children().children('input[id*=input-license-plate-]').val('');
		addLp.children().children('input[id*=input-license-plate-]').attr('name', licencePlateTagName);	
		addLp.children().children('input[id*=input-license-plate-]').attr('id', 'input-license-plate-' + nextNum);
		
		addLp.children().children('[id*=input-license-plate-]').removeClass('tx-mccustomer-field-error');
		
		addLp.children().children('[id*=option-country-license-plate-]').attr('id', 'option-country-license-plate-' + nextNum);
		addLp.children().children('[id*=option-country-license-plate-]').attr('name', licencePlateCountryCodeTagName);
		
		addLp.children().children('[id*=billing-text-license-plate-]').attr('id', 'billing-text-license-plate-' + nextNum);
		addLp.children().children('[id*=billing-text-license-plate-]').attr('name', licensePlateBillingTextTagName);	
		addLp.children().children('[id*=billing-text-license-plate-]').val('');
		
		//addLp.children('[id*=field-err-message-input-license-plate-]').attr('id', 'field-err-message-input-license-plate-' + nextNum);
		//addLp.children('[id*=field-err-message-input-license-plate-]').html('');

		addLp.children().children('.tx-mccustomer-form-button-left').children('[id*=remove-license-plate]').attr('disabled', '');	
		addLp.children().children('.tx-mccustomer-form-button-left').removeClass('tx-mccustomer-form-button-left-disabled');
		addLp.children().children('.tx-mccustomer-form-button-left').children('[id*=remove-license-plate]').removeClass('tx-mccustomer-form-button-right-disabled');

		//order
		addLp.children().children().children('.tx-mccustomer-licence-plate-order').html(nextNum + 1 );
		addLp.children().children('[id*=order-license-plate-]').attr('id', 'order-license-plate-' + nextNum);
		addLp.children().children('[id*=order-license-plate-]').attr('name', licencePlateOrderTagName);		
		addLp.children().children('[id*=order-license-plate-]').val(nextNum + 1);
		
		addLp.attr('id', 'lp-item-' + nextNum);
		
		var lastEmemId = count - 1;
		
		$('#lp-item-' + lastEmemId).after(addLp);

		if(nextNum >= maxNumbOfNFP){
			$(addLp).addClass('tx-mccustomer-fleets-parker');
		}else{
			$(addLp).removeClass('tx-mccustomer-fleets-parker');
		}

		addAjaxEventToLP();
		
		//Deaktivate the add button if count = maxNumber
		count = countLPSets();	
		checkMaxNumberLicencePlate(count);
		addFleetsInfoMessage();
		addLPPatternInfoMessage();
	
	}
	

	
	function validateLPOnSelectBox(index){
		if($('#input-license-plate-' + index).val().length > 0){
			ajaxFieldValidation('parkingData', 'input-license-plate-' + index, $('#input-license-plate-' + index).val(), $("#option-country-license-plate-" + index + " option:selected").val());
		}
	}
	
	function addEventToMobile(){
		for(k = 0; k < 150; k++){
			if($('#input-mobile-' + k).length > 0){
				$('#input-mobile-' + k).unbind('focusout');								
				$('#input-mobile-' + k).focusout({index:k}, function(e){ajaxFieldValidation('parkingData', 'input-mobile-' + e.data.index, getMobileNumberFull(e.data.index), $('#country-code-' + e.data.index + ' option:selected').val());});
				
				$('#country-code-' + k).unbind('change');
				$('#country-code-' + k).change(
												function(){setMobileDialingCode($("#" + this.id + " option:selected").val(), this.id, false)}
											);
			}			
		}
	}
	
	function getMobileNumberFull(k){
		var mobileNumberFull = $('#country-code-' + k + ' option:selected').val() + $("#option-mobile-code-" + k + " option:selected").val() + $('#input-mobile-' + k).val();
		return mobileNumberFull;
	}

	function addFleetsInfoMessage(){
		
		if($('#message-charged-service-small-wrap').length > 0){			
			$(".tx-mccustomer-service-info").qtip({
					content: $('#message-charged-service-small-wrap').html(),
					show: 'mouseover',
					style: { 
							name: 'green',
							color: 'black',
							width: 400
						},
					position: { 
						target: 'mouse', 
						adjust: { screen: true, scroll: true, mouse: false } 
						},
					show: {
							delay: 200,
							solo: true 
						},
					hide: {
						fixed: true,
						when: 'mouseout'
					}				
				})			
		}		
	}
	
	function addLPPatternInfoMessage(){
		
		if($('#info-lp-pattern-wrap').length > 0){			
			$(".tx-mccustomer-lp-pattern-info").qtip({
					content: $('#info-lp-pattern-wrap').html(),
					show: 'mouseover',
					style: { 
							name: 'green',
							color: 'black',
							width: 250
						},
					position: { 
						target: 'mouse', 
						adjust: { screen: true, scroll: true, mouse: false } 
						},
					show: {
							delay: 200,
							solo: true 
						},
					hide: {
						fixed: true,
						when: 'mouseout'
					}				
				})			
		}		
	}	
	 
	function addCCDirctBookingInfoMessage(){
		
		var contentVal = $('#info-cc-direct-booking-wrap').html();
		if(contentVal && contentVal.length > 0){			
			$(".tx-mccustomer-cc-direct-booking-info").qtip({
					content: { 
						text: contentVal,
						prerender: true 
					},
					show: 'mouseover',
					position: { 
						target: 'mouse', 
						adjust: { screen: true, scroll: true, mouse: false } 
						},
					show: {
							delay: 200,
							solo: true 
						},
					hide: {
						fixed: true,
						when: 'mouseout'
					}				
				})			
		}		
	}	
	
	function addCCAutoBookingInfoMessage(){
		var contentVal = $('#info-cc-auto-booking-wrap').html();
		if(contentVal && contentVal.length > 0){			
			$(".tx-mccustomer-cc-auto-booking-info").qtip({
					content: { 
						text: contentVal,
						prerender: true
					},
					show: 'mouseover',
					style: { 
							name: 'green',
							color: 'black',
							width: 300
						},
					position: { 
						target: 'mouse', 
						adjust: { screen: true, scroll: true, mouse: false } 
						},
					show: {
							delay: 200,
							solo: true 
						},
					hide: {
						fixed: true,
						when: 'mouseout'
					}				
				})			
		}		
	}		
	
	function addBankBookingInfoMessages(){

		var api;
		var contentVal;
		
		if(!$('#bank-booking-info').data('qtip')){
			addBankAutoBookingInfoMessage();
		}else {
			if($('#bank-debit').val() == '2'){//Info for bank debit
				contentVal = $('#bankinfo-debit-booking-wrap').html();
				
			}else if($('#bank-debit').val() == '5'){//Info for bank autoload
				contentVal = $('#bankinfo-auto-booking-wrap').html();
			}		
			$('#bank-booking-info').qtip('option', 'content.text', contentVal);
		}
	}
	
	
	
	function addBankAutoBookingInfoMessage(){
				
		var contentVal;
		var id = $('#bank-debit').val();

		if(id == 2){
			contentVal = $('#bankinfo-debit-booking-wrap').html();
		}
		if(id == 5){
			contentVal = $('#bankinfo-auto-booking-wrap').html();
		}
		
		
		if(contentVal){
			$("#bank-booking-info").qtip({
					content: { 
						text: contentVal,
						prerender: true 
						},
					show: 'mouseover',
					show: { 
							delay: 200,
							solo: true
						},
					hide: {
						fixed: true,
						when: 'mouseout'
					}				
				})			
		}		
		
		
		$("#bank-payment-type").bind("change", function() {
		    $("#bank-booking-info").qtip("show");
		}).bind("blur", function() {
		    $("#bank-booking-info").qtip("hide");
		});		
	}	
			
	
	function addAjaxEventToLP(){
		for(k = 0; k < 150; k++){
			if($('#input-license-plate-' + k).length > 0){
				$('#input-license-plate-' + k).unbind('focusout');
				$('#input-license-plate-' + k).focusout({index:k}, function(e){ajaxFieldValidation('parkingData', 'input-license-plate-' + e.data.index, $('#input-license-plate-' + e.data.index).val(), $("#option-country-license-plate-" + e.data.index + " option:selected").val());});
				//$('#option-country-license-plate-' + k).change({index:k}, function(e){ajaxFieldValidation('parkingData', 'input-license-plate-' + e.data.index, $('#input-license-plate-' + e.data.index).val(), $("#option-country-license-plate-" + e.data.index + " option:selected").val());});
				$('#option-country-license-plate-' + k).change({index:k}, function(e){validateLPOnSelectBox(e.data.index);});
			}
		}
	}
	
	function countLPSets(){
		var count = 0;		
		$('div[id*=lp-item-]').each(
			function(){
				count ++;
				}
		);
		return count;
	}
	/* LP-SET END*/
	
	function goBack(docName){

		if($('#' + docName).attr('action').search("\\[action\\]=create") > 0){
			action = $('#' + docName).attr('action').replace('[action]=create', '[action]=back');
		}else{
			action = $('#' + docName).attr('action').replace('%5Baction%5D=create', '%5Baction%5D=back');	
		}
		
		$('#' + docName).attr('action', action);
		//alert(action);
		$('#' + docName).submit();
	
	}
	
	//Submit if JS is enabled only
	function jsSubmit(formid){
		$('#' + formid).submit();
	}

	function setMobileDialingCode(countryId, rowId, init){
	
		var id = rowId.replace("country-code-", '');
		var optionsValues = countryDCOptions[countryId];
		var options = '';
		for(i=0; i < optionsValues.length; i++){
			options = options + '<option value="' + optionsValues[i] + '" >' + optionsValues[i] + '</option>';
		}
		$('#option-mobile-code-' + id).html(options);
		if(init){
			//Because of dynamically handling of select box, need to be initialized after document -> ready() again.
			$('#option-mobile-code-' + id).val($('#mobile-dialing-code-' + id).val());
			//Delete hidden field after initializing
			$('#mobile-dialing-code-' + id).remove();
		}		
	}
	
	function showLoader(id){		
		$('#' + id).css('display', 'none');
		$('#ajax-loader-' + id).css('display', 'block');
	}
