prefix = parent.location.protocol;
var xmlhttp;
var emailVerification;
var name;
var email;
var address;
var zip;
var cityState;
var phone;
var diagnosis;
var comments;
var submitted;
var formID;
var dataString;
var passable;
var stage;
var liveEmail;

var detect = navigator.userAgent.toLowerCase();
// SAFARI CSS FIX
if (checkIt('safari')){
	document.write('<style type="text/css">#kit-form .select, .inline-form .select { background: #FEFEFE; } #kit-form fieldset { clear: left; margin: 0; } .text-area, #comment { resize:none; }</style>');
}
if (checkIt("msie 6"))
	{ $(document).ready(function(){ $(document).pngFix(); });
	document.write('<script src="http://www.mesoanswers.org/scripts/jquery.pngFix.js" type="text/javascript"></script>');
}
function checkIt(string){
	var place = detect.indexOf(string) + 1;
	return place;
}

// FONT RESIZE FUNCTION
$(document).ready(function(){
	$('#content').css('font-size', 13);
	$('#footer').css('font-size', 13);
	// Increase Font Size
	$("#text-larger").click(function(){
		var currentFontSize = $('#content').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*1.1;
		if (newFontSize < 24 )
		{
			$('#content').css('font-size', newFontSize);
			$('#footer').css('font-size', newFontSize);
			return false;
		}
		else
		{
			$('#content').css('font-size', 24);
			$('#footer').css('font-size', 24);
			return false;
		}
	});
	// Decrease Font Size
	$("#text-smaller").click(function(){
	var currentFontSize = $('#content').css('font-size');
	var currentFontSizeNum = parseFloat(currentFontSize, 10);
	var newFontSize = currentFontSizeNum*0.909;
	if (newFontSize > 13 )
	{
		$('#content').css('font-size', newFontSize);
		$('#footer').css('font-size', newFontSize);
		return false;
	}
	else
	{
		$('#content').css('font-size', 13);
		$('#footer').css('font-size', 13);
		return false;
	}
	});
});

$(document).ready(function(){
	// Remove autofil on livesearch
	$('#search-input').attr('autocomplete', 'off');
	$('#inline-search-input').attr('autocomplete', 'off');
	
	// SIDE NAV EXPAND/COLLAPSE
	$(".side-nav-item").removeClass("open");
	$(".side-nav-list-item").removeClass("open");
	$(".side-nav-list-item-list").css("display","none");
	$(".expand").click(function(){
		$(this).parent().toggleClass("open");
	});
	$(".expand2").click(function(){
		$(this).parent().toggleClass("open");
		if($(this).next(".side-nav-list-item-list").css("display") == "none")
		{
			$(this).next(".side-nav-list-item-list").css("display","block");
		}
		else
		{
			$(this).next(".side-nav-list-item-list").css("display","none");
		}
	});
	
	$('.autocomplete').live('click', function() {
		$("#search-input").val($(this).text());
		$("#search-form").submit();
		return false;
	});
	
	$('.autocomplete').live('mouseenter', function() {
		$(".autocomplete").removeClass("itemhover");
		$(this).addClass("itemhover");
		$("#search-input").val($(this).text());
		currentSelection = $(this).attr('id').replace("autocomplete", "");
	});
});

currentSelection = 0;

