

function loadCarriers() {

	   var requestCarriers = new Ajax.Request("/Carrier/",
		{
			method: 'get',
			parameters: null,
			onComplete: createCarriers,
			onFailure: function() { alert("There was an error while attempting to contact the server."); }.bind(this),
			onException: function(t, e) { alert("There was an error while attempting to contact the server: " + e.message); }.bind(this)
		});
}

function createCarriers(xmlhttp) {

      if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
	  
	  	  var contentDiv = document.getElementById('supported_phones_content');
	  	  contentDiv.innerHTML = '';
		  try {
		  	var result = eval("(" + xmlhttp.responseText + ")");
		  }
		  catch(e) {
			alert(e);
		  }
		  
		  var myForm = document.createElement('form');
			  myForm.setAttribute("id", "carriers");
			  myForm.setAttribute("action", "/View/CollectHandsetInfo");
			  myForm.setAttribute("method", "POST");
		  
		  
		  var carrierNameElement = document.createElement("input");
			 carrierNameElement.name = "carrier_name";
			 carrierNameElement.value = result.results[0].carrier_name;
			 carrierNameElement.setAttribute("type", "hidden");
			 
		  myForm.appendChild(carrierNameElement);
		  
		  var mySelect = document.createElement('select');
			  mySelect.setAttribute("name", "carrier_id");
			  mySelect.setAttribute("id", "carrier_id");
		  
		  mySelect.onchange = function() {
			  								loadSelected(); 
		  									carrierNameElement.value = result.results[mySelect.selectedIndex].carrier_name;
										 };
		  
		  
		  for(var i=0; i<result.total_items; i++)
		  {	  
			  var optionElement = document.createElement("option");
			  optionElement.value = result.results[i].carrier_id;
			  optionElement.appendChild(document.createTextNode(result.results[i].carrier_name));
			  mySelect.appendChild(optionElement);
		  }
		  
		  myForm.appendChild(mySelect);
		  contentDiv.appendChild(myForm);
      }  
}

function loadSelected() {
		var myForm = document.getElementById('carriers');
		loadModels(document.getElementById('carrier_id').selectedIndex);
}

function loadModels(carrier) {
	
	   var params = 'carrier_id='+carrier+'&per_page=100';
	   var requestModels = new Ajax.Request('/Device/',
		{
			method: 'get',
			parameters: params,
			onComplete: createModels,
			onFailure: function() { alert("There was an error while attempting to contact the server."); }.bind(this),
			onException: function(t, e) { alert("There was an error while attempting to contact the server: " + e.message); }.bind(this)
		});
}

function createModels(xmlhttp) {
      if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
	  
	  	  var contentDiv = document.getElementById('supported_phones_content');
	  	  var myForm = document.getElementById('carriers');

		  try {
		  	var result = eval("(" + xmlhttp.responseText + ")");
		  }
		  catch(e) {
			alert(e);
		  }
		 
			 while(myForm.childNodes.length > 2) {
				 
			 	myForm.removeChild(myForm.childNodes[myForm.childNodes.length-1]);
			
			 }
		 
		 if(result.results.length > 0) //create the dropdown of models
		 {	
		 	 var modelnameElement = document.createElement("input");
			 modelnameElement.name = "model_name";
			 modelnameElement.value = result.results[0].model_name;
			 modelnameElement.setAttribute("type", "hidden");
			 
			 myForm.appendChild(modelnameElement);
			 
			 var selectElement = document.createElement("select");
			 selectElement.name = "device_id";
			 selectElement.onchange = function() { modelnameElement.value = result.results[selectElement.selectedIndex].model_name};
			 
			  for(var i=0; i < result.results.length; i++)
			  {
				 var optionElement = document.createElement("option");
				 optionElement.value = result.results[i].device_id;
				 optionElement.appendChild(document.createTextNode(result.results[i].device_name));
				 selectElement.appendChild(optionElement);
			  }
			  myForm.appendChild(selectElement);
		 } 
		 else 
		 { //if no models in DB create a text field to type in the model name
		 	  
			 var deviceIdElement = document.createElement("input");
			 deviceIdElement.name = "device_id";
			 deviceIdElement.value = "0";
			 deviceIdElement.setAttribute("type", "hidden");
			 
			 myForm.appendChild(deviceIdElement);
			  
			  var modelInput = document.createElement("input");
			  modelInput.setAttribute("name", "model_name");
			  modelInput.setAttribute("id", "models");
			  modelInput.setAttribute("type", "text");
			  modelInput.setAttribute("size", 20);
			  modelInput.setAttribute("value", "phone model");
		 
		 	  myForm.appendChild(modelInput);
		 }
		 
		 
		  //email
		  var inputElement = document.createElement("input");
		  inputElement.setAttribute("name", "email_address");
		  inputElement.setAttribute("id", "email");
		  inputElement.setAttribute("type", "text");
		  inputElement.setAttribute("size", 20);
		  inputElement.setAttribute("value", "email address");
		  
		  myForm.appendChild(inputElement);
		  
		  //phone number
		   var inputElement2 = document.createElement("input");
		  inputElement2.setAttribute("name", "mobile_number");
		  inputElement2.setAttribute("id", "phone");
		  inputElement2.setAttribute("type", "text");
		  inputElement2.setAttribute("size", 20);
		  inputElement2.setAttribute("value", "mobile number");
		  
		  
		  myForm.appendChild(inputElement2);
		  
		  //submit
		  var submitElement = document.createElement("input");
		  submitElement.setAttribute("class", "button");
		  submitElement.setAttribute("type", "submit");
		  submitElement.setAttribute("value", "Submit");
		  
		  myForm.appendChild(submitElement);
     }  
}



