//  Common JavaScript include file

// onLoad event to make the page scroll to the right position when using the cart

	window.onload = function() { set_scroll() }

// if query string in URL contains scroll=nnn, then scroll position will be restored

function set_scroll() {

	var search = window.location.search;			// get query string parameter with "?"		
	if (search) {
		var matches = /scroll=(\d+)/.exec(search);	// find scroll parameter in query string
		if (matches)
			window.scrollTo(0, matches[1]);		// jump to the scroll position if scroll parameter exists
	}
}

/*  toggleDivVisibility - used to show and hide product details  */

function toggleDivVisibility(divId) {
	var item = document.getElementById(divId);
	if (item) {
		if (item.className == 'visible')
			item.className = 'hidden';
		else
			item.className = 'visible';
	}
}

/*  forceCartOrder
	Prepends a sequence number to the front of part numbers in a form so that
	items appear in the cart in the order in which the customer added them. The
	current sequence number is saved in a cookie. Also creates or updates
	the "gobackto" hidden field with this page's URL and scroll position.  */

function forceCartOrder(theForm)
{

	var orderPrefix;			// order prefix as a number
	var orderPrefixStr;		// order prefix as a string from the cookie
	var partRegExp = /p\-2653\^.../
	var valueStr;			// value= string for the form field.
	var i;
	var today;				// holds today's date
	var expires;			// holds cookie expiration
	var foundGoBackTo = false;	// assume the gobackto field has not been created
	var newField;

// Retrieve previous order prefix from a cookie. Start at 100 if no cookie

	orderPrefix = 100;
	orderPrefixStr = getCookie("orderPrefix");
	if (orderPrefixStr) 
		orderPrefix = orderPrefixStr - 0;				// convert to number

// Prefix part numbers. 

	for (i=theForm.length-1; i>=0; i--) {
		if (theForm.elements[i].name == "gobackto") {		// fill out "gobackto"
			theForm.elements[i].value = getReturnURL();
//			alert("found, " + theForm.elements[i].value);
			foundGoBackTo = true;
		}		
		else if (partRegExp.test(theForm.elements[i].value)) {
  			valueStr = theForm.elements[i].value.replace(partRegExp, "p-2653^" + orderPrefix++);
			theForm.elements[i].value = valueStr;
//			alert(valueStr);	
		}
	} 

// If the gobackto field is not present, create it

	if (!foundGoBackTo) {
//		alert("adding, " + getReturnURL());
		newField = document.createElement("input");
		newField.type = "hidden";
		newField.name = "gobackto";
		newField.value = getReturnURL();
		theForm.appendChild(newField);
	}

// Write out the ordering prefix in a 2 day cookie.
	
	today = new Date();
	expires = new Date(today.getTime()+2*24*60*60*1000);
	setCookie("orderPrefix", orderPrefix.toString(), expires);
}

// Returns this page's URL with current scroll positioned in the query string

function getReturnURL() {
	var scroll;
	var url = window.location.href.split("?");

	if (typeof(window.pageYOffset) == 'number')			// Netscape compliant
		scroll = window.pageYOffset;
	else if (document.body && document.body.scrollTop)		// DOM compliant
    		scroll = document.body.scrollTop;
	else if (document.documentElement && document.documentElement.scrollTop)// IE6 standards compliant mode
		scroll = document.documentElement.scrollTop;
	else 
		scroll = 0;								// needed for IE6 (when vertical scroll bar is on the top)
	return(url[0] + '?scroll=' + scroll);
}

// viewCart - This function is required to get rid of scroll position data if view cart link is clicked.
function viewCart()
{
	var url = window.location.href.split("?");
	window.location.href="http://www.cartserver.com/sc/cart.cgi?item=p-2653&gobackto=" + escape(url[0]);
	return(false);
}


/// COOKIE FUNCTIONS

function setCookie(name, value, expires, path, domain, secure) {
	document.cookie= name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) 
			return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

