
/*handle catalog items*/
function ShowCatalogItems()
{
	var argv = ShowCatalogItems.arguments;
  	var argc = argv.length;
	var switched_item = 'true';
	for(i = 0; i < argc; i++)
	{
		switched_item = ($('catalog_'+argv[i]).style.display == 'none')?('false'):('true');
		$('catalog_'+argv[i]).style.display = (switched_item == 'false')?('block'):('none');
	}
	
	$('item_switched_'+argv[0]).innerHTML = (switched_item == 'false')?('Restrânge'):('Toate');
	$('img_item_switched_'+argv[0]).src = (switched_item == 'false')?("/anunturi/_img/generic/gen_close.png"):("/anunturi/_img/generic/gen_open.png");
}
/*******************************
*comments function
**********************************/
//create/remove an add comment form and displayed it
function show_comment_form(location,parent,offer_id)
{
	if(!$('add_comment_'+parent))
	{
	new Ajax.Updater("comment_"+location, "/anunturi/ajax_controller.php",{parameters:{parent:parent,action:"show_comment_form", location:location,offer_id:offer_id}, insertion:'after', evalScripts:true} );
	}
	else
	{
		var d = $('add_comment_'+parent);
		d.parentNode.removeChild(d);
	}	
	
	$("clip_button_html").fire("comment:change");		
	$("clip_button_bbcode").fire("comment:change");		
}


//toggle function
//show/hide children comments

var comment_toggle = new Array();

function show_comments(key, childs)
{
	var child_divs = childs.split(",");
	//alert(comment_toggle[key]);
	if(comment_toggle[key] == '0')
	{	
		comment_toggle[key] = '1';
		$('nav_icon_'+key).src = '/anunturi/_img/generic/gen_closed.png';
		for(i =0; i < child_divs.length; i++)
		{
			$('comment_'+child_divs[i]).style.display = 'none'; 
		}
	}
	else
	{
		comment_toggle[key] = '0';
		$('nav_icon_'+key).src = '/anunturi/_img/generic/gen_open.png';
		for(i = 0; i < child_divs.length; i++)
		{
			$('comment_'+child_divs[i]).style.display = "";
		}		
	}
	$("clip_button_html").fire("comment:change");		
	$("clip_button_bbcode").fire("comment:change");
}

//show/hide all replies
var show_replies = 0;

function show_hidden_comments()
{
	var hidden_comments = $('comments_zone').select('[class="comment-hidden-tr"]');
	for(i = 0; i < hidden_comments.length; i++)
	{
		var display = (show_replies == 0)?(''):('none');
		hidden_comments[i].style.display = display;		
	}
	
	$('show_replies').src = (show_replies == 1)?('/anunturi/_img/generic/gen_open.png'):('/anunturi/_img/generic/gen_close.png');
	$('show_replies_text').innerHTML = (show_replies == 1)?('Deschide toate răspunsurile'):('Restrânge toate răspunsurile');
	
	//toggle nav icons
	var nav_icons = $('comments_zone').select('[class="nav_icon"]');
	var icon = (show_replies == 0)?('/anunturi/_img/generic/gen_open.png'):('/anunturi/_img/generic/gen_closed.png');
	for(i = 0; i < nav_icons.length; i++)
	{
		nav_icons[i].src = icon;
	}
	//get root comments ids
	var root_comments = $('comments_zone').select('[class="comment"]');
	for(i = 0; i < root_comments.length; i++)
	{
		var offer_id =  root_comments[i].id.substr(8);
		comment_toggle[offer_id] = (show_replies == 0)?(0):(1);
	}
	
	show_replies = (show_replies+1)%2;
	$("clip_button_html").fire("comment:change");		
	$("clip_button_bbcode").fire("comment:change");
}

