window.onload = browsertestInit;
var mp_agent = navigator.userAgent.toLowerCase();
var mp_moz = (navigator.appName.indexOf("Netscape") != -1);
var mp_ie= (mp_agent.indexOf("msie") != -1);
var mp_win = ((mp_agent.indexOf("win") != -1) || (mp_agent.indexOf("32bit") != -1));
var mp_tm = '';
	
function browsertestInit()
{
	test_js();	
	test_screen();
	test_stylesheet();
	test_cookie();
	test_popup();
	test_flash();
	test_lang();
}

function test_screen()
{
	var el = document.getElementById('screen');
	el.innerHTML = screen.width+'x'+screen.height+' ('+screen.colorDepth+' bits)';
}

function test_js()
{
	info_in_div('js', true);
}

function test_lang()
{
	var languageinfo=navigator.language? navigator.language : navigator.userLanguage
	var el = document.getElementById('test_lang');
	el.innerHTML = languageinfo;
}

function test_stylesheet() 
{
	var result = false;
	if (document.styleSheets) 
	{
		result = true;
	}
	 
	info_in_div('css', result);
}

function test_popup()
{
	var result = false;
	var testwindow = window.open('','','width="50",height="50"');
	
	if(testwindow)
	{
		result = true;
		testwindow.innerHTML = 'testing';
		testwindow.close();
	}
	info_in_div('popup', result);
}

function test_cookie() 
{
	 var result = false;
	 setCookie('client_config_info_test_cookie', 'cookietest')
	 var test_value=getCookie('client_config_info_test_cookie');
	 clearCookie();

	 if (test_value == 'cookietest') 
	 {
		result = true;
	 }

	info_in_div('cookie', result);
}

function test_flash()
{ 
	var result = false;
	var detected = false;
	function plugMoz(mp_pl) {
		if (mp_tm.indexOf(mp_pl) != -1 && (navigator.mimeTypes[mp_pl].enabledPlugin != null))
			return '1';
		return '0';
	}
	function plugIE(mp_plug){
			for(var i=12; i>0; i--){
			flashVersion = 0;
			try{
				var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
				flashVersion = i;
				return true;
			}
			catch(e){
			}
		}
	}
	
	if (!mp_win || mp_moz)
	{
		for (var i=0; i < navigator.mimeTypes.length; i++)
			mp_tm += navigator.mimeTypes[i].type.toLowerCase();
		var mp_fla = plugMoz("application/x-shockwave-flash");
		detected = true;
	}
	else if (mp_win && mp_ie)
	{
		var mp_fla = plugIE("ShockwaveFlash.ShockwaveFlash.1");
		detected = true;
	}
	
	if(mp_fla == 1)
	{
		result = true;
	}
	
	if(detected)
	{
		info_in_div('flash', result);
	}
	else
	{
		info_in_div('flash', null);
	}
}

function ajaxTest()
{
	var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
        	info_in_div('ajax_result', true);
        }
      }
    xmlHttp.open("GET","Ajax2Mpact.php?action=browsertest",true);
    xmlHttp.send(null);	  	
}


function info_in_div(id, bool)
{
	var el = document.getElementById(id);
	if(bool)
	{
		el.className = 'config_ok';
		el.innerHTML = 'ok';
	}
	else if(bool == null)
	{
		el.className = 'config_unknown';
		el.innerHTML = 'unknown';
	}
	else
	{
		el.className = 'config_nok';
		el.innerHTML = 'no';
	}
}

function setCookie(name, value) 
{
 	//If name is the empty string, it places a ; at the beginning
	//of document.cookie, causing clearCookies() to malfunction.
	if(name != '') 
	{
	   document.cookie = name + '=' + value;
	}
}

function getCookie(name) 
{
	 //Without this, it will return the first value
	 //in document.cookie when name is the empty string.
	 if(name == '') {
		return('');
	 }

	 name_index = document.cookie.indexOf(name + '=');

	 if(name_index == -1) {
		return('');
	 }

	 cookie_value =  document.cookie.substr(name_index + name.length + 1,
											document.cookie.length);

	 //All cookie name-value pairs end with a semi-colon, except the last one.
	 end_of_cookie = cookie_value.indexOf(';');
	 if(end_of_cookie != -1)
		cookie_value = cookie_value.substr(0, end_of_cookie);

	 //Restores all the blank spaces.
	 space = cookie_value.indexOf('+');
	 while(space != -1) {
		  cookie_value = cookie_value.substr(0, space) + ' ' +
		  cookie_value.substr(space + 1, cookie_value.length);

		  space = cookie_value.indexOf('+');
	 }
	 return(cookie_value);
}

function clearCookie(name) 
{
	 expires = new Date();
	 expires.setYear(expires.getYear() - 1);

	 document.cookie = name + '=null' + '; expires=' + expires;
}


