var debugMode = false; // Enables logging to the console

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=740,height=640');");
}

function fnSmallWindow(strFileName, intWidth, intHeight) {
	window.name = "winInfo";
	window.open(strFileName,"north","width=" + intWidth + ",height=" + intHeight + ",directories=no,toolbar=no,resizable=Yes,menubar=no,scrollbars=Yes");
}

function onLoadDocument() {
	log('onLoadDocument() fired');
	
	var checkbox = document.getElementById('shipping-same');
	if(checkbox) {
		if(checkbox.addEventListener) {
			checkbox.addEventListener('click', clearShippingAddress, false);
		} else {
			checkbox.onclick = clearShippingAddress; // IE
		}
	}
	
	// Add "home" class to home page
	if(window.location.pathname == '' || window.location.pathname == '/')
		document.getElementsByTagName('body')[0].className += ' home';
}

function clearShippingAddress() {
	log('clearShippingAddress() fired');
	
	if(this.checked) {
		var shippingFieldset = this.parentNode.parentNode;
		clearFormFields(shippingFieldset.getElementsByTagName('input'));
		clearFormFields(shippingFieldset.getElementsByTagName('select'));	
	}
}

function clearFormFields(elms) {
	for(var i = 0, j = elms.length; i < j; i++) {
		if(elms[i].tagName.toLowerCase() != 'input'
			|| (elms[i].type != 'checkbox' && elms[i].type != 'hidden'))
			elms[i].value = '';
	}
}

/**
 * LOGGING
 */
function log(message) {
	if(debugMode) {
		if(window.console && window.console.log) {
			window.console.log(message);
		}
		else if(window.opera && window.opera.postError) {
			window.opera.postError(message);
		}
	}
}

if(window.addEventListener) {
	window.addEventListener('load', onLoadDocument, false);
} else if(document.addEventListener) {
	document.addEventListener('load', onLoadDocument, false);
} else {
	window.onload = onLoadDocument; // IE
}

