// JavaScript Document
function getDistributor(postcode)
{
  var xmlhttp=false; //Clear our fetching variable
  try {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object�
       } 
  catch (e) 
    {
      try 
	    {
          xmlhttp = new
          ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
        } 
		catch (E) 
		  {
            xmlhttp = false;
          }
     }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
    {
      xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
    }
  var file = 'includes/get_distributor.php?postcode='; //This is the path to the file that retrieves the distributors for this postcode *
  xmlhttp.open('GET', file + postcode, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
  xmlhttp.onreadystatechange=function() 
	 {
      if (xmlhttp.readyState==4)  //Check if it is ready to recieve data
        {      
	     var content = xmlhttp.responseText; //The content data which has been retrieved ***
         if(content.length > 1)  //Make sure there is something in the content variable
		   { 
             document.getElementById('result_content').innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
             document.getElementById('submit').innerHTML = '<input type="image" src="../images/show_my_distributor_button.png" class="submit" name="submit" /><br />';
		   }
         }		
	  else {
		document.getElementById('result_content').innerHTML = '<div id="check_dist_code"><img src="../images/wait.gif" class="wait" /></div>';
      	document.getElementById('submit').innerHTML = '';
	    }
	  }
  xmlhttp.send(null) //Nullify the XMLHttpRequest
  return;
}




function checkDistributorInUse(dist_id)
{
  var xmlhttp=false; //Clear our fetching variable
  try {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object�
       } 
  catch (e) 
    {
      try 
	    {
          xmlhttp = new
          ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
        } 
		catch (E) 
		  {
            xmlhttp = false;
          }
     }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
    {
      xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
    }
  var file = '../includes/check_distributor.php?dist_id='; //This is the path to the file that retrieves the distributors for this postcode *
  xmlhttp.open('GET', file + dist_id, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
  xmlhttp.onreadystatechange=function() 
	 {
      if (xmlhttp.readyState==4)  //Check if it is ready to recieve data
        {      
	     var content = xmlhttp.responseText; //The content data which has been retrieved ***
         if( content )  //Make sure there is something in the content variable
		   { 
             document.getElementById('check_dist_code').innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
           }
         }
      }
  xmlhttp.send(null) //Nullify the XMLHttpRequest
  return;
}


function getCurrentLastDist()
{
  var xmlhttp=false; //Clear our fetching variable
  try {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object�
       } 
  catch (e) 
    {
      try 
	    {
          xmlhttp = new
          ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
        } 
		catch (E) 
		  {
            xmlhttp = false;
          }
     }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
    {
      xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
    }
  var file = '../includes/check_distributor.php?count=yes'; //This is the path to the file that retrieves the distributors for this postcode *
  xmlhttp.open('GET', file, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
  xmlhttp.onreadystatechange=function() 
	 {
      if (xmlhttp.readyState==4)  //Check if it is ready to recieve data
        {      
	     var content = xmlhttp.responseText; //The content data which has been retrieved ***
         if( content )  //Make sure there is something in the content variable
		   { 
             var eleDIV;
			 eleDIV = document.getElementById('new_distributor_code');
			 eleDIV.value = content; //Change the conent of value tag to the newly retrieved content ****
           }
         }
      }
  xmlhttp.send(null) //Nullify the XMLHttpRequest
  return;
}



function checkDistUsernameInUse(username)
{
  var xmlhttp=false; //Clear our fetching variable
  try {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object�
       } 
  catch (e) 
    {
      try 
	    {
          xmlhttp = new
          ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
        } 
		catch (E) 
		  {
            xmlhttp = false;
          }
     }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
    {
      xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
    }
  var file = '../includes/check_distributor_username.php?name='; //This is the path to the file that retrieves the distributors for this postcode *
  xmlhttp.open('GET', file + username, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
  xmlhttp.onreadystatechange=function() 
	 {
      if (xmlhttp.readyState==4)  //Check if it is ready to recieve data
        {      
	     var content = xmlhttp.responseText; //The content data which has been retrieved ***
         if( content )  //Make sure there is something in the content variable
		   { 
             document.getElementById('new_username_result').innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
           }
         }
      }
  xmlhttp.send(null) //Nullify the XMLHttpRequest
  return;
}




function getKit(group, direction)
{
  var xmlhttp=false; //Clear our fetching variable
  try {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object�
       } 
  catch (e) 
    {
      try 
	    {
          xmlhttp = new
          ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
        } 
		catch (E) 
		  {
            xmlhttp = false;
          }
     }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
    {
      xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
    }
  var file = 'includes/navigate_kits.php?group=' + group + '&direction='; //This is the path to the file that retrieves the distributors for this postcode *
  xmlhttp.open('GET', file + direction, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
  xmlhttp.onreadystatechange=function() 
	 {
      if (xmlhttp.readyState==4)  //Check if it is ready to recieve data
        {      
	     var content = xmlhttp.responseText; //The content data which has been retrieved ***
         if( content )  //Make sure there is something in the content variable
		   { 
             document.getElementById('kit_result').innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
           }
         }
      }
  xmlhttp.send(null) //Nullify the XMLHttpRequest
  return;
}


function getPrice(shedman)
{
  // Recompile vars as string
  var requestString = '';
  for (var i in shedman) {
	  if(i) requestString += i + '=' + URLEncode(shedman[i]) + '&';
  }
  //document.write(requestString);

  //requestString = mid(requestString, 1, len(requestString)-1);
  
  var xmlhttp=false; //Clear our fetching variable
  try {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object�
       } 
  catch (e) 
    {
      try 
	    {
          xmlhttp = new
          ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
        } 
		catch (E) 
		  {
            xmlhttp = false;
          }
     }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
    {
      xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
    }
  var file = 'includes/get_price.inc.php'; //This is the path to the file that retrieves the price from the server *
  //xmlhttp.TimeOut = 35000;
  xmlhttp.open('POST', file, true); //Open the file through POST, and add the page we want to retrieve as a POST variable **
  //Send the proper header information along with the request
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
  xmlhttp.setRequestHeader("Content-length", requestString.length);
  xmlhttp.setRequestHeader("Connection", "close");

  xmlhttp.onreadystatechange=function() 
	 {
      if (xmlhttp.readyState==4)  //Check if it is ready to recieve data
        {      
	     var content = xmlhttp.responseText; //The content data which has been retrieved ***
         if(content.length > 1)  //Make sure there is something in the content variable
		   { 
			 document.getElementById('get_price').innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
		   }
         }		
	  else {
		document.getElementById('get_price').innerHTML = '<img src="../images/wait.gif" class="wait" /><p>Please wait while we process your enquiry...</p>';
	    }
	  }
  xmlhttp.send(requestString) //Nullify the XMLHttpRequest
  return;
}

function getPrice2(shedman)
{
  // Recompile vars as string
  var requestString = '';
  for (var i in shedman) {
	  if(i) requestString += i + '=' + URLEncode(shedman[i]) + '&';
  }
  //document.write(requestString);

  //requestString = mid(requestString, 1, len(requestString)-1);
  
  var xmlhttp=false; //Clear our fetching variable
  try {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object�
       } 
  catch (e) 
    {
      try 
	    {
          xmlhttp = new
          ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
        } 
		catch (E) 
		  {
            xmlhttp = false;
          }
     }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
    {
      xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
    }
  var file = 'includes/get_price_2.inc.php'; //This is the path to the file that retrieves the distributors for this postcode *
  //xmlhttp.TimeOut = 60000;
  xmlhttp.open('POST', file, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
  //Send the proper header information along with the request
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
  xmlhttp.setRequestHeader("Content-length", requestString.length);
  xmlhttp.setRequestHeader("Connection", "close");

  xmlhttp.onreadystatechange=function() 
	 {
      if (xmlhttp.readyState==4)  //Check if it is ready to recieve data
        {      
	     var content = xmlhttp.responseText; //The content data which has been retrieved ***
         if(content.length > 1)  //Make sure there is something in the content variable
		   { 
             document.getElementById('get_price').innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
		   }
         }		
	  else {
		document.getElementById('get_price').innerHTML = '<img src="../images/wait.gif" class="wait" /><p>Please wait while we calculate and send your estimate...</p>';
	    }
	  }
  xmlhttp.send(requestString) //Nullify the XMLHttpRequest
  return;
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}
