var issue = false;
var area = false;
var topic = false;
function gbuttons() {
$('input[type="radio"]').change(function(){
		$('input[type="button"]', '#quick_contact_wrap').removeAttr("disabled"); 
	});	
} 
$(function(){
	buildGuide();
	gbuttons();
	$('#gclose').live('click', gclose);
	
	// Select Topic 
	$('#submitStep2').live('click', function(){
		showStep(2);
		var sel = $("input[name='step2']:checked");
		var oo = sel.data('o');
		var val = sel.val();
		area = oo;
		$('#side-descr1').html(oo.label);
		$('#gheading').html('What is your '+oo.label+' issue about?');
		$('#gcontent').html('<p>Please select one of the following that best fits your situation.</p>');
		showImages(val, oo.imgs);
		$('#goptions').html('<ul id="theOptions" class="issues"></ul>');
		$('.question').attr('id', 'step3');
		$.each(oo.options.split(','), function(i, v){
			var o = options[v.trim()];
			$('#theOptions').append(''
				+ ' <li><label>'
				+ '	<input type="radio" id="o-'+i+'" value="'+slug(o.label)+'" name="step3" /> '+o.label
				+ '</label></li>'
			);
			$('#o-'+i).data('o', o);
		});
		$('#goptions').append('<span class="form-row"><input type="button" value="Next" id="submitStep3" class="questionBtn" disabled="true"/></span>');
		gbuttons();
	});
	
	// Select Secondary Topic 
	$('#submitStep3').live('click', function(){
		showStep(3);
		var sel = $("input[name='step3']:checked");
		var oo = sel.data('o');
		var val = sel.val();
		topic = oo;
		if ( oo.options != undefined && oo.options.length ) {
			$('#side-descr2').html(oo.label);
			$('#gheading').html('What is your '+oo.label+' issue about?');
			$('#gcontent').html('<p>Please select one of the following that best fits your situation.</p>');
			showImages(val, oo.imgs);
			$('#goptions').html('<ul id="theOptions" class="issues"></ul>');
			$('.question').attr('id', 'step3');
			$.each(oo.options.split(','), function(i, v){
				var o = options[v.trim()];
				$('#theOptions').append(''
					+ ' <li><label>'
					+ '	<input type="radio" id="o-'+i+'" value="'+slug(o.label)+'" name="step4" /> '+o.label
					+ '</label></li>'
				);
				$('#o-'+i).data('o', o);
			});
			$('#goptions').append('<span class="form-row"><input type="button" value="Next" id="submitStep4" class="questionBtn" disabled="true"/></span>');
			gbuttons();
		}
		else {
			showForm(oo);
		}
	});
	
	// Select Sub-topic
	$('#submitStep4').live('click', function(){
		showForm();
	});
});

function showImages(id, imgs)
{
	var c = 0;
	$.each(imgs, function(i, v){
		c++;
		$('#gcontent').append('<img src="'+v+'" id="g_'+id+'-'+c+'" class="g_sect_image g_'+i+'_image"/>');
	});
	$('#gcontent').append('<div class="clear"></div>');
}

