//dmitry b.
var xmlHttp;
var props_array;

var thousand_sep = ",";
var decimal_point = ".";
var default_precision = 2;

function validate()
{
	document.getElementById("main_error").innerHTMl = "";
	var verification = true;
	var elements = new Array(document.getElementById("os0").value, document.getElementById("os1").value, document.getElementById("os2").value, document.getElementById("os3").value, document.getElementById("os4").value, document.getElementById("os5").value, document.getElementById("amount").value);
	var counter = 0;
	while(counter < 7)
	{
		if(elements[counter].value == "")
		{
			document.getElementById("main_error").innerHTMl = "Please choose an item.";
			verification = false;
		}
		counter++;
	}
	
	return verification;
}

function dimm_java_warning()
{
	document.getElementById("javascript").innerHTML = "";
	document.getElementById("amount").value = "00.00";
	query('1');
}

//ajax fucntions
function query(param)
{
	xmlHttp = createXmlHttpRequestObject();
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		var url="diamond_reader.php?d=" + param;
		url+="&sid="+Math.random();
		xmlHttp.onreadystatechange= xml_handler;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	else
	{
		setTimeout("query('" + param + "')", 2000);
	}
}

function query_submit(param)
{
	xmlHttp = createXmlHttpRequestObject();
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		var url="diamond_reader.php?d=" + param;
		url+="&sid="+Math.random();
		xmlHttp.onreadystatechange= query_submit_handler;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	else
	{
		setTimeout("query_submit('" + param + "')", 2000);
	}
}

function query_submit_handler()
{
	//stud_form
	if (xmlHttp.readyState == 4)
	{
		// status of 200 indicates the transaction completed successfully
		//[0]14~[1]GEMS~[2]051263814CA~[3]14k Yellow Gold~[4]SI~[5]0.240~[6] ~[7]Round~[8] ~[9]1048.00~[10]Diamond Studs _001.jpg~[11]stud
		if (xmlHttp.status == 200)
		{
			//update tags
			props_array = xmlHttp.responseText.split("~")
			document.getElementById("os0").value = props_array[2];
			document.getElementById("os1").value = props_array[5];
			document.getElementById("os2").value = props_array[4];
			document.getElementById("os3").value = props_array[3];
			document.getElementById("os4").value = props_array[1];
			document.getElementById("os5").value = props_array[0];
			document.getElementById("amount").value = props_array[9];
			
			/*<input type=hidden name=on0 id=on0 value=ref_num />		<input type=hidden name=os0 id=os0 />
<input type=hidden name=on1 id=on1 value=Carats />		<input type=hidden name=os1 id=os1 />
<input type=hidden name=on2 id=on2 value=Clarity />		<input type=hidden name=os2 id=os2 />
<input type=hidden name=on3 id=on3 value=Metal />		<input type=hidden name=os3 id=os3 />
<input type=hidden name=on4 id=on4 value=Cert_name />	<input type=hidden name=os4 id=os4 />
<input type=hidden name=on5 id=on5 value=ItemId />		<input type=hidden name=os5 id=os5 />
<input type=hidden name=amount id=amount /> */
			
			document.getElementById("stud_form").submit();
		}
	}
}

function xml_handler()
{
	if (xmlHttp.readyState == 4)
	{
		// status of 200 indicates the transaction completed successfully
		if (xmlHttp.status == 200)
		{
			props_array = xmlHttp.responseText.split("~")
			//update tags
			document.getElementById("os0").value = props_array[2];
			document.getElementById("prop_s0").innerHTML = props_array[2];
			
			document.getElementById("os1").value = props_array[5];
			document.getElementById("prop_s1").innerHTML = props_array[5] + " ct.";
			
			document.getElementById("os2").value = props_array[4];
			document.getElementById("prop_s2").innerHTML = props_array[4];
			
			document.getElementById("os3").value = props_array[6];
			document.getElementById("prop_s3").innerHTML = props_array[6];
			
			document.getElementById("os4").value = props_array[3];
			document.getElementById("prop_s4").innerHTML = props_array[3];
			
			document.getElementById("os5").value = props_array[1];
			document.getElementById("prop_s5").innerHTML = props_array[1];
			
			document.getElementById("amount").value = props_array[9];
			document.getElementById("price_s").innerHTML = "$ " + formatInt(props_array[9]);
			
			document.getElementById("os7").value = props_array[7];
			document.getElementById("prop_s7").innerHTML = props_array[7];
			
			document.getElementById("os6").value = props_array[0];
		}
	}
}

function createXmlHttpRequestObject()
{
	var xmlHttp;
	
	if(window.ActiveXObject)
	{
		try
		{
			xmlHttp = new ActiveXObject('Msxml3.XMLHTTP');
		}
		catch (e)
		{
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				xmlHttp = false;
			}
		}
	}
	else
	{
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}
	
	if(!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}


function formatFloat(aFloat, aPrecision)
{
try
{
precision = default_precision;
if(!isNaN(aPrecision))
if(Math.abs(aPrecision)<=10)
precision = aPrecision;
}
catch(e)
{
precision = default_precision;
}
try
{
number = parseFloat(aFloat+'');
if(isNaN(number))
return "NaN";
}
catch(e)
{
return "NaN";
}

number = Math.round(number * Math.pow(10, precision)) / Math.pow(10,
precision);
integerpart = '' + ((number<0) ? Math.ceil(number) :
Math.floor(number));
decimalpart = Math.abs(Math.round((number - integerpart)*(Math.pow(10,
precision))));
if(decimalpart<10)
decimalpart="0"+decimalpart;
if(decimalpart==0)
decimalpart="00";
var buff = "";
for(j=-1, i=integerpart.length; i>=0; i--, j++){
if((j%3) == 0 && j>1)
buff = thousand_sep + buff;
buff = integerpart.charAt(i) + buff;
}
if(precision>0)
return buff+decimal_point+decimalpart;
return buff;
}

function formatInt(aInt){
return formatFloat(aInt,0);
}

function playSound(param)
{
	//do nothing
	return false;
}