var xmlhttp;
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
function addEntityToListing(form) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://crimson-veil.com/includes/ajax/addEntity.php";
	url=url+"?eType="+form.entity.value;
	url=url+"&eID="+form.entityTypeID.value;
	url=url+"&eQuant="+form.entityQuantity.value;
	if(form.uom.value == "all") {
		price = form.entityPrice.value.replace(/,/g, '');
	} else {
		price = parseInt(form.entityPrice.value.replace(/,/g, '')) * parseInt(form.entityQuantity.value);
	}
	url=url+"&ePrice="+price;
	if(form.hasNoPower.checked) {
		url=url+"&power=1";
	} else {
		url=url+"&power=0";
	} if(form.docked.checked) {
		url=url+"&docked=1";
	} else {
		url=url+"&docked=0";
	} if(form.damaged.checked) {
		url=url+"&damaged=1";
	} else {
		url=url+"&damaged=0";
	} if(form.shielded.checked) {
		url=url+"&shielded=1";
	} else {
		url=url+"&shielded=0";
	}
	if(form.selectSectors.value != "None") {
		url=url+"&sector="+form.selectSectors.value;
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=updateEntity;
	xmlhttp.send(null);
	document.getElementById("entityLocation").innerHTML='';
	var totalPriceInfo = document.getElementById("listingPrice").innerHTML.replace(/,/g,'');
	totalPriceInfo = parseInt(totalPriceInfo);
	var price = totalPriceInfo + parseInt(form.entityPrice.value);
	if(isNaN(price) == true || price < 0) {
		price = 0;
	}
	document.getElementById("marketStats").innerHTML='';
	document.getElementById("listingPrice").innerHTML='';
	document.getElementById("listingPrice").innerHTML=addCommas(price);
	form.reset();
}
function updateEntity() {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://crimson-veil.com/includes/ajax/updateEntity.php";
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=changeListing;
	xmlhttp.send(null);
} function changeListing() {
	if (xmlhttp.readyState==4 && xmlhttp.status == 200) {
		if(xmlhttp.responseText != '') {
			document.getElementById("entitiesListed").innerHTML=xmlhttp.responseText;
		}
	} else {
		document.getElementById("entitiesListed").innerHTML='<img src="http://crimson-veil.com/images/site/loading_bar.gif" alt="Loading..." />';
	}
}
function removeEntity(str, eprice) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://crimson-veil.com/includes/ajax/removeEntity.php";
	url=url+"?eElement="+str;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=updateEntity;
	xmlhttp.send(null);
	var totalPriceInfo=document.getElementById("listingPrice").innerHTML;
	var price = parseInt(totalPriceInfo)-parseInt(eprice);
	if(isNaN(price) == true || price < 0) {
		price = 0;
	}
	document.getElementById("listingPrice").innerHTML='';
	document.getElementById("listingPrice").innerHTML=addCommas(price);
}
function checkAddListingForm() {
	var form = document.getElementById("listingSubmitForm");
	if(form.listingTitle.value == '') {
		alert("Error: You need a title for your listing.");
		return false;
	} else if(form.listingType.value == '') {
		alert("Error: You need to select a listing type.");
		return false;
	} else if(document.getElementById("entitiesListed").innerHTML == 'None') {
		alert("Error: You need to select at least one Entity before posting a listing.");
		return false;
	} else {
		return true;
	}
}
function bidOnAuction(formobj, accID) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://crimson-veil.com/includes/ajax/bidListing.php"
	if(formobj.confirm.value == 0) {
		url=url+"?confirm=0";
		url=url+"&lID="+formobj.lID.value;
		url=url+"&bid="+formobj.bidAmount.value;
		url=url+"&accID="+accID;
		url=url+"&lType=1";
		xmlhttp.open("GET",url,true);
		xmlhttp.onreadystatechange=bidHTMLArea;
		xmlhttp.send(null);
	} else {
		url=url+"?lID="+formobj.lID.value;
		url=url+"&confirm=1";
		url=url+"&bid="+formobj.bidAmount.value;
		url=url+"&accID="+accID;
		url=url+"&lType=1";
		xmlhttp.open("GET",url,true);
		xmlhttp.onreadystatechange=bidHTMLArea;
		xmlhttp.send(null);
	}
}
function bidHTMLArea() {
	if (xmlhttp.readyState==4 && xmlhttp.status == 200) {
		if(xmlhttp.responseText != '') {
			document.getElementById("bidArea").innerHTML=xmlhttp.responseText;
		}
	}
}
function offerOnAuction(formobj, accID) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://crimson-veil.com/includes/ajax/bidListing.php"
	if(formobj.confirm.value == 0) {
		url=url+"?confirm=0";
		url=url+"&lID="+formobj.lID.value;
		url=url+"&offer="+formobj.offerAmount.value;
		url=url+"&accID="+accID;
		url=url+"&lType=2";
		xmlhttp.open("GET",url,true);
		xmlhttp.onreadystatechange=bidHTMLArea;
		xmlhttp.send(null);
	} else {
		url=url+"?lID="+formobj.lID.value;
		url=url+"&confirm=1";
		url=url+"&offer="+formobj.offerAmount.value;
		url=url+"&accID="+accID;
		url=url+"&lType=2";
		xmlhttp.open("GET",url,true);
		xmlhttp.onreadystatechange=bidHTMLArea;
		xmlhttp.send(null);
	}
}
function buyAuction(formobj, accID) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="http://crimson-veil.com/includes/ajax/bidListing.php"
	if(formobj.confirm.value == 0) {
		url=url+"?confirm=0";
		url=url+"&lID="+formobj.lID.value;
		url=url+"&accID="+accID;
		url=url+"&lType=3";
		url=url+"&quant="+formobj.quantityAm.value;
		xmlhttp.open("GET",url,true);
		xmlhttp.onreadystatechange=bidHTMLArea;
		xmlhttp.send(null);
	} else {
		url=url+"?lID="+formobj.lID.value;
		url=url+"&confirm=1";
		url=url+"&accID="+accID;
		url=url+"&lType=3";
		url=url+"&quant="+formobj.quantityAm.value;
		xmlhttp.open("GET",url,true);
		xmlhttp.onreadystatechange=bidHTMLArea;
		xmlhttp.send(null);
	}
}
