function windowOpener(windowHeight, windowWidth, windowName, windowUri)	{
	var centerWidth = (window.screen.width - windowWidth) / 2;
	var centerHeight = (window.screen.height - windowHeight) / 2;

	newWindow = window.open(windowUri, windowName, 'resizable=0,width=' + windowWidth + 
		',height=' + windowHeight + 
		',left=' + centerWidth + 
		',top=' + centerHeight);

	newWindow.focus();
	return newWindow.name;
}

function openDetails(id) {
	openPopup('PopupDetails');
	$('#PopupDetails .PopupContent').load('../PromoText/details.php?id='+id);
}
function closeDetails() {
	closePopup('PopupDetails');
	$('#PopupDetails .PopupContent').html('<img src="../images/icon-loader.gif" class="loader" />');
}
function openPopup(id) {
	document.getElementById(id).style.left = '0px';
}
function closePopup(id) {
	document.getElementById(id).style.left = '-2000em';
}



var listKeys = [];
var listValues = [];

function disable(id) {

	$('#slider'+id+'').slider( "option", "disabled", true );
	$('#formBox'+id+'').addClass('disabled');
}
function enable(id) {
	
	$('#slider'+id+'').slider( "option", "disabled", false );
	$('#formBox'+id+'').removeClass('disabled');
}




function reset(page) {
	
	$.ajax({
		url: 'reset.php?page='+page,
		success: function(data) {
			document.location.href='./';
		}
	});
	
	
}

function showTooltip(anchorTo, tooltipId, tooltipPosition) {
	var el_pos = $(anchorTo).position();
	var el_width = $(anchorTo).width();
	var el_height = $(anchorTo).height();
//	console.log(el_width,el_height);
	//var rel = $(this).attr('rel').split('|');
	var tooltip = $(tooltipId);
	tooltip.find('.arrow').width(tooltip.width()).height(tooltip.height());
	tooltip.find('.arrow').removeClass('tooltip_left tooltip_right tooltip_top tooltip_bottom').addClass('tooltip_' + tooltipPosition);
	switch(tooltipPosition) {
		case 'left':
			tooltip.css('left', el_pos.left + el_width + 5);
			tooltip.css('top', el_pos.top + (el_height / 2) - (tooltip.height() / 2));
			break;
		case 'right':
			tooltip.css('left', el_pos.left - 5 - tooltip.width());
			tooltip.css('top', el_pos.top + (el_height / 2) - (tooltip.height() / 2));
			break;
		case 'top':
			tooltip.css('top', el_pos.top + el_height + 5);
			tooltip.css('left', el_pos.left + (el_width / 2) - (tooltip.width() / 2));
			break;
		case 'bottom':
			tooltip.css('top', el_pos.top - 5 - tooltip.height());
			tooltip.css('left', el_pos.left + (el_width / 2) - (tooltip.width() / 2));
			break;
	}
	tooltip.show();
}

function hideTooltip(tooltipId) {
	var tooltip = $(tooltipId);
	tooltip.hide();
}

/******************************
/* CONTROLLI FORM */
$(function() {
	
	
	jQuery.validator.addMethod(
		'equalValueChiamate',
		function (inputValue, inputField, equalValue) {
			
			return (!equalValue) || (inputValue == equalValue);
		}
	);
	jQuery.validator.addMethod(
		'equalValueSMS',
		function (inputValue, inputField, equalValue) {
			
			return (!equalValue) || (inputValue == equalValue);
		}
	);
	
	$("#formAdvisory").validate({
		rules: {
			Chiamate: {
				required: true,
				digits: true,
				equalValueChiamate: function(){
					return parseInt($('#ChiamateVodafone').val())+parseInt($('#ChiamateAltri').val());
				}
			},
			Cliente: {
				required: true
			},
			ChiamateVodafone: {
				digits: true
			},
			ChiamateAltri: {
				digits: true
			},
			ChiamateYouMe: {
				digits: true,
				max: function(){
					return $('#ChiamateVodafone').val() || 0;
				}
			},
			Messaggi: {
				required: true,
				digits: true,
				equalValueSMS: function(){
					return parseInt($('#MessaggiVodafone').val())+parseInt($('#MessaggiAltri').val());
				}
			},
			MessaggiVodafone: {
				digits: true
			},
			MessaggiAltri: {
				digits: true
			},
			MessaggiYouMe: {
				digits: true,
				max: function(){
					return $('#MessaggiVodafone').val() || 0;
				}
			},
			GiorniVodafone: {
				required: true,
				digits: true,
				range: [1, 7]
			}
		},
		focusCleanup: false,
		showErrors: function(errorMap, errorList) {
			//console.log(errorMap, errorList);
			$('.tooltip').hide();
			$.each(errorList, function(idx, element) {
				var tid = '#tooltip_' + $(element.element).attr('name');
				$(tid + ' .text').html(element.message);
				showTooltip(element.element, tid, 'left');
			});
			//this.defaultShowErrors();
		}

	});

	
	$('#formAdvisory').submit(function() {
	
		if ($('#SaveStep').val() == 1) {
			if (($('#booleanValueNuovaSim').val() == '') && ($('#booleanValuePassaVodafone').val() == '')) {
				alert('ERRORE!!!!');
				return false;
			}
		}
	});	
	
	
	$('.tooltip:not(.nohide)').hide();
        $('.has_tooltip').bind('mouseenter', function(){
			var rel = $(this).attr('rel').split('|');
            showTooltip(this, rel[0], rel[1]);
        });
        $('.has_tooltip').bind('mouseleave', function(){
            var rel = $(this).attr('rel').split('|');
            hideTooltip(rel[0]);
        });   
	
});





