var http_request = false;
function makeRequest(url , temp) {

   if (window.XMLHttpRequest) { // Mozilla, Safari,...
	   http_request = new XMLHttpRequest();
   } else if (window.ActiveXObject) { // IE
	   http_request = new ActiveXObject("Microsoft.XMLHTTP");
   }
   http_request.onreadystatechange = temp;
   http_request.open('GET', url, true);
   http_request.send(null);
}



function calculation(flg)	{

	if(document.getElementById('txt_coupon').value == '') {


		document.getElementById('normal_total').innerHTML = document.getElementById('hid_normal_total').value;
		document.getElementById('discount').innerHTML = document.getElementById('hid_dicount').value;
		document.getElementById('total').innerHTML = document.getElementById('hid_normal_total').value;
		document.getElementById('total_to_pay').innerHTML = document.getElementById('hid_value_total').value;
		document.getElementById('DiscountShowHide').style.display='none' ;
		document.getElementById('NormalPriceShowHide').style.display='none' ;

		if(flg == 1) {

			document.frmshopcart.SESS_DICOUNT.value = document.getElementById('hid_dicount').value;
			document.frmshopcart.action = 'step1.php';
			document.frmshopcart.submit();
		}

	} else {


	var getStr = '';
	getStr += 'txt_coupon='+document.getElementById('txt_coupon').value;

   var url="coupon_calculation.php?"+getStr;

   function alertContentscalculation() {

	   if (http_request.readyState == 4) {
		   if (http_request.status == 200) {

            var resultValue = http_request.responseText;
			var new_total ;

			arrResultValue = resultValue.split(":");

			document.getElementById('normal_total').innerHTML = document.getElementById('hid_normal_total').value;
			discount_price = parseFloat(arrResultValue[1]);
			discount_code1 = String(arrResultValue[0]);
			discount_code  = discount_code1;

			/*--- THIS IS LOGIC FOR DESICCOUNT USING COUPON  ----*/
			if(resultValue.indexOf('Not Available') < 0 ) {

					if(resultValue.indexOf('P') > 0) {
						discount_sysmbol = '%';
						price_with_descount = ((document.getElementById('hid_normal_total').value * discount_price) / 100);
						document.getElementById('total').innerHTML = (document.getElementById('hid_normal_total').value - price_with_descount );
						document.getElementById('total_to_pay').innerHTML = ((document.getElementById('hid_normal_total').value - price_with_descount ) + parseFloat(document.getElementById('hid_portes').value));

					} else {

						discount_sysmbol = '&euro;';
						price_with_descount = discount_price;
						document.getElementById('total').innerHTML = (document.getElementById('hid_normal_total').value - discount_price );
						document.getElementById('total_to_pay').innerHTML = (parseFloat(document.getElementById('hid_normal_total').value) + parseFloat(document.getElementById('hid_portes').value) - parseFloat(discount_price) );
					}


				document.getElementById('discount').innerHTML = discount_price + discount_sysmbol;
				document.getElementById('DiscountShowHide').style.display='' ;
				document.getElementById('NormalPriceShowHide').style.display='' ;
			 } else {
				 alert("O Cupão que colocou é inválido");
			 }

			if(flg == 1) {

				  document.frmshopcart.SESS_DICOUNT.value = discount_price;
				  document.frmshopcart.SESS_DICOUNT_PRICE.value = price_with_descount;
				  document.frmshopcart.SESS_DICOUNT_SYMBOL.value = discount_sysmbol;
				  document.frmshopcart.SESS_DICOUNT_CODE.value = discount_code;

				  document.frmshopcart.action = 'step1.php';
				  document.frmshopcart.submit();

			}

		   } else {
			   alert('There was a problem with the request.');
		   }
		}
   }
	makeRequest(url , alertContentscalculation);
	}

}

