/**
 * © 2005 Propel Development, LLC - All Rights Reserved.
 * 
 * www.propeldev.com
 * 
 * Contact us for licensing info.
 * 
 */

N = (document.all) ? 0 : 1;
var ob = false;
var setNotRan=true;

var droppingArea;
var currentYposition;
var productSKU;
var checkboxID;
var mouseHasMoved = false;
var mouseIsDown = false;

var cart_open_height = 86;
var cart_open_height_IE_Win = -5;
var cart_open_margin_IE_Win = '0 0 85px 0';

var cart_closed_height = 20;
var cart_closed_height_IE_Win = -71;
var cart_closed_margin_IE_Win = '0 0 24px 0';	//on windows ie, this is so the scrollbar will position properly

var is_IE_Mac = false;
var is_IE_Win = false;

if(navigator.appName.indexOf("Microsoft") != -1 && navigator.platform.indexOf("Mac") != -1)
	is_IE_Mac = true;

if(navigator.appName.indexOf("Microsoft") != -1 && !is_IE_Mac)
	is_IE_Win = true;


function MD(sku, currentCheckboxID, iconType)
{
	
	mouseHasMoved = false;
	mouseIsDown = true;
	productSKU = sku;
	checkboxID = currentCheckboxID;
	if(iconType == '') // just to make sure there is an icon
		document.s.src = '/images_icons/icon_font.gif';
	else
		document.s.src = '/images_icons/icon_'+iconType+'.gif';
	
	if (N)
	{
		ob = document.getElementById('s').style;
		return false;
	}
	else
	{
		ob = document.s.parentElement.style;		
		X=event.offsetX;
		Y=event.offsetY;
		setNotRan=true;
  }
  
  return false;
  
}
function MM(e)
{
	ob.visibility='visible'; //the layer is now visible
	
	if (ob)
	{
		mouseHasMoved = true;
		mouseXpositionOffset = 45; //these values will center the mouse on the image
		mouseYpositionOffset = 18;
	
		if (N)
		{
			ob.left = (e.pageX) - mouseXpositionOffset;
			ob.top = (e.pageY) - mouseYpositionOffset;
			droppingArea = document.getElementById("cart").offsetTop + document.body.scrollTop;
			currentYposition = e.pageY;
		} else {
			ob.pixelLeft = event.clientX - mouseXpositionOffset;
			ob.pixelTop = event.clientY - mouseYpositionOffset + document.body.scrollTop;
			droppingArea = document.getElementById("cart").offsetTop;	
			currentYposition = event.clientY;
		}
			  
		if(currentYposition > droppingArea)
		{
			document.getElementById('cartDisplayArea').style.backgroundColor='#CCCCCC';
			cart_open_preference();  //make sure the cart is open
		} else {
			document.getElementById('cartDisplayArea').style.backgroundColor='#FFFFFF';
			cart_restore_preference();
		}
		
		return false;
	  
	}
}


function MU()
{
	mouseIsDown = false;
	
	if(mouseHasMoved)
	{
		if(N)
		{
			document.getElementById('cartDisplayArea').style.backgroundColor='#FFFFFF';
			ob.left=10;
			ob.top=10;
			ob.visibility='hidden';
			ob = null;
			ob = false;
		}
		else
		{
			document.getElementById('cartDisplayArea').style.backgroundColor='#FFFFFF';
			ob.pixelLeft=10;
			ob.pixelTop=10;
			//setTimeout("remove_visibility()", 500);
			ob.visibility='hidden';
			ob = null;
			ob = false;
		}
		
		cart_restore_preference();
		
		if(currentYposition > droppingArea)
		{			
			d=document;
		
			//verify checkbox exist
			if(d.getElementById('productCheckbox'+checkboxID)!=null)
			{
				x=d.getElementById('productCheckbox'+checkboxID);
				x.checked = true;
			}			
			add_to_cart(productSKU);
		}
	}
	else
	{
		ob = null;
		ob = false;
	}

	mouseHasMoved = false;
	
	document.s.src = '/images_icons/spacer.gif';

//	return false;

}

if (N)
{
	document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
}
document.onmousemove = MM;
document.onmouseup = MU;


function add_to_cart(currentSKU)
{
	var qty = 1;
	if(document.getElementById('pQuantity'+currentSKU)!=null)
		qty = document.getElementById('pQuantity'+currentSKU).value;

	document.cartDrop.src = '/addtocart/?sku='+escape(currentSKU)+'&qty='+qty+'&rid='+Math.floor((Math.random()*1000001))+'&baseblank=0';
	if(document.getElementById("addtionalProductsMap").innerHTML == '') //to fix a safari issue that if you added 2 items quickly, it would not write properly
		document.getElementById("addtionalProductsMap").innerHTML = '<map name="cartDropMap"><area shape="rect" coords="537,8,756,61" href="http://www.creativedelights.com/searchprod.php?showrelated=1" class="cursorLink"></map>';
}

function remove_from_cart(currentSKU)
{
	document.cartDrop.src = '/removefromcart/?sku='+escape(currentSKU)+'&rid='+Math.floor((Math.random()*1000001))+'&baseblank=0';
	document.getElementById("addtionalProductsMap").innerHTML = '';
}

function checkbox_event(currentSKU, currentCheckboxID, updateCartTotals)
{

	d=document;
	x=d.getElementById('productCheckbox'+currentCheckboxID);
	if(x.checked)
	{
		add_to_cart(currentSKU);
	}
	else
	{
		remove_from_cart(currentSKU);
	}
	if(updateCartTotals == 5) //this is only passe in viewcart in the checkbox
	{
		document.cart_totals.src = '/viewcarttotals/?rid='+Math.floor((Math.random()*1000001));
	}
}

