﻿
//string trim functions
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}


function switchThumb(on_or_off, content_id, type_id, img_path)
{
    var image_object = document.getElementById('thumbs_up_' + content_id + '_' + type_id);
    
    if (image_object)
    {
        if (on_or_off)
            image_object.style.backgroundImage = 'url(' + img_path + 'images/thumb_up_hover_lg.gif)';
        else if (image_object.style.backgroundImage != 'url(' + img_path + 'images/thumb_up_lg.gif)')
            image_object.style.backgroundImage = 'url(' + img_path + 'images/thumb_up_can_vote_lg.gif)';
    }
}

function doThumbsUp(content_id, type_id, post_url, img_path)
{
    var image_object = document.getElementById('thumbs_up_' + content_id + '_' + type_id);
    image_object.onmouseover = '';
    image_object.onmouseout = '';
    image_object.onclick = '';
                
    var http = null;

    if(http == null)
	    http = getXmlHttp();
	
    if(http == null || (http.readyState != 0 && http.readyState != 4))	// 0 uninit, 4 complete
	    return;
	
    var url = post_url + "do_thumbs_up.aspx";
    var param = "cid=" + content_id + "&tid=" + type_id;
    http.open("POST", url, true);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", param.length);
    http.setRequestHeader("Connection", "close");
	http.onreadystatechange =  function() 
	{
	    if(http.readyState != 4)
		    return;
		
		if (http.responseText == "1")
		{
	        var thumb_cnt = document.getElementById('thumb_cnt_' + content_id + '_' + type_id);
            var new_value = parseInt(thumb_cnt.innerHTML, 10) + 1;
            setThumb(content_id, type_id, "+1", img_path); 
            setTimeout("setThumb(" + content_id + ", " + type_id + ", " + new_value + ", '" + img_path + "')", 1000);	    
    	}
    }
    http.send(param); 
}

function setThumb(content_id, type_id, new_value, img_path)
{
    var thumb_cnt = document.getElementById('thumb_cnt_' + content_id + '_' + type_id);
    thumb_cnt.innerHTML = "";      
    thumb_cnt.innerHTML = new_value;      

    var image_object = document.getElementById('thumbs_up_' + content_id + '_' + type_id);
    image_object.style.backgroundImage = 'url(' + img_path + 'images/thumb_up_lg.gif)';
}

 function SubmitCommentForm(isLoggedin, url)
 {   
    var el = document.comment_form.comment_text;
    if (!isLoggedin)
    {
        alert('Please login (or register) and your comment will be posted!');   
        window.document.location = url; 
    }
    else if (el.value.trim() == "")
    {
        alert('Please enter your comment!');
        el.focus();
        return false;
    } 
    else
        return true;
 }
 
function setParentComment(new_id, returnURL)
{
    document.getElementById('parent_comment_id').value = new_id;
    document.getElementById('user_comment').focus();
    document.getElementById('redirect_url').value = returnURL;
}  

//for links on view page and click from home page
function AddCommentFocus(link)
{
    var el = document.getElementById("user_comment");
    
    if (link)
    {
        el.focus();
    }
    else
    {
        var url = String(window.location.search);
        if (url.indexOf("&add_comment") > -1)
        {               
            if (el)
            {       
                el.focus();
            }
        }
    }
}

function GetQueryVariable(variable) 
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = vars.length - 1;i >= 0; i--) 
    {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
          return pair[1];
        }
    }  
}

function CheckSubmitThumbsUp(isLoggedin, url)
{
    if (!isLoggedin)
    {
        alert('Please login (or register) to give this content the Thumbs Up!');
        window.document.location = url;
    }
}

function IE6PngFix(id, image)
{
    var isIE = document.all;
    var el = document.getElementById(id);   
    if (isIE && el)
    {   
        el.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + image + "', sizingMethod='scale');"
    
    }
    else
    {
        el.style.cssText += "background: url('" + image + "') no-repeat 0px 0px"; 
    }
}

function AddToBookmark(title, url)
{
    if (window.sidebar) 
    { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url,"");
    } 
    else if( window.external ) 
    { // IE Favorite
        window.external.AddFavorite( url, title); 
    }
    else if(window.opera && window.print) 
    { // Opera Hotlist
        var elem = document.createElement('a');
        elem.setAttribute('href',url);
        elem.setAttribute('title',title);
        elem.setAttribute('rel','sidebar');
        elem.click();
    }   
} 

function highlite(_txtInput) 
{
    var iStart = 0; // highlight start character
	 
    if (_txtInput.createTextRange){ //IE
     var r = _txtInput.createTextRange(); 
     r.moveStart("character", iStart); 
     r.select();
    } 
    else if (_txtInput.setSelectionRange){ //Firefox, Safari
     _txtInput.setSelectionRange(iStart,_txtInput.value.length);
    }
    _txtInput.focus();
}

