var Giu = true;
var L, T, X, Y;

if (window.Event && document.captureEvents) 
    document.captureEvents(Event.MOUSEMOVE || Event.MOUSEUP || Event.MOUSEDOWN);

function cambiascritta(tipo){
    if (tipo == 1) {
        document.getElementById("boxseltra").firstChild.nodeValue = "Trascina per spostare";
    }
    else {
        document.getElementById("boxseltra").firstChild.nodeValue = "Seleziona data";
    }
}

function Coordinate(e){
    if (document.getElementById("calendariettop").style.display == "block") {
        Giu = true;
        document.onmousemove = Muovi;
        document.onmousedown = Coordinate;
        
        if (window.event) {
            e = window.event;
            X = e.clientX;
            Y = e.clientY;
            L = document.getElementById("calendariettop").style.pixelLeft;
            T = document.getElementById("calendariettop").style.pixelTop;
        }
        else {
            X = e.pageX;
            Y = e.pageY;
            L = document.getElementById("calendariettop").style.left;
            T = document.getElementById("calendariettop").style.top;
        }
    }
}

function Muovi(e){
    if (Giu == true) {
        var mouseX = 0;
        var mouseY = 0;
        if (window.event) {
            e = window.event;
            mouseX = e.clientX;
            mouseY = e.clientY;
            document.getElementById("calendariettop").style.pixelLeft = (L + mouseX - X);
            document.getElementById("calendariettop").style.pixelTop = (T + mouseY - Y);
        }
        else {
            mouseX = e.pageX;
            mouseY = e.pageY;
            if (L == '') {
                document.getElementById("calendariettop").style.left = (L + mouseX - X) + "px";
            }
            else {
                document.getElementById("calendariettop").style.left = (parseInt(L) + mouseX - X) + "px";
            }
            if (T == '') {
                document.getElementById("calendariettop").style.top = (T + mouseY - Y) + "px";
            }
            else {
                document.getElementById("calendariettop").style.top = (parseInt(T) + mouseY - Y) + "px";
            }
        }
    }
}

function Su(){
    Giu = false;
}

document.onmouseup = Su;

function calendarietto(campoDataID, campoData){
    if (document.getElementById(campoDataID).disabled) {
        return;
    }
    if (campoData == 'gg/mm/aaaa' || campoData == 'dd/mm/yyyy' || campoData == 'dd/mm/jjjj') {
      exd = new Date();
			campoData = exd.getDate()+'/'+(parseInt(exd.getMonth())+1)+'/'+exd.getFullYear();
			
    }
    if (isDate(campoData)) {
        campoDataSplit = campoData.split(dtCh);
        liveReq3 = false;
        if (window.XMLHttpRequest) {
            liveReq3 = new XMLHttpRequest();
        }
        else 
            if (window.ActiveXObject) {
                liveReq3 = new ActiveXObject("Microsoft.XMLHTTP");
            }
        if (liveReq3 && liveReq3.readyState < 4) {
            liveReq3.abort();
        }
        
        var url41 = "/index.php?xsl=196&s=" + s + "&v=5&c=" + cCat + "&g=" + campoDataSplit[0] + "&m=" + campoDataSplit[1] + "&y=" + campoDataSplit[2] + "&idcampo=" + campoDataID + "&sel=0";
        liveReq3.onreadystatechange = liveReq3Fun;
        liveReq3.open("GET", url41);
        liveReq3.send(null);
    }
}

function calendarietto2(campoDataID, gg, mm, aaaa){
    liveReq3 = false;
    if (window.XMLHttpRequest) {
        liveReq3 = new XMLHttpRequest();
    }
    else 
        if (window.ActiveXObject) {
            liveReq3 = new ActiveXObject("Microsoft.XMLHTTP");
        }
    if (liveReq3 && liveReq3.readyState < 4) {
        liveReq3.abort();
    }
    var url41 = "/index.php?xsl=196&s=" + s + "&v=5&c=" + cCat + "&g=" + gg + "&m=" + mm + "&y=" + aaaa + "&idcampo=" + campoDataID + "&sel=0";
    liveReq3.onreadystatechange = liveReq3Fun;
    liveReq3.open("GET", url41);
    liveReq3.send(null);
    return false;
}

