﻿// JScript File
//window.onerror = ShowErr;

function FindCtrl(id)
{
    return (document.getElementById) ? document.getElementById(id) : eval('document.all.' + id);
}

function ShowErr(err,sURL,sLine)
{
    try 
    {
        var HtmlMsg = '';
        msgArray = new Array();
        urlArray = new Array();
        lnoArray = new Array();
        msgArray[msgArray.length] = err;
        urlArray[urlArray.length] = sURL;
        lnoArray[lnoArray.length] = sLine;
        var MsgErr = '';
        var Em = document.getElementById('imgErrJs');
        if(Em!=null)
        {
            for (var i=0; i < msgArray.length; i++) 
            {
                MsgErr += 'Errore:[' + msgArray[i] + ']';
                MsgErr += '  nel File:[' + urlArray[i] + '] alla linea:[' + lnoArray[i] + ']';
            }

            MsgErr = MsgErr.replace("'","\'");
            HtmlMsg = '/utility/InvioEmailErrJs.aspx?ErrJs='+MsgErr;
            Em.src = HtmlMsg;
        }
        else    // Html ancora non creato
        {
        }
        return true;
    }
    catch(er)
    {
        //alert(err+'\nLinea:'+sLine+'\nin:'+sURL);
        //return false;
        return true;
    }
}
function Evidenzia(oggettoTd)
{
	oggettoTd.className = 'TdElencoProdottiAttivo';
}

function deEvidenzia(oggettoTd)
{
	oggettoTd.className = 'TdElencoProdotti';
}
function StondaDiv() {
  var divs = document.getElementsByTagName('div');
  var rounded_divs = [];
  for (var i = 0; i < divs.length; i++) {
    if (/\bdivStondato\b/.exec(divs[i].className)) {
      rounded_divs[rounded_divs.length] = divs[i];
    }
  }
  for (var i = 0; i < rounded_divs.length; i++) {
    var original = rounded_divs[i];
    /* Make it the inner div of the four */
    //original.className = original.className.replace('divStondato', 'divStondato');
    /* Now create the outer-most div */
    var t = document.createElement('div');
    t.className = 't';
    /* Swap out the original (we'll put it back later) */
    original.parentNode.replaceChild(t, original);

    t.style.width = original.style.width;

    var b = document.createElement('div');
    b.className = "b";
    //b.style.width = original.style.width;
    var l = document.createElement('div');
    l.className = "l";
    //l.style.width = original.style.width;
    var r = document.createElement('div');
    r.className = "r";
    //r.style.width = original.style.width;
    var bl = document.createElement('div');
    bl.className = "bl";
    //bl.style.width = original.style.width;
    var br = document.createElement('div');        
    br.className = "br";
    //br.style.width = original.style.width;
    var tl = document.createElement('div');
    tl.className = "tl";
    //tl.style.width = original.style.width;
    var tr = document.createElement('div');        
    tr.className = "tr";   
    //tr.style.width = original.style.width; 

    t.appendChild(b);
    b.appendChild(l);
    l.appendChild(r);
    r.appendChild(bl);
    bl.appendChild(br);
    br.appendChild(tl);
    tl.appendChild(tr);        
    tr.appendChild(original);
  }
}


function StondaDivGra() {
  var divs = document.getElementsByTagName('div');
  var rounded_divs = [];
  for (var i = 0; i < divs.length; i++) {
    if (/\bdivStondatoGra\b/.exec(divs[i].className)) {
      rounded_divs[rounded_divs.length] = divs[i];
    }
  }
  for (var i = 0; i < rounded_divs.length; i++) {
    var originalGra = rounded_divs[i];
    /* Make it the inner div of the four */
    //original.className = original.className.replace('divStondato', 'divStondato');
    /* Now create the outer-most div */
    var tGra = document.createElement('div');
    tGra.className = 'tGra';
    /* Swap out the original (we'll put it back later) */
    originalGra.parentNode.replaceChild(tGra, originalGra);

    tGra.style.width = originalGra.style.width;

    var bGra = document.createElement('div');
    bGra.className = "bGra";
    //b.style.width = original.style.width;
    var lGra = document.createElement('div');
    lGra.className = "lGra";
    //l.style.width = original.style.width;
    var rGra = document.createElement('div');
    rGra.className = "rGra";
    //r.style.width = original.style.width;
    var blGra = document.createElement('div');
    blGra.className = "blGra";
    //bl.style.width = original.style.width;
    var brGra = document.createElement('div');        
    brGra.className = "brGra";
    //br.style.width = original.style.width;
    var tlGra = document.createElement('div');
    tlGra.className = "tlGra";
    //tl.style.width = original.style.width;
    var trGra = document.createElement('div');        
    trGra.className = "trGra";   
    //tr.style.width = original.style.width; 

    tGra.appendChild(bGra);
    bGra.appendChild(lGra);
    lGra.appendChild(rGra);
    rGra.appendChild(blGra);
    blGra.appendChild(brGra);
    brGra.appendChild(tlGra);
    tlGra.appendChild(trGra);        
    trGra.appendChild(originalGra);
  }
}
var wnd = null;
function ApriZoom(Img, Wid, Hei)
{
    var wndParams = 'scrollbarno,resizable=no,menu=no,status=no,width=' + (Wid+10) + ',height=' + (Hei+10);
    if(wnd != null) { if(!wnd.closed) { wnd.close(); } }
    wnd = open('/blank.htm', 'Zoom', wndParams);
    wnd.document.open();
    wnd.document.write('<html><head><title>Freddy Product Zoom</title></head><body onload="self.focus();" style="margin: 0px; text-align: center;"><a href="javascript:self.close();"><img src="' + Img + '" border="0"></a></body></html>');
    wnd.document.close();
}

