﻿function BC_GotoPage(obj, page) {
	var form = $(obj).parents("form:first");
	BC_ChangeFormFieldValue(form, "bc_currentPage", page);

	$("div", form).each(function() {
		if ($(this).attr("id") != "") {
			id = $(this).attr("id");
			return false;
		}
	});
	
	BC_AjaxSubmitForm(form, { target: '#' + id, success: ChangeSelect });

	return false;

}

function BC_AjaxSubmitForm(theform, options) {
	$(theform).ajaxSubmit(options);
}

function BC_InitializeSearchCriteria() {
	$.ajax({
		type: "POST",
		url: $("#postUrl").val(),
		data: 'xmlFile=' + $("#xmlFile").val() + '&parentObject=' + $("#parentObject").val(),
		cache: false,
		async: false,
		dataType: "xml",
		success: function(xml) {
			xmlData = xml;
			var totalCriteria = parseInt($("#totalCriteria").val(), 10);

			for (var i = 0; i < totalCriteria; i++) {
				var id = $("#fieldId" + i).val();

				if ($("#fieldValueList" + i).css("display") != "none") {
					var fieldValueList = document.getElementById("fieldValueList" + i);

					var option = new Option("", "");
					fieldValueList.options[0] = option;
					j = 1;

					var lookup = $(xmlData).find("Field[id=" + id + "]").find("Lookup");

					if ($(lookup).length > 0 && $(lookup).attr("type") == "url") {
						var url = baseUrl + $(lookup).attr("url");
						var data = "";

						if (url.indexOf('?') > 0) {
							data = url.substr(url.indexOf("?") + 1);
							url = url.substr(0, url.indexOf("?"));
						}

						$.ajax({
							type: "POST",
							url: url,
							data: data,
							cache: false,
							async: false,
							dataType: "xml",
							success: function(xml) {
								$(xml).find($(lookup).attr("nodeName")).each(function() {
									var option = new Option($(this).attr($(lookup).attr("display")), $(this).attr($(lookup).attr("value")));
									fieldValueList.options[j] = option;
									if (fieldValueList.options[j].value == $("#fieldValue" + i).val())
										fieldValueList.options[j].selected = true;
									j++;
								});
							}
						});
					}
					else {
						$(xmlData).find("Field[id = " + id + "]").find("Lookup").find("Value").each(function() {
							var option = new Option($(this).attr("display"), $(this).attr("name"));
							fieldValueList.options[j] = option;
							if (fieldValueList.options[j].value == $("#fieldValue" + i).val())
								fieldValueList.options[j].selected = true;
							j++;
						});

						ChangeParentObject(i);
					}
				}
				else {
					if ($(xmlData).find("Field[id = " + id + "]").attr("type") == "date") {
						$("#fieldValue" + i).datepicker();
					}

					if ($(xmlData).find("Field[id = " + id + "]").attr("autocomplete") != null) {
						$("#fieldValue" + i).autocomplete(baseUrl + $(xmlData).find("Field[id = " + id + "]").attr("autocomplete"));
					}
				}
			}

		}
	});
}

function BC_ChangeField(obj, row) {
	var id = obj.options[obj.selectedIndex].value;

	var operator = document.getElementById("operator" + row);

	$(operator).empty();

	if ($(xmlData).find("Field[id = " + id + "]").attr("name") != null)
		$("#fieldName" + row).val($(xmlData).find("Field[id = " + id + "]").attr("name"));
	else
		$("#fieldName" + row).val("");

	if ($(xmlData).find("Field[id = " + id + "]").attr("tableName") != null)
		$("#tableName" + row).val($(xmlData).find("Field[id = " + id + "]").attr("tableName"));
	else
		$("#tableName" + row).val("");

	if ($(xmlData).find("Field[id = " + id + "]").attr("type") != null)
		$("#fieldType" + row).val($(xmlData).find("Field[id = " + id + "]").attr("type"));
	else
		$("#fieldType" + row).val("");

	var i = 0;

	$(xmlData).find("Field[id = " + id + "]").find("Operator").each(function() {
		var option = new Option($(this).attr("display"), $(this).attr("name"));
		operator.options[i] = option;
		i++;
	});

	if ($(xmlData).find("Field[id = " + id + "]").find("Lookup").length > 0) {
		var lookup = $(xmlData).find("Field[id = " + id + "]").find("Lookup");

		$("#fieldValue" + row).css("display", "none");
		$("#fieldValueList" + row).css("display", "block");

		var fieldValueList = document.getElementById("fieldValueList" + row);
		$(fieldValueList).empty();

		var option = new Option("", "");
		fieldValueList.options[0] = option;
		i = 1;


		if ($(lookup).attr("type") == "url") {
			var url = baseUrl + $(lookup).attr("url");
			var data = "";

			if (url.indexOf('?') > 0) {
				data = url.substr(url.indexOf("?") + 1);
				url = url.substr(0, url.indexOf("?"));
			}

			$.ajax({
				type: "POST",
				url: url,
				data: data,
				cache: false,
				async: false,
				dataType: "xml",
				success: function(xml) {
					$(xml).find($(lookup).attr("nodeName")).each(function() {
						var option = new Option($(this).attr($(lookup).attr("display")), $(this).attr($(lookup).attr("value")));
						fieldValueList.options[i] = option;
						if (fieldValueList.options[i].value == $("#fieldValue" + i).val())
							fieldValueList.options[i].selected = true;
						i++;
					});
				}
			});
		}
		else {
			$(xmlData).find("Field[id = " + id + "]").find("Lookup").find("Value").each(function() {
				var option = new Option($(this).attr("display"), $(this).attr("name"));
				fieldValueList.options[i] = option;
				if (fieldValueList.options[i].value == $("#fieldValue" + row).val())
					fieldValueList.options[i].selected = true;
				i++;
			});
		}
	}
	else {
		$("#fieldValue" + row).css("display", "block");
		$("#fieldValueList" + row).css("display", "none");
		$("#fieldValue" + row).datepicker("destroy");
		$("#fieldValue" + row).unautocomplete();

		if ($(xmlData).find("Field[id = " + id + "]").attr("type") == "date") {
			$("#fieldValue" + row).datepicker();
		}

		if ($(xmlData).find("Field[id = " + id + "]").attr("autocomplete") != null) {
			$("#fieldValue" + row).autocomplete(baseUrl + $(xmlData).find("Field[id = " + id + "]").attr("autocomplete"));
		}
	}
}

