var testString = "i";
var detectedResponse = "yes";
var undetectedResponse = "no";

// You may change the above variables. But do not use true and false for the responses.

function detectTestFonts() {
    if(document.createElement) {
        testFontList = new Array();
        var hasFonts = true;
        try {hasFonts = getTestFontList();}
        catch(e) {hasFonts = false;}
        if(hasFonts) {
            placeTestFontSpans();
            if(testFontResult[0] == undetectedResponse) changeSmallFonts();  // Optional.
        }
    }
}

function placeTestFontSpans() {
    if((testFontList.length) > 0) {
        var undefined;
        var tf = testFontList.length;
        testfontdiv = document.getElementById("theTestFontDiv");
        if(testfontdiv) {
            testFontSpan = new Array();
            testFontChar = new Array();
            testMonoSpan = document.createElement("span");
            testMonoSpan.style.fontFamily = "monospace";
            testMonoChar = document.createTextNode(testString);
            testMonoSpan.appendChild(testMonoChar);
            testfontdiv.appendChild(testMonoSpan);
            testSerifSpan = document.createElement("span");
            testSerifSpan.style.fontFamily = "serif";
            testSerifChar = document.createTextNode(testString);
            testSerifSpan.appendChild(testSerifChar);
            testfontdiv.appendChild(testSerifSpan);
            for(var n=0; tf>n; n++) {
                if(testFontList[n]==undefined) testFontList[n] = "";
                testFontSpan[n] = document.createElement("span");
                testFontSpan[n].style.fontFamily = testFontList[n] + ",monospace";
                testFontChar[n] = document.createTextNode(testString);
                testFontSpan[n].appendChild(testFontChar[n]);
                testFontSpan[tf+n] = document.createElement("span");
                testFontSpan[tf+n].style.fontFamily = testFontList[n] + ",serif";
                testFontChar[tf+n] = document.createTextNode(testString);
                testFontSpan[tf+n].appendChild(testFontChar[tf+n]);
            }
            testFontSpan[2*tf] = document.createElement("span");
            testFontSpan[2*tf].fontFamily = "monospace";
            testFontChar[2*tf] = document.createTextNode(testString);
            testFontSpan[2*tf].appendChild(testFontChar[2*tf]);
            for(var n=0; (2*tf)>=n; n++) {
                testfontdiv.appendChild(testFontSpan[n]);
            }
            testfontdiv.style.display = "block";
            var referenceMonoResult = getXposition(testSerifSpan) - getXposition(testMonoSpan);
            var referenceSerifResult = getXposition(testFontSpan[0]) - getXposition(testSerifSpan);
            testFontResult = new Array();
            for(var n=0; tf>n; n++) {
                var monoFontResult = getXposition(testFontSpan[n+1]) - getXposition(testFontSpan[n]);
                var serifFontResult = getXposition(testFontSpan[tf+n+1]) - getXposition(testFontSpan[tf+n]);
                var totalResult = (monoFontResult != referenceMonoResult) || (serifFontResult != referenceSerifResult);
                if(totalResult == true) testFontResult[n] = detectedResponse;
                else testFontResult[n] = undetectedResponse;
            }
            testfontdiv.style.display = "none"
        }
    }
}

// functions getXposition() and getYposition() thanks to www.quirksmode.org

function getYposition(whichelement) {
    var ypos = 0;
    if (whichelement.offsetParent) {
        while (whichelement.offsetParent) {
            ypos += whichelement.offsetTop
            whichelement = whichelement.offsetParent;
        }
    }
    return ypos;
}

function getXposition(whichelement) {
    var xpos = 0;
    if (whichelement.offsetParent) {
        while (whichelement.offsetParent) {
            xpos += whichelement.offsetLeft
            whichelement = whichelement.offsetParent;
        }
    }
    return xpos;
}


