﻿// Synergy specific functions

function loadCountries(enabledOnly) {

	var url = "/Ajax/ListCountries";
	url = AddEnabledOnly(enabledOnly, url);
	$.getJSON(url, null, function(data) {
		$("#country").fillSelect(data.Model, "Country");
		$("#region").clearSelect("Region");
	});
}

function AddEnabledOnly(enabledOnly, url) {

	if (enabledOnly !== undefined) {
		if (enabledOnly == false) {
			url += "/?EnabledOnly=false";
		}
	}
	return url;
}

function loadRegions(enabledOnly) {
	var countryUID = $("#country").val();
	var url = "/Ajax/ListRegions/" + countryUID;
	url = AddEnabledOnly(enabledOnly, url);
	$.getJSON(url, function(data) {
		// We will clear the textbox to reset the Watermark function
		$('#zipcode').val('');
		updateZipCodeLabels();
		$("#region").fillSelect(data.Model, "").effect("highlight", {}, 500);
		$("#city").clearSelect("City");
	});
}

function loadCities(enabledOnly) {
	var regionUID = $("#region").val();
	var url = "/Ajax/ListCities/" + regionUID;
	url = AddEnabledOnly(enabledOnly, url);
	$.getJSON(url, function(data) {
		$("#city").fillSelect(data.Model, "City").effect("highlight", {}, 500);
	});
}

function loadCategories() {
	$.getJSON("/Ajax/ListCategories", function(data) {
		$("#category").fillSelect(data.Model, "Category");
		$("#subcategory").clearSelect("Sub Category");
	});
}

function loadSubCategories() {
	var categoryUID = $("#category").val();
	$.getJSON("/Ajax/ListCategories/" + categoryUID, function(data) {
		$("#subcategory").fillSelect(data.Model, "Sub Category").effect("highlight", {}, 500);
	});
}

function updateZipCodeLabels() {

	var label = "Zip Code";
	// If it's Canada we will use Postal Code as the Text
	if ($("#country option:selected").text().toLowerCase() == "canada") {
		label = "Postal Code";
	}
	// Update the Watermark text and label text
	$('#zipcode').watermark("watermark", label);
	$('#zipcodeparagraph').text('Or enter your ' + label.toLowerCase() + ':');
	$('#zipcodelabel').text('Enter your ' + label.toLowerCase() + '.');
	$('#zipcode').attr('title', 'Enter your ' + label.toLowerCase() + '.');
}

function jsonToDate(value) {
	return new Date(parseInt(value.replace(/\/Date\((\d+)\)\//gi, "$1")));
}

function ajaxFailed(result) {
	// This is for debugging only.  Remove when in Production.
	alert(result.status + ' ' + result.statusText + '\n' + result.Message);
}

function submitNavigationForm() {

	// TODO We will implement this functionality down the road
	var countryUID = $("#country").val();
	var regionUID = $("#region").val();
	var cityUID = $("#city").val();

	var zipcode = $("#zipcode").val();
	var categoryUID = $("#category").val();
	var subcategoryUID = $("#subcategory").val();

}

function checkAll(element, list) {
	$("#" + list + " :checkbox:visible").attr('checked', element.is(':checked'));
}

function allRowsChecked(headerRowSelector, tableRowSelector) {

	var allChecked = true;
	$(tableRowSelector).each(function() {
		$(this).show();
		var t = $(this).find(':checkbox');
		if (!$(this).find(':checkbox').is(':checked')) {
			allChecked = false;
		}
	});

	// If all checkboxes are checked, then check all checkbox should be checked
	if (allChecked == true) {
		$(headerRowSelector + " :checkbox").attr('checked', true);
	} else {
		$(headerRowSelector + " :checkbox").attr('checked', false);
	}
}

function showAllCheckBoxRows(headerRowSelector, tableRowSelector) {

	$(tableRowSelector).each(function() {
		$(this).show();
	});

	allRowsChecked(headerRowSelector, tableRowSelector);
}

function showAssignedCheckBoxRows(headerRowSelector, tableRowSelector) {
	showAllCheckBoxRows(headerRowSelector, tableRowSelector);
	$(tableRowSelector).each(function() {
		// if row contains an input that is not checked, hide row					
		if ($(this).find('input:not(:checked)').length == 1) {
			$(this).hide();
		}
	});
	$(headerRowSelector + " :checkbox").attr('checked', true);
}

function showUnassignedCheckBoxRows(headerRowSelector, tableRowSelector) {
	showAllCheckBoxRows(headerRowSelector, tableRowSelector);
	$(tableRowSelector).each(function() {
		// if row contains an input that is checked, hide row					
		if ($(this).find('input:checked').length == 1) {
			$(this).hide();
		}
	});
	$(headerRowSelector + " :checkbox").attr('checked', false);
}

$(document).ready(function() {
	//  loadCountries();
	//  loadCategories();

	var countryLoadRegions = function() { loadRegions(); };
	var regionLoadCities = function() { loadCities(); };
	$("#country").change(countryLoadRegions);
	$("#region").change(regionLoadCities);
	
	$("#category").change(function() { loadSubCategories(); });

	updateZipCodeLabels();

	$('#genericForm #submitButton, #genericForm .submitButton').hover(
		function () {
			$(this).addClass("submitButtonHover");
		},
		function () {
			$(this).removeClass("submitButtonHover");
	});


	// TODO We will implement this functionality down the road
	// $("#navigationForm").submit(function() { submitNavigationForm(); });

	//	var watermarks = [];
	//	$(".watermark").each(function(i) {
	//		watermarks[i] = $(this).val();
	//		$(this).focus(function() {
	//			if ($(this).val() == watermarks[i]) {
	//				$(this).val("")
	//			}
	//		}).blur(function() {
	//			if ($.trim($(this).val()) == "") {
	//				$(this).val(watermarks[i])
	//			}
	//		})
	//	});

});


