﻿function initPage() {
	buildInterface();
	creditProfile.init();
}
function buildInterface() {
	//Initialize creditProfile form object
	this.creditProfile = {
		form :  {},
		bubble : {},
		init : function() {
			creditProfile.form = $j('#Content form');
			creditProfile.loadComponents();
		},
		loadComponents : function(e) {
			$j.ajax({
					url: appPath +'_Client/Scripts/Widgets/CreditProfileForm.htm',
					cache: false,
					success: function(html){
						$j('body').append(html);
						creditProfile.bubble = $j('#ApplicationBubble');
						creditProfile.ready();
					}
				});
		},
		ready : function() {
			switch (creditProfile.step()) {
			case '1':
				creditProfile.validation.step1();
				break;
			case '2': 
				creditProfile.validation.step2();
				break;
			default:
				break;
			}
		},
		step : function() {
			var targetClass = $j('#CreditProfile h1')[0].className;
			var str = targetClass.split(' ');
			str = str[(str.length-1)];
			str = str.split('-');
			return str[1];
		},
		showRequiredBubble : function($field,t) {
			$field.parents().filter('.frow').addClass('frow-error');
			var $fieldSpan = $field.parent();
			var startLeft = $fieldSpan.width();
			var extraText = '';
			if(t !== undefined) { extraText = '&nbsp;' + t; }
			//Explicit bubble positioning for fields by title attribute
			switch($field[0].title) {
				case 'Day':
					startLeft += 125;
				break;
				case 'Month':
					startLeft += 65;
				break;
			}
			
			if (creditProfile.step() == '1')
			  $j('h2', creditProfile.bubble).html('Please check your <em>' + $field.attr('title') + '</em> is correct' + extraText);
			else
			  $j('h2', creditProfile.bubble).html('Please check your selected answer' + extraText);
			creditProfile.bubble.prependTo($fieldSpan);
			creditProfile.bubble.css({ position: 'absolute',top: '-20px',left: (startLeft + 35 + 'px') });
			/* IE9 PNG Fade Fix */
			if(isIE7) { creditProfile.bubble.show(); }
				else { creditProfile.bubble.animate({opacity: 'show'},200); }
			creditProfile.bubble.animate({left: (startLeft + 8)},60)
			.animate({left: (startLeft + 25)},140)
			.animate({left: (startLeft + 8)}, 60)
			.animate({left: (startLeft + 10)}, 100)
			.animate({left: (startLeft + 8)}, 60);
		},
		removeRequiredBubble : function($field) {
			var $fieldSpan = $field.parent();
			$field.parents().filter('.frow').removeClass('frow-error'); 
			creditProfile.bubble.hide();
		},
		errorMessage : function() {
			$j.blockUI.defaults.elementMessageCSS.background = '#ffffff';
						$j.blockUI.defaults.elementMessageCSS.padding = '10px';
						$j.blockUI.defaults.elementMessageCSS.border = '2px #ff9900 solid';
						$j.blockUI.defaults.elementMessageCSS.color = '#3a79b6';
						$j('div.form', creditProfile.form).block('<h2>oops!</h2><p>Please check the highlighted fields before we continue</p>');
						window.setTimeout(function() { $j('div.form', creditProfile.form).unblock(); }, 2000);
		},
		validation : {
				//Application Step 1
				step1 : function() {
					$j('.ncv-required').blur(function() {
						if( ncValidation.required($j(this)[0].value) == false ) {
							creditProfile.showRequiredBubble($j(this));
						}	else { creditProfile.removeRequiredBubble($j(this)); }
					});
					$j('.ncv-email').blur(function() {
						if( ncValidation.email($j(this)[0].value) == false ) {
							creditProfile.showRequiredBubble($j(this));
						}	else { creditProfile.removeRequiredBubble($j(this)); }
					});
					$j('.ncv-number').blur(function() {
						if( ncValidation.numeric($j(this)[0].value) == false ) {
							creditProfile.showRequiredBubble($j(this));
						}	else { creditProfile.removeRequiredBubble($j(this)); }
					});
					$j('.ncv-phone').blur(function() {
						if( ncValidation.phone($j(this)[0].value) == false ) {
							creditProfile.showRequiredBubble($j(this),'<span>(no spaces)</span>');
						}	else { creditProfile.removeRequiredBubble($j(this)); }
					});
					$j('.ncv-mobile').blur(function() {
						if( ncValidation.mobile($j(this)[0].value) == false ) {
							creditProfile.showRequiredBubble($j(this),'<span>(no spaces)</span>');
						}	else { creditProfile.removeRequiredBubble($j(this)); }
					});
					creditProfile.form.submit(function() {
						$j('div', creditProfile.form).removeClass('frow-error');
						$j('.ncv').each(function() {
							if( $j(this).hasClass('ncv-required') ) {
								if(ncValidation.required($j(this)[0].value) == false) { $j(this).parents().filter('.frow').addClass('frow-error'); }
							}
							if( $j(this).hasClass('ncv-email') ) {
								if(ncValidation.email($j(this)[0].value) == false) { $j(this).parents().filter('.frow').addClass('frow-error'); }
							}
							if( $j(this).hasClass('ncv-number') ) {
								if(ncValidation.numeric($j(this)[0].value) == false) { $j(this).parents().filter('.frow').addClass('frow-error'); }
							}
						});
						if($j('div', creditProfile.form).hasClass('frow-error') == false) { return true; };
						creditProfile.errorMessage();
						return false;
					});
				},
				//Application Step 2
				step2 : function() {
					$j('.ncv-required').blur(function() {
						if( ncValidation.required($j(this)[0].value) == false ) {
							creditProfile.showRequiredBubble($j(this));
						}	else { creditProfile.removeRequiredBubble($j(this)); }
					});
				}
		  },
	overlay : {
			id : '#Overlay',
			position : function() {
				var overlayWidth = $j(creditProfile.overlay.id).width();
				var windowWidth = $j(window).width();
				var siteWidth = $j('#ContainerBlock').width();
				var windowOffset = (((windowWidth - siteWidth)/2));
				var siteOffset = ((siteWidth - overlayWidth)/2);
				var leftOffset = siteOffset + windowOffset;
				return leftOffset;
			},
			show : function() {
				$j(creditProfile.overlay.id).css({
					cursor: 'default',
					display: 'block'
				});
				$j.blockUI.defaults.pageMessageCSS.left = creditProfile.overlay.position();
				$j.blockUI.defaults.pageMessageCSS.top = '30%';
				$j.blockUI.defaults.pageMessageCSS.background = 'none';
				$j.blockUI.defaults.pageMessageCSS.width = '586px';
				$j.blockUI.defaults.pageMessageCSS.height = '276px';
				$j.blockUI.defaults.pageMessageCSS.margin = '0';
				$j.blockUI.defaults.pageMessageCSS.border = '0';
				$j.blockUI($j(creditProfile.overlay.id)[0]);
				$j('.action-continue').css('cursor','pointer');
				$j('.action-continue', creditProfile.overlay.id).click(function() {
						$j.unblockUI();
				});
			}
		}
	};
}







