var sources = new Array();

function log(message) {
    if (!log.window_ || log.window_.closed) {
        var win = window.open("", null, "width=400,height=200," +
                              "scrollbars=yes,resizable=yes,status=no," +
                              "location=no,menubar=no,toolbar=no");
        if (!win) return;
        var doc = win.document;
        doc.write("<html><head><title>Debug Log</title></head>" +
                  "<body></body></html>");
        doc.close();
        log.window_ = win;
    }
    var logLine = log.window_.document.createElement("div");
    logLine.appendChild(log.window_.document.createTextNode(message));
    log.window_.document.body.appendChild(logLine);
}

function setOpacity( id, opacity ) {
    var obj = document.getElementById(id);
    log( id + ";" + opacity + ";" + (obj.src ? obj.src : obj.style.backgroundImage) );
    var style=obj.style;
    style.KhtmlOpacity = style.MozOpacity = style.opacity = (opacity / 100); 
    style.filter = "alpha(opacity=" + opacity + ")";
}

function rot_pic(id, dir, max, captions) {
	if(document.images){
		var img = document.getElementById("id_"+id);
		var src = img.src;
		
		var idx = src.replace( /.*\/(\d*)?.*/, '$1' );
		idx=+idx+dir;
		
		if( idx > max )
			idx = 1;
		else if( idx < 1 )
			idx = max;
			
		var loading = new Image();
		sources[id] = loading;
		loading.onload = function() {
			loadfinish( id );
		}
		
		loading.src=src.replace( /\/\d*\?/, "/" + idx + "?" );
		setTimeout( 'if ( sources[\'' + id + '\'] != null ) document.getElementById(\'id_' + id + '\').src = \'/images/loading.gif\'', 100 );
		
		var lbl = document.getElementById("idx_"+id);
		if( lbl != null )
			lbl.innerHTML=idx;
			
		var el = document.getElementById("imglnk_"+id);
		if( el != null )
		  {
		  var href = el.href;
		  if( href != null )
		  	{
		    if( href.indexOf( "?" ) < 0 )
		    	el.href=href+"?pic="+idx;
		    else
		    	el.href=new String( el.href ).replace( /pic=\d*/, "pic=" + idx );
		    }
		  }

		el = document.getElementById("matlnk_"+id);
		if( el != null )
		  {
		  var href = el.href;
		  if( href != null )
		  	{
		    if( href.indexOf( "?" ) < 0 )
		    	el.href=href+"?pic="+idx;
		    else
		    	el.href=new String( el.href ).replace( /pic=\d*/, "pic=" + idx );
		    }
		  }
		  
		if( captions != null )
  		  {
		  --idx;
		  var caption = "";
		  if( idx > -1 && idx < captions.length )
		    caption = captions[idx];
		    
		  el = document.getElementById("caption_"+id);
		  
		  if( el != null && el.innerHTML != caption )
		    el.innerHTML = caption;
		  }  
	}
}

function loadfinish( id ) {
	var img = document.getElementById("id_"+id);
	var loaded = sources[id];
	if ( loaded != null ) {
		img.src = loaded.src;
		delete sources[id];
	}
}

var quantities;
var prices;
var quantityTotal;
var priceTotal;
var submitButton;

function cartinit() {
	quantities = new Array();
	prices = new Array();
	var rows = document.getElementsByTagName("tr");
	for( var i = 0; i < rows.length; ++i  )	{
		var price = null;
		var qty = null;
		
		var children = rows[i].childNodes;
		for( var j = 0; j < children.length; ++j ) {
			if( "catprice" == children[j].className ) {
				price = children[j];
			} else if ( "catqty" == children[j].className ) {
				var inputs = children[j].getElementsByTagName( "input" );
				if( inputs != null && inputs.length > 0 )
					qty = inputs[0];
			} else if ( "catpricetotal" == children[j].className ) {
				priceTotal = children[j];
			} else if ( "catqtytotal" == children[j].className ) {
				quantityTotal = children[j];
			}
		}
		if( price != null && qty != null ) {
			quantities.push( qty );
			prices.push( price );
			if( document.addEventListener )
				qty.addEventListener( "change", cartChange, false );
			else
				qty.attachEvent( "onchange", cartChange );
		}
	}
	
	var inputs = document.getElementsByTagName( "input" )
	submitButton = new Array();
	for( var i = 0; i < inputs.length; ++i )
		if( "update" == inputs[i].name )
			submitButton.push( inputs[i] );
		else if( "clear" == inputs[i].name )
			inputs[i].onclick=cartClear;
		else if( "submit" == inputs[i].name )
			inputs[i].onclick=cartSubmit;
}

function initialfocus( name ) {
	var inputs = document.getElementsByTagName( "input" )
	for( var i = 0; i < inputs.length; ++i )
		if( name == inputs[i].name ) {
			inputs[i].focus();
			inputs[i].select();
			break;
		}
}

function cartClear() {
	return confirm("Are you sure you want to clear your cart?" )
}

function cartSubmit() {
	return confirm( "Are you sure you want to submit this cart?" );
}

function cartChange() {
	var qTotal = 0;
	var pTotal = 0.0;
	for( var i = 0; i < quantities.length; ++i ) {
		var qty = (+quantities[i].value);
		var price = (+prices[i].innerHTML)*100;
		
		if( isNaN( qty ) ) {
			alert( "Quantity field must be a non-negative whole number." );
			quantities[i].value = qty = (+0);
		} else if( qty != Math.round( qty ) || qty < 0 ) {
			alert( "Quantity field must be a non-negative whole number." );
			quantities[i].value = qty = Math.max( 0, Math.round( qty ) );
		}			
		
		qTotal += qty;
		if ( !isNaN( price ) )
			pTotal += (price * qty);
	}
	
	pTotal = new String( pTotal / 100 );
	qTotal = new String( qTotal );
	
	
	switch( pTotal.indexOf( "." ) )	{
	case -1:				pTotal+=".";
	case pTotal.length-1:	pTotal+="0";
	case pTotal.length-2:	pTotal+="0";
	}
	
	for( var i = pTotal.length-6; i > 0; i-=3 )
		pTotal = pTotal.substr( 0, i ) + "," + pTotal.substr( i );	
	
	priceTotal.innerHTML="$" + pTotal;
	quantityTotal.innerHTML=qTotal;
	for( var i = 0; i < submitButton.length; ++i )
		if( submitButton[i].style )
			submitButton[i].style.fontWeight="bold";
}

function wsz() {
	window.status='';
	return true;
}

function wsn() {
	window.status='next image';
	return true;
}

function wsp() {
	window.status='previous image';
	return true;
}