function checkRequiredChiamate (e) {
	
	var tot = parseInt($('#Chiamate').val());
	var vod = parseInt($('#ChiamateVodafone').val());
	var alt = parseInt($('#ChiamateAltri').val());
	
	if (isNaN(tot)) tot = 0;
	if (isNaN(vod)) vod = 0;
	if (isNaN(alt)) alt = 0;
	
	if (e) {
		
		if (e.id == 'Chiamate' && vod >= 0 && alt >= 0) {
			$('#ChiamateAltri').val('');
			$('#ChiamateVodafone').val('');
		} else if (e.id == 'Chiamate' && vod >= 0) {
			$('#ChiamateAltri').val(tot- vod);
		} else if (e.id == 'Chiamate' && alt >= 0) {
			$('#ChiamateVodafone').val(tot - alt);
		}
		
		if (e.id == 'ChiamateVodafone' && tot >= vod) {
			$('#ChiamateAltri').val(tot - vod);
		} else if (e.id == 'ChiamateVodafone' && alt >= 0) {
			$('#Chiamate').val(vod + alt);
		}
	
		if (e.id == 'ChiamateAltri' && tot >= alt) {
			$('#ChiamateVodafone').val(tot - alt);
		} else if (e.id == 'ChiamateAltri' && vod >= 0) {
			$('#Chiamate').val(vod + alt);
		}
		
	}
	
	if (tot <= vod) {
		$('#ChiamateVodafone').addClass('required');
		$('#ChiamateAltri').removeClass('required');
	} else if (tot <= alt) {
		$('#ChiamateAltri').addClass('required');
		$('#ChiamateVodafone').removeClass('required');
	} else {
		$('#ChiamateAltri').addClass('required');
		$('#ChiamateVodafone').addClass('required');
	}
	
}

function checkRequiredSMS (e) {
	
	var tot = parseInt($('#Messaggi').val());
	var vod = parseInt($('#MessaggiVodafone').val());
	var alt = parseInt($('#MessaggiAltri').val());
	
	if (isNaN(tot)) tot = 0;
	if (isNaN(vod)) vod = 0;
	if (isNaN(alt)) alt = 0;
	
	if (e) {
		

		if (e.id == 'Messaggi' && vod >= 0 && alt >= 0) {
			$('#MessaggiAltri').val('');
			$('#MessaggiVodafone').val('');
		} else if (e.id == 'Messaggi' && vod >= 0) {
			$('#MessaggiAltri').val(tot - vod);
		} else if (e.id == 'Messaggi' && alt >= 0) {
			$('#MessaggiVodafone').val(tot - alt);
		}
		
		if (e.id == 'MessaggiVodafone' && tot >= vod) {
			$('#MessaggiAltri').val(tot - vod);
		} else if (e.id == 'MessaggiVodafone' && alt >= 0) {
			$('#Messaggi').val(vod + alt);
		}
	
		if (e.id == 'MessaggiAltri' && tot >= alt) {
			$('#MessaggiVodafone').val(tot - alt);
		} else if (e.id == 'MessaggiAltri' && vod >= 0) {
			$('#Messaggi').val(vod + alt);
		} 
		
	}	
	
	if (tot <= vod) {
		$('#MessaggiVodafone').addClass('required');
		$('#MessaggiAltri').removeClass('required');
	} else if (tot <= alt) {
		$('#MessaggiAltri').addClass('required');
		$('#MessaggiVodafone').removeClass('required');
	} else {
		$('#MessaggiAltri').addClass('required');
		$('#MessaggiVodafone').addClass('required');
	}
	
}













/*
* Translated default messages for the jQuery validation plugin.
* Language: IT
*/
jQuery.extend(jQuery.validator.messages, {
required: "Questo campo è obbligatorio.",
remote: "Riempire questo campo per continuare.",
email: "Inserire un indirizzo email valido.",
url: "Inserire un indirizzo URL valido.",
date: "Inserire una data in formato mm-gg-aaaa.",
dateDE: "Inserire una data in formato gg-mm-aaaa.",
dateISO: "Inserire una data in formato aaaa-mm-gg.",
number: "Inserire un numero.",
digits: "Inserire (solo) un numero.",
creditcard: "Inserire un numero di carta di credito valido.",
//equalTo: "Inserire lo stesso valore usato sopra.",
equalTo: "Valore non corretto.",
equalValueChiamate: "Ti ricordiamo che il valore indicato deve corrispondere alla somma dei valori riportati per Chiamate verso numeri Vodafone e Chiamate verso numeri di Altri operatori.",
equalValueSMS: "Ti ricordiamo che il valore indicato deve corrispondere alla somma dei valori riportati per SMS verso numeri Vodafone e SMS verso numeri di Altri operatori.",
accept: "Usare un'estensione valida.",
maxlength: jQuery.format("Inserire al massimo {0} caratteri."),
minlength: jQuery.format("Inserire almeno {0} caratteri."),
rangelength: jQuery.format("Inserire da {0} a {1} caratteri."),
range: jQuery.format("Inserire un numero compreso tra {0} e {1}."),
max: jQuery.format("Inserire un numero minore o uguale a {0}."),
min: jQuery.format("Inserire un numero maggiore o uguale a {0}.")
});


