﻿var STR_PAD_LEFT = 1;
var STR_PAD_RIGHT = 2;
var STR_PAD_BOTH = 3;

function pad(str, len, pad, dir) {

	if (typeof (len) == "undefined") { var len = 0; }
	if (typeof (pad) == "undefined") { var pad = ' '; }
	if (typeof (dir) == "undefined") { var dir = STR_PAD_RIGHT; }

	if (len + 1 >= str.length) {

		switch (dir) {

			case STR_PAD_LEFT:
				str = Array(len + 1 - str.length).join(pad) + str;
				break;

			case STR_PAD_BOTH:
				var right = Math.ceil((padlen = len - str.length) / 2);
				var left = padlen - right;
				str = Array(left + 1).join(pad) + str + Array(right + 1).join(pad);
				break;

			default:
				str = str + Array(len + 1 - str.length).join(pad);
				break;

		} // switch

	}

	return str;
}

//-----------------------------------------------------------------------------------------------------------------------

function getHalfHeight(valore) {
	var x;

	x = valore.replace('px', '');
	x = Math.floor(x / 2)

	return x;
}

//-----------------------------------------------------------------------------------------------------------------------

function formatElcartCode(IdArticolo) {
		
	var tmpCode;
	var tmpCod1;
	var tmpCod2;
	var tmpCod3;
	
	var slashpos = IdArticolo.indexOf('/');        
	var dashpos = IdArticolo.indexOf('-');

	if (slashpos >= 0) {
		tmpCod1 = IdArticolo.substring(0, slashpos);
		tmpCod1 = pad(tmpCod1, 2, '0', STR_PAD_LEFT);

		if (dashpos > slashpos) {
			tmpCod2 = IdArticolo.substring(slashpos + 1, dashpos - slashpos - 1);
			tmpCod3 = IdArticolo.substring(dashpos + 1);
		}
		else {
			tmpCod2 = IdArticolo.substring(slashpos + 1);
			tmpCod3 = "";
		}

		tmpCod2 = pad(tmpCod2, 5, '0', STR_PAD_LEFT); 
		tmpCod2 = tmpCod2.substring(tmpCod2.length - 5);

		tmpCod3 = pad(tmpCod3, 2, '0', STR_PAD_LEFT); 
		tmpCod3 = tmpCod3.substring(tmpCod3.length - 2);

		tmpCode = tmpCod1 + tmpCod2 + tmpCod3;
	}

	return tmpCode;
}

//-----------------------------------------------------------------------------------------------------------------------

function searchHelp(idSection,IdLingua, hlpUrl) {

	jQuery.ajax({
		url: hlpUrl,
		data: 'IdLingua=' + IdLingua,
		dataType: "json",
		success: function (data) {

			var listObj = data["listObj"];

			jQuery('#bodyHelp').html('');

			jQuery.tmpl(tmplhelpTips, listObj).appendTo('#bodyHelp');

			jQuery('#HelpTips').bPopup({ escClose: 'true' });

		},
		error: function (e) {
			alert("errore");
		}

	});
}

//-----------------------------------------------------------------------------------------------------------------------

function viewNewsLetter(_lingua)
{
    jQuery('#iNewsLetter').attr('src', '/Content/newsletter/NewsletterRegistration' + _lingua + '.htm');    
    jQuery('#regNewsLetterDiv').bPopup();        
}

//-----------------------------------------------------------------------------------------------------------------------

function initNewsLetter() {

    jQuery('#xLetter').live('click', function () {
        jQuery('#regNewsLetterDiv').bPopup().close();
        jQuery('#HelpTips').bPopup().close();
    });

}

//-----------------------------------------------------------------------------------------------------------------------

function langDisc(_link,_lingua) {

    jQuery('#link' + _lingua).attr('href', _link);
    jQuery('#lang' + _lingua).css('display', 'block');
    jQuery('#disclaimer').bPopup({ onClose: function () {
        jQuery('#langEN').css('display', 'none');
        jQuery('#langES').css('display', 'none');
    }});

}

