

var theRequest = false;
var total_upload_size = 1;
var force_KB_size = 0
var force_KB_rate = 1

function goajax(page)
{
	theRequest = false;

	if(window.XMLHttpRequest)
	{
		theRequest = new XMLHttpRequest();
		if(theRequest.overrideMimeType)
		{
			theRequest.overrideMimeType('text/xml');
		}
	}
	else if(window.ActiveXObject)
	{
		try
		{
			theRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try
			{
				theRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if(!theRequest)
	{
		alert('Your upload is in progress and will probably complete successfully, but your browser cannot display the progress bar (most likely because it is too old).  Please wait while your upload completes.');
		return false;
	}

	theRequest.onreadystatechange = updateProgress;
	theRequest.open('GET', page, true);
	theRequest.send(null);
}

function updateProgress()
{
	if(theRequest)
	{
		if(theRequest.readyState == 4)
		{
			if(theRequest.status == 200)
			{
				

				var update = new Array();
				update = theRequest.responseText.split('|');

				if(update[1] != 0)
				{
					total_upload_size = update[1];
				}

				if(update[5] == 'toobig')
				{
					location.href="http://www.bigswing.us/clients/?error=toobig&size=" + total_upload_size;
				}

				var completed_upload_size = update[0];
				var elapsedtime = update[2];
				var numfinishedfiles = update[3];
				var numtotalfiles = update[4];

				if(isNum(total_upload_size) && isNum(completed_upload_size) && isNum(elapsedtime) && isNum(numfinishedfiles) && isNum(numtotalfiles))
				{
					document.getElementById('progStatus').innerHTML = 'Uploading; please wait.';

					var progressPercent = Math.ceil((completed_upload_size/total_upload_size)*100);
					if(isNum(progressPercent)) { document.getElementById('progPercent').innerHTML = progressPercent + '%'; }

					var barwidth = parseInt(progressPercent*250/100);
					if(isNum(barwidth)) { document.getElementById('progBarDone').style.width = barwidth + 'px'; }

					var totaltime = parseInt((elapsedtime * 100) / progressPercent);
					var totaltime_forprint = format_timespan_with_unit(totaltime, '&nbsp;');
					var remainingtime_forprint = format_timespan_with_unit(eval(totaltime - elapsedtime), '&nbsp;');
					var elapsedtime_forprint = format_timespan_with_unit(elapsedtime, '&nbsp;');

					var force_MB = total_upload_size > 999999 ? 1 : 0;
					var total_upload_size_forprint = format_filesize_with_unit(total_upload_size, '&nbsp;', force_MB, force_KB_size);
					var remaining_upload_size_forprint = format_filesize_with_unit(total_upload_size - completed_upload_size, '&nbsp;', force_MB, force_KB_size);
					var completed_upload_size_forprint = format_filesize_with_unit(completed_upload_size, '&nbsp;', force_MB, force_KB_size);

					var transfer_rate = format_filesize_with_unit(completed_upload_size/elapsedtime, '&nbsp;', force_MB, force_KB_rate);

					if((completed_upload_size != "")   &&   (completed_upload_size != 0))
					{

						document.getElementById('progRate').innerHTML = transfer_rate + '/s';
					}

					if(progressPercent == 100)
					{
						document.getElementById('theMeter').style.position = 'absolute';
						document.getElementById('theMeter').style.left = '-10000px';
						document.getElementById('theMeter').style.overflow = 'hidden';
						document.getElementById('theMeter').style.height = '0';

						document.getElementById('uploadCompleteMsg').style.position = 'relative';
						document.getElementById('uploadCompleteMsg').style.left = '0';
						document.getElementById('uploadCompleteMsg').style.height = 'auto';
						document.getElementById('uploadCompleteMsg').innerHTML = 'Upload complete; the server is now processing your file(s).&nbsp; This could take a minute or two if your upload was very big.&nbsp; Please wait.';

						return null;
					}
				}

				var timeout = navigator.userAgent.indexOf("Safari") == -1 ? 700 : 2200;
				window.setTimeout("goajax('" + document.getElementById('theuploadform').action + "&action=get_progress_and_size')", timeout);
			}
			else
			{
				
			}
		}
	}
}



function startupload()
{
	if(check_for_required_fields())
	{
		document.getElementById('theuploadform').submit();
		document.getElementById('progBarContainer').style.position = 'relative';
		document.getElementById('progBarContainer').style.left = '0';
		document.getElementById('progBarContainer').style.height = 'auto';
		document.getElementById('uploadbutton').disabled = true;



	document.getElementById('theuploadform').style.position = 'absolute';
	document.getElementById('theuploadform').style.left = '-10000px';
	document.getElementById('theuploadform').style.overflow = 'hidden';
	document.getElementById('theuploadform').style.height = '0';
	document.getElementById('theuploadform').style.display = 'none'; /* IE doesn't properly hide everything under HTML 4.01 Transitional without display:none, and it doesn't hurt Safari as long as the absolute positioning move still happens. */

		var timeout = navigator.userAgent.indexOf("Safari") == -1 ? 1200 : 4200;
		window.setTimeout("goajax('/cgi-bin/filechucker/filechucker.cgi?js&serial=12839944756672381071919310&action=get_progress_and_size')", timeout);
	}
}


function startorder()
{
	var inputs = document.getElementById('theorderform').getElementsByTagName('input');
	var missing = 0;
	var i = 0;
	for(i = 0; i < inputs.length; i++)
	{
		if(inputs[i].className.indexOf('required') != -1   &&   (inputs[i].value == '' || inputs[i].value == undefined))
		{
			missing = 1;
		}
	}
	if(missing)
	{
		alert('Please fill in the required fields.');
	}
	else
	{
		document.getElementById('theorderform').submit();
	}
}


function check_for_required_fields()
{
	var onlyinputs = document.getElementById('theuploadform').getElementsByTagName('input');
	var selects = document.getElementById('theuploadform').getElementsByTagName('select');
	var textareas = document.getElementById('theuploadform').getElementsByTagName('textarea');
	var inputs = new Array;
	var i = 0;
	for(i = 0; i < onlyinputs.length; i++)
	{
		inputs[i] = onlyinputs[i];
	}
	var j = 0;
	for(j = 0; j < selects.length; j++)
	{
		inputs[i + j] = selects[j];
	}
	var k = 0;
	for(k = 0; k < textareas.length; k++)
	{
		inputs[i + j + k] = textareas[k];
	}

	var items_missing = 0;
	var email_format_incorrect = 0;

	for(i = 0; i < inputs.length; i++)
	{
		if(inputs[i].className.indexOf('required') != -1   &&   (inputs[i].value == '' || inputs[i].value == undefined))
		{
			inputs[i].style.background	= '#ffb07b';
			inputs[i].style.color		= '#000';
			items_missing = 1;
		}
		else if(inputs[i].className.indexOf('emailformat') != -1   &&   !inputs[i].value.match( /.+@.+\..+/ ))
		{
			inputs[i].style.background	= '#ffb07b';
			inputs[i].style.color		= '#000';
			email_format_incorrect = 1;
		}
		else
		{
			inputs[i].style.background	= '#ffffff';
			inputs[i].style.color		= '#000';
		}
	}

	if(items_missing)
	{
		alert("Please fill in the required item(s).");
	}
	else if(email_format_incorrect)
	{
		alert("Please enter a valid email address.");
	}
	else
	{
		return 1;
	}

	return 0;
}




function format_filesize_with_unit(num,space,forceMB,forceKB)
{
	if(!isNum(num,1)) { return "?" + space + "KB"; }

	var unit;
	if(   ((num > 999999)  ||  forceMB)   &&   !forceKB)
	{
		num = num/(1024*1024);
		num = num.toString();

		var testnum = num.replace( /^(\d+\.\d).*/, '$1' ); // show 1 decimal place. // extra escaping b/c printing JS from Perl.

		if(testnum == '0.0')
		{
			testnum = num.replace( /^(\d+\.\d\d).*/, '$1' ); // show 2 decimal places.
		}
		if(testnum == '0.00')
		{
			testnum = num.replace( /^(\d+\.\d\d\d).*/, '$1' ); // show 3 decimal places.
		}
		num = testnum;

		unit = 'MB';
	}
	else
	{
		num = parseInt(num/(1024));
		unit = 'KB';
	}
	return num + space + unit;
}

function format_timespan_with_unit(num,space)
{
	if(!isNum(num)) { return "00:00:00"; }

	if(num >= (60*60))
	{
		var secs_left = num % (60*60);
		var mins_left = secs_left / 60;
		mins_left = mins_left.toString();
		mins_left = mins_left.replace( /^(\d+)\..*/, '$1' ); // show no decimal places.  // extra escaping b/c printing JS from Perl.
		mins_left = mins_left.replace( /^(\d)$/, '0$1' ); // for single-digits, prepend a zero.

		num = num/(60*60);
		num = num.toString();
		num = num.replace( /^(\d+)\..*/, '$1' ); // show no decimal places.

		num = num + ':' + mins_left + ':00';
	}
	else if(num >= 60)
	{
		var secs_left = num % 60;
		secs_left = secs_left.toString().replace( /^(\d)$/, '0$1' ); // for single-digits, prepend a zero.

		num = num/60;
		num = num.toString();
		num = num.replace( /^(\d+)\..*/, '$1' ); // show no decimal places.  // extra escaping b/c printing JS from Perl.
		num = num.replace( /^(\d)$/, '0$1' ); // for single-digits, prepend a zero.

		num = '00:' + num + ':' + secs_left;
	}
	else
	{
		num = num.toString();
		num = num.replace( /^(\d+)\..*/, '$1' ); // show no decimal places. // extra escaping b/c printing JS from Perl.
		num = num.replace( /^(\d)$/, '0$1' ); // for single-digits, prepend a zero.
		num = '00:00:' + num;
	}
	return num;
}

function isNum(testval,decimalsOK)
{
	if(typeof(testval) == 'undefined') return false;
	testval = testval.toString();
	if (!testval.length) return false;
	var numbers = decimalsOK ? '.0123456789' : '0123456789';
	for (i=0; i<testval.length; i++)
	{
		if (numbers.indexOf(testval.charAt(i),0) == -1) return false;
	}
	return true;
}

function update_form_fields()
{
	location.href="http://www.bigswing.us/clients/?js&items=" + document.getElementById("itemcount").value;
}

function set_cookie(name, value, hours_to_live, path, domain, secure)
{
	var expireDate = "";
	if(hours_to_live)
	{
		expireDate = (new Date((new Date()).getTime() + hours_to_live*3600000)).toGMTString();
	}

	var curCookie = name + "=" + escape(value) +
	((hours_to_live) ? "; expires=" + expireDate : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");

	document.cookie = curCookie;
}


function get_cookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}


function delete_cookie(name, path, domain)
{
	if(get_cookie(name))
	{
		document.cookie = name + "=" + 
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
	else
	{
		alert('Your cart is empty.');
	}
}



function select_item(newvalue)
{
	var set_it = 1;
	var oldvalue = get_cookie('selected_items');
	if(oldvalue)
	{
		if(oldvalue.indexOf(newvalue) != -1)
		{
			if(window.confirm('This item is already in your cart.  Add it again?'))
			{
				set_it = 1;
			}
			else
			{
				set_it = 0;
			}
		}

		newvalue = oldvalue + ':|:|:' + newvalue;
	}
	if(set_it)
	{
		set_cookie('selected_items', newvalue, 168, '/');
	}
}


function clear_selections()
{
	if(get_cookie('selected_items'))
	{
		if(window.confirm('About to empty your cart.  Is that OK?'))
		{
			delete_cookie('selected_items', '/');
		}
	}
	else
	{
		alert('Your cart is empty.');
	}
}


function schedule_onload_action(newfunc)
{
	var already_scheduled = window.onload;
	if(typeof window.onload != 'function')
	{
		window.onload = newfunc;
	}
	else
	{
		window.onload = function()
		{
			already_scheduled();
			newfunc();
		}
	}
}


function set_row_mouseovers()
{
	if(document.getElementById("filelist"))
	{
		var filelist_rows = document.getElementById("filelist").getElementsByTagName("tr");
		var i = 0;
		for(i = 0; i < filelist_rows.length; i++)
		{
			filelist_rows[i].onmouseover = setbg;

			if((i % 2)==0)
			{
				filelist_rows[i].onmouseout  = unsetbgeven;
			}
			else
			{
				filelist_rows[i].onmouseout  = unsetbgodd;
			}
		}
	}
}

function setbg()
{
	this.style.background = '#507090';
	//this.style.border = '';


	var tds = this.getElementsByTagName("td");
	var i = 0;
	for(i = 0; i < tds.length; i++)
	{
		tds[i].style.color = 'white';
		if(tds[i].getElementsByTagName("a"))
		{
			var links = tds[i].getElementsByTagName("a");
			var j = 0;
			for(j = 0; j < links.length; j++)
			{
				links[j].style.color = 'white';
			}
		}
	}

}

function unsetbgeven()
{
	this.style.background = '#efefef';
	//this.style.border = '';
	unsettext(this);
}

function unsetbgodd()
{
	this.style.background = '#e6e6e6';
	//this.style.border = '';
	unsettext(this);
}

function unsettext(myself)
{
	var tds = myself.getElementsByTagName("td");
	var i = 0;
	for(i = 0; i < tds.length; i++)
	{
		tds[i].style.color = 'black';
		if(tds[i].getElementsByTagName("a"))
		{
			var links = tds[i].getElementsByTagName("a");
			var j = 0;
			for(j = 0; j < links.length; j++)
			{
				links[j].style.color = 'black';
			}
		}
	}
}


schedule_onload_action(set_row_mouseovers);

