﻿// This file requires jQuery to work.
// The CLV version of jQuery is patched with this patch:
// http://groups.google.com/group/jquery-en/browse_frm/thread/f5c36c05cfa43e9a/90e3dfd019086772?#90e3dfd019086772
/// <reference path="jquery-vsdoc.js" />
var myXHR;
var myXML;

$(document).ready(violationInit);

function violationInit() {
	$('#totalFeeNote').show('fast');

	myXHR = $.ajax({
		url: "/includes/xml/Fine_and_bail_schedule.xml",
		cache: false,
		dataType: "xml",
		success: getRows
	});

	$('#formLookup').submit(validateFormLookup);

	$('#vCodeLookup').change(function() { getCodeDropdown(this) });

	//$('#vCodeDoSearch').click(function() { getCodeDirect() });
	$('#vCodeDoSearch').click(getCodeDirect);

	//alert(window.location.search.substring(1));
}

function getRows(xmlDoc, status) {
	//alert(myXHR.getResponseHeader("Content-Type"));
	myXML = xmlDoc;
	$(myXML).find('Row').each(addRow);
}

function addRow() {
	var results = "";
	var firstCell = $(this).find('Cell:first');
	results = firstCell.next().text();
	results += " ";
	results += "(" + firstCell.text() + ")";
	$('<option></option>').html(results).attr("value", firstCell.text()).appendTo('#vCodeLookup');
}

function getCodeDirect() {
	var vCode = $('#vCodeSearch').val();
	if (vCode.length === 4) {
		$('#vCodeLookup').attr('selectedIndex',0);
		setTable(vCode);
	}
	else {
		alert('All "Violation Codes" are 4 characters long.\nPlease enter a correct "Violation Code".');
	}
}

function getCodeDropdown(mySelect) {
	var vCode = $(mySelect).val();
	$('#vCodeSearch').val('');
	setTable(vCode);
}

function clearTable() {
	$('#vCode').html('&nbsp;');
	$('#vDescription').html('&nbsp;');
	$('#vDetails').html('&nbsp;');
	
	// Mandatory Court 
	$('#vCourt').html('&nbsp;');
	document.getElementById('vCourtNote').style.display = 'none';
	$('#mcnText').html('');
	$('#mandatoryCourtNote').hide('normal');
	
	// Violation Dismissable
	$('#vDismissible').html('&nbsp;');
	document.getElementById('vDismissibleNote').style.display = 'none';
	$('#proofOfComplianceNote').hide('normal');

	$('#vProof').html('&nbsp;');
	$('#vTotalWithProof').html('&nbsp;');

	// Traffic School
	$('#vTraffic').html('&nbsp;');
	document.getElementById('vTrafficNote').style.display = 'none';
	$('#trafficSchoolNote').hide('normal');

	$('#vTotalNoProof').html('&nbsp;');
	$('#vTotalSecond').html('&nbsp;');
}

function setTable(myCode) {
	// Clear the table
	clearTable();
	
	if (myXML) {
		var myRow = $(myXML).find('Cell:first-child:contains(' + myCode + ')').parent();
		var currentCell = myRow.find('Cell:first');
		var currentCellText = '';
		
		//Violation Code
		$('#vCode').html(currentCell.text());
		
		//Violation Description
		currentCell = currentCell.next();
		$('#vDescription').html(currentCell.text());

		//Violation Details
		currentCell = currentCell.next();
		$('#vDetails').html(currentCell.text());
		
		//Mandatory court notice section
		currentCell = currentCell.next();
		currentCellText = currentCell.text();
		$('#vCourt').html(currentCellText);
		if (currentCellText.search(/yes/i) >= 0) {
			$('#mcnText').html(currentCellText.substring(6));
			document.getElementById('vCourtNote').style.display = 'inline';
			$('#mandatoryCourtNote').show('normal');
		}
		
		//Violation is dismissible with proof of compliance
		currentCell = currentCell.next();
		currentCellText = currentCell.text();
		$('#vDismissible').html(currentCellText);
		if (currentCellText.search(/yes/i) >= 0) {
			document.getElementById('vDismissibleNote').style.display = 'inline';
			$('#proofOfComplianceNote').show('normal');
		}
		
		//Proof required
		currentCell = currentCell.next();
		$('#vProof').html(currentCell.text());
		
		//Firt offense with proof
		currentCell = currentCell.next();
		$('#vTotalWithProof').html(currentCell.text());

		//Traffic school option section
		currentCell = currentCell.next();
		currentCellText = currentCell.text();
		$('#vTraffic').html(currentCellText);
		if (currentCellText.search(/yes/i) >= 0) {
			document.getElementById('vTrafficNote').style.display = 'inline';
			$('#trafficSchoolNote').show('normal');
		}
		
		//First offense without proof
		currentCell = currentCell.next();
		currentCellText = currentCell.text();
		currentCellText = currentCellText.replace(/B\)/, '<br />B)');
		currentCellText = currentCellText.replace(/C\)/, '<br />C)');
		$('#vTotalNoProof').html(currentCellText);
		
		//Second offense total
		currentCell = currentCell.next();
		$('#vTotalSecond').html(currentCell.text());
	}
}

function validateFormLookup() {
	getCodeDirect();
	//Return false to keep the page from posting
	return false;
}
