var xmlHttp = null;

function getXmlHttpObject() {

	xmlHttp = null;
	
	if (window.XMLHttpRequest) {
		 // code for IE7+, Firefox, Chrome, Opera, Safari
		xmlHttp = new XMLHttpRequest();
	}
	
	else if (window.ActiveXObject) {
		// code for IE6, IE5
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
}


function toggle_state_list() {
	
	//show the state list
	if(document.getElementById('state_list').innerHTML == "") {
		document.getElementById('state_expand_button').src = "minus.gif";
		
		getXmlHttpObject();
		
		// Build the URL to connect to
		  // Open a connection to the server
		  xmlHttp.open("get","get_states_div.php", true);
		
		  // Setup a function for the server to run when it's done
		  xmlHttp.onreadystatechange=function() {
		  
		  	if (xmlHttp.readyState == 4) { 
		  		// Check if it is ready to recieve data
		  		document.getElementById('state_list').style.height = "auto";
				document.getElementById('state_list').style.display = "block";
				document.getElementById('state_list').innerHTML = xmlHttp.responseText;
			}
		}
		
		xmlHttp.send(null);
	}
	
	//hide the state list
	else {
		document.getElementById('state_expand_button').src = "plus.gif";
		document.getElementById('state_list').innerHTML = "";
		document.getElementById('state_list').style.height = "0px";
		document.getElementById('state_list').style.display = "none";
	}
}


function get_town_list(state_id) {
	
	if(document.getElementById('town_list_' + state_id).innerHTML == "") {
		
		document.getElementById('town_expand_button_' + state_id).src = "minus.gif";
	
		getXmlHttpObject();
		
		// Build the URL to connect to
		// Open a connection to the server
		xmlHttp.open("get","get_towns_div.php?state_id=" + state_id, true);
		
		// Setup a function for the server to run when it's done
		xmlHttp.onreadystatechange=function() {
		  
			if (xmlHttp.readyState == 4) { 
				// Check if it is ready to recieve data
				document.getElementById('town_list_' + state_id).style.height = "auto";
				document.getElementById('town_list_' + state_id).style.display = "block";
				document.getElementById('town_list_' + state_id).innerHTML = xmlHttp.responseText;
			}
		}
		
		xmlHttp.send(null);
	}
	
	else {
		document.getElementById('town_expand_button_' + state_id).src = "plus.gif";
		document.getElementById('town_list_' + state_id).innerHTML = "";
		document.getElementById('town_list_' + state_id).style.height = "0px";
		document.getElementById('town_list_' + state_id).style.display = "none";
	}
}


function selectRadio(n){

document.forms["add"]["matte_selected"][n].checked=true

} 
function selectRadio_fr(n){

document.forms["add"]["frame_selected"][n].checked=true

}
function bux_button(tr) {
content = "<input type=image src=images/b_add_cart.gif name=btn id=btn />";
			// Make sure there is something in the content variable
			document.getElementById(tr).innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
	// Send the request
 	//xmlhttp.send(null);
}
function eraseBux(tr) {
content = "<img src=images/blank_matte.jpg>";
			// Make sure there is something in the content variable
			document.getElementById(tr).innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
	// Send the request
 	 //xmlhttp.send(null);
}

function getNewFrame(url, contentBox) {
			
	var newURL = url;
	window.alert(newURL);
	var xmlhttp=false; // Clear our fetching variable
	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); // Try the first kind of active x object
	}
	catch (e) {
		try {
			xmlhttp = new
			ActiveXObject('Microsoft.XMLHTTP'); // Try the second kind of active x object
		}
		catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest(); // If we were able to get a working active x object, start an XMLHttpRequest
	}

	// Build the URL to connect to
  	// Open a connection to the server
  	xmlhttp.open("GET", newURL, true);

  	// Setup a function for the server to run when it's done
  	xmlhttp.onreadystatechange=function() {
  	if (xmlhttp.readyState==4) { 
  		// Check if it is ready to recieve data
		var content = xmlhttp.responseText; // The content data which has been retrieved ***
		if(content) { 
			// Make sure there is something in the content variable
			document.getElementById(contentBox).innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
		}
	}
	}

	// Send the request
 	 xmlhttp.send(null);
}



  function getCheckedRadio(element) {
			      var radioButtons = document.getElementsByName(element);
			      var chk = '';
			      for (var x = 0; x < radioButtons.length; x ++) {
					if (radioButtons[x].checked) {
					  var chk = radioButtons[x].value;

					}
				}
				//window.alert(chk);
			          return chk;
			}



