function lodgeComplaint() {
	// setup resize/scroll callbacks if we haven't already
	window.question = "start";
	if(!window.lodgedOnce) {
		$(window).scroll(function(){
			$(window).resize();
		}).resize(function() {
			window.overlay.css({
				height: $(window).height(),
				width: $(window).width(),
				top: $(window).scrollTop(),
				left: $(window).scrollLeft()
			});
			
			window.popup.css({
				'top': $(window).height()/2+$(window).scrollTop()-$(window.popup).outerHeight()/2,
				'left': $(window).width()/2+$(window).scrollLeft()-$(window.popup).outerWidth()/2
			});
		});
		window.lodgedOnce = true;
	}

	// create overlay
	var overlay = window.overlay = $(document.body).append("<div id='overlay'></div>").find("#overlay").hide();
	overlay.css("opacity",0.8).click(function(){
		$("#overlay").remove();
		$("#vettingpopup").remove();
	}).fadeIn("fast");
	
	// create popup
	var popup = window.popup = $(document.body).append("<div id='vettingpopup'></div>").find("#vettingpopup").hide();
	popup.fadeIn("fast");
	popup.html("<h2></h2><div class='text'></div><div class='buttons'><span class='pleaseAnswer'>Please choose an answer before continuing.</span> <input type='button' rel='close' value='Cancel' /> <input type='button' rel='back' value='Back' /> <input type='button' rel='next' value='Continue' /></div>").show();
	
	popup.find("h2").html("Can we consider your complaint?");
	popup.find(".text").html("<p>The following questions are designed to identify if the Advertising Standards Board is able to consider the matters you have raised.</p> <p>Before answering these questions, we recommend you read through our FAQs .  The FAQs provide information about the charter of the Advertising Standards Board and outlines issues that the Board can and cannot consider.</p> <p>We also suggest that before answering the questions you <a href=\"/casereports/determinations\">click here</a> to see if we have already considered complaints about the advertisement that is of concern to you. Please also see our <a href=\"/legal\">Privacy Policy</a>.</p> <p>If after reading this information you believe your complaint is within the ASB charter, please proceed.</p> <p>Answering these questions will help to make sure your complaint is one that the ASB can consider.</p>");
	
	popup.find("[rel=close]").click(function() {
		$(window.overlay).remove();
		$(window.popup).remove();
	});
	
	popup.find("[rel=back]").click(function() {
		// display the question
		var question = window.vettingQuestions[window.question];
		window.popup.find("h2").html(question.title);
		window.popup.find(".text").html("<div class='question'>"+question.question+"</div><div class='response'><p><label><input type='radio' name='questionResponse' value='yes' /> Yes</label></p><p><label><input type='radio' name='questionResponse' value='no' /> No</label></p></div>")
		$(window).resize();
		popup.find("[rel=close]").val("Cancel")
		popup.find("[rel=back]").hide();
		popup.find("[rel=next]").show();
	}).hide();
	
	popup.find("[rel=next]").click(function() {
		popup.find("[rel=close]").val("Cancel")
		popup.find("[rel=back]").hide();
		popup.find("[rel=next]").show();
		window.popup.find(".pleaseAnswer").hide();
		if(window.question == "start") {
			window.question = 0;
		} else {
			// validate question answer
			var answer = window.popup.find("input[checked=true]").val();
			if(!answer && window.popup.find("input[type=radio]").size()) {
				window.popup.find(".pleaseAnswer").show();
				return false;
			} else {
				var todo = window.vettingQuestions[window.question][answer];
				if(window.showingInfo == true) {
					window.showingInfo = false;
					window.question++;
				} else if(todo == "next") {
					window.question++;
				} else if(todo == "launch") {
					popup.animate({
						width:'817px',
						height:'420px',
						top:$(window).height()/2-450/2+$(window).scrollTop(),
						left:$(window).width()/2-847/2+$(window).scrollLeft()
					});
					popup.html("<iframe src='http://122.99.94.111/01_std_complaint_form.asp?iframe=true'></iframe>");
					return true;
				} else {
					if(window.vettingQuestions[window.question].nextanyway) {
						popup.find(".text").html(todo);
						window.showingInfo = true;
						return false;
					} else {
						popup.find("h2").html("We cannot consider your complaint");
						popup.find(".text").html(todo);
						popup.find("[rel=next]").hide();
						popup.find("[rel=back]").show();
						popup.find("[rel=close]").val("Close");
						return false;
					}
				}
			}
		}
		// display the question
		var question = window.vettingQuestions[window.question];
		window.popup.find("h2").html(question.title);
		window.popup.find(".text").html("<div class='question'>"+question.question+"</div><div class='response'><p><label><input type='radio' name='questionResponse' value='yes' /> Yes</label></p><p><label><input type='radio' name='questionResponse' value='no' /> No</label></p></div>")
		$(window).resize();
	});
	
	$(window).resize();
	return true;
};

