/********************************
 * 
 * Voucher Functionality. See end of file for popup code.
 * 
 */

var vouchers_list = new Array();

function setOKButtonDisabled(isDisabled) {
	var buttonOK = document.getElementById('popupOKButton');
	var buttonAnother = document.getElementById('popupAnotherButton');
	buttonOK.disabled = isDisabled;
	buttonAnother.disabled = isDisabled;
}

function clearVoucherFields(clearOrderNumber) {
	
	var listSection = document.getElementById('voucherPopupTicketList');
	var errorElement = document.getElementById('voucherPopupErrorMessage');
	var inputBarcode = document.getElementById('voucher_code');
	var inputOrderNumber = document.getElementById('order_number');
	
	inputBarcode.value = "";
	if (clearOrderNumber) {
		inputOrderNumber.value = "";
	}
	
	listSection.style.visibility = 'hidden';
	errorElement.style.visibility = 'hidden';
	
}

function initVoucherPopup(clearOrderNumber) {
	
	setOKButtonDisabled(false);
	clearVoucherFields(clearOrderNumber);
	
}

function verifyVoucherCodeCallback(response) {
	
	var errorNodes = response.getElementsByTagName('error');
	var listElement = document.getElementById('voucherPopupTicketSelect');
	var listSection = document.getElementById('voucherPopupTicketList');
	var errorElement = document.getElementById('voucherPopupErrorMessage');
	
	if (errorNodes[0].childNodes.length > 0) {
		errorElement.innerHTML = errorNodes[0].firstChild.nodeValue;
		errorElement.style.visibility = 'visible';
		listSection.style.visibility = 'hidden';
		elList.selectedIndex = -1;
	} else {
		var productNodes = response.getElementsByTagName('product');
		listElement.length = 0;
		for (var i = 0; i < productNodes.length; i++) {
			var j= listElement.length;
			listElement.options[j] = new Option(productNodes[i].firstChild.nodeValue, productNodes[i].getAttribute('id'));			
		}
		listSection.style.visibility = 'visible';
		errorElement.style.visibility = 'hidden';
	}
}

function verifyVoucherCode() {
	
	var inputVoucherCode = document.getElementById('voucher_code');
	var inputOrderNumber = document.getElementById('order_number');
	
	if (inputVoucherCode == null || inputOrderNumber == null) {
		alert('Could not find the voucher code box or the order number box on the page.');
	} else if (inputVoucherCode.value.length == 0) {
		alert('You must enter a voucher code.');
	} else if (inputOrderNumber.value.length == 0) {
		alert('You must enter the order number that the voucher was purchased in.');		
	} else {
		
		voucherCode = inputVoucherCode.value;
		orderNumber = inputOrderNumber.value;
		
		var cp = new cpaint();
		cp.set_debug(false);
		cp.set_response_type('XML');
		cp.set_async(true);
	    cp.set_persistent_connection(false);	
		
		cp.call('cpaint_voucher.php', 'verify_voucher_barcode', verifyVoucherCodeCallback, voucherCode, orderNumber);		
	}
	
}

function addVoucherCallback(response) {
	
	var resultNodes = response.getElementsByTagName('is_adding_another');
	
	if (resultNodes[0].getAttribute('value') == 0) {
		disablePopup();
		cpaintCalcDelivery();
		initVoucherPopup(true);
	} else {
		initVoucherPopup(false);
	}
}

/**
 * Performs the AJAX call to assign the voucher to the current shopping
 * cart. The response is processed by the addVoucherCallback() method.
 * 
 * While the process is underway, the OK button is disabled to stop the
 * user from initiating a second AJAX call. The button is always re-enabled
 * when the voucher redemption DIV is made visible.
 * 
 */
function addVoucher(isAddingAnother) {

	var elList = document.getElementById('voucherPopupTicketSelect');
	var divList = document.getElementById('voucherPopupTicketList');
	
	if (divList.style.visibility == 'visible') {
		if (elList.selectedIndex >= 0) {
			
			setOKButtonDisabled(true);
			
			var cp = new cpaint();
			cp.set_debug(false);
			cp.set_response_type('XML');
			cp.set_async(true);
		    cp.set_persistent_connection(false);	
			
			cp.call('cpaint_voucher.php', 'add_voucher', addVoucherCallback, voucherCode, elList.options[elList.selectedIndex].value, isAddingAnother);
		} else {
			alert("You must select a product from the list.");
		}
	} else {
		alert("You must enter a valid voucher code.");
	}
}

function removeVoucherCallback(response) {
	cpaintCalcDelivery();
}

function removeVoucher(voucherCode) {
	
	var cp = new cpaint();
	cp.set_response_type('XML');
	cp.set_async(true);
    cp.set_persistent_connection(false);	
	
	cp.call('cpaint_voucher.php', 'remove_voucher', removeVoucherCallback, voucherCode.name);
	
}

/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/** ************************ */

// SETTING UP OUR POPUP
// 0 means disabled; 1 means enabled;
var popupStatus = 0;

// centering popup
function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if (typeof (window.pageYOffset) == 'number') {
		// Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if (document.body
			&& (document.body.scrollLeft || document.body.scrollTop)) {
		// DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if (document.documentElement
			&& (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		// IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return {
		X : scrOfX,
		Y : scrOfY
	};
}

function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	if (typeof (window.innerWidth) == 'number') {
		// Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement
			&& (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		// IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body
			&& (document.body.clientWidth || document.body.clientHeight)) {
		// IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return {
		X : myWidth,
		Y : myHeight
	}
}

// loading popup with jQuery magic!
function loadPopup() {
	// loads popup only if it is disabled
	if (popupStatus == 0) {
		initVoucherPopup(true);
		$("#backgroundPopup").css( {
			"opacity" : "0.7"
		});
		$("#backgroundPopup").fadeIn("fast");
		$("#popupContent").fadeIn("fast");
		popupStatus = 1;
	}
}

// disabling popup with jQuery magic!
function disablePopup() {
	// disables popup only if it is enabled
	if (popupStatus == 1) {
		$("#backgroundPopup").fadeOut("fast");
		$("#popupContent").fadeOut("fast");
		popupStatus = 0;
	}
}

// centering popup
function centerPopup() {
	// request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var windowDim = getWindowSize();
	var popupHeight = $("#popupContent").height();
	var popupWidth = $("#popupContent").width();

	var scroll = getScrollXY();

	// centering
	$("#popupContent").css( {
		"position" : "absolute",
		// "top": windowHeight/2-popupHeight/2,
		// "left": windowWidth/2-popupWidth/2
		"top" : windowDim.Y / 2 - popupHeight / 2 + scroll.Y - 15,
		"left" : windowDim.X / 2 - popupWidth / 2 + scroll.X - 15

	});
	// only need force for IE6

	$("#backgroundPopup").css( {
		"height" : windowDim.Y, // windowHeight
		"width" : windowDim.X
	});

}

// CONTROLLING EVENTS IN jQuery
$(document).ready(function() {

	// LOADING POPUP
		// Click the button event!
		$("#voucher_button").click(function() {
			// centering with css
				centerPopup();
				// load popup
				loadPopup();
			});

		// CLOSING POPUP
		// Click the x event!
		$("#popupContentClose").click(function() {
			disablePopup();
		});
		$("#popupCloseButton").click(function() {
			disablePopup();
		});
		// Cancel event!
		$("#popupCancelButton").click(function() {
			disablePopup();
		});
		// Press Escape event!
		$(document).keypress(function(e) {
			if (e.keyCode == 27 && popupStatus == 1) {
				disablePopup();
			}
		});

	});
