﻿<!-- THC Script Library -->

// Info windows

var window1;	// Static globals for ship window and gift window
var window2;

function openAndReturnInfoWindow(infoWindow,url,name,size)
{
	if (navigator.userAgent.indexOf('Chrome/') > 0) {
		if (infoWindow) {
			infoWindow.close();
			infoWindow = null;
		}
	}	
	
	if (navigator.userAgent.indexOf('Opera/') >= 0) {
		if (infoWindow) {
			infoWindow.close();
			infoWindow = null;
		}
	}	
	
	if (infoWindow)
	{
		if (infoWindow.closed == false)
		{
			infoWindow.focus();
			return (infoWindow);
		}
	}
  
	infoWindow = window.open(url,name,"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,"+size);
  
  	if (infoWindow)
  	{
		if (infoWindow.opener == null)
			infoWindow.opener = window;
		
		infoWindow.focus();
	}

	return (infoWindow);
}

function openInfoWindow(infoWindow,url,name,size)
{
	openAndReturnInfoWindow(infoWindow,url,name,size);
}

function openInfoWindowBySize(infoWindow,url,name,height,width)
{
	var IE = /*@cc_on!@*/false;
	
	if (IE)
	{
		height += 15;
		width  += 25;
	}
	
	if (navigator.userAgent.indexOf('Chrome/') > 0) {
		height += 35;
	}	

	var size = "width="+String(width)+",height="+String(height)
	
	return (openAndReturnInfoWindow(infoWindow,url,name,size));
}

