$(function(){
/*
	if (!jQuery.browser.msie || (jQuery.browser.version >= 7 && jQuery.browser.msie))
		$('head').append('<link href="/styles/card.css" media="screen" rel="stylesheet" type="text/css" />');
*/

    $('#steptwo').click(function(){
        var digits = new RegExp('^[0-9]+$');
        if ( digits.test( $('#costoCardQuantita').val() ) ){
            $('#wizard_two').css('opacity', '0');
            $('#wizard_two').css('display', 'block');
            $('#wizard_one').animate({opacity: 0}, 3000, 'easeInExpo', function(){;});
            $('#wizard_two').animate({opacity: 1}, 3000, 'easeOutExpo', function(){;});
            $('#cardListCalculator').animate({
                left: '-795px'
            }, 2000, function(){;});
            $('#fieldset-base').animate({
                opacity: 0
            }, 1000, function(){
                $('#fieldset-opzioni').animate({
                    opacity: 1
                }, 1000, function(){});
            });
        } else {
            alert("Nel campo quantità non ci devono essere caratteri non numerici. \r\n ps Fai attenzione alle quantità minime.");
        }
    });
    
    $('#stepthree').click(function(){
        $('#wizard_three').css('opacity', '0');
        $('#wizard_three').css('display', 'block');
        $('#wizard_two').animate({opacity: 0}, 3000, 'easeInExpo', function(){;});
        $('#wizard_three').animate({opacity: 1}, 3000, 'easeOutExpo', function(){;});
        $('#cardListCalculator').animate({
            left: '-1592px'
        }, 2000, function(){;});
        $('#fieldset-opzioni').animate({
            opacity: 0
        }, 1000, function(){
            $('#fieldset-personalizzazioni').animate({
                opacity: 1
            }, 1000, function(){});
        });
    });
    
    $('#stepfour').click(function(){
        $.ajax({
            url:'/support/index/do-math',
            data: getFormValues(),
            dataType: 'json',
            type: "POST",
            success: function(response){
                if(response['status'] == 'ko') {
                    alert(response['error']);
                } else if (response['status'] == 'login') {
                    alert('Devi effettuare il login per vedere i prezzi.');
                    return false;
                } else {
                    var costo = response['costo'] / 1000;
                    $('#costounitario').val( costo + " €" );

                    var costototale = costo * response['form']['costoCardQuantita'];
                    costototale = Math.round(costototale * Math.pow(10, 2)) / Math.pow(10, 2);
                    $('#costototale').val( costototale + " €" );

                    $('#wizard_four').css('opacity', '0');
                    $('#wizard_four').css('display', 'block');
                    $('#wizard_three').animate({opacity: 0}, 3000, 'easeInExpo', function(){;});
                    $('#wizard_four').animate({opacity: 1}, 3000, 'easeOutExpo', function(){;});
                    $('#cardListCalculator').animate({
                        left: '-2390px'
                    }, 2000, function(){;});
                    $('#fieldset-personalizzazioni').animate({
                        opacity: 0
                    }, 1000, function(){
                        $('#fieldset-calc, #fieldset-askquotation').animate({
                            opacity: 1
                        }, 1000, function(){});
                    });
                    
                    return true;
                }
            }
        });
    });
    
    $('#backtoone').click(function(){
        $('#wizard_two').animate({opacity: 0}, 3000, 'easeOutExpo', function(){;});
        $('#wizard_one').animate({opacity: 1}, 3000, 'easeOutExpo', function(){;});
        $('#cardListCalculator').animate({
            left: '0px'
        }, 2000, function(){;});
        $('#fieldset-opzioni').animate({
            opacity: 0
        }, 1000, function(){
            $('#fieldset-base').animate({
                opacity: 1
            }, 1000, function(){});
        });
    });
    
    $('#backtotwo').click(function(){
        $('#wizard_three').animate({opacity: 0}, 3000, 'easeOutExpo', function(){;});
        $('#wizard_two').animate({opacity: 1}, 3000, 'easeOutExpo', function(){;});
        $('#cardListCalculator').animate({
            left: '-795px'
        }, 2000, function(){;});
        $('#fieldset-personalizzazioni').animate({
            opacity: 0
        }, 1000, function(){
            $('#fieldset-opzioni').animate({
                opacity: 1
            }, 1000, function(){});
        });
    });
    
    $('#backtothree').click(function(){
        $('#wizard_four').animate({opacity: 0}, 3000, 'easeOutExpo', function(){;});
        $('#wizard_three').animate({opacity: 1}, 3000, 'easeOutExpo', function(){;});
        $('#cardListCalculator').animate({
            left: '-1592px'
        }, 2000, function(){;});
        $('#fieldset-calc, #fieldset-askquotation').animate({
            opacity: 0
        }, 1000, function(){
            $('#fieldset-personalizzazioni').animate({
                opacity: 1
            }, 1000, function(){});
        });
    });

	/*
	 * Setup inline help
	 * 
	 */
	$('#cardHelp span').hide();
	$('#hlp').show();
	
	// setup eventi inline help
	$('#costoCardQuantita').click(function(){$('#cardHelp span').hide();$('#qty').show('slow');});
	$('#firma').click(function(){$('#cardHelp span').hide();$('#frm').show('slow');});
	$('#coloreserigrafico').click(function(){$('#cardHelp span').hide();$('#srg').show('slow');});
	$('#colorepantone').click(function(){$('#cardHelp span').hide();$('#pnt').show('slow');});
	$('#punzonatura').click(function(){$('#cardHelp span').hide();$('#pnz').show('slow');});
	$('#asolaturacard').click(function(){$('#cardHelp span').hide();$('#asl').show('slow');});
	$('#nomeprogettocard').click(function(){$('#cardHelp span').hide();$('#nme').show('slow');});
	$('#note').click(function(){$('#cardHelp span').hide();$('#nte').show('slow');});
	$('#chipcontatto').click(
			function() {
					$('#cardHelp span').hide();
					switch($('#chipcontatto').val()){
						case 'no':$('#nochip').show('slow');break;
						case 'sle4442':$('#chpSLE4442').show('slow');break;
						case 'sle4428':$('#chpSLE4428').show('slow');break;
						case 'sle5542':$('#chpSLE5542').show('slow');break;
						case 'at24c04':$('#chpAT24C04').show('slow');break;
						default: $('#cnt').show('slow');break;
					}
				}
		);
	$('#chipcrfid').click(
			function() {
					$('#cardHelp span').hide();
					switch($('#chipcrfid').val()){
						case 'no':$('#chpnorfid').show('slow');break;
						case 'em4100':$('#chpEM4100').show('slow');break;
						case 't5567':$('#chpT5567').show('slow');break;
						case 'mifares501k':$('#chpmifare').show('slow');break;
						case 'icode2':$('#chpiCode2').show('slow');break;
						default: $('#rfd').show('slow');break;
					}
				}
		);
	$('#persdativariabili').click(function(){$('#cardHelp span').hide();$('#var').show('slow');});
	$('#persstampafoto').click(function(){$('#cardHelp span').hide();$('#fto').show('slow');});
	$('#colorifronte').click(function(){
		$('#cardHelp span').hide();
		if($('#colorifronte').val()=='mono'){$('#frn2').show('slow');}
		else if($('#colorifronte').val()=='quad'){$('#frn1').show('slow');}
		else $('#frn0').show('slow');
	});
	$('#coloriretro').click(function(){
		$('#cardHelp span').hide();
		if($('#coloriretro').val()=='mono'){$('#rtr2').show('slow');}
		else if($('#coloriretro').val()=='quad'){$('#rtr1').show('slow');}
		else $('#rtr0').show('slow');
	});
	$('#spessoreCard').click(function(){
		$('#cardHelp span').hide();
		if($('#spessoreCard').val()=='40'){$('#sps2').show('slow');}
		else if($('#spessoreCard').val()=='76'){$('#sps1').show('slow');}
		else $('#hlp').show();
	});
	$('#bandamagnetica').click(function(){
		$('#cardHelp span').hide();
		if($('#bandamagnetica').val()=='no')$('#bnd0').show('slow');
		else if($('#bandamagnetica').val()=='loco')$('#bnd1').show('slow');
		else $('#bnd2').show('slow');
	});
	
    $('#costoCardQuantita').change(function(e) {checkTipoStampa();});

	/*
	 * controllo login
	 */
	$('#quotation').click(function(){
		$('#domath').click();
		$('#costounitario').removeAttr("disabled");
		$('#costototale').removeAttr("disabled");
	});

	/*
	 * setup eventi per la preview
	 */
	$('#coloriretro').change(function(){preview();});
	$('#bandamagnetica').change(function(){preview();});
	$('#colorifronte').change(function(){preview();});
	$('#chipcontatto').change(function(){preview();});
	$('#chipcrfid').change(function(){preview();});
	$('#firma').change(function(){preview();});
	$('#perscodicebarre').change(function(){preview();});
	$('#persdativariabili').change(function(){preview();});
	$('#punzonatura').change(function(){preview();});
	$('#persstampafoto').change(function(){preview();});
	$('#asolaturacard').change(function(){preview();});
	$('#tipostampa').change(function(){preview();checkTipoStampa();});

	/*
	 * setup preview
	 */
	preview();
});