// After test, the names of the tested fonts are in array testFontList[n]
// The results of the tests are in array testFontResult[n]
// If you did not provide a testFontList[index] entry for a particular index,
// then testFontList[index] will be the empty string "".














function getTestFontList() {
        testFontList[0] = "Arial Sakha Unicode";
        testFontList[1] = "Arial Unicode MS";
    var hasMoreFonts = true;
    try {hasMoreFonts = getMoreTestFontList();}
    catch(e) {hasMoreFonts = false;}
    if(hasMoreFonts) hasMoreFonts = getMoreTestFontList();
    return true;
}


// You do not need to use consecutive index numbers for the test font list.
// If you skip any numbers, the detection understands the omission.

// Each entry in testFontList includes one or more font names.
// If a font name has more than one word, then enclose the name in single quotes.
// Within a single entry, separate font names by a comma. Space is optional.
// When several fonts are in the same entry, detection is positive if any one or more are detected.

// Font names must be exact. 'Comic Sans MS' is a font name, but "Comic Sans' is not a font name.
// 'Palatino Linotype' and Palatino are two different (but similar) fonts.
// Names are usually case-sensitive.

// Do not use generic names such as serif, sans-serif, or monospace.

// Do not attempt to test for common dingbat or symbolic fonts,
// such as Webdings, Wingdings, Symbol, or Zapf Dingbats.
// Some browsers will report false positives, others will report false negatives, for such fonts.
// Many rare "amateur" dingbat fonts can be detected (such as my own MusiQwik),
// but it is unlikely that many visitors will have those fonts.

// Some systems have tables of equivalent fonts. For example, it appears that
// Windows regards Helvetica and Arial as equivalent. The test for Helvetica may
// be positive, even if the font is missing, because the system will substitute Arial.
// Such false positives are usually not an item of concern.

// My script allows you to put the font list in more than one block.
// So, you could test some fonts in common script for all your pages,
// plus additional fonts for a specific page.
// The master list is kept within the function getTestFontList(),
// and the individual entries are kept as testFontList[index].
// If you create a function getMoreTestFontList(),
// Then you can include addition testFontList[index] entries within it.
// After the additional fonts, the final line in getMoreTestFontList() must be:
// return true;

















// These functions are specific to my zipped demo page, not my online page.

// What you do with the results is up to you.
// Typically, I detect the fonts immediately after the body tag is written, but before page content.
// If you intend to use the result to change style sheets, then you can do it immediately.
// But if you intend to display the results on the page, then you have to wait until later.
// In that case, use either window.onload, or call the result function using inline script.
// For this demo page, I call useTestFontResult() by inline script, after the containing div is written.

// Results are kept in testFontResult[index] array. I use "yes" or "no" as possible results.

// Since the displayTestFontResult() function places text into the document body,
// This function is invoked after a certain div element (which will contain the text)
// has been written.


