var compareField = 4;
var currentTable = 0;
var selectedMode = 0;
var domain = "pricenoia";
var receivedData = new Array();

// Store cookie if javascript enabled
setCookie( "js", "1", 365 );

// Get last mode
selectedMode = getCookie( "modes" );
if( selectedMode == null || selectedMode < 0 || selectedMode > 3 ) selectedMode = 0;

function updateModes()
{
	setCookie( "modes", selectedMode, 365 );
}

function getCookie( name )
{
	if( document.cookie.length > 0 ) {
		start = document.cookie.indexOf( name + "=" );
		if( start != -1 ) {
			start += name.length + 1;
			end = document.cookie.indexOf( ";", start );
			if( end == -1 ) end = document.cookie.length;
			return unescape( document.cookie.substring( start, end )); 
		}
	}
	return null;
}

function setCookie( name, value, expiredays )
{
	var expire  = new Date ();
	expire.setTime( expire.getTime() + ( expiredays * 24 * 3600 * 1000 ));
	document.cookie = name + "=" + escape( value ) + (( expiredays == null ) ? "" : "; expires=" + expire.toGMTString()) + "; path=/";
}

function floatCompare( a, b )
{
	sa = new String( a[compareField][0])
	sb = new String( b[compareField][0])
	
	if (sa.indexOf("Free") >= 0) sa = "0.0";
	if (sb.indexOf("Free") >= 0) sb = "0.0";
	
	sa = sa.replace( /[^0-9\.]/gi, "" );
	sb = sb.replace( /[^0-9\.]/gi, "" );

	fa = parseFloat( sa );
	fb = parseFloat( sb );

	if( fa !== fa && fb !== fb ) return( stringCompare( a[0][0], b[0][0] ));
	else if( fa !== fa ) return( 1 );
	else if( fb !== fb ) return( -1 );
	else if( fa == fb ) return( stringCompare( a[0][0], b[0][0] ));
	else if( fa > fb ) return( 1 );
	else return( -1 );
}

function stringCompare( a, b )
{
	fa = new String( a );
	fb = new String( b );

	if( fa !== fa && fb !== fb ) return( 0 );
	else if( fa !== fa ) return( 1 );
	else if( fb !== fb ) return( -1 );
	else if( fa == fb ) return( 0 );
	else if( fa > fb ) return( 1 );
	else return( -1 );
}

function strip_tags(txt) {
	a = txt.indexOf('<');
	b = txt.indexOf('>');
	len = txt.length;
	c = txt.substring(0, a);
	if(b == -1) {
		b = a;
	}
	d = txt.substring((b + 1), len);
	txt = c + d;
	cont = txt.indexOf('<');
	if (cont != -1) {
		txt = strip_tags(txt);
	}
	return txt;
}

function orderTable( type )
{
	var data = new Array();
	var i,k;

	tab = document.getElementById( 'priceTable_' + type  );
	rows = tab.rows.length;

	for( i=1; i<rows; i++ ) {
		data[i-1] = new Array();
		for( k=0; k<tab.rows[i].cells.length; k++ ) {
			data[i-1][k] = new Array();
			data[i-1][k][0] = tab.rows[i].cells[k].innerText;
			data[i-1][k][1] = tab.rows[i].cells[k].innerHTML;
			if( data[i-1][k][0] == undefined ) {
				data[i-1][k][0] = strip_tags( data[i-1][k][1] );
			}
		}
	}

	switch( compareField ) {
		case 0: 
			data.sort( stringCompare );
			break;
		default:
			data.sort( floatCompare );
	}

	// Set innerHTML
	for( i=1; i<rows; i++ ) {
		for( k=0; k<tab.rows[i].cells.length; k++ ) {
			tab.rows[i].cells[k].innerHTML = data[i-1][k][1];
		}
	}
}

