function addInputField(appendId, rowId, addRowNumber)
{
	var new_el 			= Builder.node('div',{id: rowId+addRowNumber, style: 'display: none'});
	new_el 				= $(new_el);
	$(appendId).appendChild(new_el);
	return false;
}

var reloadInitialBox=false;

function deleteInputField(rowId, plusId, delRowNumber, formId, initFunction)
{
	delRowNumber = parseInt(delRowNumber);

	// get the highest element
	var form 	= $(formId);
	var counter = parseInt(form.getInputs('hidden', 'counter').size());

	if (counter && counter>=1)
	{
		var previousDivTag  = $(rowId+delRowNumber).previous();
		var nextDivTag		= $(rowId+delRowNumber).next();

		$(rowId+delRowNumber).remove();

		if (previousDivTag && nextDivTag == null)
		{
			previousId = $(previousDivTag).down('input.hiddenCounter').getValue();
			if (delRowNumber >= previousId)
			{
				$(previousDivTag).down('a.plusIcon').toggle();
			}
		}
	}

	if (delRowNumber<=0)
		reloadInitialBox = true;
	else
		reloadInitialBox = false;

	return false;
}

function hideNotificationAndErrorBox()
{
	$('flash_error_box').hide();
	$('flash_notifications_box').hide();
}

function updateNotificationBoxes(request, json, ulDivId, boxDivId)
{
	hideNotificationAndErrorBox();
	$('header').scrollTo();

	responses = Object.values(json);

	if (!json){
	  //if you don't use the json tips then evaluate the renderedText instead
	  if (request.responseText && Object.isString(request.responseText))
		  var responses = request.responseText.evalJSON(true);
	  else
		  var responses = request; 	
	}

	$(ulDivId).update('');
	var uList = Builder.node('ul', {className:'flash_inner_message', id: ''});

	if (Object.isString(responses))
	{
		var innerText = new Element('span', { 'class': 'index' }).update(responses);
		uList.appendChild(Builder.node('li', innerText));
	}
	else
	{
		// responses = responses;
		responses.each(function(jsonRow) {
			var innerText = new Element('span', { 'class': 'index' }).update(jsonRow);
			uList.appendChild(Builder.node('li', innerText));
		});
	}

	$(ulDivId).appendChild(uList);
	$(boxDivId).show();
}

function showJSONErrorBox(request, json)
{
	updateNotificationBoxes(request, json, 'addError', 'flash_error_box');
}

function hideErrorBox()
{
	$('flash_error_box').hide();
}

function showJSONNotificationsBox(request, json)
{
	updateNotificationBoxes(request, json, 'addNotificationMessage', 'flash_notifications_box');
}

function image_upload_submit() {
	replaceContentWithIndictator('tempUserPictureBox');
 }

 function display_uploaded_temp_image(url, hashString) {
 	var bgImage;
 	$('tempUserPictureBox').hide();
 	new Ajax.Updater('tempUserPictureBox', url, { asynchronous:true, onSuccess:function(){ new Effect.Appear('tempUserPictureBox');}, parameters: { pictureHash: hashString }});
 }
 
 
 function setSelectToLoading(selectId)
 {
 	// set all options to null to delete them
 	
 	if (Object.isElement(selectId))
 	{
 		if ($(selectId).length > 0)
 		{
 			for(i=0;i<$(selectId).length;i++)
			{
				$(selectId).options[i] = null;
			}
 		}
 		$(selectId).options[0] = new Option('Loading...', 0);
	}
 }
 
 function avatarBoxPopup(id, name, url, timeStamp)
 {

 	new Tip('member_picture_'+id+'_'+timeStamp,
			'<div id="member_avatarboxBig_'+id+'_'+timeStamp+'" style="width: 320px; height: 170px;"><div style="width: 100%; height: 100%; margin: 0 auto; vertical-align: middle; text-align: center;" align="center"><img src="/images/loadingd_tp.gif" align="center" style="text-align: center; vertical-align: middle; margin: 0px auto;" width="50" height="57"/></div></div><span class="ptip-arrow ptip-arrow-left-middle" style="left: -10px; top: 100px;"/>', 
			{ 
				className: 'decayennePtip', 
				fixed: true, 
				hideOn: false,
				hideAfter: 0.0001,
				hook:{
					target: 'rightMiddle', 
					tip: 'leftMiddle'
				}, 
				title: name
			}
	);

	$('member_picture_'+id+'_'+timeStamp).observe('mouseover',
									function()
									{
										new PeriodicalExecuter(function(pe) {
											if ($('member_avatarboxBig_'+id+'_'+timeStamp))
											{
												if ($('member_avatarboxBig_'+id+'_'+timeStamp).firstDescendant().identify() != 'avatarbox_popup_innner_'+id) 
												{
													new Ajax.Updater(
														'member_avatarboxBig_'+id+'_'+timeStamp, 
														url, 
														{ 
															parameters: { member_id: id},
															evalScripts: true
														}
													)
												}
												pe.stop();
											}
										}, 0.1);
									}
	);

 }
 
 
function setVotes(request, json)
{
	votes = request.responseText.evalJSON(true);
	rating.setValue(votes.rating);
	pricing.setValue(votes.pricing);
	$('countVotes').update(votes.votes);
}

