// JavaScript Document
var xmlHttp;
function showDiscount(str,total)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
 
var url="getdiscount.php";
url=url+"?q="+str+"&t="+total;
url=url+"&sid="+Math.random();

xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
//document.registerform.applycoupon.value = true;
}

function stateChanged() 
{ 
	var total2 = document.getElementById('price').innerHTML;
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 	//document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
	if(xmlHttp.responseText == '100') {
		document.getElementById('price').innerHTML = total2;
		document.getElementById('total').innerHTML = total2;
		document.getElementById('couponErr').innerHTML = " <b>[ Invalid Coupon ]</b>";	
	} else if(isFloat(xmlHttp.responseText)){
		document.getElementById('price').innerHTML = '$'+xmlHttp.responseText;
		document.getElementById('total').innerHTML = '$'+xmlHttp.responseText;
		document.getElementById('couponErr').innerHTML = " <b>[ Coupon Applied ]</b>";
	}else {
	document.getElementById('couponErr').innerHTML = " <b>[ Unexpected Error ]</b>";
	document.getElementById('price').innerHTML = total2;
	document.getElementById('total').innerHTML = total2;	 
 }
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
//check response if is not string
function isFloat(v){
	dotPos = 0;
	for (var i = 0; i < v.length; i++) 
    {
        var ch = v.charAt(i)
        if (i == 0 && ch == "-")
            continue;
		if(ch == ".")
		{
			if(i==0)
				return false;
			if(dotPos==0){
				dotPos++;
				continue;
			}
			else
				return false;
		}
        if (ch < "0" || ch > "9")
            return false;
    }
	return true;
}