function showForm(oo)
{
	showStep(4);
	if (oo == undefined) {
		var sel = $("input[name='step4']:checked");
		var oo = sel.data('o');
	}
	issue = oo;
	$('#side-descr3').html(oo.label);
	$('.question').attr('id', 'finish');
	$('#gheading').html("Free "+issue.label+" Case Evaluation");
	$('#gcontent').html('<p>We want to help you navigate this situation. Send us your information for a free case evaluation. We will also email you information on this issue and redirect you to the appropriate page on our website."</p>');
	$('#goptions').html(''
		+ '<form id="contactForm" method="post">'
		+ '<div class="form-row">'
		+ '		<label for="FirstName">First Name <span class="small">Enter first name</span></label> '
		+ '		<input type="text" class="text" id="FirstName" name="FirstName" /> '
		+ '	</div>'
		+ '	<div class="form-row">'
		+ '		<label for="LastName">Last Name <span class="small">Enter last name</span></label> '
		+ '		<input type="text" class="text" id="LastName" name="LastName" /> '
		+ '	</div>'
		+ '	<div class="form-row">'
		+ '		<label for="phone">Phone Number <span class="small">Enter phone</span></label> '
		+ '		<input type="text" class="text" id="phone" maxlength="12" name="Phone" /> '
		+ '	</div>'
		+ '	<div class="form-row">'
		+ '		<label for="email">Email <span class="small">Email Address</span></label> '
		+ '		<input type="text" class="text" id="email" name="Email" />'
		+ '	</div>'
		+ '	<div class="form-row">'
		+ '		<label for="CaseDetails">Case Details <span class="small">Tell us about your case</span></label> '
		+ '		<textarea id="CaseDetails" name="CaseDetails"></textarea>'
		+ '	</div>'
		+ '	<div class="form-row">'
		+ '		<input type="submit" id="submitFinish" value="Submit Free Case Evaluation"/>'
		+ '	</div>'
		+ '</form>'
	);
	
	//Submit Form
	$('#contactForm').submit(function(){
		if(issue && area){
			var data = {
				env_report: "REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT,AUTH_TYPE,REMOTE_USER",
				recipients: "info.AT.coloradolaw.net,intake.AT.coloradolaw.net,dawn.AT.coloradolaw.net,webmaster.AT.coloradolaw.net,dschanker.AT.coloradolaw.net,kbachus.AT.coloradolaw.net,marketing.AT.coloradolaw.net,webintake.AT.coloradolaw.net, webintakes.AT.coloradolaw.net, oncallattorney.AT.gmail.com",
				conditions: ":##TEST#email ~ /^[a-z0-9_.]+@[a-z0-9_]+\.([a-z0-9_.]+)*[a-z0-9_.]$/i #Please enter a valid email address.#",
				required: "email:Your email address",
				good_url: "http://www.coloradolaw.net/thanks-all.html",
				subject: "Client Contact from Guided Form",
				contact_greeting: $('#FirstName').val()+' '+$('#LastName').val(),
				contact_first_name: $('#FirstName').val(),
				contact_last_name: $('#LastName').val(),
				contact_area: area.label,
				contact_topic: topic.label,
				contact_issue: issue.label,
				email: $('#email').val(),
				contact_home_phone: $('#phone').val(),
				contact_comment: $('#CaseDetails').val(),
				formname: "Form_6002211_20031121161510"
			};
			$('#submitFinish').val('Submitting your Case...');
			$.post('http://www.coloradolaw.net/clientcontact.php', data, function(){
				thankYouPage();
			});
			$.post('http://www.coloradolaw.net/guidebox/emailer.php', data);
		}
		return false;
	});
}
function buildGuide()
{
	$('body').append(''
		+ '<div id="quick_contact_wrap">'
		+ '	<div class="question" id="step2">'
		+ '		<div class="leftWrap">'
		+ ' 	<a href="#" id="gclose">Close</a>'
		+ '			<dl>'
		+ '				<dt id="side-step1">'
		+ '					Step 1'
		+ '				</dt>'
		+ '				<dd id="side-descr1">'
		+ '					Choose an area of law'
		+ '				</dd>'
		+ '				<dt id="side-step2" class="step-gray">'
		+ '					Step 2'
		+ '				</dt>'
		+ '				<dd id="side-descr2" class="step-gray">'
		+ '					Select a practice topic'
		+ '				</dd>'
		+ '				<dt id="side-step2" class="step-gray">'
		+ '					Step 3'
		+ '				</dt>'
		+ '				<dd id="side-descr3" class="step-gray">'
		+ '					Select a legal issue'
		+ '				</dd>'
		+ '				<dt id="side-step4" class="step-gray">'
		+ '					Finish'
		+ '				</dt>'
		+ '				<dd id="side-descr4" class="step-gray">'
		+ '					Let us know who you are'
		+ '				</dd>'
		+ '			</dl>'
		+ '		</div>'
		+ '		<div class="rightWrap">'
		+ '			<h1 id="gheading"></h1>'
		+ '			<div id="gcontent"></div>'
		+ '			<div id="goptions"></div>'
		+ '		</div>'
		+ '		<div class="clear"></div>'
		+ '	</div>'
		+ '</div>'
		+ '<div id="goverlay"></div>'
	);
	
	//Start Page
	$('#gheading').html('What is your legal issue about?');
	$('#gcontent').html('');
	$('#goptions').html('<dl id="theOptions"></dl>');
	$.each(options['start'].options.split(','), function(i, v){
		var ov = v.trim();
		var o = options[ov];
			$('#theOptions').append(''
			+ ' <dt>'
			+ '	<label><input type="radio" id="o-'+i+'" value="'+slug(o.label)+'" name="step2" /> '+o.label+'</label>'
			+ '</dt>'
			+ '<dd>'+o.content+'</dd>'
		);				
		$('#o-'+i).data('o', o);
	});
	$('#goptions').append('<span class="form-row"><input type="button" value="Next" id="submitStep2" class="questionBtn" disabled="true"/></span><div class="clear"></div>');
}

function thankYouPage(){
	showStep(5);
	$('#gheading').html('Thank you!');
	$('#gcontent').html('<p>We\'ve received your request and will respond shortly.</p>');
	$('#goptions').empty();
	if(issue.url != undefined){
		$('#goptions').html('You will now be redirected to more information about '+issue.label+'. <a href="'+issue.url+'">Click here to go there immediately.</a>');
		setTimeout(function(){
			location.href = issue.url;
		}, 6000)
	}
}

function slug(str){
	return str.replace(/ /g, '-').toLowerCase();
}

function showStep(s)
{
	$('dt', '.leftWrap').addClass('step-gray');
	$('dd', '.leftWrap').addClass('step-gray');
	$('#side-step'+s).removeClass('step-gray');
	$('#side-descr'+s).removeClass('step-gray');
}

function gopen()
{
	var box = $('#quick_contact_wrap');
	$('#goverlay').show();
	box
		.show()
		.css('left', (($(window).width()/2) - (box.width()/2))+'px')
		.css('top', $(window).scrollTop() + 0 + 'px');
	$('#flash-outer').hide();
	$('#flashlogo').hide();
	return false;
}

function gclose()
{
	$('#goverlay').hide();
	$('#quick_contact_wrap').hide();
	$('#flash-outer').show();
	$('#flashlogo').show();
	return false;
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