try
{
	Abstract.DelayedObserver = function() {};
	Abstract.DelayedObserver.prototype = {
		initialize:function(element, frequency, callback){
			this.element = $(element);
			this.event = 'keyup';
			this.frequency = frequency;
			this.timer = null;
			this.callback = callback;
			
			this.registerObserver();
		},
		
		registerObserver: function(){
			Event.observe(this.element, this.event, this.onTimeoutEvent.bind(this));
		},
		
		doCallback: function() {
	    	this.callback(this.element, this.getValue());
		},
		
		onTimeoutEvent: function(){
			clearTimeout(this.timer);
			this.timer = setTimeout(this.doCallback.bind(this), this.frequency * 1000);
		}
	};
}
catch (e)
{
	
}

/*
Form.Element.DelayedObserver = Class.create();
Form.Element.DelayedObserver.prototype = Object.extend(new Abstract.DelayedObserver(), {
	getValue: function() {
    	return Form.Element.getValue(this.element);
	}
});
*/


function keepMemberOnline(ef)
{

	if (keepMemberOnlineURL && !keepMemberOnlineURL.empty())
	{
		url = keepMemberOnlineURL.stripScripts();
		new Ajax.Request(url);
	}
}
var checkingImRequest;

function hasIMRequests(url, urlToCall, pe)
{
	if (checkingImRequest==0)
	{
		new Ajax.Request(url, {
			  method: 'get',
			  onCreate: function(transport) {
			  	checkingImRequest = 1;
			  },
			 onLoading: function(transport) {
			  	checkingImRequest = 1;
			  },
			  onSuccess: function(transport) {
			  	checkingImRequest = 0;
			    if (transport.responseText=="1")
			    {
			    	var win = Dialog.info(
		    			{
		    				url: urlToCall,
		    				options:
		    				{
		    					evalScripts:1,
		    					method:"get"
		    				}
			    		},
		    			{
		    				className:'woBorder',
		    				draggable: false, 
		    				hideEffect:Element.hide,
		    				opacity:1, 
		    				recenterAuto:true, 
		    				resizable:false, 
		    				script:1, 
		    				showEffect:Element.show, 
		    				zIndex:1000
		    			}
			    	);
			    	pe.stop(); 
			    }
			  }
			});
	}
}

function checkIMRequests(urlToCheckPendingRequests, urlToCall)
{
	checkingImRequest = 0;
	pe = new PeriodicalExecuter(function(pe) {
		hasIMRequests(urlToCheckPendingRequests, urlToCall, pe);
	}, 5);
}

function refreshPendingRequestLoop(urlToGetPendingRequests)
{
	new Ajax.Updater('pendingRequestLoop', urlToGetPendingRequests, {
		parameters: { refreshLoop: 1 },
		evalScripts: true,
		evalJS: 'force',
		onLoading: replaceContentWithIndictator('pendingRequestLoop')
	});
}

function checkPendingRequestInPopupLoop(urlToCheckPendingRequests, urlToGetPendingRequests, checkingImRequest)
{
	if (checkingImRequest==0)
		{
			new Ajax.Request(urlToCheckPendingRequests, {
				  method: 'get',
				  onCreate: function(transport) {
				  	checkingImRequest = 1;
				  },
				  onLoading: function(transport) {
					checkingImRequest = 1;
					replaceContentWithIndictator('pendingRequestLoop');
				  },
				  onSuccess: function(transport) {
				  	checkingImRequest=0;
				    if (transport.responseText=="1")
				    {
				    	refreshPendingRequestLoop(urlToGetPendingRequests);
				    }
				    else
				    {
				    	pe.stop();
				    	self.checkIMRequests(urlToCheckPendingRequests, urlToGetPendingRequests);
				    	Dialog.okCallback();
				    }
				}
			});
	}
}

function checkPendingRequestInPopup(urlToCheckPendingRequests, urlToGetPendingRequests)
{
	checkingImRequest = 0;
	checkPendingRequestInPopupLoop(urlToCheckPendingRequests, urlToGetPendingRequests, checkingImRequest);
	pe = new PeriodicalExecuter(checkPendingRequestInPopupLoop(urlToCheckPendingRequests, urlToGetPendingRequests, checkingImRequest), 5);
}

function acceptIMRequest(sender, recipient, urlToGetPendingRequests, urlToCheckPendingRequests)
{
	replaceContentWithIndictator('pendingRequestLoop');
	window.open('http://www.decayenne.com/chat/im.php?rec='+sender, 'IM_'+sender, 'width=550,height=500,left=0,top=0,location=no,menubar=no,toolbar=no,resizable=yes,scrollbars=no,status=no');
	window.setTimeout("checkPendingRequestInPopup('"+urlToCheckPendingRequests+"','"+urlToGetPendingRequests+"')", 3000);
}

function deleteIMRequest(sender, recipient, urlToGetPendingRequests, urlToCheckPendingRequests)
{
	new Ajax.Request('http://www.decayenne.com/chat/XML.php?module=im&action=deletePend&sender='+sender+'&recipient='+recipient+'&_t='+GetRandom(1,90000), 
	{
		method: 'get',
		evalJS: 'force',
		onLoading: replaceContentWithIndictator('pendingRequestLoop'),
		onSuccess: function(transport) {
			checkPendingRequestInPopup(urlToCheckPendingRequests,urlToGetPendingRequests);
	  	}
	});
}

function closePendingRequestBoxAndStartCheckingAgain(urlToCheckPendingRequests, urlToCall)
{
	Dialog.okCallback();
	self.checkIMRequests(urlToCheckPendingRequests, urlToGetPendingRequests);
}