function getParameterByName( name ){  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  var regexS = "[\\?&]"+name+"=([^&#]*)";  var regex = new RegExp( regexS );  var results = regex.exec( window.location.href );  if( results == null )    return "";  else    return results[1];}
var isValidEmailAddress = function(emailAddress) { var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i); return pattern.test(emailAddress); };

$(function() {
	
	var services = new Array(	
		new Array(),
		new Array(
				"Commercial Debt Collection",
				"Consumer Debt Collection",
				"Government Debt Collection"
		),
		new Array(
				"Design forms, procedures, letters",
				"Reminder calls & letters",
				"Credit reporting & searches",
				"Complete outsource quote"
		),
		new Array(
				"Legal debt recovery",
				"Commercial law",
				"Property law & conveyancing"
		),
		new Array(
				"Field calls / Skip tracing",
				"Business background inquiries",
				"Individual background inquiries and identity verification"
		)		
	);
	
	var optionFormat = "<option>text</option>";
	
	$("#service").change(function() {
		
		var index = $(this).attr("selectedIndex");
		
		optionHtml = optionFormat.replace(/text/, " - Select Service Type -");
		$.each(services[index], function(index, value){
			optionHtml += optionFormat.replace(/text/, value);
		});
		
		$("#service-type").html(optionHtml);
		
		if ($("#service").attr("selectedIndex")>2) {
			$("#no-matters-container,#average-debt-container").slideUp();
		} else {
			$("#no-matters-container,#average-debt-container").slideDown();
		}
		
	});
	
	
	
	$("#request-quote").click(function() {
		
		$("#invalid-fields").slideUp();
		$("#form-error").slideUp();
		$("#form-success").slideUp();
		
		
		var ignoreIds = "fax,additional-info";
		
		$("div.form-row").attr("class", "form-row");
		
		var valid = true;
		$("select").each(function() {
			if (!$(this).attr("selectedIndex")) {
				$(this).parent().addClass("form-row-error");
				valid = false;
			}
		});
		
		if ($("#service").attr("selectedIndex")>2) {
			ignoreIds += ",no-matters,average-debt";
		}
				
		$("input").each(function() {
			var id = $(this).attr("id");
			if (ignoreIds.indexOf(id)<0) {
				if (!$(this).val()) {
					$(this).parent().addClass("form-row-error");
					valid = false;					
				}
			}
		});
		
		var emailValid = isValidEmailAddress($("#email").val());
		if (!emailValid) {
			$("#email").parent().addClass("form-row-error");
			valid = false;
		}
		
		if (valid) {
			
			$("#quote-form").slideUp();
			
			var data = "";
			$("select,input,textarea").each(function() {
				data += $(this).attr("id") + "=" + $(this).val() + "&";
			});	
			
			data += "time=" + (new Date).getTime() + "&";
			data += "c=SendQuote";
			
			$.ajax({
				type: "GET",
				url: "Requests/Request.aspx",
				data: data,
				success: function(data, textStatus){
					$("#form-success").slideDown();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					$("#form-error").slideDown();
					$("#quote-form").slideDown();
				}
			});			
		} else {
			$("#invalid-fields").slideDown();
			$("#quote-form").slideDown();
		}
		
		return false;
	
	});
	
	$("#contact-link").click(function() {

		$("#invalid-fields").slideUp();
		$("#form-error").slideUp();
		$("#form-success").slideUp();		
		
		var ignoreIds = "fax,additional-info";
		
		$("div.form-row").attr("class", "form-row");
		
		var valid = true;
		$("select").each(function() {
			if (!$(this).attr("selectedIndex")) {
				$(this).parent().addClass("form-row-error");
				valid = false;
			}
		});
				
		$("input").each(function() {
			var id = $(this).attr("id");
			if (ignoreIds.indexOf(id)<0) {
				if (!$(this).val()) {
					$(this).parent().addClass("form-row-error");
					valid = false;					
				}
			}
		});
		
		var emailValid = isValidEmailAddress($("#email").val());
		if (!emailValid) {
			$("#email").parent().addClass("form-row-error");
			valid = false;
		}
		
		if (valid) {
			
			$("#contact-form").slideUp();
			
			var data = "";
			$("select,input,textarea").each(function() {
				data += $(this).attr("id") + "=" + $(this).val() + "&";
			});	
			
			data += "time=" + (new Date).getTime() + "&";
			data += "c=SendEnquiry";
			
			$.ajax({
				type: "GET",
				url: "Requests/Request.aspx",
				data: data,
				success: function(data, textStatus){
					$("#form-success").slideDown();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					$("#form-error").slideDown();
					$("#contact-form").slideDown();
				}
			});			
		} else {
			$("#invalid-fields").slideDown();
			$("#contact-form").slideDown();
		}
		
		return false;
	
	});
	
	$("#menu a").each(function(){
	
		var dropdownid = $(this).attr("id").replace(/menu\-link/, "drop-down");
		if (dropdownid) {
			var dropdown = $("#" + dropdownid);
			if (dropdown.size()>0) {
				
				var hideDropdown = function() {
					dropdown.hide();
				};
				
				var dropdownTimer = null;
				
				$(this).mouseenter(function() {
					if (dropdownTimer) { clearTimeout(dropdownTimer); }
					var pos = $(this).offset();
					var x = pos.left + 0;
					var y = pos.top + 34;
					dropdown.css({
						"left": x + "px",
						"top": y + "px"
					});
					dropdown.show();
				});
				
				$(this).mouseleave(function() {
					if (dropdownTimer) { clearTimeout(dropdownTimer); }
					dropdownTimer = setTimeout(hideDropdown, 500);
				});
				
				dropdown.mouseenter(function() {
					if (dropdownTimer) { clearTimeout(dropdownTimer); }
				});

				dropdown.mouseleave(function() {
					if (dropdownTimer) { clearTimeout(dropdownTimer); }
					dropdownTimer = setTimeout(hideDropdown, 500);
				});
				
			}
		}		
	
	});
	
	var infoboxes = $(".four-columns .column-content p");
	var maxh = 0;
	if (infoboxes.size() > 0) {
		setTimeout(function(){
			infoboxes.each(function() {
				var h = $(this).height();
				if (maxh==0) { maxh = h; }
				if (h>maxh) { maxh = h; }
			});
			if (maxh>0) {
				infoboxes.css("height", maxh+"px");
			}
		}, 500);
	}
	
	$("#service").trigger("change");
	
	
	
});


