/**
* @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");
Venda.namespace("Ebiz.Price");
/**
 * example function below
 * ebiz.myFunctionname = function(param1, param2) {
 *	..... Place function code here ....
 * }; 
 */

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

Venda.Ebiz.Price.showSavingPrice = function(invtwas, invtsell, elementId) {
        var msg = Venda.Ebiz.Price.SavingPrice(invtwas, invtsell,elementId);
        if (document.getElementById(elementId)) {
                document.getElementById(elementId).innerHTML = msg;
        }

};

Venda.Ebiz.Price.showSavingPriceAttr = function(elementId) {

    var divSaveId = "savingDiv_"+elementId;
	var divSaveLabel = "savingDiv_"+elementId+"Label";
    var attrSelect = $("select");
	var attrVal=""
	for(var i=0;i<attrSelect.length;i++){
		attrVal+= attrSelect[i].value;
	};
	if(product.attributeValues[attrVal]!=undefined){
		var invtsell = product.attributeValues[attrVal].data["atrsell"];
		var invtwas = product.attributeValues[attrVal].data["atrwas"];
	}else{
		var invtsell = document.getElementById("PriceDiv_"+elementId).innerHTML;
   		var invtwas = document.getElementById("wasDiv_"+elementId).innerHTML;
	};
	if(invtsell==""){
		document.getElementById("PriceDiv_"+elementId+"Label").style.display="none";
	}else{
		document.getElementById("PriceDiv_"+elementId+"Label").style.display="inline";
	}
	if(invtwas==""){
		document.getElementById("wasDiv_"+elementId+"Label").style.display="none";
	}else{
		document.getElementById("wasDiv_"+elementId+"Label").style.display="inline";
	}
    if(invtwas!=""){
           invtwas = parseFloat(invtwas.replace(/^\D+/, ''));
           invtsell = parseFloat(invtsell.replace(/^\D+/, ''));
    		var invtwasRep = invtwas.toFixed(2);
   			var invtsellRep= invtsell.toFixed(2);
			msg = Venda.Ebiz.Price.SavingPrice(invtwas,invtsell,elementId);
        	if (document.getElementById(divSaveId)) {
                document.getElementById(divSaveId).innerHTML = msg;
				document.getElementById(divSaveLabel).innerHTML ="";
       		 }
    }else{
		if (document.getElementById(divSaveId)) {
                document.getElementById(divSaveId).innerHTML = "";
				document.getElementById(divSaveLabel).innerHTML ="";
       		 }
	}
};

Venda.Ebiz.Price.SavingPrice = function(invtwas, invtsell,elementId) {
        var msg = "";
		var invtwasDiv = document.getElementById("wasDiv_"+elementId+"Label").innerHTML;
        if (invtwas > invtsell) {
                var invtsaving = invtwas - invtsell;
          		if (invtsaving > 0) {
						if(invtwasDiv.indexOf("from")==-1){
                        	msg = "Save &pound;"+invtsaving.toFixed(2);
						}else{
							msg = "Save &pound;"+invtsaving.toFixed(2);
						}
                } else {
                        msg = '';
                }
        }
        return msg;
};

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


$(document).ready(function(){
	$(".Lcat_topnavmenuandsearch div:first").addClass("first");
	$(".Lfooter ul li:last").addClass("last");
}); 






//Declare namespace for bklist
Venda.namespace("Ebiz.BKList");
Venda.Ebiz.BKList.jq = jQuery; 
Venda.Ebiz.BKList.configBKList = {
		bklist: "",
		divArray: ['#sortby','.sort_results', '.searchpsel', '.pagn', '.refinelist'],
		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.jq(document).ready(function() {
	if(Venda.Ebiz.BKList.configBKList.enableBklist){
		Venda.Ebiz.BKList.ChangeLink();
	}
});

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);
		}			
	}
}

Venda.Ebiz.RemoveAttribute = function(){
	jQuery("#productdetail select[name=att1],#productdetail select[name=att2]").each(function(){
		var optionLength=jQuery(this).find("option").length;
		if(optionLength<3){jQuery(this).find("option:first").remove();}
	});
};

Venda.Ebiz.attributesValue ="";
Venda.Ebiz.attributeEMWBIS = function(){
 var attributesValue= document.form.att1.value + document.form.att2.value;
 Venda.Ebiz.attributesValue = product.attributeValues[attributesValue].data["atrsku"];
};