/*
 * funzione per le combo dati in preview
 */
function preview()
{

	// RETRO
	switch ($('#coloriretro').val()){
		case 'quad':
			$('#cpr').css('background-image', 'url(/images/card/quadricromia-[Convertito].png)');
			break;
		case 'mono':
			$('#cpr').css('background-image', 'url(/images/card/monocromia-[Convertito].png)');
			break;
		default:
			$('#cpr').css('background-image', 'url(/images/card/blank-[Convertito].png)');
			break;
	}

	// FRONTE
	switch ($('#colorifronte').val()){
		case 'quad':
			$('#cpf').css('background-image', 'url(/images/card/quadricromia-[Convertito].png)');
			break;
		case 'mono':
			$('#cpf').css('background-image', 'url(/images/card/monocromia-[Convertito].png)');
			break;
		default:
			$('#cpf').css('background-image', 'url(/images/card/blank-[Convertito].png)');
			break;
	}

	$('.data').hide();
	$('.barc').hide();
	switch ($('#persdativariabili').val()){
		case 'fronte':
			$('#cpf .data').show();
			$('#cpf .barc').show();
			break;
		case 'retro':
			$('#cpr .data').show();
			$('#cpr .barc').show();
			break;
		case 'ambo':
			$('.data').show();
			$('.barc').show();
			break;
	}

	switch ($('#persstampafoto').val()){
		case 'no':
			$('.foto').hide();
			break;
		case 'fronte':
			$('#cpf .foto').show();
			$('#cpr .foto').hide();
			break;
		case 'retro':
			$('#cpf .foto').hide();
			$('#cpr .foto').show();
			break;
	}

	switch ($('#bandamagnetica').val()){
		case 'hico':
			$('#cpr .banh').show();
			$('#cpr .banl').hide();
			break;
		case 'loco':
			$('#cpr .banh').hide();
			$('#cpr .banl').show();
			break;
		default:
			$('#cpr .banh').hide();
			$('#cpr .banl').hide();
	}

	$('#cpf .AT24C04').hide();
	$('#cpf .SLE4428').hide();
	$('#cpf .SLE4442').hide();
	$('#cpf .SLE5542').hide();
	$('#cpf .chip').hide();
	if ($('#chipcontatto').val() != 'no'){
		switch ($('#chipcontatto').val()){
			case 'at24c04':
				$('#cpf .AT24C04').show();
				break;
			case 'sle4428':
				$('#cpf .SLE4428').show();
				break;
			case 'sle4442':
				$('#cpf .SLE4442').show();
				break;
			case 'sle5542':
				$('#cpf .SLE5542').show();
				break;
			default:
				$('#cpf .chip').show();
		}
	} else if ($('#chipcrfid').val() != 'no') $('.chip').show();

	if ($('#firma').attr('checked')) $('#cpr .firm').show();
	else $('#cpr .firm').hide();

	if ($('#punzonatura').attr('checked')) $('.punz').show();
	else $('.punz').hide();

	if ($('#asolaturacard').attr('checked')) $('.asol').show();
	else $('.asol').hide();
}//**

