var xmlHttp;
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0; 
var url;
var ticketFees;
var status; //variable to check if AJAX is being used for gift cards or gift vouchers
var ticketQty;
var newFee;
var giftVoucherList;
var giftCardBalance;

function getBalance(fees){
	status = 1;
	ticketFees = fees;
	var requestURL = '/includes/ingenico.asp';
	if (document.getElementById) {
		url = requestURL+ '?cardNo=' + document.getElementById("giftCardNo").value + '&pinNo=' + document.getElementById("pinNo").value +'&transactionType=f&amount=';
	} else if (document.all) {
		url = requestURL + '?cardNo=' + document.all["giftCardNo"].value + '&pinNo=' + document.all["pinNo"].value +'&transactionType=f&amount=';
	} else if (document.layers) {
		url = requestURL + '?cardNo=' + document.layers["giftCardNo"].value + '&pinNo=' + document.layers["pinNo"].value +'&transactionType=f&amount=';
	}
	xmlHttp = GetXmlHttpObject(stateChangeHandler);
	xmlHttp_Get(xmlHttp, url);
}

function validateGiftVoucher(orderID,numTickets, bookingFee){	
	status = 2;
	var loop, element;
	ticketQty = numTickets;
	newFee = bookingFee;
	url = "";
	var requestURL = '/includes/giftVoucher.asp?orderid='+orderID;
	if (document.getElementById) {
		for(loop=1;loop<=numTickets;loop++) {
			element = "Voucher_WGV9000_"+loop;
			url = url + '&Voucher_WGV9000_' + loop + '=' + document.getElementById(element).value;
		}
	} else if (document.all) {
		url = requestURL+'?';
		for(loop=1;loop<=numTickets;loop++) {
			element = "Voucher_WGV9000_"+loop;
			url = url + '&Voucher_WGV9000_' + loop + '=' + document.all[element].value;
		}
	} else if (document.layers) {
		url = requestURL+'?';
		for(loop=1;loop<=numTickets;loop++) {
			element = "Voucher_WGV9000_"+loop;
			url = url + '&Voucher_WGV9000_' + loop + '=' + document.layers[element].value;
		}
	}
	url = requestURL+url;
	xmlHttp = GetXmlHttpObject(stateChangeHandler);
	xmlHttp_Get(xmlHttp, url);
}

function stateChangeHandler()
{
	var oDiv = null;		
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete'){
		var str = xmlHttp.responseText;
		//gift card
		if (status == 1) {
			var giftCardBalance = (+str);
			var totalCost = (+ticketFees);
			var newBalance;
			if (document.getElementById){				
				document.getElementById("giftCardBalance").innerHTML = "$"+giftCardBalance.toFixed(2);
				document.getElementById("validateGiftCard").innerHTML = 'Validate this gift card';
				document.getElementById("showPriceWarning").style.display = "none";
				document.getElementById("submitPage").style.display = "block";
				document.getElementById("cardBalance").value = giftCardBalance;
				if (str < totalCost && totalCost != 0) {				
					document.getElementById("showPriceWarning").style.display = "block";
					document.getElementById("cardBalance").value = giftCardBalance;
					newBalance = totalCost-str;			
					document.getElementById("totalCost").innerHTML = '<b>$'+newBalance.toFixed(2)+'</b>';
				}
			} else if (document.all) {
				document.all["giftCardBalance"].innerHTML = "$"+giftCardBalance;
				document.all["validateGiftCard"].innerHTML = 'Validate this gift card';
				document.all["showPriceWarning"].style.display = "none";
				document.all["submitPage"].style.display = "block";
				document.all["cardBalance"].value = giftCardBalance;
				if (str < totalCost && totalCost != 0) {				
					document.all["showPriceWarning"].style.display = "block";
					document.all["cardBalance"].value = '';
					newBalance = totalCost-str;
					document.all["totalCost"].innerHTML = '<b>$'+newBalance.toFixed(2)+'</b>';
				}
			} else if (document.layers) {
				document.layers["giftCardBalance"].innerHTML = "$"+giftCardBalance;
				document.layers["validateGiftCard"].innerHTML = 'Validate this gift card';
				document.layers["showPriceWarning"].style.display = "none";
				document.layers["submitPage"].style.display = "block";
				document.layers["cardBalance"].value = giftCardBalance;
				if (str < totalCost && totalCost != 0) {				
					document.layers["showPriceWarning"].style.display = "block";
					document.layers["cardBalance"].value = '';
					newBalance = totalCost-str;					
					document.layers["totalCost"].innerHTML = '<b>$'+newBalance.toFixed(2)+'</b>';
				}
			}
			//show the credit card section for the payment
			if (str < totalCost)
				showSecondPaymentSection(1);
		}
		//gift voucher
		else {
			if (document.getElementById){
				document.getElementById("validBarcodes").innerHTML = str;
				document.getElementById("totalCost").innerHTML = '<b>$'+newFee+'</b>';
				document.getElementById("validBarcodes").style.display = "block";
			} else if (document.all) {
				document.all["validBarcodes"].innerHTML = str;
				document.all["totalCost"].innerHTML = '<b>$'+newFee+'</b>';
				document.all["validBarcodes"].style.display = "block";
			} else if (document.layers) {
				document.layers["validBarcodes"].innerHTML = str;
				document.layers["totalCost"].innerHTML = '<b>$'+newFee+'</b>';
				document.layers["validBarcodes"].style.display = "block";
			}
			//show the credit card section for the payment
			if (str.indexOf("gv_tick.gif") != -1 )
				showSecondPaymentSection(1);			
		}
	}
}

function xmlHttp_Get(xmlhttp, url) {
	xmlhttp.open('GET', url, true);
	xmlhttp.send(null);
} 

function GetXmlHttpObject(handler) { 
	var objXmlHttp = null;   
	if (is_ie){
		var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP';
		try{ 
			objXmlHttp = new ActiveXObject(strObjName);
			objXmlHttp.onreadystatechange = handler;
		}
		catch(e){
			alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled');
			return;
		}
	}
	else if (is_opera){ 
		alert('Opera detected. The page may not behave as expected.'); 
		return;
	}
	else{
		objXmlHttp = new XMLHttpRequest(); 
		objXmlHttp.onload = handler; 
		objXmlHttp.onerror = handler; 
	}
	return objXmlHttp;
}		
	
