var ActionCommons = {};

ActionCommons.getDojoDiv = function(name)
{
	var div = dojo.widget.byId(name);
	if(!div)
	{
		if(name == 'floating_form'){
			throw "Please wait for the page to load completely and try again.";
		}else {
			throw "ActionCommons.getDojoDiv: div not found :" + name;
		}
	}
	return div;
}

ActionCommons.getElement = function(name)
{
	var elem = document.getElementById(name);
	if(!elem)
	{
		throw "ActionCommons.getElement: element not found :" + name;
	}
	return elem;
}

ActionCommons.showDiv = function(name)
{
	var div = ActionCommons.getElement(name);
	div.style.display = "block";	
}

ActionCommons.hideDiv = function(name, clear)
{
	var div = ActionCommons.getElement(name);
	div.style.display = "none";
	
	if(clear)
	{
		div.innerHTML = "";	
	}
}

ActionCommons.refreshDojoDiv = function(name, target, unescapeAMP, unescape)
{
	var div = ActionCommons.getDojoDiv(name);
	var divelem = ActionCommons.getElement(name);
	divelem.innerHTML = "<img src='resources/images/loading_ani.gif'/>";
	if(target)
	{
		if(unescapeAMP)
		{
			target = target.replace(/&amp;/g,'&');
		}

		if(unescape)
		{
			unescape(target);
		}
		
		div.setUrl(target);
	}
	else
	{
		div.refresh();	
	}	
}

ActionCommons.executeSearchfr = function(div, url)
{
		var minPrice = ActionCommons.getElement("minPrice_txt_fr");
		var maxPrice = ActionCommons.getElement("maxPrice_txt_fr");
		var sorting = ActionCommons.getElement("sorting_txt_fr");
		var view = ActionCommons.getElement("view_txt_fr");
		
		var pageNumber;
		if(view.value=="GRID")
		{
			pageNumber = ActionCommons.getElement("pageNumber_txt_fr_grid");
		}
		else
		{
			pageNumber = ActionCommons.getElement("pageNumber_txt_fr_list");
		}
		
		url += "&sorting=" + sorting.value+"&searchView=" + view.value+"&minPrice=" + minPrice.value+"&maxPrice=" + maxPrice.value+"&number=" + pageNumber.value;

		ActionCommons.refreshDojoDiv(div, url, true);	
}

ActionCommons.executeSearchall = function(div, url)
{
		var minPrice = ActionCommons.getElement("minPrice_txt_all");
		var maxPrice = ActionCommons.getElement("maxPrice_txt_all");
		var sorting = ActionCommons.getElement("sorting_txt_all");
		var view = ActionCommons.getElement("view_txt_all");
		var pageNumber;
		
		if(view.value == "GRID")
		{
			pageNumber = ActionCommons.getElement("pageNumber_txt_all_grid");
		}
		else
		{
			pageNumber = ActionCommons.getElement("pageNumber_txt_all_list");
		}
		
		var onlyReview = ActionCommons.getElement("onlyreview_txt_all");
		url += "&sorting=" + sorting.value+"&searchView=" + view.value+"&minPrice=" + minPrice.value+"&maxPrice=" + maxPrice.value+"&number=" + pageNumber.value+"&onlyReviewed="+onlyReview.value;
		
		//changed on Dec 7, 2009 to include supertagtypeid in the query string
		//if url already has superTagTypeIds (it will be, if coming from price slider or sorting drop down)
		// then no need to append this
		if(url.indexOf('superTagTypeIds=') < 0){
			var element  =  document.getElementById('superTagTypeIds_all');
			if(element){
				var superTagTypeIds = element.value;
				if(superTagTypeIds != ""){
					url += "&superTagTypeIds=" + superTagTypeIds;
				}
			}
		}
		
		
		ActionCommons.refreshDojoDiv(div, url, true);
}

ActionCommons.validateSearchForm = function(frm, txt, stat)
{
	var searchfrm = document[frm];
	var searchtxt = searchfrm[txt];
	if(searchtxt.value.length == 0 || searchtxt.value == stat)
	{
		return false;
	}
	var arTxt = searchtxt.value.split(":");
	if(arTxt.length > 1){
		searchtxt.value = arTxt[1];
		var element;
		element = document.getElementById("catName");
		if(element){
			element.value = (arTxt[0]);
		}
		element = document.getElementById("catNameTopBar");
		if(element){
			element.value = (arTxt[0]);
		}
	}
	return true;
}

ActionCommons.validateAddProductForm = function(frm, txt, stat)
{
	var searchfrm = document[frm];
	var searchtxt = searchfrm[txt];
	if(searchtxt.value.length == 0 || searchtxt.value == stat)
	{
		alert( "Please enter a product URL");
		return false;
	}
	/*var tbx = document.getElementById('addproduct_txtbx');
	if(tbx){
		var f = {scheme:'true'}
		if(!dojo.validate.isUrl(tbx.value, f)){
			alert( "Please enter a valid product URL");
			return false;
		}
	}*/
	return true;
}