var ActionCommons = {};

ActionCommons.getDojoDiv = function(name)
{
	var div = dojo.widget.byId(name);
	if(!div)
	{
		if(name == 'floating_form' || name == 'pagedproductactivities_product'){
			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,searchtype)
{
    var searchfrm = document[frm];
    var searchtxt = searchfrm[txt];
    var searchfor=$('searchFor').value;
    if(searchfor != null && searchfor.length == 0 )
    {
       searchfor= searchtype;
    }
    if(searchtxt.value.length == 0 || searchtxt.value == stat)
	{
		throw "Enter Search String";
		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]);
		}
	}else {
		var catId = document.getElementById('catId');
		if(catId) {
			catId.value = "";
		}
	}
	
	var loc = window.location;
	var tempSearchName;
	tempSearchName=dojo.string.trim(searchtxt.value);
	var intIndexOfMatch = tempSearchName.indexOf("/");
	while (intIndexOfMatch != -1){
	  tempSearchName = tempSearchName.replace( "/", " ");
	  intIndexOfMatch = tempSearchName.indexOf("/");
	}
	if( searchfor!=null && dojo.string.trim(searchfor)=='products')
	{
		loc.href = loc.protocol + "//" + loc.host + "/" + 'search/' + encodeURIComponent(tempSearchName) + prettyUrlExtension;
	}else if(searchfor!=null && dojo.string.trim(searchfor)=='friends')
	{
	   loc.href = loc.protocol + "//" + loc.host + "/" + 'index.action?contentaction=searchpeopleresult&searchpeople_txtbx=' + dojo.string.trim(searchtxt.value);
	}	
	return false;
	
}

ActionCommons.validateAddProductForm = function(frm, txt, stat)
{
	var searchfrm = document[frm];
	var searchtxt = searchfrm[txt];
	var hdn = document.getElementById('hdnUserId');
	var tbx = searchfrm[txt].value;
	var index =tbx.indexOf('wanelo.com');
	if(index > -1)
	{
			alert("Please do not import the products from wanelo itself.");
			return false;
	}
	
	if(hdn.value.length <= 0) {
		executeAction('showLoginbarForm', 'userlogin.action');
		return false;
	}
	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;
		}
	}*/
	//var loc = window.location;
	//var searchString=dojo.string.trim(searchtxt.value);
	//var indexSearch =searchString.indexOf('?');
	//while (indexSearch != -1)
	//{
	//	searchString = searchString.replace( "?", "%3F");
	//	indexSearch = searchString.indexOf("?");
	//}
	//searchString = encodeURIComponent(searchString);
	//loc.href = loc.protocol + "//" + loc.host + "/" + 'addproduct/' + searchString + prettyUrlExtension;
	//return false;
	return true;
}
