﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

function loadCountries() {
	$.getJSON("/Ajax/ListCountries", null, function(data) {
		$("#country").fillSelect(data, "Country");
		$("#region").clearSelect("Region");
	});
}

function loadRegions() {
	var countryUID = $("#country").val();
	$.getJSON("/Ajax/ListRegions/" + countryUID, function(data) {
		// We will clear the textbox to reset the Watermark function
		$('#zipcode').val('');
		updateZipCodeLabels();
		$("#region").fillSelect(data, "").effect("highlight", {}, 500);
		$("#city").clearSelect("City");
	});
}

function loadCities() {
	var regionUID = $("#region").val();
	$.getJSON("/Ajax/ListCities/" + regionUID, function(data) {
		$("#city").fillSelect(data, "City").effect("highlight", {}, 500);
	});
}

function loadCategories() {
	$.getJSON("/Ajax/ListCategories", function(data) {
		$("#category").fillSelect(data, "Category");
		$("#subcategory").clearSelect("Sub Category");
	});
}

function loadSubCategories() {
	var categoryUID = $("#category").val();
	$.getJSON("/Ajax/ListCategories/" + categoryUID, function(data) {
		$("#subcategory").fillSelect(data, "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")));
}

// Get the Search Info
function getSearchInfo(pathname) {
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "GET",
		url: "/Ajax/GetGeographyNavigationInfo",
		data: "pathname=" + pathname,
		success: updateSearchCriteria,
		error: ajaxFailed, // This is for debugging only.  Remove when in Production.
		dataType: "json"
	});
}

function updateSearchCriteria(data) {

	if (data != "") {
		// Select and Fill the data that we did receive
		if (data.d.SelectedCountry != null) {
			$("#country").val(data.d.SelectedCountry.Value);
		}
		if (data.d.Regions != null) {
			$("#region").fillSelect(data.d.Regions, "Region");
		}
		if (data.d.SelectedRegion != null) {
			$("#region").val(data.d.SelectedRegion.Value);
		}
		if (data.d.Cities != null) {
			$("#city").fillSelect(data.d.Cities, "City");
		}
		if (data.d.SelectedCity != null) {
			$("#city").val(data.d.SelectedCity.Value);
		}
	}
}

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();


}

$(document).ready(function() {
	//  loadCountries();
	//  loadCategories();
	$("#country").change(function() { loadRegions(); });
	$("#region").change(function() { loadCities(); });
	$("#category").change(function() { loadSubCategories(); });

	updateZipCodeLabels();

	// 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])
	//			}
	//		})
	//	});

});