function sendKeyStroke(e)
{
	if(e == 38 || e == 40)
	{
		if($('#livesearch').css('display')!='none')
		{
			switch(e) { 
				// User pressed "up" arrow
				case 38:
					if (currentSelection > 1)
					{
						currentSelection--;
						$('#search-input').val($("#autocomplete" + currentSelection).text());
					}
					else
					{
						currentSelection = $('.autocomplete').size();
						$('#search-input').val($("#autocomplete" + currentSelection).text());
					}
				break;
				// User pressed "down" arrow
				case 40:
					if(currentSelection < $('.autocomplete').size())
					{
						currentSelection++;
						$('#search-input').val($("#autocomplete" + currentSelection).text());
					}
					else
					{
						currentSelection = 1;
						$('#search-input').val($("#autocomplete" + currentSelection).text());
					}
				break;
			}
		}
		
		$(".autocomplete").removeClass("itemhover");
		$("#autocomplete" + currentSelection).addClass("itemhover");
	}
	else
	{
		str = $('#search-input').val();
		str = str.replace(/[\^'"$.|?*+(){}\[\]\\@!#%œ®†¥øπ«»åß∂ƒ©∑˙∆˚¬…æΩ≈ç√∫ñÑ“”‘’≤≥÷¿Œ„é´‰ˇÁ¨ˆØ∏ÅÍÎÏ˝ÓÔÚÆ¸˛Ç◊ı˜Â¯⁄€‹›ﬁxﬂ‡°·‚—±+&*ÁÉÍÓÚáéíóúÂÊÎÔÛâêîôûÄËÏÖÜŸäëïöüÿ=,;]+/gi, "");
		
		currentSelection = 0;
		
		if (str.length==0)
		{
			$('#livesearch').html("");
			$('#livesearch').hide();
			$('#form-diagnosis-input').show();
			$('#kit-form .city-state-input').show();
			return;
		}
		xmlhttp=null;
		if (window.XMLHttpRequest)
		{//Firefox, Opera, IE7, etc.
		xmlhttp=new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{//IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (xmlhttp!=null)
		{			
			xmlhttp.onreadystatechange=state_Change;
			xmlhttp.open("GET",""+ prefix + "//www.mesoanswers.org/quick-search.php/search/live_search/"+str,true);
			xmlhttp.send(null);
		}
		else
		{
			alert("Your browser does not support XMLHTTP.");	
		}
	}
}

function listCitiesStates(str)
{
	str = str.replace(/[\D]+/gi, "");
	$(".zip-field").val(str);
	$(".zip-item label").fadeOut();
	if($('#form-zip-input').val() == ""){$(".zip-item label").fadeIn();}
	
	if (str.length==0)
	{
		$(".city-state-item").html('<span class="address-span">Enter Zip for City and State</span>');
		return;
	}
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{//Firefox, Opera, IE7, etc.
 	xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{//IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null)
	{			
		xmlhttp.onreadystatechange=state_Change_Zip;
		xmlhttp.open("GET",""+ prefix + "//www.mesoanswers.org/quick-search.php/search/zip/"+str,true);
		xmlhttp.send(null);
	}
	else
	{
		alert("Your browser does not support XMLHTTP.");
	}
}

function suggestionHide()
{
	$('#livesearch').css('display', 'none');
	$(".autocomplete").removeClass("itemhover");
	$('#form-diagnosis-input').show();
	$('#kit-form .city-state-input').show();
	currentSelection = 0;
}

function suggestionShow()
{
	if($('#livesearch').html() == '')
	{
		$('#livesearch').css('display', 'none');
		$('#form-diagnosis-input').show();
		$('#kit-form .city-state-input').show();
	}
	else
	{
		$('#livesearch').css('display', 'block');
		if($('#livesearch').height() > 240) { $('#kit-form .city-state-input').hide(); if($('#livesearch').height() > 277) { $('#form-diagnosis-input').hide(); } }
		else { $('#form-diagnosis-input').show(); $('#kit-form .city-state-input').show(); }
	}
	$(".autocomplete").removeClass("itemhover");
	currentSelection = 0;
}

function state_Change()
{
	if (xmlhttp.readyState==4)
	{// 4 = "loaded"
		if (xmlhttp.status==200)
		{// 200 = "OK"
			$('#livesearch').html(xmlhttp.responseText);
			$('#livesearch').css("display", "block");
			$('#livesearch').css("width", "252px");
			if($('#livesearch').height() > 240) { $('#kit-form .city-state-input').hide(); if($('#livesearch').height() > 277) { $('#form-diagnosis-input').hide(); } }
			else { $('#form-diagnosis-input').show(); $('#kit-form .city-state-input').show(); }
		}
		else
		{
			$('div.#livesearch').html('<span style="padding:5px;display:block;">You have entered invalid text</span>');
		}
	}
}

function state_Change_Zip()
{
	if (xmlhttp.readyState==4)
	{// 4 = "loaded"
		if (xmlhttp.status==200)
		{// 200 = "OK"
			$(".city-state-item").html(xmlhttp.responseText);
		}
	}
}

function secondaryInField() {
	$("#secondary-form label").inFieldLabels();
}

// Load panels and main form to top of page.
$(document).ready(function(){
	$('div#top').load('' + prefix + '//www.mesoanswers.org/includes/ajax-includes/top-section.php?video_file_name=' + vid + '&play_video=' + pvid, function() {
		
		importantFlash = $("param[name='flashvars']").val();
		videoContent = '<object type="application/x-shockwave-flash" data="' + prefix + '//www.mesoanswers.org/media/player/player-licensed.swf" height="304" width="542"><param name="movie" value="' + prefix + '//www.mesoanswers.org/media/player/player-licensed.swf"/><param name="allowfullscreen" value="true"/><param name="flashvars" value="' + importantFlash.replace("autostart=true", "autostart=false") + '"/><a href="' + prefix + '//get.adobe.com/flashplayer/" title="Get the Adobe Flash Player" onclick="window.open(\'' + prefix + '//get.adobe.com/flashplayer/\',\'_blank\',\'toolbar=yes,location=yes, directories=yes, status=yes, menubar=yes, scrollbars=yes, resizable=yes, width=1024, height=768\'); return false;"><img src="' + prefix + '//www.mesoanswers.org/media/preview-images/get_flash.gif" width="542" height="304" alt="Get the Adobe Flash Player"/></a></object>';
		
		//MOVE LABELS INSIDE OF FORM FIELDS, FADE WHEN FOCUSED, DISAPPEAR WHEN TYPED WITHIN
		(function($){$.InFieldLabels=function(b,c,d){var f=this;f.$label=$(b);f.label=b;f.$field=$(c);f.field=c;f.$label.data("InFieldLabels",f);f.showing=true;f.init=function(){f.options=$.extend({},$.InFieldLabels.defaultOptions,d);if(f.$field.val()!=""){f.$label.hide();f.showing=false};f.$field.focus(function(){f.fadeOnFocus()}).blur(function(){f.checkForEmpty(true)}).bind('keydown.infieldlabel',function(e){f.hideOnChange(e)}).change(function(e){f.checkForEmpty()}).bind('onPropertyChange',function(){f.checkForEmpty()})};f.fadeOnFocus=function(){if(f.showing){f.setOpacity(f.options.fadeOpacity)}};f.setOpacity=function(a){f.$label.stop().animate({opacity:a},f.options.fadeDuration);f.showing=(a>0.0)};f.checkForEmpty=function(a){if(f.$field.val()==""){f.prepForShow();f.setOpacity(a?1.0:f.options.fadeOpacity)}else{f.setOpacity(0.0)}};f.prepForShow=function(e){if(!f.showing){f.$label.css({opacity:0.0}).show();f.$field.bind('keydown.infieldlabel',function(e){f.hideOnChange(e)})}};f.hideOnChange=function(e){if((e.keyCode==16)||(e.keyCode==9))return;if(f.showing){f.$label.hide();f.showing=false};f.$field.unbind('keydown.infieldlabel')};f.init()};$.InFieldLabels.defaultOptions={fadeOpacity:0.5,fadeDuration:300};$.fn.inFieldLabels=function(c){return this.each(function(){var a=$(this).attr('for');if(!a)return;var b=$("input#"+a+"[type='text'],"+"input#"+a+"[type='password'],"+"textarea#"+a);if(b.length==0)return;(new $.InFieldLabels(this,b[0],c))})}})(jQuery);
		
		//INITIATE THE LABEL MOVE
		$("#kit-form label").inFieldLabels();
		
		//ADD SPECIAL MASKS TO PHONE FIELD
		$("#form-phone-input").mask("(999) 999-9999");
		
		$('div.inline-form').load('' + prefix + '//www.mesoanswers.org/includes/ajax-includes/inline-form.php', function() {
			
			//ADD SPECIAL MASKS TO PHONE FIELD
			$("#inline-form-phone-input").mask("(999) 999-9999");
			
			$("#inline-form label").inFieldLabels();
			
			// validate inline form on keyup and submit
			$("#inline-form").validate({
				rules: {
					name: "required",
					phone: "required",
					diagnosis: "required"
				},
				messages: {
					name: "Required",
					diagnosis: "Required",
					phone: "Required"
				},
				errorElement: "em",
				submitHandler:function(form) 
				{
					if($("#inline-form-submitted-input").val() == 1)
					{
						name = $("input#inline-form-name-input").val();
						address = $("input#inline-form-address-input").val();
						zip = $("input#inline-form-zip-input").val();
						cityState = $("select.city-state-input").val();
						phone = $("input#inline-form-phone-input").val();
						diagnosis = $("select#inline-form-diagnosis-input").val();
						comments = $("textarea#inline-form-comments-input").val();
						submitted = $("#inline-form-submitted-input").val();
						formID = '2';
					}
					email = $("input#inline-form-email-input").val();
					formID = '2';
					
					dataString = 'name=' + name + '&address=' + address + '&cityState=' + cityState + '&zip=' + zip + '&phone=' + phone + '&email=' + email + '&diagnosis=' + diagnosis + '&comments=' + comments + '&formID=' + formID + '&submitted=' + submitted;
					
					$(".inline-form").remove();
					$('#article').after('<div class="inline-form"><span></span><div id="inline-result"></div></div>');
					$('#inline-result').load(encodeURI('http://www.mesoanswers.org/includes/ajax-includes/dataHandler.php?name=' + name + '&address=' + address + '&cityState=' + cityState + '&zip=' + zip + '&phone=' + phone + '&email=' + email + '&diagnosis=' + diagnosis + '&comments=' + comments + '&formID=' + formID + '&submitted=' + submitted));
					
					return false;
				}
			});
		});
		
		// validate kit form on keyup and submit
		$("#kit-form").validate({
			rules: {
				name: "required",
				phone: "required",
				diagnosis: "required"
			},
			messages: {
				name: "Required",
				diagnosis: "Required",
				phone: "Required"
			},
			errorElement: "em",
			submitHandler:function(form) 
			{
				if($("#form-submitted-input").val() == 1)
				{
					name = $("input#form-name-input").val();
					address = $("input#form-address-input").val();
					zip = $("input#form-zip-input").val();
					cityState = $("select.city-state-input").val();
					phone = $("input#form-phone-input").val();
					diagnosis = $("select#form-diagnosis-input").val();
					comments = $("textarea#form-comments-input").val();
					submitted = $("#form-submitted-input").val();
					formID = '1';
				}
				email = $("input#form-email-input").val();
				formID = '1';
				
				dataString = 'name=' + name + '&address=' + address + '&cityState=' + cityState + '&zip=' + zip + '&phone=' + phone + '&email=' + email + '&diagnosis=' + diagnosis + '&comments=' + comments + '&formID=' + formID + '&submitted=' + submitted;
				
				$(".kit-form").remove();
				$('#panels').after('<div class="kit-form"><span></span><div id="result"></div></div>');
				$('#result').load(encodeURI('http://www.mesoanswers.org/includes/ajax-includes/dataHandler.php?name=' + name + '&address=' + address + '&cityState=' + cityState + '&zip=' + zip + '&phone=' + phone + '&email=' + email + '&diagnosis=' + diagnosis + '&comments=' + comments + '&formID=' + formID + '&submitted=' + submitted));
				$("#secondary-form label").inFieldLabels();
				
				return false;
			}
		});
		
		$('.secondary-submit').live('click', function() {
			// validate kit form on keyup and submit
			$("#secondary-form").validate({
				rules: {
					email: {
					  required: true,
					  email: true
					},
					submitted: "required",
					secondary: "required"
				},
				messages: {
					email: {
					  required: "Required",
					  email: "Invalid Email"
					},
					submitted: "Required",
					secondary: "Required"
				},
				errorElement: "em",
				submitHandler:function(form) 
				{
					email = $("input#form-email-input").val();
					secondary = $("input#form-secondary-input").val();
					
					dataString = 'name=' + name + '&address=' + address + '&cityState=' + cityState + '&zip=' + zip + '&phone=' + phone + '&email=' + email + '&diagnosis=' + diagnosis + '&comments=' + comments + '&formID=' + formID + '&submitted=' + submitted + '&secondary=' + secondary;
					
					$("#message").remove();
					$('#result').load(encodeURI('http://www.mesoanswers.org/includes/ajax-includes/dataHandler.php?name=' + name + '&address=' + address + '&cityState=' + cityState + '&zip=' + zip + '&phone=' + phone + '&email=' + email + '&diagnosis=' + diagnosis + '&comments=' + comments + '&formID=' + formID + '&submitted=' + submitted + '&secondary=' + secondary));
					$("#secondary-form label").inFieldLabels();
					
					return false;
				}
			});
		});
		
		$('.no-thank-you').live('click', function() {
			$('#result').html('<div id="message"><h4>Information Received;<br />Thank You!</h4><p>' + name + ', thank you for completing our form. We appreciate your trust in doing so and hope that we can be of assistance.</p><p>If you provided us with your email address in the previous step, you should receive your free copy of our informative booklet via email immediately. Additionally, within the next 24 hours you should receive a call from a mesothelioma consultant who may be able to help you with your particular situation - including talking to you about your legal rights and other issues that you may be facing with your diagnosis.</p><p>Thank you again for requesting additional information; our thoughts are with you and your family in this trying time.</p><p>Yours,</p><p class="last">The MesoAnswers.org Team<img width=1 height=1 src="https://805920.r.msn.com/?type=1&cp=1&dedup=1"/><img height="1" width="1" style="border-style:none;" alt="" src="https://www.googleadservices.com/pagead/conversion/1029639729/?value=0&amp;label=F1xHCJ_7rgEQsZz86gM&amp;guid=ON&amp;script=0"/></p><script language="JavaScript" type="text/javascript" src="srv1.wa.marketingsolutions.yahoo.com/script/ScriptServlet?aid=1VRVESBP5LVJB3FRMH7KONDA4P8"></script></div>');
			$('#inline-result').html('<div id="message"><h4>Information Received;<br />Thank You!</h4><p>' + name + ', thank you for completing our form. We appreciate your trust in doing so and hope that we can be of assistance.</p><p>If you provided us with your email address in the previous step, you should receive your free copy of our informative booklet via email immediately. Additionally, within the next 24 hours you should receive a call from a mesothelioma consultant who may be able to help you with your particular situation - including talking to you about your legal rights and other issues that you may be facing with your diagnosis.</p><p>Thank you again for requesting additional information; our thoughts are with you and your family in this trying time.</p><p>Yours,</p><p class="last">The MesoAnswers.org Team<img width=1 height=1 src="https://805920.r.msn.com/?type=1&cp=1&dedup=1"/><img height="1" width="1" style="border-style:none;" alt="" src="https://www.googleadservices.com/pagead/conversion/1029639729/?value=0&amp;label=F1xHCJ_7rgEQsZz86gM&amp;guid=ON&amp;script=0"/></p><script language="JavaScript" type="text/javascript" src="srv1.wa.marketingsolutions.yahoo.com/script/ScriptServlet?aid=1VRVESBP5LVJB3FRMH7KONDA4P8"></script></div>');
			return false;
		});
	});
	
	$(".panelButtonLink").live("mouseenter", function(){
		fullClass = $(this).parent().parent().attr('class');
		if (fullClass.indexOf("current") > - 1) { return false; }
		else
		{
			if ($(this).parent().parent().attr('id') == 'panelButton0')
			{
				$('.fader').stop();
				$('.fader').stop();
				$(".panel-button.current").removeClass("current");
				$(this).parent().parent().toggleClass("current");
				$.hoveredButton = $(this).parent().parent().attr("id").replace("panelButton", "");
				currentPanel=parseFloat($.hoveredButton);
				$('#panel3').after('<div class="fader"></div>');
				$('.fader').fadeTo(500, 1, function() {
					$('#panel0').html(videoContent);
					$(".panel.current").removeClass("current");
					$("#panel"+currentPanel).toggleClass("current");
				});
				$(".fader").fadeTo(500, 0, function() { $(this).remove(); });
			}
			else
			{
				$('.fader').stop();
				$('.fader').stop();
				$(".panel-button.current").removeClass("current");
				$(this).parent().parent().toggleClass("current");
				$.hoveredButton = $(this).parent().parent().attr("id").replace("panelButton", "");
				currentPanel=parseFloat($.hoveredButton);
				$('#panel3').after('<div class="fader"></div>');
				$('.fader').fadeTo(500, 1, function() {
					$('#panel0').html('');
					$(".panel.current").removeClass("current");
					$("#panel"+currentPanel).toggleClass("current");
				});
				$(".fader").fadeTo(500, 0, function() { $(this).remove(); });
			}
		}
	});
});