function calendariettoSel(idgiorno,idmese,idanno) {
  liveReq3 = false;
  if (window.XMLHttpRequest) {
    liveReq3 = new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    liveReq3 = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (liveReq3 && liveReq3.readyState < 4) {
    liveReq3.abort();
  }
  var giorno = document.getElementById(idgiorno);
  var mese = document.getElementById(idmese);
  var anno = document.getElementById(idanno);

  var url41 = "/index.php?xsl=196&s=" + s + "&v=5&c=" + cCat + "&g=" + giorno.options[giorno.options.selectedIndex].value + "&m=" + mese.options[mese.options.selectedIndex].value + "&y=" + anno.options[anno.options.selectedIndex].value + "&idgiorno=" + idgiorno + "&idmese=" + idmese + "&idanno=" + idanno + "&sel=1";
  liveReq3.onreadystatechange = liveReq3Fun;
  liveReq3.open("GET",url41 );
  liveReq3.send(null);
}

function calendariettoSel2(idgiorno,idmese,idanno,giorno,mese,anno) {
  liveReq3 = false;
  if (window.XMLHttpRequest) {
    liveReq3 = new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    liveReq3 = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (liveReq3 && liveReq3.readyState < 4) {
    liveReq3.abort();
  }
  var url41 = "/index.php?xsl=196&s=" + s + "&v=5&c=" + cCat + "&g=" + giorno + "&m=" + mese + "&y=" + anno + "&idgiorno=" + idgiorno + "&idmese=" + idmese + "&idanno=" + idanno + "&sel=1";
  liveReq3.onreadystatechange = liveReq3Fun;
  liveReq3.open("GET",url41 );
  liveReq3.send(null);
  return false;
}


function liveReq3Fun(){
    if (liveReq3.readyState == 4) {
        var calendariettop = document.getElementById("calendariettop");
        calendariettop.innerHTML = liveReq3.responseText;
        document.getElementById("calendariettop").style.display = "block";
        document.getElementById("divcontenitore").style.zIndex = "10";
    }
}

function chiudical(){
    document.getElementById("calendariettop").style.display = "none";
    document.getElementById("divcontenitore").style.zIndex = "-10";
    return false;
}

function valorizza(idcampo, giorno, mese, anno){
    document.getElementById(idcampo).value = giorno + '/' + mese + '/' + anno;
    chiudical();
    return false;
}

function valorizza2(idgiorno,idmese,idanno,giorno,mese,anno) {
  document.getElementById(idgiorno).value = giorno;
  document.getElementById(idmese).value = mese;
  document.getElementById(idanno).value = anno;
  chiudical();
  return false;
}


function collocagifcal(){
    miolink = document.createElement("a");
    miolink.href = "javascript: calendariettoSel(\'dagg\',\'damm\',\'dayy\');";
    miolink.setAttribute("href", "javascript: calendariettoSel(\'dagg\',\'damm\',\'dayy\');");
    miaimg = document.createElement("img");
    miaimg.setAttribute("src", "/immagini/1_76_20071213092152.gif");
    miaimg.setAttribute("style", "width:1.97em;float:left;border:0px;padding:0px;margin:0px;");
    miolink.appendChild(miaimg);
    document.getElementById("colonninada").appendChild(miolink);
    
    miolink2 = document.createElement("a");
    miolink2.href = "javascript: calendariettoSel(\'agg\',\'amm\',\'ayy\');";
    miolink2.setAttribute("href", "javascript: calendariettoSel(\'agg\',\'amm\',\'ayy\');");
    miaimg2 = document.createElement("img");
    miaimg2.setAttribute("src", "/immagini/1_76_20071213092152.gif");
    miaimg2.setAttribute("style", "width:1.97em;float:left;border:0px;padding:0px;margin:0px;");
    miolink2.appendChild(miaimg2);
    document.getElementById("colonninaa").appendChild(miolink2);
}



var dtCh = "/";
var minYear = 2006;
var maxYear = 2012;

function isInteger(s){
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) 
            return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) 
            returnString += c;
    }
    return returnString;
}

function daysInFebruary(year){
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}

function DaysArray(n){
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) {
            this[i] = 30
        }
        if (i == 2) {
            this[i] = 29
        }
    }
    return this
}

function isDate(dtStr){
    var daysInMonth = DaysArray(12)
    var pos1 = dtStr.indexOf(dtCh)
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
    var strDay = dtStr.substring(0, pos1)
    var strMonth = dtStr.substring(pos1 + 1, pos2)
    var strYear = dtStr.substring(pos2 + 1)
    strYr = strYear
    if (strDay.charAt(0) == "0" && strDay.length > 1) 
        strDay = strDay.substring(1)
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) 
        strMonth = strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0) == "0" && strYr.length > 1) 
            strYr = strYr.substring(1)
    }
    month = parseInt(strMonth)
    day = parseInt(strDay)
    year = parseInt(strYr)
    if (pos1 == -1 || pos2 == -1) {
        alert("Formatto della data deve essere : gg/mm/aaaa")
        return false;
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        alert("Il mese non e' valido");
        return false
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        alert("il giorno non e' valido");
        return false;
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
        alert("L'anno deve essere 4 carrattere tra " + minYear + " e " + maxYear);
        return false;
    }
    if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
        alert("Inserisci un data valido");
        return false;
    }
    return true;
}