//show report formular
function show_report()
{
	var report = $('report_offer').style.display;
	
	$('report_offer').style.display = (report == 'none')?(''):('none');	
	$('report_offer_th').style.display = (report == 'none')?(''):('none');	
	
	$("clip_button_html").fire("comment:change");		
	$("clip_button_bbcode").fire("comment:change");
}

function report_offer()
{
	if($F('report_text') == "")
	{
		return;
	}
	ser = $H(Form.serializeElements([ $("report_offer_id"), $("report_text") ], true));	
	ser.set('action', 'report_offer');
	
	var MyAjax = new Ajax.Request
	(
		'/anunturi/ajax_controller.php',
		{
			method:'post',
			parameters:ser,
			onSuccess:ReportOfferSuccess		
		}	
	)
}

function ReportOfferSuccess(Response)
{
	$('report_offer').style.display = 'none';
	$('report_offer_th').style.display = 'none';
	$('report_area').innerHTML = "Anunţul a fost raportat.";
	var div_success = $('report_area');
	new Effect.Highlight(div_success, {startcolor: '#99FF99',endcolor: '#ECF0F8', duration: 3});
	
	$("clip_button_html").fire("comment:change");		
	$("clip_button_bbcode").fire("comment:change");
}


//swap currencies
function swap_currency(currency)
{
	var currencies = new Array('offer_price_ro', 'offer_price_eur', 'offer_price_usd'); 
	//reset
	for(i = 0; i < currencies.length; i++)
	{
		$(currencies[i]).style.display = 'none';
	}
	
	//show current currency
	$(currencies[currency]).style.display = 'inline';
}

// show offer message form
function show_msg_form()
{
	$('tr_msg_title').style.display = '';
	$('tr_msg_body').style.display  = '';
	
	$("clip_button_html").fire("comment:change");		
	$("clip_button_bbcode").fire("comment:change");
}

function hide_msg_form()
{
	$('tr_msg_title').style.display = 'none';
	$('tr_msg_body').style.display  = 'none';
	$('msg_text').value = ' *Trimite un mesaj vânzătorului prin care iţi exprimi dorinţa de a achiziţiona produsul.';
	
	$("clip_button_html").fire("comment:change");		
	$("clip_button_bbcode").fire("comment:change");
}

function check_msg_text()
{
	//clear textarea
	if($F('msg_text')== ' *Trimite un mesaj vânzătorului prin care iţi exprimi dorinţa de a achiziţiona produsul.')
	{
		$('msg_text').value = "";		
		return;
	}
	
	//reset textarea
	if($F('msg_text') == "")
	{
		$('msg_text').value = ' *Trimite un mesaj vânzătorului prin care iţi exprimi dorinţa de a achiziţiona produsul.';		
		return;
	}
}

function send_message()
{
	var errors = "";
	if($F('msg_title') == "") errors += "- Completează titlul mesajului\n";
	if($F('msg_text') == "" || $F("msg_text") == " *Trimite un mesaj vânzătorului prin care iţi exprimi dorinţa de a achiziţiona produsul.")  errors += "- Completează mesajul\n";
	
	if(errors != "")
	{
		alert(errors);
		return;
	}
	
	$('send_message_form').submit();
}

function delete_message(msg_id)
{
	if(confirm('Doriţi sa ştergeţi oferta?'))
	{
		document.location = "/anunturi/delete_message.php?msg_id="+msg_id;
	}
}

/** rating functions **/
function filter_ratings()
{
	//get rating type
	rt = $F('filter_rating_type');
		
	//get current url
	var url = location.href;
	var has_params = url.search(/\?/);
	if(has_params == -1)
	{
		var new_url = (rt!= "")?(url+"?rt="+rt):(url);
	}
	else
	{		
		var new_url = (rt != "")?(url.substr(0,has_params)+"?rt="+rt):(url.substr(0,has_params));
	}
		
	//redirect
	document.location = new_url;
}

function show_rating_reply(rating)
{
	var display = ($('rating_reply_'+rating).style.display == 'none')?('block'):('none');
	
	$("rating_reply_"+rating).style.display = display;
}

