﻿// Parcel Information Search Script

function makeDoubleDelegate(function1, function2) {
    return function() {
        if (function1)
            function1();
        if (function2)
            function2();
    }
}

function FillDates(control, dateString) {
		control.SetDate(dateString);
}

function hidediv(id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // NS4
			document.id.display = 'none';
		}
		else { // IE4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // NS4
			document.id.display = 'block';
		}
		else { // IE4
			document.all.id.style.display = 'none';
		}
	}
}

function WaitMessage(toHide, toShow) {
	hidediv(toHide);
	showdiv(toShow);
}

//Tax Payment Functions

function onlyNumbers(e) {
	var keynum;
	var keychar;
	var numcheck = /\d|\./;
	var targ;
	
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	var entered = numcheck.exec(String.fromCharCode(code));
	var newAmount = targ.value;
	return entered;
}

function TotalPayAmount() {
	var dTotal = 0.0;
	var reg = /\d{4}$/;
	var totalBox;
	
//	for (var j=0;j<theForm.length;j++) {
//		if (theForm.elements[j].id.match("chkSelect")) {
//			if (theForm.elements[j].checked) {	
//				var match = reg.exec(theForm.elements[j].id);
//				var year = match[0];
//				
				for (var k=0;k<theForm.length;k++) {
					if (theForm.elements[k].id.match("txtAmount") && !theForm.elements[k].disabled) {
						dTotal += parseFloat(theForm.elements[k].value);
						var nfAmount = new NumberFormat(parseFloat(theForm.elements[k].value))
						nfAmount.setSeparators(false);
						nfAmount.setPlaces(2);
						theForm.elements[k].value = nfAmount.toFormatted();
					}
					else if (theForm.elements[k].id.match("txtTotalPayment")) {
						totalBox = theForm.elements[k];
					}
				}
//				
//			}
//		}
//	}
	
	var nf = new NumberFormat(dTotal);
	nf.setSeparators(false);
	nf.setPlaces(2);
	totalBox.value = nf.toFormatted();
	
	return amountValid()
}

function PostPayment() {
	var year = [];
	var roll = [];
	var amount = [];
	var transID;
}

function VerifyPayment() {
	if (TotalPayAmount()) {
		var myForm = document.forms[0];
		myForm.target = "_blank";
		myForm.action = "verify.aspx"
		myForm.submit();
	}
	return false;
}

function amountValid() {
	var isValid = true;
	var targ;
	
	removeErrors();
	
	for (var k=0;k<theForm.length;k++) {
		if (theForm.elements[k].id.match("txtAmount") && !theForm.elements[k].disabled) {
			var amount, year;
			var due = 0.0;
			year = theForm.elements[k].id.match('txtAmount\\d+-(\\d{4})')[1]
			amount = parseFloat(theForm.elements[k].value);
			
			for (var i=0;i<theForm.length;i++) {
				if (theForm.elements[i].id.match("txtDue\\d+-" + year)) {
					if (parseFloat(theForm.elements[i].value) > due)
						due = parseFloat(theForm.elements[i].value);
				}
			}
			
			if (amount > due) {
				createError("Amount entered is more than is due for " + year);
				isValid = false;
			}
		}
		else if (theForm.elements[k].id.match("txtTotalPayment")) {
			var total = parseFloat(theForm.elements[k].value);
			if (total < 1 || total > 5000) {
				createError("Amount must be between 1.00 and 5000.00");
				isValid = false;
			}
		}
	}
	
	return isValid;
}

function createError(text) {
	var error = document.createElement('P');
	var errtext = document.createTextNode(text);
	error.appendChild(errtext);
	
	var node = document.getElementById('payError');
	node.appendChild(error);
	node.className = "error";
}

function removeErrors() {
	var node = document.getElementById('payError');
	while (node.childNodes.length > 0)
		node.removeChild(node.childNodes[0]);

	node.className = "nodisplay";
}

function mouseClick(e) { //1
	var targ;
	
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	
	if (targ.id.match('chkSelect')) { //2
		var matches = targ.id.match('chkSelect(\\d{4})');
		var year = matches[1];
		var line;
		
		if (targ.checked) { //3
			for (var i=0;i<theForm.length;i++) { //4
				if (theForm.elements[i].id.match('radOpt.*' + year)) { //5
					line = theForm.elements[i].id.match('radOpt(\\d+-\\d{4})')[1]
					if (theForm.elements[i].checked) { //6
						for (var j=0;j<theForm.length;j++) { //7
							if (theForm.elements[j].id.match("txtAmount.*" + year)) { //8
								if (theForm.elements[j].id.match("txtAmount" + line))
									theForm.elements[j].disabled = null;
								else
									theForm.elements[j].disabled = 'disabled';
							} //8
						} //7
					} //6
				} //5
			} //4
		} //3
		else { //9
			for (var i=0;i<theForm.length;i++) { //10
				if (theForm.elements[i].id.match('txtAmount.*' + year))
					theForm.elements[i].disabled = 'disabled';
			} //10
		} //9
		
		TotalPayAmount();
	} //2
	else if (targ.id.match('radOpt')) { // 11
		var matches = targ.id.match('radOpt(\\d+-(\\d{4}))');
		var year = matches[2];
		var line = matches[1];
		
		for (var i=0;i<theForm.length;i++) { //12
			if (theForm.elements[i].id.match("txtAmount.*" + year)) { //13
				if (theForm.elements[i].id.match("txtAmount" + line))
					theForm.elements[i].disabled = null;
				else
					theForm.elements[i].disabled = 'disabled';
			} //13
		} //12
		
		TotalPayAmount();
	} //11
} //1