//-----------------------------------------------------------------------------------------------------------------------

function initBaseSettings(newsPage) {

    initNewsLetter();

    jQuery('.espandi').click(function () {

        var obj = '#' + jQuery(this).attr('id') + 'Body';

        if (jQuery(obj).css('display') == 'none') {
            jQuery(this).attr('src', '/Content/img/meno.png')
            jQuery(obj).css('display', 'block');

            if (jQuery.browser.msie && jQuery.browser.version == "8.0")            
                jQuery('#MainBlock').height(jQuery('#MainBlock').height() + jQuery(obj).height());            
        }
        else {
            jQuery(this).attr('src', '/Content/img/piu.png')
            jQuery(obj).css('display', 'none');

            if (jQuery.browser.msie && jQuery.browser.version == "8.0")
                jQuery('#MainBlock').height(600);

        }

    });


	jQuery('.mOver').live('mouseover', function () {
		jQuery(this).css("cursor", "pointer");
	});

	jQuery('.closeX').live('mouseover mouseout click', function (evt) {

		if (evt.type == "mouseover") {
			jQuery(this).css("text-decoration", "underline");
			jQuery(this).css("cursor", "pointer");
		}

		if (evt.type == "mouseout") {
			jQuery(this).css("text-decoration", "none");            
		}

		if (evt.type == "click") {
			jQuery(this).parent().parent().bPopup().close();
		}
	});


	jQuery('#NewsTitle').live('click', function () {

		window.location = newsPage;

	});

}

//----------------------------------------------------------------------------------------------------

jQuery.fn.center = function () {
	this.css("position", "absolute");
	this.css("top", ((63 - this.height()) / 2));
	this.css("left", ((106 - this.width()) / 2));
	return this;
}

//----------------------------------------------------------------------------------------------------

function sezThumbLink(obj,objHref) {

	if (jQuery(obj).children().attr("class") == "thumbSezioniText")
		jQuery(obj).mouseover(function () {
			jQuery(obj).css("cursor", "pointer");
		}).click(function () {
			document.location = objHref;            
		});

	}

//----------------------------------------------------------------------------------------------------

function divLink(obj, objHref) {
	
	jQuery(obj).mouseover(function () {            
		jQuery(obj).css("cursor", "pointer");
	}).click(function () {        
		document.location = objHref;
	});

}

//----------------------------------------------------------------------------------------------------

function setiMenuTitle(titolo) {

	//jQuery('#TopSection').find('#iMenu').find('#iMenuTitolo').append(titolo);
	jQuery('#iMenu').find('#iMenuTitolo').append(titolo);

}

//----------------------------------------------------------------------------------------------------

function setiMenuSection(sezione,viewShadow) {

	//jQuery('#TopSection').find('#iMenu').find(sezione).find('a').removeClass('lnkiMenu').addClass('lnkiMenuSel');
	jQuery('#iMenu').find(sezione).find('a').removeClass('lnkiMenu').addClass('lnkiMenuSel');

	if (viewShadow)
		jQuery('#iMenuShadow').css('display', 'inline-block');
}

//----------------------------------------------------------------------------------------------------

function initiMenu() {

	jQuery('#imgViewMenu').click(function () {
		iMenuEngine();
	});

	jQuery('#imgViewMenuSM').click(function () {
		iMenuSMEngine();
	});
}

//----------------------------------------------------------------------------------------------------

function setiMenuOpen() {
	jQuery('#iMenu').removeClass('iMenu').addClass('iMenuOpen');
	jQuery('#iMenuInterno').removeClass('iMenuInterno').addClass('iMenuInternoOpen');
	jQuery('#iMenuContenuto').removeClass('iMenuContenuto').addClass('iMenuContenutoOpen');
	jQuery('#iMenuFooter').css('display', 'none');
	jQuery('#iMenuVoci').css('display', 'block');

}

