function ct2_setCookie(name, value, expires, path, domain, secure){
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime(today.getTime());
	
	//1 minute
	if (expires)expires = expires * 1000 * 60 ;

	var expires_date = new Date(today.getTime() + (expires));
	
	document.cookie = name + "=" +escape( value ) +	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +	( ( path ) ? ";path=" + path : "" ) +	( ( domain ) ? ";domain=" + domain : "" ) +	( ( secure ) ? ";secure" : "" );
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function ct2_getCookie(check_name){
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function ct2_deleteCookie(name) {
	document.cookie = name +'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
} 


function ct2_S4() {
	// add a seed to randome number generator, RDF
	now = new Date();
	seed = now.getSeconds();
	// replace 0×10000 with this format: *parseInt(‘10000′,16); RDF
	// replace substring(1) with a 4 digit hex string like this: substring(1,5); RDF
	return ((1+Math.random(seed))*parseInt("10000",16)).toString(16).substring(1,5);
}

function ct2_generateGuid(){
	return (ct2_S4()+ct2_S4()+"-"+ct2_S4()+"-"+ct2_S4()+"-"+ct2_S4()+"-"+ct2_S4()+ct2_S4()+ct2_S4()).toLowerCase();
}

function ct2_insertVisitorData(params, url_to_ct2_lib_dir){
	var doc=document;
	
	var host = (("https:" == document.location.protocol) ? "https://" : "http://");
	var destination = host + "tracking.rankinspector.com/ConversionTacker2DataHandling/CT_Data_Handling.asmx/insert_data?urlstring="+escape(params);
	
	try{
		//#IFRAME SOLUTION				
		var el = document.createElement("iframe");
		el.setAttribute('id', 'ifrm');
		document.body.appendChild(el);
		el.setAttribute('src', destination);
		el.width = 0;
		el.height = 0;
		el.style.visibility = "hidden";
		//#
	}catch(err){
		var image = new Image();
		image.src=destination;	
	}		
	
	//#AJAX
	//ct2_getResponse(params);
	//#
}

function ct2_putVisitorData(aId, dId, cookieExpireMinutes, url_to_ct2_lib_dir){
	var account_id = aId;
	var domain_id  = dId;
		
	var location = (parent == self) ? window.location   : top.document.location;			
	var referrer = (parent == self) ? document.referrer : top.document.referrer;
	var ref_tmp  = referrer;		
						
	var xy = navigator.appVersion;
	xz = xy.substring(0,4);
				
	var user_agent   = navigator.userAgent;	
	var platform     = navigator.platform;
	var resolution   = screen.width+"x"+screen.height;	
									
	var cookieEnabled= navigator.cookieEnabled;	
	var cookieValue  = ct2_getCookie(domain_id+"ct2_ri_id");
	var uId 		 = "disabled";
	
	if(cookieValue){
		uId = ct2_getCookie(domain_id+"ct2_ri_id");
	}else{
		if(cookieEnabled){
			uId = ct2_generateGuid();
			ct2_setCookie(domain_id+"ct2_ri_id", uId, cookieExpireMinutes, "/", null);
			cookieValue = uId;
		}else cookieValue = "disabled";
	}
	
	ct2_insertVisitorData("account_id="+account_id+"&domain_id="+domain_id+"&"+domain_id+"ct2_ri_id="+cookieValue+"&user_agent="+user_agent+"&resolution="+resolution+"&referrer="+escape(ref_tmp)+"&location="+escape(location),url_to_ct2_lib_dir);
	
}

function ct2_putConversion(aId, dId, conversionTypeId, conversionLabel, conversionValue, customerOrderId, currency, cookieExpireMinutes, url_to_ct2_lib_dir){
	var conversion_type_id = conversionTypeId;
	var conversion_value   = conversionValue;//trial
	var conversion_label   = conversionLabel;//trial
	var customer_order_id  = (customerOrderId)?customerOrderId:"";//optional
	var account_id 		   = aId;
	var domain_id  		   = dId;
	var cur		  		   = (currency)?currency:"EUR";
					
	var xy = navigator.appVersion;
	xz = xy.substring(0,4);
				
	var user_agent  = navigator.userAgent;
	var platform    = navigator.platform;
	var resolution  = screen.width+"x"+screen.height;
			
	var cookieEnabled= navigator.cookieEnabled;	
	var cookieValue  = ct2_getCookie(domain_id+"ct2_ri_id");
	var cookieValue2  = ct2_getCookie(domain_id+"ct2_ri_cm");
	var uId 		 = "disabled";
	
	if(cookieValue){
		uId = ct2_getCookie(domain_id+"ct2_ri_id");
	}else{
		if(!cookieEnabled)cookieValue = "disabled";
	}
	
	//If user has disabled cookies, we should re-check the transaction at ct_db_handling.php (if transaction date/time is less than the max time the transaction should be aborted)
	if(cookieValue2){
		//abort (do nothing)
	}else{
		if(!cookieEnabled)cookieValue2 = "disabled";
		ct2_insertVisitorData("account_id="+account_id+"&domain_id="+domain_id+"&"+domain_id+"ct2_ri_id="+cookieValue+"&"+domain_id+"ct2_ri_cm="+cookieValue2+"&conversion_type_id="+conversion_type_id+"&customer_order_id="+customer_order_id+"&value="+conversion_value+"&label="+conversion_label+"&user_agent="+user_agent+"&resolution="+resolution+"&currency="+cur+"&ccem="+cookieExpireMinutes, url_to_ct2_lib_dir);
		
		if(cookieEnabled){
			ct2_setCookie(domain_id+"ct2_ri_cm", "true", cookieExpireMinutes, "/", null);
		}else cookieValue2 = "disabled";		
	}		
}
