function setProduct(prodCodeValue, prodNameValue, prodQtyValue, _loc){
    var prodCode = jQuery.trim(prodCodeValue.toString());
    var prodName = jQuery.trim(prodNameValue.toString());
	var prodQuantity = jQuery.trim(prodQtyValue.toString());
	var location 
   
	if (_loc === 'bags') {
		location = "/bags-quote-form.html?prodCode=" + jQuery.URLEncode(prodCode) + "&prodName=" + jQuery.URLEncode(prodName) + "&prodQty=" + prodQuantity;
	}
	else {
		location = "/promotional-quote-form.html?prodCode=" + jQuery.URLEncode(prodCode) + "&prodName=" + jQuery.URLEncode(prodName) + "&prodQty=" + prodQuantity;
	}
	window.location = location;
}

function executeOnClick(prodNameValue,tagCounter){
			var prodCodeSelected = jQuery.trim(jQuery("#prodCodeSelect-"+tagCounter).val());
			var prodQtySelected = jQuery.trim(jQuery("#quantitySelect-"+tagCounter).val());
			var error = '';
			if(prodQtySelected == ""){
				prodQtySelected = jQuery.trim(jQuery(".quantity-"+tagCounter).text());
			}else if(prodQtySelected == 0){
				error += "You did not select Product Quantity \n";
			}
			if(prodCodeSelected == ""){
				prodCodeSelected = jQuery.trim(jQuery(".prodCode-"+tagCounter).text());
			}else if(prodCodeSelected == 0){
				error += "You did not select Product Code \n";
			}
			if(error == ""){
				setProduct(prodCodeSelected,prodNameValue,prodQtySelected);
			}else{
				alert(error);
			}
}



window.Sbau = (function () {
	
	return {
		stripTags : function (_s) {
			// See: http://phpjs.org/functions/strip_tags:535
			return _s.replace(/(<\/?[\S][^>]*>)/gi, '');
		},
		
		isNumeric : function (_n) {
			return (/^[0-9,]+$/).test(_n);
		},
		
		byId : function (_id) {
			return document.getElementById(_id);
		}
	};
	
})();




jQuery(function ($) {
	var $divCon = $('div.bag-profile'),
		makeSelect = function (_str, _name) {
			var items = _str.split(';'),
				len = items.length,
				html = '';
			
			if (len) {
				html += '<select name="'+_name+'" style="width:100px;">';
				for (var i=0;i<len;i++) {
					html += '<option value="'+items[i]+'">'+items[i]+'</option>';
				}
				html += '</select>';
				return html;
			}
			else {
				return '<input type="hidden" name="'+_name+'" id="" value=""/>';
			} 			
				
		};
		
		
	if ($divCon.length > 0 && window.location.href.indexOf('/promotional/') > -1) {
		$divCon.each(function () {
			var $me = $(this),
				qty = $.trim($me.find('input.-qty').val()),
				color = $.trim($me.find('input.-color').val()),
				moq = $.trim($me.find('input.-moq').val());
			if (qty !== '') {
				$me.find('p.qty-con').append(makeSelect(qty, 'prodQty')).removeClass('hide');
			}
			
			
			if (color !== '') {
				color = '<span style="font-weight:normal;">' + color.replace(/;/g, ', ').replace(/,\s$/, '')+ '</span>';
				$me.find('p.color-con').append(color).removeClass('hide');
			}
			
			if (moq !== '') {
				$me.find('p.moq').append('<span style="font-weight:normal;">' + moq + '</span>').removeClass('hide');
			}
				
		});
		
		var $qtySelect = $('select[name=prodQty]');
		if ($qtySelect.length) {
			$qtySelect.bind('change', function () {
				if (this.value.toLowerCase().indexOf('other') > -1) {
					$(this).after('<input type="text" class="specify-qty" maxlength="11" style="width:55px;margin-left:5px;" name="'+this.name+'" />');
					this.name = '';
				}
				else {
					$(this).siblings('input.specify-qty').remove();					
					this.name = 'prodQty';
				}
			});
		}
	}
	
	// STOCKED ITEMS //
	if ($divCon.length > 0 && window.location.href.indexOf('/stockeditems_bags') > -1) {
		$('p.no-of-cartons-con').removeClass('hide');
		
		$('div.bag-profile a.sfont').live('click', function (e) {
			var $noOfCart = $(this).parents('div.bag-profile').find('input.no-of-cartons'),
				v = $.trim($noOfCart.val());
			if (v === '') {
				alert('Please specify the number of cartons');
				e.preventDefault();
				return false;
			}
			else {
				if (!/^\d+$/.test(v)) {
					alert('Please specify valid number of cartons');
					e.preventDefault();
					return false;
				}
				this.href = this.href+'&cartons='+v;
			}
		});
	}
});




function displayQuantityCode(quantityString,prodCodeString, counter){

	var prodCodeTrim = jQuery.trim(prodCodeString);
	var prodCode = prodCodeTrim.split(";");
	var quantityArr = jQuery.trim(quantityString).split(";");
	
	var strhtml = "";
	var quantityStr = "";
	
	
	//write product code
	//don't create dropdown if only 1 product code
	if (prodCodeTrim.indexOf(";") != -1) {
	
		strhtml += '<select id="prodCodeSelect-' + counter + '">';
		strhtml += '<option value="0">--Please Select--</option>';
	
		
		jQuery(prodCode).each(function(index){
			strhtml += "<option value='" + jQuery.trim(this) + "'>" + jQuery.trim(this) + "</option>";
		});
		
		strhtml += '</select>';
	
	}
	else{
		strhtml = prodCodeTrim;

		
	}
	jQuery(".prodCode-" + counter).append(strhtml);
	
	
	
	
	//write quantity
	if (quantityString.indexOf(";") != -1) {
	
		quantityStr += '<select id="quantitySelect-' + counter + '">';
		quantityStr += '<option value="0">--Please Select--</option>';
	
		
		jQuery(quantityArr).each(function(index){
			quantityStr += "<option value='" + jQuery.trim(this) + "'>" + jQuery.trim(this) + "</option>";
		});
		
		quantityStr += '</select>';
	
	}
	else{
		quantityStr = jQuery.trim(quantityString);

	}
	jQuery(".quantity-" + counter).append(quantityStr);	
	
	
	
}

function linkFAQ(faqid,pageapplicable){
	var base_url = "http://smartbagaustralia.smartwebmarketing.com.au";
	if(document.location == base_url + "/" + pageapplicable){
		if(typeof showFAQ == 'function')
			showFAQ(faqid);
	}
	else
		document.location = base_url + "/" + pageapplicable;
}




function onMouseOver(selector,imageOnMouseOver,imageOnMouseOut){
	jQuery(selector).hover(function(){
	jQuery(this).attr('src', imageOnMouseOver);
	}, 
	function(){
		jQuery(this).attr('src', imageOnMouseOut);
	});
}


jQuery.preloadImages = function() {
	var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
	for(var i=0; i < a.length; i++) {
		jQuery("<img>").attr("src", a[i]);
	}
}
	