function CheckCodiceFiscale(CodiceFiscale)
{
    var result = false;
    var caratteri = 16;
    
    if (CodiceFiscale == null)
        return result;

    if (CodiceFiscale.length != caratteri)
        return result;

    // stringa per controllo e calcolo omocodia 
    var omocodici = "LMNPQRSTUV";
    // per il calcolo del check digit e la conversione in numero
    var listaControllo = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    var listaPari = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25);
    var listaDispari = new Array(1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23);

    CodiceFiscale = CodiceFiscale.toUpperCase();

    var chkOmoCod = "\\d|" + omocodici;
    var rgx = new RegExp("^[A-Z]{6}[" + chkOmoCod + "]{2}[A-Z][" + chkOmoCod + "]{2}[A-Z][" + chkOmoCod + "]{3}[A-Z]$");
    var m = rgx.exec(CodiceFiscale);
    result = (m != null);

    if(result)
    {
        var somma = 0;
        for (var i = 0; i < 15; i++)
        {
            var c = CodiceFiscale.charAt(i);
            var x = "0123456789".indexOf(c);
            if (x != -1) { c = listaControllo.charAt(x); }
            x = listaControllo.indexOf(c);
            // i modulo 2 = 0 è dispari perchè iniziamo da 0
            if ((i % 2) == 0)
                x = listaDispari[x];
            else
                x = listaPari[x];
            somma += x;
        }
        result = (listaControllo.charAt((somma % 26)) == CodiceFiscale.charAt(15));
    }
    return result;
}

function CheckValiditaData(DateValue)
{
    var checkstr = "0123456789";
    //var DateField = oData;
    //var Datevalue = "";
    var DateTemp = "";
    var seperator = "/";
    var day;
    var month;
    var year;
    var leap = 0;
    var err = 0;
    var i;

    err = 0;
    //DateValue = DateField.value;

    for (i = 0; i < DateValue.length; i++)
    {
        if (checkstr.indexOf(DateValue.substr(i,1)) >= 0)
        {
            DateTemp = DateTemp + DateValue.substr(i,1);
        }
    }
    DateValue = DateTemp;
    /* Always change date to 8 digits - string*/
    /* if year is entered as 2-digit / always assume 20xx */
    if (DateValue.length == 6) { DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
    if (DateValue.length != 8) { err = 19; }
    /* year is wrong if year = 0000 */
    year = DateValue.substr(4,4);
    if (year == 0) { err = 20; }
    /* Validation of month*/
    month = DateValue.substr(2,2);
    if ((month < 1) || (month > 12)) { err = 21; }
    /* Validation of day*/
    day = DateValue.substr(0,2);
    if (day < 1) { err = 22; }
    /* Validation leap-year / february / day */
    if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) { leap = 1; }
    if ((month == 2) && (leap == 1) && (day > 29)) { err = 23; }
    if ((month == 2) && (leap != 1) && (day > 28)) { err = 24; }
    /* Validation of other months */
    if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) { err = 25; }
    if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) { err = 26; }
    /* if 00 ist entered, no error, deleting the entry */
    if ((day == 0) && (month == 0) && (year == 00)) { return false; }
    /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
    if (err == 0) { return true; }
    /* Error-message if err != 0 */
    else { return false; }
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

