//////////////////////////////////////////////
// Program name : conversion.js
//////////////////////////////////////////////
function execConversion(userId) {

	// get nowtime
	var nowtime = new Date();
	var nowtimestr = getDatetimeString( nowtime );
	var cookieId;
	var luc;
	var dsnc;
	var kwc;
	var clickid;
	// Get cookie data
	var cookie_data = document.cookie + ";";
	var key = "Convcast_SessID";
	var cookieId = getConvcastCookie( cookie_data, key );
	key="@ConvcastAccessCountID-"+userId;	
	luc=getConvcastCookie(cookie_data,key);
	
	key="@ConvcastRotationID-"+userId;
	dsnc=getConvcastCookie(cookie_data,key);	
	
	key="@ConvcastClickID-"+userId;	
	clickid=getConvcastCookie(cookie_data,key);
	setConvcastCookie( key, '0', nowtime, 360, "" ); 

	var uri = getConvcastDomain() + "/access/conversion.php"
	+ "?uid=" + escape(userId)
		+ "&frm=" + escape( document.referrer )
		+ "&uri=" + escape(document.location)
		+ "&dt=" + nowtimestr;

	if( cookieId.length>0 )
		uri += "&cid=" + escape(cookieId);

	if( luc.length>0 )
		uri += "&luc=" + escape(luc);

	if( dsnc.length>0 )
		uri += "&dsnc=" + escape(dsnc);
	if( clickid.length>0 )
		uri += "&clickid=" + escape(clickid);
	cookieId=null;
	luc=null;
	dsnc=null;
	clickid=null;
	
	//if(userId==565) prompt('',uri);

	var convcast = document.getElementById("conversion");

	var out = document.createElement("script");

	out.id = "conversion_start"; 
	out.src = uri;	
	uri=null;

	convcast.appendChild( out );
}


////////////////////////////////////////////////////////////
// get Convcast server domain name
var CVcurrentScriptTag = (function (e) { if(e.nodeName.toLowerCase() == 'script') return e; return arguments.callee(e.lastChild) })(document);
function getConvcastDomain()
{
	var src_str;
	var prtcl;
	var nodeElement;

	src_str = CVcurrentScriptTag.getAttribute('src');
	if ((src_str != null) && (src_str.search('conversion.js') >= 0))
	{
		prtcl = 'http';
		if (src_str.substr(0,6) == 'https:')
			prtcl = 'https';
		src_str = src_str.substr(src_str.search('//') + 2);
		src_str = src_str.substr(0,src_str.search('/'));
		return prtcl + '://' + src_str;
	}
	return null;
}

/***********************************************************
 * get date to string
 * @param date
 * @return string
 */
function getDatetimeString( date ) {
	var yyyy = date.getFullYear();
	var mm   = date.getMonth() + 1;
	var dd   = date.getDate();
	var hh   = date.getHours();
	var mi   = date.getMinutes();
	var ss   = date.getSeconds();

	ret  = "" + yyyy;
	ret += Math.floor(mm/10)%10;
	ret += mm%10;
	ret += Math.floor(dd/10)%10;
	ret += dd%10;
	ret += Math.floor(hh/10)%10;
	ret += hh%10;
	ret += Math.floor(mi/10)%10;
	ret += mi%10;
	ret += Math.floor(ss/10)%10;
	ret += ss%10;

	return ret;
}

//////////////////////////////////////////////
// Get Convcast cookie data
function getConvcastCookie( cookieData, key ) {
	var ret = "";
	key = key + "=";
	var offset = cookieData.indexOf( key );

	if( offset>-1 ) {	// There is.
		s = offset + key.length;
		e = cookieData.indexOf( ";", s );
		ret = unescape(cookieData.substring(s, e));
	} else {	// none
		ret = "";
	}

	return ret;
}

//////////////////////////////////////////////
// Set Convcast cookie data
// @param cookie_key
// @param write value
// @param now cookietime string
// @param how long days does it keep cookie.
// @param cookie path
function setConvcastCookie( cookieKey, myValue, cookietime, myDay, myPath ) {

	if( myValue.length<=0 )	return;
	
	var myItem = cookieKey + "=" + escape(myValue) + ";";
	var myExp = new Date();
	myExp.setTime(cookietime.getTime()+(myDay*24*60*60*1000));
	var myExpires = "expires=" + myExp.toGMTString() + ";";

	if( myPath.length>0 ) {
		myPath = "path=" + myPath + ";";
		document.cookie =  myItem + myExpires + myPath;
	} else {
		document.cookie =  myItem + myExpires;
	}

	CVmakeServerCookie(cookieKey,myValue,myDay);

}

//make server side cookie
function CVmakeServerCookie(parm,value,d){
	var CVtmpSrc=getConvcastDomain()+ '/js/makeServerCookie.php?t=' + new Date().getTime();
	CVtmpSrc += '&parm=' + parm;
	CVtmpSrc += '&value=' + value;
	CVtmpSrc += '&d=' + d;

	var CVtmpElement=document.createElement('script');
	CVtmpElement.setAttribute('src',CVtmpSrc);
	document.body.appendChild(CVtmpElement);
	document.body.removeChild(CVtmpElement);
}

/*******************************************************
 * get browser type
 * @return using browser version
 */
function getBrowserType() {
	var ua = navigator.userAgent;
	var ret = "unknown";

	if( ua.indexOf("Safari")>-1) {
		ret = "Safari";
	} else if( ua.indexOf("Opera")>-1 ) {
		ret = "Opera";
	} else if( ua.indexOf("Firefox")>-1 ) {
		ret = "Firefox";
	} else if( ua.indexOf("Gecko")>-1 ) {
		ret = "Netscape / Mozilla";
	} else if( ua.indexOf("Sleipnir")>-1 ) {
		ret = "Sleipnir";
	} else if( ua.indexOf("MSIE ")>-1 ) {
		ret = "MSIE";
	} else if( ua.indexOf("Mozilla")>-1 ) {
		ret = "Netscape";
	}

	return ret;
}

/*******************************************************
 * copy textarea text 
 * @param textarea's id
 */
function JM_cc(bb)
{
    var ob=document.getElementById(bb);
    ob.select();
    js=ob.createTextRange();
    js.execCommand("Copy");	
}	
// End of file
