function ch_make(cat, make, type) {
	if (type == '') type = 0;
	res = xmlrequest('o=models/c='+cat+'/brid='+make+'/t='+type);
	tw = document.getElementById('twi_model');
	tw.innerHTML = res;
	if (make == '0') {
		document.getElementById('omake').disabled = false;
		document.getElementById('omodel').disabled = false;
	}
	else {
		document.getElementById('omake').disabled = true;
		document.getElementById('omodel').disabled = true;
	}
}

function ch_subcats(cat, type) {
	res = xmlrequest('o=subcats/c='+cat+'/t='+type);
	tw = document.getElementById('twi_subsubcat');
	tw.innerHTML = res;
}

function chm_nomod(make) {
	if (make == '0') {
		document.getElementById('omake').disabled = false;
	}
	else {
		document.getElementById('omake').disabled = true;
	}
}

function conv(type, el1, el2) {
	x1 = document.getElementById(el1);
	x2 = document.getElementById(el2);
	if (parseFloat(x1.value) != x1.value) x1.value = 0;
	if (parseFloat(x2.value) != x2.value) x2.value = 0;
	val = parseFloat(x1.value);
	switch (type) {
		case 'kw_hp':
			x2.value = Math.round(val*1.3405*100)/100;
			break;
		case 'hp_kw':
			x2.value = Math.round(val*0.7460*100)/100;
			break;
		case 'km_m':
			x2.value = Math.round(val*0.621);
			break;
		case 'm_km':
			x2.value = Math.round(val*1.609);
			break;
		case 'm_f':
			x2.value = Math.round(val*3.28*100)/100;
			break;
		case 'f_m':
			x2.value = Math.round(val*0.304*100)/100;
			break;
	}
}

function maxchars(obj) {
	txt = obj.value;
	ml = 1000;
	if (obj.id == 'avionics') lbl = document.getElementById('avchars');
	else lbl = document.getElementById('chars');
	if (txt.length > ml) {
		obj.value = txt.substring(0, ml);
		lbl.innerHTML = 0;
	}
	else {
		lbl.innerHTML = ml-txt.length;
	}
}

// Startup variables
var imageTag = false;
var theSelection = false;
 
// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version
 
var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_moz = 0;
 
var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);
 
// Define the bbCode tags
bbcode = new Array();
bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[quote]','[/quote]','[code]','[/code]','[list]','[/list]','[list=]','[/list]','[img]','[/img]','[url]','[/url]');
imageTag = false;
 
 
// Replacement for arrayname.length property
function getarraysize(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}
 
// Replacement for arrayname.push(value) not implemented in IE until version 5.5
// Appends element to the array
function arraypush(thearray,value) {
	thearray[ getarraysize(thearray) ] = value;
}
 
// Replacement for arrayname.pop() not implemented in IE until version 5.5
// Removes and returns the last element of an array
function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

function bbstyle(txtarea, bbnumber) {
	txtarea.focus();
	donotinsert = false;
	theSelection = false;
	bblast = 0;
	
	if (bbnumber == -1) { // Close all open tags & default button names
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;
			txtarea.value += bbtags[butnumber + 1];
			buttext = eval('document.form.addbbcode' + butnumber + '.value');
			eval('document.form.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		imageTag = false; // All tags are closed including image tags :D
		txtarea.focus();
		return;
	}
 
	if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			// Add tags around selection
			document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
			txtarea.focus();
			theSelection = '';
			return;
		}
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, bbtags[bbnumber], bbtags[bbnumber+1]);
		return;
	}
 
	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < bbcode.length; i++) {
		if (bbcode[i] == bbnumber+1) {
			bblast = i;
			donotinsert = true;
		}
	}
 
	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (bbcode[bblast]) {
				butnumber = arraypop(bbcode) - 1;
				txtarea.value += bbtags[butnumber + 1];
				buttext = eval('document.form.addbbcode' + butnumber + '.value');
				eval('document.form.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				imageTag = false;
			}
			txtarea.focus();
			return;
	} else { // Open tags
 
		if (imageTag && (bbnumber != 14)) {		// Close image tag before adding another
			txtarea.value += bbtags[15];
			lastValue = arraypop(bbcode) - 1;	// Remove the close image tag from the list
			document.post.addbbcode14.value = "Img";	// Return button back to normal state
			imageTag = false;
		}
 
		// Open tag
		txtarea.value += bbtags[bbnumber];
		if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(bbcode,bbnumber+1);
		eval('document.form.addbbcode'+bbnumber+'.value += "*"');
		txtarea.focus();
		return;
	}
	storeCaret(txtarea);
}
 
// From http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2)
		selEnd = selLength;
 
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}
 
// Insert at Claret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

