function toggleDiv(divName)
{
	var d = document.getElementById(divName);
	if (d.style.display == 'none') {d.style.display = 'block'}
	else {d.style.display = 'none'}
	var l = document.getElementById(divName+"link");
	if (l.className == 'section-open') {l.className = 'section-close'}
	else {l.className = 'section-open';}
}

function toggleTabs(tabRef, numTabs, numThis)
{
	var tabLabel = tabRef + "-label-";
	var tabBlock = tabRef + "-block-";
	
	for (var i=1; i<(numTabs+1); i++)
	{
		document.getElementById(tabLabel+i).className = "off";
		document.getElementById(tabBlock+i).className = "hidden";
	}
	document.getElementById(tabLabel+numThis).className = "on";
	document.getElementById(tabBlock+numThis).className = "block";
}

function scrollThumbs(refFrame, dir)
{
	var offset = 200;	// value that divides into max evenly
	var max = 10000;
	var frame = document.getElementById(refFrame);
	if (frame.style.left == '') {frame.style.left = "0px"}	// initialize
	if (dir == 'L')		// dir can be 'l' for left or 'r' for right
	{
		var pos = parseInt(frame.style.left);
		if (pos != 0) {pos = pos+offset}
		frame.style.left = pos+"px";
	}
	else
	{
		var pos = parseInt(frame.style.left);
		if (pos != max) {pos = pos-offset}
		frame.style.left = pos+"px";
	}
}

function viewThumb(imgMainSrc, imgFullSrc, imgCaption, imgAlt)
{
	var photoMain = document.getElementById('photo-main');
	var photoLink = document.getElementById('photo-link');
	var photoCap = document.getElementById('photo-caption');

	photoMain.src = imgMainSrc;
	photoMain.alt = imgAlt;
	photoCap.innerHTML = imgCaption;
	photoLink.title = imgCaption;
	photoLink.href = imgFullSrc;
}

// Copied over from TruckCustomizers v3
function getObj(name)
{
	if (document.layers)
		return document.layers[name];
	if (document.getElementById)
		return document.getElementById(name);
	if (document.all)
		return document.all[name];
}

function check_email(e)
{
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	for(i=0; i < e.length ;i++)
		if(ok.indexOf(e.charAt(i))<0)
			return (false);

	if (document.images)
	{
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two))
			return (-1);
	}
}
function padRight(string, newlength)
{
  var pad = "";
  var len = newlength-String(string).length;
  for (i = 0; i<len; i++)
  	pad += " ";

  return string + pad;
}

function setCaretAtEnd (field)
{
  if (field.createTextRange)
  {
    var r = field.createTextRange();
    r.moveStart('character', field.value.length);
    r.collapse();
    r.select();
  }
}

function replaceAll(source, search, replace)
{
	temp = source;
	while (temp.indexOf(search)>-1)
	{
		pos= temp.indexOf(search);
		temp = "" + (temp.substring(0, pos) + replace + temp.substring((pos + search.length), temp.length));
	}
	return temp;
}

function formatMoney(num)
{
	if (isNaN(num))
		return 0;
	neg = num < 0;
	num = Math.abs(num);

	// USE BANKING ALGORITHM
	thous = Math.floor(num*1000) % 10; // + 0.5);
	hund = Math.floor(num*100) % 10;

	if (thous == 5 && (hund % 2 == 0))
		num -= .005;

	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 (neg ? "-" : "") + num + '.' + cents;	
}

var popup = '';
function openPopup(url)
{
	if (popup.location && !popup.closed)
		popup.focus();
	else
   		popup=window.open(url,'popup','width=590,height=445,resizable=1');
}

function closePopup()
{
	if (popup.location && !newwindow.closed) 
		popup.close();
}

function openNewWin(winURL, winFeatures)
{
	defFeatures="menubar=no,resizable=yes,width=760,height=575,top=100,left=100,scrollbars=yes,status=no";

	if (winFeatures == "")
		win = open(winURL,"newwin");
	else if (winFeatures == "popup")
		win = open(winURL,"newwin",defFeatures);
	else
		win = open(winURL,"newwin",winFeatures);
}