function BC_ChangeOperator(obj, row) {
	var id = $("#fieldId" + row).val();

	if ($("#operator" + row).val().indexOf("getutcdate") >= 0 || $("#operator" + row).val().indexOf("getdate") >= 0)
		$("#fieldValue" + row).datepicker("destroy");
	else if ($("#operator" + row).val() == "is null" || $("#operator" + row).val() == "is not null") {
		$("#fieldValue" + row).datepicker("destroy");
		$("#fieldValue" + row).val("");
	}
	else {
		if ($(xmlData).find("Field[id = " + id + "]").attr("type") == "date" || $(xmlData).find("Field[id = " + id + "]").attr("type") == "regulardate") {
			$("#fieldValue" + row).datepicker();
		}
	}
}


function BC_ChangeFormToAjax(theform, options) {
	if (options.targetId == null)
		options.targetId = '#gridData';

	if (options.successFunc == null)
		options.successFunc = SearchComplete;

	if ($(options.targetId).length == 0)
		options.targetId = null;

	$(theform).ajaxForm({
		target: options.targetId,
		beforeSubmit: BC_DisplayWaitImage,
		error: BC_ReportError,
		success: function(responseText, statusText) {
			options.successFunc(responseText, statusText, options);
		}
	});
}

function SearchComplete(responseText, statusText, options) {
	if (responseText != null && responseText.indexOf("url:") == 0) {
		self.location = responseText.substr(4);
	}

	$("#waitImage").css("display", "none");

	if ($("#totalRow").val() != "0") {
		$("#message").html($("#totalRow").val() + " records found.");
		$("#pageSizeSelector").show();
	}
	else {
		$("#message").html("No matching record");
		$("#pageSizeSelector").css("display", "none");
	}
}

function BC_PreSaveCriteria(theform) {
	$(theform).ajaxForm({ target: '#searchCriteria', success: BC_InitializeSearchCriteria });
}

function BC_DisplayWaitImage() {
}

function hover_on(tip, linkId) {
	$(tip).show();
	var offset = $(linkId).offset();
	var left = offset.left + $(linkId).width() + 48;
	var top = offset.top - 27;

	$(tip).css("left", left);
	$(tip).css("top", top);
}

function hover_off(tip) {
	$(tip).hide();
}

function StripCharsInBag(s, bag) {
	var i;
	var returnString = "";

	for (var i = 0; i < s.length; i++) {
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}

	return returnString;
}

function ChangeCompany() {
	var company = $("#companyName").val();
	company = StripCharsInBag(company, " .").toLowerCase();

	if (company != "") {
		$("#domainName").val(company);
		ChangeDomain();
	}
}

function ChangeDomain() {
	$.ajax({
		type: "POST",
		url: '/Support/Home/CheckDomain',
		data: 'domainName=' + $("#domainName").val() + "&suffix=" + $("#suffix").val(),
		cache: false,
		async: false,
		dataType: "xml",
		success: function(xml) {
			if ($(xml).find("Error").length > 0) {
				$(xml).find("Error").each(function() {
					$("#domainError").html($(this)[0].text);
				});
			} else
				$("#domainError").html("");
		}
	});
}

function ChangePlan(list) {
	if (list.selectedIndex == (list.length - 1))
		$("#additional").show();
	else
		$("#additional").hide();
}

function SelectCheckItem(theform, obj, inputName) {
	var totalElement = theform.elements.length;

	for (var i = 0; i < totalElement; i++) {
		if (theform.elements[i].type == "checkbox" && theform.elements[i].name.indexOf(inputName) >= 0) {
			theform.elements[i].checked = obj.checked;
		}
	}
}