function loadPhoneContent(content) {
	var contentDiv = document.getElementById('supported_phones_content');
	
	switch(content) {
		
		case 'alltel':
			contentDiv.innerHTML = '<ul class="instruction">To get Rabble on your Alltel phone:<li>Download the Rabble application by selecting the Axcess icon from the main menu on your phone</li><li>Select Catalog</li><li>Search for Rabble and select the title to purchase.</li></ul>';
			break;
			
		
		case 'tmo':
			contentDiv.innerHTML = '<ul class="instruction">To get Rabble on your TMobile Sidekick:<li>*Requires developer key from developer.danger.com</li><li><a href="http://www.skdr.net/index.php?op=applications/show_view&application_id=533">get the download here</a></li></ul>';
			break;
			
		case 'tmo2':
			contentDiv.innerHTML = '<ul class="instruction">To get Rabble on your phone, goto:<li>Menu -> T-Zones -> Beta Apps -> Social Connections</li></ul>';
			break;
			
		case 'centennial':
			contentDiv.innerHTML = '<ul class="instruction">Descargar Rabble en tu tel&eacute;fono de Centennial Puerto Rico, es muy f&aacute;cil:<li>Simplemente selecciona el icono de Go Fwd o BREW&#8482; desde tu celular</li><li>Selecciona el carrito de compras</li><li>Selecciona la categor&iacute;a de Conocer Amigos/Meet Friends y despu&eacute;s selecciona Rabble para comprar</li><li>La aplicaci&oacute;n ser&aacute; descargada automaticamente a tu tel&eacute;fono</li></ul>';
			break;
		
		
		case 'sprint':
			contentDiv.innerHTML = '<ul class="instruction">Getting Rabble on your Sprint phone is a piece of cake!<li>Download the Rabble application by selecting the Web option from the main menu on your phone</li><li>Select Messaging</li><li>Select the "Social Zones" category and then select Rabble to purchase</li><li>The application will automatically be downloaded to your phone</li></ul>';
			break;
			
		case 'cingular':
			contentDiv.innerHTML = 'Getting Rabble on your AT&amp;T wireless phone is a piece of cake!<br/><br/><span style="position:relative;left:5;font-weight:bold">Enter your 10 digit mobile phone number:<span style="position:relative;top:5"><form action="/View/About/supported_phones" method="post"><input maxlength="10" type="textbox" name="ph_no"/><input type="hidden" name="provider" value="'+ content +'"><input type="submit" value="Go!"/></form></span><span style="position:relative;top:15">You will receive a text message from us with a link to get rabble - how easy is that?<BR/><BR/>To access Rabble on your phone in the future, select Games &amp; Apps and then select Rabble.<BR/><BR/></span>';
			break;
			
		case 'cingular_v1':
			contentDiv.innerHTML = '<ul>To download Rabble on your Wireless phone:<li>From your phone’s menu, select AT&amp;T Mall.</li><li>Select Shop Applications.</li><li>Select the Entertainment category.</li><li>Select Rabble.</li><li>Select Subscribe to download Rabble.</li><li>The application will now download and auto-install on your phone.</li></ul>';
			break;
		
		case 'cellular_south':
			contentDiv.innerHTML = '<b>Getting Rabble on your Cellular South phone is a piece of cake!</b><br />&middot; Download the Rabble application by selecting the BREW&trade; icon on your phone<br />&middot; Search for Rabble or find it under the Mixed Bag category<br />&middot; Select Rabble to purchase<br />&middot; The application will be automatically downloaded to your phone<br />';
			break;
			
		case 'cricket':
			contentDiv.innerHTML = '<b>Getting Rabble on your Cricket Clicks enabled phone is a piece of cake!</b><br />&middot; Download the Rabble application by selecting the Cricket Clicks&trade; or BREW&trade; icon on your phone<br />&middot; Click on the Shopping Cart <br />&middot; Select the Entertainment category and then select Rabble to purchase<br />&middot; The application will be automatically downloaded to your phone<br />';
			break;
			
		case 'metro_pcs':
			contentDiv.innerHTML = '<b>Getting Rabble on your Metro PCS phone is a piece of cake!</b><br />&middot; Download the Rabble application by selecting the @metro icon on your @metro enabled phone<br />&middot; Select the Shopping Cart to browse the @metro shop.<br />&middot; Select the @pics category and select Rabble to purchase.<br />&middot; Once you approve the payment with funds from your metro&shy;connect account, the application will automatically download. You can start using the application as soon as the download is complete.';
			break;
			
		case 'verizon_v1':
			contentDiv.innerHTML = '<b>To download Rabble on your Wireless phone:</b><br>&middot; On your Get it Now® enabled phone, select the Get It Now icon.<br/>&middot; Select the Shopping Cart to access the Get It Now shop.<br>&middot; Select Get Going.<br>&middot; Select the Communications category and select the Rabble application.<br>&middot; Click to subscribe.<br/><br/><b>The application will now download and auto-install on your phone.</b><br/><br/><b>To access Rabble on your phone in the future, access the Get It Now shop on your phone and select Rabble.</b>';
			break;
			
		case 'claro_pr':
			contentDiv.innerHTML = '<ul class="instruction">Para descargar Rabble a tu celular:<li>En tu tel&eacute;fono con Get it Now&reg;, selecciona el icono de Get in Now&reg;.</li><li>Selecciona el carrito de compras para accesar a la tienda de Get It Now&reg;.</li><li>Selecciona Get Going.</li><li>Selecciona la categor\'ia de la Comunidad y selecciona la aplicaci&oacute;n de Rabble</li></ul>';
			break;
		
		case 'virgin':
			contentDiv.innerHTML = '<b>Getting Rabble on your Virgin Mobile phone is a piece of cake!</b><br/><br/>&middot; Download the Rabble application by going to <b>Menu</b> > <b>VirginXL</b> > <b>Messaging</b> > <b>Rabble</b> on your Virgin Mobile phone.<br/>&middot; In a second or two, an alert will pop up on your phone asking you to complete the download.<br/>&middot; Once it\'s finished downloading, launch Rabble by going to <b>Menu</b> > <b>My Stuff</b>.';
			break;
			
			case 'verizon':
			contentDiv.innerHTML = '<ul class="instruction">Getting Rabble on your Get It Now enabled phone is easy!<li>Download the Rabble application by selecting the Get Going or icon on your phone</li><li>Select Get Going</li><li>Select the "Community" category and then select Rabble to purchase.</li><li>The application will be automatically downloaded to your phone</li></ul>';
			break;
			
			case 'boost':
			contentDiv.innerHTML = '<ul class="instruction">Getting Rabble on your Boost Mobile phone is easy!<li>Download the Rabble application by pressing the up directional arrow from the Main Screen.</li><li>Select "Date &amp; Chat"</li><li>Select Rabble to preview and purchase.</li><li>The application will be automatically downloaded to your phone.</li></ul>';
			break;
			
		case 'not_supported':
			loadCarriers();
			break;
	}
	
	contentDiv.style.display = "block";
}
