/**
* @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;
};
Venda.Ebiz.doCheckOnce = function(oForm, oCheckbox, fieldsetName) {
		
	var fieldSets = oForm.getElementsByTagName("fieldset");
	
	for (var i = 0; i < fieldSets.length; i++) {
		if (fieldSets[i].className == fieldsetName) {
			var eachInputs = fieldSets[i].getElementsByTagName("input");
			for (var j = 0; j < eachInputs.length; j++) {
				if (eachInputs[j].type == "checkbox") {
					if (eachInputs[j].id != oCheckbox.id) {
						eachInputs[j].checked = false;
					}
				}
			}
		}
	}
};
Venda.Ebiz.doAddBasket = function (oForm) {
	// valid qty is allowed only number
	var qtyIsNan= isNaN(oForm.qty.value);
	var validQTY = true; 
	if (qtyIsNan) {
		alert("Please enter numbers only.");
		validQTY = false;
	} else {
		if (oForm.qty.value < 1) {
			alert("Please enter numbers more than 0.");
			validQTY = false;
		} 
	}
	if (validQTY) {
		if (typeof oForm.curcatref != "undefined" && typeof document.getElementById("currecordtype") != "undefined" && typeof document.getElementById("currecordref") != "undefined") {
			if (typeof oForm.invt != "undefined") {
					oForm.removeChild(oForm.invt);
			}
			var curRecordType = document.getElementById("currecordtype").innerHTML;
			if (curRecordType != "locayta") {
				var inputElement = document.createElement("input");
				inputElement.name = curRecordType;
				inputElement.type = "hidden";
				inputElement.value = document.getElementById("currecordref").innerHTML;
				oForm.appendChild(inputElement);
			} else {
				var searchHidden = document.createElement("input");
				searchHidden.name = "searchparameters";
				searchHidden.type = "hidden";
				var param = document.getElementById("cursearchparameters").innerHTML;
				searchHidden.value = param.replace(/\&amp\;/g,"\&");
				oForm.appendChild(searchHidden);
				
				oForm.curpage.value = "search";
				oForm.next.value = "locayta";
			}
			oForm.removeChild(oForm.curcatref);
		} 
		oForm.submit();
		return true;
	} else {
		oForm.qty.focus();
		return false;
	}
};
//Remove function from general.js to Ebiz.js
//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>' ));
}

// Variables for dynamic nav
turnonToggle = 1; //change this to 0 if you don't want to use toggling
// Bullet images
// 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";
//following function to be removed when RT#98865 in stable
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);
	}
};

//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.
function grabURL(currURL,urlParam) {
	//find out a value where is passed from current url
	var url = unescape(currURL);
	var spliter = '&';
	var sField = spliter+urlParam+'=';
	
	if (url.search(sField) == -1) {               
		sField = '?'+urlParam+'=';         
	}
	
	var urlArray = url.split(sField);
	if (urlArray[1]) {
		//get url param value
		var paramArray = urlArray[1].split(spliter);
		return(paramArray[0]);
	}
}
function validateQty(iform){
var iNum = /\D/;
var qty = iform.qty;

    if(iNum.test(qty.value)==true){
        alert("Please enter numbers only");
        return false;
    }
    else if(qty.value < "1"){
        alert("Please enter numbers more than 0."); 
    }
    else{
         iform.submit();
    }		
}	

// This function is used for defaulting the quantity to 1 or to make the quantity positive incase the user enters it wrong by mistake
function qtyValidate(thisBox,item){
  var regExp = /\D/g;
  var nonNum= thisBox.value.match(regExp);

    if(item > 0) {
        if ((nonNum != null) && (nonNum.length > 0) || (thisBox.value == "")) {
             thisBox.value = item;       
        }
    }else{
        if ((nonNum != null) && (nonNum.length > 0) || (thisBox.value == 0)) {
            thisBox.value = 1;        
        }
    }

}

// This function is used for  EqualHeight - search result page
Venda.Platform.EqualHeight.jq(document).ready(function(){
var classtoset = new Array ('#searchresults .prods ul p.invtdesc2','#searchresults .prods ul .details h2','#searchresults .prods ul li','#myacc-col-two .myaccpredictor ul li .image','#myacc-col-two .myaccpredictor ul li .details','#myacc-col-two .myaccpredictor ul li .details h3','#myacc-col-two .myaccpredictor ul li .details .sectionprice','#myacc-col-two .myaccpredictor ul li .details p.moreinfo','#productlist ul li .details h2','#productlist ul li p.invtdesc2','#productlist ul li');
 Venda.Platform.EqualHeight.init(classtoset);
});


//Jquery
$(document).ready(function(){
	$("#productlist ul li:last").addClass("last");	
	$("#productlist ul li:eq("+($("#productlist ul li").length-2)+")").addClass("bflast");
	$("#productlist ul li:even").addClass("eveli");	
	$("#searchresults .prods ul li:last").addClass("last");	
	$("#searchresults .prods ul li:eq("+($("#searchresults .prods ul li").length-2)+")").addClass("bflast");
	$("#searchresults .prods ul li:even").addClass("eveli");			
	
}); 
var cjComCookie;
function webGainPutCookie(){
	var wtcj = new CookieJar({expires: 3600 * 24 * 30,path: '/'});
	var wtPPD = Venda.Platform.getUrlParam(location.href, "affnetwork");
	if(wtPPD != ''){
	wtcj.put("affnetwork", wtPPD);
	}
}

function webGainGetCookie(){
	var wtcj = new CookieJar({expires: 3600 * 24 * 30,path: '/'});
	if (wtcj.get("affnetwork")){
		cjComCookie = wtcj.get("affnetwork");
	}
}
addEvent(window, 'load', webGainPutCookie, false);