function send_reply(rating)
{	
	var rating_reply = $F('rating_reply_text_'+rating);
	rating_reply = escape(rating_reply);
	if(rating_reply == '')
	{
		return;
	}
	var MyAjax = new Ajax.Request
	(
		'/anunturi/ajax_controller.php',
		{
			method:'post',
			parameters:"action=rating_reply"+
						"&rating_reply="+rating_reply+
						"&rating_id="+rating,						
			onSuccess:SendReplySuccess		
		}	
	)	
}

function SendReplySuccess(Response)
{
	
	eval("var Result="+Response.responseText);
	
	var rating_comment = $('rating_comment_'+Result.rating);
	var reply_link     = $('rating_reply_link_'+Result.rating);
	var reply_textarea = $('rating_reply_'+Result.rating);
	
	rating_comment.removeChild(reply_textarea);
	reply_link.innerHTML = "<b>Raspuns:</b>"+Result.text;
}

function filter_users()
{
	//get user type
	ut = $F('user_types');
		
	//get current url
	var url = location.href;
	var has_params = url.search(/\?/);
	if(has_params == -1)
	{
		var new_url = (ut!= "")?(url+"?ut="+ut):(url);
	}
	else
	{		
		var new_url = (ut != "")?(url.substr(0,has_params)+"?ut="+ut):(url.substr(0,has_params));
	}
	
	document.location = new_url;
}

function show_add_rating(offer, user)
{
	display = ($('add_rating_span_'+offer+'_'+user).style.display =="none")?("inline"):("none");
	$('add_rating_span_'+offer+'_'+user).style.display = display;
}


//add rating 
function add_rating(offer, user)
{
	var rating = $F('rating_'+offer+'_'+user);
	var rating_comment = $F('rating_comment_'+offer+'_'+user);
	rating_comment = escape(rating_comment);
	var MyAjax = new Ajax.Request
	(
		'/anunturi/ajax_controller.php',
		{
			method:'post',
			parameters:"action=add_rating"+
						"&rt_id="+rating+
						"&rating_comment="+rating_comment+
						"&offer_id="+offer+
						"&buyer="+user,						
			onSuccess:AddRatingSuccess,
			onFailure:AddRatingFailure		
		}	
	)	
	
}

function AddRatingSuccess(Response)
{
	eval("var Result="+Response.responseText);
	
	if(Result.success == false)
	{
		AddRatingFailure(Response);
		return;
	}
	
	//remove row
	var row = $("tr_rating_"+Result.id);
	row.parentNode.removeChild(row);	
	
	//show log
	var div_success = $('add_rating_success');
	div_success.style.display = "block";
	new Effect.Highlight(div_success, {startcolor: '#99FF99',endcolor: '#ECF0F8', duration: 3});
}

function AddRatingFailure(Response)
{
	var div_success = $('add_rating_success');
	div_success.style.display = "none";
}

function show_edit_rating(rating_id)
{
	display = ($('add_rating_span_'+rating_id).style.display =="none")?("inline"):("none");
	$('add_rating_span_'+rating_id).style.display = display;
}
//edit rating 
function edit_rating(rating_id)
{
	var rating = $F('rating_'+rating_id);
	var rating_comment = $F('rating_comment_'+rating_id);
	rating_comment = escape(rating_comment);
	var MyAjax = new Ajax.Request
	(
		'/anunturi/ajax_controller.php',
		{
			method:'post',
			parameters:"action=edit_rating"+
						"&rt_id="+rating+
						"&rating_comment="+rating_comment+
						"&rating_id="+rating_id,	
			onSuccess:EditRatingSuccess		
		}	
	)	
	
}

function EditRatingSuccess(Response)
{
	eval("var Result = " +Response.responseText);
	
	if(Result.success == true)
	{
		document.location = Result.link;
	}	
	else
	{
		alert("Calificativul nu a fost modificat");
		return;
	}	
}

