/**
* 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 = "";
        if(Get_Cookie('__AID'))
        {
            loadAddUrl(url_added);
        }
        else
        {
            loadLogin();
            AFTER_LOGIN = "addIt";
        }
}

 
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 addComment(url_encode)
{
    if(Get_Cookie('__AID'))
    {
        loadAddComment(url_encode);
    }
    else
    {
        loadLogin();
        AFTER_LOGIN = "addComment";
    }
}
function loadAddComment(url_encode)
{
    document.getElementById('box-title').innerHTML       = "Easy way to add a comment";
    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+"url/browserUrlComments/"+url_encode,"url_encode="+url_encode,
        function(){
            if(xmlh.readyState == 4)
            {
                document.getElementById('box-content').innerHTML = xmlh.responseText;
                document.getElementById('send-ta').focus();
            }
        });
    
}

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();
            }
        });
}
function setBrowserNextUrl()
{
    if(document.getElementById("browser-previous-icon"))
        var previousDivId=document.getElementById("browser-previous-icon");
    if(document.getElementById("browser-history-icon"))
        var previousDivId=document.getElementById("browser-history-icon");

    var nextDivId=document.getElementById("browser-next-icon");
    if(document.getElementById("browser-previous-icon"))
    previousDivId.className="browser-previous-icon";
    var category_select_box_val=document.getElementById("category-select-box").value;
    //code to set previous history count as hidden variable
    var browser_previous_history_count_element=document.getElementById('browser-previous-history-count');
    var browser_previous_history_count=browser_previous_history_count_element.value;
    browser_previous_history_count=parseInt(browser_previous_history_count);
    var num_one=parseInt("1");
    var browser_previous_history_count_plus;
    browser_previous_history_count_plus=browser_previous_history_count+num_one;
    browser_previous_history_count_element.value=browser_previous_history_count_plus;

    // Ajax code to load next link
    if(category_select_box_val!='select_category')
    {
        xmlh=GetXmlHttpObject();
        var response=SendPostRequest(xmlh,siteURL+"url/setNextUrlToVisit","cat_sel_box_val="+category_select_box_val+"&prev_history_count="+browser_previous_history_count_plus,function()


        {
                if(xmlh.readyState<4)
                {
                    //nextDivId.className='';
                    nextDivId.style.visibility="hidden";
                }
                if(xmlh.readyState==4)

                {
                    nextDivId.style.visibility="visible";
                    nextDivId.className='browser-next-icon';
                    var response_text=xmlh.responseText;
                    var response_text_split_array=response_text.split("$%*&");
                    // cHref(response_text_split_array[3],'home/browser/'+response_text_split_array[0]+'/'+response_text_split_array[1]+'/'+response_text_split_array[2]);
                    // var tttest=GLOBAL_SITE_URL+'home/browser/'+response_text_split_array[0]+'/'+response_text_split_array[1]+'/'+response_text_split_array[2];
                    // alert(tttest);
                    location.href=GLOBAL_SITE_URL+'home/browser/'+response_text_split_array[0]+'/'+response_text_split_array[1]+'/'+response_text_split_array[2]+'?historyCount='+response_text_split_array[4];
                    return true;
                }
            });

        browser_previous_history_count_element.value=browser_previous_history_count_plus;
    }
    else
    {
        alert("Please select category");
    }

        
}
function setBrowserPreviousUrl()
{
    var previousDivId=document.getElementById("browser-previous-icon");
    var previousDivIdClassName=previousDivId.className;
    // if(previousDivIdClassName=='browser-previous-icon')
    // {
    var browser_previous_history_count_element=document.getElementById('browser-previous-history-count');
    var browser_previous_history_count=document.getElementById('browser-previous-history-count').value;
    browser_previous_history_count=parseInt(browser_previous_history_count);
    if(browser_previous_history_count!='0')
    {
        var browser_previous_history_count_with_symbol='-'+browser_previous_history_count;
        browser_previous_history_count_with_symbol=parseInt(browser_previous_history_count_with_symbol);
        browser_previous_history_count_element.value=browser_previous_history_count-1;
        if(browser_previous_history_count_element.value=='0')
            previousDivId.className="browser-previous-icon-disabled";
        history.go(-1);
            
    }
//}
}
function showVisitedPreviousUrlByUser(start)
{
    var category_select_box_val=document.getElementById("category-select-box").value;
    
    var load_url_box_class = document.getElementById('browser-load-url-box').className;
    document.getElementById('box-load-url-content').style.height = "125 px";
    if(load_url_box_class != "browserloadurlbox" && start == 0)
        document.getElementById('browser-load-url-box').className    = "browserloadurlbox";
    else
    {
        document.getElementById('browser-load-url-box').className    += " block";
        document.getElementById('box-load-url-content').innerHTML     = "<img src='"+GLOBAL_TEMPLATE_URL+"images/loader.gif' /> loading...";
        xmlh		= GetXmlHttpObject();
        var response	= SendPostRequest(xmlh, GLOBAL_SITE_URL+"url/loadVisitedPreviousURLByUser",
            'cat_sel_box_val='+category_select_box_val,
            function(){
                if(xmlh.readyState == 4)
                    document.getElementById('box-load-url-content').innerHTML = xmlh.responseText;
            });
    }

}
