
//customised addEvent function for having a proper onload function call.
function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};


function runFlash()
{
	if(typeof sIFR == "function"){
		//sIFR.replaceElement("#bannerLanding_tagline p", named({sFlashSrc: "flash/myriad_pro.swf", sColor: "#ffffff", sWmode: "transparent"}));
		//sIFR.replaceElement("#header_sitename", named({sFlashSrc: "flash/gill_sans_f8.swf", sColor: "#003038", sWmode: "transparent"}));
	};
	/*var so = new SWFObject("includes/flash/home_banner.swf", "flash_area", "680", "190", "8", "#ffffff");
	so.addParam("wmode", "transparent");
	if(document.getElementById("flash_b")) { so.write("flash_b"); }

	var so_test = new SWFObject("includes/flash/kite680-190-b.swf", "flash_area", "680", "190", "8", "#ffffff");
	so_test.addParam("wmode", "transparent");
	if(document.getElementById("flash_b2")) { so_test.write("flash_b2"); }
*/
}

function hoverCheck ()
{
	try {
		var sfEls = document.getElementById("mainContent").getElementsByTagName("ul");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() { this.className+=" sfhover"; }
			sfEls[i].onmouseout=function() { this.className=this.className.replace(new RegExp(" sfhover\\b"), ""); }
		}
	}
	catch(e) { }
}

function showHideTab (x)
{
		var getDiv = document.getElementById(x);
		getDiv.style.display = 'block';
		var getAllDivs = getElementsByClassName('', 'div', 'document');
		for(var i = 0; i < test; i++)
		{

			test[i].style.display = 'none';
		}
}

function setSubnavHeight()
{
	try 
	{	// Only run if the wrapper is found & the browser is IE6
		if(document.getElementById("textContentWrapper") && BrowserDetect.browser == "Explorer" && BrowserDetect.version == "6" ) {
			var getContent = document.getElementById("textContentWrapper").offsetHeight;
			document.getElementById("subnavigation_wrapper").style.height= getContent;
		}
	}
	catch(e) { }
}

function showHide()
{
	if(document.getElementById("showHide").style.display == "none") { document.getElementById("showHide").style.display = "block"; document.getElementById("showHidePara").style.display = "none"; }
}


function initPage()
{
	//runFlash();
	BrowserDetect.init();
	setSubnavHeight();
}

addEvent(window, 'load', initPage);
//if (window.attachEvent) window.attachEvent("onload", hoverCheck);
// An IE only onload event (there's no need to use the custom function as the li:hover state works with FF / IE7

$.fn.stickyfloat = function(options, lockBottom) {
	var $obj 				= this;
	var parentPaddingTop 	= parseInt($obj.parent().css('padding-top'));
	var startOffset 		= $obj.parent().offset().top;
	var opts 				= $.extend({ startOffset: startOffset, offsetY: parentPaddingTop, duration: 200, lockBottom:true }, options);
	
	$obj.css({ position: 'absolute' });
	
	if(opts.lockBottom){
		var bottomPos = $obj.parent().height() - $obj.height() + parentPaddingTop; //get the maximum scrollTop value
		if( bottomPos < 0 )
			bottomPos = 0;
	}
	
	$(window).scroll(function () { 
		$obj.stop(); // stop all calculations on scroll event

		var pastStartOffset			= $(document).scrollTop() > opts.startOffset;	// check if the window was scrolled down more than the start offset declared.
		var objFartherThanTopPos	= $obj.offset().top > startOffset;	// check if the object is at it's top position (starting point)
		var objBiggerThanWindow 	= $obj.outerHeight() < $(window).height();	// if the window size is smaller than the Obj size, then do not animate.
		
		// if window scrolled down more than startOffset OR obj position is greater than
		// the top position possible (+ offsetY) AND window size must be bigger than Obj size
		if( (pastStartOffset || objFartherThanTopPos) && objBiggerThanWindow ){ 
			var newpos = ($(document).scrollTop() -startOffset + opts.offsetY );
			if ( newpos > bottomPos )
				newpos = bottomPos;
			if ( $(document).scrollTop() < opts.startOffset ) // if window scrolled < starting offset, then reset Obj position (opts.offsetY);
				newpos = parentPaddingTop;

			$obj.animate({ top: newpos }, opts.duration );
		}
	});
};