/*global search*/
function submitGlobalSearch()
{	
	if($F("global_search_term") == "" || $F("global_search_term").length < 3)
	{
		return;
	}
	else
	{
		if($F('search_category') == 0) $('search_category').disabled = true;
		
		document.global_search.submit();
	}
}

//copy to clipboard 
function initClipboard()
{
	clip_html = new ZeroClipboard.Client();
	clip_html.setHandCursor(true);
	clip_html.addEventListener('mouseOver', copyClipboard('html'));	
	clip_html.addEventListener('complete', my_complete);
	clip_html.glue('clip_button_html');
	
	clip_bbcode = new ZeroClipboard.Client();
	clip_bbcode.setHandCursor(true);
	clip_bbcode.addEventListener('mouseOver', copyClipboard('bbcode'));	
	clip_bbcode.addEventListener('complete', my_complete);
	clip_bbcode.glue('clip_button_bbcode');	
}			

function copyClipboard(type)
{				
	var url = $F('url');
	var title = document.title;
	if (type == 'html') 
	{
		text = '<a href="' + url + '">' + title + '</a>';
		clip_html.setText(text);
	}
	else 
	{
		text = '[url=' + url + ']' + title + '[/url]';
		clip_bbcode.setText(text);
	}	
}


function my_complete()
{
	$('url').select();	
}

function repositionClipboard()
{
	clip_html.reposition();	
	clip_bbcode.reposition();
}

//suspend offer
function suspend_popup(id, offer_page)
{	
	var cls = (offer_page == true)?('hide_offer_page'):('hide_offer');		
	var hide_content = "<div  class='"+cls+"'>\n"+
						"<p  class='hide_offer_motiv'>Motiv:</p>\n"+
						"<textarea id='hide_reason_"+id+"' name='hide_reason'></textarea>\n"+
						"<p  class='hide_offer_links'><a href='javascript:suspend_offer("+id+");'>Do it</a>&nbsp;&nbsp;<a href='javascript:remove_popup("+id+");'>Cancel</a></p>"+
						"</div>";
	Element.update($('hide_div_'+id), hide_content);
	
}

function remove_popup(id)
{
	Element.update($('hide_div_'+id), '');
}

function suspend_offer(id)
{
	var reason = $F('hide_reason_'+id);
	var from = ($('from_page') != null)?("page"):("general");
	if(reason =='') 
	{
		alert('Specifică un motiv');
		return;
	}	
	var MyAjax = new Ajax.Request
	(
		'/anunturi/ajax_controller.php',
		{
			method:'post',
			parameters:"action=suspend_offer"+
						"&offer_id="+id+
						"&from="+from+
						"&reason="+encodeURIComponent(reason),
			onSuccess:SuspendOfferSuccess						
		}
	)
}

function SuspendOfferSuccess(Response)
{	
	if(Response.responseText != null)
	{
		eval("var Result="+Response.responseText);
		Element.update('hide_'+Result.id, Result.text);		
		Element.update($('hide_div_'+Result.id), '');
		
		if($('offer_img_'+Result.id) != null)
		{
			$('offer_img_'+Result.id).setStyle
			(
				{
					border: '1px solid red'
				}
			);
		}	
	}		
}

function restore_offer(id)
{
	var from = ($('from_page') != null)?("page"):("general");
	var MyAjax = new Ajax.Request
	(
		'/anunturi/ajax_controller.php',
		{
			method:'post',
			parameters:"action=restore_offer"+
						"&from="+from+
						"&offer_id="+id,
			onSuccess:RestoreImgSuccess
		}
	)
}

function RestoreImgSuccess(Response)
{
	if(Response.responseText != null)
	{		
		eval("var Result="+Response.responseText);
		Element.update('hide_'+Result.id, Result.text);	
		if($('offer_img_'+Result.id) != null)
		{
			$('offer_img_'+Result.id).setStyle
			(
				{
					border:'1px solid #DDDDDD'
				}
			);
		}				
	}	
	//remove moderate log
	$('moderate_log').remove();
}

