/*
	Name:		Application Form Specific Functions
	File:		/js/modules/applications.js
	Version:	2009-07-17
----------------------------------------------- */

/*	InitialiseApp
	- Called on all App pages
----------------------------------------------- */

var application = new Class({

	initialize: function(){
		
		this.environment;
		this.isValid = false;
		this.InitialiseApp();
		
		
	
	},


	InitialiseApp: function() {

		var myUrl = document.location.href;
		var theURL = new ssl_enforce(myUrl);
						
		if(!theURL.url.match("https")){
			
			//Local File - Ensure POST to STG
			this.environment = "STAGING";
						
		}
		else
		{
			//redirect to https if not already https
			if (myUrl != theURL.url)
			{
			    window.location = theURL.url;
			}
			//Could be either PRD or STG
			this.environment = ( location.href.indexOf("staging") == -1 ) ? "PRODUCTION" : "STAGING" ;

		}
		
		window.application = this;
	
	    //$('appForm').set('action', 'https://wwwa.staging.applyonlinenow.com/FDCapp/FormDataCollectionServlet');
		
		
		//Now add event to submit button
		$('appForm').addEvent('submit', function(){
			
			var isValid = new ValidateLSP();
			
			if (isValid.isValid){
			
			    //new fdcsubmit(window.application.environment, "");
			    $('appForm').submit();
			
			} else {
			
			    return false;
			    
			}
			
			
		
		});
		
		//Add event to postcode box
		$('FDC_INSURANCE_POSTCODE').addEvent('blur', function(){
		
		    if ($('FDC_INSURANCE_POSTCODE').value != "" && isValidPostcode($('FDC_INSURANCE_POSTCODE').value)){
	            var newPostCode = formatPostcode($('FDC_INSURANCE_POSTCODE').value)
	            $('FDC_INSURANCE_POSTCODE').value = newPostCode.toUpperCase();
	            $('FDC_INSURANCE_POSTCODE_LBL').setStyle('display', 'none');
	        
	        }
	        else
	        {
	        
	            $('FDC_INSURANCE_POSTCODE_LBL').setStyle('display', 'block');
	        
	        }
		
		});		
		
		return this.environment;
		
		
		
		
	},
	
	//Function to allow auto tab for fields like Account number etc
	moveMax: function(field, nextFieldID) {
	
	    if ($(field).value.length >= field.maxLength){
	    
	        $(nextFieldID).focus();
	        
	    }
	
	},
	
	//Function to allow fields to select all contained text
	selectAllText: function(field) {
	
	    $(field).select();
	
	}
	
	
	
});