function getCost(url, contentBox, element, element2) {
			
	var newURL = url + '&old_size='+ document.getElementById('selected_size').value +'&cost='+ document.getElementById('the_cost').value;
		if (element2 == 'frame')
		{
			var newURL = newURL +'&matte='+ document.getElementById('selected_matted').value;
				if (document.getElementById('selected_framed').value == '0' && element > 0)
					var newURL = newURL +'&price=' + element +'&frame=1';
				else if (element < 0 && document.getElementById('selected_framed').value >= 1)
					var newURL = newURL +'&mike=1&price=' + element +'&frame=0';
				else 
					var newURL = newURL +'&price=0&frame=1';

		}
		else if (element2 == 'map_size')
		{		
				if (document.getElementById('selected_size').value != document.getElementById('map_size').value)		
					var newURL = newURL +'&material='+ document.getElementById('material').value +'&map_size='+ document.getElementById('map_size').value +'&frame='+ document.getElementById('selected_framed').value +'&matte='+ document.getElementById('selected_matted').value;

		}
		else if (element2 == 'material')
		{		
					var newURL = newURL +'&map_size='+ document.getElementById('map_size').value +'&material='+ document.getElementById('material').value +'&frame='+ document.getElementById('selected_framed').value +'&matte='+ document.getElementById('selected_matted').value;

		}
		else
		{
			var newURL = newURL +'&frame='+ document.getElementById('selected_framed').value;
				if (document.getElementById('selected_matted').value == '0' && element > 0)
					var newURL = newURL +'&price=' + element +'&matte=1';
				else if (element < 0 && document.getElementById('selected_matted').value >= 1)
					var newURL = newURL +'&price=' + element +'&matte=0';
				else 
					var newURL = newURL +'&price=0&matte=1';
		}
		
	//window.alert(newURL);
	var xmlhttp=false; // Clear our fetching variable
	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); // Try the first kind of active x object
	}
	catch (e) {
		try {
			xmlhttp = new
			ActiveXObject('Microsoft.XMLHTTP'); // Try the second kind of active x object
		}
		catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest(); // If we were able to get a working active x object, start an XMLHttpRequest
	}

	// Build the URL to connect to
  	// Open a connection to the server
  	xmlhttp.open("GET", newURL, true);

  	// Setup a function for the server to run when it's done
  	xmlhttp.onreadystatechange=function() {
  	if (xmlhttp.readyState==4) { 
  		// Check if it is ready to recieve data
		var content = xmlhttp.responseText; // The content data which has been retrieved ***
		if(content) { 
			// Make sure there is something in the content variable
			document.getElementById(contentBox).innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
		}
	}
	}

	// Send the request
 	 xmlhttp.send(null);
}

function getMat_pr(url, contentBox, element) {
			
	var newURL = url + '&map_size=' + document.getElementById('map_size').value;
	//window.alert(newURL);
	var xmlhttp=false; // Clear our fetching variable
	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); // Try the first kind of active x object
	}
	catch (e) {
		try {
			xmlhttp = new
			ActiveXObject('Microsoft.XMLHTTP'); // Try the second kind of active x object
		}
		catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest(); // If we were able to get a working active x object, start an XMLHttpRequest
	}

	// Build the URL to connect to
  	// Open a connection to the server
  	xmlhttp.open("GET", newURL, true);

  	// Setup a function for the server to run when it's done
  	xmlhttp.onreadystatechange=function() {
  	if (xmlhttp.readyState==4) { 
  		// Check if it is ready to recieve data
		var content = xmlhttp.responseText; // The content data which has been retrieved ***
		if(content) { 
			// Make sure there is something in the content variable
			document.getElementById(contentBox).innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
		}
	}
	}

	// Send the request
 	 xmlhttp.send(null);
}

