/*************************************************************************************
*	function that sets a cookie; you can omit trailing arguments that aren't needed
*************************************************************************************/
function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

/*************************************************************************************
*	function to return the value of the cookie 'name'
*************************************************************************************/
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );

}

/*************************************************************************************
*	function to delete a cookie; you can omit trailing arguments if not needed
*************************************************************************************/
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

/*************************************************************************
* Cookie Setter for TrackingID cookie with 30 minute timeout
**************************************************************************/
function setTrackingCookie( name, value, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	var expires_date = new Date( today.getTime() + (30 * 1000 * 60) );
	document.cookie = name + '=' + escape( value ) + ';expires=' + expires_date.toGMTString() + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}



/****************************************************************************************************************
*	function to get the values of the MoveTracking cookie KID<CID,WID  and write an Image Pixel to PageSource
*****************************************************************************************************************/
function getMoveTrackingCookie( pageName ) {

	var Cookie = YAHOO.util.Cookie;

	if((pageName == 'rate') && (Cookie.get("TrackingCookie")!=null)){
		var  kid = Cookie.getSub("TrackingCookie","kid");
		var  cid = Cookie.getSub("TrackingCookie", "cid");
		var  wid = Cookie.getSub("TrackingCookie", "wid");
		var  kdt = Cookie.getSub("TrackingCookie", "kdt");

		if ((kid != null) && (kid != "")){
			createPixelImage(cid,wid,kid,kdt);
		}
        }         
}

/****************************************************************************************************************
*	function to append  an Image Pixel to footer in the PageSource
*****************************************************************************************************************/

function createPixelImage(cid,wid,kid,kdt){

	var varImage = document.createElement('img');
	var varDiv = document.createElement('div');
	varDiv.setAttribute('id', 'imageDiv');
	varDiv.setAttribute('style', 'display:none;');
	
	var src = 'http://test.movetracking.net/log.aspx?'+ 
		((kid != '') ? 'kid=' + kid : '') +
		((cid != '') ? '&cid=' + cid : '')  +
		((wid != '') ? '&wid=' + wid : '') +
		((kdt != '') ? '&kdt=' + kdt : '') +
		'&type=lead&saleAmt=25&commAmt=5&txid=123456';
			    
	varImage.setAttribute('src', src);
	varImage.setAttribute('alt', '');
	varImage.setAttribute('border', '0');
	varImage.setAttribute('style', 'height:1px;width:1px;');
	varImage.setAttribute('id','imagePixel');
	
	var insertLocation;
	
	if(document.getElementById('footer')){
		rtLocation = document.getElementById('footer')
	}
	else{
		insertLocation = document.getElementById('ft')
	}
	varDiv.appendChild(varImage);
	insertLocation.appendChild(varDiv);

}
