﻿/// <reference path="../includes/jquery/jquery-1.4.1-vsdoc.js" />

function printContent(id) { // stampa il contenuto di un oggetto dato il suo ID di riconoscimento...
	str = $("#" + id).html();
	newwin = window.open('', 'printwin', 'left=0,top=0,width=' + ($("#" + id).width()).toString() + ',height=' + ($("#" + id).height()).toString() + '')
	newwin.document.write('<HTML>\n<HEAD>\n')
	newwin.document.write('<TITLE>Print Page</TITLE>\n')
	newwin.document.write('<script>\n')
	newwin.document.write('function chkstate(){\n')
	newwin.document.write('if(document.readyState=="complete"){\n')
	newwin.document.write('window.close()\n')
	newwin.document.write('}\n')
	newwin.document.write('else{\n')
	newwin.document.write('setTimeout("chkstate()",2000)\n')
	newwin.document.write('}\n')
	newwin.document.write('}\n')
	newwin.document.write('function print_win(){\n')
	newwin.document.write('window.print();\n')
	newwin.document.write('chkstate();\n')
	newwin.document.write('}\n')
	newwin.document.write('<\/script>\n')
	newwin.document.write('<link rel="stylesheet" href="../styles/stylesheet.css" />')
	newwin.document.write('</HEAD>\n')
	newwin.document.write('<BODY onload="print_win()">\n')
	newwin.document.write('<div id="' + id + '">\n')
	newwin.document.write(str)
	newwin.document.write('</div>')
	newwin.document.write('</BODY>\n')
	newwin.document.write('</HTML>\n')
	newwin.document.close()
}

// [legge o setta la posizione del caret in un campo input]
function doGetCaretPosition(oField) {

	// Initialize
	var iCaretPos = 0;

	// IE Support
	if (document.selection) {

		// Set focus on the element
		oField.focus();

		// To get cursor position, get empty selection range
		var oSel = document.selection.createRange();

		// Move selection start to 0 position
		oSel.moveStart('character', -oField.value.length);

		// The caret position is selection length
		iCaretPos = oSel.text.length;
	}

	// Firefox support
	else if (oField.selectionStart || oField.selectionStart == '0')
		iCaretPos = oField.selectionStart;

	// Return results
	return (iCaretPos);
}

function doSetCaretPosition(oField, iCaretPos) {

	// IE Support
	if (document.selection) {

		// Set focus on the element
		oField.focus();

		// Create empty selection range
		var oSel = document.selection.createRange();

		// Move selection start and end to 0 position
		oSel.moveStart('character', -oField.value.length);

		// Move selection start and end to desired position
		oSel.moveStart('character', iCaretPos);
		oSel.moveEnd('character', 0);
		oSel.select();
	}

	// Firefox support
	else if (oField.selectionStart || oField.selectionStart == '0') {
		oField.selectionStart = iCaretPos;
		oField.selectionEnd = iCaretPos;
		oField.focus();
	}
}
// [/legge o setta la posizione del caret in un campo input]

// [maschera un campo input accettando soltanto valori numerici]
function intMask(inputObj) {
	if (inputObj.value=="") inputObj.value = "0";
	$(inputObj).css("text-align", "right");
	$(inputObj).css("padding-right", "4px");
	$(inputObj).css("width", "40px");
	$(inputObj).focus(function() { if (inputObj.value == "0") inputObj.value = ""; });
	$(inputObj).blur(function() { if (inputObj.value == "") inputObj.value = "0"; });
	$(inputObj).keypress(
	function(event) {
		var charCode = (event.which) ? event.which : event.keyCode;
		if (
			(charCode >= 48 && charCode <= 57) // carattere numerico
			|| charCode == 8 // delete
			|| charCode == 9 // tab
			|| charCode == 37 // move left
			|| charCode == 39 // move right
			) {
			return true;
		}
		else {
			return false;
		}
	}
	);
}
// [/maschera un campo input accettando soltanto valori numerici]

// [maschera un campo input accettando soltanto valori money]
function moneyMask(inputObj) {
	$(inputObj).css("text-align", "right");
	$(inputObj).css("padding-right", "4px");
	inputObj.value = inputObj.value.replace(",", ".");
	if (inputObj.value.indexOf(".") != -1) {
		var intero = inputObj.value.substring(0, inputObj.value.indexOf("."));
		var decimale = inputObj.value.substring(inputObj.value.indexOf(".") + 1, inputObj.value.length);
		if (decimale.length > 2) decimale = decimale.substring(0, 2);
		inputObj.value = intero + "." + decimale;
	}
	else if (inputObj.value == "0" || inputObj.value == "") inputObj.value = "0.00";
	$(inputObj).focus(function() { if (inputObj.value == "0" || inputObj.value == "0.00") inputObj.value = ""; });
	$(inputObj).keypress(
	function(event) {
		var charCode = (event.which) ? event.which : event.keyCode;
		if (charCode == '46' && inputObj.value.indexOf(".") == -1) { // permessa UNA SOLA virgola
			return true
		}
		else {
			if (
			(charCode >= 48 && charCode <= 57) // carattere numerico
			|| charCode == 8 // delete
			|| charCode == 46 // canc
			|| charCode == 9 // tab
			|| charCode == 37 // move left
			|| charCode == 39 // move right
			) {
				if (inputObj.value.indexOf(".") != -1 && (charCode >= 48 && charCode <= 57)) { // se il valore contiene già una virgola e si sta tentando di inserire un altro valore numerico
					if (doGetCaretPosition(inputObj) > inputObj.value.indexOf(".")) { // se mi trovo a destra della virgola
						if ((inputObj.value.length - inputObj.value.indexOf(".")) <= 2) { // se non ci sono già due decimali dopo la virgola
							return true;
						}
						else {
							return false;
						}
					}
					else {
						return true;
					}
				}
				else {
					return true;
				}
			}
			else { // carattere non permesso
				return false;
			}
		}
	})
	$(inputObj).blur( // il campo input perde il focus
	function() {
		if (inputObj.value == "") // vuoto => 0.00
		{ inputObj.value = "0.00"; }
		else if (inputObj.value.indexOf(".") == -1) { // mancano i valori decimali => aggiungere
			inputObj.value = inputObj.value + ".00";
		}
		else {
			var decimali_mancanti = 2 - (inputObj.value.length - inputObj.value.indexOf(".") - 1);
			for (var i = 0; i < decimali_mancanti; i++) // tanti 0 per quante sono le posizioni decimali vacanti
			{
				inputObj.value = inputObj.value + "0";
			}
		}
	})
}
// [/maschera un campo input accettando soltanto valori money]
