

function browserCheck() {
	if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
	 var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	 if (ffversion<3)
	  document.getElementById("errorMessage").innerHTML=("<font class='general'>This site is designed to run best in FireFox 3 or above..</font>")
	}
	
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
	 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	 if (ieversion<7)
	  document.getElementById("errorMessage").innerHTML=("<font class='general'>This site is designed to run best in IE 7 or above..</font>")
	}
}

function startChat(aUrl){

	// DISPLAY IN NEW WINDOW
	//window.open("html/viewChat.html", "Live Chat!", "height=500,width=400,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0");
	//window.open(aUrl, "Live Chat!", "height=500,width=400,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0");
	window.open(aUrl,"Live Chat!","width=500,height=400,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0");

	/*
	// DISPLAY IFRAME IN DIV
	var chatFrame = document.getElementById("chatFrame");
	chatFrame.src = "html/viewChat.html";
	var chatWindow = document.getElementById("chatWindow");
	chatWindow.style.display = "block";
	*/
}

function openLink(aUrl,aTitle,aWidth,aHeight){
	window.open(aUrl,aTitle,"width="+aWidth+",height="+aHeight+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0");
}

function displayAltImage(node,content){
	node.innerHTML = content;
}

function noResults(){
	var img = document.getElementById("image_1");
	img.src = "images/background/noResults.jpg";
}
function focusOn(item_id,item_name,item_class,focusOn,alts,admin){

	// Replace the main image
	//var img = document.getElementById("image_1");
	var img = document.getElementById("gallery_image_zoom").childNodes[0];
	img.src = "images/items/" + item_class + "/" + item_name + "_"+focusOn+"-gallery.jpg";
	img.parentNode.href = img.src.replace(/-gallery/,"-full");
	img.setAttribute('alt',item_name);		
	img.setAttribute('title',item_name);		

	//buy form
	/*
	var aForm = "<form class='buy' action='listItems.php' method='POST' name='buyForm' id='buyForm'>";
	aForm = aForm + "<input type='text' name='quantity' maxlength='2' size='2' value='1'>";
	aForm = aForm + "<input type='hidden' name='item_id' value='" + item_id + "'>";
	if(admin==1){
		aForm = aForm + "<input type='submit' name='submit' value='u'>";
		aForm = aForm + "<input type='submit' name='submit' value='x'>";
	}
	aForm = aForm + "</form><button onClick=\"liveUpdate('gallery_title_cell','buyForm','GET','item.php')\">buy</button>";
	var galleryButtons = document.getElementById("gallery_button_cell");
	galleryButtons.innerHTML = aForm;
	*/
	// Update title
	var titleCell = document.getElementById("gallery_title_cell");
	titleCell.innerHTML = "Antique Pine " + item_name + "";
	
	
	// Update description
	var descCell = document.getElementById("gallery_desc_cell");
	descCell.innerHTML = document.getElementById("desc_"+item_id).innerHTML;

	// Update price
	var priceCell = document.getElementById("gallery_price_cell");
	priceCell.innerHTML = document.getElementById("price_"+item_id).innerHTML;
		
	// Update the surrounding link
	var imgZoom = document.getElementById("gallery_image_cell");
	imgZoom.href = img.src.replace(/-gallery/,"-full");
	imgZoom.target = "_blank";

	for(x = 1; x <= 2; x++) {
		var append = x+'';
		var alt = document.getElementById("gallery_image_" + append);

		if((alt.childNodes.length > 0) && (x <= alts)){
			// replace existing href and src
			imgSrc = "images/items/" + item_class + "/" + item_name + "_" + append + "-thumb.jpg";
			var img = document.getElementById("gallery_image_src_" + append);
			img.src = imgSrc;
			var aLink = document.getElementById("gallery_image_link_" + append);
			aLink.href = imgSrc.replace(/-thumb/,"-full");
		}
		if((alt.childNodes.length == 0) && (x <= alts)){
			// create new nodes
			var imgSrc = "images/items/" + item_class + "/" + item_name + "_" + append + "-thumb.jpg";
			aLink=document.createElement("a");
			aLink.id = "gallery_image_link_" + append;
			aLink.href = imgSrc.replace(/-thumb/,"-full");
			aLink.target = "_blank";
			img = document.createElement("img");
			img.id = "gallery_image_src_" + append;
			img.src=imgSrc;
			img.border=0;
			img.setAttribute('alt','alternative view of ' + item_name);	
			img.width=56;	
			img.height=70;
			aLink.appendChild(img);
		    alt.appendChild(aLink);
		}
		if((alt.childNodes.length > 0) && (x > alts)){
			// remove child nodes
			while (alt.childNodes[0]) {
    			alt.removeChild(alt.childNodes[0]);
			}
		}
	}
	var message = document.getElementById("messageContent");
	if(message != null){message.innerHTML = "";}

}

function zoomIn(td){
	
	var mainImageLink = document.getElementById("gallery_image_zoom");
	var imageString = "";
	var thumbnailCell = document.getElementById(td);
	if(thumbnailCell.childNodes.length > 0){
		
		// swap existing gallery image for alt image
		var img = thumbnailCell.childNodes[0].childNodes[0];
		imageString = img.src;
		mainImageLink.childNodes[0].src = imageString.replace(/-thumb/,"-gallery");
		//mainImageLink.childNodes[0].id = img.id;
		
		// swap href for the wrapper link so jqZoom knows what to target
		
		
	}	
	document.getElementById("gallery_image_zoom").href = imageString.replace(/-thumb/,"-full");
}

function removeError(){
	var error = document.getElementById("javascript_error");
	if(error != null){
		while (error.childNodes[0]) {
    		error.removeChild(error.childNodes[0]);
		}
	}
}

// GLOBAL VARIABLES for slideshow
var slideShowSpeed = 2000;
var j = 1;
var opacity = 0;
var preloadedImages;

function fadeIn() {

	if(opacity < 100){
		var container = document.getElementById("slideShow");
		if(container != null){
			opacity = opacity +2;
			container.style.opacity = opacity / 100;
			container.style.filter = "alpha(opacity=" + (opacity) + ")";
			t = setTimeout('fadeIn(\"slideShow\")', 30);
		}
	}
}

function runSlideShow(){

	var container = document.getElementById("slideShow");
	if(container != null){
	   	if(j < preloadedImages.length){

			// Reset
		   	opacity = 0;
		   	container.style.opacity = 0;
			container.style.filter = "alpha(opacity=0)";

			// Fade in
	   		container.src = preloadedImages[j].src;
	   		fadeIn();

			// Go again
	   		setTimeout('runSlideShow()', 3000);

	   	}
		j++;
	}
}

function liveUpdate(elementId,formId,httpMethod,aURL)
{
	if(formId != null){

		var sub = document.getElementById(formId).getElementsByTagName('input');
		query = new Array();
		for(i in sub){
			if(sub[i].name){
				query.push(sub[i].name + '=' + sub[i].value);
			}
		}
		query.push('submit=buy');
		query = '?' + query.join('&');
		aURL = aURL + query;
	}

	var xmlhttp;
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			document.getElementById(elementId).innerHTML=xmlhttp.responseText;
		}
	}

    xmlhttp.open(httpMethod,aURL,true);
    xmlhttp.send(null);
}

