jQuery(document).ready(function(){
	if (document.getElementById("divMainSystem"))
	{
        document.getElementById("divMainSystem").style.width = document.body.scrollWidth + 'px';
        document.getElementById("divMainSystem").style.height = document.body.scrollHeight + 'px';
	}
    
    jQuery('#txtKeyword').toggleVal();
    jQuery('#txtAdvancedKeyword').toggleVal();
    jQuery('#txtContentProvider').toggleVal();        
    
    /*if(typeof sIFR == "function"){  	    	    	    	    	
    	
    	sIFR.replaceElement(named({
    		sSelector:".rotator_box h1",
    			sFlashSrc: rootUrl + "flash/dearJoe_5_CASUAL_h1.swf", 
    			sColor:"#990000",
    			sWmode:"transparent"
    	}));

    	sIFR.replaceElement(named({
    		sSelector:".side_a h1", 
    			sFlashSrc: rootUrl + "flash/dearJoe_5_CASUAL_h1.swf", 
    			sColor:"#990000",
    			sWmode:"transparent"
    	}));   	    	
    	
    	sIFR.replaceElement(named({
    		sSelector:".side_b h1", 
    			sFlashSrc: rootUrl + "flash/dearJoe_5_CASUAL_h1.swf", 
    			sColor:"#990000",
    			sWmode:"transparent"
    	}));
    	
    	sIFR.replaceElement(named({
    		sSelector:"body .h1 h1", 
    			sFlashSrc: rootUrl + "flash/dearJoe_5_CASUAL_h1.swf", 
    			sColor:"#990000",
    			sWmode:"transparent"
    	}));		
		
		sIFR.replaceElement(named({
    		sSelector:"body h5.name_profile", 
    			sFlashSrc: rootUrl + "flash/dearJoe_5_CASUAL.swf", 
    			sColor:"#a0a0a0",
    			sWmode:"transparent"
    	}));
    };*/
});

var felem;
var reload = false;

function changeFavorite(elem, userId, articleId)
{
   felem = '#' + elem;
   var url = document.location.href;
   url = registerUrl + 'return/' + url.replace(rootUrl,'').replace(/\//g, '_')+'_save_'+articleId;
   
   if (!userId)
   {
	   //systemConfirmRegister('Please create an account in order to add this to your bookmarks. Registration is free:', 
	   systemConfirmLogin('You have to be logged in to do that.',
			   function(){reload = true;jQuery.post(favoriteUrl, {articleId: articleId}, checkChangeFavorite, 'json');});
   }
   else
   {
	   jQuery.post(favoriteUrl, {articleId: articleId}, checkChangeFavorite, 'json');
   };
};

function checkChangeFavorite(data)
{
	systemAlert(data.Response.FavoriteError.Message,
	function()
	{
		if (reload) document.location.reload();
	});
	if (parseInt(data.Response.FavoriteError.Code) == 0)
	{				
		if (jQuery(felem).attr('title') == 'Add to Bookmarks')
		{							
			jQuery(felem).attr('title', 'Remove from Bookmarks');
			jQuery(felem).attr('innerHTML','Remove from Bookmarks');
		}
		else
		{						
			jQuery(felem).attr('title', 'Add to Bookmarks');
			jQuery(felem).attr('innerHTML','Add to Bookmarks');
		};
	};
};

function showAdvancedSearch()
{
	document.getElementById('advancedSearch').style.left = HTMLElement.findPosX(document.getElementById('lnkAdvancedSearch')) + 'px';
	document.getElementById('advancedSearch').style.top  = HTMLElement.findPosY(document.getElementById('lnkAdvancedSearch')) - 30 + 'px';
	jQuery('#advSearch').val(1);
	jQuery('#advancedSearch').show();
};

function closeAdvancedSearch()
{
	jQuery('#advSearch').val(0);
	jQuery('#advancedSearch').hide();
};

function checkFormSearch()
{
	if (parseInt(jQuery('#advSearch').val()))
	{
	   return checkAdvancedSearch('txtAdvancedKeyword');	
	}
	else
	{
		return checkSearch('txt');
	};
};

function checkSearch(idKeyword, keywordPrefilled)
{
   var keywordPrefilled = keywordPrefilled ? keywordPrefilled : 'Keyword';
   var search = document.getElementById(idKeyword).value;

   search = search.replace(/^\s*$/, '');
   if (!search.length || search == keywordPrefilled)
   {
        systemAlert('Please type your search term!');
        return false;
   };
   return true;
};

function checkAdvancedSearch()
{
   var keywordPrefilled = 'Keyword';
   var contentProviderPrefilled = 'Content Provider';
   var idKeyword = 'txtAdvancedKeyword';
   var idContentProvider = 'txtContentProvider';
   var search;
   
   search = document.getElementById(idKeyword).value;
   search = search.replace(/^\s*$/, '');
   search2 = document.getElementById(idContentProvider).value;
   search2 = search2.replace(/^\s*$/, '');
   if ((!search.length || search == keywordPrefilled) &&
       (!search2.length || search2 == contentProviderPrefilled)
       && !parseInt(jQuery('#ddlSection').val()))
   {
        systemAlert('Please type your search term!');
        return false;
   };
   return true;
};

function limitChars(textid, limit, infodiv)
{
    var text = jQuery('#'+textid).val(); 
    var textlength = text.length;
    if(textlength > limit)
    {
        jQuery('#' + infodiv).html('You cannot write more then '+limit+' characters!');
        jQuery('#'+textid).val(text.substr(0,limit));
        return false;
    }
    else
    {
        jQuery('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
        return true;
    };
};
