function createRequestObject(){
	var xmlHttp;
	try{// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

function getPhoneModel(phoneMakeValue){
	var request1 = createRequestObject();
	request1.open("GET", "transport.php?action=http://www.uniquephones.com/projects/support/brita/getmodels.php&method=GET&make="+phoneMakeValue, true);
	request1.onreadystatechange = function(){
											if(request1.readyState==4){
												var xmlDoc = request1.responseXML;
												var models = xmlDoc.childNodes[0].childNodes;
												var phoneModel = document.getElementById("model_id");
												phoneModel.options.length = 0;
												for(i=0; i< models.length; i++){
													var optionID = models[i].getAttribute("id");
													var optionText = models[i].firstChild.nodeValue;
													var newOption = document.createElement("OPTION");
													newOption.text = optionText;
													newOption.value = optionID;
													phoneModel.options.add(newOption);
												}
												var span = document.getElementById("selectphoneModel");
												span.innerHTML = models[0].firstChild.nodeValue;
											}
									};
	request1.send(null);
}

function getCountry(phoneMakeValue){
	var request2 = createRequestObject();
	request2.open("GET", "transport.php?action=http://www.uniquephones.com/projects/support/brita/getcarriers.php&method=GET&make="+phoneMakeValue, true);
	request2.onreadystatechange = function(){
											if(request2.readyState==4){
												var xmlDoc = request2.responseXML;
												var countries = xmlDoc.childNodes[0].childNodes;
												var countriesControl = document.getElementById("country_id");
												countriesControl.options.length = 0;
												for(i=0; i< countries.length; i++){
													var optionID = countries[i].getAttribute("name");
													var optionText = optionID;
													var newOption = document.createElement("OPTION");
													newOption.text = optionText;
													newOption.value = optionID;
													countriesControl.options.add(newOption);
												}
											}
									};
	request2.send(null);
}

function getCarrier(phoneMakeValue, country){
	var request3 = createRequestObject();
	request3.open("GET", "transport.php?action=http://www.uniquephones.com/projects/support/brita/getcarriers.php&method=GET&make="+phoneMakeValue, true);
	request3.onreadystatechange = function(){
											if(request3.readyState==4){
												var xmlDoc = request3.responseXML;
												var countries = xmlDoc.childNodes[0].childNodes;
												var carrierContlrol = document.getElementById("carried_id");
												carrierContlrol.options.length = 0;
												var newOption1 = document.createElement("OPTION");
												newOption1.text = '---';
												newOption1.value = '---';
												carrierContlrol.options.add(newOption1);
												for(i=0; i< countries.length; i++){
													var getCountry = countries[i].getAttribute("name");
													if(country == getCountry){
														var carriers = countries[i].childNodes;
														for(var j=0; j < carriers.length; j++){
															var optionID = carriers[j].getAttribute("id");
															var optionText = carriers[j].firstChild.nodeValue;
															var newOption = document.createElement("OPTION");
															newOption.text = optionText;
															newOption.value = optionID;
															carrierContlrol.options.add(newOption);
														}
													}
												}
											}
									};
	request3.send(null);
}

function dispResult(phoneModelValue, carrier){
	if(carrier != '---'){
	var request4 = createRequestObject();
	request4.open("GET", "transport.php?action=http://www.uniquephones.com/projects/support/brita/result.php&method=GET&phone="+phoneModelValue+"&carrier="+carrier, true);
	request4.onreadystatechange = function(){
											if(request4.readyState==4){
												var xmlDoc = request4.responseXML;
												document.getElementById("displayResult").style.display="block";
												//show options here
												var optionstag = xmlDoc.getElementsByTagName("options")[0].childNodes;
												var optionsHTML = '';
												optionsHTML += '<a href="text.php?phone='+document.getElementById('model_id').value+'&carrier='+document.getElementById('carried_id').value+'"><img src="image/go.gif" width="60" height="20" style="border:0px;"/></a><br /><br />';
												for(var i=0; i < optionstag.length; i++){
													var nodeval = new String(optionstag[i].firstChild.nodeValue);
													optionsHTML += '<b>Option: </b>'+nodeval+"<br />";
													try{
														var optionval = xmlDoc.getElementsByTagName(nodeval.toLowerCase())[0].childNodes;
														for(var j=0; j < optionval.length; j++){
															if(optionval[j].tagName == "waittime"){
																optionsHTML += '<b>Wait Time: </b>'+optionval[j].firstChild.nodeValue+'<br />';
															}
															if(optionval[j].tagName == "pricing"){
																optionsHTML += '<b>Pricing: </b><br />';
																var priceval = optionval[j].childNodes;
																for(var k = 0; k < priceval.length; k++){
																	optionsHTML += priceval[k].tagName +": "+priceval[k].firstChild.nodeValue+"<br />";
																}
																optionsHTML += '<br />';
															}
														}
													}catch(e){
														// N
													}
												}
												//alert(optionsHTML);
												document.getElementById("options_id").innerHTML = optionsHTML;

												/*// show wait time here
												document.getElementById("waittime_id").innerHTML = xmlDoc.getElementsByTagName("waittime")[0].firstChild.nodeValue+"<br />";

												// Get the pricing
												var pricing = xmlDoc.getElementsByTagName("pricing")[0].childNodes;
												
												var priceHTML = '<br />';
												for(var i=0; i < pricing.length; i++){
													priceHTML += pricing[i].tagName +": "+pricing[i].firstChild.nodeValue+"<br />";
												}
												document.getElementById("pricing_id").innerHTML = priceHTML;*/
											}
									};
	request4.send(null);
	}
}

function getInitialValues(){
	getPhoneModel(document.getElementById('make_id').options[0].value); 
	getCountry(document.getElementById('make_id').options[0].value);
	setTimeout("getInitialCarrier();", 1000);
	setTimeout("getInitialResult();", 1000);
}
function getInitialCarrier(){
	getCarrier(document.getElementById('make_id').value, document.getElementById('country_id').value);
}
function getInitialResult(){
	dispResult(document.getElementById('model_id').value, document.getElementById('carried_id').value)
}
