/**
 * AJAX code to fill search field with server items
 */

var wl_window;
var wl_window_initstr = '' +
"          <div style=\"position: absolute; right: 0; padding: 0 0 5px 5px;\">  <!-- closing button -->\n" +
"            <a href=\"javascript:;\" onclick=\"toggleGlobalOverlay(); document.getElementById('idea2wishlist').style.visibility = 'hidden'; return;\" title=\"Close\"><b>x</b></a>\n" + 
"          </div>";

var wl_req;
var wl_idea_id, wl_idea;
var wl_scripturl;
var wl_lng;
var wl_domain;

var noofresults = 0;
var current = -1;



/**
 * display layer with dynamic content
 */
function idea2Wishlist(lng, domain, idea_id, idea, user_id, scripturl)
{
  wl_init();

  wl_window.style.visibility = 'visible';

  wl_window.innerHTML = wl_window_initstr;

  if( !user_id )
  {
    if( lng == 'de' )
    {
      wl_window.innerHTML += '' +
      "Bitte beachten Sie: Zur Nutzung der Wunschlisten ist eine Anmeldung bei uns notwendig.<br />" + 
      "<b>Bitte <a href=\"" + scripturl + "/do=login\">loggen</a> Sie sich deshalb erst ein,<br />um diese Funktion nutzen zu k&ouml;nnen.</b>"; 
    }
    else if( lng == 'en' )
    {
      wl_window.innerHTML += '' +
      "Note: You need an account to create a wishlist.<br />" + 
      "<b>Please <a href=\"" + scripturl + "/do=login\">login</a> first,<br />to be able to use this function.</b>"; 
    }
  }
  else
  {
    //wl_window.innerHTML += '' +
    //"to be continued ...";

    wl_idea_id   = idea_id;
    wl_idea      = idea;
    wl_scripturl = scripturl;
    wl_lng       = lng;
    wl_domain    = domain;

    // start AJAX wl_request
    //wl_req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');    // needed in case of POST (not GET) only
    wl_req.open('GET','http://' + domain + '/wishlists_peruser.php?user_id=' + user_id + '&lng=' + lng, true);
    wl_req.onreadystatechange = process_wlprefetch;   // call it AFTER open() to make it work in f**king IE
    wl_req.send(null);
  }
}


/**
 * handle response
 */
function process_wlprefetch()
{
  if( wl_req.readyState == 4 )   // XHR-state 'complete'
  {
    if( wl_req.status == 200 )   // want HTTP status code 'OK' only
    {
      var wl_lines_str = wl_req.responseText;
      var wl_lines_arr = wl_lines_str.split("\n");

      if( wl_lines_arr[0] == '0' )        // no wishlist existing yet
      {
        if( wl_lng == 'de' )
        {
          wl_window.innerHTML += "<b>Bitte legen Sie erst eine " + 
                                 "<a href=\"" + 
                                 wl_scripturl + '/do=wl/wishlist_item_nr_200=1' + 
                                 '/wishlist_item_idea_id_200=' + wl_idea_id + '/wishlist_item_200=' + wl_idea + "\">neue Wunschliste</a>" +
                                 " an!</b>";
        }
        else if( wl_lng == 'en' )
        {
          wl_window.innerHTML += "<b>Please create a " + 
                                 "<a href=\"" + 
                                 wl_scripturl + '/do=wl/wishlist_item_nr_200=1' + 
                                 '/wishlist_item_idea_id_200=' + wl_idea_id + '/wishlist_item_200=' + wl_idea + "\">new wishlist</a>" +
                                 " first!</b>";
        }
      }
      else if( wl_lines_arr[0] == '1' )    // one and only wishlist existing yet
      {
        if( wl_lines_arr[1] != "" )
        {          
          var wl_line_splitted = wl_lines_arr[1].split(";");

          assignIdea2wishlist(wl_line_splitted[0], wl_line_splitted[1]);
        }
      }
      else                                // several wishlists present
      {
        var wl_line_splitted;
            
        if( wl_lng == 'de' )
          wl_window.innerHTML += "<b>Bitte w&auml;hlen Sie eine Wunschliste aus</b>:";
        else if( wl_lng == 'en' )
          wl_window.innerHTML += "<b>Please choose one of your wishlists</b>:";
        wl_window.innerHTML += "<ul>";

        // adding server response to div
        for( i=1; i<wl_lines_arr.length; i++)
        {
          if( wl_lines_arr[i] != "" )
          {
            var wl_line_splitted = wl_lines_arr[i].split(";");

            // set links
            wl_window.innerHTML += "<li><a href=\"javascript: assignIdea2wishlist(" + wl_line_splitted[0] + ",'" + wl_line_splitted[1] + "');\">" + 
                                   wl_line_splitted[1] + "</a></li>";
          }
        }
        
        wl_window.innerHTML += "</ul>";
      }
    }
    else
      alert("There was a problem retrieving the XML data:\n" + wl_req.statusText);
  }
}


/**
 * really assign idea to wishlist
 */
function assignIdea2wishlist(wishlist_id, wishlist)
{
  wl_req.open('GET','http://' + wl_domain + '/wishlists_peruser.php?wishlist_id=' + wishlist_id + '&idea_id=' + wl_idea_id + '&wishlist_item=' + wl_idea, true);
  wl_req.onreadystatechange = null;
  wl_req.send(null);

  wl_window.innerHTML = wl_window_initstr;
  if( wl_lng == 'de' )
    wl_window.innerHTML += '<img src="img/chosen.gif" border="0" width="12" alt="chosen" />Die Geschenkidee wurde der<br />Wunschliste &quot;' + wishlist + '&quot; hinzugef&uuml;gt!<br /><br />&raquo;<b>Wunschliste</b> <a href="' + wl_scripturl + '/do=wl/id=' + wishlist_id + '"><b>ansehen</b></a> oder &raquo;<a href="javascript:;" onclick="toggleGlobalOverlay(); document.getElementById(\'idea2wishlist\').style.visibility = \'hidden\'; return;" title="Close"><b>weitershoppen</b></a>?<br />&nbsp;';
  else if( wl_lng == 'en' )
    wl_window.innerHTML += '<img src="img/chosen.gif" border="0" width="12" alt="chosen" />The gift idea was added to your wishlist &quot;' + wishlist + '&quot;!<br /><br />&raquo;<a href="' + wl_scripturl + '/do=wl/id=' + wishlist_id + '"><b>open wishlist</b></a> or &raquo;<a href="javascript:;" onclick="toggleGlobalOverlay(); document.getElementById(\'idea2wishlist\').style.visibility = \'hidden\'; return;" title="Close"><b>go on shopping</b></a>?<br />&nbsp;';
}


/**
 * init funcs
 */
function wl_init()
{
  if( typeof wl_window == 'undefined' )      
    wl_window = document.getElementById('idea2wishlist');
  if( typeof wl_req == 'undefined' )      
    wl_req = createXMLHttpRequest();
}

function createXMLHttpRequest()
{
  var my_wlreq = null;

  if( window.XMLHttpRequest )      // Mozilla, Safari, ...
    my_wlreq = new XMLHttpRequest();
  else if( window.ActiveXObject )  // IE
    my_wlreq = new ActiveXObject("Microsoft.XMLHTTP");

/*  var req = null;
  try 
  {
    req = new ActiveXObject("MSXML2.XMLHTTP");
  }
  catch( err_MSXML2 )
  {
    try
    {
      req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch( err_Microsoft )
    {
      if( typeof XMLHttpRequest != "undefined" ) 
        req = new XMLHttpRequest;
    }
  } */

  return my_wlreq;
}


