var stop_loop = true;

url = document.location.href;
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);

var ajax_get_error = false;

function ajax_do (url) 
{
	// Does URL begin with http?
	if (url.substring(0, 4) != 'http') { url = base_url + url; }

	// Create new JS element
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = url;

	// Append JS element (therefore executing the 'AJAX' call)
	document.body.appendChild (jsel);

	return true;
}

function load_product_combos(product_combo)
{
	var product_price = product_combo.options[product_combo.options.selectedIndex].getAttribute('price');
	var	product_size = document.getElementById('option_0').options[document.getElementById('option_0').options.selectedIndex].value;
	ajax_do('product_info.ajx.php?pid='+product_combo.value+'&pprice='+product_price+'&psize='+product_size);
}

function load_product_combos2(product_combo,cont)
{
	var product_price = product_combo.options[product_combo.options.selectedIndex].getAttribute('price');
	var product_newprice = product_combo.options[product_combo.options.selectedIndex].getAttribute('new_price');

	if(cont == 1)
	{
		if(document.getElementById('0').className == 'active')
		{
			var obj = document.getElementById('1');
		}
		else
		{
			var obj = document.getElementById('0');	
		}

		change_tab(obj,0);
	}

	document.getElementById('pid').value = product_combo.value;
	document.getElementById('pprice').value = product_price;
	document.getElementById('pnewprice').value = product_price;
	
	document.getElementById('psize').value = document.getElementById('option_0').options[document.getElementById('option_0').options.selectedIndex].value;
	document.IE_form.submit();
}

function size_change(){ 
	load_product_combos2(document.getElementById('product_models'));
}

 
function popupWindow(url) 
{
	window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=640,height=480,screenX=100,screenY=100,top=100,left=100')
}

function change_tab(el,go_on)
{
	//alert(el);
	var product_model_id = el.getAttribute('slt_id');	

	if(el.id > 0)
	{
		document.getElementById('0').className = '';
		el.className = 'active';
		document.getElementById('prod_img').src = aryTabImages[0];
		document.getElementById('tab_gallery').style.display = 'block';
		document.getElementById('main_gallery').style.display = 'none';
		document.getElementById('prod_img').onclick = function() { popupWindow('popup_image.php?pID='+product_model_id+'&img_url='+aryTabImages[0]); return false; }
		document.getElementById('product_models').value = product_model_id;
	}
	else
	{
		document.getElementById('1').className = '';
		el.className = 'active';
		document.getElementById('prod_img').src = aryImages[0];
		document.getElementById('main_gallery').style.display = 'block';
		document.getElementById('tab_gallery').style.display = 'none';
		document.getElementById('prod_img').onclick = function() { popupWindow('popup_image.php?pID='+product_model_id+'&img_url='+aryImages[0]); return false; }
		document.getElementById('product_models').value = product_model_id;
	}

	if(go_on == 1)
	{
		load_product_combos2(document.getElementById('product_models'),0);
	}
}

function calc_price()
{
	var model_el = document.getElementById('product_models');
	var size_el =  document.getElementById('option_0');
	var base_el =  document.getElementById('option_1');
	var rails_el = document.getElementById('option_2');

	var product_id = model_el.options[model_el.options.selectedIndex].value;

	var model_price = model_el.options[model_el.options.selectedIndex].getAttribute('price');
	var model_new_price = model_el.options[model_el.options.selectedIndex].getAttribute('new_price');
	var model_size_price = size_el.options[size_el.options.selectedIndex].getAttribute('price');
	var model_size_text = size_el.options[size_el.options.selectedIndex].getAttribute('text_vit');
	var model_base_price = base_el.options[base_el.options.selectedIndex].getAttribute('price');
	var model_rails_price = rails_el.options[rails_el.options.selectedIndex].getAttribute('price');
	var full_price = parseFloat(model_price) + parseFloat(model_size_price) + parseFloat(model_base_price) + parseFloat(model_rails_price);
	var full_new_price = parseFloat(model_new_price) + parseFloat(model_size_price) + parseFloat(model_base_price) + parseFloat(model_rails_price);
	document.getElementById('product_price_bit').innerHTML = format_currency(full_price);
	document.getElementById('product_new_price_bit').innerHTML = format_currency(full_new_price);
	document.getElementById('product_size_bit').innerHTML = model_size_text;
}

function format_currency(num) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function show_flash()
{
	document.getElementById('img_gallery').style.display = 'none';
	document.getElementById('flash_help').style.display = 'block';
}

function hide_flash()
{
	//document.getElementById('flash_help').innerHTML = '';
	document.getElementById('flash_help').style.display = 'none';
	document.getElementById('img_gallery').style.display = 'block';
}