function addReportLink(obj, county_id, link_url) {
	reportLink.select(obj, county_id, link_url);
	ReportLink_operation = '0';
}

// everything below here is for the popup blog comment form
ReportLink_thisWindow = null;
ReportLink_thisObject = null;
ReportLink_link_url = null;
ReportLink_button_id = null;
ReportLink_county_id = null;
ReportLink_operation = '0';	// 0=blog post 1=contact form 2=notify list 3=remove notify list

function ReportLink_show(anchorname) {
	this.showPopup(anchorname);
}

function ReportLink_doSubmit() {

	var link_url = window.ReportLink_link_url;
	var button_id = window.ReportLink_button_id;
	var county_id = window.ReportLink_county_id;
	var name = document.forms.reportform.elements['bcname'].value;
	var email = document.forms.reportform.elements['bcemail'].value;
	var comment = document.forms.reportform.elements['bccomment'].value;

	var errs = '';
	var errcnt = 0;

	var operation=ReportLink_operation;

	if (operation == '0' || operation == '1' || operation == '2') {
		// validate name
		var validRegExp = /^[a-z -_]+$/i;
		if (name.search(validRegExp) == -1) {
			errcnt++;
			errs += errcnt+') Please enter a valid name\n';
		}
	}

	// validate email
	validRegExp = /^([_+a-z0-9-]+\.)*[_+a-z0-9-]+@([_+a-z0-9-]+\.)*([_+a-z0-9-]+\.)*[a-z0-9-]+\.[a-z]{2,}$/i;

    if (email.search(validRegExp) == -1) {
		errcnt++;
		errs += errcnt+') Please enter a valid email address\n';
	}

	if (operation == '0' || operation == '1') {
		// validate comment
		if (comment.length === 0) {
			errcnt++;
			errs += errcnt+') Please enter a comment\n';
		}
		else {

			// no html allowed
			var validRegExp = /<[^>]*>/;

			// validate comment
			if (comment.search(validRegExp) != -1) {
				errcnt++;
				errs += errcnt+') Sorry, no HTML is allowed in comment\n';
			}
		}
	}

	if (errcnt) {
		alert('THERE WERE SOME ERRORS IN THE FORM\n'+errs);
		return false;
	}

	var query = 'county_id='+county_id+'&link_url='+link_url+'&name='+name+'&email='+email+'&stamp='+stamp+'&operation='+operation+'&comment='+comment+'&button_id='+button_id;

	ajaxRequest('/traffic_court/reportlink.php', query, reportResponse);

	window.ReportLink_thisWindow.hidePopup();

	return false;
}

function reportResponse() {
	if (xmlhttp.readyState==4) {
		if (xmlhttp.responseText != 'OK')
			alert(xmlhttp.responseText);
		else
			window.location.reload( false );
	}
}

function nothingResponse() {
}

function ReportLink_doCancel() {
	window.ReportLink_thisWindow.hidePopup();
	return false;
}

function ReportLink_select(anchorname, county_id, link_url) {

	window.ReportLink_link_url = link_url;
	window.ReportLink_button_id = anchorname;
	window.ReportLink_county_id = county_id;

	this.show(anchorname);

	// clear the form
	document.forms.reportform.elements['bcname'].value='';
	document.forms.reportform.elements['bcemail'].value='';
	document.forms.reportform.elements['bccomment'].value='';

}
	
function ReportLink() {
	var windowMode = false;
	var divname, cp;
	// Create a new PopupWindow object
	if (arguments.length===0) {
		divname = "ReportLinkDiv";
	}
	else if (arguments[0] == "window") {
		divname = '';
		windowMode = true;
	}
	else {
		divname = arguments[0];
	}
	
	if (divname !== "") {
		cp = new PopupWindow(divname);
	}
	else {
		cp = new PopupWindow();
		cp.setSize(500,200);
	}

	// Object variables
	cp.currentValue = "";
	
	// Method Mappings
	cp.show = ReportLink_show;
	cp.select = ReportLink_select;

	// this is our html for window
	var cp_contents = "";
	var windowRef = (windowMode)?"window.opener.":"";

	window.ReportLink_thisWindow = cp;
	window.ReportLink_thisObject = cp;

	if (windowMode) {
		cp_contents += '<HTML><HEAD>\n';
		cp_contents += '<TITLE>Comment or Report Bad Link</TITLE></HEAD>\n';
		cp_contents += '<BODY MARGINWIDTH="0" MARGINHEIGHT="0" LEFTMARGIN="0" TOPMARGIN="0"><CENTER>\n';
	}
	cp_contents += '<form class="reportlinkform" name="reportform" onSubmit="return false;" action="" method="post" autocomplete="off">\n';
	cp_contents += '<table align="center" border="0" cellspacing="0" cellpadding="4">\n';
	cp_contents += '	<tr>\n';
	cp_contents += '		<th colspan="2" id="post_title">Comment or Report Bad Link</th>\n';
	cp_contents += '	</tr>\n';
	cp_contents += '	<tr>\n';
	cp_contents += '		<td colspan="2" id="message_row">In order to limit spam, <strong>no HTML</strong> is allowed in the message.<br>Your email address will remain private.<br><strong>Please complete all required fields</strong>.</td>\n';
	cp_contents += '	</tr>\n';
	cp_contents += '	<tr id="name_row">\n';
	cp_contents += '		<td align="right">Your Name <font color=red>*</font></td>\n';
	cp_contents += '		<td align="left"><input type="text" name="bcname" size="32" maxlength="64"></td>\n';
	cp_contents += '	</tr>\n';
	cp_contents += '	<tr>\n';
	cp_contents += '		<td align="right">Your Email <font color=red>*</font></td>\n';
	cp_contents += '		<td align="left"><input type="text" name="bcemail" size="32" maxlength="64"></td>\n';
	cp_contents += '	</tr>\n';
	cp_contents += '	<tr id="comment_row">\n';
	cp_contents += '		<td colspan="2" align="left">Message <font color=red>*</font><br>\n';
	cp_contents += '		<textarea name="bccomment" cols="48" rows="6"></textarea>\n';
	cp_contents += '		</td>\n';
	cp_contents += '	</tr>\n';
	cp_contents += '	<tr>\n';
	cp_contents += '		<td colspan="2" align="right"><font color=red>*</font> required fields \n';
	cp_contents += '          <input onClick="'+windowRef+'ReportLink_doCancel();" class="submitinput" type="submit" name="Cancel" id="Cancel" value="Cancel">\n';
	cp_contents += '          <input onClick="'+windowRef+'ReportLink_doSubmit();" class="submitinput" type="submit" name="Submit" id="Submit" value="Submit">\n';
	cp_contents += '		</td>\n';
	cp_contents += '	</tr>\n';
	cp_contents += '</table>\n';
	cp_contents += '</form>\n';
	if (windowMode) {
		cp_contents += "</CENTER></BODY></HTML>\n";
	}

	// Write the contents to the popup object
	cp.populate(cp_contents+"\n");
	cp.offsetY = -100;
	cp.offsetX = -200;
	cp.width = 400;
	cp.autoHide();
	return cp;
}

var reportLink = new ReportLink(); 
