var xml3;
var upp;
var pp;
var m;
var errors = new Array();
errors[-3] = 'Details are not available';
errors[-4] = 'Wrong PinPinŽ';
errors[-5] = 'PinPinŽ request sent';
errors[-6] = 'Invalid e-mail address';
var detailblocks = new Array();
detailblocks[0] = 'contactdetails';
detailblocks[1] = 'locationdetails';

function unlock_location() {
  pp = document.getElementById( 'pinpin_l' );
  ppv = pp.value;
  upp = ppv;
  rm = document.getElementById( 'remember_l' );
  rmv = rm.checked;
  em = document.getElementById( 'email_l' );
  emv = em.value;
  if( ppv == '' && emv == '' )
      return;
  if( !xml3 )
      if( navigator.appName == "Microsoft Internet Explorer" )
          xml3 = new ActiveXObject( "Microsoft.XMLHTTP" );
      else
          xml3 = new XMLHttpRequest;

  // Check if mobile page was requested ...
  qs = window.location.search.substring(1);
  qp = qs.split( '&' );
  for( i = 0; i < qp.length; i++ ) {
      qv = qp[i].split( '=' );
      if( qv[0] == 'mobile' ) {
          var mobile = qv[1];
      }
  }
  xml3.open( 'POST', 'beta.php' + ( mobile ? '?mobile='+mobile:'' ) );
  xml3.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
  xml3.onreadystatechange = handlePinPinLocation;

  cd = document.getElementById( 'locationdetails' );
  pp = cd.innerHTML;
  vv = document.getElementsByName( 'view' );
  for( i = 0; i < vv.length; i++ ) {
      v = vv[i];
      v.disabled = true;
  }
  if( emv != '' ) {
      if( m == 1 )
          cd.innerHTML = '<div align="center"><br><br><font class="boxStyle">Requesting ...</font><br><br><img src="images/ajax-loader_m.gif"></div>';
      else
          cd.innerHTML = '<div align="center"><br><br><font class="boxStyle">Requesting ...</font><br><br><img src="images/ajax-loader.gif"></div>';
      xml3.send( 'pinpin=1&email=' + emv + '&details=1' );
  }
  else {
      if( m == 1 )
        cd.innerHTML = '<div align="center"><br><br><font class="boxStyle">Verifying PinPin&reg;...</font><br><br><img src="images/ajax-loader_m.gif"></div>';
      else
        cd.innerHTML = '<div align="center"><br><br><font class="boxStyle">Verifying PinPin&reg;...</font><br><br><img src="images/ajax-loader.gif"></div>';
      xml3.send( 'pinpin=1&pp=' + ppv + '&rm=' + rmv + '&details=1' );
  }
}

function handlePinPinLocation() {
  if( xml3.readyState == 4 ) {
      var response = xml3.responseText;
      if( response.length > 0 ) {
          if( response.length < 3 ) {
            alert( errors[response] );
            if( response == -3 )
                window.location.reload();
            cd.innerHTML = pp;
          }
          else {
            // PinPinŽ was correct, check if cookie needs to be set
            if( rmv && ppv != '' ) {
                sp = /<script(.*?)<\/script>/g;
                m = response.match( sp );
                if( m ) {
                    for( i = 0; i < m.length; i++ ) {
                        sc1 = m[i].slice( 31, m[i].search( /<\/script>/ ) );
                        eval( sc1 );
                    }
                }
            }
            cd = document.getElementById( 'locationdetails' );
            cd.innerHTML = response;
            // Show other blocks (if any)
            if( typeof( unlock ) == 'function' ) {
                ppc = document.getElementById( 'pinpin' );
                if( ppc ) {
                    ppc.value = upp;
                    ppc = document.getElementById( 'email' );
                    ppc.value = '';
                    unlock();
                }
            }
            if( typeof( unlock_signature ) == 'function' ) {
                if( sig != 1 ) {
                    ppl = document.getElementById( 'pinpin_s' );
                    if( ppl ) {
                        ppl.value = upp;
                        //ppl = document.getElementById( 'email_s' );
                        //ppl.value = '';
                        unlock_signature();
                    }
                }
            }
            if( typeof( initGMaps ) == 'function' ) {
                initGMaps();
            }
          }
      }
      vv = document.getElementsByName( 'view' );
      for( i = 0; i < vv.length; i++ ) {
          v = vv[i];
          v.disabled = false;
      }
  }
}

