// THIS FUNCTION TAKES THE INPUT OF A TEXT FIELD (THE SEARCHID) AND RUNS AN AJAX REQUEST
// TO A PHP SCRIPT (THEURL) WHICH LISTS THE RESULTS IN A DIV (THERESULTSID)
function fetchResultsAddress(thesearchid, theresultsid, theurl) {
	var searchtext = $('#' + thesearchid).val();
	$.ajax({
		type: "GET",
		url: theurl,
		dataType: "text",
		data: "search=" + searchtext,
		success: function(data){
			
			var theresults;
			
			if (data != "") {
				$('#' + theresultsid).html(data);
				$('#' + theresultsid).css('display', 'block');
			} else {
				$('#' + theresultsid).css('display', 'none');
			}
			
			$("#" + theresultsid + " > ul > li > a").live('click', function() { return fillResultsAddress($(this)); });
			$("#" + theresultsid + " > ul > li > a").live('keyup', function() { return fillResultsTypeAddress($(this)); });
			$("#" + theresultsid).live('blur', function() { return hideResultsAddress(); });
			
			function hideResultsAddress() {
				$('#' + theresultsid).css('display', 'none');
			}
			
			function fillResultsAddress(el) {
				var theresults = $(el).attr('title');
				$('#' + thesearchid).val(theresults);
				$('#' + thesearchid).focus();
				
				hideResultsAddress();
				return false;
			}
			
			function fillResultsTypeAddress(theelement, thesecondelement) {
				var theresults = theelement.title;
				$('#' + thesearchid).val(theresults);
				if (theelement.KeyCode == 13 || theelement.KeyCode == 32) {
					hideResultsAddress();
				}
				return false;
			}
		}
	});
	return false;
}

// THIS FUNCTION SUBMITS A FORM (FORM) VIA AJAX TO A URL (THEURL) WHICH THEN RETURNS A MESSAGE
// TO A DIV (MESSAGEDIALOGUE)
function ajaxSubmit(theform, theurl) {
	var str = $(theform).serialize();
	$.ajax({
		type: "POST",
		url: theurl,
		dataType: "text",
		data: str,
		success: function(data){

			var howlong = "5000";
			$("#messagedialogue").css("background-color", "#fff6bf");
			$("#messagedialogue").css("border", "solid 1px #ffd324");
			
			$("#messagedialogue").slideDown("slow");
			$("#messagedialogue").text(data);
			
			function fadeAway() {
				$("#messagedialogue").slideUp("slow");
			}
			setTimeout(fadeAway, howlong);
		},
		failure: function(data){
			$("#messagedialogue").slideUp("slow");
			$("#messagedialogue").text("Whoops. It appears that you're offline.");
			
			function fadeAway() {
				$("#messagedialogue").slideUp("slow");
			}
			setTimeout(fadeAway, 5000);
		}
	});
	return false;
}


// THIS FUNCTION ADDS THE ADDRESS OF AN INDIVIDUAL TO OTHER DROPDOWNS ON THE FORM
function showSelectedAddressCheck(theaddressid, theaddressdiv, theaddressstreet, theaddresssuburbstatepostcode, theclass) {
	$('#' + theaddressid).bind('click keyup change', function() { showSelectedAddress(theaddressid, theaddressdiv, theaddressstreet, theaddresssuburbstatepostcode, theclass); });
	$('#' + theaddressstreet).bind('click keyup blur', function() { showSelectedAddress(theaddressid, theaddressdiv, theaddressstreet, theaddresssuburbstatepostcode, theclass); });
	$('#' + theaddresssuburbstatepostcode).bind('click keyup blur', function() { showSelectedAddress(theaddressid, theaddressdiv, theaddressstreet, theaddresssuburbstatepostcode, theclass); });
	$(document).ready(function() { showSelectedAddress(theaddressid, theaddressdiv, theaddressstreet, theaddresssuburbstatepostcode, theclass); });

	// THIS FUNCTION TAKES THE CURRENTLY SELECTED DROPDOWN OR MANUALLY ENTERED ADDRESS AND COPIES IT TO
	// ANY OTHER DROPDOWNS WITH AN ENTERED CLASS
	function showSelectedAddress(taid, tad, tas, tassp, tc) {
		if ($('#' + taid)[0].selectedIndex == "2") {
			var x = $('#' + tas).val() + ", " + $('#' + tassp).val()
			x = x.toUpperCase();
			if (x == ", ") {
				x = "None";
			}
			$('.' + tc).text(x);
			
		} else {
			$('#' + tad).css('display', 'none');
		}
		if ($('#' + taid)[0].selectedIndex > "3") {
			var x = $('select#' + taid + ' option:selected').text();
			$('.' + tc).text(x);
		}
	}
}


// THIS FUNCTION TOGGLES THE DISPLAY OF A DIV ACCORDING TO THE SELECTED INDEX OF A DROPDOWN
function dropdownDivToggle(dropdown, divtotoggle, theindex) {
	var theindex = parseInt(theindex);
	
	$('#' + dropdown).bind('click keyup change', function() { dropdownDivCheck(dropdown, divtotoggle, theindex); });
	$(document).ready(function() { dropdownDivCheck(dropdown, divtotoggle, theindex); });
	
	function dropdownDivCheck(dd, dtt, ti) {
		if ($('#' + dd)[0].selectedIndex == ti) {
			$('#' + dtt).css('display', 'block');
		} else {
			$('#' + dtt).css('display', 'none');
		}
	}
}