function LoadCommentsEditor()
{
	//get comments div
	var comm = $('comments_table').select('[class="edit_comment"]');
	var ids = new Array();	
	//get comments ids
	var key = 0;
	comm.each(function(s)
		{
			var id = parseInt(s.id.replace('edit_com_', ''));
			ids[key] = id;
			key++;
		}
	)
	
	//add editors
	ids.each(function(s)
		{	
			new Ajax.InPlaceEditor
			(
				'edit_com_'+s,
				'/anunturi/ajax_controller.php', 
				{					 
					okText:'Modifică',
					cancelControl:'button', 
					cancelText:'Renunţă', 
					clickToEditText:'',
					externalControl:'external_controller_'+s, 
					externalControlOnly:true, 
					rows:4, 
					cols:20,
					highlightcolor:"#F5F9FD",
					highlightendcolor:"#F5F9FD" ,
					callback:function(form, value){return 'action=edit_comment&com_id='+s+'&com_text='+ encodeURIComponent(value) 					}										
				} 
			);
		}
	)
	
}

//hide comment
function hide_comment(id)
{
	if(confirm("Vrei să ascunzi comentariul?"))
	{
		var MyAjax = new Ajax.Request
		(
			'/anunturi/ajax_controller.php',
			{
				method:'post',
				parameters:"action=hide_comment"+
							"&com_id="+id,
				onSuccess:HideCommentSuccess
			}
		)
	}		
}

function HideCommentSuccess(Response)
{
	if(Response.responseText != null)
	{
		eval("var Result="+Response.responseText);
			
		if(Result.display == true)
		{
			//set pink background
			
			Result.ids.each(function(s)
				{
					$('comment_user_'+s).setStyle
					(
						{
							backgroundColor: '#FAE7E6'					
						}
					)
										
					$('comment_utility_'+s).setStyle
					(
						{
							backgroundColor: '#FAE7E6'					
						}
					)
					
					//replace link "Ascunde"
					Element.replace("hide_comment_link_"+s, "<a id='hide_comment_link_"+s+"' style='color: rgb(102, 134, 199);' href='javascript:restore_comment("+s+");'>Arată</a>")
				}
			)
			
			
			
		}
		else
		{
			
			Result.ids.each(function(s)
				{
					$('comment_'+s).remove();
				}
			)
			
		}
		//fire clipboard event
		$("clip_button_html").fire("comment:change");		
		$("clip_button_bbcode").fire("comment:change");
	}	
}

function restore_comment(id)
{
	var MyAjax = new Ajax.Request
	(
		'/anunturi/ajax_controller.php',
		{
			method:'post',
			parameters:"action=restore_comment"+
						"&com_id="+id,
			onSuccess:RestoreCommentSuccess
		}
	)
}

function RestoreCommentSuccess(Response)
{
	if(Response.responseText != null)
	{
		eval("var Result="+Response.responseText);
		
		Result.ids.each(function(s)
			{
				//set th background
				$('comment_user_'+s).setStyle
				(
					{				
						backgroundColor: '#ECF0F8'					
					}	
				);
						
				$('comment_utility_'+s).setStyle
				(
					{				
						backgroundColor: '#ECF0F8'					
					}	
				);
				
				//replace link "Arata"
				Element.replace("hide_comment_link_"+s, "<a id='hide_comment_link_"+s+"' style='color: rgb(102, 134, 199);' href='javascript:hide_comment("+s+");'>Ascunde</a>");
			}
		)	
		
		
		//fire clipboard event
		$("clip_button_html").fire("comment:change");		
		$("clip_button_bbcode").fire("comment:change");	
	}	
}

