function callInProgress (xmlhttp) {
	switch (xmlhttp.readyState) {
		case 1: case 2: case 3:
			return true;
			break;
		// Case 4 and 0
		default:
			return false;
			break;
	}
}
function updateProgData(method, id, returnto) {
	var url = 'progress_populate.php';
	var myAjax = new Ajax.Request( url, {
		method: 'post',
		parameters: {q: method, v: id, field: returnto},
		onSuccess: function(Response) {
			$(returnto).innerHTML = Response.responseText;
		}
	});
}
function show_all_job_progress(lcs) {
	var IDtoUpdate = 'jobProgress'+lcs;
	var url = 'ajax_calls.php';
	var myAjax = new Ajax.Request( url, {
		method: 'post',
		parameters: {q: 'show_all_job_progress', lcs: lcs},
		onSuccess: function(Response) {
			$(IDtoUpdate).innerHTML = Response.responseText;
		}
	});
}
function show_latest_job_progress(lcs) {
	var IDtoUpdate = 'jobProgress'+lcs;
	var url = 'ajax_calls.php';
	var myAjax = new Ajax.Request( url, {
		method: 'post',
		parameters: {q:'show_latest_job_progress', lcs: lcs},
		onSuccess: function(Response) {
			$(IDtoUpdate).replace(Response.responseText);
		}
	});
}
function progressHover(box) {
	box.style.width = '200px';
	box.style.overflowX = 'visible';
}
function progressUnHover(box) {
	box.style.width = '130px';
	box.style.overflowX = 'hidden';
}

function setInShop(lcs) {
	document.location.href='work_schedule.php?JobToSetAsInShop='+lcs;
}
var checkAjax;
function checkLCS(lcs) {
	var url = 'ajax_calls.php';
	if (checkAjax)
		if (callInProgress(checkAjax.transport))
			checkAjax.transport.abort();
	if (lcs.value > 9000) {
		checkAjax = new Ajax.Request( url, {
			method: 'post',
			parameters: {q:'checkLCS', lcs: lcs.value},
			onSuccess: function(Response) {
				lcs.style.backgroundImage = "";
				if (Response.responseText == 'true')
					$('sidebar').submit();
				else
					$('isValidLCSErrorMessage').innerHTML = "invalid LCS#";
			}
		});
		lcs.style.background = "#F0E8D7 url(images/loading.gif) no-repeat center";
	}
}
function admin_delete_confirm(lcs) {
	var answer = confirm('Are you sure that you want to delete this job (lcs#'+lcs+')? All data in the job will be deleted.');
	if (answer)
		return true;
	alert('This job will not be deleted');
	return false;
}
function open_new_window(url) {
	new_window = window.open(url,'window_name','resizable=0,width=200,height=60,left=80,top=80,scrollbars=0')
}
function open_help_window(url,w,h) {
	new_window = window.open(url,'help','resizable=0,width='+w+',height='+h+',left=80,top=80,scrollbars=0')
}
function findSelected(select, value) {
	for (i=0; i < select.options.length; i++) {
		if (select.options[i].text == value) {
			select.selectedIndex = i;
			break;
		}
	}
}

