// JavaScript Document

function createRequestObject() 
{
      http_request = false;
      if ( window.XMLHttpRequest ) // Mozilla, Safari,...
      { 
         http_request = new XMLHttpRequest();
         if ( http_request.overrideMimeType ) 
         {
            http_request.overrideMimeType('text/xml');
         }
      }
      else if( window.ActiveXObject ) // IE 
      { 
         try 
         {
            http_request = new ActiveXObject( "Msxml2.XMLHTTP" );
         } 
         catch (e) 
         {
            try 
            {
               http_request = new ActiveXObject( "Microsoft.XMLHTTP" );
            }
            catch (e) 
            {
              alert("Cannot create XMLHTTP instance");
            }
         }
      }
      return http_request;
}

function sndReq( url, condition ) 
{
    http.open( 'GET', url );
    http.onreadystatechange = function() { handleResponse( condition ); };
    http.setRequestHeader('Content-Type',  "text/xml");
    http.send(null);
}


function handleResponse( condition ) 
{
  if( http.readyState == 4  && http.status == 200 )
  {
	result = http_request.responseText;
	
	 Opts = result.split('|');
	 getKeys = Opts[0].split(';');
	 getValues = Opts[1].split(';');
	 
	 document.getElementById( condition ).options.length = 0;
	 
	 
	 for(i=0; i<getKeys.length; i++) {
		document.getElementById( condition ).options[i] = new Option(getValues[i],getKeys[i]);
    } 
  }
} 

var http = createRequestObject();
var result = null;
var Details = new Array();


			
function getCountry(indice)
{
	document.getElementById('country').options.length = 0;
	if(indice==0)
	{
		alert("Selezionare un continente");
		document.getElementById('country').options.length = 0;
		return false;
	}

	if(indice!=0)
	{
		sndReq( 'getCountry.php?id='+ indice, 'country' );
	}
}

function controlForm( fields )
{
	fields = fields.split('|');
	for( i=0; i<=fields.length; i++ )
	{
		if( fields[i] )
		{
			if( document.getElementById(fields[i]).value == 0 || document.getElementById(fields[i]).value == '' )
			{
				alert( "I campi che hanno un asterisco sono obbligatori, controllare eventuali mancanze." );
				return false;
			}
		}
	}
	document.formDistributors.submit();
	return true;
}


function proof( url, dealers ){
	if( dealers != 0 )
	{
		question = confirm("Eliminando questo distributore saranno eliminati "+dealers+" dealers, continuare?");
	}
	else
	{
		question = confirm("Sicuri di voler eliminare questo distributore?");
	}
	if (question !="0"){
		top.location = url;
	}
	else
	{
		return false;
	}
}

function proof2( url ){
	question = confirm("Sicuri di voler eliminare questo dealers?");
	if (question !="0"){
		top.location = url;
	}
	else
	{
		return false;
	}
}

function proof3( url ){
	question = confirm("Sicuri di voler eliminare questa news?");
	if (question !="0"){
		top.location = url;
	}
	else
	{
		return false;
	}
}


function sndReq2( url, indice ) 
{
    http.open( 'GET', url );
    http.onreadystatechange = function() { handleResponse2( indice ); };
    http.setRequestHeader('Content-Type',  "text/xml");
    http.send(null);
}


function handleResponse2( indice ) 
{
  if( http.readyState == 4  && http.status == 200 )
  {
	result = http_request.responseText;
	
	 Opts = result.split('|');
	 getKeys = Opts[0].split(';');
	 getValues = Opts[1].split(';');
	 
	 document.getElementById( 'distributors' ).options.length = 0;
	 
	 
	 for(i=0; i<getKeys.length; i++) {
		document.getElementById( 'distributors' ).options[i] = new Option(getValues[i],getKeys[i]);
		if(indice==getKeys[i])
		{
		document.formDistributors.distributors.options[i].selected = true;
		}
    } 
  }
} 

function getDistributors( country, indice )
{
	sndReq2('getDistributors.php?id='+ country, indice );
}


function Popup(img,w,h) {
   var w = w;
   var h = h;
   var l = Math.floor((screen.width-w)/2);
   var t = Math.floor((screen.height-h)/2);
      window.open("popup.php?id="+img,"","width=" + w + ",height=" + h + ",top=" + t + ",left=" + l);
 }