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

 /**
 * The following global variables (directly below) are NEEDED to support legacy javascript functions - DO NOT REMOVE! see RT#113376 for more details.
 * 1. shown
 * 2. hidden
 */

var shown = new Image();
shown.src = "/venda-support/images/bulleton.gif";
var hidden = new Image();
hidden.src = "/venda-support/images/bulletoff.gif";

 /**
 * 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);
	}
	if(document.getElementById(dispElem)){
		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;
};

var J = jQuery.noConflict();
Venda.Ebiz.personalised = function(){};
J(document).ready(function(){
		Venda.Ebiz.personalised.checkCharsLength = function(ele){
					if(Venda.Ebiz.personalised.chars.bodyoixtinput.value!=""){
						if(!Venda.Ebiz.personalised.chars.allowedChars.test(Venda.Ebiz.personalised.chars.bodyoixtinput.value)){
						alert(Venda.Ebiz.personalised.chars.notAllowedCharsMessage);
						return false;
						}
					}
					if(Venda.Ebiz.personalised.chars.baseoixtinput.value!=""){
						if(!Venda.Ebiz.personalised.chars.allowedChars.test(Venda.Ebiz.personalised.chars.baseoixtinput.value)){
							alert(Venda.Ebiz.personalised.chars.notAllowedCharsMessage);
							return false;
						}
					}
					Venda.Ebiz.personalised.checkCharsLength.oixtValue={
						bodyoixt:Venda.Ebiz.personalised.chars.bodyoixtinput.value.split(' ').join(''),
						baseoixt:Venda.Ebiz.personalised.chars.baseoixtinput.value.split(' ').join('')
					};
					if(Venda.Ebiz.personalised.checkCharsLength.oixtValue.bodyoixt.length > Venda.Ebiz.personalised.chars.maxbodychars){
						Venda.Ebiz.personalised.checkCharsLength.oixtValue.bodyoixt=Venda.Ebiz.personalised.checkCharsLength.oixtValue.bodyoixt.substr(0,Venda.Ebiz.personalised.chars.maxbodychars);
		Venda.Ebiz.personalised.chars.bodyoixtinput.value=Venda.Ebiz.personalised.chars.bodyoixtinput.value.substr(0,Venda.Ebiz.personalised.chars.maxbodychars);
					}
					if(Venda.Ebiz.personalised.checkCharsLength.oixtValue.baseoixt.length > Venda.Ebiz.personalised.chars.maxbasechars){
						Venda.Ebiz.personalised.checkCharsLength.oixtValue.baseoixt=Venda.Ebiz.personalised.checkCharsLength.oixtValue.baseoixt.substr(0,Venda.Ebiz.personalised.chars.maxbasechars);
						Venda.Ebiz.personalised.chars.baseoixtinput.value=Venda.Ebiz.personalised.chars.baseoixtinput.value.substr(0,Venda.Ebiz.personalised.chars.maxbasechars);
					}
						Venda.Ebiz.personalised.checkCharsLength.totText = Venda.Ebiz.personalised.checkCharsLength.oixtValue.bodyoixt+Venda.Ebiz.personalised.checkCharsLength.oixtValue.baseoixt;
						if(Venda.Ebiz.personalised.checkCharsLength.totText.length > 0){J("select[name="+Venda.Ebiz.personalised.chars.attrElementname+"]" + " option[value=" + Venda.Ebiz.personalised.checkCharsLength.totText.length + "]").attr("selected","selected");
							Venda.Ebiz.personalised.attrVal = Venda.Ebiz.personalised.checkCharsLength.totText.length;
						}else{
							J("select[name="+Venda.Ebiz.personalised.chars.attrElementname+"]" + " option[value="+Venda.Ebiz.personalised.chars.defaultcharoption+"]").attr("selected","selected");
							Venda.Ebiz.personalised.attrVal = Venda.Ebiz.personalised.chars.defaultcharoption;
						}
						if(ele!=undefined){
							J(Venda.Ebiz.personalised.chars.personalisedbuttonDiv).show("slow");
							J(Venda.Ebiz.personalised.chars.personalisedcalcLink).hide("slow");
							J([Venda.Ebiz.personalised.chars.bodyoixtinput,Venda.Ebiz.personalised.chars.baseoixtinput]).attr("readonly",true);
							J([Venda.Ebiz.personalised.chars.bodyoixtinput,Venda.Ebiz.personalised.chars.baseoixtinput]).addClass("oixtreadonly");
						}
					if(product.attributeValues[Venda.Ebiz.personalised.attrVal]){
							Venda.Ebiz.personalised.chars.personalisedSpan.innerHTML = Venda.Ebiz.personalised.chars.currencySymbol+((product.attributeValues[Venda.Ebiz.personalised.attrVal].data["atrsell"] - Venda.Ebiz.personalised.chars.defaultPrice).toFixed(2));
							Venda.Ebiz.personalised.chars.personalisedTotalPrice.innerHTML = Venda.Ebiz.personalised.chars.currencySymbol + parseFloat(product.attributeValues[Venda.Ebiz.personalised.attrVal].data["atrsell"]).toFixed(2);
							
 	         }
		};
		Venda.Ebiz.personalised.changepersonalised = function(){
			J(Venda.Ebiz.personalised.chars.personalisedbuttonDiv).hide("slow");
			J(Venda.Ebiz.personalised.chars.personalisedcalcLink).show("slow");
			J([Venda.Ebiz.personalised.chars.bodyoixtinput,Venda.Ebiz.personalised.chars.baseoixtinput]).removeAttr("readonly");
			J([Venda.Ebiz.personalised.chars.bodyoixtinput,Venda.Ebiz.personalised.chars.baseoixtinput]).removeClass("oixtreadonly");
		};
		
		/*Function for adding the text into input text and swap textbox when click calculate button*/
		Venda.Ebiz.swappersonalisedText = function(){
			J("input#fakeinputbody").css("display","none");
			J("input#fakeinputbase").css("display","none");
			J(Venda.Ebiz.personalised.chars.bodyoixtinput).css("display","inline");
			J(Venda.Ebiz.personalised.chars.baseoixtinput).css("display","inline");
		};
		Venda.Ebiz.addinstruction = function(){
		    //clear texts when the product is added in firefox
		    J([Venda.Ebiz.personalised.chars.bodyoixtinput,Venda.Ebiz.personalised.chars.baseoixtinput]).attr("value","");
			//added instruction texts into input boxes
			J("input#fakeinputbody").attr("value","Maximum of "+Venda.Ebiz.personalised.chars.maxbodychars+" letters including spaces");
			J("input#fakeinputbase").attr("value","Maximum of "+Venda.Ebiz.personalised.chars.maxbasechars+" letters including spaces");
			//add listener on focus event to instruction input boxes
			J("input#fakeinputbody").focus(function(){
				J(this).css("display","none");
				J(Venda.Ebiz.personalised.chars.bodyoixtinput).css("display","inline");
				J(Venda.Ebiz.personalised.chars.bodyoixtinput).focus();
			});
			J("input#fakeinputbase").focus(function(){
				J(this).css("display","none");
				J(Venda.Ebiz.personalised.chars.baseoixtinput).css("display","inline");
				J(Venda.Ebiz.personalised.chars.baseoixtinput).focus();
			});
			//if the variable of show default instruction is true then display the instruction boxes when it is out of focus in case the oixt boxes is blank
			if(Venda.Ebiz.personalised.chars.showdefaultinstruction==true){
				J(Venda.Ebiz.personalised.chars.baseoixtinput).blur(function(){
					if(J(Venda.Ebiz.personalised.chars.baseoixtinput).attr("readonly")!=true){
						if(Venda.Ebiz.personalised.chars.baseoixtinput.value==""){
							J(Venda.Ebiz.personalised.chars.baseoixtinput).css("display","none");
							J("input#fakeinputbase").css("display","inline");
						}
					}
				});
				J(Venda.Ebiz.personalised.chars.bodyoixtinput).blur(function(){
					if(J(Venda.Ebiz.personalised.chars.bodyoixtinput).attr("readonly")!=true){
						if(Venda.Ebiz.personalised.chars.bodyoixtinput.value==""){
							J(Venda.Ebiz.personalised.chars.bodyoixtinput).css("display","none");
							J("input#fakeinputbody").css("display","inline");
						}
					}
				});
			}
		};
		Venda.Ebiz.swapinstructionText = function(){
				if(Venda.Ebiz.personalised.chars.showdefaultinstruction==true){
					if(Venda.Ebiz.personalised.chars.baseoixtinput.value==""){
						J(Venda.Ebiz.personalised.chars.baseoixtinput).css("display","none");
						J("input#fakeinputbase").css("display","inline");
					}
					if(Venda.Ebiz.personalised.chars.bodyoixtinput.value==""){
						J(Venda.Ebiz.personalised.chars.bodyoixtinput).css("display","none");
						J("input#fakeinputbody").css("display","inline");
					}
				}
		};	
});

 /**
 * For Expand Collapse Hidden Div on Home Page
 */
 J(document).ready(function(){
	J("#favopen").click(function(E){
		J("#showorhide").show("slow");
		J("#favclose").show("slow");
		J("#favopen").hide("slow");
	});
	J("#favclose").click(function(E){
		J("#showorhide").hide("slow");
		J("#favclose").hide("slow");
		J("#favopen").show("slow");
	});
 });

	 /* Find and add class last to the last element */
  	 /* This is made in order to hilight the last element of crumtrail. It might be reused in other purpose but the same way of use. */
  	 /* findElement: The element that jQuery will go through */
  	 /* className: The class name that would like to add as last element */
  	 /* countwhen: jQuery will add class name when this return more than this amount */
  	 Venda.Ebiz.addClassLast = function(findElement,className,countwhen){
  	         var elementCount = jQuery(findElement).length;
  	         if ((countwhen > 0) && (elementCount > 0) && (countwhen < elementCount)){
  	                 jQuery (findElement+":last").addClass(className);
  	         }
  	 };
	 