function setInitial() {
	$('whatselector').value = initial_what;
	whatOnChange();
	findSelected($('woodselector'),initial_wood);
	findSelected($('styleselector'),initial_style);
	//styleOnChange();
	findSelected($('panelselector'),initial_panel);
	findSelected($('stileRailselector'),initial_stile);
	findSelected($('edgeselector'),initial_edge);
}
function whatOnChange() {

	if(document.getElementById("whatselector").options[0].text == "--select--") {
		document.getElementById("whatselector").remove(0)
	}
	getWoods();
	getStyles();
	getStileAndRail();

/*	$("LeftStileBox").style.display = 'inline';
	$("RightStileBox").style.display = 'inline';
	$("SBSBox").style.display = 'inline';
	$("DBLBox").style.display = 'inline';
	$('stilerailboxmessage').innerHTML="";
	//set each of the stiles and rails and the sbs and dbls to the default of 2 1/2
	$('TopRail').value="2 1/2";
	$('BottomRail').value="2 1/2";
	$('LeftStile').value="2 1/2";
	$('RightStile').value="2 1/2";
	$('SBS').value="2 1/2";
	$('DBL').value="2 1/2";
	if($("whatselector").options[$("whatselector").selectedIndex].text == "Breadboard") {
		//that means that we need to get rid of the stile options and show only top and bottom rail options
		$("LeftStileBox").style.display = 'none';
		$("RightStileBox").style.display = 'none';
		$("SBSBox").style.display = 'none';
		$("DBLBox").style.display = 'none';
		$('LeftStile').value=0;
		$('RightStile').value=0;
		$('SBS').value=0;
		$('DBL').value=0;
		$('stilerailboxmessage').innerHTML="<br /><div align=center>A Breadboard always has a Maple panel, but the top and bottom rails will be of whatever woodtype you select above</div>";
	}
	if (perp_grain[$('whatselector').value] == true) {
		if (typeof(valid) != 'undefined')
			valid.useJS('width_height_explain.html');
	}
	styleOnChange();*/
}
function srOnChange() {
	if ($('styleselector').value == 18) {
	if (!initial_edge)
		initial_edge = $('edgeselector').value;
	getEdges();
	}
}
function getWoods() {
	wreturnto = 'woodselector';
	$(wreturnto).disabled = true;
	if ($('whatselector').value != '') {
		$(wreturnto).style.background = 'url(images/loading.gif) no-repeat center';
		var url = 'dynamic_list.php';
		var WoodAjax = new Ajax.Request( url, {
			method: 'post',
			parameters: {action: 'getWoods', what: $('whatselector').value, initial: initial_wood, field: wreturnto},
			onSuccess: function(Response) {
				$(wreturnto).style.background = '';
				$(wreturnto).disabled = false;
				$(wreturnto).replace(Response.responseText);
				if (initial_wood) {
					$(wreturnto).value = initial_wood;
					$(wreturnto).onchange();
				}
				swapImage($(wreturnto));
				getPanels();
				getPricingData();

			}
		});
	}
}
function getStyles() {
	sreturnto = 'styleselector';
	$(sreturnto).disabled = true;
	if ($('whatselector').value != '') {
		$(sreturnto).style.background = 'url(images/loading.gif) no-repeat center';
		var url = 'dynamic_list.php';
		var StyleAjax = new Ajax.Request( url, {
			method: 'post',
			parameters: {action: 'getStyles', what: $('whatselector').value, field: sreturnto},
			onSuccess: function(Response) {
				$(sreturnto).style.background = '';
				$(sreturnto).disabled = false;
				$(sreturnto).replace(Response.responseText);
				if (initial_style) {
					$(sreturnto).value = initial_style;
					$(sreturnto).onchange();
				}
// 	                        swapImage($(sreturnto));
				getEdges();
// 				getPricingData();
			}
		});
	}
}
function getStileAndRail() {
	srreturnto = 'stileRailselector';
	$(srreturnto).disabled = true;
	if ($('whatselector').value != '') {
		$(srreturnto).style.background = 'url(images/loading.gif) no-repeat center';
		var url = 'dynamic_list.php';
		var StileAjax = new Ajax.Request( url, {
			method: 'post',
			parameters: {action: 'getStile', what: $('whatselector').value, field: srreturnto},
			onSuccess: function(Response) {
				$(srreturnto).style.background = '';
				$(srreturnto).disabled = false;
				$(srreturnto).replace(Response.responseText);
				if (initial_stile) {
					$(srreturnto).value = initial_stile;
					$(srreturnto).onchange();
				}
/*	                        swapImage($(srreturnto));
				getPricingData();*/
			}
		});
	}
}
function getPanels() {
	returnto = 'panelselector';
	$(returnto).disabled = true;
	if ($('woodselector').value != '') {
		$(returnto).style.background = 'url(images/loading.gif) no-repeat center';
		var url = 'dynamic_list.php';
		var myAjax = new Ajax.Request( url, {
			method: 'post',
			parameters: {action: 'getPanels', what: $('whatselector').value, wood: $('woodselector').value, field: returnto},
			onSuccess: function(Response) {
				$(returnto).style.background = '';
				$(returnto).disabled = false;
				$(returnto).replace(Response.responseText);
				if (initial_panel) {
					$(returnto).value = initial_panel;
					$(returnto).onchange();
				}
/*	                        swapImage($(returnto));
				getPricingData();*/
			}
		});
	}
}
function getEdges() {
	ereturnto = 'edgeselector';
	$(ereturnto).disabled = true;
	if ($('styleselector').value != '') {
		$(ereturnto).style.background = 'url(images/loading.gif) no-repeat center';
		var url = 'dynamic_list.php';
		var EdgeAjax = new Ajax.Request( url, {
			method: 'post',
			parameters: {action: 'getEdges', what: $('whatselector').value, style: $('styleselector').value, sr: $('stileRailselector').value, field: ereturnto},
			onSuccess: function(Response) {
				$(ereturnto).style.background = '';
				$(ereturnto).disabled = false;
				$(ereturnto).replace(Response.responseText);
				if (initial_edge) {
					$(ereturnto).value = initial_edge;
				}
/*				swapImage($(ereturnto));
				getPricingData();*/
			}
		});
	}
}
function delAccount(id) {
	var answer = confirm("Deleting this account will remove all payments associated with it. Are you sure you wish to continue?");
	if (answer) {
		document.location.href='bank_accounts.php?delAccount=' + id;
	}
}
function checkUncheckAll(theElement, theClass) {
var theForm = theElement.form, z = 0;
while (theForm[z]) {
	if (theForm[z].name != 'checkall' && theForm[z].type == 'checkbox' && theForm[z].className == theClass)
		theForm[z].checked = theElement.checked;
	z++;
}
}
function editLine(id) {
// 	alert('prow'+id);
	$('prow'+id).show();
	$('qty'+id).setAttribute('onfocus','');
	$('width'+id).setAttribute('onfocus','');
	$('length'+id).setAttribute('onfocus','');
	$('wide'+id).setAttribute('onfocus','');
	$('long'+id).setAttribute('onfocus','');
}
function insertPanelRow(id) {
//	var panelcount = $('panelset'+id).childElements().length;
	var newrow = document.createElement('tr');
	var newqtytd = document.createElement('td');
	var newqtybox = document.createElement('input');
	newqtybox.setAttribute('name','pqty'+id+'[]');
	newqtybox.setAttribute('size','1');
	newqtybox.setAttribute('type','text');
	newqtytd.appendChild(newqtybox);
	newrow.appendChild(newqtytd);

	var newwidthtd = document.createElement('td');
	var newwidthbox = document.createElement('input');
	newwidthbox.setAttribute('name','pwidth'+id+'[]');
	newwidthbox.setAttribute('size','5');
	newwidthbox.setAttribute('type','text');
	newwidthtd.appendChild(newwidthbox);
	newrow.appendChild(newwidthtd);

	var newlengthtd = document.createElement('td');
	var newlengthbox = document.createElement('input');
	newlengthbox.setAttribute('name','plength'+id+'[]');
	newlengthbox.setAttribute('size','5');
	newlengthbox.setAttribute('type','text');
	newlengthtd.appendChild(newlengthbox);
	newrow.appendChild(newlengthtd);

	var newdeltd = document.createElement('td');
	var newdelbox = document.createElement('input');
	newdelbox.setAttribute('value','-');
	newdelbox.setAttribute('onclick','delPartRow(this)');
	newdelbox.setAttribute('type','button');
	newdeltd.appendChild(newdelbox);
	newrow.appendChild(newdeltd);

	$('panelset'+id).appendChild(newrow);
}
function insertStileRow(id) {
//	var stilecount = $('stileset'+id).childElements().length;
	var newrow = document.createElement('tr');
	var newqtytd = document.createElement('td');
	var newqtybox = document.createElement('input');
	newqtybox.setAttribute('name','sqty'+id+'[]');
	newqtybox.setAttribute('size','1');
	newqtybox.setAttribute('type','text');
	newqtytd.appendChild(newqtybox);
	newrow.appendChild(newqtytd);

	var newlengthtd = document.createElement('td');
	var newlengthbox = document.createElement('input');
	newlengthbox.setAttribute('name','slength'+id+'[]');
	newlengthbox.setAttribute('size','5');
	newlengthbox.setAttribute('type','text');
	newlengthtd.appendChild(newlengthbox);
	newrow.appendChild(newlengthtd);

	var newtypetd = document.createElement('td');
	var newtypebox = document.createElement('select');
	newtypebox.setAttribute('name','stype'+id+'[]');
	newtypebox.innerHTML = "<option value='both'>Both</option><option value='left'>Left</option><option value='right'>Right</option>";
	newtypetd.appendChild(newtypebox);
	newrow.appendChild(newtypetd);

	var newdeltd = document.createElement('td');
	var newdelbox = document.createElement('input');
	newdelbox.setAttribute('value','-');
	newdelbox.setAttribute('onclick','delPartRow(this)');
	newdelbox.setAttribute('type','button');
	newdeltd.appendChild(newdelbox);
	newrow.appendChild(newdeltd);

	$('stileset'+id).appendChild(newrow);
//	alert(sitlecount);
}
function insertRailRow(id) {
//	var railcount = $('railset'+id).childElements().length;
	var newrow = document.createElement('tr');
	var newqtytd = document.createElement('td');
	var newqtybox = document.createElement('input');
	newqtybox.setAttribute('name','rqty'+id+'[]');
	newqtybox.setAttribute('size','1');
	newqtybox.setAttribute('type','text');
	newqtytd.appendChild(newqtybox);
	newrow.appendChild(newqtytd);

	var newlengthtd = document.createElement('td');
	var newlengthbox = document.createElement('input');
	newlengthbox.setAttribute('name','rlength'+id+'[]');
	newlengthbox.setAttribute('size','5');
	newlengthbox.setAttribute('type','text');
	newlengthtd.appendChild(newlengthbox);
	newrow.appendChild(newlengthtd);

	var newtypetd = document.createElement('td');
	var newtypebox = document.createElement('select');
	newtypebox.setAttribute('name','rtype'+id+'[]');
	newtypebox.innerHTML = "<option value='both'>Both</option><option value='top'>Top</option><option value='bottom'>Bottom</option>";
	newtypetd.appendChild(newtypebox);
	newrow.appendChild(newtypetd);

	var newdeltd = document.createElement('td');
	var newdelbox = document.createElement('input');
	newdelbox.setAttribute('value','-');
	newdelbox.setAttribute('onclick','delPartRow(this)');
	newdelbox.setAttribute('type','button');
	newdeltd.appendChild(newdelbox);
	newrow.appendChild(newdeltd);

	$('railset'+id).appendChild(newrow);
	//alert(railcount);
}
function insertSBSRow(id) {
//	var sbscount = $('sbsset'+id).childElements().length;
	var newrow = document.createElement('tr');
	var newqtytd = document.createElement('td');
	var newqtybox = document.createElement('input');
	newqtybox.setAttribute('name','sbsqty'+id+'[]');
	newqtybox.setAttribute('size','1');
	newqtybox.setAttribute('type','text');
	newqtytd.appendChild(newqtybox);
	newrow.appendChild(newqtytd);

	var newlengthtd = document.createElement('td');
	var newlengthbox = document.createElement('input');
	newlengthbox.setAttribute('name','sbslength'+id+'[]');
	newlengthbox.setAttribute('size','5');
	newlengthbox.setAttribute('type','text');
	newlengthtd.appendChild(newlengthbox);
	newrow.appendChild(newlengthtd);

	var newdeltd = document.createElement('td');
	var newdelbox = document.createElement('input');
	newdelbox.setAttribute('value','-');
	newdelbox.setAttribute('onclick','delPartRow(this)');
	newdelbox.setAttribute('type','button');
	newdeltd.appendChild(newdelbox);
	newrow.appendChild(newdeltd);

	$('sbsset'+id).appendChild(newrow);
}
function insertDBLRow(id) {
//	var dblcount = $('dblset'+id).childElements().length;
	var newrow = document.createElement('tr');
	var newqtytd = document.createElement('td');
	var newqtybox = document.createElement('input');
	newqtybox.setAttribute('name','dblqty'+id+'[]');
	newqtybox.setAttribute('size','1');
	newqtybox.setAttribute('type','text');
	newqtytd.appendChild(newqtybox);
	newrow.appendChild(newqtytd);

	var newlengthtd = document.createElement('td');
	var newlengthbox = document.createElement('input');
	newlengthbox.setAttribute('name','dbllength'+id+'[]');
	newlengthbox.setAttribute('size','5');
	newlengthbox.setAttribute('type','text');
	newlengthtd.appendChild(newlengthbox);
	newrow.appendChild(newlengthtd);

	var newdeltd = document.createElement('td');
	var newdelbox = document.createElement('input');
	newdelbox.setAttribute('value','-');
	newdelbox.setAttribute('onclick','delPartRow(this)');
	newdelbox.setAttribute('type','button');
	newdeltd.appendChild(newdelbox);
	newrow.appendChild(newdeltd);

	$('dblset'+id).appendChild(newrow);
}
function delPartRow(el) {
	el.up('tr').remove();
}
function deleteLine(box) {
	box.up('tr').remove();
}
