﻿// JavaScript Document
function cookiesAllowed() {
    setCookie('checkCookie', 'test', 1);
    if (getCookie('checkCookie')) {
        deleteCookie('checkCookie');
        return true;
    }
    return false;
}
function setCookie(name, value, expires, options) {
    if (options == undefined) { options = {}; }
    if (expires) {
        var expires_date = new Date();
        expires_date.setDate(expires_date.getDate() + expires)
    }
    document.cookie = name + '=' + escape(value) +
      ((expires) ? ';expires=' + expires_date.toGMTString() : '') +
      ((options.path) ? ';path=' + options.path : '') +
      ((options.domain) ? ';domain=' + options.domain : '') +
      ((options.secure) ? ';secure' : '');
}
function getCookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1) return null;
    var end = document.cookie.indexOf(';', len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}
function deleteCookie(name, path, domain) {
    if (getCookie(name)) document.cookie = name + '=' +
      ((path) ? ';path=' + path : '') +
      ((domain) ? ';domain=' + domain : '') +
      ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function fnWhenDone(XML) {
}

function initStopwatch() {
    var myTime = new Date();
    var timeNow = myTime.getTime();
    var timeDiff = timeNow - clockStart;
    this.diffSecs = timeDiff / 1000;
    return (this.diffSecs);
}
function getSecs() {
    var mySecs = initStopwatch();
    var mySecs1 = "" + mySecs;

    if (timespent == 15) {
        if (cookiesAllowed()) {
            var ajaxConn = new XHConn();
            ajaxConn.connect("/qualscore.aspx", "POST", "PAGENAME=" + pageName + "&PAGETIME=" + timespent + "&QUALSCORE=" + qualScore, fnWhenDone);
            return;
        }
    }

    mySecs1 = mySecs1.substring(0, mySecs1.indexOf("."));
    timespent = mySecs1
    window.setTimeout('getSecs()', 1000);
}
function untimedQual() {
    if (cookiesAllowed()) {
        var ajaxConn = new XHConn();
        ajaxConn.connect("/qualscore.aspx", "POST", "PAGENAME=" + pageName + "&PAGETIME=1&QUALSCORE=" + qualScore, fnWhenDone);
    }
}
function flashQual(pName, ftime) {
    //alert('flashQual called with pName = ' + pName + ' and ftime = ' + ftime);
    if (cookiesAllowed()) {
        var qualScore = getQualScore(pName);
        var ajaxConn = new XHConn();
        ajaxConn.connect("/qualscore.aspx", "POST", "PAGENAME=" + pName + "&PAGETIME=" + ftime + "&QUALSCORE=" + qualScore, fnWhenDone);
    }

}

function XHConn() {
    var xmlhttp, bComplete = false;
    try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {
        try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (e) {
            try { xmlhttp = new XMLHttpRequest(); }
            catch (e) { xmlhttp = false; } 
        } 
    }
    if (!xmlhttp) return null;
    this.connect = function(sURL, sMethod, sVars, fnDone) {
        if (!xmlhttp) return false;
        bComplete = false;
        sMethod = sMethod.toUpperCase();
        try {
            if (sMethod == "GET") {
                xmlhttp.open(sMethod, sURL + "?" + sVars, true);
                sVars = "";
            }
            else {
                xmlhttp.open(sMethod, sURL, true);
                xmlhttp.setRequestHeader("Method", "POST " + sURL + " HTTP/1.1");
                xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && !bComplete) {
                    bComplete = true;
                    fnDone(xmlhttp);
                } 
            };
            xmlhttp.send(sVars);
        }
        catch (z) { return false; }
        return true;
    };
    return this;
}

function getQualScore(pageName) {

    if (pageName == "doctor discussion guide") return '3';
    if (pageName == "fto") return '3';
    if (pageName == "experience card") return '3';
    if (pageName == "xpressionsPrint") return '3'; 
    
    if (pageName == "nasal allergy toolkit") return '2';
    if (pageName == "my xperts") return '2';
    if (pageName == "stay informed") return '2';

    if (pageName == "xpressions") return '2'; 
	if (pageName == "xpressionsPrint") return '3'; 
    
    if (pageName == "how xyzal works") return '1';


    if (pageName == "Blaiss: Allergist") return '1';
    if (pageName == "Jill: Indoor Allergy Sufferer") return '1';
    if (pageName == "Elliott: Outdoor Allergy Xpert") return '1';
    if (pageName == "Michelle and Lauren: Patients Age 6 and Up") return '1';
    if (pageName == "May: Indoor Allergy Xpert") return '1';
    if (pageName == "Lisa: Outdoor Allergy Sufferer") return '1';

}