Venda.Ebiz.addClassFirstLastToTable = function() {
   	// add class first/last to every tables
	J("table thead").ready(function() {
		J("table thead").each(function(index, domElement) {
			var trLength = J(this).find("tr").length;

			J(this).find("tr").each(function(index) {
				if (index == 0) { J(this).addClass("first"); }
				if (index == (trLength-1) &&(index !=0)) { J(this).addClass("last"); }

			});
		});
	}); 
	J("table tbody").ready(function() {
		J("table tbody").each(function(index, domElement) {
			var trLength = J(this).find("tr").length;

			J(this).find("tr").each(function(index) {
				if (index == 0) { J(this).addClass("first"); }
				if (index == (trLength-1) &&(index !=0)) { J(this).addClass("last"); }

			});
		});
	});   	
};

Venda.Ebiz.addClass = function(selector, className) {	
   J(selector).ready(function() {J(selector).addClass(className)});
};

J(document).ready(Venda.Ebiz.addClassFirstLastToTable);

J(document).ready(function() {
	Venda.Ebiz.addClass(".ordertable tr:odd","odd");
});
J(document).ready(function() {
	Venda.Ebiz.addClass(".ordertable tr:even","even");
});
J(document).ready(function() {
	Venda.Ebiz.addClass("ul#minicart_products li:odd","odd");
});
J(document).ready(function() {
	Venda.Ebiz.addClass("ul#minicart_products li:even","even");
});
J(document).ready(function() {
	Venda.Ebiz.addClass(".ohSumDetail tr:odd","odd");
});
J(document).ready(function() {
	Venda.Ebiz.addClass(".ohSumDetail tr:even","even");
});
J(document).ready(function() {
	Venda.Ebiz.addClass(".orderitems tr:odd","odd");
});
J(document).ready(function() {
	Venda.Ebiz.addClass(".orderitems tr:even","even");
});
J(document).ready(function() {
	Venda.Ebiz.addClass("#paymentdetails tr:odd","odd");
});
J(document).ready(function() {
	Venda.Ebiz.addClass("#paymentdetails tr:even","even");
});
J(document).ready(function() {
	Venda.Ebiz.addClass("#ordersummary tr:odd","odd");
});
J(document).ready(function() {
	Venda.Ebiz.addClass("#ordersummary tr:even","even");
});
