function getXmlHttpRequestObject()
{
  var xhr;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xhr = false;
      }
    }
  @else
  xhr = false;
  @end @*/
  if (!xhr && typeof XMLHttpRequest != 'undefined') {
    try {
      xhr = new XMLHttpRequest();
    } catch (e) {
      xhr = false;
    }
  }
  return xhr;
}

var valid_code, amount, is_percent;

function check_coupon()
{
	var get_obj = getXmlHttpRequestObject();
	get_obj.open('POST', 'functions/checkcode.php', true);
	get_obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	get_obj.onreadystatechange = function()
	{
		if(get_obj.readyState == 4)
		{
			var resp = get_obj.responseText;
			if(resp.charAt(0) == 0)
			{
				valid_code = 0;
				alert ("'" + document.getElementById('coupon').value + "'  is not a valid promo code! \n\n" + "Codes are case sensitive! \n");

			}
			else
			{
				valid_code = 1; 
				var resp	= eval( '(' + resp + ')' );
				amount		= resp.coupon[0].amount;
				is_percent	= resp.coupon[0].is_percent;
				var coupon_code = document.getElementById('coupon').value;
					if (is_percent == '0'){
						alert (coupon_code + " is a valid promo code! \n\n" + "$" + amount + " discount is now in effect.");
					}
				  else {
				  		alert (coupon_code + " is a valid promo code! \n\n" + amount + "%" + " discount is now in effect.");
				  }
			}
		}
	}
	var post_params = 'coupon=' + document.getElementById('coupon').value;
	
	get_obj.send(post_params);
}

function ReadForm (obj1) {  // apply the discount and values to main form if discount is enabled
var amt,des;
  amt = obj1.price_option.value;       // base amount
  des = obj1.basedes.value;           // base description
	
	if (valid_code > '0'){ // Checks if code is valid
		if (is_percent == '1'){
			var amt_off = (amount / 100) * amt;
			amt = amt - amt_off;
			amt = amt.toFixed(2);
			des = des + ", " + amount + "% off, Promo Code= " + document.getElementById('coupon').value;
		}
		else {
			amt = amt - amount;
			amt = amt.toFixed(2);
			des = des + ", $" + amount + " off, Promo Code= " + document.getElementById('coupon').value;
		}
	}

  
  obj1.amount.value = amt;
  obj1.item_name.value = des;

  msg = "";
   if(obj1.select_agree.value != 'agree'){ msg += "- You must agree to our terms and conditions before making a purchase.\n"; }
   if(msg != ""){
     alert(msg);
     return false;
   }
}

// This function formats numbers by adding commas
function numberFormat(nStr,prefix){
    var prefix = prefix || '';
    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 prefix + x1 + x2;
}