String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };

function $(elm) { return document.getElementById(elm); }

function $C(elm, tag, klass) {
	var children = elm.getElementsByTagName(tag);
	var list = [];
	for (var i = 0; i < children.length; i++) {
		if (children[i].className == klass) {
			list.push(children[i]);
		}
	}
	if (list.length == 0) return null;
	else if (list.length == 1) return list[0];
	else return list;
}

var Cart = {
	removeCartItem : function(id) {
		$('cart_item[' + id + ']').value = 0;
		$('frmCart').submit();
	}
}

var Product = {

	newCategoryHTML: '',
	newShippingHTML: '',
	newOptionHTML: '', //set in _form

	addImage: function(MAX) {
		var found = 0;
		for (var i = 0; i < MAX; i++) {
			var elm = $('image' + i + 'Container');
			if (elm.style.display == 'none') {
				elm.style.display = 'block';
				var iframe = elm.getElementsByTagName("iframe")[0]
				iframe.style.height = '130px';
				found = i;
				break;
			}
		}
		if (i == MAX-1 && MAX == 5) {
			$("addImageLink").style.display = 'none';
		} else if (i == MAX) {
			$('imageLimitError').style.display = 'block';
		}
	},

	removeImage: function(elm, i) {
		field = elm.getElementsByTagName("input")
		field[0].value = 0;
		iframe = elm.getElementsByTagName("iframe")[0]
		iframe.src = '/Apps/browserFramePreview.jsp?&parentForm=webstore%5FaddProduct&multi=0&type=image&name=image' + i;
		elm.style.display = 'none';
		$("addImageLink").style.display = '';
	},

	addCategory: function() {
		var list = $('categoriesList'),
			li = document.createElement('li');
		li.innerHTML = Product.newCategoryHTML;
		list.appendChild(li);
		inputs = li.getElementsByTagName('input')[0].checked = true;
		$C(li, 'input', 'categoryItem').focus();
		$C(li, 'input', 'newFromProductChecked').value = 1;
	},

	markCategoryForDestroy : function(option, id) {
		shouldDestroy = $('categoryShouldDestroy_' + id);
		shouldDestroy.value = 1;
		Product.removeListItem(option, true);
	},

	markCategoryChecked : function(checkbox) {
		elm = $C(checkbox.parentNode, 'input', 'newFromProductChecked');
		if (elm) {
			elm.value = checkbox.checked ? 1: 0;
		}
	},

	removeListItem : function(option, hide) {
		li = option.parentNode;
		if (hide)
			li.style.display = 'none'
		else
			li.parentNode.removeChild(li);
	},
	
	addOption : function(noFocus, defaultTitle) {
		var list = $('productOptions'),
			li = document.createElement('li');
		li.innerHTML = Product.newOptionHTML;
		list.appendChild(li);
		nameBox = li.getElementsByTagName('input')[0];
		if (defaultTitle) nameBox.value = "Default";
		if (!noFocus) nameBox.focus();
	},

	addShipping : function(noFocus) {
		var list = $('productShippings'),
			li = document.createElement('li');
		li.innerHTML = Product.newShippingHTML;
		list.appendChild(li);
		if (!noFocus) li.getElementsByTagName('input')[0].focus();
	},

	markOptionForDestroy : function(option, id) {
		shouldDestroy = $('optionShouldDestroy_' + id);
		shouldDestroy.value = 1;
		Product.removeListItem(option, true);
	},
		
	markShippingForDestroy : function(option, id) {
		shouldDestroy = $('shippingShouldDestroy_' + id);
		shouldDestroy.value = 1;
		Product.removeListItem(option, true);
	},
		
	goWithId : function(url) {
		W.redirect(url + '/' + Product.cbId);
	},
	
	edit : function(url) {
		Product.setCbId();
		Product.goWithId(url);
	},

	remove : function(url) {
		Product.setCbId();
		if (confirm('Are you sure you want permanently remove this product from your store?')) {
			Product.goWithId(url);
		}
	},

	setCbId : function() {
		Product.cbId = fwGetContextBar('productContextBar').getID();
	},

	showFullImage : function(link, title) {
		new Popover($('img', link).attr('src').replace('size=large', 'size=full'), {heading: title, type: 'IMG', width: 200, height: 200}).show();
	},

	validate : function(frm) {
		var price = $('product_price');
		if (/(,\d\d)$/.test(price.value)) {
			alert('Do not use commas in your price, use a dot (.) instead.');
			price.focus();
			return false
		}
		var inStockList = $C($('productOptions'), 'input', 'productOptionInStock') || [];
		for (var i = 0; i < inStockList.length; i++) {
			var val = inStockList[i].value;
			if (val.trim() != '' && !/^\d+$/.test(val)) {
				alert('The inventory amount(s) you specified must be a number.');
				return false;
			}
		}
		selectList = $('productShippings').getElementsByTagName('select');
		selects = [];
		for (var i = 0; i < selectList.length; i++) {
			if (selectList[i].parentNode.style.display != 'none')
				selects.push(selectList[i]);
		}
		for (var i = 0; i < selects.length; i++) {
			var value = selects[i].value;
			var found = false;
			for (var j = i+1; j < selects.length; j++) {
				if (value == selects[j].value) {
					found = true;
					break;
				}
			}
			if (found) {
				alert('You cannot have the same country for more than one shipping option.');
				return false;
			}
		}
		return true;
	},
	
	toggleInstructions : function() {
		if ( $('special_instructions_check').checked ) {
			$('special_instructions_div').style.display='block';
			if ($('product[special_instructions]').value == null || $('product[special_instructions]').value == '') {
				$('product_special_instructions_req').checked = true;
			}
		} else {
			$('special_instructions_div').style.display='none';
			$('product[special_instructions]').value='';
			$('product_special_instructions_req').checked=false;
		}
	}
}

var Settings = {
	toggleGoogle: function() {
		options_s = $("googleCheckoutSettings").style
		if (options_s.display == 'block')
			options_s.display = 'none'
		else
			options_s.display = 'block'
	}
}

var ShowProduct = {
	changeImage: function(newImg) {
		document.getElementById('largeImage').src = 'http://thumbs.freewebs.com/Members/viewThumb.jsp?fileID='+newImg+'&size=large'
	}
}
