/**
* on load of document make url as visted one
*/
function setUrlAsVisited()
{  
  var url       = document.getElementById('browser-iframe').src;
  if(url)
  {
    var encoded_url = encodeURIComponent(url);
    xmlh            = GetXmlHttpObject();
    var response    = SendPostRequest(xmlh, GLOBAL_SITE_URL+"home/setUrlAsVisited",
            'url='+encoded_url, function(){
                    if(xmlh.readyState == 4)
                    {
                    }});
  }
}

/**
 * Set the current browsing url as favorite or unfavorite and change the images of them 
 */
function setIsFavorite(like_or_kill)
{
  var is_favorite = 2;
  var like_class_new  = 'browserlike';
  var kill_class_new  = 'browserkill';
  var url             = document.getElementById('browser-iframe').src;
  if(Get_Cookie('__AID'))
  {
    var like_class	= document.getElementById('browser-like').className;
    var kill_class	= document.getElementById('browser-kill').className;
    //alert(like_or_kill+"--"+like_class+"--"+kill_class)
    if((like_class == 'browserselflike' && like_or_kill == 'kill') || (kill_class == 'browserselfkill' && like_or_kill == 'like'))
    {
      is_favorite = 2;
      like_class_new  = 'browserlike';
      kill_class_new  = 'browserkill';
    }

    if(like_class == 'browserlike' && like_or_kill == 'kill')
    { 
      is_favorite = 0;
      like_class_new  = 'browserlike';
      kill_class_new  = 'browserselfkill';
    }      

    if(like_class == 'browserlike' && like_or_kill == 'like' && kill_class == 'browserkill')
    {
      is_favorite = 1;
      like_class_new = 'browserselflike';
      kill_class_new = 'browserkill';
    }     

    xmlh		= GetXmlHttpObject();
    var response	= SendPostRequest(xmlh, GLOBAL_SITE_URL+"home/setIsFavorite", 
                                            'url='+url+'&is_favorite='+is_favorite,
                                            function(){
                                                document.getElementById('browser-like').className = like_class_new;
                                                document.getElementById('browser-kill').className = kill_class_new;
                                            });
  }
  else
  {
    loadLogin();
    document.getElementById('browser-box').style.right    = "auto";
    AFTER_LOGIN = "setIsFavorite,"+like_or_kill;
    //setIsFavorite(like_or_kill);
  }
}

/**
* To share link
*/
function shareIt()
{
  // Load this url in share window
  var load_url = GLOBAL_SITE_URL+'share/share/?url=' + encodeURIComponent(document.getElementById('browser-iframe').src) + '&title=' + CURRENT_TITLE;
  showHide('popup', 630, load_url);
}

/**
 * Open tags panel
 */
function loadTags(start)
{  
   var box_class = document.getElementById('browser-box').className;
   document.getElementById('box-content').style.height = "125 px";
   if(box_class != "browserbox" && start == 0)
       document.getElementById('browser-box').className    = "browserbox";
   else
   {
       document.getElementById('browser-box').className    += " block";
       document.getElementById('box-content').innerHTML     = "<img src='"+GLOBAL_TEMPLATE_URL+"images/loader.gif' /> loading...";
       xmlh		= GetXmlHttpObject();
       var response	= SendPostRequest(xmlh, GLOBAL_SITE_URL+"home/browserBox", 
                                                'tag='+CURRENT_TAG+'&start='+start,
                                                function(){
                                                    if(xmlh.readyState == 4)
                                                      document.getElementById('box-content').innerHTML = xmlh.responseText;
                                                });
   }
}

/**
 * Load login module into modal box
 */
function loadLogin()
{
    var loginOrSignout = document.getElementById('login-text').innerHTML
    if(loginOrSignout == 'Sign-out')
    {
      signOut();
    }
    else
    {
        var box_class = document.getElementById('browser-box').className;
        
       if(box_class == "browserbox")
       {
           document.getElementById('browser-box').className    += " boxlogin";
           document.getElementById('box-title').innerHTML       = "Login/Signup";
           document.getElementById('box-content').innerHTML     = "<img src='"+GLOBAL_TEMPLATE_URL+"images/loader.gif' /> loading...";
           xmlh		= GetXmlHttpObject();
           var response	= SendPostRequest(xmlh, GLOBAL_SITE_URL+"home/browserBox", "login=true",
                                                function(){
                                                    if(xmlh.readyState == 4)
                                                      document.getElementById('box-content').innerHTML = xmlh.responseText;
                                                });
       }
       else
        document.getElementById('browser-box').className    = "browserbox";
    }
  
}

function instantLogin(loginOrSignup)
{     
      var email_element     = document.getElementById('email-ilogin');
      var password_element  = document.getElementById('password-ilogin');
      var login_err         = document.getElementById('err-ilogin');
      if(email_element.value == "")
      {
              login_err.className = "red";
              login_err.innerHTML = "Email field cannot be left blank";
              email_element.focus();
              return false;
      }
      else if(verifyEmail(email_element.value) == false)
      {
              login_err.className = "red";
              login_err.innerHTML = "Invalid email address";
              email_element.focus();
              return false;
      }
      else if(password_element.value == "")
      {
              login_err.className = "red";
              login_err.innerHTML = "Password cannot be left blank";
              password_element.focus();
              return false;
      }
      else
      {
        xmlh          = GetXmlHttpObject();
        var response  = SendPostRequest(xmlh,
                                siteURL+'user/ajaxLogin', 'email_login=' + email_element.value + '&password_login=' + password_element.value,
                                function()
                                {
                                        if(xmlh.readyState < 4)
                                          login_err.innerHTML     = "<img src='"+GLOBAL_TEMPLATE_URL+"images/loader.gif' /> logging-in...";

                                        if(xmlh.readyState == 4)
                                        {
                                                if(xmlh.responseText != 1)
                                                {
                                                    login_err.className = "red";
                                                    login_err.innerHTML = "Invalid Email or Password";
                                                }
                                                else if(Get_Cookie('rurl'))
                                                {
                                                    var rurl = Get_Cookie('rurl');
                                                    Set_Cookie( 'rurl', '', '', '/', '', '' );
                                                    window.location = rurl;
                                                }
                                                else
                                                {
                                                    hideBrowserBox();
                                                    setLikeKill();                                                    
                                                }
                                        }
                                });
      }
    
    return true;
}

