var link = 'http://www.apple-iphone.com.au/Tables/TheAJAXTable.php';
var sort = '';
var model = '';
var size = 'All';
var providers = ['Virgin Mobile', 'Vodafone', 'Optus'];
var minCost = '0';
var maxCost = '999999999';
var minData = '0';
var maxData = '999999999';

function sortBy(field){
	if(sort == field){
		sort = field + " DESC";
	} else {
		sort = field;
	}
	updateTable();
}

function updateProvider(src, name){
	if(src.checked){
		providers.push(name);
	} else { //unchecked - remove!
		for ( var i = 0; i < providers.length; i++) {
			if(providers[i] == name){
				providers.splice(i, 1);
				i--;
			}
		}
	}
	
	updateTable();
}

function updatePhones(newModel, newSize){
	model = newModel;
	size = newSize;
	
	updateTable();
}

function updateModel(newModel){
	model = newModel;
	
	updateTable();
}

function updateSize(newSize){
	size = newSize;
	
	updateTable();
}

function updateMinCost(val){
	minCost = val;
	
	updateTable();
}

function updateMaxCost(val){
	maxCost = val;
	
	updateTable();
}

function updateMinData(val){
	minData = val;
	
	updateTable();
}

function updateMaxData(val){
	maxData = val;
	
	updateTable();
}


function showOptions(){
	var e = document.getElementById('options');
	e.style.display = 'block';
	
	var a = document.getElementById('showhideoptions');
	a.className = 'hide';
	a.onclick = hideOptions;
}

function hideOptions(){
	var e = document.getElementById('options');
	e.style.display = 'none';
	
	var a = document.getElementById('showhideoptions');
	a.className = 'show';
	a.onclick = showOptions;
}



function updateTable(){
	var theLink = link + '?sort=' + sort + '&model=' + model + '&size=' + size + '&providers=' + providers.join(',') + '&minCost=' + minCost + '&maxCost=' + maxCost  + '&minData=' + minData + '&maxData=' + maxData;
	cbaUpdateElement('tableContainer', theLink);
}

function cbaUpdateElement(elId, url){
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById(elId).innerHTML = xmlhttp.responseText;
		}
	};
	xmlhttp.open("GET", url, true);
	xmlhttp.send();
}
