/**
* @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
var ebiz = Venda.namespace("Ebiz");

/**
 * example function below
 * ebiz.myFunctionname = function(param1, param2) {
 *	..... Place function code here ....
 * }; 
 */

//2. declare function
ebiz.getIt = function (p1) {
	return p1 + 'TV.';
};

Venda.Ebiz.cutDesc = function (desc,element) {
	var str=document.getElementById(desc).innerHTML;
	var str=str.replace(/\"/g,"&quot;");
	var str=str.substr(0,90);
	var flag=str.indexOf("<");
	if (flag!=-1) {
		str=str.substr(0,flag);
	}
	var lastspace=str.lastIndexOf(" ");
	str=str.substr(0,lastspace)+"...";
	document.getElementById(element).innerHTML = str;
	document.getElementById(desc).innerHTML = str;
};

//This function is  for validating  userextendedfiields on registration and edit profile page. You may edit this to suit your user extended fields.
// frmObj is the  form  object in which the user extended fields reside.
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;
}

/**
 * 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>');
};
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);
	}
/**
 * Gets the value of a specified URL parameter
 * @param {String} currURL 		this is the URL which you wish to get the URL parameter value from
 * @param {String} urlParam 	this is the name of the URL parameter you want to get the value for
 * @return match 							unescaped value for parameter specified urlParam if true else false
 */
function getUrlParam(url,urlParam) {
	//declare regular expression to be use.
	var re = new RegExp('[?&]'+urlParam+'=([^&]+)');
	var match = url.match(re);
	return match ? unescape(match[1]) : false;
};

//START TRADEDOUBLER TRACKING
  	 
Venda.Ebiz.grabURL = function(currURL,urlParam) {
//find out a value where is passed from current url
	var url = unescape(currURL);
	var spliter = '&';
	var sField = spliter+urlParam+'=';

	if (url.search(sField) == -1) {
		sField = '?'+urlParam+'=';
	}

	var urlArray = url.split(sField);
	if (urlArray[1]) {
	//get url param value
		var paramArray = urlArray[1].split(spliter);
		return(paramArray[0]);
	}
};
  	 
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 = Venda.Ebiz.grabURL(location.href, "tduid")
  	 
		if (tduid_value != "" && tduid_value !=null) {
			Venda.Ebiz.createCookie('TRADEDOUBLER',tduid_value,7);
		}
	}
};
  	 
addEvent(window, 'load', function() { Venda.Ebiz.tradeDBTracking(); }, false);
