/*
* @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");
/*
* 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;
};

//Declare namespace for bklist
Venda.namespace("Ebiz.BKList");
Venda.Ebiz.BKList.jq = jQuery; 
Venda.Ebiz.BKList.configBKList = {
		bklist: "",
		divArray: ['#sortby','.sort_results', '.searchpsel', '.pagn', '.refineblock', '.iconView'],
		removeDivArray:['.categorytree'],
		enableBklist: true
}; 
/**
* Sets the config values to each config type
* @param {string} configType this is an configuration type name
* @param {array} settings this is the value of each configuration type
*/

Venda.Ebiz.BKList.init = function(settings) {
	for (var eachProp in settings) {
		this.configBKList[eachProp] = settings[eachProp];
	}
};

Venda.Ebiz.BKList.getUrl = function(){
	var curUrl = document.location.href; 
	if(curUrl.indexOf("&amp;") != -1){
		 curUrl = curUrl.replace(/&amp;/gi,'&');		
	}
	return Venda.Platform.getUrlParam(curUrl, "bklist");
}

Venda.Ebiz.BKList.ChangeLink = function(){
var divArray = Venda.Ebiz.BKList.configBKList.divArray;  
var removeDivArray = Venda.Ebiz.BKList.configBKList.removeDivArray;  
var bklist = Venda.Ebiz.BKList.configBKList.bklist || Venda.Ebiz.BKList.getUrl() || "";
var strBklist = "&bklist";
	if (bklist != "") {
		var addBklist =  strBklist + "=" + bklist;
		for (var i = 0; i < divArray.length; i++) {

			if(Venda.Ebiz.BKList.jq(divArray[i]+ " a").attr("href")){
				Venda.Ebiz.BKList.jq(divArray[i]+ " a").attr("href", function() {
					return Venda.Ebiz.BKList.jq(this).attr("href") + addBklist; 			
				});	
			}
			
			if(Venda.Ebiz.BKList.jq(divArray[i] + " option").attr("value")){
				Venda.Ebiz.BKList.jq(divArray[i] + " option").attr("value", function() {	
					return Venda.Ebiz.BKList.jq(this).attr("value") + addBklist; 
				});				
			}
		}	
		
		for (var i = 0; i < removeDivArray.length; i++) {
			Venda.Ebiz.BKList.jq(removeDivArray[i]+" a").attr("href", function() {
				var currentLink = Venda.Ebiz.BKList.jq(this).attr("href");
				var newLink = currentLink;
				if (newLink.length > 2) {
					if (currentLink.indexOf("&bklist=") != -1) {
						newLink = currentLink.substring(0, currentLink.indexOf("&bklist="));
					}
					if (newLink.indexOf("&view=") != -1) {
						newLink = newLink.substring(0, newLink.indexOf("&view="));
					}
				}
				return newLink;
			});
		}
		if(Venda.Ebiz.BKList.jq("#tag-pageSearchurlFull")){
			var newUrl = Venda.Ebiz.BKList.jq("#tag-pageSearchurlFull").text()+ addBklist ;
			Venda.Ebiz.BKList.jq("#tag-pageSearchurlFull").text(newUrl);
		}
		if(Venda.Ebiz.BKList.jq("#tag-pageSearchurl")){
			var newUrl = Venda.Ebiz.BKList.jq("#tag-pageSearchurl").text()+ addBklist ;
			Venda.Ebiz.BKList.jq("#tag-pageSearchurl").text(newUrl);
		}		
		if(Venda.Ebiz.BKList.jq("#tag-pageIcaturl")){
			var newUrl = Venda.Ebiz.BKList.jq("#tag-pageIcaturl").text()+ addBklist ;
			Venda.Ebiz.BKList.jq("#tag-pageIcaturl").text(newUrl);
		}			
	}
};

//create namespace
//Venda.namespace("Ebiz");
Venda.Ebiz.awCookieChecker=function(awCookieURL){var AW_COOKIE_NAME='awchecking';if(awCookieURL.indexOf("source=aw")!=-1){var cj=new CookieJar({expires:3600*24*30,path:'/'});cj.put(AW_COOKIE_NAME,"1")}};

