/*--------------------------------------------------------------------------*
 * 
 * Copyright (C) 2008 Brand Labs LLC
 * 
 * Live Search cashback Affiliate Conversion Code
 * 
 * Version 1.0.0
 * 
 *--------------------------------------------------------------------------*/
/**
 * 
 * @param {Object} merchantId
 */
function cashbackConvert(merchantId) {
	var index;
	
	//Make sure these variables exists
	if(Order && OrderDetails && document.writeln) {
		//Initiate the script with the merchant, order id, and items array
		document.writeln('<script type="text/javascript">');
		document.writeln('var jf_merchant_id = \'' + merchantId + '\';');
		document.writeln('var jf_merchant_order_num = \'' + Order[0] + '\';');
		document.writeln('var jf_purchased_items = new Array();')
		document.writeln('var jf_item = null;');
		
		//Details of order
		for(index = 0; index < OrderDetails.length; index++) {
			document.writeln('jf_item = new Object();');
			document.writeln('jf_item.mpi = \'' + OrderDetails[index][2] + '\';');   //Product Code
			document.writeln('jf_item.price = \'' + OrderDetails[index][5] + '\';'); //Product Price
			document.writeln('jf_item.quantity = ' + OrderDetails[index][6] + ';');  //Quantity
			document.writeln('jf_purchased_items.push(jf_item);');
		}
		
		//Output finish script tag and jellyfish submission
		document.writeln('</script>');
		document.writeln('<script type="text/javascript" src="https://www.jellyfish.com/javascripts/1x1tracking.js"></script>')
	}	
}

/*--------------------------------------------------------------------------*
 * 
 * Copyright (C) 2008 Brand Labs LLC
 * 
 * General cookie manipulation methods
 * 
 * Version 1.0.0
 * 
 *--------------------------------------------------------------------------*/
/**
 * 
 * @param {String} name
 */
function doesCookieExist(name) {
	var value = null;
	
	//Retrieve the value
	value = getCookieValue(name);
	
	if(value == null) {
		return false;
	}
	
	return true;
}

/**
 * 
 * @param {String} name
 */
function getCookieValue(name) {
	var cookies = null;
	var cookiePackage = null;
	var value = null;
	var index;
		
	if(!document.cookie || document.cookie == null) {
		return null;
	}
	
	//Divide up the cookies
	cookies = document.cookie.split(';');

	for(index = 0; index < cookies.length; index++) {
		cookiePackage = cookies[index];
		while(cookiePackage.charAt(0) == ' ') {
			cookiePackage = cookiePackage.substring(1, cookiePackage.length);
		} 
				
		if(cookiePackage.indexOf(name + '=') == 0) {
			value = cookiePackage.substring(name.length + 1);
			
			//Check for empty
			if(value == null || value == '') {
				return '';
			}
			else {
				return value;
			}
		}
	}
	
	return null;
}

/**
 * 
 * @param {Object} name
 * @param {Object} value
 */
function addCookie(name, value) {
	document.cookie = escape(name) + '=' + escape(value) + '; path=/';
}

/**
 * 
 * @param {Object} name
 */
function deleteCookie(name) {
	document.cookie = escape(name) + '=; expires=-1; path=/';
}

/*--------------------------------------------------------------------------*
 * 
 * Copyright (C) 2008 Brand Labs LLC
 * 
 * General URL manipulation methods
 * 
 * Version 1.0.0
 * 
 *--------------------------------------------------------------------------*/
/**
 * 
 * @param {Object} name
 */
function getQueryStringValue(name) {
	var queryStrings = null;
	var index;
	var queryStringPackage = null;
	var value = null;
	
	if(!window.location || window.location.search == null) {
		return null;
	}
	
	//Break up by ampersand
	queryStrings = window.location.search.split('&');
	
	for (index = 0; index < queryStrings.length; index++) {
		queryStringPackage = queryStrings[index];
		
		//Remove the ? if it exists
		if(queryStringPackage.length > 1 && queryStringPackage.charAt(0) == '?') {
			queryStringPackage = queryStringPackage.substring(1, queryStringPackage.length);
		}
		
		//Check for null
		if(queryStringPackage == null) {
			continue;
		}
		
		//Breakup into array
		queryStringPackage = queryStringPackage.split('=');
		
		if(queryStringPackage[0] != null && queryStringPackage[0].toLowerCase() == name.toLowerCase()) {
			if(queryStringPackage.length == 1) {
				return null;
			}
 			
			return queryStringPackage[1];
		}
	}

	return null;	
}

/*--------------------------------------------------------------------------*
 * Execute in order to assign cookie
 *--------------------------------------------------------------------------*/
if(getQueryStringValue('source') != null && getQueryStringValue('source') == 'jellyfish') {
	addCookie('source', 'jellyfish');
}
/*--------------------------------------------------------------------------*/