var Core = (function () { 

	var _FadeSpeed = 350;
	var _SlideSpeed = 350;	

	var _o = {
		
		// Initialise function
		'Init': function () {
			
			// setTimeout(function(){  Core.SubUI.CreateBox({'Width': 350, 'Text' : '<h1>Struggling To Find What You Need?</h1>'}) }, 5000);
			$('.cbsubmitbutton').click(function(){ _o.SubmitCallBack(); });		
			$('.submitbutton').click(function(){ _o.SubmitApplication(); });		
			
			$('input#cb_name').focus(function(){ _o.SlideandScroll('.hidecb'); });
			$('input#app_debtlevel').focus(function(){ _o.Scroll('#applyform'); });
			
			// $(document).mouseleave(function(){ _o.ShowBox(); });
			
		},
		
		'SlideDown' : function (id) {
			$(id).slideDown(_SlideSpeed);
		},
		
		'SlideandScroll' : function (id) {
			$(id).slideDown(_SlideSpeed);
			$(document).scrollTo($('#cbform').parent('div'), 800 );	
		},
		
		'Scroll' : function (id) {
			$(document).scrollTo($(id), 800 );	
		},
		
		// Show Defination Box
		'Define' : function (Id) {
									
			$.ajax({
			  url: 'content/ajax/help.php',
			  type: "GET",
			  data: "id=" + Id.title,
			  cache: false,
			  dataType: 'json',
			  success: function(data) {
				  Core.SubUI.CreateBox({'Width': 335, 'Text' : data.title + data.msg });		
			  }
			});
			
			
		},
		
		// Ask me pop up box
		'AskMe' :  function () {
			Core.SubUI.CreateBox({'Width': 150, 'Text' : '<h1>Got A Question?</h1><p><input type="text" class="AskMe" /></p>' });
		},
		
		// Load overlay box
		'ShowBox': function () {		
			Core.UI.SetPopupBox({'Title':'Need Help?', 'Show':true, 'ShowClose': true, 'ShowProgress': false, 'Text': '<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />', 'Width': '750px'});
		},
		
		// Load overlay box
		'Terms': function () {		
			
			$.ajax({
			  url: 'content/ajax/terms.php',
			  cache: false,
			  success: function(data) {
					Core.UI.SetPopupBox({'Title':'Terms &amp; Conditions', 'Show':true, 'ShowClose': true, 'ShowProgress': false, 'Text': data, 'Width': '750px'});
			  }
			});
			
		},
		
		// Show after timeout
		'ShowBoxTimeOut': function () {		
			_o.AJAXDebtTest();
		},
			
		// Get test	
		'AJAXDebtTest' : function () {
					
			$.ajax({
				url: 'index.php',
				cache: false,
				type: 'POST',
				success: function(Data){
					Core.UI.SetPopupBox({'Title':'Can I Help?', 'Show':true, 'ShowClose': true, 'ShowProgress': false, 'Text': Data, 'Width': '750px'});
					_o.ProgressBar(1);
				},
				error: function (Data){
						alert ('Error Fetching Page');
				}
			});
										
		},
		
		// Debt test question
		'GetQuestion' : function (Id) {

			var GetQuestionID = $(Id).attr('rel');
			var GetDivID = $(Id).parents('div').attr('id');	
			var GetIndex = $('#AJAXDebtTest > div').index($('#' + GetQuestionID)) + 1;

			$('#' + GetDivID).slideUp(_SlideSpeed);
			$('#' + GetQuestionID).slideDown(_SlideSpeed);
						
			_o.ProgressBar(GetIndex);
			
		},
		
		// Create progress bar
		'ProgressBar' : function (IndexId) {
			var a = parseInt($('#AJAXDebtTest > div').size());
			var Progress = (IndexId / a) * 100;
			
			if (Progress == 100) {
				$('.Bar').animate({width: Progress + '%', opacity: '0.2'});
				$('#ProgressText').html('Debt Test Complete').animate({opacity: '0.8'});
			} else {
				$('.Bar').animate({width: Progress + '%'});
			}
			
		},
		
		'SubmitCallBack' : function () {
		
			_o.SlideDown('.hidecb');
											
			var x = _o.ValidateApplication(1);
			
			if (x.length == 0){
				
				$.ajax({
					'url': '/system/classes/ajax/callback.php',
					'type': 'GET',
					'cache': false,
					'data': "cb_name="+ $('#cb_name').val() +"&cb_phone="+ $('#cb_phone').val() +"&cb_email="+  $('#cb_email').val() +"&cb_debtlevel=" + $('#cb_debtlevel').val(),
					'dataType': 'json',
					'success': function (Data){
										
						if (Data.Success == '1' && Data.Error != '1') {
							window.location.href = Data.URL;
							} else { 							
							
							var Msg = '<h2>Please correct the following fields:</h2><ul style="margin:15px; 0 0 0;" class="error">';
							for (var i = 0; i < Data.Error.length; i++) {
								Msg += '<li>' +  Data.Error[i] + '</li>';
							}									
							Msg += '</ul>';
							
							Core.SubUI.CreateBox({'Width': 300, 'Text' : Msg });
							Core.SubUI.CloseBoxCustomDelay(15000);		
							
						}
							
					if (typeof CallBack !== 'undefined'){
								// CallBack();
						}
					}, 
					'error': function(a,b,c) {
						alert (a + b + c);
					}
				});
				
			} else {
														
				var Msg = '<h2>Please correct the following fields:</h2><ul style="margin:15px; 0 0 0;" class="error">';
				for (var i = 0; i < x.length; i++) {
					Msg += '<li>' +  x[i] + '</li>';
				}									
				Msg += '</ul>&nbsp;';
				
				Core.SubUI.CreateBox({'Width': 300, 'Text' : Msg });
				Core.SubUI.CloseBoxCustomDelay(15000);				
					
			}	
			
		}, 
		
		'SubmitApplication' : function () {
					
			var x = _o.ValidateApplication(2);
			
			if (x.length == 0){
				
			$.ajax({
					'url': '/system/classes/ajax/enquire.php',
					'type': 'GET',
					'cache': false,
					'data': "firstname="+ $('#app_firstname').val() + "&lastname="+ $('#app_lastname').val() + "&phone="+  $('#app_phone').val() +"&email=" + $('#app_email').val()+"&debtlevel=" + $('#app_debtlevel').val() + "&creditors=" + $('#app_creditors').val() + "&income=" + $('#app_income').val() + "&employment=" + $('#app_employment').val(),
					'dataType': 'json',
					'success': function (Data){		
					
					if (Data.Success == '1' && Data.Error != '1') {
								window.location.href = Data.URL;
							} else { 							
							
							var Msg = '<h2>Please correct the following fields:</h2><ul style="margin:15px; 0 0 0;" class="error">';
							for (var i = 0; i < Data.Error.length; i++) {
								Msg += '<li>' +  Data.Error[i] + '</li>';
							}									
							Msg += '</ul>';
							
							Core.SubUI.CreateBox({'Width': 300, 'Text' : Msg });
							Core.SubUI.CloseBoxCustomDelay(15000);		
							
						}
								
						if (typeof CallBack !== 'undefined'){
									// CallBack();
							}
						}, 
						'error': function(a,b,c) {
							alert (a + b + c);
						}
					});								
							
				} else {
											
				var Msg = '<h2>Please correct the following fields:</h2><ul style="margin:15px; 0 0 0;" class="error">';
				for (var i = 0; i < x.length; i++) {
					Msg += '<li>' +  x[i] + '</li>';
				}									
				Msg += '</ul>&nbsp;';
							
				Core.SubUI.CreateBox({'Width': 300, 'Text' : Msg });
				Core.SubUI.CloseBoxCustomDelay(15000);				
					
			}	
			
		}, 
		
		'ValidateApplication' : function (FormType) {
		
			var Errors = [];
			
			if (FormType == 1) {
				
				if (!(/^[a-zA-Z \-'.]+$/.test($('#cb_name').val()))){ Errors.push('Full Name'); } 
				if (!(/^(0|44)[1278]\d{9}$/.test($('#cb_phone').val().replace(/ /g,'')))){ Errors.push('Phone Number'); }
			 	if (!(/^\d{3,10}$/.test($('#cb_debtlevel').val()))){ Errors.push('Debt Level (&pound;100+)'); }
				// if (!(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/.test($('#cb_email').val()))){ Errors.push('Email Address'); }
						
			} else {
				
				if (!(/^\d{3,10}$/.test($('#app_debtlevel').val()))){ Errors.push('Debt Level (&pound;100+)'); }
				if ($('#app_creditors').val() == '-1'){ Errors.push('Number of Creditors'); }
				if (!(/^\d{3,10}$/.test($('#app_income').val()))){ Errors.push('Income'); }
				if ($('#app_employment').val() == '-1'){ Errors.push('Employment Status'); }
				
				if (!(/^[a-zA-Z \-'.]+$/.test($('#app_firstname').val()))){ Errors.push('First Name'); } 
				if (!(/^[a-zA-Z \-'.]+$/.test($('#app_lastname').val()))){ Errors.push('Last Name'); } 
				if (!(/^(0|44)[1278]\d{9}$/.test($('#app_phone').val().replace(/ /g,'')))){ Errors.push('Phone Number'); }
				// if (!(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/.test($('#app_email').val()))){ Errors.push('Email Address'); }
		
			}
			
			return Errors;
		
		},
		
		'CurrencyFormat' : function (id) {

			var currency = id.value;
			var element = id.name;
			
				var i = parseFloat(currency);
				if(isNaN(i)) { i = 0; }
				var minus = '';
				if(i < 0) { minus = '-'; }
				i = Math.abs(i);
				i = parseInt((i + .005) * 100);
				i = i / 100;
				s = new String(i);
				if(s.indexOf(',') < 0) { s += ''; }
				if(s.indexOf(',') == (s.length - 2)) { s += ''; }
				s = minus + s
				
				$('[name='+element+']').val(s);
				
		}

	} 
		
return _o;	

})();
