// JavaScript Document

// We may want to use the paresJSON function later

if(!String.prototype.parseJSON)
{(function(s){var m={'\b':'\\b','\t':'\\t','\n':' \\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};s.parseJSON=function()
{try{if( /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(this))
{return eval('('+this+')');}}catch(e) {}throw new SyntaxError("parseJSON");};})
(String.prototype);}

// Display Brands for the customer support form
function getSupportBrands()
{
	var x;
	var BrandDivContent;
	var previousBrand
	previousBrand = "";
	x = bindings.length;

	BrandDivContent = "<select name='brand' onChange='getSupportModels();' style='width:150px; overflow:hidden'><option value=''>Select a brand</option>";
	if (x != 0)
	{	
		for (i=0; i<x; i++)
		{
			if (previousBrand != bindings[i].brand)
			{
				BrandDivContent = BrandDivContent + "<option value='"+bindings[i].brand+"'>"+bindings[i].brand+"</option>";
				previousBrand = bindings[i].brand
			}
			
		}
	}
	BrandDivContent = BrandDivContent + "</select>";
	document.getElementById("BrandDisplay").innerHTML = BrandDivContent;
}
// Display Models for the customer support form
function getSupportModels()
{
	var SelectedBrand = ""
	var ModelDivContent = "";
	var x;
	x = bindings.length;
	SelectedBrand = document.Device.brand.value
	if (x != 0)
	{	
		if (SelectedBrand != "")
		{
			ModelDivContent = "<select name='modelselect' style='width:150px; overflow:hidden'>";
			ModelDivContent = ModelDivContent+"<option value=''>Select a model</option>"
			ModelDivContent = ModelDivContent+"<option value='viewall'>I don't know</option>"
			
			for (i=0; i<x; i++)
			{
				if (SelectedBrand == bindings[i].brand)
				{
					ModelDivContent = ModelDivContent + "<option value='"+bindings[i].model+"'>"+bindings[i].model+"</option>";
				}
			}
			ModelDivContent = ModelDivContent+"</select>";
			document.getElementById("ModelDisplay").innerHTML = ModelDivContent;
		}
	}
}

// Display Brands and Models for the PDA form.

function getBrands()
{
	var x;
	var BrandDivContent;
	var previousBrand
	previousBrand = "";
	x = bindings.length;

	BrandDivContent = "<select name='brand' onChange='getModels();' style='width:150px; overflow:hidden'><option value=''>Select a brand</option>";
	if (x != 0)
	{	
		for (i=0; i<x; i++)
		{
			if (previousBrand != bindings[i].brand)
			{
				BrandDivContent = BrandDivContent + "<option value='"+bindings[i].brand+"'>"+bindings[i].brand+"</option>";
				previousBrand = bindings[i].brand
			}
			
		}
	}
	BrandDivContent = BrandDivContent + "</select>";
	document.getElementById("BrandDisplay").innerHTML = BrandDivContent;
}

function getModels()
{
	var SelectedBrand = ""
	var ModelDivContent = "";
	var x;
	x = bindings.length;
	SelectedBrand = document.Device.brand.value
	if (SelectedBrand != "")
	{
		ModelDivContent = "<select name='model' onChange='displayDetails();' style='width:150px; overflow:hidden'>";
		ModelDivContent = ModelDivContent+"<option value=''>Select a model</option>"
		ModelDivContent = ModelDivContent+"<option value='viewall'>I don't know</option>"
		
		for (i=0; i<x; i++)
		{
			if (SelectedBrand == bindings[i].brand)
			{
				ModelDivContent = ModelDivContent + "<option value='"+bindings[i].model+"'>"+bindings[i].model+"</option>";
			}
		}
		ModelDivContent = ModelDivContent+"</select>";
		document.getElementById("ModelDisplay").innerHTML = ModelDivContent;
	}
}

var http = getHTTPObject();

function getDeviceDetails(brand, model_, image, ua)
{
  //alert(brand+", "+model_+", "+image+", "+ua)
  var CurrentSeletedmodel = document.Device.model.value;
  var url = "device_details.jsp?ua=";
  
  if (CurrentSeletedmodel != model_)
  {
	  var modelListIndex = document.Device.model.length;
	  for (i=0;i<modelListIndex;i++)
	  {
	  	if (model_ == document.Device.model[i].value)
		{
	 		document.Device.model[i].selected = true;
		}
	 }  
  }
  //alert(encoded_ua);
  http.open("GET", url+ua, true);
  http.onreadystatechange = handleHttpResponse;
  http.send("");
  if (IE = true)
  {
  	document.getElementById("DetailsDisplay").innerHTML = "";
  	//document.getElementById("DetailsDisplay").style.visibility = "visible";
	DeviceDisplayDiv = "<table width='350'><tr>"
	DeviceDisplayDiv = DeviceDisplayDiv + "<td align='center'><img src='"+OTAexURL+"/image.jsp?imageName="+image+"'><br>"+brand+"<br>"+model_+"</td>";
	DeviceDisplayDiv = DeviceDisplayDiv + "<td><div id='DownloaduserAgent'>Processing...&nbsp;&nbsp;<img src='images/status.gif'></td>";
	DeviceDisplayDiv = DeviceDisplayDiv + "</tr></table>";
  	document.getElementById("DetailsDisplay").innerHTML=DeviceDisplayDiv;
}
  else
  {
  	document.getElementById("DetailsDisplay").innerHTML= "";
  	//document.getElementById("DetailsDisplay").style.visibility = "visible";
	DeviceDisplayDiv = "<table width='350'><tr>"
	DeviceDisplayDiv = DeviceDisplayDiv + "<td align='center'><img src='"+OTAexURL+"/image.jsp?imageName="+image+"'><br>"+brand+"<br>"+model_+"</td>";
	DeviceDisplayDiv = DeviceDisplayDiv + "<td><div id='DownloaduserAgent'>Processing...&nbsp;&nbsp;<img src='images/status.gif'></td>";
	DeviceDisplayDiv = DeviceDisplayDiv + "</tr></table>";
  	document.getElementById("DetailsDisplay").innerHTML=DeviceDisplayDiv;
  }
}

function handleHttpResponse() 
{
	if (http.readyState == 4) 
  	{
		if(http.status == 200)
		{
			document.getElementById("DownloaduserAgent").innerHTML= "";
			document.getElementById("DownloaduserAgent").innerHTML = http.responseText;
		}
		else
		{
			document.getElementById("DownloaduserAgent").innerHTML= "";
			document.getElementById("DownloaduserAgent").innerHTML = "System Error: "+http.status+" <br/> "+http.statusText;
		}
  	}
}


function getHTTPObject() {
  var xmlhttp;
  try 
  {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	IE = true;
  } 
  catch (e) 
  {
     try 
	 {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		IE = true;
     } 
	 catch (E) 
	 {
        xmlhttp = false;
     }
  }
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try 
	{
      xmlhttp = new XMLHttpRequest();
    } 
	catch (e) 
	{
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

function displayDetails()
{
	var SelectedBrand = ""
	var SelectedModel = ""
	var displayDivContent = "";
	var x;
	var i;
	var tableCounter = 0

	x = bindings.length;	
	SelectedBrand = document.Device.brand.value
	SelectedModel = document.Device.model.value

	if (SelectedBrand != "")
	{
		if (SelectedModel != "")
		{
			if (SelectedModel == "viewall")
			{
				displayDivContent = "<table align=\"center\">";
				for (i=0; i<x; i++)
				{
 					if (SelectedBrand == bindings[i].brand)
					{
					
						if (tableCounter == 0)
						{
							displayDivContent = displayDivContent+"<tr>";
						}
						
						displayDivContent = displayDivContent+"<td align=center>";
						displayDivContent = displayDivContent+"<a href='#' OnClick=\"getDeviceDetails('"+SelectedBrand+"', '"+bindings[i].model+"', '"+bindings[i].image+"', '"+bindings[i].userAgent+"'); \">";
						displayDivContent = displayDivContent+"<img src='"+OTAexURL+"/image.jsp?imageName="+bindings[i].image+"' border='0'>";
						displayDivContent = displayDivContent+"</a>";
						displayDivContent = displayDivContent+"<a href='#' OnClick=\"getDeviceDetails('"+SelectedBrand+"', '"+bindings[i].model+"', '"+bindings[i].image+"', '"+bindings[i].userAgent+"'); \">";
						displayDivContent = displayDivContent+"<br>"+bindings[i].brand;
						displayDivContent = displayDivContent+"<br/>"+bindings[i].model;
						displayDivContent = displayDivContent+"</a>";
						displayDivContent = displayDivContent+"</td>";
						tableCounter++;
						
						if (tableCounter == 3)
						{
							displayDivContent = displayDivContent+"</tr>"
							tableCounter = 0;
						}
						
					}
				}
				displayDivContent = displayDivContent+"</table>";
				document.getElementById("DetailsDisplay").innerHTML = displayDivContent;
			}
			else
			{	
				for (i=0; i<x; i++)
				{	
					if (SelectedBrand == bindings[i].brand)
					{
						if (SelectedModel == bindings[i].model)
						{
							getDeviceDetails(SelectedBrand, SelectedModel, bindings[i].image, bindings[i].userAgent)
						}
					}
				}
			}
		}
		else
		{
			document.getElementById("DetailsDisplay").innerHTML = "<br>Please select a model.<br>";
		}
	}
	else
	{
		document.getElementById("DetailsDisplay").innerHTML = "<br>Please select a brand.<br>";
	}	
}
