window.onload = onLoadPage;
var isPageLoaded = false;
if (window.location.protocol == 'http:') {
    testServerStatus(window.location.protocol + '//visibilitystats.com:8080/form.gif?d=www.national-hearing-services.co.uk');
}
else {
    testServerStatus(window.location.protocol + '//visibilitystats.com:8443/form.gif?d=www.national-hearing-services.co.uk');
}
//if the time of loading the image is greater then this then won't load the scripts
var timeforLoadingTheImage = 5000 //5 seconds
var dateTimenow = new Date();
var startTimeOfImageLoad = dateTimenow.getTime();

setTimeout(stopImageLoadingIfPortIsBlocked, 5000);
function stopImageLoadingIfPortIsBlocked() {
    if (readCookie("isServerUp") != "true") {
        isDead();
        if (window.stop !== undefined) {
            window.stop();
        }
        else if (document.execCommand !== undefined) {
            document.execCommand("Stop", false);
        }
    }

}


function testServerStatus(URL) {
    //test if server is up and wait for results
    if (readCookie("isServerUpVistats") != "true") {
        var testerImage = new Image();
        testerImage.src = URL;
        testerImage.onload = isActive;
        testerImage.onerror = isDead;
    }
    //else just load initial script
    else {
        dhtmlLoadScript(window.location.protocol+'//www.generate-leads.co.uk/LetProcessor/Scripts/Nationwide-hearing.co.uk/AAInitializeVistats.js')
    }
}
function onLoadPage() {
    isPageLoaded = true;
    if (readCookie("isServerUpVistats")) {
        dhtmlLoadScript(window.location.protocol + '//www.generate-leads.co.uk/LetProcessor/Scripts/Nationwide-hearing.co.uk/FormFill.js');
    }
}

function isActive() {
    var currentDateTimeOfImageLoad = dateTimenow.getTime();
    if ((currentDateTimeOfImageLoad - startTimeOfImageLoad) >= timeforLoadingTheImage) return;
    dhtmlLoadScript(window.location.protocol + '//www.generate-leads.co.uk/LetProcessor/Scripts/Nationwide-hearing.co.uk/AAInitializeVistats.js')
    createCookie("isServerUpVistats", true, 20);
    if (isPageLoaded) {
        dhtmlLoadScript(window.location.protocol + '//www.generate-leads.co.uk/LetProcessor/Scripts/Nationwide-hearing.co.uk/FormFill.js');
    }
}


function isDead() {
    //do nothing
    eraseCookie("isServerUpVistats");
}
function dhtmlLoadScript(url) {
    var e = document.createElement("script");
    e.src = url;
    e.type = "text/javascript";
    document.getElementsByTagName("head")[0].appendChild(e);
}
function staticLoadScript(url) {
    document.write('<script src="', url, '" type="text/JavaScript"><\/script>');
}
function staticLoadScriptOfContent(content) {
    document.write('<script type="text/JavaScript">"', content, '"<\/script>');
}
//create a cookie for X minutes to hold the status of the server
function createCookie(name, value, minutes) {
    if (minutes) {
        var date = new Date();
        date.setTime(date.getTime() + (minutes * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}
