
if (typeof(http_referrer) == 'undefined')
{
	var http_referrer = '';
}
if (typeof(url) == 'undefined')
{
	var url = "";
}
if (typeof(app_type) == 'undefined')
{
	var app_type = '';
}
if (typeof(app_name) == 'undefined')
{
	var app_name = '';
}

if(typeof(productId) == 'undefined')
{
	var productId = 0;
}
if(typeof(product_type) == 'undefined')
{
	var product_type = 0;
}
if(typeof(extra_indexId) == 'undefined')
{
	var extra_indexId = 0;
}
if(typeof(extra_number) == 'undefined')
{
	var extra_number = "0";
}

if(typeof(extra_description) == 'undefined')
{
	var extra_description = "0";
}

if(typeof(javascript_referrer) == 'undefined')
{
	var javascript_referrer = "";
}

var entranceId = 0;

var UrlEncoding = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

// Generic call fuction.
function GetXmlHttpObject() {
    var xmlHttp=null;
    try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
    } catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
    }
    return xmlHttp;
}

/* CREATE ENTRANCE */
var entry_params = "http_referrer="+http_referrer+"&landing_url="+url+"&app_type="+app_type+"&app_name="+app_name+"&javascript_referrer="+UrlEncoding.encode(javascript_referrer);

xmlHttp=GetXmlHttpObject();
xmlHttp.onreadystatechange= function(){
        if(xmlHttp.readyState == 4){
        	entranceId = xmlHttp.responseText;
            //trackAction('read');
            }};
xmlHttp.open("POST", "/tracking/track_entrance.php", true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(entry_params);

//xmlHttp.setRequestHeader("Content-length", entry_params.length);
//xmlHttp.setRequestHeader("Connection", "close");

 
function trackAction(action, a_productId, a_product_type, a_extra_indexId, a_extra_description, a_extra_number)
{
    if (typeof(a_productId) == 'undefined' || !a_productId)
    {
        var a_productId = productId;
    }     
    if (typeof(a_product_type) == 'undefined' || !a_product_type)
    {
        var a_product_type = product_type;
    }   
    if (typeof(a_extra_indexId) == 'undefined' || !a_extra_indexId)
    {
        var a_extra_indexId = extra_indexId;
    }       
    if (typeof(a_extra_description) == 'undefined' || !a_extra_description)
    {
        var a_extra_description = extra_description;
    }
    if (typeof(a_extra_number) == 'undefined' || !a_extra_number)
    {
        var a_extra_number = extra_number;
    }
    var params = "url="+url+"&action="+action+"&productId="+a_productId+"&product_type="+a_product_type+"&extra_indexId="+a_extra_indexId+"&extra_description="+a_extra_description+"&extra_number="+a_extra_number;    
    
    xmlHttp=GetXmlHttpObject();
    xmlHttp.open("POST", "/tracking/track_action.php", true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    //xmlHttp.setRequestHeader("Content-length", params.length);
    //xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(params);
}

// callback for facebook function FB.Connect.streamPublish
function trackFBShare(post_id, exception) {
	if(post_id)
	{
		trackAction('share_facebook');
	}
}

