//-----------------
//ONLOAD FUNCTIONS
//-----------------
window.onload = function()
{
	noimage();
	verification();
	postCommentFeedback();
};

$(window).load(function(){
  //Sets logo to a maximum size
  var logo = $("div.logo img");
  
  if(logo.height() > 96){
		logo.height(96);
	}
	if(logo.width() > 206){
		logo.width(260);
	}
	logo.css("visibility","visible");  
	$.each($.browser, function(i, val) {
  if(i=="mozilla" && $.browser.version.substr(0,3)=="1.9")
     $("#header h1").css("-moz-box-sizing","padding-box");
  });
});

function noimage()
{
	var elems = document.getElementsByTagName( "img" );
	for ( var i = 0; i < elems.length; i++ )
	{
		if (elems[i].src.indexOf('imgsc') != -1 && (!elems[i].complete || (typeof elems[i].naturalWidth != "undefined" && elems[i].naturalWidth < 80)))
		{
			elems[i].src = "http://www.auctiva.com/templates/thumbs/no_thumb.gif";
		}
	}
}
function verification(){
	//sets random number in verification image on blogComments page
	var verification = document.getElementById("verificationImage");
	if(verification)
	{
		verification.src = "verificationimage.aspx?rand=" + Math.floor(Math.random()*10001);
	}
}



function postCommentFeedback()
{
	var data_posted = document.getElementById("data_posted");
	var moderated = getQStringVar("posted");
	//If user tries to post comment
	if(data_posted)
	{
		if(data_posted.value != 1)
		{
			var comment = unescape(getQStringVar("comment"));
			if(comment != null)
			{
			  while(comment.indexOf("%20") != -1) 
			    comment = comment.replace("%20", " ");
			  document.getElementById("commentText").value = comment;
			}
			var user = getQStringVar("user");
			if(user != null)
			{
			  while(user.indexOf("%20") != -1)
			    user = user.replace("%20", " ");
			  document.getElementById("username").value = user;
			}
			alert('Comment failed to post. You may have entered the wrong verification code. Please try again.');
		}
	}
	else if(moderated)
	{
		if(moderated == '1')
		{
			alert('Your comment has been added pending approval');
		}
		else
		{
			alert('Comment posted successfully');
		}
	}
}

//-----------------
//HELPER FUNCTIONS
//-----------------
function getQStringVar(variable)
{	
	var qstring = location.search;
	var loc = qstring.indexOf(variable);
	//if variable found in qstring
	if(loc > 0)
		 loc = loc + variable.length + 1;
	else
		return null;
		
	var endLoc = qstring.indexOf("&", loc);
	//slice from end of variable to either next variable or end of the qstring
	if(endLoc > 0)
		return qstring.slice(loc, endLoc);
	else
		return qstring.slice(loc);
}

//----------------------
//EVENT DRIVEN FUNCTIONS
//----------------------

//SEARCHING

function search()
{
	var txt_search = document.getElementById("txt_search");
	if(txt_search.value==""){return false;}
	function searchStore(){
		window.location.href="?id="+userId+"&page=home&search=" + searchText;
	}
	function searchEBay(){
		var aid = "0";
		var pairs = location.search.substring(1).split(",");
		for (var i = 0; i < pairs.length; i++)
		{
			var key_val = pairs[i].split("=");
			if (key_val[0] == "id")
			{
			  aid = key_val[1];
			  break;
			}
		}
		window.location.href="http://rover.ebay.com/rover/1/711-1751-2978-71/1?AID=5463217&PID=1446521&SID=" + aid + "%4011%409%40" + rlid + "&mpre=" + escape("http://search.ebay.com/search/search.dll?from=R40&satitle=")+ escape(searchText);
	}
	if(txt_search)
	{
		var userId = document.getElementById("userId_txt").value;
		var rlid = document.getElementById("rlid_txt").value;
		var searchText = txt_search.value;
		var tselect = document.getElementById("dd_searchLoc");
		var trad = document.getElementById("rad_ebay");
		//using default mode
		if(tselect){
			// If search eBay is selected
			if(tselect.selectedIndex ==1){searchEBay();}
			else{searchStore();}
		}
		//using radio mode
		if(trad){
			// If search eBay is selected
			if(trad.checked){searchEBay();}
			else{searchStore();}
		}
		
	}
}
function checkEnter(e)
{
	if(window.event)//IE
	{
		if(e.keyCode == 13)
		{
			search();
		}
	}
	else if(e.which)
	{
		if(e.which == 13)
		{
			search();
		}
	}
}


//PER PAGE
function itemsPerPage()
{
	var tselect = document.getElementById("itemsPerPage");
	var userId = document.getElementById("userId_txt").value;
	var noItems = tselect.options[tselect.selectedIndex].value;
	thehref="?id="+userId+"&itemsPerPage=" + noItems;
	var searchString = getQStringVar("search");
	if(searchString)
	{
		thehref= thehref + "&search="+ searchString;
	}
	var filterString = getQStringVar("itemFilter");
	if(filterString)
	{
		thehref= thehref + "&itemFilter="+ filterString;
	}
	
	window.location.href = thehref;
}

function blogsPerPage()
{
	var tselect = document.getElementById("blogsPerPage");
	var userId = document.getElementById("userId_txt").value;
	var noItems = tselect.options[tselect.selectedIndex].value;
	window.location.href="?id="+userId+"&blogsPerPage=" + noItems;
}

//COMMENT POSTING
function postComment(blogId)
{
	var postText = document.getElementById("commentText").value;
	var userId = document.getElementById("userId_txt").value;
	var verification = document.getElementById("verification").value;
	var username = document.getElementById("username").value;
	window.location.href="?id="+userId+"&page=blogComments&comment=" + escape(postText)+"&user="+ escape(username) +"&verify="+ escape(verification)+"&blogId="+ blogId;
}

//BOOKMARK THIS PAGE
function addBookmark() 
{
	if( document.all ) 
	{
		window.external.AddFavorite( document.URL, document.title);
	} 
	//else
	else 
	{
		alert("Press CTRL-D (Netscape/Firefox) or CTRL-T (Opera) to bookmark");
	}
}