function show_report_comment(id)
{
	if(!$('report_comment_'+id))
	{	
	var report_form = '<div  id="report_comment_'+id+'" style="margin-left:135px;">'+
							'<img src="/anunturi/_img/generic/warning_16.gif"> <b><u>Raportează comentariu:</u></b>'+
							'<form class="form-report">'+								
								'<p style="padding-top:0px;padding-left:0px;">Motiv:</p>'+
								'<textarea name="report_comment" id="report_text_'+id+'" cols="7" rows="3" style="height:60px;margin-top:5px;" ></textarea>&nbsp;'+
								'<div style="margin:0px 0px 5px 60px;"><input type="button" name="report" value="Raportează" style="width:100px;" onclick="javascript:reportComment('+id+')">&nbsp;<input type="button" name="cancel" value="Renunţă" style="width:100px;" onclick="javascript:show_report_comment('+id+')"></div>'+
							'</form>'+							
							'</div>';
							
	$('edit_com_by_'+id).insert({after:report_form});							
	
	}
	else
	{
		var d = $('report_comment_'+id);
		d.parentNode.removeChild(d);
	}	
	
	
	$("clip_button_html").fire("comment:change");		
	$("clip_button_bbcode").fire("comment:change");	
}

function reportComment(id)
{
		
	if($F("report_text_"+id) == '') return;
		
	ser = $H({action:"report_comment", com_id:id, "report_text":$F("report_text_"+id)});	
		
	var MyAjax = new Ajax.Request
	(
		'/anunturi/ajax_controller.php',
		{
			method:'post',
			parameters:ser,
			onSuccess:ReportCommentSuccess		
		}	
	)
}

function ReportCommentSuccess(Response)
{
	eval("var Result="+Response.responseText);
	if(Result.success == true)
	{
		$("report_comment_"+Result.id).update('<img src="/anunturi/_img/generic/info_16.gif"> <i>Comentariul a fost raportat.</i>');
		//remove link
		$('report_comment_link_'+Result.id).remove();
		
		var div_success = $('report_comment_'+Result.id);
		new Effect.Highlight(div_success, {startcolor: '#99FF99',endcolor: '#ECF0F8', duration: 3});	
	}
	else
	{
		$("report_comment_"+Result.id).update('<i>Comentariul nu a fost raportat.</i>');
	}
	
	$("clip_button_html").fire("comment:change");		
	$("clip_button_bbcode").fire("comment:change");	
}
function spa__initClipboard()
{
	clip_html = new ZeroClipboard.Client();
	clip_html.setHandCursor(true);
	clip_html.addEventListener('mouseOver', spa__copyClipboard('html'));	
	clip_html.addEventListener('complete', spa__my_complete);
	clip_html.glue('clip_button_html');
	
	clip_bbcode = new ZeroClipboard.Client();
	clip_bbcode.setHandCursor(true);
	clip_bbcode.addEventListener('mouseOver', spa__copyClipboard('bbcode'));	
	clip_bbcode.addEventListener('complete', spa__my_complete);
	clip_bbcode.glue('clip_button_bbcode');	
}	
function spa__copyClipboard(type)
{				
	var url = $F('spa__url');
	var title = document.title;
	if (type == 'html') 
	{
		text = '<a href="' + url + '">' + title + '</a>';
		clip_html.setText(text);
	}
	else 
	{
		text = '[url=' + url + ']' + title + '[/url]';
		clip_bbcode.setText(text);
	}	
}
function spa__my_complete()
{
	$('spa__url').select();	
}
function spa__resizeLinkInput()
{
	
	$('spa__url').style.width = ($('spa__url_td').getWidth() - 20)+'px';
	//for IE
	$('spa__url').style.width = ($('spa__url_td').getWidth() - 20)+'px';
	
	spa__initClipboard();
}


function move(event)
{		
	var element = event.element();		
	var step = parseInt((element.title));
	
	$('next_step').value = step;
	$("main_form").submit();
}