/**
* generate dropdown
* @param {string} jquery selector
* @param {number} start number
* @param {number} end number
*/
Venda.Ebiz.addNumberDropdown = function(selectors,start,end, prefix){
	var selectorArray=selectors.split(",");
	for(var i=0;i<selectorArray.length;i++){
		var selector = selectorArray[i];
		if(start>end){
			for(var j=start;j>=end;j--){
				var opvalue=j;
				if(typeof(prefix) != "undefined"){
					if(opvalue<10) opvalue="0"+opvalue;
				}
				jQuery(selector).append('<option value="'+opvalue+'">'+opvalue+'</option>');
			}
		}else{
			for(var j=start;j<=end;j++){
				var opvalue=j;
				if(typeof(prefix) != "undefined"){
					if(opvalue<10) opvalue="0"+opvalue;
				}
				jQuery(selector).append('<option value="'+opvalue+'">'+opvalue+'</option>');
			}
		}
	}
};
/* CCC13 validation */
Venda.Ebiz.CCC13=function(GTIN, checkDigit){
	var factor = 3;
	var sum = 0;
	var cc=0;
	GTIN=String(GTIN);
	if (GTIN.length!=12){
		return false;
	}else{
		for (index = GTIN.length; index > 0; --index) {
			sum = sum + GTIN.substring (index-1, index) * factor;
			factor = 4 - factor;
		}
		cc = ((1000 - sum) % 10);
		if(cc==checkDigit){
			return true;
		}else{
			return false;
		}
	}
};
/* Remove special char. and Replace space with underscore(_) */
Venda.Ebiz.removeSpecialChar=function(str){
	if(str.length>0){
		str=str.replace(/[^a-zA-Z 0-9]+/g,'');
		str=str.replace(/[ ]+/g,'_');
		return str;
	}else{
		return "";
	}
};

Venda.Ebiz.daysInMonth = function(Y, M) {with (new Date(Y, M, 1, 12)) {setDate(0);return getDate();}};
Venda.Ebiz.dateDiff = function(date1, date2) {
    var y1 = date1.getFullYear(), m1 = date1.getMonth(), d1 = date1.getDate(),y2 = date2.getFullYear(), m2 = date2.getMonth(), d2 = date2.getDate();
    if (d1 < d2) {m1--;d1 += Venda.Ebiz.daysInMonth(y2, m2);}
    if (m1 < m2) {y1--;m1 += 12;}
    return [y1 - y2, m1 - m2, d1 - d2];
};


/**
* Equal width
* @param {string} inputbox selector
* @param {string} selectbox selector
*/
Venda.namespace('Ebiz.EqualWidth');
Venda.Ebiz.EqualWidth.jq = jQuery;
Venda.Ebiz.EqualWidth.set = function(inputClass, selectClass) {
	var maxWidth = 0;
	var maxBorder = 0;
	var maxLeftPadding=0;
	var maxRightPadding=0;
	var allClass=inputClass+", "+selectClass;
	Venda.Ebiz.EqualWidth.jq(allClass).each(function(){
		var curWidth = Venda.Ebiz.EqualWidth.jq(this).width();
		var curLeftPadding = parseInt(Venda.Ebiz.EqualWidth.jq(this).css("padding-left"));
		var curRightPadding = parseInt(Venda.Ebiz.EqualWidth.jq(this).css("padding-right"));
		if (curLeftPadding >= maxLeftPadding) {maxLeftPadding=curLeftPadding;}
		if (curRightPadding >= maxRightPadding) {maxRightPadding=curRightPadding;}
		if(this.offsetWidth>curWidth){curWidth=this.offsetWidth;}
		if (curWidth >= maxWidth) {
			maxWidth = curWidth;
		}
	});
	Venda.Ebiz.EqualWidth.jq(selectClass).each(function(){
		var curBorder=parseInt(Venda.Ebiz.EqualWidth.jq(this).css("border-left-width"))+parseInt(Venda.Ebiz.EqualWidth.jq(this).css("border-right-width"));
		if(curBorder>=maxBorder){maxBorder=curBorder;}
	});
	Venda.Ebiz.EqualWidth.jq(inputClass).css('width', maxWidth + 'px');
	if (Venda.Ebiz.EqualWidth.jq.browser.safari || Venda.Ebiz.EqualWidth.jq.browser.opera){
		Venda.Ebiz.EqualWidth.jq(selectClass).css('width', (maxWidth+maxLeftPadding+maxBorder) + 'px');
	}else if (!(Venda.Ebiz.EqualWidth.jq.browser.safari || Venda.Ebiz.EqualWidth.jq.browser.opera)) {
		Venda.Ebiz.EqualWidth.jq(selectClass).css('width', (maxWidth+maxLeftPadding+maxRightPadding+maxBorder) + 'px');
	}
};

Venda.namespace('Platform.EqualHeight');

/**
 * Stub function is used to support JSDoc.
 * @class Venda.Platform.EqualHeight
 * @constructor
 */