/* Cambio Lingua */
function clipValues(obj, which)
{
	if(obj.clip)
	{
		var re = /rect\(([0-9]+).*[ |,]([0-9]+).*[ |,]([0-9]+).*[ |,]([0-9]+).*\)/;
		var clipv = re.exec(obj.clip);
		if (which=="t") return Number(clipv[1]);
		if (which=="r") return Number(clipv[2]);
		if (which=="b") return Number(clipv[3]);
		if (which=="l") return Number(clipv[4]);
	}
	return null;
}
function clipTo(obj, t, r, b, l)
{
	if(obj.clip) { obj.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)"; }
}
function clipBy(obj, t, r, b, l)
{
	if(obj.clip) { obj.clip = "rect("+(this.clipValues(obj,'t')+t)+"px "+(this.clipValues(obj,'r')+r)+"px "+Number(this.clipValues(obj,'b')+b)+"px "+Number(this.clipValues(obj,'l')+l)+"px)"; }
}
var openTimeout = null;
var closeTimeout = null;
function OpenCloseLang(e)
{
    if (!e && window.event) e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();

    if(openTimeout == null)
        OpenLang();
    else
        CloseLang();
}
function OpenLang()
{
    var tbl = document.getElementById('tdCntLangMerc');
    var dv = document.getElementById('dvMercLang');
	var stepClip = 10;
	var maxHei = tbl.offsetHeight + 5;
	var curHei = dv.offsetHeight;
	if(curHei < maxHei)
	{
		if(curHei < maxHei) { curHei += stepClip; } else { curHei = maxHei; }
		if(dv.style) { dv.style.height = curHei + 'px'; }
		else { dv.height = curHei + 'px'; }
		clearTimeout(closeTimeout); closeTimeout = null;
		openTimeout = setTimeout("OpenLang()",10);
	}
}
function CloseLang()
{
    var dv = document.getElementById('dvMercLang');
	var stepClip = 10;
	var minHei = 0;
	var curHei = dv.offsetHeight;
	if(curHei > minHei)
	{
		if(curHei > minHei) { curHei -= stepClip; } else { curHei = minHei; }
		if(dv.style) { dv.style.height = curHei + 'px'; }
		else { dv.height = curHei + 'px'; }
		clearTimeout(openTimeout); openTimeout = null;
		closeTimeout = setTimeout("CloseLang()",10);
	}
}
function LangMerc(merc)
{
    if(currLng == null) { currLng = 1; }
    if(currMkt == null) { currLng = 'ITA'; }
    if(chkNaz == null) { currLng = 'ITA'; }

    var lng = currLng; var mkt = currMkt; var naz = chkNaz;
    switch(merc)
    {
        case 'ITA': lng = 1; mkt = 'ITA'; naz = 'ITA'; break;
        case 'UK': lng = 2; mkt = 'CEE'; naz = 'UK'; break;
        case 'FR': lng = 3; mkt = 'CEE'; naz = 'FR'; break;
        case 'ES': lng = 4; mkt = 'CEE'; naz = 'ES'; break;
        case 'CEE': lng = 2; mkt = 'CEE'; naz = 'CEE'; break;
        case 'EXT': lng = 2; mkt = 'EXT'; naz = 'USA'; break;
    }
    if(currLng == lng && currMkt == mkt && chkNaz == naz)
    {
        var dv1 = document.getElementById('dvMerc');
        var dv2 = document.getElementById('dvMercBox');
        if(dv1 && dv2)
        {
            dv1 = (dv1.style) ? dv1.style : dv1;
            dv2 = (dv2.style) ? dv2.style : dv2;
            dv1.display = 'none';
            dv2.display = 'none';
        }
        else
        {
            CloseLang();
        }
    }
    else
    {
        self.location.href = '/default.aspx?MERC=' + merc;
    }
}


/* Nuove Funzioni */
function AjaxPopup(popupTitle, popupUrl, htmlNodeToExtract) {
    // create dialog with loading
    var dialog = $('<div style="display:hidden">Loading...</div>').appendTo('body');
    // show the dialog
    dialog.dialog({
        width: 750,
        height: 500,
        title: popupTitle,
        modal: true,
        draggable: false,
        resizable: false
    });
    // load remote content
    dialog.load(popupUrl + ((htmlNodeToExtract != undefined && htmlNodeToExtract != null && htmlNodeToExtract != '') ? ' ' + htmlNodeToExtract : ''));
}
function HtmlPopup(popupTitle, htmlNodeIdToInsert) {
    var node = jQuery('#' + htmlNodeIdToInsert);
    if (node != null) {
        //var dialog = $('<div style="display:hidden">Loading...</div>').appendTo('body');
        node.dialog({
            width: 750,
            height: 500,
            title: popupTitle,
            modal: true,
            draggable: false,
            resizable: false
        });
    }
}
