var push=0;
function GetPostcode()
{
  postcode = document.getElementById('postcode').value;
  nummer = document.getElementById('hnummer').value;
 postcode=postcode.replace(/ *([a-z][a-z])$/i," $1").toUpperCase();
 document.getElementById('postcode').value=postcode;
  //if(( postcode.length == 7 ) && push == 0 && nummer.length > 0)
  if(( postcode.length == 7 ) && push == 0 )
  { 
    push = 1;
    //file='postcode.php';
    //file='webservices.php';
    file='pc/api.php';
    str='postcode='+postcode.replace(' ','') +'&hnummer='+nummer;
    xmlPostcode=GetXmlHttpObject();
    xmlPostcode.onreadystatechange = alertPostcode;
    xmlPostcode.open( "POST", file, true );
    xmlPostcode.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    xmlPostcode.setRequestHeader("Content-length", str.length);
    xmlPostcode.setRequestHeader("Connection", "close");
    xmlPostcode.send(str);


  } else {
	//ResetPostcode();
 }
}

function SetPostcode(response)
{
  velden=response.split(";");
  document.getElementById('straat').value = velden[0];
  document.getElementById('straat').style.backgroundImage = 'url(passed_icon.png)';
  document.getElementById('straat').disabled = true;
  document.getElementById('city').value = velden[1];
  document.getElementById('city').style.backgroundImage = 'url(passed_icon.png)';
  document.getElementById('hnummer').style.backgroundImage = 'url(passed_icon.png)';
  document.getElementById('postcode').style.backgroundImage = 'url(passed_icon.png)';
  document.getElementById('city').disabled = true;
  //document.getElementById('state').value = velden[2];
  //document.getElementById('land').value = 'Nederland';
  //document.getElementById('state').disabled = true;
  //document.getElementById('land').disabled = true;
  push = 0;
}

function ResetPostcode()
{
  //document.getElementById('straat').disabled = false;
  document.getElementById('straat').value = '';
  document.getElementById('city').value = '';
  //document.getElementById('city').disabled = false;
  document.getElementById('city').style.backgroundImage = 'url(neutral_icon.png)';
  document.getElementById('straat').style.backgroundImage = 'url(neutral_icon.png)';
  document.getElementById('hnummer').style.backgroundImage = 'url(failed_icon.png)';
  document.getElementById('postcode').style.backgroundImage = 'url(failed_icon.png)';
  push = 0;
}






function alertPostcode() {
if (xmlPostcode.readyState == 4) {
         if (xmlPostcode.status == 200) {
		ResetPostcode( );
         if ( xmlPostcode.responseText  == 'invalid' ) {
         } else {
		SetPostcode( xmlPostcode.responseText);
         }
      }
}

}