function bbclean(txtarea) {
	for (i=0; i<bbtags.length; i++) {
		var toreplace = bbtags[i].replace(/\[/g, '\\[');
		toreplace = toreplace.replace(/\]/g, '\\]');
		toreplace = toreplace.replace(/\//g, '\\/');
		txtarea.value = txtarea.value.replace(eval("/"+toreplace+"/g"), '');
	}
	maxchars(txtarea);
}

function sh_tradecats() {
	t = document.getElementById('tradecats');
	cscroll = findScrollTop();
	cpos = findPos('tc_anchor');
	anchor_width = findWidth('tc_anchor');
	t.style.top = cpos[0]-358+"px";
	t.style.left = cpos[1]+anchor_width+2+"px";
	t.style.display = ((t.style.display == 'none' || t.style.display == '') ? 'block' : 'none');
}

function sh_tcbrands(id) {
	t = document.getElementById('tc_brands');
	cscroll = findScrollTop();
	cpos = findPos('tc_cat_'+id);
	anchor_width = findWidth('tc_cat_'+id);
	t.style.top = cpos[0]-338+"px";
	t.style.left = cpos[1]+anchor_width+2+"px";
	if (tcb_cache[id] == undefined) {
		if (skip_brands[id] != undefined) skip_str = skip_brands[id];
		else skip_str = '';
		tcb_cache[id] = xmlrequest("o=bftc/c="+id+"/s="+skip_str);
	}
	if (t.style.display == 'none' || t.style.display == '') t.innerHTML = tcb_cache[id];
	t.style.display = ((t.style.display == 'none' || t.style.display == '') ? 'block' : 'none');
}

function tc_add(id, name, hide_obj) {
	tbl = document.getElementById('trade_cats');
	if (document.getElementById('tc_none')) tbl.deleteRow(document.getElementById('tc_none').rowIndex);
	rows = tbl.rows.length;
	if (rows >= 3) {
		alert(LNG_TRADE_MAX_CAT_INT);
		return;
	}
	newrow = tbl.insertRow(rows);
	newrow.id = 'tc'+id;
	var newcell = newrow.insertCell(0);
	newcell.innerHTML = '<a id="tc_cat_'+id+'" href="" onClick="sh_tcbrands('+id+'); return false;">'+name+'</a>';
	newcell.width = 110;
	if (rows > 0) newcell.style.paddingTop = 3+'px';
	newcell = newrow.insertCell(1);
	newcell.style.verticalAlign = 'top';
	newcell.innerHTML = '<a href="" class="tc" onClick="tc_remove('+id+', \''+name+'\'); return false;">'+LNG_TRADE_REMOVE+' &raquo;</a>';
	if (rows > 0) newcell.style.paddingTop = 3+'px';
	hide_obj.parentNode.innerHTML = '<img src="'+STATIC_IMAGES+'/checked.gif">';
	sh_tradecats();
	cats_to_trade_in[id] = id;
	populateHiddenTCFields();
}

function tc_remove(id, name) {
	tbl = document.getElementById('trade_cats');
	tbl.deleteRow(document.getElementById('tc'+id).rowIndex);
	document.getElementById('tc_cells_'+id).innerHTML = '<a href="" class="tc" onclick="tc_add('+id+', \''+name+'\', this); return false;">'+LNG_TRADE_ADD+' &raquo;</a>';
	if (tbl.rows.length == 0) {
		newrow = tbl.insertRow(0);
		newrow.id = 'tc_none';
		newcell = newrow.insertCell(0);
		newcell.innerHTML = 'None';
		newrow.insertCell(1);
	}
	delete cats_to_trade_in[id];
	populateHiddenTCFields()
}

function populateHiddenTCFields() {
	container = document.getElementById('tc_field_container');
	container.innerHTML = '';
	for (id in cats_to_trade_in) {
		container.innerHTML += '<input type="hidden" name="tc_selected['+id+']" value="'+id+'">';
	}
}

function tcb_add(cat, brand) {
	brand = brand.toString();
	if (brands_to_trade_in[cat] == undefined || brands_to_trade_in[cat] == '') brands_to_trade_in[cat] = brand;
	else {
		arr = brands_to_trade_in[cat].toString().split('-');
		for (i in arr) {
			if (arr[i] == '0') {
				alert(LNG_TRADE_ALL_SELECTED);
				return;
			}
		}

		if (brand == '0') {
			for (i in arr) {
				if (document.getElementById('tcb_'+cat+'_'+arr[i])) {
					document.getElementById('tcb_'+cat+'_'+arr[i]).innerHTML = '<a href="" onclick="tcb_add('+cat+', '+arr[i]+'); return false;" class="tc">'+LNG_TRADE_ADD+' &raquo;</a>';
				}
			}
			brands_to_trade_in[cat] = '0';
		}
		else {
			brands_to_trade_in[cat] += '-'+brand;
		}
	}
	sh_tcbrands(cat);
	cell = document.getElementById('tcb_'+cat+'_'+brand);
	cell.innerHTML = '<a href="" onclick="tcb_remove('+cat+', '+brand+'); return false;" class="tcred">'+LNG_TRADE_REMOVE+' &raquo;</a>';
	tcb_cache[cat] = cell.parentNode.parentNode.parentNode.parentNode.parentNode.innerHTML;
	populateHiddenTCBFields();
}

function tcb_remove(cat, brand) {
	brand = brand.toString();
	arr = brands_to_trade_in[cat].toString().split('-');
	var newstr = '';
	for (i=0; i<arr.length; i++) {
		if (arr[i] != brand) {
			newstr += arr[i]+'-';
		}
	}
	newstr = newstr.substr(0,newstr.length-1);
	
	if (newstr == '') delete brands_to_trade_in[cat];
	else brands_to_trade_in[cat] = newstr;
	
	sh_tcbrands(cat);
	cell = document.getElementById('tcb_'+cat+'_'+brand);
	cell.innerHTML = '<a href="" onclick="tcb_add('+cat+', '+brand+'); return false;" class="tc">'+LNG_TRADE_ADD+' &raquo;</a>';
	tcb_cache[cat] = cell.parentNode.parentNode.parentNode.parentNode.parentNode.innerHTML;
	populateHiddenTCBFields();
}

function populateHiddenTCBFields() {
	container = document.getElementById('tcb_field_container');
	container.innerHTML = '';
	for (id in brands_to_trade_in) {
		container.innerHTML += '<input type="hidden" name="tcb_selected['+id+']" value="'+brands_to_trade_in[id]+'">';
	}
}