/*
 * getValues x calcolo ajax
 */
function getFormValues(){
	var card = {};
	card.costoCardQuantita = 	$('#costoCardQuantita').val();
	card.spessoreCard = 		$('#spessoreCard').val();
	card.colorifronte = 		$('#colorifronte').val();
	card.coloriretro = 			$('#coloriretro').val();
	card.tipostampa = 			$('#tipostampa').val();
	card.nomeprogettocard = 	$('#nomeprogettocard').val();
	card.note = 				$('#note').val();
	card.bandamagnetica = 		$('#bandamagnetica').val();
	card.chipcontatto = 		$('#chipcontatto').val();
	card.chipcrfid = 			$('#chipcrfid').val();
	card.perscodicebarre = 		$('#perscodicebarre').val();
	card.persdativariabili = 	$('#persdativariabili').val();
	card.persstampafoto = 		$('#persstampafoto').val();

	card.coloreserigrafico = 	$('#coloreserigrafico').attr('checked') ? 'true' : 'false';
	card.colorepantone = 		$('#colorepantone').attr('checked') ? 'true' : 'false';
	card.firma = 				$('#firma').attr('checked') ? 'true' : 'false';
	card.punzonatura = 			$('#punzonatura').attr('checked') ? 'true' : 'false';
	card.asolaturacard = 		$('#asolaturacard').attr('checked') ? 'true' : 'false';

	return card;
}//***

/*
 * funzione di controllo qty <-> tipostampa
 */
function checkTipoStampa(){
	$('#costounitario').val('');
	$('#costototale').val('');

    var quantita = $('#costoCardQuantita').val();
    $('option[value=off]').attr('disabled', '');
	$('#tipostampa option').removeAttr('disabled');
    if (quantita == "") {
    	return;
    }else if (quantita > 999) {
    	return;
    } else {
    	$('option[value=off]').attr('disabled', 'disabled');
    	$('#tipostampa').val('dir');
    }//*****

    $('#cardHelp span').hide();
    $('#qty').show();
}//***






















