// custom functions for CD

var Paramstring = "";
var lParams = new Array();
var idx = window.location.href.indexOf('?');
var not_fromsysreq = true;

var logkey = Math.round( 10000000 * Math.random() );

if (idx != -1) 
{
     Paramstring = window.location.href.substring(idx+1, window.location.href.length);
     lParams   = getParams();
}
if (typeof(lParams["viewfrom"]) != "undefined" && lParams["viewfrom"] == "sysreq") 
  not_fromsysreq = false;


//preload images

var images_array = new Array();
images_array["adobe"] = new Image();
images_array["adobe"].src="images/adobe_reader.gif";
images_array["apple"] = new Image();
images_array["apple"].src="images/apple_logo.jpg";
images_array["flash"] = new Image();
images_array["flash"].src="images/flashplayer.gif";
images_array["ie"] = new Image();
images_array["ie"].src="images/ie.gif";
images_array["java"] = new Image();
images_array["java"].src="images/java.gif";
images_array["netscape"] = new Image();
images_array["netscape"].src="images/netscape.gif";
images_array["safari"] = new Image();
images_array["safari"].src="images/safari_icon.jpg";
images_array["shockwave"] = new Image();
images_array["shockwave"].src="images/shockwave.gif";
images_array["bad"] = new Image();
images_array["bad"].src="images/status_bad.gif";
images_array["good"] = new Image();
images_array["good"].src="images/status_good.gif";
images_array["windows"] = new Image();
images_array["windows"].src="images/windows_logo.gif";
images_array["mediaplayer"] = new Image();
images_array["mediaplayer"].src="images/windowsmedia.jpg";


function getParams(){

    var params = new Array();
    var pairs = window.location.href.substring(idx+1,window.location.href.length).split('&');
    for (var i=0; i<pairs.length; i++)
    {
      var nameVal = pairs[i].split('=');
      params[nameVal[0]] = nameVal[1];
    }
    return params;
}

function load_splash(check_WinXPSP2){
   if(check_WinXPSP2){
      if(bt.platformVersion == "XP" && bt.browserName == "Internet Explorer" && navigator.appMinorVersion.indexOf("SP2") > -1)
        window.location.href = "sp2warning.htm"+(Paramstring ? "?"+Paramstring : "");
      else
        window.location.href = "splash.html"+(Paramstring ? "?"+Paramstring : "");
   }else{
      window.location.href = "splash.html"+(Paramstring ? "?"+Paramstring : "");
   }
}

var Cookie_Name = "NETTUTOR_BT_TESTCOOKIE";
function set_bt_cookie(name){
  var today = new Date();
  var expires_date = new Date(today.getTime() + (365 * 86400000));
  Set_Cookie(name,name,expires_date);
}

function Get_Cookie(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 Set_Cookie(name, value, expires) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "" ) ;
}

function getDiv(id, doc){
        var mydiv;
        if( doc == null) doc= this.document;
                                                                                                                                 
        if(document.all && !document.getElementById){
          mydiv = doc.all[id];
        }else if(document.getElementById){
          mydiv = doc.getElementById(id);
        }
        return mydiv;
}
                                                                                                                                 
function show_debug_text(s) {
     s=s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
     if (s.toLowerCase().indexOf("table")<0) s=s.replace(/\n/g,"<br>");
     var win=window.open("",'result',
         'scrollbars=yes,resizable=yes,width=630,height=350');
     win.document.writeln("<HTML><BODY><PRE>"+s+"</PRE></BODY></HTML>");
     win.document.close();
     if( win.focus ) win.focus();
}
function corner( h, r, c ) {
  if(typeof(c) == "undefined" ) c = "#FFFFFF";
  var j;
  if (h == 'top') {
    for (var i = r; i >= 1; i--) {
      j = Math.round( Math.sqrt( (r * r) - (i*i) ) );
      draw_line( j, c );
//alert(j);
    }
  } else if ( h == 'bottom') {
    for (var i = 1; i <= r; i++) {
      j = Math.round( Math.sqrt( (r * r) - (i*i) ) );
      draw_line( j, c );
//alert(j);
    }
  }
}

function draw_line( j, c ) {

  // var j = r - Math.sqrt( (r * r) - (i*i) );
  //var j = Math.sqrt( (r * r) - (i*i) );
  if( j == 0 ) j = 4;
  document.write( '<div style="height: 1px; width: ' +
    j + 'px; background: ' + c + ';line-height: 1px; font-size: 1px;">&nbsp;<\/div>' );

}

function hcircle( p, s, c){
  if(typeof(c) == "undefined" ) c = "#FFFFFF";
  var r = 2 * s;
  var str = "";
  if(p=="top"){
    for (var i = r-1; i > s; i--) { 
      j = 2 * Math.sqrt( (r * r) - (i*i) );
      if(i==r) j = 2*j;
      str += get_line( j, c );
    }
  }else if(p=="bottom"){
    for (var i = s+1; i < r; i++) { 
      j = 2 * Math.sqrt( (r * r) - (i*i) );
      //if(i==r) j = 2*j;
      if(j==1) j=4;
      str += get_line( j, c );
    }
  }
  return str;
}