function leaveComment()
{
	$(window).scroll(function(){
		$(window).resize();
	}).resize(function() {
		window.overlay.css({
			height: $(window).height(),
			width: $(window).width(),
			top: $(window).scrollTop(),
			left: $(window).scrollLeft()
		});
		
		window.popup.css({
			'top': $(window).height()/2+$(window).scrollTop()-$(window.popup).outerHeight()/2,
			'left': $(window).width()/2+$(window).scrollLeft()-$(window.popup).outerWidth()/2
		});
	});
	var overlay = createOverlay();
	var popup = createPopupcell();
	popup.find("h2").html('');
	popup.find(".text").html('<p>Loading comment form ...</p>');
	popup.animate({
		width:'817px',
		height:'420px',
		top:$(window).height()/2-450/2+$(window).scrollTop(),
		left:$(window).width()/2-847/2+$(window).scrollLeft()
	});
	popup.html("<iframe src='http://adstandards.cre8ive.com.au/skins/main/templates/form.leave_a_comment.php'></iframe>");
}

function createOverlay()
{
	window.overlay = $(document.body).append("<div id='overlay'></div>").find("#overlay").hide();
	window.overlay.css({opacity:0.8, width:'100%', height:'100%', top:0, left:0}).click(function(){
		$("#overlay").remove();
		$("#vettingpopup").remove();
	}).fadeIn("fast");
	return window.overlay;
}

function createPopupcell()
{
	window.popup = $(document.body).append("<div id='vettingpopup'></div>").find("#vettingpopup").hide();
	window.popup.fadeIn("fast");
	window.popup.html("<h2></h2><div class='text'></div><div class='buttons'><span class='pleaseAnswer'>Please choose an answer before continuing.</span></div>").show();
	return window.popup;
}

