/* The following function creates an XMLHttpRequest object... */
var valor="";
var price=0;
var totalprice=0;
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function clearRB(buttonGroup)
{
  for (i=0; i < buttonGroup.length; i++) {
 
    if (buttonGroup[i].checked == true) { // if a button in group is checked,
          buttonGroup[i].checked = false;  // uncheck it
      }
     
  }  
}

function hideBlocks()
{
                document.getElementById('mobilezone').style.display='none';
                document.getElementById('summary').style.display='none';
}

function clearValues(block)
{
// document.form_category_select.payment[0].checked=false;
// document.form_category_select.payment[1].checked=false;

/*
if (typeof(document.form_category_select.typemap) != "undefined")
{
   delete document.form_category_select.typemap;
}
if (typeof(document.form_category_select.cobertura) != "undefined")
   delete document.form_category_select.cobertura;
if (typeof(document.form_category_select.select_regions_select) != "undefined")
   delete document.form_category_select.select_regions_select;
  document.getElementById('summary').style.display="none";
*/
if(block == 1)
{
  document.getElementById('product_cage').innerHTML="";
  document.getElementById('selectmap').innerHTML="";
  document.getElementById('mobile').innerHTML="";
  document.getElementById('resume').innerHTML="";
  document.getElementById('payform').innerHTML="";
}

if(block == 2)
{
  document.getElementById('selectmap').innerHTML="";
  document.getElementById('mobile').innerHTML="";
  document.getElementById('resume').innerHTML="";
  document.getElementById('payform').innerHTML="";
  if (typeof(document.form_category_select.typemap) != "undefined")
  {
    clearRB(document.form_category_select.typemap);
  }
}

if(block == 3)
{
  document.getElementById('mobile').innerHTML="";
  document.getElementById('resume').innerHTML="";
  document.getElementById('payform').innerHTML="";
}

if(block == 4)
{
  document.getElementById('resume').innerHTML="";
  document.getElementById('payform').innerHTML="";
}
}


function setInfoParams()
{
if (typeof(document.form_category_select.extraprice) != "undefined")
{
  price=getCheckedValue(document.form_category_select.extraprice);
  if(price==1)
  {
   totalprice=3;
  }
  if(price==0)
  {
   totalprice=0;
  }

}
else
   totalprice=0;



if (typeof(document.form_category_select.typemap) != "undefined")
{
  valor=getCheckedValue(document.form_category_select.typemap);
  if(valor == 'regions')
  { 
     document.form_category_select.maptype.value="regions";
      var choice=document.form_category_select.select_regions_select.selectedIndex;
      document.form_category_select.mapid.value=document.form_category_select.select_regions_select.options[choice].value;
  }

  if(valor == 'countries')
  {
   document.form_category_select.maptype.value="countries";
   var choice=document.form_category_select.select_countries_select.selectedIndex;
   document.form_category_select.mapid.value=document.form_category_select.select_countries_select.options[choice].value;
  } 

  if(valor == '') valor="regions";

}
else
{
 if (typeof(document.form_category_select.cobertura) != "undefined")
 {
   valor="regions";
   document.form_category_select.mapid.value=document.form_category_select.cobertura.value;
 }
  else
 {
   valor="regions";
   if (typeof(document.form_category_select.cobertura) != "undefined")
   {
     document.form_category_select.mapid.value=document.form_category_select.cobertura.value;
   }
  }

}

   var choice=document.form_category_select.select_category_select.selectedIndex;
   document.form_category_select.idproduct.value=document.form_category_select.select_category_select.options[choice].value;
   var choice=document.form_category_select.select_modelos.selectedIndex;
   document.form_category_select.idmodel.value=document.form_category_select.select_modelos.options[choice].value;
}

function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

/* You can get more specific with version information by using 
	parseInt(navigator.appVersion)
	Which will extract an integer value containing the version 
	of the browser being used.
*/
/* The variable http will hold our new XMLHttpRequest object. */
var http = createRequestObject(); 

/* Function called to get the product categories list */
function getProducts(){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
		We can reference document.form_category_select.select_category_select and we will 		
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
        clearValues(1);
	var choice=document.form_category_select.select_category_select.selectedIndex;
	http.open('get', '/internal_request.php?action=get_products&id=' 
			+ document.form_category_select.select_category_select.options[choice].value);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleProducts; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleProducts(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		document.getElementById('product_cage').innerHTML = response;
		document.getElementById('mobilezone').style.display='none';
		document.getElementById('summary').style.display='none';
	}
}
function getMobile(){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
		We can reference document.form_category_select.select_category_select and we will 		
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
	setInfoParams();

        clearValues(3);
	http.open('get', '/internal_request.php?action=get_mobile&modelid=' 
	+document.form_category_select.idmodel.value+'&mapid='+document.form_category_select.mapid.value+
        '&mapcover='+valor);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleMobile; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleMobile(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		document.getElementById('mobile').innerHTML = response;
		document.getElementById('mobilezone').style.display='block';
	}
}

function getSummary(){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
		We can reference document.form_category_select.select_category_select and we will 		
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
	setInfoParams();

        clearValues(4);
	http.open('get', '/internal_request.php?action=get_summary&modelid=' 
	+document.form_category_select.idmodel.value+'&mapid='+document.form_category_select.mapid.value+
        '&mapcover='+valor+'&extramob='+totalprice);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleSummary; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleSummary(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		document.getElementById('summary').style.display='block';
		document.getElementById('resume').innerHTML = response;
	}
}

function getInstructions(){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
		We can reference document.form_category_select.select_category_select and we will 		
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
        clearValues(2);
	var choice=document.form_category_select.select_modelos.selectedIndex;
	if(choice != 0)
        {
	http.open('get', '/internal_request.php?action=get_instructions&id=' 
			+ document.form_category_select.select_modelos.options[choice].value);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleInstructions; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
	}

}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleInstructions(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		document.getElementById('info').innerHTML = response;
	}
}

function getModels(){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
		We can reference document.form_category_select.select_category_select and we will 		
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
        clearValues(2);
	var choice=document.form_category_select.select_modelos.selectedIndex;
	http.open('get', '/internal_request.php?action=get_models&id=' 
			+ document.form_category_select.select_modelos.options[choice].value);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleModels; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleModels(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		getInstructions();
		document.getElementById('selectmap').innerHTML = response;
		document.getElementById('mobilezone').style.display='none';
		document.getElementById('summary').style.display='none';
		document.getElementById('selectmap').style.display='block';
	}
}

function getPayform(){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
		We can reference document.form_category_select.select_category_select and we will 		
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
	http.open('get', '/internal_request.php?action=get_payform&email=' 
	+ document.form_category_select.email.value+'&paymethod='
        + document.form_category_select.paymethod.value+'&maptype='
        + valor+'&mapid='
        + document.form_category_select.mapid.value+'&idproduct='
        + document.form_category_select.idproduct.value+'&idmodel='
        + document.form_category_select.idmodel.value+'&extramob='
        + totalprice); 
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handlePayform; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handlePayform(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		document.getElementById('payform').innerHTML = response;
		document.getElementById('payform').style.display='block';
	}
}

