//Init 'Unobtrusive' JavaScript
addEvent(window, 'load', function() {

	//Handle dropdown navigation

	var item_height = 29;
	var item_margin = 1;

	var hideshownav = document.getElementById("navigation");
	var hideshownavitem = hideshownav.getElementsByTagName("div");

	var item_heights = [];

	for (i=0; i<hideshownavitem.length; i++) {

		if (hideshownavitem[i].className == "dropdown") {

			var item_total = hideshownavitem[i].getElementsByTagName("a").length;

			//hideshownavitem[i].style.height = (item_total * item_height) + (item_total * item_margin) - item_margin + "px";

			var curritem_height = (item_total * (item_height + item_margin)) - item_margin + "px";

			item_heights[i] = curritem_height;

			hideshownavitem[i].id = "hideshownav_" + i;

			hideshownavitem[i].onmouseover = function() {
				//this.style.overflow = "visible";

				var re = new RegExp("^hideshownav_(.{1,3})$", "i");
				var mymatch = re.exec(this.id);
				var id_no = mymatch[1];

				this.style.height = parseInt(item_heights[id_no], 10) + "px";
			}

			hideshownavitem[i].onmouseout = function() {
				//this.style.overflow = "hidden";
				this.style.height = item_height + "px";
			}

		}

	}

	//Side Form exists
	if ($("formquote_side") != null) {

		//Email to aweber
		if ($("quote_email") != null) {

			//On email blur (ie email entered)
			$("quote_email").onblur = function() {

				//If email specified
				if ($("quote_email").value.length != 0) {

					//Set aweber form target to the iframe
					$("aweberfrm").target = "aweberiframe";

					//Pass the aweber form the email address
					$("aweber_from").value = $("quote_email").value;

					//Pass the aweber form the name
					$("aweber_name").value = $("quote_forename").value + " "+ $("quote_surname").value;

					//Post data to the iframe
					$("aweberfrm").submit();

				}

			}

		}

		$("formquote_side").onsubmit = function() {

			var checkfields = {
				"quote_amount_owed": "Please fill in \"Total Amount Owed\"",
				"quote_no_debts": "Please fill in \"Number of Debts\"",
				"quote_net_income": "Please fill in \"Monthly Net Income\"",
				"quote_expenses": "Please fill in \"Monthly Expenses\"",
				"quote_nature_income": "Please fill in \"Nature of Income\"",
				"quote_postcode": "Please fill in \"Postcode\"",
				"quote_address_1": "Please fill in \"Address line 1\"",
				"quote_town": "Please fill in \"Town\"",
				"quote_recidency_status": "Please fill in \"Residency Status\"",
				"quote_forename": "Please fill in \"Forename\"",
				"quote_surname": "Please fill in \"Surname\"",
				//"quote_email": "Please fill in \"Email address\"",
				"quote_homeworkphone": "Please fill in \"Home/Work Tel.\"",
				"quote_mobile": "Please fill in \"Mobile Tel.\"",
				"quote_besttime": "Please fill in \"Best contact time\""
			};

			var errormsgs = "";
			for (i in checkfields) {

				if ($(i).value == "") {
					errormsgs += checkfields[i] + "\n\n";
				}

			}

			var checkfields_decimal = {
				"quote_amount_owed": "Please enter decimals only for \"Total Amount Owed\"",
				"quote_net_income": "Please enter decimals only for \"Monthly Net Income\"",
				"quote_expenses": "Please enter decimals only for \"Monthly Expenses\""
			};
			for (i in checkfields_decimal) {
				if ($(i).value != "") {

					var re = new RegExp("[^0-9\.\,]", "i");
					if ($(i).value.match(re)) {
						errormsgs += checkfields_decimal[i] + "\n\n";
					}

				}
			}

			errormsgs = rtrim(errormsgs);

			if (errormsgs) {
				alert(errormsgs);
				return false;
			} else {
				return true;
			}

		};

		/*
		//Show additional address fields
		$("quote_postcode").onblur = function() {

			//Show address
			showaddress();

		};

		//Show address if needed
		showaddress();
		*/

		//Total owed Popup

		var quote_owed_counter = 0;

		$("quote_amount_owed_help").onmouseover /*= $("quote_net_income").onfocus*/ = function() {

			quote_owed_counter++;
			if (quote_owed_counter == 1) {
				classaddremove($("quote_amount_owed_popup"), "formpopup-hidden", false);

				//Show/hide relativly positioned block due to ie underlay bug with popup
				$("block_income").style.visibility = "hidden";
				$("block_expenses").style.visibility = "hidden";

			}

		};

		$("quote_amount_owed_help").onmouseout /*= $("quote_net_income").onblur*/ = function() {

			quote_owed_counter--;
			if (quote_owed_counter == 0) {
				classaddremove($("quote_amount_owed_popup"), "formpopup-hidden", true);

				//Show/hide relativly positioned block due to ie underlay bug with popup
				$("block_income").style.visibility = "visible";
				$("block_expenses").style.visibility = "visible";

			}

		};

		$("quote_amount_owed_help").onclick = function() {
			return false;
		};


		//Monthly Net Income Popup

		var quote_net_counter = 0;

		$("quote_net_income_help").onmouseover /*= $("quote_net_income").onfocus*/ = function() {

			quote_net_counter++;
			if (quote_net_counter == 1) {
				classaddremove($("quote_net_income_popup"), "formpopup-hidden", false);
			}

		};

		$("quote_net_income_help").onmouseout /*= $("quote_net_income").onblur*/ = function() {

			quote_net_counter--;
			if (quote_net_counter == 0) {
				classaddremove($("quote_net_income_popup"), "formpopup-hidden", true);
			}

		};

		$("quote_net_income_help").onclick = function() {
			return false;
		};


		//Monthly Expenses Popup

		var quote_expenses_counter = 0;

		$("quote_expenses_help").onmouseover /*= $("quote_expenses").onfocus*/ = function() {

			quote_expenses_counter++;
			if (quote_expenses_counter == 1) {
				classaddremove($("quote_expenses_popup"), "formpopup-hidden", false);
			}

		};

		$("quote_expenses_help").onmouseout /*= $("quote_expenses").onblur*/ = function() {

			quote_expenses_counter--;
			if (quote_expenses_counter == 0) {
				classaddremove($("quote_expenses_popup"), "formpopup-hidden", true);
			}

		};

		$("quote_expenses_help").onclick = function() {
			return false;
		};

	}

});

/*
//Show address if postcode entered
function showaddress() {

	//If there is data in the poscode field
	if ($("quote_postcode").value.length > 0) {

		$("address_block").style.display = "block";

	}

}
*/

function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}

function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}
