/*	Javascript for AJAX reports. */

var xmlhttp = false;

function ajaxFunction(req_type, req_page, req_string)
{

if (window.XMLHttpRequest)
	{
  	// code for IE7+, Firefox, Chrome, Opera, Safari
  	xmlhttp=new XMLHttpRequest();
	}
else if (window.ActiveXObject)
  	{
  	// code for IE6, IE5
  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
else
  	{
  	alert("Your browser does not support XMLHTTP!");
  	}

xmlhttp.onreadystatechange=handle_server_response;

//	.send is what sends the POST data.  Parameter should be null for GET requests.
xmlhttp.open(req_type, req_page, true);

if (req_type == 'POST')
{

	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", req_string.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(req_string);
}
else if (req_type == 'GET')
{
	xmlhttp.send(null);
}
}

function handle_server_response()
{
	if(xmlhttp.readyState == 4)
	{
		if (xmlhttp.status == 200)
		{
			switch(xmlhttp.responseText)
			{
				case 'captcha_error':
					captcha_error();
					break;
				case 'success':
						submission_success();
					break;
				default:
					default_message();
			}
		}
	}
}


function submit_report()
{
	//	Call ajaxFunction from here.  This is where you construct the POST url.
	if (document.getElementById("report_text").value == "")
	{
		alert ("You must include text in your report!  Please try again.");
		return false;
	}

	if (document.getElementById('captcha_code').value == '')
	{
		alert ('You must enter the visual verification code.  Click for new image if you cannot read it.');
		return false;
	}

	var post_string = 'report_type=' + encodeURIComponent(report_type) +
					'&subject=' + encodeURIComponent(subject) +
					'&report_text=' + encodeURIComponent(document.getElementById('report_text').value) +
					'&captcha_code=' + encodeURIComponent(document.getElementById('captcha_code').value);

	document.getElementById('submit_p').innerHTML = '<span style="font-weight: bold;">Sending report, please wait...</span> \
			<img src="/images/loading.gif" />';

	ajaxFunction('POST', submit_page, post_string);
}


//function show_report_form(report_type)
function show_report_form(report_type)
{
	document.getElementById('report_p').style.display = 'none';
	//	IE confuses variable names with DOM IDs it seems.
	var report_ele = document.getElementById('report_div');
	report_ele.style.display = 'block';

	var club_report_p = '<p class="report_legend">If we have inaccurate information about this club or it is closed,\
			please let us know by providing as much relevant information as possible.\
			We will research it and make any necessary changes.</p>';

	var state_report_p = '<p class="report_legend">If we are missing a new strip club, one of our listed clubs has closed, or we\
			have any other type of inaccurate information, please let us know so we can correct it.</p>';

	var form_html = '<textarea id="report_text"></textarea>\
	<p id="captcha_instruction">Enter the verification code in the box below before submitting.  Letters are not case sensitive.</p>\
	<div id="captcha_div"><img id="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image" /> <a class="refresh_captcha" href="#" onclick="document.getElementById(\'captcha\').src = \'/securimage/securimage_show.php?\' + Math.random(); return false">Click For New Image</a>\
	<input type="text" class="captcha_input" name="captcha_code" id="captcha_code" size="10" maxlength="6" /></div>\
	<div class="submit_link" id="submit_link"><p class="submit_p" id="submit_p"><a href="#" onclick="submit_report(); return false;">Submit Report</a></p></div>';

	if (report_type == 'club')
	{
		report_ele.innerHTML = club_report_p + form_html;
	}
	else if (report_type == 'state')
	{
		report_ele.innerHTML = state_report_p + form_html;
	}
}


function captcha_error()
{
	document.getElementById('captcha_div').className = 'error';
	document.getElementById('captcha_instruction').innerHTML = '<span style="font-weight: bold;">You entered an incorrect verification code.  Please try again and re-submit.</span>';
	document.getElementById('captcha_code').value = '';
	document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random();
	document.getElementById('submit_p').innerHTML = '<a href="#" onclick="submit_report(); return false;">Re-submit Report</a>';
}

function submission_success()
{
	document.getElementById('report_div').innerHTML = 'Thank you.  We have received your submission and will research it shortly.';
}

function default_message()
{
	document.getElementById('report_div').innerHTML = 'Error.  Something did not go right.  Please try submitting your report at a later time.';
}
