function getRates()
{
	ajaxRequest('http://www.ktcollection.com/example.php', showShipping);
	return false;
}

function ajaxRequest(url, callback)
{
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (!http_request) {
		unLink();
		return false;
	}
	http_request.onreadystatechange = function() { callback(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function showShipping(http_request)
{
	if (http_request.readyState == 4) {
		var ship_div = document.createElement('div');
		ship_div.setAttribute('id', 'shipping_info');
		ship_div.setAttribute('class', 'popup_div');
		
		var close = document.createElement('p');
		close.setAttribute('id', 'ship_close');
		close.appendChild(document.createTextNode('close'));
		close.onclick = function(){
			return hideShipping();
		}
		ship_div.appendChild(close);
		
		var title = document.createElement('h2'); 
		title.appendChild(document.createTextNode('Shipping Rates'));
		ship_div.appendChild(title);
		
		if (http_request.status == 200) {
			
			
			
			var xmldoc = http_request.responseXML;
			var categories = xmldoc.getElementsByTagName('category');
			cat_num = categories.length;
			//document.createElement('table');
			for ( var x=0; x < cat_num; x++ ){
				
				var cutoff = categories[x].getElementsByTagName('cutoff')[0];
				var cutoff_rate = cutoff.getAttribute('reg');
				var exp_cutoff_rate = cutoff.getAttribute('exp');
				var int_cutoff_rate = cutoff.getAttribute('int');
				
				var increment = categories[x].getElementsByTagName('increment')[0];
				var inc_rate = increment.getAttribute('reg');
				var exp_inc_rate = increment.getAttribute('exp');
				var int_inc_rate = increment.getAttribute('int');
				
				var name = categories[x].getAttribute('name');
				var h3 = document.createElement('h3');
				h3.appendChild(document.createTextNode(ucfirst(name)));
				ship_div.appendChild(h3);
				
				var table = document.createElement('table');
				var thead = document.createElement('thead');
				var tbody = document.createElement('tbody');
				
				
				var toprow = document.createElement('tr');
				toprow.appendChild(document.createElement('td'));
				
				var th1 = document.createElement('th');
				th1.setAttribute('scope', 'col');
				th1.appendChild(document.createTextNode('Up to $'+cutoff.firstChild.data));
				
				var th2 = document.createElement('th');
				th2.setAttribute('scope', 'col');
				th2.appendChild(document.createTextNode('Each Additional $'+increment.firstChild.data));
				
				toprow.appendChild(th1);
				toprow.appendChild(th2);
				
				
				thead.appendChild(toprow);
				table.appendChild(thead);
				
				//Regular Rates
				var reg_row = document.createElement('tr');
				
				var regular = document.createElement('th');
				regular.setAttribute('scope', 'row');
				regular.appendChild(document.createTextNode('Regular'));
				
				reg_row.appendChild(regular);
				
				var reg1 = document.createElement('td');
				reg1.appendChild(document.createTextNode(formatCurrency(cutoff_rate)));
				
				var reg2 = document.createElement('td');
				reg2.appendChild(document.createTextNode(formatCurrency(inc_rate)));
				
				reg_row.appendChild(reg1);
				reg_row.appendChild(reg2);
				tbody.appendChild(reg_row);
				
				//Express
				var exp_row = document.createElement('tr');
				var express = document.createElement('th');
				express.setAttribute('scope', 'row');
				express.appendChild(document.createTextNode('Express'));
				
				var exp1 = document.createElement('td');
				exp1.appendChild(document.createTextNode(formatCurrency(exp_cutoff_rate)));
				
				var exp2 = document.createElement('td');
				exp2.appendChild(document.createTextNode(formatCurrency(exp_inc_rate)));
				
				exp_row.appendChild(express);
				exp_row.appendChild(exp1);
				exp_row.appendChild(exp2);
				tbody.appendChild(exp_row);
				
				//International
				var int_row = document.createElement('tr');
				var international = document.createElement('th');
				international.setAttribute('scope', 'row');
				international.appendChild(document.createTextNode('International'));
				
				var int1 = document.createElement('td');
				int1.appendChild(document.createTextNode(formatCurrency(int_cutoff_rate)));
				
				var int2 = document.createElement('td');
				int2.appendChild(document.createTextNode(formatCurrency(int_inc_rate)));
				
				int_row.appendChild(international);
				int_row.appendChild(int1);
				int_row.appendChild(int2);
				tbody.appendChild(int_row);
				
				table.appendChild(tbody);
				ship_div.appendChild(table);
			}
			
			document.body.appendChild(ship_div);
		} else {
			alert('There was a problem with the shipping request.');
		}
	}
	
	return false;
}

function hideShipping(obj){
	document.body.removeChild(document.getElementById('shipping_info'));
}

function formatCurrency(num) {
    num = isNaN(num) || num === '' || num === null ? 0.00 : num;
    var number = parseFloat(num).toFixed(2);
    return '$'+number;
}

function ucfirst(string)
{
    return string.charAt(0).toUpperCase() + string.slice(1);
}