function toggle_cart()
{

	if(getCookie('cartPref')==0 || getCookie('cartPref')==null)
	{
		if(is_IE_Win)
		{
			document.getElementById("cart").style.bottom = cart_closed_height_IE_Win;
			document.body.style.margin = cart_closed_margin_IE_Win;
		}
		else
		{
			document.getElementById("cart").style.height = cart_closed_height;
		}
		document.cartStatus.src = '/images_global/cart_open.gif';
		setCookie('cartPref', 1);
	}
	else
	{
		if(is_IE_Win)
		{
			document.getElementById("cart").style.bottom = cart_open_height_IE_Win;
			document.body.style.margin = cart_open_margin_IE_Win;
		}
		else
		{
			document.getElementById("cart").style.height = cart_open_height;
		}
		document.cartStatus.src = '/images_global/cart_close.gif';
		setCookie('cartPref', 0);
	}

	//return true;
}

function cart_open_preference()
{
	if(is_IE_Win)
	{
		document.getElementById("cart").style.bottom = cart_open_height_IE_Win;
		document.body.style.margin = cart_open_margin_IE_Win;
	}
	else
	{
		document.getElementById("cart").style.height = cart_open_height;
	}
}

function cart_restore_preference()
{
	if(getCookie('cartPref')==1)
	{
		if(is_IE_Win)
		{
			document.getElementById("cart").style.bottom = cart_closed_height_IE_Win;
			document.body.style.margin = cart_closed_margin_IE_Win;
		}
		else
		{
			document.getElementById("cart").style.height = cart_closed_height;
		}
	}
	else
	{
		if(is_IE_Win)
		{
			document.getElementById("cart").style.bottom = cart_open_height_IE_Win;
			document.body.style.margin = cart_open_margin_IE_Win;
		}
		else
		{
			document.getElementById("cart").style.height = cart_open_height;
		}
	}
}

function custom_cd_selection()
{
	var custom_cd = '';
	
	if(document.getElementById('custom_cd').checked)
		custom_cd = 1;
	document.cart_totals.src = '/additionalitems/?cop='+custom_cd+'&rid='+Math.floor((Math.random()*1000001));
	
	setTimeout("dragndrop_refresh()", 500);
}

function additional_items_selection()
{
	var custom_cd = '';
	var additional_item = '';
		
	if(document.getElementById('custom_cd').checked)
		custom_cd = 1;
	
	if(document.getElementById('additional_item')!=null)
	{
		if(document.getElementById('additional_item').checked)
			additional_item = 1;
	}
		
	document.cart_totals.src = '/additionalitems/?cop='+custom_cd+'&add1='+additional_item+'&rid='+Math.floor((Math.random()*1000001));
	
	setTimeout("dragndrop_refresh()", 500);
}

function dragndrop_refresh()
{
	document.cartDrop.src = '/dragndropcart/?'+'&rid='+Math.floor((Math.random()*1000001));
}

function update_orderline_item_total(currentSKU, orderlineID)
{

	var orderlineItemTotal;
	var priceEach;
	var priceAdditional;
	var pQuantity = 1;
	 
	if(orderlineID > 0 && document.getElementById('multiunit'+orderlineID).value == 1)
	{
		
		if(document.getElementById('productCheckbox'+currentSKU).checked)
		{
			document.getElementById('unitqty'+orderlineID).value = parseInt(document.getElementById('unitqty'+orderlineID).value) + 1;
		}
		else
		{
			document.getElementById('unitqty'+orderlineID).value = parseInt(document.getElementById('unitqty'+orderlineID).value) - 1;
		}
		
		var SKU = currentSKU.split(".");
		var currentSKU = SKU[0];
		
		priceEach = parseInt(document.getElementById("unitpricefirst"+orderlineID).value);
		priceAdditional = parseInt(document.getElementById("unitpriceadd"+orderlineID).value);
		
		if(parseInt(document.getElementById("unitqty"+orderlineID).value) > 0)
		{
			pQuantity = parseInt(document.getElementById("unitqty"+orderlineID).value) - 1;
			orderlineItemTotal = pQuantity  * priceAdditional + priceEach;
		}
		else 
		{
			orderlineItemTotal = 0;
		}
		
		document.getElementById('orderline_item_total'+currentSKU).innerHTML = '$'+orderlineItemTotal.toFixed(2);
		document.getElementById('orderline_item_price'+currentSKU).innerHTML = '$'+orderlineItemTotal.toFixed(2);
		
	}
	else 
	{
		if(document.getElementById('productCheckbox'+currentSKU).checked)
		{
			if(document.getElementById('pQuantity'+currentSKU).value > 1)
				pQuantity = parseInt(document.getElementById('pQuantity'+currentSKU).value);
			else
				document.getElementById('pQuantity'+currentSKU).value = 1;
		
			priceEach = document.getElementById("priceEach"+currentSKU).value;
			orderlineItemTotal = pQuantity * priceEach;
			document.getElementById('orderline_item_total'+currentSKU).innerHTML = '$'+orderlineItemTotal.toFixed(2);
		
			if(orderlineID > 0)
			{
				document.cart_totals.src = '/viewcarttotals/?qty='+pQuantity+'&olid='+orderlineID+'&rid='+Math.floor((Math.random()*1000001));
			}
		}
		else
		{
			orderlineItemTotal = 0;
			document.getElementById('orderline_item_total'+currentSKU).innerHTML = '$'+orderlineItemTotal.toFixed(2);
		}
	}
}
function multi_sample_click(url, sku)
{
	var skusplit = document.getElementById(sku+'unitid').value.split('.'); 
	var unit = skusplit[1]; 
	location.href=url+'searchdetail.php?psku='+sku+'&uid='+unit;
}
