
// --- Label required ---
$(document).ready(function () {
	$("input[data-val]").each(function () {
		if (!$(this).is('input:checkbox') && !$(this).is('input:radio'))
			$("label[for='" + $(this).attr("id") + "']").attr("class", "required");
	});
	$("select[data-val]").each(function () {
		$("label[for='" + $(this).attr("id") + "']").attr("class", "required");
	});
});

// --- Button rollover ---
$(document).ready(function () {
	$(".button").each(function () {
		setButtonStyle($(this), "button", "button-hover");
	});
	$(".bulletsky").each(function () {
		setButtonStyle($(this), "bulletsky", "bulletsky-hover");
	});
});

function setButtonStyle(element, className, classNameHover) {
	if (className == undefined) className = "button";
	if (classNameHover == undefined) classNameHover = "button-hover";

	$(element).hover(function () {
		$(this).removeClass(className);
		$(this).addClass(classNameHover);
	}, function () {
		$(this).removeClass(classNameHover);
		$(this).addClass(className);
	});
}

// --- Datepicker ---
$(document).ready(function () {

	$('.datepicker').each(function () {
		$(this).datepicker(
            {
            	firstDay: 1,
            	showOtherMonths: true
            }
        );
	});

	$('.datepickerQuestionario').each(function () {
		$(this).datepicker(
            {
            	firstDay: 1,
            	showOtherMonths: true,
            	minDate: 1
            }
        );
	});

	$('.datepickerBirth').each(function () {
		$(this).datepicker(
        {
        	firstDay: 1,
        	showOtherMonths: true,
        	dateFormat: 'dd/mm/yy',
        	changeMonth: true,
        	changeYear: true,
        	maxDate: -1
        }
    );
	});
});

// --- Popup dialog ---
$(document).ready(function () {
	$("#dialog").dialog("destroy");
});

(function ($) {
	$.jqfunction = {

		OpenDialog: function () {
			$.ajaxSetup({ cache: false });
			$('html, body').animate({ scrollTop: 0 });
			$("#dialog").dialog({
				autoOpen: false,
				closeOnEscape: true,
				draggable: true,
				height: 250,
				modal: true,
				resizable: false,
				title: "Attenzione!",
				open: function (event, ui) { event.preventDefault(); $('.ui-widget-overlay').css('width', '100%'); },
				close: function (event, ui) { redesignPage(); }
			});
			var myDialogX = ($(document).width - $('#dialog').width) / 2;
			var myDialogY = -$(document).scrollTop() + 70;
			$('#dialog').dialog('option', 'position', [myDialogX, myDialogY]);
			$('#dialog').dialog('open');
			return false;
		}

,

		OpenDialogDynamics: function (dialogHeight, dialogWidth, dialogTitle) {
			$.ajaxSetup({ cache: false });
			$('html, body').animate({ scrollTop: 0 });
			$("#dialog").dialog({
				autoOpen: false,
				closeOnEscape: true,
				draggable: true,
				height: dialogHeight,
				width: dialogWidth,
				modal: true,
				resizable: false,
				title: dialogTitle,
				open: function (event, ui) { event.preventDefault(); $('.ui-widget-overlay').css('width', '100%'); },
				close: function (event, ui) { redesignPage(); }
			});
			var myDialogX = ($(document).width - $('#dialog').width) / 2;
			var myDialogY = -$(document).scrollTop() + 70;
			$('#dialog').dialog('option', 'position', [myDialogX, myDialogY]);
			$('#dialog').dialog('open');
			return false;
		}


,

		OpenDialogCentered: function (dialogHeight, dialogWidth, dialogTitle) {
			$.ajaxSetup({ cache: false });
			$('html, body').animate({ scrollTop: 0 });
			$("#dialog").dialog({
				autoOpen: false,
				closeOnEscape: true,
				draggable: true,
				height: dialogHeight,
				width: dialogWidth,
				modal: true,
				resizable: false,
				title: dialogTitle,
				open: function (event, ui) { event.preventDefault(); $('.ui-widget-overlay').css('width', '100%'); },
				close: function (event, ui) { redesignPage(); }
			});
			var myDialogX = ($(document).width - $('#dialog').width) / 2;
			var myDialogY = ($(document).height - $('#dialog').height) / 2;
			$('#dialog').dialog('option', 'position', [myDialogX, myDialogY]);
			$('#dialog').dialog('open');
			return false;
		}

	};

})(jQuery);

