//create namespace
Venda.namespace("Ebiz");

Venda.Ebiz.bklistReplace = function (url,icatref,catref,bklist){ 
	var bkListLocation=location.href;
	 var shopCatreeCheck = bklist;
	if(bkListLocation.indexOf('&bklist=')!=-1){
		var bkListLocationOnly=bkListLocation.split('&bklist=');
		bkListLocationOnly = bkListLocationOnly[1].replace(/#/g,"");
	}else{
		var bkListLocationOnly = "";
	}
	if(bkListLocation.indexOf("&bklist=")!=-1 || bklist=="shop"){
		if(document.getElementById("nobklist")){
			var noBklistNeed = document.getElementById("nobklist").innerHTML;
			var checkBklist = url;
			 if(noBklistNeed.indexOf(checkBklist)==-1){
			 var bklistUrl = bkListLocationOnly;
				 if(bklistUrl.indexOf("invt,")!=1 && (icatref!="" &&catref !="")){
				 var replacebklistUrl = bklistUrl.replace(/invt,/g,"icat,");
				 	originalbklist = originalbklist.replace(/bklist=invt,/g,"bklist=icat,shop");
				 }
				if(bklistUrl.indexOf("icat,3")!=-1 && (icatref!="" &&catref !="")){
				    var replacebklistUrl = bklistUrl.replace(/icat,3/g,"icat,4");
					replacebklistUrl += "," +icatref;
					location.href = url +"&bklist=" + replacebklistUrl;
				 }	
				if(bklistUrl.indexOf("icat,4")!=-1 && (icatref!="" &&catref !="")){
				    var replacebklistUrl = bklistUrl.replace(/icat,4/g,"icat,5");
					replacebklistUrl += "," +icatref;
					location.href = url +"&bklist=" + replacebklistUrl;
				 }
				 if(bklistUrl.indexOf("icat,5")!=-1 && (icatref!="" &&catref !="")){
				    var replacebklistUrl = bklistUrl.replace(/icat,5/g,"icat,6");
					replacebklistUrl += "," +icatref;
					location.href = url +"&bklist=" + replacebklistUrl;
				 }
				 if(shopCatreeCheck=="shop"){
					var replacebklistUrl = "icat,4,shop," + catref + "," + icatref;
					location.href = url +"&bklist=" + replacebklistUrl;
				 }
				 if((icatref=="" &&catref =="")){
				 	var replacebklistUrl = bklistUrl;
					location.href = url +"&bklist=" + replacebklistUrl;	
				 }
			}else{
				location.href = url;
			}
		}
		else{
			 	var bklistUrl = bkListLocationOnly;
				 if(bklistUrl.indexOf("invt,")!=1 && (icatref!="" &&catref !="")){
				 var replacebklistUrl = bklistUrl.replace(/invt,/g,"icat,");
				 	originalbklist = originalbklist.replace(/bklist=invt,/g,"bklist=icat,shop");
				 }
				if(bklistUrl.indexOf("icat,3")!=-1 && (icatref!="" &&catref !="")){
				    var replacebklistUrl = bklistUrl.replace(/icat,3/g,"icat,4");
					replacebklistUrl += "," +icatref;
					location.href = url +"&bklist=" + replacebklistUrl;
				 }	
				if(bklistUrl.indexOf("icat,4")!=-1 && (icatref!="" &&catref !="")){
				    var replacebklistUrl = bklistUrl.replace(/icat,4/g,"icat,5");
					replacebklistUrl += "," +icatref;
					location.href = url +"&bklist=" + replacebklistUrl;
				 }
				 if(bklistUrl.indexOf("icat,5")!=-1 && (icatref!="" &&catref !="")){
				    var replacebklistUrl = bklistUrl.replace(/icat,5/g,"icat,6");
					replacebklistUrl += "," +icatref;
					location.href = url +"&bklist=" + replacebklistUrl;
				 }
				 if(shopCatreeCheck=="shop"){
					var replacebklistUrl = "icat,4,shop," + catref + "," + icatref;
					location.href = url +"&bklist=" + replacebklistUrl;
				 }
				 if((icatref=="" &&catref =="")){
				 	var replacebklistUrl = bklistUrl;
					location.href = url +"&bklist=" + replacebklistUrl;	
				 }
		}
		
	}else{
		location.href = url;
	}
};

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

Venda.Ebiz.loadingPanel = function(waitDiv,widthVal,modalVal){
	var waitMsg=document.getElementById(waitDiv).innerHTML;
	    if (!Venda.Ebiz.wait) {
	        Venda.Ebiz.wait = 
	                new YAHOO.widget.Panel("wait",  
	                                                { width: widthVal, 
	                                                  fixedcenter: true, 
	                                                  close: false, 
	                                                  draggable: false, 
	                                                  zindex:4,
	                                                  modal: modalVal,
	                                                  visible: false
	                                                } 
	                                            ); 
	        Venda.Ebiz.wait.setBody(waitMsg);
	        Venda.Ebiz.wait.render(document.body);
	
	    } 
	    // Show the Panel
	    Venda.Ebiz.wait.show();

}
