
function style_on(the_cell, style)
{
   if (the_cell) {
     var index = the_cell.className.indexOf(style);
     if (index  == -1) {
   	the_cell.className = the_cell.className + ' ' + style;
     }
   }
}

function style_off(the_cell, style)
{
   if (the_cell) {
     var index = the_cell.className.indexOf(style);
     if (index != -1) {
	the_cell.className = the_cell.className.substr(0, index);
     }
  }
}

function hl1(e)
{
   var index = e.className.indexOf('selected');
   if (index == -1)
      style_on(e, 'hl');
}

function hl0(e)
{
   var index = e.className.indexOf('selected');
   if (index == -1)
      style_off(e, 'hl');
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y ) {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
    cookie_string += "; path=" + escape ( path );

  if ( domain )
    cookie_string += "; domain=" + escape ( domain );

  if ( secure )
    cookie_string += "; secure";

  document.cookie = cookie_string;
}

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString() + "; path=/; domain=appminer.com";
}

var cur_locale   = "en-us";

function init_locale(locale)
{
   cur_locale = locale;

   var store = document.getElementById("store");
   if (store != null) {
       var index;
       for (index = store.options.length; --index >= 0; ) {
	   if (store.options[index].value == cur_locale) {
	       store.selectedIndex = index;
	       break;
	   }
       }
   }
}

function set_locale(locale)
{
   cur_locale = locale;   
   set_cookie('lc', locale, '2011', '01', '01', '/', 'appminer.com');
   document.location = 'default.psp?lc=' + cur_locale;
}

var current_screenshot = 0;

function set_screenshot(appid, index, maxss)
{
    var i;

    if (index == -1) {
	if (current_screenshot == maxss-1)
	    current_screenshot = 0;
	else
	    current_screenshot++;

	index = current_screenshot;
    }
    
    document.getElementById("screenshot").src = "screenshot.psp?lc=" + cur_locale + "&id=" + appid + "&index=" + index;
    for (i = 0; i < maxss; i++) {
	var link   = document.getElementById("ssl"+i);
        var button = document.getElementById("ssb"+i); 
	if  (i == index) { 
	    style_on(link,   'aselected');
	    style_on(button, 'screenshot_button_selected');
	} else {
	    style_off(link, 'aselected');
	    style_off(button, 'screenshot_button_selected');
	}
    }
}

function set_search_type(search_type)
{
    var search_by_title   = document.getElementById("search_by_title");
    var search_by_keyword = document.getElementById("search_by_keyword");
    var search_by_company = document.getElementById("search_by_company");

    if (search_type == 0) {
	style_on (search_by_keyword, 'aselected');
        style_off(search_by_title,   'aselected');	
        style_off(search_by_company, 'aselected');	
    } else if (search_type == 1) {
        style_off(search_by_keyword, 'aselected');	
	style_on (search_by_title,   'aselected');
        style_off(search_by_company, 'aselected');
    } else if (search_type == 2 || search_type == 3) {
	style_off(search_by_title,   'aselected');
        style_off(search_by_keyword, 'aselected');
        style_on (search_by_company, 'aselected');
    }
    document.getElementById("stype").value = search_type;
}

function set_search_focus()
{
    var search_text = document.getElementById("search");
    search_text.focus();
}

// ========================================
// AJAX
// ========================================

function get_xml_http_request()
{
    if (window.XMLHttpRequest) {
	return new XMLHttpRequest(); //Not IE
    } else if(window.ActiveXObject) {
	return new ActiveXObject("Microsoft.XMLHTTP"); //IE
    } else {
	alert("Operation not supported. Please use Firefox, Safari or a later version of Internet Explorer.")
    }
}

var xml_http_request = get_xml_http_request();

function get_code_callback()
{
    if (xml_http_request.readyState == 4) {
	var div = document.getElementById('codes');
        div.style.visibility = "visible";
        div.innerHTML = xml_http_request.responseText;
	
	var x = div.getElementsByTagName("script");   
	for (var i=0; i < x.length; i++)
	    eval(x[i].text);
    }
}

function get_code(app_id)
{
    if (xml_http_request.readyState == 4 || xml_http_request.readyState == 0) {   
	xml_http_request.open("GET", "codes.psp?id=" + app_id, true);
	xml_http_request.onreadystatechange = get_code_callback;
	xml_http_request.send(null);
    }
}

function hide_codes()
{
    var div = document.getElementById('codes');
    hide_self(div);
}

function get_it(url)
{
    var popup_div = document.getElementById("itunes");
    popup_div.style.visibility = "visible";
    popup_div.innerHTML = "<iframe src=\"" + url + "\" width=\"100%\" height=\"100%\" frameborder=0 scrolling=no></iframe>"; 
}

function hide_self(e)
{
   e.style.visibility = "hidden";
}

function hide_itunes()
{
    var popup_div = document.getElementById("itunes");
    popup_div.style.visibility = "hidden";
}