// JavaScript Document
function deletePricingOption(row) {
	row.parentNode.removeChild(row)	
}
function addPricingOption() {
	var row = document.createElement('TR')
	var num = $('numrows')
	row.innerHTML = $('newrow').value.replace(/~NEW~/g,'NEW_' + num.value)
	$('pricingOptions').appendChild(row)
	num.value = parseFloat(num.value)+1
}

function discountSave(caller) {
	caller.form.className = 'sending'
}

function productSaveComplete(num, html) {
	if($('prd_' + num)) {
		var x = document.createElement('DIV')
		x.innerHTML = html
		$('prd_' + num).innerHTML = x.firstChild.innerHTML
		$('prd_' + num).className = x.firstChild.className
		currentLB.deactivate()
		initializeLB()
		new Effect.Highlight($('prd_' + num))
	} else {
		var x = document.createElement('DIV')
		x.style.display = 'none'
		x.innerHTML = html
		x.id = 'prd_' + num
		$('shopContents').appendChild(x.firstChild)
		currentLB.deactivate()
		initializeLB()
		Effect.ScrollTo(x.id)
		Effect.SlideDown(x.id)
		window.setTimeout(function() {new Effect.Highlight($('prd_' + num))},1000)
	}
	
}

function productSave(caller) {
	$('ajax').value = '1';
	caller.form.target = 'submitter';
	caller.form.className = 'sending'
}

function addToCart(id, caller) {

	caller.style.display = 'none'
	addlink	= document.createElement('DIV')
	caller.parentNode.insertBefore(addlink,caller.nextSibling)
	addlink.innerHTML = '<img src="/assets/images/loading.gif" alt="Adding to cart" style="float:none" />'
	var url = '/_shop/addtocart?id=' + id
	xmlHttp=GetXmlHttp()
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
			var num = xmlHttp.responseText
			addlink.innerHTML = '<img src="/assets/images/tick.png" alt="Added to cart" style="float:left" />Added to cart<br/><a href="/_shop/cart">View Cart</a>'
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)	
}

function calcShipping(sel) {
	var shp_num = sel.options[sel.selectedIndex].className.substr(3)
	shc_cost[shp_num]
	$('shippingcost').innerHTML = '$' + shc_cost[shp_num].moneyFormat()
	$('totalcost').innerHTML = '$' + (shc_cost[shp_num]+subtotal).moneyFormat()
}

var moving = false

function moveProduct(down, caller) {
	moveProductStart()
	while(caller.parentNode && caller.className.substring(0,7)!='product') {
		caller = caller.parentNode
	}
	if(down>0 && caller.nextSibling) {
		//Down
		if(caller.nextSibling.nextSibling) {
			caller.parentNode.insertBefore(caller,caller.nextSibling.nextSibling)
		} else {
			caller.parentNode.appendChild(caller)
		}
	} else if(caller.previousSibling) {
		//Up
		caller.parentNode.insertBefore(caller,caller.previousSibling)
		
	}
}

function moveProductStart() {
	if(!moving) {
		moving = true
		$('savechanges').style.display = ''
		alert('Once you have set the desired order of all products, please click the "Save Changes" button')
	}	
}

function saveProductOrder() {
	loadingOverlay(true)
	var order = ''
	var prod = $('shopContents').firstChild
	while (prod) {
		order+=prod.id.substr(4) + ","
		prod = prod.nextSibling	
	}
	var url = '/_shop/ajaxorder?o=' + order
	xmlHttp=GetXmlHttp()
	$('savechanges').style.display = 'none'
	moving = false
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
			loadingOverlay(false)
		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)	
}

function deleteProduct(prd_num, caller) {
	if(confirm('Are you sure you want to permanently delete this product?')) {
		loadingOverlay(true)
		var url = '/_shop/ajaxdel?p=' + prd_num
		xmlHttp=GetXmlHttp()
		xmlHttp.onreadystatechange=function() {
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
				while(caller.parentNode && caller.className.substring(0,7)!='product') {
					caller = caller.parentNode
				}
				loadingOverlay(false)
				Effect.SlideUp(caller)
				window.setTimeout(function() {
					caller.parentNode.removeChild(caller)
			   },1000)
			}
		}
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)	
	}
}
