function trimAll(sString) {
	while (sString.substring(0,1) == ' ')
		{
		sString = sString.substring(1, sString.length);
		}
	while (sString.substring(sString.length-1, sString.length) == ' ')
		{
		sString = sString.substring(0,sString.length-1);
		}
	return sString;
}

function create3DArray(d1, d2, d3) {
  var lineTxt = "5";

  var x = new Array(d1);
	
  for (var i=0; i < d1; i++) {
    x[i] = new Array(d2);
  }

  for (var i=0; i < d1; i++) {
    for (var j=0; j < d2; j++) {
      x[i][j] = new Array(d3);
    }
  }
  return x;
}

function setDivValue(elem,v) {
	//alert("setting "+elem+" to "+v);
	obj = document.getElementById(elem);
	obj.innerHTML=v;
}

function create2DArray(d1, d2) {

  var x = new Array(d1);
	
  for (var i=0; i < d1; i++) {
    x[i] = new Array(d2);
  }

  return x;
}

// make arrays always 3d
var i_selectedDim = new Array(0,0,0);
var i_currentImgL;

function dimChange(dim, idx) {
	i_selectedDim[dim] = idx;
//	alert(i_selectedDim[0] + "|" + i_selectedDim[1] + "|" + i_selectedDim[2])

	setDivValue("dimsel"+dim, i_dimVal[i_selectedDim[0]][i_selectedDim[1]][i_selectedDim[2]]);
	if (i_listprice[i_selectedDim[0]][i_selectedDim[1]][i_selectedDim[2]] != undefined)	{

		lp = i_listprice[i_selectedDim[0]][i_selectedDim[1]][i_selectedDim[2]];
		setProdListprice(lp);
		p = i_price[i_selectedDim[0]][i_selectedDim[1]][i_selectedDim[2]];
		setProdPrice(p);
		imgM = i_imgM[i_selectedDim[0]][i_selectedDim[1]][i_selectedDim[2]];
		setMediumProdImg(imgM);

		setDivValue("avail", i_avail[i_selectedDim[0]][i_selectedDim[1]][i_selectedDim[2]]);
		setDivValue("save", i_save[i_selectedDim[0]][i_selectedDim[1]][i_selectedDim[2]]);
		setDivValue("ship", i_ship[i_selectedDim[0]][i_selectedDim[1]][i_selectedDim[2]]);
		setDivValue("swatchBar", i_imgT[i_selectedDim[0]][i_selectedDim[1]][i_selectedDim[2]]);
		//setDivValue("cartAddHtml", i_cartAdd[i_selectedDim[0]][i_selectedDim[1]][i_selectedDim[2]]);

		i_currentImgL = i_imgL[i_selectedDim[0]][i_selectedDim[1]][i_selectedDim[2]];
	}
	else {
		setProdListprice("");
		setProdPrice("");
		setMediumProdImg("images\spacer.gif");
		i_currentImgL = "images\spacer.gif";

		setDivValue("avail", "Sorry, this " + dimText + " combination is not available.");
		setDivValue("save", "");
		setDivValue("ship", "");
		setDivValue("swatchBar", "");		
	}
}

function addToCart(anchor) {
	if (i_listprice[i_selectedDim[0]][i_selectedDim[1]][i_selectedDim[2]] == undefined)	{
		//alert(document.getElementById("avail").innerHTML);
		anchor.href="javascript:void(0);";
		return false;
	}
	else {
		anchor.href=i_cartAdd[i_selectedDim[0]][i_selectedDim[1]][i_selectedDim[2]]+'&Qty='+document.getElementById('qty').value;
	}
}

function swatchOnMouseOver(imgM, imgL) {
	this.border=0;
	i_currentImgL= imgL;
	setMediumProdImg(imgM);
}

function setMediumProdImg(img) {
	document.images['prodImg'].src=img;
}

function setProdListprice(p) {
	setDivValue("listprice", p);
}

function setProdPrice(p) {
	setDivValue("salesprice", p);
}

function showTarget(href) { 
	wnd = window.open(href,'PDP', "height=" + screen.height + ",width=" + screen.width + ",links=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=no,resizable=yes"); 
	return wnd;
} 


function showTrackInfo(tag) { 
	lrg = window.open('http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http%3A%2F%2Fwww.amazon.com%2Fgp%2Fhelp%2Fcustomer%2Fdisplay.html%3FnodeId%3D468530&tag='+tag+'&linkCode=ur2&camp=1789&creative=9325','TrackInfo', "width=800,height=880,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no"); 
} 

function showShipInfo(tag) { 
	lrg = window.open('http://www.amazon.com/gp/help/customer/display.html?ie=UTF8&nodeId=468520&tag='+tag,'TrackInfo', "width=800,height=880,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no"); 
} 

function showReturnPolicy(tag) { 
	lrg = window.open('http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http%3A%2F%2Fwww.amazon.com%2Fgp%2Fhelp%2Fcustomer%2Fdisplay.html%3FnodeId%3D15015711&tag='+tag+'&linkCode=ur2&camp=1789&creative=9325','ReturnPolicy', "width=800,height=880,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no"); 
} 

function showFreeShip() { 
	lrg = window.open('http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http%3A%2F%2Fwww.amazon.com%2Fgp%2Fhelp%2Fcustomer%2Fdisplay.html%3Fie%3DUTF8%26nodeId%3D527692%26pop-up%3D1&tag=pricepinx-20&linkCode=ur2&camp=1789&creative=9325','FREESuperSaverShipping', "width=800,height=880,toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no"); 
} 


function BrowserType() {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.version = navigator.appVersion
	this.v = parseInt(this.version)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie5 = (this.version.indexOf('MSIE 5')>0)
	this.min = (this.ns||this.ie)
}

bType = new BrowserType()


function openWindow (URL, WinName, Features) { 
	window.open(URL, WinName, Features); 
} 

function winOpen (URL, WinName, Features) { 
	window.open(URL, WinName, Features); 
} 

function showLargeImage(title, h, w) { 

	lrg = window.open('','title',"width=" + w + ",height=" + h + ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no"); 
	lrg.document.write("<HTML><HEAD><TITLE>"+title+"</TITLE><BODY><IMG SRC='"+i_currentImgL+"'></BODY></HTML>");

} 


function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );



	//alert("name="+ name);
	//alert("setting val="+ value);

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{ 
	//	  alert("found cookie");
		c_start=c_start + c_name.length+1 ;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
	//	alert("val="+document.cookie.substring(c_start,c_end));
		return unescape(document.cookie.substring(c_start,c_end));
		} 
	  }
	return "";
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function signOut() {
	Delete_Cookie( "auth", "/", "" );
	Delete_Cookie( "uid", "/", "" );
	Delete_Cookie( "fn", "/", "");
	Delete_Cookie( "ia", "/", "");
	Delete_Cookie( "add2catID", "/", "");
}
