var firstval = "";
var secondval = "";
var firstdone = false;
var seconddone = false;
var offset=3;

function popucalc(){

document.getElementById('calcresults').innerHTML = '<b>Calculating</b><br /><img src="/images/pleasewait.gif">';

var firstname = escape(document.popcalc.firstname.value);
var getURL = '/sources/popucalc.php?SP=' + firstname;
parseName(getURL,'first name');

var secondname = escape(document.popcalc.secondname.value);
getURL = '/sources/popucalc.php?SP=' + secondname;
parseName(getURL,'second name');

}

function parseName(getURL,nameID)
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }

//this is the part that processes the incoming data
    xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
        var myresults = xmlHttp.responseText; 
        parseResults(myresults,nameID);
      }
    }


//This is the part that sends it
  xmlHttp.open("get",getURL,true);
  xmlHttp.send(null);
  
  
  }
function parseResults(result,nameID){
  
    if(nameID == 'first name') {
       firstval = (isNaN(parseInt(result))) ? 0 : parseInt(result);
        firstdone = true;
       
          } else if (nameID == "second name"){
          secondval = (isNaN(parseInt(result))) ? 0 : parseInt(result);
          seconddone = true;
          }
 
     if((firstdone)&&(seconddone)) showresults(); 
 }
      
      
      
     
function showresults(){
     
     var topper;
     var bottomer;
     firstdone=false;
     seconddone=false;
     
     if(firstval > secondval){
          bottomer = firstval;
          topper = secondval;
          } else {
          topper = firstval;
          bottomer = secondval;
          }

     var popindex;
          
          if((topper == 0)||(bottomer==0)){
               popindex = 0;
               } else {
 
          var popindex = (topper/bottomer);
          popindex = Math.round(popindex*1000)/1000
               }
               
      var popcover = Math.round(200 - ((popindex * 190) + 10));
      var popamt = Math.round(popindex * 1000)/10;
      
     var resultspot = document.getElementById('calcresults');
     
     var resout = '<b>Matched: ' + popamt + '% </b><br>' + document.popcalc.firstname.value + ' and ' + document.popcalc.secondname.value + '<br>';
     
     resout += '<div style="width:210px;height:35px;padding:5px;border:1pt solid #222;font-size:12px;"><div style="width:100px;float:left;text-align:left">Cool</div><div style="width:100px;float:left;text-align:right">Scorching</div><br style="clear:all">';

     var quoter = "'/images/pcgradient.gif'";

     resout += '<div style="width:200px;height:20px;margin:0px;padding:0px; background:url('+quoter+');text-align:right;"><img src="/images/pcwhite.gif" height=20 width=' + popcover + ' style="margin:0px;padding:opx;border:none;" /></div></div><br /><br />';
     
     resultspot.innerHTML = resout;

   }
  