function liveSend(elementId,aURL)
{	
	var xmlhttp;
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			document.getElementById(elementId).innerHTML=xmlhttp.responseText;
			document.getElementById(elementId).style.display = "block";
		}
	}

    xmlhttp.open("GET",aURL,true);
    xmlhttp.send(null);
}


function liveFormSubmit(formId,elementId,aURL){
	// getElementsById doesn't always work!?
	for(i=0; i<document.forms.length; i++){
		if(formId == document.forms[i].id){
			var postString = "";
			var formData = document.forms[i].elements;
			for(i=0; i<formData.length; i++)
			{						
				if(formData[i].name != "submit"){
					//alert(formData[i].name + "=" + formData[i].value);
					if(i==0){
						postString = postString + "?" + formData[i].name + "=" + formData[i].value;
					} else {
						postString = postString + "&" + formData[i].name + "=" + formData[i].value;
					}
				}
				
			}
			liveSend(elementId,aURL+postString);
		}	
	}
}

function swap(goingOff,comingOn){
	document.getElementById(goingOff).innerHTML = document.getElementById(comingOn).innerHTML;
}

function makeVisable(comingOn){
	document.getElementById(comingOn).style.display = "block";
}

function makeInvisible(goingOff){
	document.getElementById(goingOff).style.display = "none";
}

