/* javascript */
faq_open = 0;
tooltip_open = 0;
cid = '';
prod = 0;
cat = 0;

/**
 * function getVal - take values of $_GET parameters
 * @param p - name of paramater
 */
String.prototype.getVal = function(p)
{
    return (match = this.match(new RegExp("[?|&]?" + p + "=([^&]*)"))) ? match[1] : false;
}

// window.location = ?test1=var1&test2 

cid = window.location.search.getVal('cid'); // get $cid
prod = window.location.search.getVal('prod'); // get $prod id
cat = window.location.search.getVal('cat'); // "get $cat id

if(prod == '')
{
	prod = 0;
}

if(cat == '')
{
	cat = 0;
}

//alert("cid=" + cid + "&prod_id=" + prod + "&cat_id=" + cat);


var http_request = false;
	
function makePOSTRequest(url, parameters) 
{
	http_request = false;
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
		{
			// set type accordingly to anticipated content type
			// http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} 
	else if (window.ActiveXObject) 
	{ // IE
		try 
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
           	try 
           	{
           		http_request = new ActiveXObject("Microsoft.XMLHTTP");
           	} 
           	
           	catch (e) {}
       	}
	}
	
	if (!http_request) 
	{
		alert('Your browser do not support AJAX');
		return false;
	}

	http_request.onreadystatechange = alertContents();
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function alertContents() 
{
	if (http_request.readyState == 4) 
	{

		if (http_request.status == 200) 
		{
//			alert(http_request.responseText);
			result = http_request.responseText;
		}
		else 
		{
			alert('There was a problem with the request.');
		}
	}
}

function get_stat(type, val) 
{
	//alert(val);
	
	getScr();
	
	// 0 - none, 1 - faqs, 2 - updates, 3 - manuals, 4 - tooltip 
	
	switch(type)
	{
		case 0: // none
			var post_stats = "faq_id=0&stat_update_id=0" + 
				"&stat_manual_id=0&stat_screen_res=" + width + "x" + height 
				+ "&vote_faq=0&comment_faq=0&tooltip=0&cid=" + cid + "&prod="
				+ prod + "&cat=" + cat + "&js=1";
			break;
		
		case 1: // faqs
			if(faq_open == 1) // only when he expand content of faq
			{
				var post_stats = "faq_id=" + val + "&stat_update_id=0"   
					+ "&stat_manual_id=0&stat_screen_res=" + width + "x" 
					+ height + "&vote_faq=0&comment_faq=0&tooltip=0"
					+ "&cid=" + cid + "&prod=" + prod + "&cat=" + cat + "&js=1";
			}
			else
			{
				var post_stats = "faq_id=0&stat_update_id=0" + 
					"&stat_manual_id=0&stat_screen_res=" + width + "x" + height 
					+ "&vote_faq=0&comment_faq=0&tooltip=0&cid=" + cid 
					+ "&prod=" + prod + "&cat=" + cat + "&js=1";
			}
			break;
		
		case 2: // updates
			var post_stats = "faq_id=0&stat_update_id=" +  val + 
				"&stat_manual_id=0&stat_screen_res=" + width + "x" + height 
				+ "&vote_faq=0&comment_faq=0&tooltip=0&js=1";
			break;
		
		case 3: // manuals
			var post_stats = "faq_id=0&stat_update_id=0&stat_manual_id=" 
				+ val + "&stat_screen_res=" + width + "x" + height 
				+ "&vote_faq=0&comment_faq=0&tooltip=0&cid=" + cid 
				+ "&prod=" + prod + "&cat=" + cat + "&js=1";
			break;
		
		case 4: // tooltips
			var post_stats = "faq_id=0&stat_update_id=0&stat_manual_id=" 
				+ val + "&stat_screen_res=" + width + "x" + height 
				+ "&vote_faq=0&comment_faq=0&tooltip=1&cid=" + cid 
				+ "&prod=" + prod + "&cat=" + cat + "&js=1";
			break;
			
		case 5: // have vote an faq
			var post_stats = "faq_id=0&stat_update_id=0&stat_manual_id=" 
				+ val + "&stat_screen_res=" + width + "x" + height 
				+ "&vote_faq=1&comment_faq=0&tooltip=1&cid=" + cid 
				+ "&prod=" + prod + "&cat=" + cat + "&js=1";
			break;
			
		case 6: // have put an comment
			var post_stats = "faq_id=0&stat_update_id=0&stat_manual_id=" 
				+ val + "&stat_screen_res=" + width + "x" + height 
				+ "&vote_faq=0&comment_faq=1&tooltip=1&cid=" + cid 
				+ "&prod=" + prod + "&cat=" + cat + "&js=1";
			break;			
		
		default:
			var post_stats = "faq_id=0&stat_update_id=0" + 
				"&stat_manual_id=0&stat_screen_res=" + width + "x" + height 
				+ "&vote_faq=0&comment_faq=0&tooltip=0&cid=" + cid 
				+ "&prod=" + prod + "&cat=" + cat + "&js=1";
	}
	
//	alert(post_stats);
	makePOSTRequest('_ajax_stat.php', post_stats);
}

function getScr()
{
	if (self.screen) 
	{     // for NN4 and IE4
		width = screen.width
		height = screen.height
		// Testing this first prevents firing the slow Java of NN4
	}
	else if (self.java) 
	{   // for NN3 with enabled Java
		var jkit = java.awt.Toolkit.getDefaultToolkit();
		var scrsize = jkit.getScreenSize();       
		
		width = scrsize.width; 
		height = scrsize.height; 
	}
	else
	{
		width = height = '?' // N2, E3, N3 w/Java off, probably Opera and WebTV
	}
	
	//alert(width +"x"+ height);
}

//window.onload(getScr());