/************************************************/
/*
 * File type : JavascriptFile(counter.js)
 * Created by : vishwajeet chakraborty
 * Date : 09th Mar'09 on 4:00 AM
 */
/************************************************/
//plz change this path
//setTimeout(window.location,7000);
//var host =location.host;
var counter =  host+'analytics-api/services/counter';

//*******************************************/

var atags = document.getElementsByTagName('a');
for (var i=0; i < atags.length; i++)
{
    
    var aa = atags[i].getAttribute('href',2);
    
   
}
cc_add_click_monitor(aa);
/********************************************/
function cc_add_click_monitor(aa)
{
    
    var atags = document.getElementsByTagName('a');
    for (var i=0; i < atags.length; i++)
	{
			$(atags[i]).click(function()
			{
				cc_click(this, this.getAttribute('href',2));
                this.setAttribute("href",'javascript:void(0);');
				return true;
			});
    }
}
/**
	Handle a click by doing an ajax call to the php click handler script
*/

function getXmlHttp()
{
	var xmlhttp = false;
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e)
	{
		try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");	}
		catch (oc) { xmlhttp = null; }
	}
	if (!xmlhttp && typeof XMLHttpRequest != undefined) xmlhttp = new XMLHttpRequest();
	return xmlhttp;
}

function cc_click(tag,link)
{
   var params='';
	tag.style.cursor='wait';  		
	// make url of php script that counts clicks.
	var xmlhttp;
    xmlhttp = getXmlHttp();
	xmlhttp.onreadystatechange = function()
	{
        
		if(xmlhttp.readyState == 4)
		{
            if(xmlhttp.responseText=="Inserted successfully" || xmlhttp.status=="404")
                {
                    if(tag.target == '_blank')
                    {
                     window.location.target=link;
                    }
                    else
                    {
                        window.location.href=link;
                    }
                }
            //alert(http.responseText);
			tag.style.cursor='pointer';
			cc_http_response_callback(xmlhttp.responseText);
			return true;				
		}
	}
	var click = tag.getAttribute("onclick",2);
	var url = link;
    var start_index_temp = url.indexOf('/', 0);
    if(start_index_temp<0)
    {
        start_index_temp=-1;
    }
    url = url.substr(start_index_temp+1, url.length);
    start_index_temp =null;
	var title = tag.childNodes[0].nodeValue;
    var strHref = window.location.href;

	// uncomment to turn off onclick for this <a> tag to prevent repeat clicks
	//tag.setAttribute('onclick',null);
	
   params = 's_url=' + encodeURIComponent(url) + '&title=' + escape(title) + '&click='+ encodeURIComponent(click)+ '&siteurl=' + encodeURIComponent(strHref);
    //alert(counter+'?'+params);
	xmlhttp.open('GET', counter+'?'+params , true);
	
	xmlhttp.send(null);
	return true;
}
/******************************************************************/

function cc_http_response_callback()
{
    
}