function init(imageCount){
	// GLOBAL VARIABLES for slideshow
	preloadedImages = new Array();
	imageDir = document.getElementById("slideShow").src;
	imageDir = imageDir.replace(/1.jpg/,"");
	for(x=1;x<=imageCount;x++){
		preloadedImages[x] = new Image();
		preloadedImages[x].src = imageDir + x + ".jpg";
	}
	removeError();
	runSlideShow();
}
function get_url_parameter( param ){
	param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var r1 = "[\\?&]"+param+"=([^&#]*)";
	var r2 = new RegExp( r1 );
	var r3 = r2.exec( window.location.href );
	if( r3 == null ) return "";
	else return r3[1];
}

function getAllSheets() {
 if( !window.ScriptEngine && navigator.__ice_version ) {
    return document.styleSheets; }
  if( document.getElementsByTagName ) {
    //DOM browsers - get link and style tags
    var Lt = document.getElementsByTagName('link');
    var St = document.getElementsByTagName('style');
  } else if( document.styleSheets && document.all ) {

    var Lt = document.all.tags('LINK'), St = document.all.tags('STYLE');
  } else { return []; } //lesser browser - return a blank array
  //for all link tags ...
  for( var x = 0, os = []; Lt[x]; x++ ) {
    //check for the rel attribute to see if it contains 'style'
    if( Lt[x].rel ) { var rel = Lt[x].rel;
    } else if( Lt[x].getAttribute ) { var rel = Lt[x].getAttribute('rel');
    } else { var rel = ''; }
    if( typeof( rel ) == 'string' && rel.toLowerCase().indexOf('style') + 1 ) {
      //fill os with linked stylesheets
      os[os.length] = Lt[x];
    }
  }
  //include all style tags too and return the array
  for( var x = 0; St[x]; x++ ) { os[os.length] = St[x]; } return os;
}
function changeStyle() {
	
	for( var x = 0, ss = getAllSheets(); ss[x]; x++ ) {
		
	    //for each stylesheet ...
	    if( ss[x].title ) {
	    	//disable the stylesheet if it is switchable
	      	ss[x].disabled = true;	      
	    }
	    for( var y = 0; y < arguments.length; y++ ) {
	      	//check each title ...
	      	if( ss[x].title == arguments[y] ) {
		        //and re-enable the stylesheet if it has a chosen title
	    	    ss[x].rel = "stylesheet";
	    	    ss[x].disabled = false;
	    	    htmlString = "<div class='hidden' id='styleVal' title='"+arguments[y]+"'></div>";
	    	    document.write(htmlString);
			}
		}
	}
  	if( !ss.length ) { alert( 'Your browser cannot change stylesheets' ); }
}

function changeDimensions(itemIndex){

	imperial = document.getElementById("imperial_"+itemIndex);
	metric = document.getElementById("metric_"+itemIndex);
	if(imperial.style.display == "none"){
		imperial.style.display = "block";
		metric.style.display = "none";
		document.getElementById("swapLink_"+itemIndex).innerHTML = "- [swap to metric dimensions]";
	} else {
		imperial.style.display = "none";
		metric.style.display = "block";
		document.getElementById("swapLink_"+itemIndex).innerHTML = "- [swap to imperial dimensions]";
	}
	
}
