function UsingAppleWebKit() {
  // String found if this is a AppleWebKit based product
	var kitName = "applewebkit/";
	var tempStr = navigator.userAgent.toLowerCase();
	var pos = tempStr.indexOf(kitName);
	var isAppleWebkit = (pos != -1);
		
	if (isAppleWebkit) {
		return true;
	} else {
		return false;
	}
}

function createRequestObject(){
  var o = false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
      try {
        o = new ActiveXObject("Msxml2.XMLHTTP");
      }   catch (e) {
      try {
        o = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        o = false;
      }
    }
  @end @*/
  /*if (!o && typeof XMLHttpRequest!='undefined' && !UsingAppleWebKit()) {*/
  if (!o && typeof XMLHttpRequest!='undefined') {  
    o = new XMLHttpRequest();
  }
  
  return o;
} 

function getStateInfo(){
	country = document.entryform.country.options[document.entryform.country.selectedIndex].value;

  xmlhttp.open('get', 'templates/ajax_states.php?country=' + country);
  xmlhttp.onreadystatechange = handleStateInfo; 
  xmlhttp.send('');
}

function getStateInfoDB(){
	country = document.entryform.country.options[document.entryform.country.selectedIndex].value;
  xmlhttp.open('get', 'templates/ajax_states_db.php?country=' + country);
	xmlhttp.onreadystatechange = handleStateInfo; 
	xmlhttp.send('');
}

function handleStateInfo(){
	if(xmlhttp.readyState == 4){ //Finished loading the response
		if (xmlhttp.status == 200) {
		  var response = xmlhttp.responseText;
		  document.getElementById('state_select').innerHTML = response;
    } else {
      /* Error or no data */
      document.getElementById('state_select').innerHTML = '';
    }
	}
}

function ReviewVote(rid, vote){
  xmlhttp.open('get', 'templates/ajax_revvote.php?rid=' + rid + '&vote=' + vote);
  xmlhttp.onreadystatechange = function(){ handleReviewVote(rid) };
  xmlhttp.send('');
}

function handleReviewVote(rid){
	if(xmlhttp.readyState == 4){ //Finished loading the response
		if (xmlhttp.status == 200) {
			var response = xmlhttp.responseText;
		  document.getElementById(rid).innerHTML = response;
    } else {
      /* Error or no data */
      document.getElementById(rid).innerHTML = '';
    }
	}
}

function lookupZip(){
  if(document.getElementById('city_zip').value.length < 4)
  {
	  alert("Please enter at least 4 characters of the city name.");
	  document.getElementById('city_zip').focus();
	  return false;
	}
  if(document.getElementById('state_zip').value == 0)
  {
	  alert("Please choose a state");
	  document.getElementById('state_zip').focus();
	  return false;
	}
	
	var state = document.getElementById('state_zip').value;
	var city = encodeURI(document.getElementById('city_zip').value);
	
  xmlhttp.open('get', 'templates/ajax_ziplookup.php?state=' + state + '&city=' + city);
  xmlhttp.onreadystatechange = function(){ handleZipLookup() };
  xmlhttp.send('');
}

function handleZipLookup(){
	if(xmlhttp.readyState == 4){ //Finished loading the response
		if (xmlhttp.status == 200) {
			var response = xmlhttp.responseText;
		  document.getElementById('zip_result').value = response;
		  document.getElementById('zipcode').value = response;
    } else {
      /* Error or no data */
      document.getElementById('zip_result').value = 'Error';
    }
	}
}

function ReviewSort(pid, uid, eid, sort, order, mode){
	document.getElementById(eid).innerHTML = '&nbsp;&nbsp;&nbsp;Loading... <img src="images/ajax_loading.gif" width="16" height="16" border="0">';
  xmlhttp.open('get', 'templates/ajax_revsort.php?pid=' + pid + '&uid=' + uid + '&sort=' + sort + '&order=' + order + '&mode=' + mode);
  xmlhttp.onreadystatechange = function(){ handleReviewSort(eid) };
  xmlhttp.send('');
}

function handleReviewSort(eid){
	if(xmlhttp.readyState == 4){ //Finished loading the response
		if (xmlhttp.status == 200) {
			var response = xmlhttp.responseText;
		  document.getElementById(eid).innerHTML = response;
    } else {
      /* Error or no data */
      document.getElementById(eid).innerHTML = 'Error!';
    }
	}
}

var xmlhttp = createRequestObject();