Venda.Platform.EqualHeight = function(){};

//set jQuery object
Venda.Platform.EqualHeight.jq = jQuery;

/**
 * This function will get the list of html element and class name that need to set height then set height of each element by using function 'set' <br>Example: Code will need to be placed in the template where the function will be used<br><pre>var productInfo = new Array ('li .productname', 'li .brd_featprods', 'li .iconbox', 'li .invtdesc2', 'li .font-red');<br>Venda.Platform.EqualHeight.init(productInfo);</pre>
 * 
 *  @param {Array} elementsToSet - Array that contain html element with class name under html tag e.g. 'li .invtname'
 */
Venda.Platform.EqualHeight.init = function(elementsToSet) {
	var elementsToSetLen = elementsToSet.length;
	Venda.Platform.EqualHeight.jq(document).ready(function(){
		for (var i=0; i < elementsToSetLen; i++) {
			Venda.Platform.EqualHeight.set(elementsToSet[i]);
		}
	});
};

Venda.Platform.EqualHeight.set = function(setClass) {
	var maxHeight = 0;
	Venda.Platform.EqualHeight.jq(setClass).each(function(){
		var curHeight = Venda.Platform.EqualHeight.jq(this).height();
		if (curHeight >= maxHeight) {
			maxHeight = curHeight;
		}
	});
	if(maxHeight>0){
		Venda.Platform.EqualHeight.jq(setClass).css((Venda.Platform.EqualHeight.jq.browser.msie && Venda.Platform.EqualHeight.jq.browser.version < 7 ? '' : 'min-') + 'height', maxHeight + 'px');
	}
};

Venda.Ebiz.runEqualHeight = function(){
	var productInfo = new Array ('.bestsellersproducts .details .price', '.bestsellersproducts .details h2', '.bestsellersproducts .details', '.featprods .details .price', '.featprods .details h2', '.featprods .details', '.products .details h2', '.products .details .price', '.products .details .loyaltyproductinfo', '.products .details', '.detailswrap');
	Venda.Platform.EqualHeight.init(productInfo);
};

Venda.Ebiz.restoreAns = function(Answers){
	
	var AnswersArray = Answers.split("&");

	for(var i=0;i<AnswersArray.length;i++){
		ans = AnswersArray[i].split("=");
		if(ans[0]!="" && ans[1]!=""){
			ans[1]=ans[1].replace("+"," ")
			if(jQuery("*[name="+ans[0]+"]").attr("type")=="checkbox"){
				if(ans[1] ==jQuery("*[name="+ans[0]+"]").val()){
					jQuery("*[name="+ans[0]+"]").attr("checked", true);
				}
			}else{
				jQuery("*[name="+ans[0]+"]").val(decodeURIComponent(ans[1].replace("+"," ")));
			}
		}
	}

	if(jQuery(".registercollectionservice select").val()!=""){jQuery(".registercollectionservice").show();}
	if(jQuery(".sendcollectionservice select").val()!=""){jQuery(".sendcollectionservice").show();}

	var showChildInfo = false;
	for(var i=1;i<=4;i++){
		if(jQuery("*[name=childname"+i+"]").val()!="" || jQuery("*[name=childgender"+i+"]").val()!="" || jQuery("*[name=childbirthday"+i+"]").val()!=""){
			showChildInfo=true;
			jQuery("div.childlist div.child"+i).show();
		}
	}
	if(showChildInfo){
		jQuery("div.childlist").show();
	}
};


jQuery(function() {
	//Bklist
	if(Venda.Ebiz.BKList.configBKList.enableBklist){
		Venda.Ebiz.BKList.ChangeLink();
	}
	
	if(jQuery("input[name=fname]").length>0){
		Venda.Ebiz.EqualWidth.set("#fname, #lname, #num, #addr1, #addr2, #city, #statetext, #zipc, .phonewrap, #usxtmobile, #addrname, #usemail, #uspswd, #uspswd2",", #statelist, #cntrylist, #usxtgender, .phonewrap, #title");
	}
	jQuery("input[type=checkbox], input[type=radio]").css("border","none");
	
	if(jQuery(".earnpoint").length > 0){
		jQuery(".earnpoint").each(function(){
			var price = jQuery(this).find("span").text();
			var earnPoint = Math.floor(price) * 1;
 			if(earnPoint > 0){
				jQuery(this).find("span").text(earnPoint);
				if(earnPoint > 1){
					jQuery(this).append('s');
				}
				jQuery(this).show();
			}else{
				return;
			}
		});
	}
	
});
