var id = null;
var featured_id = null;

/* ---------------------------- */
/* XMLHTTPRequest Enable 		*/
/* ---------------------------- */
function createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
	request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_type = new XMLHttpRequest();
	}
		return request_type;
}

var http = createObject();

/* -------------------------- */
/* SEARCH					 */
/* -------------------------- */
function showKeywords(website_id) {  
  if(this.id != null)
    document.getElementById('keywords_' + this.id).innerHTML = '';
  if(this.id == website_id) {
    document.getElementById('keywords_' + this.id).innerHTML = '';
    this.id = null;
    return;
  }
  
  this.id = website_id;  
  
  nocache = Math.random();
  f = http.open('get', '/keywords/ajax/' + website_id);
  http.onreadystatechange = showKeywordsHtml;
  http.send(null); 
}

function showKeywordsHtml() {
  if(http.readyState == 4){
  	var response = http.responseText;
    writeResponse(response);
  }
  else {
    showPreloader();
  }
}

function writeResponse(response) {
  document.getElementById('row_1_' + this.id).style.background = "#F3FFD0";
  document.getElementById('row_2_' + this.id).style.background = "#F3FFD0";
  document.getElementById('keywords_' + this.id).innerHTML = response;
}

function showPreloader() {
  document.getElementById('keywords_' + this.id).innerHTML = 'Loading...';
}

/*  Featured keywords   */
function setFeatured(keyword_id) {   
  document.getElementById('featured_message').style.display = 'none';
  document.getElementById('featured_not_message').style.display = 'none'; 
  this.featured_id = keyword_id;  
  
  nocache = Math.random();
  f = http.open('get', '/keywords/featured/' + keyword_id);
  http.onreadystatechange = showFeaturedHtml;
  http.send(null); 
}

function showFeaturedHtml() {
  if(http.readyState == 4){
  	var response = http.responseText;
    writeFeaturedResponse(response);
  }
  else {
    showFeaturedPreloader();
  }
}

function showFeaturedPreloader() {
  document.getElementById('featured_' + this.featured_id).src = '/img/ajax-loader.gif';
}

function writeFeaturedResponse(response) {
  if(response == 'yes') {
    document.getElementById('featured_' + this.featured_id).src = '/img/icons/star.png';
    document.getElementById('featured_message').style.display = 'block';
  }
  else {
    document.getElementById('featured_' + this.featured_id).src = '/img/icons/star_none.png';
    document.getElementById('featured_not_message').style.display = 'block';
  }
}