function buildURL() { 
	var url; 
	url = new String( "/search/" + (encodeURI( document.f.s.value )).replace( /\//gi, "|" ) + "/" + escape( document.f.m.value ) + "/" + escape( document.f.sv.value )) + "/index.html"; 
	url = url.replace( /\%20/gi, "+" ); 
	document.location =  url;
	return( false ); 
}

function buildUpdate() { 
	var url; 
	url = new String( "/comp/" + escape( document.fu.isbn.value ) + "/" + escape( document.fu.sv.value ) + "/" + encodeURI( document.fu.s.value ) + "/" + escape( document.fu.m.value ) + "/" + escape( document.fu.p.value ) + "/" + encodeURI( document.fu.t.value ) + "/" + escape( document.fu.c.value )) + "/index.html"; 
	url = url.replace( /\%20/gi, "+" ); 
	document.location =  url;
	return( false ); 
}


function hld(obj)
{
	hl(obj);
}

function hl(obj)
{
	obj.style.backgroundColor='#FFF5DC'

	if( obj.cells != undefined ) {
		if( obj.cells[compareField] != undefined ) {
			obj.cells[compareField].bgColor='#FFEBBC';
		}
	}
}


function old(obj)
{
	ol(obj);
}

function ol(obj)
{
	obj.style.backgroundColor='#FFFFFF';
	if( obj.cells != undefined ) {
		if( obj.cells[compareField] != undefined ) {
			obj.cells[compareField].bgColor='#F8F8F8';
		}
	}
}

function r( k, v ) 
{
	var txt = "<FONT Color=Red>no stock</FONT>";
	var name, title, o, className, id;

	if( v == null || v == undefined || v == "-1" ) v = txt;

	o = document.getElementById( k );
	if( typeof( o ) != "undefined" ) o.innerHTML = new String( v );

	for( id=0; id<4; id++ ) {

		switch( id ) {
			case 0: name = "mode_new"; title = "Show amazon products"; break;
			case 1: name = "mode_used"; title = "Show used products";  break;
			case 2: name = "mode_third"; title = "Show third party products"; break;
			case 3: name = "mode_all"; title = "Show all products merged"; break;
		}

		if( id == selectedMode ) {
			className = "boxTabSelected";
			title = "";
		} else { 
			className = "boxTab";
		}
		document.getElementById( name ).className = className;
		document.getElementById( name ).title = title;

		tabOut( id, document.getElementById( name ));
	}
}

function t( update, k, o1, o2, o3, p1, p2, p3, s1, s2, s3, t1, t2, t3, l ) 
{
	var txt = "<FONT Color=Red>no stock</FONT>";
	var i = receivedData.length;

	receivedData[i] = new Array();
	receivedData[i][0] = k;
	receivedData[i][1] = l;

	receivedData[i][2] = new Array();	// new
	receivedData[i][3] = new Array();	// used
	receivedData[i][4] = new Array();	// third

	receivedData[i][2][0] = o1;
	receivedData[i][2][1] = p1;
	receivedData[i][2][2] = s1;
	receivedData[i][2][3] = t1;

	receivedData[i][3][0] = o2;
	receivedData[i][3][1] = p2;
	receivedData[i][3][2] = s2;
	receivedData[i][3][3] = t2;

	receivedData[i][4][0] = o3;
	receivedData[i][4][1] = p3;
	receivedData[i][4][2] = s3;
	receivedData[i][4][3] = t3;

	if( update ) updateIndex( i );
}

function tf( k )
{
	i = receivedData.length;
	receivedData[i] = new Array();
	receivedData[i][0] = k;
	receivedData[i][1] = null;
	updateIndex( i );
}


function updateTable(type)
{
	var tBody = document.getElementById( 'priceTable_' + type ).tBodies[0];
	var rows = tBody.rows;
	var total = rows.length;

	for( i=1; i<total; i++ ) {
	    tBody.removeChild( rows[rows.length - 1] );
	}

	for( i=0; i<receivedData.length; i++ ) {
		updateIndex( i );
	}

	orderTable(type);
}

// new data received
function updateIndex( i, type ) 
{
	var body = document.getElementById( 'priceTable_' + type ).tBodies[0];
	var t, k, tr, td, txt, align, color, bgColor;

	for( t=0; t<3; t++ ) {


		if( selectedMode != t && selectedMode != 3 ) continue;

		switch( t ) {
			case 0: color = "#000000"; sufix = " (new)"; break;
			case 1: color = "8F0000"; sufix = " (used)"; break;
			case 2: color = "006F2F"; sufix = " (third)"; break;
		}
		
		// Hide no stock in multiple
		if( selectedMode == 3 ) {
			if( receivedData[i][1] == null ) continue;
			if( receivedData[i][2+t][0] == "-1" ) continue;
		}

		tr = body.insertRow( body.rows.length );

		tr.onmouseover = hld;
		tr.onmouseout = old;
		
		if( selectedMode == 3  ) {
			switch( receivedData[i][0] ) {
				case "aus":	txt = ".com"; break;
				case "auk":	txt = ".uk"; break;
				case "ajp": txt = ".jp"; break;
				case "ade": txt = ".de"; break;
				case "aca":	txt = ".ca"; break;
				case "afr": txt = ".fr"; break;
			}
			txt += sufix;
		} else {
			switch( receivedData[i][0] ) {
				case "aus":	txt = "amazon.com"; break;
				case "auk":	txt = "amazon.co.uk"; break;
				case "ajp": txt = "amazon.jp"; break;
				case "ade": txt = "amazon.de"; break;
				case "aca":	txt = "amazon.ca"; break;
				case "afr": txt = "amazon.fr"; break;
			}
		}

		td = tr.insertCell( tr.cells.length );
		td.innerHTML = "<NOBR><FONT Color='" + color + "'>" + txt + "</FONT></NOBR>";

		for( k=0; k<5; k++ ) {
			
			bgColor = null;

			if( receivedData[i][1] == null ) {
				// Failed
				txt = "<FONT Color=Red>failed</FONT>";
				if( k < 4 ) {
					align = "right";
				} else {
					align = "center";
				}
			} else {
				switch( k ) { 
					case 4:
						txt = receivedData[i][1];
						if( receivedData[i][2+t][0] == "-1" ) txt = "<FONT Color='red'>no stock</FONT>";
						align = "center";
						break;
					default:

//						if( compareField+1 == k ) bgColor = "#F0F0F0";
//						else bgColor = "#FFFFFF";
						txt = receivedData[i][2+t][k];
						switch( txt ) {
							case "-1": 
								txt = "<FONT Color='red'>no stock</FONT>";
								break;
							case "-2":
								txt = "<FONT Color='#FF4000'>N/A</FONT>";
								break;
							case "-3":
								txt = "<FONT Color='green'>Free!</FONT>";
								break;
						}
						align = "right";
				
				}
			}
			td = tr.insertCell( tr.cells.length );
			td.innerHTML = "<NOBR><FONT Color='" + color + "'>" + txt + "</FONT></NOBR>";
			td.align = align;
//			if( bgColor != null ) td.bgColor = bgColor;
		}

		if( tr.cells[compareField] != undefined ) {
			tr.cells[compareField].bgColor='#F8F8F8';
		}
	}
}

function modeSelect( id )
{
	var name, title;

	selectedMode = id;

	for( i=0; i<4; i++ ) {

		if( id == i ) {
			className = "boxTabSelected";
		} else { 
			className = "boxTab";
		}

		switch( i ) {
			case 0: name = "mode_new"; title = "Show amazon products"; break;
			case 1: name = "mode_used"; title = "Show used products";  break;
			case 2: name = "mode_third"; title = "Show third party products"; break;
			case 3: name = "mode_all"; title = "Show all products merged"; break;
		}

		document.getElementById( name ).className = className;

		if( id == i ) {
			document.getElementById( name ).title = "";
		} else {
			document.getElementById( name ).title = title;
		}

		tabOut( i, document.getElementById( name ));
	}

	updateModes();
	updateTable();
}

function rn( k )
{
	var txt = "<FONT Color=Red>no stock</FONT>";
	r(k, txt);
}

function mt( obj )
{
	obj.style.cursor = "pointer";
}

function st( id, type )
{
	var obj = document.getElementById( 'priceTable_' + type );

	compareField = id;

	for( i=1; i<obj.rows.length; i++ ) {
		for( j=0;j<5;j++ ) {
			if( j == compareField ) {
				obj.rows[i].cells[j].bgColor='#F8F8F8';
			} else {
				obj.rows[i].cells[j].bgColor='';
			}
		}
	}

	orderTable(type);

	return false;
}

function imageLoaded( obj )
{
//	if( obj.width <= 24 ) {
		obj.src = '/images/noimage.png';
		obj.width = 50;
		obj.style.border = 'none';
//	}
}

function tabOver( id, obj )
{
	obj.style.backgroundColor='#FFF5DC';
	obj.style.cursor = 'pointer';
}

function tabOut( id, obj )
{
	if( selectedMode == id ) {
		obj.style.backgroundColor='white';
	} else {
		obj.style.backgroundColor='#F8F8F8';
	}
	obj.style.cursor = 'default';

}

function select_tab(type)
{
	var tabs = [ "price_amazon", "price_third", "price_used", "price_all" ];

	for( i=0; i<tabs.length; i++ )  {
		$('tab_' + tabs[i]).className = "";
		$(tabs[i]).style.display = "none";
		
		if (tabs[i] == type) {
			$('tab_' + tabs[i]).className = "selected";
			$(tabs[i]).style.display = "block";
		}
			
	}
}

