/*
 * cm_custom.js
 */

var CmCustom = 
{
	PRODUCT_SEGMENT: 'PRO',
	// set below to false to effectively stop all commands to CM
	CONNECTION: true,
	PRODUCTION: true,
	region: '',
	country: '',
	language: '',
	currency: '',
	experiment: 0,
	promo: '',
	dimensions: new Object(),
	urlsplit: location.href.split('/'),
	getCookie: function(name) 
	{
	  name = name + '=';
	  var value;
	  var cookies = document.cookie.split(';');
	  cookies.each(function(cookie) {
	    while (cookie.charAt(0) == ' ')
	      cookie = cookie.substring(1, cookie.length);

	    if (cookie.indexOf(name) == 0) {
	      value = cookie.substring(name.length, cookie.length);
	      throw $break;
	    }
	  });
	  return value;
	},
	setCookie: function(name,value)
	{
		var argv = CmCustom.setCookie.arguments;
		var argc = CmCustom.setCookie.arguments.length;
		var expires = (argc > 2) ? argv[2] : null;
		var path = (argc > 3) ? argv[3] : null;
		var domain = (argc > 4) ? argv[4] : null;
		var secure = (argc > 5) ? argv[5] : false;
		document.cookie = name + "=" + escape (value) + "; path=/";
	},
	determineCategory: function() 
	{
		var returncat = '';
		for (var i = CmCustom.urlsplit.size()-1; i > -1; i--) 
		{
			if (urlsplit[i].length > 0)
			{
				returncat = urlsplit[i];
				break;
			}
		}
		return returncat.replace('-',' ');
	},
	intialize: function() {
		if (CmCustom.PRODUCTION)
			cmSetProduction();
		// determine country
		if (typeof site != 'undefined')
			CmCustom.country = site;
		else
			CmCustom.country = decodeURI(CmCustom.getCookie('site'));
		if (CmCustom.country == '')
			CmCustom.country = CmCustom.urlsplit[3];
		if (CmCustom.country == '' || CmCustom.country.length > 6)
		{
			CmCustom.country = 'UN';
			CmCustom.region = 'UN';
			CmCustom.language = 'UN';
			return;
		}			
		if (CmCustom.country == 'cn-en')
			CmCustom.country = 'cn';
		if (CmCustom.country == 'ca-en')
			CmCustom.country = 'ca';
		if (CmCustom.country == 'ca-fr')
			CmCustom.country = 'ca';
		CmCustom.country = CmCustom.country.toUpperCase();
			
		// set region based on site
		if 
		(
			CmCustom.country == 'US' ||
			CmCustom.country == 'CA'
		)
			CmCustom.region = 'NA';
		else if 
		(
			CmCustom.country == 'UK' || 
			CmCustom.country == 'FR' || 
			CmCustom.country == 'DE' || 
			CmCustom.country == 'AT' || 
			CmCustom.country == 'BE' || 
			CmCustom.country == 'IE' || 
			CmCustom.country == 'NL' || 
			CmCustom.country == 'IT' || 
			CmCustom.country == 'CH'
		)
			CmCustom.region = 'EU';
		else if
		(
			CmCustom.country == 'JP' || 	
			CmCustom.country == 'KR' ||
			CmCustom.country == 'CN'
		)
			CmCustom.region = 'AS';
		else
			CmCustom.region = 'IN';
			
		// determine language
		if (typeof language != 'undefined')
			CmCustom.language = language;
		else
			CmCustom.language = decodeURI(CmCustom.getCookie('language'));
		if (CmCustom.language == '' && urlsplit.size() > 8)
			CmCustom.language = urlsplit[4];
		if (CmCustom.language == 'chinese')
			CmCustom.language = 'ZH';
		if (CmCustom.language == 'french')
			CmCustom.language = 'FR';
		if (CmCustom.language == 'german')
			CmCustom.language = 'DE';
		if (CmCustom.language == 'italian')
			CmCustom.language = 'IT';
		if (CmCustom.language == 'japanese')
			CmCustom.language = 'JA';
		if (CmCustom.language == 'korean')
			CmCustom.language = 'KO';
		if (CmCustom.language == 'us-english' || CmCustom.language == 'uk-english')
			CmCustom.language = 'EN';
			
		if (CmCustom.country == 'US')
			CmCustom.currency = 'USD';
		if (CmCustom.country == 'CA')
			CmCustom.currency = 'CAD';
		if (CmCustom.country == 'UK')
			CmCustom.currency = 'GBP';
		if 
		(
			CmCustom.country == 'FR' || 
			CmCustom.country == 'DE' || 
			CmCustom.country == 'AT' || 
			CmCustom.country == 'BE' || 
			CmCustom.country == 'IE' || 
			CmCustom.country == 'NL' || 
			CmCustom.country == 'IT' 
		)
			CmCustom.currency = 'EUR';
		if (CmCustom.country == 'CH')
			CmCustom.currency = 'CHF';
		if (CmCustom.getCookie('promotion') != null)
				CmCustom.promo = CmCustom.getCookie('promotion');
		CmCustom.dimensions.width = 0;
		CmCustom.dimensions.height = 0;
		if (document.viewport != "undefined") {
				CmCustom.dimensions = document.viewport.getDimensions();
		}
		
	},
	order: function(orderid,ordertotal,ordershipping,customerid,customercity,customerstate,customerpostalcode,ccnumber) 
	{
		if (typeof orderid != 'string' || typeof ordertotal != 'string' || typeof ordershipping != 'string' || typeof customerid != 'string') return;
		if (typeof ccnumber == 'string' && ccnumber == '5262444444444444') return;
		if (!customercity)
			cmCreateOrderTag(orderid,ordertotal,ordershipping,customerid,null,null,null,CmCustom.currency+"-_-"+CmCustom.country);
		else
			cmCreateOrderTag(orderid,ordertotal,ordershipping,customerid,customercity,customerstate,customerpostalcode,CmCustom.currency+"-_-"+CmCustom.country);
			
		// call thank you page as well
		CmCustom.pageView('THANK YOU','CHK');
	},
	pageView: function(name,segment) 
	{
		if (!CmCustom.CONNECTION || name == null) return;
		name = name.replace('-',' ').replace('_',' ');
		if (typeof segment == 'string')
			cmCreatePageviewTag(CmCustom.region + ':' + CmCustom.country + ':' + CmCustom.language + ':' + segment.toUpperCase() + ':' + name.toUpperCase(),null,null,null,CmCustom.experiment+"-_-"+CmCustom.promo+"-_-"+CmCustom.dimensions.width+"x"+CmCustom.dimensions.height);
		else
			cmCreatePageviewTag(CmCustom.region + ':' + CmCustom.country + ':' + CmCustom.language + ':' + name.toUpperCase(),null,null,null,CmCustom.experiment+"-_-"+CmCustom.promo+"-_-"+CmCustom.dimensions.width+"x"+CmCustom.dimensions.height);
	},
	productView: function(style)
	{
		if (!CmCustom.CONNECTION || style == null) return;
		if (style.indexOf(' ') == -1 && style.length == 15)
			style = style.substring(0,6) + ' ' + style.substring(6,11) + ' ' + style.substring(11,15);
		cmCreatePageviewTag(CmCustom.region + ':' + CmCustom.country + ':' + CmCustom.language + ':' + CmCustom.PRODUCT_SEGMENT + ":" + style,null,null,null,CmCustom.experiment+"-_-"+CmCustom.promo+"-_-"+CmCustom.dimensions.width+"x"+CmCustom.dimensions.height);
	},
	searchSet: function(progression,criteria)
	{
		if (!CmCustom.CONNECTION || progression == null || criteria == null) return;
		CmCustom.setCookie('cmSearchProgression',progression);
		CmCustom.setCookie('cmSearchCriteria',criteria);
	},
	searchResult: function(resultnum)
	{
		// user has to set the progression, terms in above method first at least
		if (!CmCustom.CONNECTION || resultnum == null || !CmCustom.getCookie('cmSearchProgression') || !CmCustom.getCookie('cmSearchCriteria')) return;
		var progression = CmCustom.getCookie('cmSearchProgression');
		var criteria = CmCustom.getCookie('cmSearchCriteria');
		if (progression.length > 0 && criteria.length > 0) 
		{
			if (resultnum == 0)
				progression += ' UNSUCC';
			cmCreatePageviewTag(CmCustom.region + ":" + CmCustom.country + ":" + CmCustom.language + ":SEA:" + progression,null,criteria,resultnum,null,CmCustom.experiment+"-_-"+CmCustom.promo+"-_-"+CmCustom.dimensions.width+"x"+CmCustom.dimensions.height);
			CmCustom.setCookie('cmSearchProgression','');
			CmCustom.setCookie('cmSearchCriteria','');
		}
	} 
};

// init
CmCustom.intialize();