jQuery(document).ready(function() 
{

	//var popUpOptions = "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes,width=800, height=450";
	//jQuery(".wizard_download_options input").click(function(){ 
	//	window.open("/test.html", "CNN_WindowName", popUpOptions);
	//});

	if (document.getElementById("form_showHide") && document.getElementById("formVisible")) 
			{ jQuery("#form_showHide").hide();}
	else 	{ jQuery("#form_showHidePara").hide(); }
	 
	jQuery("#form_showHidePara").click(function(){
		jQuery(this).hide();
		jQuery("#form_showHide").show();
	});
	
	jqueryReady();
	
	// "Title" Show & Hide on Step 2
	jQuery(".titleShowHideSelect option:selected").val() == "Other" ? jQuery(".titleShowHideDiv").show() : jQuery(".titleShowHideDiv").hide();
	jQuery(".titleShowHideSelect").change( function(){ jQuery("option:selected", this).val() == "Other" ? jQuery(".titleShowHideDiv").show() : jQuery(".titleShowHideDiv").hide(); });
	
	// "Title" Show & Hide on Step 2 - for when "Mrs." is selected
	jQuery(".titleShowHideSelect option:selected").val() == "Mrs." ? jQuery(".title_note").show() : jQuery(".title_note").hide();
	jQuery(".titleShowHideSelect").change( function(){ jQuery("option:selected", this).val() == "Mrs." ? jQuery(".title_note").show() : jQuery(".title_note").hide(); });

	// "How Did you Hear of Us" Show & Hide on Step 2
	jQuery(".hearOfShowHideSelect option:selected").val() == "Other" ? jQuery(".hearOfShowHideDiv").show() : jQuery(".hearOfShowHideDiv").hide();
	jQuery(".hearOfShowHideSelect").change( function(){ jQuery("option:selected", this).val() == "Other" ? jQuery(".hearOfShowHideDiv").show() : jQuery(".hearOfShowHideDiv").hide(); });

	jQuery(".step1_isExisting input:checked").each(function(){
		jQuery(".isExistingAnswer").show();
	});
	jQuery(".step1_isExisting input").click(function(){ jQuery(".isExistingAnswer").show(); });
	jQuery(".step1_isNew input").click(function(){ jQuery(".isExistingAnswer").hide(); });

	/* ========================= END FAQ FUNCTIONALITY */
	jQuery(".faq_answer").wrapInner("<div class=\"faq_answer_body\" />");
	jQuery(".faq_answer").prepend("<span class=\"faq_answer_head\"></span>");

	jQuery(".faq_question").addClass("faq_isClosed");
	jQuery(".faq_question").click(function() { 
		
		if((jQuery(this).attr("class")).indexOf("faq_isClosed") > 0)
		{
			jQuery(".faq_question").addClass("faq_isClosed");
			jQuery(".faq_answer").slideUp(300);
			
			jQuery(this).removeClass("faq_isClosed");
			jQuery(this).next(".faq_answer").slideDown(300);
		}
		else
		{
			jQuery(".faq_question").addClass("faq_isClosed");
			jQuery(".faq_answer").slideUp(300);
		}
	});
	/* ========================= END FAQ FUNCTIONALITY */
		
	
	
}); // END jQuery(document).ready