function displayTestFontResult() {
    if(document.getElementById("theFontResultDiv")) {
        fontresultdiv = document.getElementById("theFontResultDiv");
        var hasResults = true;
        try {if(testFontResult[0]=="") void(0);}
        catch(e) {hasResults = false;}
        if(hasResults) {
            var thisfont;
            var test;
            var tf = testFontList.length;
            fontResultExhibit = new Array();
            fontResultExhibitText = new Array();
            for(var n=0; tf>n; n++) {
                thisfont = testFontList[n];
                if(thisfont != "") {                	if (testFontResult[n]!="yes"){                	   test = "yes";
                    }
                }
            }
            if (test=="yes"){               thisfont = thisfont + " = " + testFontResult[n];

               fontResultExhibitText[n] = document.createTextNode('Установка шрифта');
               fontResultExhibit[n] = document.createElement("h1");
               fontResultExhibit[n].appendChild(fontResultExhibitText[n]);
               fontresultdiv.appendChild(fontResultExhibit[n]);

               fontResultExhibitText[n] = document.createTextNode('Возможно, в  Вашей операционной системе не установлен шрифт "Arial Sakha Unicode" для правильного отображения сайта. Для того чтобы его установить скачайте с сайта файл со шрифтом, закройте браузер и установите скачанный шрифт. Затем открывайте наш сайт заново. Если шрифт будет установлен, то Вы не должны видеть данный текст. Если все же увидите, то ни чего страшного, просто Ваш браузер не корректно обрабатывает проверку установленного шрифта.');
               fontResultExhibit[n] = document.createElement("div");
               fontResultExhibit[n].style.font = "14px Arial";
               fontResultExhibit[n].style.padding = "18px";
               fontResultExhibit[n].style.float = "left";
               fontResultExhibit[n].appendChild(fontResultExhibitText[n]);
               fontresultdiv.appendChild(fontResultExhibit[n]);

               fontResultExhibitText[n] = document.createTextNode('Скачать шрифт "Arial Sakha Unicode"');
               fontResultExhibit[n] = document.createElement("a");
               fontResultExhibit[n].href = "http://www.kyym.ru/files/arialsa.ttf";
               fontResultExhibit[n].style.font = "14px Arial";
               fontResultExhibit[n].style.padding = "18px";
               fontResultExhibit[n].appendChild(fontResultExhibitText[n]);
               fontresultdiv.appendChild(fontResultExhibit[n]);
            }
        }
        else {
            noFontTestResult = document.createTextNode("List of fonts was not specified.");
            noFontTestResultDiv = document.createElement("div");
            noFontTestResultDiv.style.marginLeft = "20px";
            noFontTestResultDiv.style.marginBottom = "20px";
            noFontTestResultDiv.appendChild(noFontTestResult);
            fontresultdiv.appendChild(noFontTestResultDiv);
        }
    }
}

function changeSmallFonts() {
  var undefined;
  var sheetNumber = 0;  // see explanation below
  var ruleNumber = 1;  // see explanation below
  if((document.styleSheets) && (document.styleSheets.length) > sheetNumber) {
    var thisSheet = document.styleSheets[sheetNumber];
    if((thisSheet.cssRules) && ((thisSheet.cssRules[ruleNumber]) != undefined)) {
      thisSheet.cssRules[ruleNumber].style.fontSize = "14px";  //  test appropriate for most non-MS browsers
    }
    else {
        if((thisSheet.rules) && ((thisSheet.rules[ruleNumber]) != undefined)) {
          thisSheet.rules[ruleNumber].style.fontSize = "14px";  // test appropriate for most MS browsers
        }
    }
  }
}

// Explanation of sheetNumber and ruleNumber (these are my own variable names):
// In this context, a "style sheet" includes both external *.css files,
// and internal script elements in the document head. (I'm not sure how @import rules are counted.)
// The browser keep its style sheets in an array called document.styleSheets.
// Each style sheet is indexed, with 0 being the first encountered from the top of the document head.
// In my font-detector.htm file, document.styleSheets[0] is the internal style element.
// In this "style sheet," the "cssRules" (most browsers other than Microsoft)
// or the "rules" (most Microsoft browsers) are the various element styles.
// It is necessary to branch the logic so that either "cssRules" or "rules" can be used without error.
// For each style sheet, the rules are kept in an array, indexed from 0 for the topmost rule.
// In my style sheet, rule 1 has the style for p.changeme class.
// Fonts are detected before any content text is displayed in the document body.
// If Verdana is not present, then the font-size style for p.changeme is rasied from 12px to 14px.
// Note that CSS font-size is JavaScript fontSize.

// If you wish to be fancy, you could write script that determines which sheet has which rules,
// so that you don't need to know the index numbers in advance.
// That might be necessary if you have several un-coordinated pages on your web site.
// In my case, all pages use the same organization, so I know in advance where things are.

// If you wish to know where I got this kind of scripting information, surf the Internet for
// DOM CSS.



gotScript = true;  // This is for my own use.