// --- Checkbox Radio CSS ---
$(document).ready(function () {
	$("input[type=checkbox]").attr("class", "checkbox");
	$("input[type=radio]").attr("class", "radio");
});

function redesignPage(){
	$("select").each(function () {
		$(this).css('display', 'block');
		$(this).css('visibility', 'visible');
		$(this).css('position', 'absolute');
	});

	$('#menuin').attr("class", $('#menuin').attr("class"));
	$('#data').attr("class", $('#data').attr("class"));
	$('#Questionario').attr("class", $('#Questionario').attr("class"));
	$("#floating-box").attr("class", $('#floating-box').attr("class"));
}

// --- Menu ---
/* senza animazione */
$(document).ready(function () {

	// Bilanciamento menu
	var wordMenuCount = 0;
	var widthTot = 0; var widthVoice = 0; var widthFree = 0;
	$("ul.menuin > li").each(function () {
		wordMenuCount += 1;
		widthTot += $(this).width();
	});
	widthFree = parseInt((680 - (wordMenuCount * 3) - widthTot) / wordMenuCount);
	$("ul.menuin > li").each(function () {
		$(this).width(($(this).width() + widthFree) + "px");
	});
	// Hover
	$("#menu ul li").hover(function () {
		$(this).addClass("hover");
		$(this).children('ul').css('visibility', 'visible');
	}, function () {
		$(this).removeClass("hover");
		$(this).children('ul').css('visibility', 'hidden');
	});
});