function instantSignup()
{
	var name_element     = document.getElementById('name-isignup');
	var email_element     = document.getElementById('email-isignup');
    var password_element  = document.getElementById('password-isignup');
    var signup_err         = document.getElementById('err-isignup');
    if(name_element.value == "")
    {
    	signup_err.className = "red";
    	signup_err.innerHTML = "User name field cannot be left blank";
        name_element.focus();
        return false;
    }
    else if(email_element.value == "")
    {
    	signup_err.className = "red";
        signup_err.innerHTML = "Email field cannot be left blank";
        email_element.focus();
        return false;
    }
    else if(verifyEmail(email_element.value) == false)
    {
        signup_err.className = "red";
        signup_err.innerHTML = "Invalid email address";
        email_element.focus();
        return false;
    }
    else if(password_element.value == "")
    {
        signup_err.className = "red";
        signup_err.innerHTML = "Password cannot be left blank";
        password_element.focus();
        return false;
    }
    else
    {
      xmlh          = GetXmlHttpObject();
      var response  = SendPostRequest(xmlh,
                              siteURL+'user/instantSignup', 'name=' + name_element.value + '&email=' + email_element.value + '&password=' + password_element.value,
                              function()
                              {
                                      if(xmlh.readyState < 4)
                                        signup_err.innerHTML     = "<img src='"+GLOBAL_TEMPLATE_URL+"images/loader.gif' /> Signing up...";

                                      if(xmlh.readyState == 4)
                                      {
                                              if(xmlh.responseText != 1)
                                              {
                                                  signup_err.className = "red";
                                                  signup_err.innerHTML = "Invalid Email or Password";
                                              }
                                              else if(Get_Cookie('rurl'))
                                              {
                                                  var rurl = Get_Cookie('rurl');
                                                  Set_Cookie( 'rurl', '', '', '/', '', '' );
                                                  window.location = rurl;
                                              }
                                              else
                                              {
                                                  hideBrowserBox();
                                                  setLikeKill();                                                    
                                              }
                                      }
                              });
    }
  
  return true;
}

function hideBrowserBox()
{    
    document.getElementById('login-text').innerHTML     = "Sign-out";
    document.getElementById('browser-box').className    = "browserbox";
}



/**
 * Signout action
 */
function signOut()
{
    xmlh          = GetXmlHttpObject();
    var response  = SendPostRequest(xmlh,
                            siteURL+'user/ajaxSignout','',
                            function()
                            {
                                if(xmlh.readyState == 4)
                                {                                  
                                  document.getElementById('login-text').innerHTML = "Log-in";
                                }
                            });
}

/**
 * Add a link from browser
 */
function addIt()
{
    var url_added = document.getElementById("add-link").value;
    //var add_error = document.getElementById("add-err");
    if(verifyUrl(url_added))
    {
      if(Get_Cookie('__AID'))
      {
          loadAddUrl(url_added);
      }
      else
      {
        loadLogin();
        AFTER_LOGIN = "addIt";
      }
    }
    else
        alert("Invalid url");
}


var AFTER_LOGIN = '';
function afterLogin()
{
    var fun_params = AFTER_LOGIN.split(',');
    switch(fun_params[0])
    {
       case "addIt": addIt();
            break;
       case "setIsFavorite": setIsFavorite(fun_params[1]);
            break;
       default:
            break;
    }
}

function loadAddUrl(url_added)
{
    document.getElementById('box-title').innerHTML       = "Easy way to add a link";
    document.getElementById('browser-box').className    += " boxadd";
    document.getElementById('box-content').style.height = "";
    document.getElementById('box-content').innerHTML    = "<img src='"+GLOBAL_TEMPLATE_URL+"images/loader.gif' /> loading...";
     xmlh		= GetXmlHttpObject();
     var response	= SendPostRequest(xmlh, GLOBAL_SITE_URL+"home/browserBox", "show=AddLink&url="+encodeURIComponent(url_added),
                                          function(){
                                             if(xmlh.readyState == 4)
                                                document.getElementById('box-content').innerHTML = xmlh.responseText;
                                          });
}




function setLikeKill()
{
    //Fill the div with relevant info
    xmlh		= GetXmlHttpObject();
    var response	= SendPostRequest(xmlh,
                                            siteURL + "home/browserValues",
                                            "url_encode="+CURRENT_URL_ENCODE,
                                            function()
                                            {
                                              if(xmlh.readyState == 4)
                                              {
                                                var self_like = xmlh.responseText;
                                                // determine the class of likes and dislikes
                                                document.getElementById('browser-like').className = 'browserlike';
                                                document.getElementById('browser-kill').className = 'browserkill';
                                                
                                                if(self_like == 0)                                                        
                                                    document.getElementById('browser-kill').className = 'browserselfkill';
                                                else if(self_like == 1)
                                                    document.getElementById('browser-like').className = 'browserselflike';
                                                
                                                afterLogin();
                                              }
                                            });
}
