var currentType='06';
var resultType='01';
var ajax=new Ajax();
function setCountType(countType){
	currentType=countType;
	var listDiv=document.getElementById('selectorDiv');
	if(listDiv.firstChild){
		listDiv.removeChild(listDiv.firstChild);
	}
	listDiv.innerHTML='Please wait...';
	d=new Date();
	t=d.getTime();
	ajax.doGet('o2_CountTool.php?command=getlist2&type='+currentType+'&t='+t, showList);
}
function showList(str){
	var listDiv=document.getElementById('selectorDiv');
	if(listDiv.firstChild){
		listDiv.removeChild(listDiv.firstChild);
	}
	listDiv.innerHTML=str;
}
function selectItem(selID, whichType){
	//var i=document.frmCount.selCountType.selectedIndex;
	var o=document.getElementById(selID);
	i=o.selectedIndex;
	if(i==-1){
		alert('Please select an item');
		return;
	}
	var t=o.options[i].text;
	var v=o.options[i].value;
	var newOption=new Option(t, whichType+v);
	var where=document.frmCount.selections.length;
	document.frmCount.selections.options[where]=newOption;
}
function selectItem2(itemValue, itemText){
	var newOption=new Option(itemText, itemValue);
	var where=document.frmCount.selections.length;
	document.frmCount.selections.options[where]=newOption;
}
function removeItem(){
	var i=document.frmCount.selections.selectedIndex;
	if(i==-1){
		alert('Please select an item to remove');
		return;
	}
	document.frmCount.selections.options[i]=null;
}

function removeAllItems() {
	while(document.frmCount.selections.length>0){
		document.frmCount.selections.options[0]=null;
	}
}

function setResultType(setType){
	resultType=setType;
}

function getResults(){
	//showDownloadResults('');
	var resultDiv=document.getElementById('resultDiv');
	if(resultDiv.firstChild){
		resultDiv.removeChild(resultDiv.firstChild);
	}
	if(document.frmCount.selections.length==0){
		alert('Please make a selection');
		return;
	}
	r='';
	for(i=0;i<document.frmCount.selections.length;++i){
		if(r!=''){
			r+='&';
		}
		r+='selection[]='+escape(document.frmCount.selections.options[i].value);
	}
	emailonly=(document.frmCount.emailonly.checked?document.frmCount.emailonly.value:0);
	resultDiv.innerHTML='Please wait...';
	d=new Date();
	t=d.getTime();
	ajax.doGet('o2_CountTool.php?command=getcount&type='+resultType+'&'+r+'&emailonly='+emailonly+'&t='+t, showResults);
}
function showResults(str){
	var resultDiv=document.getElementById('resultDiv');
	if(resultDiv.firstChild){
		resultDiv.removeChild(resultDiv.firstChild);
	}
	resultDiv.innerHTML=str;
}
function doDownload(){
	if(document.frmCount.selections.length==0){
		alert('Please make a selection');
		return(false);
	}
	var s=document.getElementById("downloadSelections");
	while(s.length>0){
		s.options[0]=null;
	}
	for(i=0;i<document.frmCount.selections.length;i++){
		var newOption=new Option(document.frmCount.selections.options[i].text, document.frmCount.selections.options[i].value);
		newOption.selected=true;
		s.options[i]=newOption;
	}
	document.frmDownload.emailonly.value=(document.frmCount.emailonly.checked?document.frmCount.emailonly.value:0);
	document.frmDownload.submit();
	alert('Your selection is being prepared and will download shortly\nwhere you can save it to your computer\n\nPlease be patient if you have selected a large dataset...\nClick OK now');
}
function showDownloadResults(str){
	var resultDiv=document.getElementById('downloadResults');
	if(resultDiv.firstChild){
		resultDiv.removeChild(resultDiv.firstChild);
	}
	resultDiv.innerHTML=str;
}
function requestQuotation(){
	var quoteDiv=document.getElementById('quoteDiv');
	if(quoteDiv.firstChild){
		quoteDiv.removeChild(quoteDiv.firstChild);
	}
	if(document.frmCount.selections.length==0){
		alert('Please make a selection');
		return;
	}
	r='';
	for(i=0;i<document.frmCount.selections.length;++i){
		if(r!=''){
			r+='&';
		}
		r+='selection[]='+escape(document.frmCount.selections.options[i].value);
	}
	email=document.getElementById('email');
	emailRegExp=/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{1,4}$/i;
	if(trim(email.value)==''){
		alert('Please enter your email address');
		email.focus();
	   return(false);
	}
	else if(!emailRegExp.test(email.value)){ 
		alert('Your email address does not appear to be correctly formatted');
		email.focus();
	   return(false);
	}
	emailonly=(document.frmCount.emailonly.checked?document.frmCount.emailonly.value:0);
	quoteDiv.innerHTML='Please wait...';
	d=new Date();
	t=d.getTime();
	ajax.doGet('o2_CountTool.php?command=sendcountquotation&email='+escape(email.value)+'&emailonly='+emailonly+'&'+r+'&t='+t, showQuoteResults);
}
function showQuoteResults(str){
	var quoteDiv=document.getElementById('quoteDiv');
	if(quoteDiv.firstChild){
		quoteDiv.removeChild(quoteDiv.firstChild);
	}
	quoteDiv.innerHTML=str;
}
function trim(str){
	return(str.replace(/\A\s*(.*\S)\s*\z/g, "\1"));
}