// --- ToolTip ---
$(document).ready(function () {

	/* CONFIG */
	xOffset = 10;
	yOffset = 20;
	// these 2 variable determine popup's distance from the cursor
	// you might want to adjust to get the right result		
	/* END CONFIG */
	$("a.tooltip").hover(function (e) {
		this.t = this.title;
		this.title = "";
		$("body").append("<p id='tooltip'>" + this.t + "</p>");
		$("#tooltip")
			.css("top", (e.pageY + xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px")
			.fadeIn("fast");
	},
	function () {
		this.title = this.t;
		$("#tooltip").remove();
	});
	$("a.tooltip").mousemove(function (e) {
		$("#tooltip")
			.css("top", (e.pageY + xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px");
	});
});

// update dom solo per debug jquery poi commentare
//$(document).ready(function () {
//$("#ildom").text($("body").html());
//});

/* DISMESSA
function createMenuOld(listName, elementName, filterValue) {
$('input[id$=' + elementName + ']').unautocomplete().autocomplete(listName + filterValue,
{ lineSeparator: '\n', cellSeparator: '|', minChars: 0, delay: 0, mustMatch: true, matchContains: false, autoFill: false, scroll: true, scrollHeight: 95 });
}
*/

// Cambio titolo sito
$(document).ready(function () {
	document.title = SolveTitleSite(document.title);
});

function SolveTitleSite(doctitle) {
	doctitle = doctitle.toLowerCase().replace("europ assistance", "");
	doctitle = doctitle.toLowerCase().replace("auropassistance", "");
	doctitle = doctitle.charAt(0).toUpperCase() + doctitle.substring(1, doctitle.length);
	if (doctitle.length != 0)
		doctitle = 'Europ Assistance - ' + doctitle;
	else
		doctitle = 'Europ Assistance';
	return doctitle;
}

function TheEnd() {
	$("input[type=text]:disabled").each(function () {
		$(this).attr("class", "inputdisabled");
	});
}

/* Creazione grafica box */
$(document).ready(function () {
	$('.jqbox').before("<div class='jqboxTop'></div>");
	$('.jqbox').after("<div class='jqboxBottom'></div>");
	$('.box').prepend("<div><b class='rtop'><b class='r1'></b><b class='r2'></b><b class='r3'></b><b class='r4'></b></b></div>");
	$('.box').append("<div><b class='rbottom'><b class='r4'></b><b class='r3'></b><b class='r2'></b><b class='r1'></b></b></div>");
});

/* LOGOUT */
$(document).ready(function () {
	$('a[href$="Logout"]').attr("class", "logout");
});

/* LOGO */
$(document).ready(function () {

	$("#userLogo").load(function () {
		if (parseInt($('#userLogo').css('width')) > 210) $('#userLogo').css('width', '210px');
	});

	$("#userLogo").error(function () {
		$('#logopdvin').css('display', 'none');
	});

	$("#userLogo").attr("src", $("#userLogo").attr("src"));
});

$(window).resize(function () {
	redesignPage();
});
/*
$(document).ready(function () {
	if ($(".validation-summary-errors").length != 0) {
		if ($(".validation-summary-errors").html().length != 0) {
			$(".validation-summary-errors").css('display', 'none');
			$('<div id="CustomValidationSummaryErrors" />').html($(".validation-summary-errors").html()).dialog({
				autoOpen: false,
				closeOnEscape: true,
				draggable: true,
				width: 400,
				height: 250,
				modal: true,
				resizable: false,
				title: "Attenzione!",
				open: function (event, ui) { event.preventDefault(); $('.ui-widget-overlay').css('width', '100%'); },
				close: function (event, ui) { redesignPage(); }
			});
			$('#CustomValidationSummaryErrors').dialog('open');
		}
	}
});
*/


/*
$(document).ready(function ($) {

	$("#container").before($('<div id="floating-box" />'));

	//this is the floating content
	var $floatingbox = $('#floating-box');

	if ($('#container').length > 0) {

		var bodyY = parseInt($('#container').offset().top) - 20;
		var originalX = $floatingbox.css('margin-left');

		$(window).scroll(function () {

			var scrollY = $(window).scrollTop();
			var isfixed = $floatingbox.css('position') == 'fixed';

			if ($floatingbox.length > 0) {

				alert(("srollY : " + scrollY + ", bodyY : " + bodyY + ", isfixed : " + isfixed));

				if (scrollY > bodyY) {
					 $floatingbox.stop().css({
						position: 'fixed',
						left: 100,
						top: 20,
						marginLeft: -500

					});
				} else if (scrollY < bodyY) {
					$floatingbox.css({
						position: 'absolute',
						left: 0,
						top: 0,
						marginLeft: originalX
					});
				}

			}

		});
	}
});


*/

function autoFormatDate(e, control, format) {
	this.Format = format;
	var keycode = (e.which) ? e.which : event.keyCode;
	if (keycode == 45 || keycode ==46 || keycode ==47) {
		control.value += '/';
		control.value = tryFormatDate(control, control.value.length, format);
		return false;
	}
	else if ((keycode > 31 && (keycode < 48 || keycode > 57)) && (keycode != 45 && keycode != 46 && keycode != 47)) {
		return false;
	} else {
		var DateFormatPattern = /^dd\/mm\/yyyy$|^mm\/dd\/yyyy$|^mm\/dd\/yy$|^yyyy\/mm\/dd$/;
		if (DateFormatPattern.test(this.Format)) {
			var SplitFormat = this.Format.split("/");
			if (control.value.length >= this.Format.length) {
				if (keycode != 8) {
					return false;
				}
			}
			if (control.value.length == SplitFormat[0].length) {
				if (keycode != 8) {
					control.value += '/';
				}
			}
			if (control.value.length == (SplitFormat[1].length + SplitFormat[0].length + 1)) {
				if (keycode != 8) {
					control.value += '/';
				}
			}
		} else {
			// alert("Supplied date format parameter is incorrect.");
		}
	}
}

function tryFormatDate(control, to, format) {
	var lastring = "";
	var ilchar = "";
	var ilcheck = "";
	var ilnumber = "";
	var stringachecked = "";

	var lastslash = 0; var nextslash = 0;
	var j = 0;

	nextslash = format.indexOf("/");
	theFormat = nextslash - lastslash;
	
	for (i = 0; i < to; i++) {

		ilchar = control.value.substring(i, i + 1);
		ilcheck = format.substring(i, i + 1);
		if (ilchar != "/") ilnumber = ilchar;
		if (ilchar == "/" && ilcheck == "/") { lastring += ilchar; stringachecked = lastring; }
		if (ilchar != "/" && ilcheck != "/") { lastring += ilchar; }
		if ((ilchar != "/" && ilcheck == "/") || (ilchar == "/" && ilcheck != "/")) {
			lastring = stringachecked + pad(ilnumber, theFormat) + "/";
		}
	
	}

	return lastring;
}

function pad(str, max) {
	return str.length < max ? pad("0" + str, max) : str;
}
