﻿var newUrl;

function SetExternalScript(url) {
	var scriptObj = document.createElement('script');
	scriptObj.setAttribute('type', 'text/javascript');
	scriptObj.setAttribute('src', url);

	document.getElementsByTagName('head').item(0).appendChild(scriptObj);
}

function ParseOldURL() {
	var oldUrl = location.href;
	var query = oldUrl.replace(/^[^\?]+\??/, '');
	if (oldUrl.indexOf("?") !== -1) {
		newUrl = oldUrl.substr(0, oldUrl.indexOf("?"));
	} else {
		newUrl = oldUrl;
	}
	var Params = {};
	var Pairs = query.split(/[;&]/);
	for (var i = 0; i < Pairs.length; i++) {
		var KeyVal = Pairs[i].split('=');
		if (!KeyVal || KeyVal.length != 2) { continue; }
		var key = unescape(KeyVal[0]);
		var val = unescape(KeyVal[1]);
		val = val.replace(/\+/g, ' ');
		Params[key] = val;
	}
	return Params;
}

function fnSubmitSelectedFilter(filter_Id, filterToClear) {
	var filterValue = "";
	if (filterToClear != "true") {
		$('.FK_' + filter_Id + ':checked').each(function () {
			if (filterValue == "") {
				filterValue = $(this).attr("value");
			}
			else {
				filterValue = filterValue + "," + $(this).attr("value");
			}
		});
	}
	var Params = ParseOldURL();
	var sep = "?";
	for (var i in Params) {
		if ("FK_" + filter_Id != i && "CPI" != i) {
			newUrl = newUrl + sep + i + "=" + Params[i];
			sep = "&";
		}
	}
	if (filterToClear != "true") {
		if (filterValue != "") {
			location.href = newUrl + sep + "FK_" + filter_Id + "=" + filterValue + "&CPI=0";
		} else {
			location.href = newUrl + sep + "CPI=0";
		}
	} else {
		location.href = newUrl + sep + "CPI=0";
	}
}

function fnChangePageSize(pageSize) {
	var Params = ParseOldURL();
	var sep = "?";
	for (var i in Params) {
		if (i != "PS" && i != "CPI") {
			newUrl = newUrl + sep + i + "=" + Params[i];
			sep = "&";
		}
	}
	location.href = newUrl + sep + "PS=" + pageSize + "&CPI=0";
}

/* get address information */
function fnGetAddressInfo(zipCode, houseNumber, houseNumberAddition, addressType) {
	if (zipCode.length > 5 && houseNumber.length > 0) {
		var address = {};
		address.ZipCode = zipCode;
		address.HouseNumber = houseNumber;
		address.HouseNumberAddition = houseNumberAddition;
		address.AddressType = addressType;
		IERSService.GetAddressInfo(address, onAddressInfo);
	}
}

function onAddressInfo(address) {
	$("#hint_street").text("");
	$("#hint_place").text("");

	$("#fc_postcode_invoice_not_found").hide();
	$("#fc_postcode_delivery_not_found").hide();

	$("#fc_postcode_invoice_not_valid").hide();
	$("#fc_postcode_delivery_not_valid").hide();

	$("#fc_zip_placeholder").removeClass("error");
	$("#fc_zip2_placeholder").removeClass("error");

	if (!address.ServiceError && address.AddressFound == true) {

		if (!address.IsPOBox) {
			if (address.AddressType == 'invoice') {
				$("#fc_street").val(address.Street);
				$("#fc_place").val(address.City);
				$("#fc_street").attr("readonly", true);
				$("#fc_place").attr("readonly", true);
				$("#fc_street_placeholder").addClass("disabled");
				$("#fc_place_placeholder").addClass("disabled");

				$("#fc_street_placeholder").addClass('filled');
				$("#fc_place_placeholder").addClass('filled');
			}
			else {
				$("#fc_street2").val(address.Street);
				$("#fc_place2").val(address.City);
				$("#fc_street2").attr("readonly", true);
				$("#fc_place2").attr("readonly", true);
				$("#fc_street2_placeholder").addClass("disabled");
				$("#fc_place2_placeholder").addClass("disabled");

				$("#fc_street2_placeholder").addClass('filled');
				$("#fc_place2_placeholder").addClass('filled');
			}
		} else {
			releaseAddressInfo(address.AddressType, 'notvalid');
		}
	}
	else {
		releaseAddressInfo(address.AddressType, 'notfound');
	}


	resetStreetNumberErrorFields(); 

}

function releaseAddressInfo(addressType, errorMessageType) {
	if (addressType == 'invoice') {
		if (errorMessageType == 'notfound')
			$("#fc_postcode_invoice_not_found").show();
		else
			$("#fc_postcode_invoice_not_valid").show();

		$("#fc_zip_placeholder").addClass("error");
		$("#fc_street").val("");
		$("#fc_place").val("");
		$("#fc_street").removeAttr("readonly");
		$("#fc_place").removeAttr("readonly");
		$("#fc_street_placeholder").removeClass("disabled");
		$("#fc_place_placeholder").removeClass("disabled");
	}
	else {
		if (errorMessageType == 'notfound')
			$("#fc_postcode_delivery_not_found").show();
		else
			$("#fc_postcode_delivery_not_valid").show();

		$("#fc_zip2_placeholder").addClass("error");
		$("#fc_street2").val("");
		$("#fc_place2").val("");
		$("#fc_street2").removeAttr("readonly");
		$("#fc_place2").removeAttr("readonly");
		$("#fc_street2_placeholder").removeClass("disabled");
		$("#fc_place2_placeholder").removeClass("disabled");
	}
}

function resetStreetNumberErrorFields() {
	if ($("#fc_street").val() == "") {
		$("#fc_street_rfv").show();
	} else {
		$("#fc_street_rfv").hide();
	}
	
	if ($("#fc_place").val() == "") {
		$("#fc_place_rfv").show();
	} else {
		$("#fc_place_rfv").hide();
	}

	if ($("#fc_street2").val() == "") {
		$("#fc_street2_rfv").show();
	} else {
		$("#fc_street2_rfv").hide();
	}

	if ($("#fc_place2").val() == "") {
		$("#fc_place2_rfv").show();
	} else {
		$("#fc_place2_rfv").hide();
	}
	
}
