function pagination(args,page_num)
{
	//alert(args+page_num);
	var param=args.split("###");
	var action=param[0];
	if(action=="coupon_list")
	{
		if(param[2]=="coupon")
		{
			var div_name=document.getElementById('store_coupons');
		}
		else if(param[2]=="deals")
		{
			var div_name=document.getElementById('store_deals');
		}
		//document.getElementById('store_coupons').innerHTML="<center><img src='/admin/images/preloader.gif'></center>";
	}
	else if(action=="article_list")
	{
		var div_name=document.getElementById("store_articles");
	}
	else if(action=="comment_list")
	{
		var div_name=document.getElementById("store_comments");
	}
	div_name.innerHTML="<center><img src='/admin/images/preloader.gif'></center>";
	
	x_pagination(args,page_num,ReturnCode);
}

function ReturnCode(theCode)
{
	//alert(theCode);
	var tmp_arr=new Array();
	tmp_arr=theCode.split("~~~");
	var action=tmp_arr[0];
	var CodeToShow=tmp_arr[1];
	
	if(action=="article_list")
	var div_name=document.getElementById("store_articles");
	else
	{
		if(action=="add_new_comment")
		{
			ShowLayer('add_comment');
			
			var div_name=document.getElementById("store_comments");
		}
		else
		{
			
			if(action=="comment_list")
			{
				var div_name=document.getElementById("store_comments");
			}
			else
			{
				if(action=="coupon_list")
				{
					CodeToShow=tmp_arr[2];
					if(tmp_arr[1]=='coupon')
					{
						var div_name=document.getElementById("store_coupons");
					}
					else if(tmp_arr[1]=='deals')
					{
						var div_name=document.getElementById("store_deals");
					}
				}
				else
				{
					if(action=="category_list")
					{
						var div_name=document.getElementById("category_list");
					}
					else
					{
						if(action=="category_all_stores")
						{
							var div_name=document.getElementById("category_all_stores");
						}
					}
				}
			}
		}
	}
	div_name.innerHTML=CodeToShow;
}


function go_to_store(store_id)
{
	if(store_id=="")
	return false;
	var host_name=get_host_name();
	location.href=host_name+"/store.php?store="+store_id;
}

function store_search(action,params)
{
	
	
	var max_page=0;
	if(params=="undefined")
	{
		var params_field=document.getElementById("store_keyword");
		var params=params_field.value;
		
	}
	if(action=="keyword_search")
	{
		var params_field=document.getElementById("store_keyword");
		var params=params_field.value;
		params_field.value="";
	}
	else
	{
		if(action=="category_all_stores")
		{
			show_category_all_stores(action,params);
			return false;
		}
	}
	
	
	var args=action+"###"+params+"###"+max_page;
	page_num=0;
	
	pagination(args,page_num);
	
}



function show_category_all_stores(action,category_id)
{
	var myForm = document.createElement("form");
  	myForm.method="post" ;
  	myForm.action = "store_cat_view.php" ;
  	
  	var myInput1 = document.createElement("input") ;
    myInput1.setAttribute("name", "action") ;
    myInput1.setAttribute("value", action);
    myForm.appendChild(myInput1) ;
    
    
    var myInput2 = document.createElement("input") ;
    myInput2.setAttribute("name", "category_id") ;
    myInput2.setAttribute("value", category_id);
    myForm.appendChild(myInput2) ;
    
    
    document.body.appendChild(myForm) ;
  	myForm.submit() ;
 	document.body.removeChild(myForm) ;
}


function favourite_this_store(store_id)
{
	
	store_id=trim(store_id);
	if(store_id=="")
	{
		
		return false;
	}
	x_favourite_this_store(store_id,ReturnFavourites);
}

function ReturnFavourites(theCode)
{
	
	if(theCode=="notloggedin")
	{
		alert("You need to log in to favourite this store.");
		return false;
	}
	else
	{
		if(theCode=="already_exists")
		{
			alert("You have already added this store to your favourite list.");
			return false;
		}
		else
		{
			if(theCode>0)
			{
				theCode="("+theCode+" favourites)";
			}
			document.getElementById("favourite_count").innerHTML=theCode;
	
		}
	}
	
}

function update_store_content(action,store_id,params)
{
	var go=0;
	if(params=="undefined")
	{
		params="";
	}
	var args="";
	
	if(action=="add_new_comment")
	{
		go=1;
		var content_div=document.getElementById("add_new_comment");
		var params=content_div.value;
		params=trim(params);
		if(params=="")
		{
			return false;
		}
		
		content_div.value="";
		args=action+"###"+store_id+"###"+params;
		
		x_update_store_content(args,ReturnCode);
		
	}
	
	
}



function updateRating(action,item_id,rate,total_rating,total_votes)
{
	var args=action+"###"+item_id+"###"+rate+"###"+total_rating+"###"+total_votes;
	
	x_update_rating(args,ReturnRating);
}


function ReturnRating(theCode)
{
		var tmp_arr=new Array();
		tmp_arr=theCode.split("~~~");
		action=tmp_arr[0];
		item_id=tmp_arr[1];
		CodeToShow=tmp_arr[2];
		var error_flag=0;
		if(action=="comment_rating")
		{
		
			if(CodeToShow!='rated' && CodeToShow!='notloggedin')
			{
				var div_id_name="comment_rate_"+item_id;
				
			}
			else
			{
				if(CodeToShow=='rated')
				{
					error_flag=1;
					var alert_message="You have already rated this comment.";
				}
				else
				{
					if(CodeToShow=="notloggedin")
					{
						error_flag=1;
						var alert_message="You need to login to rate this comment.";
					}
				}
			}
			
		}
		else
		{
			if(action=="store_rating")
			{
				
				if(CodeToShow!='rated' && CodeToShow!='notloggedin')
				var div_id_name="store_rate_"+item_id;
				else
				{
					if(CodeToShow=='rated')
					{
						error_flag=1;
						var alert_message="You have already rated this store.";
					}
					else
					{
						if(CodeToShow=="notloggedin")
						{
							error_flag=1;
							var alert_message="You need to login to rate this store.";
						}
					}
				}
			
			}
		}
		
		if(!error_flag)
		document.getElementById(div_id_name).innerHTML=CodeToShow;
		else
		{
			alert(alert_message);
		}
	
}



function search_products(store_id)
{
	action="keyword_search";
	if(action=="keyword_search")
	{
		var params_field=document.getElementById("product_keyword");
		var params=trim(params_field.value);
		params_field.value="";
	}
	if(params!="")
	{
	
		var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = "/products-search" ;
  
    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", "store_id") ;
    myInput.setAttribute("value", store_id);
    myForm.appendChild(myInput) ;
     var myInput1 = document.createElement("input") ;
    myInput1.setAttribute("name", "keyword") ;
    myInput1.setAttribute("value",  params);
    myForm.appendChild(myInput1) ;
     
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;
	
		
	}
	else
	{
		alert("Please provide a key word to search products.");
		return false;
	}
}
