/**
* @fileoverview ebiz.js: A module used for client specific functionality
*
* This module defines a single symbol named "Venda.Ebiz"
* all ebiz utility functions are stored as properties of this namespace
* functions that are spacific this site shoudl be added to this file only.
*/

//Declare namespace for ebiz
Venda.namespace("Ebiz");

//Description: Returns the value of a specified URL parameter
//Parameters:
//1. currURL = this is the URL which you wish to get the URL parameter value from
//2. urlParam = this is the name of the URL parameter you want to get the value for
//Returns: value for parameter specified urlParam.

Venda.Ebiz.createCookie = function(name,value,days) {
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
};


Venda.Ebiz.tradeDBTracking = function() {
	if (location.href.indexOf("tduid")!=-1) {
		var tduid_value = getUrlParam(location.href, "tduid")
		
		if (tduid_value != "" && tduid_value !=null) {
			Venda.Ebiz.createCookie('TRADEDOUBLER',tduid_value,7);
		}
	}
};

addEvent(window, 'load', function() { Venda.Ebiz.tradeDBTracking(); }, false);

//------- Start RT103241 -------
Venda.Ebiz.formatDate = function (formatDate, formatString) {
	if(formatDate instanceof Date) {
		var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		var yyyy = formatDate.getFullYear();
		var yy = yyyy.toString().substring(2);
		var m = formatDate.getMonth();
		var m2 = m+1;
		var mm = m2 < 10 ? "0" + m2 : m2;
		var mmm = months[m];
		var d = formatDate.getDate();
		var dd = d < 10 ? "0" + d : d;

		var h = formatDate.getHours();
		var hh = h < 10 ? "0" + h : h;
		var n = formatDate.getMinutes();
		var nn = n < 10 ? "0" + n : n;
		var s = formatDate.getSeconds();
		var ss = s < 10 ? "0" + s : s;

		formatString = formatString.replace(/yyyy/i, yyyy);
		formatString = formatString.replace(/yy/i, yy);
		formatString = formatString.replace(/mmm/i, mmm);
		formatString = formatString.replace(/mm/i, mm);
		formatString = formatString.replace(/m/i, m);
		formatString = formatString.replace(/dd/i, dd);
		formatString = formatString.replace(/d/i, d);
		formatString = formatString.replace(/hh/i, hh);
		formatString = formatString.replace(/h/i, h);
		formatString = formatString.replace(/nn/i, nn);
		formatString = formatString.replace(/n/i, n);
		formatString = formatString.replace(/ss/i, ss);
		formatString = formatString.replace(/s/i, s);
			return formatString;
		} else {
			return "";
		}
};
Venda.Ebiz.uniqueNumber = function(){
	var randomnumber = Math.floor(Math.random()*101);
	var number = randomnumber<10?"0"+randomnumber:randomnumber;
	var oid = Venda.Ebiz.formatDate(new Date(), "ddmmyyyyhhnnss")+number;
	
	return oid;
};

Venda.Ebiz.removeSpaces = function(string) {
 return string.split(' ').join('');
};

var url = document.referrer;
var cj = new CookieJar({expires: 3600 * 24 * 7, path: '/'});
var CJ_COOKIE_NAME = 'cjtracking';
var LEADPIXEL_COOKIE_NAME = 'leadpixel';
if(url.indexOf("leekes.") == "-1"){
	if((cj.get(CJ_COOKIE_NAME)!=null) || (cj.get(CJ_COOKIE_NAME)!="")){
		cj.remove(CJ_COOKIE_NAME);
		cj.put(CJ_COOKIE_NAME, Venda.Ebiz.uniqueNumber());
	}
}

Venda.Ebiz.commissionJunction = function(catname){
	var catnospace = Venda.Ebiz.removeSpaces(catname);
	var itemid = catnospace.toLowerCase();
	var oid = cj.get(CJ_COOKIE_NAME);
	var urlfirst = "https://www.emjcd.com/u?CID=1508256&OID=";
	var urlsecond = "&TYPE=322047&ITEM1="+itemid+"&AMT1=0&QTY1=1&METHOD=IMG";
	var cjurl = urlfirst+oid+urlsecond;
	
	cj.put(LEADPIXEL_COOKIE_NAME, cjurl);
};
var feedComcj = new CookieJar({expires:'',path: '/'});
	if(getUrlParam(window.location.href,'source')!=""){
		var feedComparamValue = getUrlParam(window.location.href,'source');
		var feedComCookie = feedComcj.put("source",feedComparamValue);
	}

/**
* Split a string so it can be displayed on multiple lines so it does not break display layout - used on order confirmation and order receipt page
* @param {string} strToSplit string that needs to be split 
* @param {Integer} rowLen length of row which will hold the string
* @param {string} displayElem the html container which will display the splitted string
*/
Venda.Ebiz.splitString = function(strToSplit, rowLen, dispElem) {
var stringlist = new Array();
while (strToSplit.length > rowLen) {
stringlist.push( strToSplit.slice(0,rowLen));
strToSplit=strToSplit.substr(rowLen);
}
if (strToSplit.length) {
stringlist.push(strToSplit);
}
document.getElementById(dispElem).innerHTML = stringlist.join('<br>');
};

/**
* A skeleton function for validating user extened fields - needs to be amended by the build team
* @param {object} frmObj HTML form containing user extended field elements
*/
Venda.Ebiz.validateUserExtendedFields = function(frmObj) {
if(frmObj) {
/*if ( (frmObj.usxtexample1.checked==false) && (frmObj.usxtexample2.checked==false) && (frmObj.usxtexample3.checked==false)) { 
alert("Please tick at least one checkbox");
return false;
} */ 
return true; 
} 
return false;
};