function vcircle( s, c){
  if(typeof(c) == "undefined" ) c = "#FFFFFF";
  var r = 2 * s;
  var str = "";
  for (var i = s; i >=1; i--) {
      j = Math.sqrt( (r * r) - (i*i) ) - s;
      str += get_line( j, c );
  }
  for (var i = 1; i <=s; i++) {
      j = Math.sqrt( (r * r) - (i*i) ) - s; 
      str += get_line( j, c );
  }
  return str;
}

function get_line( j, c ) {
  
  j = Math.round( j );                                                                                                                                         
  return '<div style="height: 1px; width: ' +
    j + 'px; background: ' + c + ';line-height: 1px; font-size: 1px;">&nbsp;<\/div>';
                                                                                                                                           
}


function get_circle_html(r, n, c, fc){
   
   var s = 2 * r;
   var str = 
'<table border="0" cellpadding="0" cellspacing="0" align=center>\n'+
'  <tr>\n'+
'    <td colspan="3" align="center" valign="bottom">\n'+
      hcircle( "top", r, c ) +
'    </td>\n'+
'  </tr>\n'+
'  <tr>\n'+
'    <td align="right" width=25%>\n'+
      vcircle( r, c ) +
'    </td>\n'+
'    <td align="center" valign="middle" style="background-color:' + c + '; font-size:11px; color:'+ fc + '"'+
'     width="' + s + '" height="' + s + '">' + n + '</td>\n'+
'    <td align="left" width=25%>\n'+
      vcircle( r, c ) +
'    </td>\n'+
'  </tr>\n'+
'  <tr>\n'+
'    <td colspan="3" align="center" valign="top">\n'+
      hcircle( "bottom", r, c ) +
'    </td>\n'+
'  </tr>\n'+
'</table>';

  return str;
}

  function report_to_log(name, value){
      var url = "/nt/perform_log.cgi?logkey=" + logkey;

      var parm = "";

      if( typeof(lParams["source"]) != "undefined" ) parm += "&testfrom=" + lParams["source"];

      if( typeof(lParams["uid"]) != "undefined" ) parm += "&uid=" + lParams["uid"];

      if(typeof(value) == "string" && value.search(/\s/) > -1) value = value.replace(/\s/g, "+");   
      parm += "&itemname="+name+"&itemvalue="+value;
      call_to_server(url + parm);      
  }

  var IFrameObj;
  function call_to_server(URL){
 //alert("call to server " + URL);
                                                                                                                                                     
        if (!document.createElement) {return true};
        var IFrameDoc;
                                                                                                                                                     
        if (!IFrameObj && document.createElement) {
                // create the IFrame and assign a reference to the
                // object to our global variable IFrameObj.
                // this will only happen the first time
                // callToServer() is called
                try {
                        var tempIFrame=document.createElement('iframe');
                        tempIFrame.setAttribute('id','RSIFrame');
                        tempIFrame.style.border='0px';
                        tempIFrame.style.width='0px';
                        tempIFrame.style.height='0px';
                        IFrameObj = document.body.appendChild(tempIFrame);
                                                                                                                                                     
                        if (document.frames) {
                                // this is for IE5 Mac, because it will only
                                // allow access to the document object
                                // of the IFrame if we access it through
                                // the document.frames array
                                IFrameObj = document.frames['RSIFrame'];
                        }
                } catch(exception) {
                        // This is for IE5 PC, which does not allow dynamic creation
                        // and manipulation of an iframe object. Instead, we'll fake
                        // it up by creating our own objects.
                        iframeHTML='<iframe id="RSIFrame" style="';
                        iframeHTML+='border:0px;';
                        iframeHTML+='width:0px;';
                        iframeHTML+='height:0px;';
                        iframeHTML+='"><\/iframe>';
                        document.body.innerHTML+=iframeHTML;
                        IFrameObj = new Object();
                        IFrameObj.document = new Object();
                        IFrameObj.document.location = new Object();
                        IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
                        IFrameObj.document.location.replace = function(location) {
                                this.iframe.src = location;
                        }
                }
        }
                                                                                                                                                     
        if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
                // we have to give NS6 a fraction of a second
                // to recognize the new IFrame
                setTimeout('call_to_server("'+URL+'")',10);
                return false;
        }
                                                                                                                                                     
        if (IFrameObj.contentDocument) {
                // For NS6
                IFrameDoc = IFrameObj.contentDocument;
        } else if (IFrameObj.contentWindow) {
                // For IE5.5 and IE6
                IFrameDoc = IFrameObj.contentWindow.document;
        } else if (IFrameObj.document) {
                // For IE5
                IFrameDoc = IFrameObj.document;
        } else {
                return true;
        }
                                                                                                                                                     
        IFrameDoc.location.replace(URL);
        return false;
 }

