<!--

function makeRequest( paramsObj ) {
	var params = '&';
	for( key in paramsObj ) {
		if( key != "onComplete" )
			params += key + '=' + paramsObj[key] + '&';
	}

	var Ajax = false;
	try { Ajax = new XMLHttpRequest(); } catch(e) {};
	try { Ajax = new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) {};
	try { Ajax = new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) {};
	if( !Ajax ) return false;
	
	Ajax.open( "POST", "/shipping_estimator.html", true);
	Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	Ajax.onreadystatechange = function() {
		if (Ajax.readyState == 4) {
			if (Ajax.status == 200) {
				try {
					paramsObj['onComplete'](Ajax);
				} catch(e) {}
			} else {
				alert("A " + Ajax.status + " status was returned.");
			}
		}
	}
	Ajax.send(params);
}

var inlineWindow = null, windowContent = null;
function CalcPop_Show() {
	this.blur();	// So hitting Return doesn't call it again
	if( inlineWindow == null ) {
		inlineWindow = document.createElement( 'div' );
		inlineWindow.id = "inlineWindow";
		inlineWindow.innerHTML = '<div class="overlay"></div><div class="display"><div class="titlebar"><a href="#" class="close">Close [X]</a><span class="title">Shipping Estimator</span></div><div class="content"></div></div>';
		inlineWindow.firstChild.onclick = CalcPop_Hide;	// Hide when overlay is clicked
		if( window.attachEvent ) {	// IE Fix to mimic position:fixed
			inlineWindow.style.top = document.documentElement.scrollTop;
			document.documentElement.onscroll = function() {
				inlineWindow.style.top = document.documentElement.scrollTop;
			}
		}
		document.body.appendChild( inlineWindow );
		windowContent = inlineWindow.lastChild.lastChild;
	}

	inlineWindow.lastChild.firstChild.firstChild.onclick = CalcPop_Hide;	// Hide when close button clicked
	
	inlineWindow.className = "small loading";
	makeRequest( {
		'source': "cart",
		'onComplete': function( response ) {
			windowContent.innerHTML = response.responseText;
		}
	} );
	
	inlineWindow.style.display = 'block';
	return false;
}

function CalcPop_Hide() {
	inlineWindow.style.display = 'none';
	windowContent.parentNode.style.width = null;
	windowContent.parentNode.style.marginLeft = null;
	windowContent.parentNode.style.marginTop = null;
	windowContent.innerHTML = '';
	return false;
}

function CalcPop_Validate( form ) {
	errors = [];
	if( form.elements['country_id'].selectedIndex == 0 )
		errors.push( "Please select country" );
	else
		country_id = form.elements['country_id'].options[ form.elements['country_id'].selectedIndex ].value;
		
	if( form.elements['postcode'].value == '' )
		errors.push( "Please enter a postcode" );
	else if( !/^[0-9]+$/.test( form.elements['postcode'].value ) )
		errors.push( "Post code must only contain numbers" );
	else
		postcode = form.elements['postcode'].value;
	
	if( errors.length ) {
		alert( errors.join( "\n" ) );
	} else {
		CalcPop_GetQuotes( country_id, postcode );
	}
}

function CalcPop_GetQuotes( country_id, postcode ) {
	windowContent.innerHTML = '';
	makeRequest( {
		'source': "cart",
		'country_id': country_id,
		'postcode': postcode,
		'onComplete': function( response ) {
			inlineWindow.className = "";
			windowContent.innerHTML = response.responseText;
			windowContent.parentNode.style.width = '400px';
			windowContent.parentNode.style.marginLeft = '-' + ( windowContent.parentNode.offsetWidth / 2 ) + 'px';
			windowContent.parentNode.style.marginTop = '-' + ( windowContent.parentNode.offsetHeight / 2 ) + 'px';
		}
	} );
}
//-->