function getMaterial(url, contentBox) {
			
	var newURL = url + '&material='+ document.getElementById('material').value;
	//window.alert(newURL);
	var xmlhttp=false; // Clear our fetching variable
	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); // Try the first kind of active x object
	}
	catch (e) {
		try {
			xmlhttp = new
			ActiveXObject('Microsoft.XMLHTTP'); // Try the second kind of active x object
		}
		catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest(); // If we were able to get a working active x object, start an XMLHttpRequest
	}

	// Build the URL to connect to
  	// Open a connection to the server
  	xmlhttp.open("GET", newURL, true);

  	// Setup a function for the server to run when it's done
  	xmlhttp.onreadystatechange=function() {
  	if (xmlhttp.readyState==4) { 
  		// Check if it is ready to recieve data
		var content = xmlhttp.responseText; // The content data which has been retrieved ***
		if(content) { 
			// Make sure there is something in the content variable
			document.getElementById(contentBox).innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
		}
	}
	}

	// Send the request
 	 xmlhttp.send(null);
}
function getData(url, contentBox, element) {
			
	var newURL = url;
	if (getCheckedRadio(element) != '')
	{
	var newURL = newURL + '&frame=' + getCheckedRadio(element);
	}	
	//window.alert(newURL);
	var xmlhttp=false; // Clear our fetching variable
	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); // Try the first kind of active x object
	}
	catch (e) {
		try {
			xmlhttp = new
			ActiveXObject('Microsoft.XMLHTTP'); // Try the second kind of active x object
		}
		catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest(); // If we were able to get a working active x object, start an XMLHttpRequest
	}

	// Build the URL to connect to
  	// Open a connection to the server
  	xmlhttp.open("GET", newURL, true);

  	// Setup a function for the server to run when it's done
  	xmlhttp.onreadystatechange=function() {
  	if (xmlhttp.readyState==4) { 
  		// Check if it is ready to recieve data
		var content = xmlhttp.responseText; // The content data which has been retrieved ***
		if(content) { 
			// Make sure there is something in the content variable
			document.getElementById(contentBox).innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
		}
	}
	}

	// Send the request
 	 xmlhttp.send(null);
}
function justGet(url, contentBox, element) {
			
	var newURL = url;
	var newURL = newURL + '?id=' + document.getElementById(element).value;
	
	window.alert(newURL);
	var xmlhttp=false; // Clear our fetching variable
	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); // Try the first kind of active x object
	}
	catch (e) {
		try {
			xmlhttp = new
			ActiveXObject('Microsoft.XMLHTTP'); // Try the second kind of active x object
		}
		catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest(); // If we were able to get a working active x object, start an XMLHttpRequest
	}

	// Build the URL to connect to
  	// Open a connection to the server
  	xmlhttp.open("GET", newURL, true);

  	// Setup a function for the server to run when it's done
  	xmlhttp.onreadystatechange=function() {
  	if (xmlhttp.readyState==4) { 
  		// Check if it is ready to recieve data
		var content = xmlhttp.responseText; // The content data which has been retrieved ***
		if(content) { 
			// Make sure there is something in the content variable
			document.getElementById(contentBox).innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
		}
	}
	}

	// Send the request
 	 xmlhttp.send(null);
}
function getData_fr(url, contentBox, element) {
	var newURL = url + '&matte=' + getCheckedRadio(element);
	//window.alert(newURL);
	var xmlhttp=false; // Clear our fetching variable
	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); // Try the first kind of active x object
	}
	catch (e) {
		try {
			xmlhttp = new
			ActiveXObject('Microsoft.XMLHTTP'); // Try the second kind of active x object
		}
		catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest(); // If we were able to get a working active x object, start an XMLHttpRequest
	}

	// Build the URL to connect to
  	// Open a connection to the server
  	xmlhttp.open("GET", newURL, true);

  	// Setup a function for the server to run when it's done
  	xmlhttp.onreadystatechange=function() {
  	if (xmlhttp.readyState==4) { 
  		// Check if it is ready to recieve data
		var content = xmlhttp.responseText; // The content data which has been retrieved ***
		if(content) { 
			// Make sure there is something in the content variable
			document.getElementById(contentBox).innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
		}
	}
	}

	// Send the request
 	 xmlhttp.send(null);
}
function checkSelectedTown() {
	var msg = "";
	var error = 0;
	var send = true;
	if(!document.getElementById('town').value) {
		msg = msg + "Please fill in a town to continue.\n";
		error = 1;
	}
	if(error == '1') {
		window.alert(msg);
		send = false;
	}
	return send;
}
function checkSelectedMat() {
	var msg = "";
	var error = 0;
	var send = true;
	if(!document.getElementById('name').value) {
		msg = msg + "Please fill in a matting tile to continue.\n";
		error = 1;
	}
	if(error == '1') {
		window.alert(msg);
		send = false;
	}
	return send;
}
function checkSelectedFrame() {
	var msg = "";
	var error = 0;
	var send = true;
	if(!document.getElementById('name').value) {
		msg = msg + "Please fill in a frame tile to continue.\n";
		error = 1;
	}
	if(error == '1') {
		window.alert(msg);
		send = false;
	}
	return send;
}