function openPhotoWindow(url,name,height,width)
{
	if (window.screen != null)
	{
		if (window.screen.availHeight != null)
		{
			if (window.screen.availHeight < height + 80)
				height = window.screen.availHeight - 80;
		}	

		if (window.screen.availWidth != null)
		{
			if (window.screen.availWidth < width)
				width = window.screen.availWidth;
		}
	}
	
	var IE = /*@cc_on!@*/false;
	
	if (IE)
	{
		height += 15;
		width  += 25;
	}
	
	if (navigator.userAgent.indexOf('Chrome/') > 0) {
		height += 35;
	}	

	var photoWindow = window.open(url,name,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width="+String(width)+",height="+String(height));
  
  	if (photoWindow)
  	{
		if (photoWindow.opener == null)
			photoWindow.opener = window;
			
		photoWindow.focus();
	}
}

function openHarpPhotoWindow(url,name)
{
	openPhotoWindow(url,name,910,650);
}

function CloseGracefully()
{
	if (window.opener != null)
		window.opener.focus();
		
	window.close();
}

// Routines for showing and hiding objects dynamically
// Plus routines to manage the mini-cart

var dom = document.getElementById?1:0;
var ie  = document.all?1:0;
var ns  = document.layers?1:0;

function getObject(itsID)
{
	var i,found,doc;
		
	doc=document;
	
	if	(!(found=doc[itsID])&&ie)
	{
		found=doc.all[itsID];
	}
	
	for (i=0;!found&&i<doc.forms.length;i++)
	{
		found=doc.forms[i][itsID];
	}
	
	for (i=0;!found&&ns&&i<doc.layers.length;i++)
	{
		found=find(itsID,doc.layers[i].document);
	}
	
	if (!found&&dom)
	{
		found=document.getElementById(itsID);
	}
	
	return found;
}

function showObject(obj)
{
	if (!obj)
		return;
	
	if (dom||ie)
	{
		obj.style.visibility = "visible";
		obj.style.display    = "";
	}
	
	else if (ns)
	{
		if (document.layers[obj])
		{
			obj.visibility = "show";
			obj.display    = "";
		}
	}
}

function showObjectID(itsID)
{
	var obj=getObject(itsID);
	
	showObject(obj);
}

function hideObject(obj)
{
	if(!obj)
		return;
	
	if(dom||ie)
	{
		obj.style.visibility = "hidden";
		obj.style.display    = "none";
	}
	
	else if(ns)
	{
		if(document.layers[obj])
		{
			obj.visibility = "hide";
			obj.display    = "none";
		}
	}
}

function hideObjectID(itsID)
{
	var obj=getObject(itsID);
	
	hideObject(obj);
}

function showHideObjectID(showId,hideId)
{
	showObjectID(showId);
	hideObjectID(hideId);
}

// Mini-cart

function getCookie(c_name)
{
	if (!document) return "";
	
	if (!document.cookie) return "";

	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		
		if (c_start!=-1)
		{
			c_start = c_start + c_name.length + 1;
			c_end   = document.cookie.indexOf(";",c_start);
			
			if (c_end==-1) c_end=document.cookie.length;
			
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	
	return "";
}

function setCookiePath(c_name,value,path,expiredays)
{
	var exdate = new Date();
	var domain = String(window.location.host);
	
	exdate.setDate(exdate.getDate() + expiredays);
	
	domain = domain.replace("www","");
	domain = domain.replace("music","");
	domain = domain.replace("strings","");
	domain = domain.replace("harps","");
	domain = domain.replace("server1","");
	domain = domain.replace("server2","");
	domain = domain.replace("server3","");

	document.cookie = c_name + "=" + escape(value) + ";expires=" + exdate.toGMTString() + ";path=" + path + ";domain=" + domain;
}

function setCookie(c_name,value,expiredays)
{
	setCookiePath(c_name,value,"/",expiredays);
}

var	cartName   = "harp_connection_cart";
var	itemsName  = "harp_connection_items";
var	promoName  = "harp_connection_promo_code";
var	miniCart   = "";
var	newItems   = "";
var	orderNum   = "";
var	itemQty    = 0;
var 	valString  = "";
var	itemSKUs   = "";
var	itemVal    = 0;
var	didAdd     = false;

function initCart()
{
	if (miniCart == "")
	{
		miniCart = getCookie ( cartName  );
		newItems = getCookie ( itemsName );
	}
	
	if (miniCart == "")
		miniCart = "TBD|0|0.00";
		
	var cartWheels = miniCart.split("|");
	
	if (cartWheels[0] != null)	orderNum  = cartWheels[0];
	if (cartWheels[1] != null)	itemQty   = Number ( cartWheels[1] );
	if (cartWheels[2] != null)	valString = cartWheels[2];
	if (cartWheels[3] != null)	itemSKUs  = cartWheels[3];

	if (valString != "")
		itemVal = Number ( valString );
}	

function showCart()
{
	var qtyObj    = getObject ( "cartQty" );
	var valObj    = getObject ( "cartVal" );
	
	if (miniCart == "")
		initCart();
		
	if (miniCart == "")
		return;
		
	if (itemQty == 0)
		showHideObjectID("emptyCart", "fullCart")
	
	else
	{	
		if (itemQty == 1 && qtyObj != null)
			qtyObj.innerHTML = "1 item";
		
		if (itemQty != 1 && qtyObj != null)
			qtyObj.innerHTML = itemQty.toString() + " items";
			
		if (valObj != null)
			valObj.innerHTML = valString;
	
		showHideObjectID("fullCart", "emptyCart")
	}
}	

function inCart(sku)
{
	if (miniCart == "")
		initCart();
		
	if (miniCart == "")
		return (false);
	
	if (itemSKUs.indexOf(sku, 0) >= 0)
		return (true);
	else
		return (false);	
}	

function showInCart(sku)
{
	if (inCart(sku))
		showHideObjectID('in' + sku, 'notin' + sku);
}

// Returns true if should submit form because cookie could not be stored
function addToCart(sku,qty,price,tabstop,obj)
{
	var wrap = "0";
	
	// Look up wrapping
	if (obj != null)
	{
		if (obj.form != null)
		{
			if (obj.form['productwrap1'] != null)
			{
				if ( obj.form['productwrap1'].checked == true)
					wrap = "2";
				else
					wrap = "1";
			}
			if (obj.form['wrap'] != null)
			{
				if ( obj.form['wrap'].checked == true)
					wrap = "2";
				else
					wrap = "1";
			}
		}
	}
	
	if (miniCart == "")
		initCart();
		
	if (miniCart == "")
		return (true);
	
	if ( !inCart ( sku ) )
	{
		var valCents    = 0;

		itemQty   = itemQty  + 1;
		itemVal   = Math.round ( ( itemVal  + ( qty * price ) ) * 100 ) / 100;	
		itemSKUs  = itemSKUs + ( ( itemSKUs != "" ) ? "/" : "" ) + sku;		// Mini cart holds SKUs only
		
		valCents  = Math.round ( itemVal * 100 );
		
		if (valCents == 0)
			valString = "0.00";
			
		else if (valCents < 10)
			valString = "0.0" + (valCents.toString());
	
		else if (valCents < 100)
			valString = "0." + (valCents.toString());
			
		else
		{
			var valLen = 0;
			
			valString  = valCents.toString();
			valLen     = valString.length;
			
			valString  = valString.substring ( 0, valLen - 2 ) + "." + valString.substring ( valLen - 2 );
		}
	
		miniCart = orderNum + "|" + itemQty.toString() + "|" + valString + "|" + itemSKUs;
		
		setCookie ( cartName, miniCart, 1 );
		
		// Double check - make sure cookie gets stored. If not, submit traditional form
		if ( getCookie ( cartName ) != miniCart)
			return (true);
			
		// Create specific list of new items to add to cart
		newItems = newItems + ( ( newItems != "" ) ? "/" : "" ) + qty.toString() + "," + sku + "," + wrap;	// Qty, SKU, Wrap
		
		setCookie ( itemsName, newItems, 1 );
		
		if ( getCookie ( itemsName ) != newItems)
			return (true);
			
		// Update HTML
		showHideObjectID('added' + sku, 'notin' + sku);
			
		didAdd = true;
	}
	
	showCart();

	return (false);
}	

// URL Management - Updates cart submit form to go to product anchor
function updateRedirURL(anchor,doUpdate,doSubmit)
{
	var theForm = document.forms['viewcartform'];
	var theURL  = "";
	var where   = 0;
	
	if (!theForm) return ("");
	if (!theForm.itemRedir) return ("");
	
	theURL = String(theForm.itemRedir.value);
	where  = theURL.indexOf('#',0);
	
	if (where > 0)
		theURL = theURL.substring ( 0, where );
	
	theURL = theURL + '#' + anchor;
	
	if (doUpdate)
		theForm.itemRedir.value = theURL;
		
	if (doSubmit)
		theForm.submit();
		
	return (theURL);
}

function updateRedirAndSubmit(anchor)
{
	// Leave URL intact on product detail pages
	if (String(window.location).indexOf('product.php',0)>=0)
		updateRedirURL(anchor,false,true);
	else
		updateRedirURL(anchor,true,true);
}

// smartBackButton - goes back under normal circumstances; goes forward to url if we added something to mini cart so minicart gets refreshed
function smartBackButton(anchor)
{
	if (didAdd)
	{
		var theURL = updateRedirURL(anchor,false,false);
		window.location = theURL;
		return;
	}
	
	window.history.back(-1);
}

// Init during startup (cookies are available early on)
initCart();

