var isFirefox = false; if(navigator.appName.indexOf("Netscape") != -1) isFirefox = true;
var _xmlHttp = true;

function genericAJAXGET(qryStr,rscFunc)
{
	_xmlHttp = false;
	if (window.XMLHttpRequest)  // Mozilla, Safari,...
	{
		_xmlHttp = new XMLHttpRequest();
		if (_xmlHttp.overrideMimeType)
		{
			_xmlHttp.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject)  // IE
	{
		try {
			_xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				_xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!_xmlHttp)
	{
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	if(isFirefox == true) _xmlHttp.overrideMimeType('text/html');		// Needed to avoid Firefox js error
	_xmlHttp.onreadystatechange = rscFunc;
	_xmlHttp.open('GET', qryStr, true);
	_xmlHttp.send(null);
}

function genericAJAXPOST(qryStr,parameters,rscFunc)
{
	_xmlHttp = false;
	if (window.XMLHttpRequest)  // Mozilla, Safari,...
	{
		_xmlHttp = new XMLHttpRequest();
		if (_xmlHttp.overrideMimeType)
		{
			_xmlHttp.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject)  // IE
	{
		try {
			_xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				_xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!_xmlHttp)
	{
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	if(isFirefox == true) _xmlHttp.overrideMimeType('text/html');		// Needed to avoid Firefox js error
	_xmlHttp.onreadystatechange = rscFunc;
	_xmlHttp.open('POST', qryStr, true);
	_xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	_xmlHttp.setRequestHeader("Content-length", parameters.length);
	_xmlHttp.setRequestHeader("Connection", "close");
	_xmlHttp.send(parameters);
}

function applyCoupon(num)
{

        if(num == 1)
        {
		document.getElementById("error").innerHTML = "";
              var frm=document.CouponForm;
              var qs = "act=1&cc="+frm.couponcode.value;
              genericAJAXPOST("/store/process.php",qs,function(){couponFollowUp(1);});
        }
        else
        {
		document.getElementById("error3").innerHTML = "";
              var frm=document.CouponForm2;
              var qs = "act=1&cc="+frm.couponcode.value;
              genericAJAXPOST("/store/process.php",qs,function(){couponFollowUp(2);});
        }

}
function couponFollowUp(num)
{

        if(_xmlHttp.readyState == 4){
                if(_xmlHttp.responseText != "")
                {
                        if(num == 1)
                        {
                                var dref = document.getElementById("error");
                                var dref2 = document.getElementById("success");
                        }
                        else
                        {
                                var dref = document.getElementById("error3");
                                var dref2 = document.getElementById("success3");
                        }

                        if(_xmlHttp.responseText == "1")                // Coupon Applied to Order
                        {
                                dref2.innerHTML = "Coupon applied";
                                dref2.style.display = "";
                                dref.style.display = "none";
                                document.location.reload();
                        }
                        else if(_xmlHttp.responseText == "0")           // Coupon Error
                        {
                                dref.innerHTML = "There was an error applying your coupon code.<br/><br/> - The coupon code you entered may be invalid.<br/> - The coupon you entered may have expired.<br/> -  You may already have another coupon in your cart.<br/><br/>Please check your coupon code, or try again.";
                                dref.style.display = "";
                                dref2.style.display = "none";
                        }
                }
        }
}

function applyTip()
{
	var frm=document.TipForm;

	if(frm.tipamt[frm.tipamt.selectedIndex].value == 0)
	{
		var dref = document.getElementById("error2");
		dref.innerHTML = "No tip will be applied.";
		dref.style.display = "";
	}
	else
	{
		var qs = "act=2&tamt="+frm.tipamt[frm.tipamt.selectedIndex].value;
		genericAJAXPOST("/store/process.php",qs,function(){tipFollowUp();});
	}
}

function tipFollowUp()
{
	var dref = document.getElementById("error2");
	var dref2 = document.getElementById("success");
	
	if(_xmlHttp.readyState == 4){
		if(_xmlHttp.responseText != "")
		{
			if(_xmlHttp.responseText == "1")		// Tip Applied to Order
			{
				dref.innerHTML = "Your tip has been applied.";
				dref.style.display = "";
			}
			else if(_xmlHttp.responseText == "0")		// Tip Error
			{
				dref.innerHTML = "There was an error applying your tip.  Please try again.";
				dref.style.display = "";
			}
		}
	}
}

function applyTip2()
{
	var frm=document.TipForm;
	var frm2=document.MainForm;
	if(frm.tipamt[frm.tipamt.selectedIndex].value == 0) { frm2.submit(); }
	else
	{
		var qs = "act=2&tamt="+frm.tipamt[frm.tipamt.selectedIndex].value;
		genericAJAXPOST("/store/process.php",qs,function(){tipFollowUp2();});
	}
}

function tipFollowUp2()
{
	var frm=document.MainForm;
	if(_xmlHttp.readyState == 4){
		if(_xmlHttp.responseText != "")
		{
			if(_xmlHttp.responseText == "1")		// Tip Applied to Order
			{
				frm.submit();
			}
			else if(_xmlHttp.responseText == "0")		// Tip Error
			{
				frm.submit();
			}
		}
	}
}

function SetCookie(cookieName,cookieValue,nDays)
{
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue) + ";path=/;expires="+expire.toGMTString();
}
function noenter(e)
{
        var key;
        if(window.event) key = window.event.keyCode;     //IE
        else key = e.which;     //firefox
        if(key == 13) return false;
        else return true;
}
function couponenter1(e)
{
	var key;
	if(window.event) key = window.event.keyCode;     //IE
	else key = e.which;     //firefox
	if(key == 13) { applyCoupon(1); return false; }
	else return true;
}
function couponenter2(e)
{
	var key;
	if(window.event) key = window.event.keyCode;     //IE
	else key = e.which;     //firefox
	if(key == 13) { applyCoupon(2); return false; }
	else return true;
}