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

 /**
 * 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.splitEmailAdd = function(usemail){
	var stringlist = new Array();
	while (usemail.length > 30) {
	   stringlist.push( usemail.slice(0,30));
	   usemail=usemail.substr(30);
	}
	if (usemail.length) {
	  stringlist.push(usemail);
	}
	document.write(stringlist.join( '<br>' ));
}

Venda.Ebiz.dynamicContent= function(where,what) {
	// find out what tag the function is called from so the correct value is passed for url
	identifyTag = where.tagName;
	if (identifyTag == "A"){
		ajaxFunction(where+'&layout=noheaders&temp=subcategories',what);
		if (turnonToggle == 1){toggle(where);}
	} else if (identifyTag == "INPUT" || identifyTag == "SELECT") {
		ajaxFunction(where.value,what);
	}
}
Venda.Ebiz.dynamicWindow=function(where,name) {
	detailWin=dhtmlwindow.open('productdetailWin', 'iframe', where+'&layout=iframe&temp=productdetail_layer', name, 'width=650px,height=400px,resize=1,scrolling=1,center=1');
}

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 invtsell = document.getElementById("PriceDiv_"+elementId).innerHTML;
    var invtwas = document.getElementById("wasDiv_"+elementId).innerHTML;
    if(invtwas!=""){
           invtwas = parseFloat(invtwas.replace(/^\D+/, ''));
           invtsell = parseFloat(invtsell.replace(/^\D+/, ''));
		   var invtwasRep = invtwas.toFixed(2);
           var invtsellRep= invtsell.toFixed(2);
           var msg = Venda.Ebiz.Price.SavingPrice(invtwasRep, invtsellRep,elementId);
		   document.getElementById("PriceDiv_"+elementId).style.color = '#FF2F2F';

        if (document.getElementById(divSaveId)) {
                document.getElementById(divSaveId).innerHTML = msg;
				document.getElementById(divSaveLabel).innerHTML ="";
        }
    }
	else {
		document.getElementById("PriceDiv_"+elementId).style.color = '#000000';
	}
};

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 from &pound;"+invtsaving.toFixed(2);
						}
                } else {
                        msg = '';
                }
        }
        return msg;
};


//########## Style old version don't remove before update Templates or Brach update #############//
//order confirmation and order receipt page - split the email address on the RHN if too long
function splitEmailAdd(usemail) {
	var stringlist = new Array();
	while (usemail.length > 30) {
	   stringlist.push( usemail.slice(0,30));
	   usemail=usemail.substr(30);
	}
	if (usemail.length) {
	  stringlist.push(usemail);
	}
	document.write(stringlist.join( '<br>' ));
}

//dynamic nav script
turnonToggle = 1; //change this to 0 if you don't want to use toggling
// preload controls
// change path to the desired location
shown = new Image();
shown.src = "/venda-support/images/bulleton.gif";
hidden = new Image();
hidden.src = "/venda-support/images/bulletoff.gif";

function dynamicContent(where,what) {
	// find out what tag the function is called from so the correct value is passed for url
	identifyTag = where.tagName;
	if (identifyTag == "A"){
		ajaxFunction(where+'&layout=noheaders&temp=subcategories',what);
		if (turnonToggle == 1){toggle(where);}
	} else if (identifyTag == "INPUT" || identifyTag == "SELECT") {
		ajaxFunction(where.value,what);
	}
}

//dynamic popup window script
// images default to venda-support directory, change the path to ebiz resources directory if you need to source your own versions
mingif = '/venda-support/images/min.gif';
closegif = '/venda-support/images/close.gif';
restoregif = '/venda-support/images/restore.gif';
resizegif = '/venda-support/images/resize.gif';

function dynamicWindow(where,name) {
	detailWin=dhtmlwindow.open('productdetailWin', 'iframe', where+'&layout=iframe&temp=productdetail_layer', name, 'width=650px,height=400px,resize=1,scrolling=1,center=1');
}
// you may edit the parameters in the final argument above to alter the appearance of the popup window

//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.


$(document).ready(function(){
	$("table.wizrtable tbody").each(function (){
		$(this).find("tr:first td").addClass("selected");
	});
	$("div.deliveryrate ul").each(function (){
		$(this).find("li:first").addClass("selected");
	});	
	$(".f-payment_details div").each(function (index){		
		$(this).addClass("line"+index);
	});	
	$(".postbox label").each(function (index){		
		$(this).addClass("line"+index);
	});
	$(".emailbox label").each(function (index){		
		$(this).addClass("line"+index);
	});
	$("table.orderdetails tbody tr:first th").addClass("selected");
	$("table.orderdetails tbody tr:first td").addClass("selected");
	$("table.orderdetails tbody tr:last").addClass("last");		
	$("#orderreceipt #paymentdetails table tr:first").addClass("first");
	$("#orderreceipt #paymentdetails table tr:last").addClass("last");
	$("#redeempromocode p:last").addClass("last");
	$("#help h3:first").addClass("first");
	$("#giftwrap p:last").addClass("last");
	$(".Lcattree a:last").addClass("last");
	$("#orderreceipt #paymentdetails p:first").addClass("first");
	$("#orderreceipt #paymentdetails p:last").addClass("last");	
}); 