// THIS FUNCTION TOGGLES THE DISPLAY OF 2 DIVs ACCORDING TO THE SELECTED INDEX OF A DROPDOWN
function doubledropdownDivToggle(dropdown, div1, div2, theindex) {
	
	$('#' + dropdown).bind('click keyup change', function() { dropdownDivCheck(dropdown, div1, div2, theindex); });
	$(document).ready(function() { dropdownDivCheck(dropdown, div1, div2, theindex); });
	
	function dropdownDivCheck(dd, d1, d2, ti) {
		if ($('#' + dd)[0].selectedIndex == ti) {
			$('#' + d1).css('display', 'block');
			$('#' + d2).css('display', 'none');
		} else {
			$('#' + d1).css('display', 'none');
			$('#' + d2).css('display', 'block');
		}
	}
}

// THIS FUNCTION TOGGLES THE DISPLAY OF 2 DIVs ACCORDING TO THE SELECTED INDEX OF A DROPDOWN
function doublehideDivToggle(dropdown, div1, div2, theindex) {
	
	$('#' + dropdown).bind('click keyup change', function() { dropdownDivCheck(dropdown, div1, div2, theindex); });
	$(document).ready(function() { dropdownDivCheck(dropdown, div1, div2, theindex); });
	
	function dropdownDivCheck(dd, d1, d2, ti) {
		if ($('#' + dd)[0].selectedIndex == ti) {
			$('#' + d1).css('display', 'none');
			$('.' + d2).css('display', 'none');
		} else {
			$('#' + d1).css('display', 'block');
			$('.' + d2).css('display', 'block');
		}
	}
}

// THIS SCRIPT HIDES THE REMAINING SHAREHOLDERS FIELDS IF THE SHARES ARE HELD JOINTLY. IT ALSO DEFAULTS THE SHARE
// VALUE TO 1.00
function sharesheldjointlyToggle(theid, thediv, thevalue) {
	$('#' + theid).bind('click keyup change', function() { sharesheldjointlyCheck(theid, thediv, thevalue); });
	$(document).ready(function() { sharesheldjointlyCheck(theid, thediv, thevalue); });
	
	function sharesheldjointlyCheck(tid, td, tv) {
		if ($('#' + tid)[0].selectedIndex == "0") {
			$('#' + td).css('display', 'block');
		} else {
			$('#' + td).css('display', 'none');
		}
		if ($('#' + tv).val() == "") {
			$('#' + tv).val('1.00');
		}
	}
}

// SHOW A DIALOG WITH AN 'OK' BOX
function showDialog(message, title, xwidth, xheight) {

	if (xheight === undefined) {
		xheight = 140;
	}
	
	if (xwidth === undefined) {
		xwidth = 360;
	}
	
	$("#dialog-confirm p").text(message);
    $("#dialog-confirm").attr("title", title);
    
    $("#dialog-confirm").dialog({
		height: xheight,
		width: xwidth,
		modal: true,
		buttons: {
			OK: function() {
				$(this).dialog('close');
				return false;
			}
		}
	});
	
	return false;
}

function isDropdownValid(id) {
	
	var selectedOption = $("#" + id + " option:selected");
	
	if ($(selectedOption).val() == "" || $(selectedOption).val() == "FAKE" || $(selectedOption).val() == "fake" || $(selectedOption).val() == "Fake" || $(selectedOption).val() == "blank") {
		return false;
	} else {
		return true;
	}
}

// RETURN WHETHER OR NOT A COMPANY IS SELECTED FROM THE INDIVIDUAL DROPDOWN
function companySelected(prefix) {
	if ($("#" + prefix + "_existingindividual option:selected").attr("rel") == "Company") {
		return true;
	} else if ($("#" + prefix + "_existingindividual option:selected").val() == "New") {
		if ($("#" + prefix + "_type").val() == "Company") {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

// RETURN WHETHER OR NOT A INDIVIDUAL IS SELECTED FROM THE INDIVIDUAL DROPDOWN
function individualSelected(prefix) {
	if ($("#" + prefix + "_existingindividual option:selected").attr("rel") == "Individual") {
		return true;
	} else if ($("#" + prefix + "_existingindividual option:selected").val() == "New") {
		if ($("#" + prefix + "_type").val() == "Individual") {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

//RETURN WHETHER OR NOT ANY POSITIONS HAVE BEEN SELECTED
function positionsSelected(minCheckboxNumber) {
	if (minCheckboxNumber == null) {
		minCheckboxNumber = 0;
	}
	var positions = $(".positionscontainer input:checked:visible");
	if (positions.length > minCheckboxNumber) {
		return true;
	} else {
		return false;
	}
}

function dateOfBirthCheck(id, type, dialogheight, dialogwidth) {
	$.ajax({
		type: "POST",
		url: "dateofbirth_saved.php",
		data: "id=" + id + "&type=" + type,
		success: function(data) {
			if (data != "") {
				
				$("#dialog-confirm p").html(data);
			    $("#dialog-confirm").attr("title", "Missing Date of Birth Information");
				
				$("#dialog-confirm").dialog({
					height: dialogheight,
					width: dialogwidth,
					modal: true
				});
			}
		}
	});
}

$(document).ready(function() {
	$(".addressautocomplete").autocomplete({
		source: "address_fetch.php",
		delay: 100
	});
	$(".goback").bind("click", function() { history.go(-1); });
	
	$("input:submit, input:button, a.fancybutton, .submitbutton").button();
	
	$(".datepicker").datepicker({
		changeYear: true,
		changeMonth: true,
		dateFormat: 'dd/mm/yy'
	});
});