//----------------------------------------------------------------------------------------------------

function setiMenuClose() {
    jQuery('#iMenu').removeClass('iMenuOpen').addClass('iMenu');
    jQuery('#iMenuInterno').removeClass('iMenuInternoOpen').addClass('iMenuInterno');
    jQuery('#iMenuContenuto').removeClass('iMenuContenutoOpen').addClass('iMenuContenuto');    
    jQuery('#iMenuFooter').css('display', 'block');
    jQuery('#iMenuVoci').css('display', 'none');
}

//----------------------------------------------------------------------------------------------------

function initPreventivo(linkQta, linkInvio, linkDettaglio) {

	var paramsInvio = "";

//	jQuery('#xPreventivo').live('click', function () {
//		jQuery('#dettaglioPreventivo').bPopup().close();        
//	});

	jQuery('#GoToPreventivo').click(function () {	    

	    jQuery('#dettaglioPreventivoDiv').load(linkDettaglio, '', function () {

	        jQuery("#dettaglioPreventivo").bPopup({ escClose: 'true', follow: [false, false] });

	    });

	});

	jQuery("#preventivoForm").validate();

	jQuery("#preventivoForm").live('submit', function () {

	    if (jQuery("#preventivoForm").valid()) {

	        paramsInvio = 'societa=' + jQuery('#societa').attr('value') + '&responsabile=' + jQuery('#responsabile').attr('value') + '&indirizzo=' + jQuery('#indirizzo').attr('value')
				+ '&cap=' + jQuery('#CAP').attr('value') + '&citta=' + jQuery('#citta').attr('value') + '&prov=' + jQuery('#provincia').attr('value') + '&telefono=' + jQuery('#telefono').attr('value')
				+ '&fax=' + jQuery('#fax').attr('value') + '&cellulare=' + jQuery('#cellulare').attr('value') + '&codFisc=' + jQuery('#codicefiscale').attr('value') + '&email=' + jQuery('#email').attr('value')
				+ '&note=' + jQuery('#note').attr('value') + '&tipo=' + jQuery('#preventivoForm').find(':checked').attr('value');

	        jQuery.ajax({
	            url: linkInvio,
	            data: paramsInvio,
	            dataType: "json",
	            success: function (data) {

	                var result = data["res"];

	                if (result) {
	                    alert("Invio preventivo effettuato correttamente");
	                    jQuery('.closeX').trigger('click');
	                }
	                else
	                    alert("Errore");

	            },
	            error: function (data) {
	                //alert("Errore JSon");
	            }
	        });	        
	    }

	    return false;
	});

	jQuery('#aggiornaQta').live('click', function () {

	    var elencoArt = "";
	    var elencoQt = "";
	    var elencoInp = jQuery('#prevRighe').find('input:text');

	    for (var i = 0; i < elencoInp.length; i++) {
	        elencoArt += jQuery(elencoInp[i]).attr('id') + '-';
	        elencoQt += jQuery(elencoInp[i]).attr('value') + '-';
	    }

	    if (elencoArt != "")
	        elencoArt = elencoArt.substring(0, elencoArt.length - 1);

	    if (elencoQt != "")
	        elencoQt = elencoQt.substring(0, elencoQt.length - 1);

	    jQuery.ajax({
	        url: linkQta,
	        data: 'articoli=' + elencoArt + '&qta=' + elencoQt,
	        dataType: "json",
	        success: function (data) {

	            var result = data["res"];

	            jQuery.each(result, function (i, v) {
	                jQuery('#qta_' + i).html(v);	                
	                jQuery('#' + i).val(v);	                
	            });

	            //				for (var y = 0; y < result.length; y++)
	            //						jQuery('#qta_' + jQuery(result[y].).attr('id')).html(jQuery(elencoInp[y]).attr('value'));
	            //				
	            //				else
	            //					alert("Errore");

	        },
	        error: function (data) {
	            //alert("Errore JSon");
	        }
	    });
	});
}
