﻿//gestione ahah
function ahah(url, target) {
    document.getElementById(target).innerHTML = '<center><div class="loading">Loading</div></center>';
    if (window.XMLHttpRequest) {
        var req = new XMLHttpRequest();
        req.onreadystatechange = function() { ahahDone(target, req); };
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        var req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() { ahahDone(target, req); };
            req.open("GET", url, true);
            req.send();
        }
    }
}

function ahahDone(target, req) {
    if (req.readyState == 4) {
        if (req.status == 200 || req.status == 304) {
            results = req.responseText;
            document.getElementById(target).innerHTML = results;
        } else {
            document.getElementById(target).innerHTML = '<center><div class="error">Error</div></center>';
        }
    }
}

//controllo asincrono email già presente
function isEmailPresent(email) {
    if (window.XMLHttpRequest) {
        var req = new XMLHttpRequest();
        req.onreadystatechange = function() { isEmailPresentDone(req); };
        req.open("GET", "email_present.aspx?email=" + email, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        var req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() { isEmailPresentDone(req); };
            req.open("GET", "email_present.aspx?email=" + email, true);
            req.send();
        }
    }
}

function isOtherEmailPresent(email, cnid) {
    if (window.XMLHttpRequest) {
        var req = new XMLHttpRequest();
        req.onreadystatechange = function() { isEmailPresentDone(req); };
        req.open("GET", "other_email_present.aspx?email=" + email + "&cnid=" + cnid, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        var req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() { isEmailPresentDone(req); };
            req.open("GET", "other_email_present.aspx?email=" + email + "&cnid=" + cnid, true);
            req.send();
        }
    }
}

function isEmailPresentDone(req) {
    if (req.readyState == 4) {
        if (req.status == 200 || req.status == 304) {
            document.forms[0].submit();
        } else {
            document.getElementById("sendthisform").disabled = false;
            alert("Email inserita già presente nel sistema se la mail ti appartiene puoi recuperare la password usando la funzione recupera password, altrimenti modifica la mail.")
        }
    }
}

function wl(string) {
    document.write(string);
}

function npNotPresent() {
    $('#popUpContent').fadeIn('slow');
    document.getElementById('wrapper').style.display='none';
    document.getElementById('msgPopUpContent').innerHTML = "<div style=\"width: 100%; font-size: 14px; font-weight: bold; text-align: center; color: Red;\">Questa edizione non è presente</div>"
}

function checkString(string) {
    var answer = confirm(string);
    if (answer) {
        return true;
    } else {
        return false;
    }
} 

//jQuery plugins
//wait
(function($) {
    $.fn.wait = function(option, options) {
        milli = 1000;
        if (option && (typeof option == 'function' || isNaN(option))) {
            options = option;
        } else if (option) {
            milli = option;
        }
        // set defaults
        var defaults = {
            msec: milli,
            onEnd: options
        },
        settings = $.extend({}, defaults, options);

        if (typeof settings.onEnd == 'function') {
            this.each(function() {
                setTimeout(settings.onEnd, settings.msec);
            });
            return this;
        } else {
            return this.queue('fx',
            function() {
                var self = this;
                setTimeout(function() { $.dequeue(self); }, settings.msec);
            });
        }

    }
})(jQuery);

function addWait(container) {
    $('#' + container).append('<div id="temploader" style="position: absolute; margin-left:-10px; margin-top: -20px"><img src="../img/loading.gif" /></div>');
}

