// Product Finder V2
// Requires the Jquery framework and thickbox
// Author: Jacob Wood
// Company: Integrity Corporation
var $query;
var $questions;
var $questionCount;
var $products;
var $productsString="";//matt
var $contactUrl;
var $recommendations = [0,0,0,0,0, 0, 0,0,0,0,0, 0,0,0,0,0,0,0];
var CurrentQuestion = 0;
$.fn.productFinder = function(products, questions, contactUrl) 
{
	$query = this;
	$questions = $.extend({}, $.fn.productFinder.questions, questions);
	$products = $.extend({}, $.fn.productFinder.products, products);
	$contactUrl = contactUrl;
	var count = 0;
	for(i in $questions)
	{
		count++;
	}
	$questionCount = count;
    return this.each(function()
	{
		start($(this));
		//$.fn.productFinder.dump($(this));
	});
};
// Defaults
$.fn.productFinder.questions = 
{
	0:
	{
		text:"No Questions",
		type:"text",
		isDriller:false,
		answers:
		{
			0:"No Answers"
		},
		productMatrix:
		{
			0:[0,0,0,0,0, 0, 0,0,0,0,0, 0,0,0,0,0,0,0]
		},
		applicability:[0,0,0,0,0, 0, 0,0,0,0,0, 0,0,0,0,0,0,0]
	}
};
$.fn.productFinder.products = 
{
	0:
	{
		name:"No name",
		desc:"No description",
		url:"http://"
	}
};
// Starts of the finder with the first question that serves as a collection point.
function start(query)
{
	var content = "";
	content    += "<form id='productFinderForm' action=''>";
	content    += "<div id='question-0' class='question'>";
	content    += "<strong>" + $questions[0]['text'] + "</strong>";
	
	var inputType = $questions[0]['type'];
	for(var i in $questions[0]['answers'])
	{
		content += "<div id='answer-0-" + i + "' class='answer'>";
		content += "<input id='a0-" + i + "' name='a0-" + i + "' type='" + inputType + "' class='" + inputType + "' value='" + $questions[0]['answers'][i] + "' />";
		content += "<label for='a0-" + i + "'>" + $questions[0]['answers'][i] + "</label>";
		content += "</div>";
	}
	content    += "</div>";
	content    += "<input id='next' type='button' value='Next' />";
	content    += "<input id='restart' type='button' value='Restart' />";
	content    += "</form>";
	query.html(content);
		
	$('#next').click(function()
	{
		if($('#productFinderForm #question-'+CurrentQuestion+' input:checked').length > 0)
		{
			if($('#error').length > 0)
			{
				$('#error').remove();
			}
			next(query);
		}
		else
		{
			if($('#error').length == 0)
			{
				$('#next').after("<span id='error' style='color:red;display:block'>Please select at least one option</span>");
			}
		}
	});
	$('#restart').css('display','none');
	$('#restart').click(function()
	{
		restart(query);
	});
}
// Keeps the previous chosen answers, hides the non-chosen, and also takes care of the knock-out process
function next(query)
{
	updateRecommendations();
	CurrentQuestion++;	
	if($questions[CurrentQuestion] != null)
	{
		if(CurrentQuestion != $questionCount-1)
		{
			if(isApplicable())
			{
				var content;
				content    = "<div id='question-"+CurrentQuestion+"' class='question'>";
				content    += "<strong>" + $questions[CurrentQuestion]['text'] + "</strong>";
				
				var inputType = $questions[CurrentQuestion]['type'];
				for(var i in $questions[CurrentQuestion]['answers'])
				{
					content += "<div id='answer-"+CurrentQuestion+"-" + i + "' class='answer'>";
					if(inputType == "checkbox")
					{
						content += "<input id='a"+CurrentQuestion+"-" + i + "' name='a"+CurrentQuestion+"-" + i + "' type='" + inputType + "' class='" 
								+ inputType + "' value='" + $questions[CurrentQuestion]['answers'][i] + "' />";
					}
					else
					{
						content += "<input id='a"+CurrentQuestion+"-" + i + "' name='a"+CurrentQuestion+"' type='" + inputType + "' class='" 
								+ inputType + "' value='" + $questions[CurrentQuestion]['answers'][i] + "' />";
					}
					content += "<label for='a"+CurrentQuestion+"-" + i + "'>" + $questions[CurrentQuestion]['answers'][i] + "</label>";
					content += "</div>";
				}
				content    += "</div>";
				$('#productFinderForm #next:eq(0)').before(content);
			}
			else
			{
				next(query);
			}
		}
		else
		{
				var content;
				content    = "<div id='question-"+CurrentQuestion+"' class='question'>";
				content    += "<strong>" + $questions[CurrentQuestion]['text'] + "</strong>";
				
				var inputType = $questions[CurrentQuestion]['type'];
				for(var i in $questions[CurrentQuestion]['answers'])
				{
					content += "<div id='answer-"+CurrentQuestion+"-" + i + "' class='answer'>";
					if(inputType == "checkbox")
					{
						content += "<input id='a"+CurrentQuestion+"-" + i + "' name='a"+CurrentQuestion+"-" + i + "' type='" + inputType + "' class='" 
								+ inputType + "' value='" + $questions[CurrentQuestion]['answers'][i] + "' />";
					}
					else
					{
						content += "<input id='a"+CurrentQuestion+"-" + i + "' name='a"+CurrentQuestion+"' type='" + inputType + "' class='" 
								+ inputType + "' value='" + $questions[CurrentQuestion]['answers'][i] + "' />";
					}
					content += "<label for='a"+CurrentQuestion+"-" + i + "'>" + $questions[CurrentQuestion]['answers'][i] + "</label>";
					content += "</div>";
				}
				content    += "</div>";
				$('#productFinderForm #next:eq(0)').before(content);
		}
	}
	else
	{
		end();
	}
}
function updateRecommendations()
{
	if(!$questions[CurrentQuestion]['isDriller'])
	{
		$('div#question-'+CurrentQuestion+' input').each(function(i)
		{
			if($('div#question-'+CurrentQuestion+' input:eq(' + i + ')').is(':checked'))
			{
				$recommendations = combine($questions[CurrentQuestion]['productMatrix'][i], $recommendations);
			}
			else
			{
				$('div#answer-'+CurrentQuestion+'-'+i).css('display','none');
			}
			$(this).attr('disabled','true');
		});
	}
	else
	{
		$('div#question-'+CurrentQuestion+' input').each(function(i)
		{
			if($('div#question-'+CurrentQuestion+' input:eq(' + i + ')').is(':checked'))
			{
				$recommendations = filter($recommendations,$questions[CurrentQuestion]['productMatrix'][i],$questions[CurrentQuestion]['applicability']);
			}
			else
			{
				$('div#answer-'+CurrentQuestion+'-'+i).css('display','none');
			}
			$(this).attr('disabled','true');
		});
	}
}
function filter( a, f, ap)
{
	var filtered = new Array();
	for (var i = 0; i < a.length; i++)
	{
		if ( ap[i] == 1)
		{
			if ((a[i]==0&&f[i]==0)||(a[i]==1&&f[i]==0)||(a[i]==0&&f[i]==1))
			{
				filtered[i] = 0;
			}
			if ( ( a[i] == 1 &&  f[i] == 1 ) )
			{
				filtered[i] = 1;
			}
		}
		else
		{
			filtered[i] = a[i];
		}
	}
	return filtered;
}
function combine(a1, a2)
{
	var combined = new Array();
	for (var i = 0; i < a1.length; i++)
	{
		if ( a1[i] == 0 &&  a2[i] == 0 )
		{
			combined[i] = 0;
		}
		if ( a1[i] == 1 &&  a2[i] == 1 )
		{
			combined[i] = 1;
		}
		if ( ( a1[i] == 0 &&  a2[i] == 1 ) || ( a1[i] == 1 &&  a2[i] == 0) )
		{
			combined[i] = 1;
		}
	}
	return combined;
}
function isApplicable()
{
	var isApplicable = false;
	if ( CurrentQuestion > 0 )
	{
		for ( var i = 0; i < $recommendations.length; i++ )
		{
			if ( $recommendations[i] == 1 &&  $questions[CurrentQuestion]['applicability'][i] == 1 )
			{
				isApplicable = true;
				break;
			}
		}
	}
	else
	{
		isApplicable = true;
	}
	return isApplicable;
}
// Ends the finder flow by taking the final recommendation array and displaying an overlay with the user's $recommendations
// Also sends form info to the server for user experience functionality
function end()
{
	
	$('#next').css('display','none');
	$('#restart').css('display','block');
	var content = getResults();
	content += "<a id='contact' href='"+$contactUrl+"'>Contact me about these solutions</a>";
	
	$.nyroModalManual(
	{
		bgColor: '#000000',
		content: content,
		minWidth: 380,
		width:380,
		height:480,
		title:'Product Finder Results'
    });
	//alert(compileProductFinderData());
	$('.result:odd').removeClass('result').addClass('result-grey');
	$.post("/productfinder/inc/productFinderSmartNav.php", {productFinderData: compileProductFinderData()}, function(data){});
}
function compileProductFinderData()
{
	var html = buildProductFinderDataHtml();
	var css = buildProductFinderDataCss();
	var script = buildProductFinderDataScript(html);
	return css+script;
}
function buildProductFinderDataCss()
{
	var css = "";
	css += "<style type='text/css'>";	
	css += "	#productFinderData a:hover{color:#00a994;font-family:Verdana,Arial,Helvetica,sans-serif;font-weight:bold;text-decoration:none;}";
	css += "	#productFinderData{font-family:Verdana,Arial,Helvetica,sans-serif;}";
	css += "	#productFinderData a{font-family:Verdana,Arial,Helvetica,sans-serif;font-size:12px;font-weight:bold;text-decoration:none;}";
	css += "	#productFinderData span{color:#FF9A00;display:block;font-size:11px;font-style:normal;font-weight:normal;margin:0 0 10px;}";
	css += "	#productFinderData strong{color:#444444;font-size:10px;font-weight:bold;}";
	css += "	#productFinderData ul{list-style-image:none;list-style-position:outside;list-style-type:none;margin:10px 0;padding:0;}";
	css += "	#productFinderData ul li{color:#444444;font-size:10px;font-weight:normal;}";
	css += "	#productFinderData span strong{color:#00a994;font-size:11px;font-weight:bold;}";
	css += "	#productFinderData ul li a{font-family:Verdana,Arial,Helvetica,sans-serif;font-weight:bold;text-decoration:none;font-size:11px;}";
	css += "	#EPFR_left {display:block;display:block;float:left;height:90px;margin-left:6px;width:3px;}";
	css += "	#EPFR {display:block;padding-left:15px;color:#FF9A00;float:left;font-size:11px;font-weight:bold;padding-bottom:10px;padding-top:14px;width:170px;}";
	css += "	#EPFR_right {display:block;float:left;height:90px;width:4px;}";
	css += "</style>";
	return css;
}
function buildProductFinderDataScript(html)
{
	var script = "";
	script += "<script type='text/javascript'>";
	script += "if(document.getElementById('EPFR')){document.getElementById('EPFR').innerHTML=\""+html+"\";}";
	script += "if(!backgroundimages){if(document.getElementById('EPFR_left')){document.getElementById('EPFR_left').style.backgroundImage='none';}if(document.getElementById('EPFR')){document.getElementById('EPFR').style.backgroundImage='none';}if(document.getElementById('EPFR_right')){document.getElementById('EPFR_right').style.backgroundImage='none';}}";
	script += "</script>";
	return script;
}
function buildProductFinderDataHtml()
{
	var html = "";
	html += "<div id='productFinderData'>";
	html += "<span><strong>Product Finder Results</strong></span>";
	$('.question strong').each(function(i)
	{
		html += "<strong>" + $(this).text() + "</strong>";
		html += "<ul>";
		$(".question:nth("+i+") input:checked").each(function(j)
		{
			html += "<li>" + $(this).val() + "</li>";
		});
		html += "</ul>";
	});
	html += "<span><strong>Recommendations</strong></span>";
	html += "<ul>";
	for(var k in $recommendations)
	{
		if($recommendations[k])
		{
			//alert($products[k]['name']);//Matt
			$productsString += $products[k]['name'] +",";//matt
			html += "<li><a href='" + $products[k]['url'] + "'>" + $products[k]['name'] + "</a></li>";
		}
	}
	//alert($productsString);//matt
	$.post("/productfinder/inc/productFinderSmartNav2.php", {productsString: $productsString}, function(data){});//matt
	html += "</ul>";
	html += "<a href='/providers'>New Search</a>";
	html += "</div>";
	return html;
}
function getResults()
{
	var content = "";
	var productCount = 0;
	content += "<div id='results' class='results'>";
	for(var i in $recommendations)
	{
		if($recommendations[i])
		{
			content += "	<a href='" + $products[i]['url'] + "' class='result'>";
			content += "		<span>" + $products[i]['name'] + "</span>";
			content += "		<p>" + $products[i]['desc'] + "</p>";
			content += "	</a>";
			productCount++;
		}
	}
	if(productCount == 0)
	{
			content += "	<a href='http://emdeon.com/ProviderSolutions/product_finder_contact.php' class='result'>";
			content += "		<span>Emdeon Custom Provider Solutions</span>";
			content += "		<p>Based on the data you entered, it appears your needs require a customized solution. One of our representatives would be happy to contact you to discuss the options for developing the right solution to meet your needs.</p>";
			content += "	</a>";
	}
	content += "</div>";
	return content;
}
function restart(query)
{
	$recommendations = [0,0,0,0,0, 0, 0,0,0,0,0, 0,0,0,0,0,0,0];
	CurrentQuestion = 0;
	query.empty();
	start(query);
}
// Used to display all the information contained in the questions object
$.fn.productFinder.dump = function(element)
{
	var infoDump ="";
	infoDump = "<em>Question Data:</em><br />";
	for(index in $questions)
	{
		infoDump += "<em>" + index + "</em>:" + $questions[index];
		infoDump += "<br />";
		for(indexb in $questions[index])
		{
			infoDump += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			infoDump += "<em>" + indexb + "</em>:" + $questions[index][indexb];
			infoDump += "<br />";
			if(indexb == "answers" || indexb == "productMatrix")
			{
				for(indexc in $questions[index][indexb])
				{
					infoDump += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
					infoDump += "<em>" + indexc + "</em>:" + $questions[index][indexb][indexc];
					infoDump += "<br />"
				}
			}
		}
	}
	infoDump += "<br />===============================<br />";
	infoDump += "<em>Product Data:</em><br />";
	for(i in $products)
	{
		infoDump += "<em>" + i + "</em>:" + $products[i];
		infoDump += "<br />"
		for(j in $products[i])
		{
			infoDump += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			infoDump += "<em>" + j + "</em>:" + $products[i][j];
			infoDump += "<br />";
		}
	}
	element.html(infoDump);
};