function getXMLNodeValue(xmlNode) {
	if (xmlNode) {
		if (xmlNode.textContent) {
			return xmlNode.textContent;
		} else if (xmlNode.text) {
			return xmlNode.text;
		} else if (xmlNode.firstChild && xmlNode.firstChild.nodeValue) { // sometimes the value text is a child (to Safari)
			return xmlNode.firstChild.nodeValue;
		}
	}
}

function toggleGiftMessageUI(cb) {
	if (cb.checked) { 
		new Effect.BlindDown('giftMessageUI', {duration: 0.5});
		
		// correct the subtotal amount
		var cartSubTotal = $('subTotal').innerHTML + '';		
		if (cartSubTotal.charAt(0) == '$')
		{
			cartSubTotal = cartSubTotal.substring(1);
		}
		$('subTotal').innerHTML = '$' + (parseFloat(cartSubTotal) + 3).toFixed(2);
		new Effect.Highlight('subTotal');
		
		// let's fake out adding the item to the cart as well
		var myAjax = new Ajax.Request('./add-gift-message.do', 
    	{
    		method: 'get', // TODO: post may be more appropriate 
			onComplete: function(request) {
				// there's really nothing to do but be happy
    		}
		});		
		
	} else { 
		new Effect.BlindUp('giftMessageUI', {duration: 0.5});

		var cartSubTotal = $('subTotal').innerHTML;
		if (cartSubTotal.charAt(0) == '$')
		{
			cartSubTotal = cartSubTotal.substring(1);
		}
		$('subTotal').innerHTML = '$' + (parseFloat(cartSubTotal) - 3).toFixed(2);
		new Effect.Highlight('subTotal');

		// let's fake out removing the item from the cart as well
		var myAjax = new Ajax.Request('./remove-gift-message.do', 
    	{
    		method: 'get', // TODO: post may be more appropriate 
			onComplete: function(request) {
				// let's clear out the user entered data
				$('gmLine1').value = '';
				$('gmLine2').value = '';
				$('gmLine3').value = '';
				$('gmLine4').value = '';
    		}
		});		
	}			
}

function loadGiftMessageUI() {
	if ($('giftMessageUI') && $('enableGiftMessageCB').checked) {
	    showGiftMessage();
		// correct the subtotal amount
		var cartSubTotal = $('subTotal').innerHTML + '';		
		if (cartSubTotal.charAt(0) == '$')
		{
			cartSubTotal = cartSubTotal.substring(1);		}
		
		$('subTotal').innerHTML = '$' + (parseFloat(cartSubTotal) + 3).toFixed(2);
		new Effect.Highlight('subTotal');	    
		$('giftMessageUI').show();
	}
}

var containsCurseWords = false;

function blockCurseWords() {
	
	// the user is obviously typing, so let's hide the validation
	// message if it's visible
	if ($('gmValidationMessage') && $('gmValidationMessage').visible()) {
		$('gmValidationMessage').hide();
		$('gmLine1').style.border = '';
		containsCurseWords = false;		
	}
	
	if (hasGiftMessageInput()) {

		var url = './block-curse-words.do';
		//var pars = Object.toQueryString(getGiftMessageLinesHash());
		var pars = getGiftMessageLinesQueryString();

		
		var http = Ajax.getTransport();
		
	  	http.open("POST", url + '?' + pars, true);
	  
		http.onreadystatechange = function() {
			if (http.readyState == 4) {		
				eval(http.responseText);
				return;
			}
		};
		http.send(null);		
	}
}


function validateGiftMessage() {
	
    if ($('enableGiftMessageCB') != null && $F('enableGiftMessageCB') == 'enableGiftMessage') {
		if (!hasGiftMessageInput()) {
			new Effect.Appear('gmValidationMessage');
			$('gmLine1').style.border = '2px solid red';		
			return false;		
		} else if (containsCurseWords) {
			new Effect.Shake('gmErrorMessage');
			return false;
		}
	} 	
	return true;
}

// This will work for any number of gift message lines 
function hasGiftMessageInput() {

	var lines = $$('#gmLines input');
	var hasInput = false;
	
	lines.each(function(line) {
		if ($F(line)) {
			hasInput = true;
		} 
	});
	
	return hasInput;
}

function getGiftMessageLinesQueryString() {
	var lines = $$('#gmLines input');
	var qs = "";
	
	for (i = 0; i < lines.length; i++) {
		qs += lines[i].id + '=' + $F(lines[i].id);
		if (i < (lines.length - 1)) {
			qs += '&';	
		}
	}
	return qs;
}

function getGiftMessageLinesHash() {
	var lines = $$('#gmLines input');
	var hash = new Hash();
	for (i = 0; i < lines.length; i++) {
		hash[lines[i].id] = $F(lines[i].id);
	}
	
	return hash;
}

function showGiftMessage(){
    showHideElement('gift-message-cart-total');
}