window.vettingQuestions = [
	{
		'title': "Question 1 of 6",
		'question' : '<p>Is your complaint about one particular advertisement that you can identify?</p>',
		'yes':'next',
		'no':"<p>You must be able to clearly identify the advertisement so that the ASB can investigate your complaint fully. The ASB is not able to investigate complaints if the advertiser, the product advertised and the date/time/location of advertisement cannot be identified.  Please ensure you have as much detailed information as possible to help identify the advertiser, the product and the broadcast/publication details.</p>"
	},
	{
		'title': "Question 2 of 6",
		'question' : '<p>Have you actually seen or heard the specific advertisement?</p>',
		'yes':'next',
		'no':"<p>The ASB will not process complaints if you have not physically seen or heard the specific advertisement or marketing communication.  Campaign or form letter complaints will also not be accepted if you have not actually seen or heard the specific advertisement.  Similarly, complaints based on having seen or heard media reports about an advertisement (including billboards, websites etc) will not be accepted.</p>"
	},
	{
		'title': "Question 3 of 6",
		'question' : '<p>Is your complaint about the product/service being advertised (at any time) rather than the content of the advertisment?</p>',
		'yes':"<p>The role of the ASB is to consider complaints against the content of advertisements and marketing communications. The ASB does not consider whether a product or service is able to be advertised.</p>",
		'no':'next'
	},
	{
		'title': "Question 4 of 6",
		'question' : '<p>Is your complaint about the content of a television program?</p>',
		'yes':"<p>The role of the ASB is to consider complaints against the content of an advertisement or marketing communication.  The ASB's charter does not extend to television program content, a broadcaster's own advertisements for television programs or television program guides.  Complaints of this nature should be referred to the particular broadcaster and FreeTV Australia.</p>",
		'no':'next'
	},
	{
		'title': "Question 5 of 6",
		'question' : '<p>Is your complaint about the characteristics of television advertisements, including the loudness or the frequency of advertisements?</p>',
		'yes':"<p>The role of the ASB is to consider complaints against the content of an advertisement or marketing communication. The ASB does not consider complaints about the loudness or the frequency of advertisements being broadcast.  Complaints of this nature should be referred to the particular broadcaster and/or FreeTV Australia.</p>",
		'no':'next'
	},
	{
		'title': "Question 6 of 6",
		'question' : '<p>Is your complaint about the truth and/or accuracy of claims made in an advertisement?</p>',
		'yes':"<p>The ASB is only able to consider complaints regarding the truth and accuracy of advertising or marketing under specific provisions of the following advertising codes:<span>clause 2.2 of the AANA Code for Advertising & Marketing Communications to Children</span><span>Clauses 2.1, 2.4 and 3.1 of the AANA Food & Beverages Advertising & Marketing Communications Code</span><span>Section 1 of the AANA Environmental Claims in Advertising and Marketing Code</span></p>",
		'nextanyway':true,
		'no':'launch'
	},
	{
		'title': "Question 6a of 6",
		'question' : "<p>Is your complaint about the overall truth and/or accuracy of an advertisement or marketing communication that:<span>is directed at children</span><span>is related to Food and Beverage products</span><span>contains specific Environmental claims?</span></p>",
		'yes':'launch',
		'no': "The role of the ASB is to consider complaints against the content of advertisements and marketing communications. The ASB does not consider general complaints about the truth and accuracy of advertisements or marketing communications, except for specific provisions identified in the codes. Complaints about the general truth and accuracy of advertising claims should be referred to the office of Fair Trading/Consumer Affairs in your State or Territory or the Australian Competition and Consumer Commission."
	}
]

/* case reports search */
$(function() {
	$("#caseReports").each(function() {
		$("#caseReports .expandable").show();
		//$("#advancedSearch").hide();
		//$("#simpleSubmit").css('visibility','visible');
		$("#caseReports a.advanced").click(function(){
			if(this.getAttribute('tog') == 'closed') {
				this.setAttribute('tog',"open");
			} else {
				this.setAttribute('tog',"closed");
			}
			if(this.getAttribute('tog') == 'open') {
				$("#caseReports .expandable").addClass('expanded');
				$("#advancedSearch").show();
				$("#simpleSubmit").css('visibility','hidden');
				$("#advancedSearch .datepicker, #advancedSearch input[type=hidden]").attr("disabled",false)
/* 				$("#advancedSearch input, #advancedSearch select").attr("disabled",false); */
			} else {
				$("#caseReports .expandable").removeClass('expanded');
				$("#advancedSearch").hide();
				$("#simpleSubmit").css('visibility','visible');
				$("#advancedSearch .datepicker, #advancedSearch input[type=hidden]").attr("disabled",true)
			}
		});
		$("#advancedSearch").hide();
		$("#caseReports a.advanced").attr('tog','open');
/* 		if($("#advancedOn").val() != 'true') { */
			$("#caseReports a.advanced").click();
/* 		} */
		
		$("#caseReports .datepicker").datepicker({
			dateFormat: 'dd/mm/yy'
		});
	});
});
/* case report browse */
$(function() {
	$("#caseBrowser .title").addClass("expandable").each(function() {
		if(this.getAttribute('rel') == 'on') {
			this.setAttribute('tog','open');
			$(this).addClass("expanded");
		} else {
			this.setAttribute('tog','closed');
			$(this).next().hide();
		}
	}).click(function() {
		if(this.getAttribute('tog') == 'on') {
			this.setAttribute('tog','off');
			$(this).removeClass("expanded").next().hide();
		} else {
			this.setAttribute('tog','on');
			$(this).addClass("expanded").next().show();
		}
	});
	$("a[href=javascript:lodgeComplaint();]").click(function() {
		lodgeComplaint();
		return false;
	})
});
function sendtofriend() {
	window.open("/sendtofriend/?url="+window.location.href,"sendToFriend","menubar=1,resizable=1,width=534,height=600");
}