function jqueryReady()
{
    var i = 0;
	jQuery(".appform_row_wrapper").each( function(i){
		i % 2 == 0 ? jQuery(this).addClass("zebra_row_0") : jQuery(this).addClass("zebra_row_1");
		i++;
	});
	
	jQuery(".appform_row_wrapper input").focus( function() 
		{ jQuery(this).parents(".appform_row_wrapper").addClass("row_hover") }
	).blur( function()
		{ jQuery(this).parents(".appform_row_wrapper").removeClass("row_hover") }
	);
	jQuery(".appform_row_wrapper select").focus( function() 
		{ jQuery(this).parents(".appform_row_wrapper").addClass("row_hover") }
	).blur( function()
		{ jQuery(this).parents(".appform_row_wrapper").removeClass("row_hover") }
	);
	
	jQuery(".prev_step, .next_step, .wizard_download_options img, .wizard_download_options input").hover( function(){ jQuery(this).fadeTo("fast", 0.5); }, function(){ jQuery(this).fadeTo("fast", 1.0); } );
	
	// show / hide toggle for step 3
	
	jQuery(".showhide_wrapper").css("display", "none");
	jQuery(".showhide .heading").click(
		function() { 
			jQuery(this).parents(".showhide").toggleClass("s_closed");
			
			jQuery(this).next(".showhide_wrapper").slideToggle(300, function(){
			    jQuery(".wizard_help_wrapper").height(jQuery("#wizard_content").height());
			    $('.wizard_help_float').stickyfloat({ duration: 400 });
			}); 
		}
	);
		
    //$(".wizard_help_wrapper").floating();
    jQuery(".wizard_help_wrapper").height(jQuery("#wizard_content").height());
    $('.wizard_help_float').stickyfloat({ duration: 400 });
    
	
	// Apply opacity to AJAX loading
	jQuery(".progress_overlay").width(jQuery("#inner_wrapper").innerWidth());
	jQuery(".progress_overlay").height(jQuery("#inner_wrapper").innerHeight());
	jQuery(".progress_overlay").fadeTo("fast", 0.5);
	jQuery(".progress_overlay").bgiframe( { height: jQuery("#inner_wrapper").innerHeight(), width: jQuery("#inner_wrapper").innerWidth() } );
	
	$('.progress').css('top', $(this).scrollTop() + ($(this).height() / 2) + "px");
	$(window).scroll(function() {
		$('.progress').css('top', $(this).scrollTop() + ($(this).height() / 2) + "px");
	});

	
	/* ---------------------------------------------- Applicant 2 Warning */
	//jQuery(".applicant2_highlight").fadeOut("slow");
	
	/* ---------------------------------------------------------------------------------------------------- ERROR MESSAGE EVENT MONITORING */
	
	// (Onload) Check for any validation warnings that are visible and highlight the associated row.
	jQuery(".warning_popup").each( function() { jQuery(this).css("visibility") != "hidden" ? jQuery(this).parent(".appform_row_wrapper").addClass("error_row") : null; } );
	
	// Check for any visible error messages when the submit button is clicked.
	var errorsVisible = 0;
	var scrollTo = 0;
	jQuery(".next_step").bind("click", function(i)
	{ 
	    jQuery(".warning_popup").each(function(){
			//alert(jQuery(this).css("visibility"));
			if(jQuery(this).css("visibility") != "hidden")
			{
				errorsVisible == 0 ? scrollTo = jQuery(this).offset().top : null; 
				jQuery(this).parents(".appform_row_wrapper").addClass("error_row");
				errorsVisible ++;
			}
		});
		if(errorsVisible > 0 ){ errorsVisible=0; jQuery("html, body").animate( { scrollTop: scrollTo }, "normal");  }
	});
	
	// If the user has changed any values in the input / select fields, check for any visible error messages.

	
	jQuery(".appform_row_wrapper input, .appform_row_wrapper select").change(function() {
		jQuery(this).parents(".appform_row_wrapper").removeClass("error_row");
	});
		
	jQuery(".form_row_other input, .form_row_other2 input").change(function() {
		jQuery(this).next(".warning_popup").toggleClass("hide_warning_popup");
	});

	jQuery(".numonly").change(function(){
		re = /[a-z]/ig;
		var str = jQuery(this).val();
		str.match(re) != null ? jQuery(this).parents(".appform_row_wrapper").addClass("error_row") : null; 
	});


	/* Fix the issue of IE6 showing <select> above the warning pop-up */
	jQuery(".warning_popup").bgiframe({height: 34});
	
	/* ---------------------------------------------------------------------------------------------------- ERROR MESSAGE EVENT MONITORING */

	/* ---------------------------------------------------------------------------------------------------- STEP 2 HELP TEXT TOGGLE - SHOW / HIDE */
	jQuery(".showhide_help").focus(
		function()
		{
			jQuery(".help_show_1").fadeOut("fast", function(){ jQuery(".help_show_2").fadeIn("fast"); } );
		}
	);
	jQuery(".showhide_help").blur(
		function()
		{
			jQuery(".help_show_1").fadeIn("fast", function() { jQuery(".help_show_2").fadeOut("fast");} );
		}
	);
	
	/* ---------------------------------------------------------------------------------------------------- STEP 2 SUPERSCRIPT HOVER FUNCTION */
	jQuery("span.superscript").each(function(i){
		jQuery(this).children(".superscript_note").css({"left" : (jQuery(this).position().left + 15) });
	});
	jQuery("span.superscript").hover(
		function(e) { jQuery(this).children(".superscript_note").fadeIn("fast"); },
		function(e) { jQuery(this).children(".superscript_note").fadeOut("fast"); }
	);
	
	jQuery(".dob_1").attr("autocomplete", "off");
   	jQuery(".dob_1").keyup(function() {
		if (jQuery(this).val().length == 2)
		{
			jQuery(this).blur();
			jQuery(".dob_2").focus();
		}
    });
	jQuery(".dob_2").attr("autocomplete", "off");
   	jQuery(".dob_2").keyup(function() {
		if (jQuery(this).val().length == 2)
		{
			jQuery(this).blur();
			jQuery(".dob_3").focus();
		}
    });
	jQuery(".dob_3").attr("autocomplete", "off");
   	jQuery(".dob_3").keyup(function() {
		if (jQuery(this).val().length == 4)
		{
			jQuery(this).blur();
			jQuery(".dob_exit").focus();
		}
    });
	
    

}

				
