var dates=[];


function showErrorBubble(obj, value)
{
	obj = $(obj);
	var pos =obj.getPosition();
	var size = obj.getSize().size;
	var wh = Window.getWidth();
	
	var bubble = $('errorBubble');
	
	if (pos.x+size.x+280 >= wh) 
	{
		bubble.className="errorBubble_reversed";
		bubble.style.left = (pos.x-size.x) + 'px';
		bubble.style.top = (pos.y-9)+'px';
		
	} else {
		bubble.className="errorBubble";		
		bubble.style.left = (pos.x+size.x) + 'px';
		bubble.style.top = (pos.y-9)+'px';
	}
	bubble.innerHTML = '<table style="width:100%; height:100%"><tr><td style="vertical-align:middle">' + value + '</td></tr></table>';
	bubble.style.display='';
}

function hideErrorBubble()
{
	var bubble = $('errorBubble');
	bubble.style.display='none';
}

function clearError(prefix,key)
{
	var inputs = $ES('input');
	var textareas = $ES('textarea');
	var selects = $ES('select');
	
	for (var i=0; i != inputs.length;++i)
	{
		var input = inputs[i];
		if (input.id == prefix+'_'+key && input.hasClass('error'))
		{
			input.removeClass('error');
			input.removeEvents();
			//input.removeEvent('focus');
			//input.removeEvent('blur');
			return;
		}
	}
	
	
	for (var i=0; i != inputs.length;++i)
	{
		var input = inputs[i];
		if (input.name == key && input.hasClass('error'))
		{
			input.removeClass('error');			
			input.removeEvents();
			//input.removeEvent('focus', showErrorBubble);
			//input.removeEvent('blur', hideErrorBubble);
			return;
		}
	}
	
	for (var i=0; i != textareas.length;++i)
	{
		var input = textareas[i];
		if  (input.name == key && input.hasClass('error'))
		{
			input.removeClass('error');			
			input.removeEvents();
			//input.removeEvent('focus');
			//input.removeEvent('blur');
			return;
		}
	}
	
	for (var i=0; i != selects.length;++i)
	{
		var input = selects[i];
		if  (input.name == key && input.hasClass('error'))
		{
			input.removeClass('error');			
			input.removeEvents();
			//input.removeEvent('focus');
			//input.removeEvent('blur');
			return;
		}
	}	
	
	var errorTD = $(prefix+'_'+key+'Error');
	if (errorTD)
	{
		errorTD.innerHTML='';
		if (errorTD.className == 'errorMsg')
			errorTD.style.display='none';
	}
		

}

function addErrorBubble(country, prefix, key,value)
{
	var errorTD = $(prefix+'_'+key+'Error');
	if (errorTD && errorTD.className == 'errorMsg')
	{
		errorTD.innerHTML = value;
		errorTD.style.display='';
		return;
	}
	

	
	if (errorTD)
	{
	//	var over = "showErrorBubble(this, '"+value+"')";
	//	errorTD.innerHTML = '<img src="/static_images/'+country+'/error.jpg" alt="" onmouseover="'+over+'" onmouseout="hideErrorBubble()" />';
	}

	var inputs = $ES('input');
	var textareas = $ES('textarea');
	var selects = $ES('select');
	
	
	for (var i=0; i != inputs.length;++i)
	{
		var input = inputs[i];
		if (input.id == prefix+'_'+key && !input.hasClass('error'))
		{
			input.addClass('error');
			input.addEvent('focus', function() { showErrorBubble(input, value) });
			input.addEvent('blur', function() { hideErrorBubble() });
			return;
		}
	}
	
	
	for (var i=0; i != inputs.length;++i)
	{
		var input = inputs[i];
		if  (input.name == key && !input.hasClass('error'))
		{
			input.addClass('error');
			input.addEvent('focus', function() { showErrorBubble(input, value) });
			input.addEvent('blur', function() { hideErrorBubble() });
			return;
		}
	}
	
	for (var i=0; i != textareas.length;++i)
	{
		var input = textareas[i];
		if  (input.name == key && !input.hasClass('error'))
		{
			input.addClass('error');
			input.addEvent('focus', function() { showErrorBubble(input, value) });
			input.addEvent('blur', function() { hideErrorBubble() });
			
			return;
		}
	}
	
	for (var i=0; i != selects.length;++i)
	{
		var input = selects[i];
		if  (input.name == key && !input.hasClass('error'))
		{
			input.addClass('error');
			input.addEvent('focus', function() { showErrorBubble(input, value) });
			input.addEvent('blur', function() { hideErrorBubble() });			
			return;
		}
	}	
	
} 

function showLogin()
{
	var shadow = $('shadow'), popup=$('popup'), ww=Window.getScrollWidth(), wh=Window.getScrollHeight();
	shadow.style.width=ww+'px';
	shadow.style.height=wh+'px';
	shadow.setOpacity(0.8);
	shadow.style.display='';
	
	popup.style.left=(ww/2-500/2)+'px';
	popup.style.top=(Window.getHeight()/2-250/2+Window.getScrollTop())+'px';
	popup.style.width='400px';
	popup.style.height='200px';
	
	new Ajax('/login.php', { method:'post', update:'popup', evalScripts:true}).request();
	
	
}

function buyStartingSubscription()
{
	var shadow = $('shadow'), popup=$('popup'), ww=Window.getScrollWidth(), wh=Window.getScrollHeight();
	shadow.style.width=ww+'px';
	shadow.style.height=wh+'px';
	shadow.setOpacity(0.8);
	shadow.style.display='';
	
	popup.style.left=(ww/2-500/2)+'px';
	popup.style.top=(Window.getHeight()/2-250/2+Window.getScrollTop())+'px';
	popup.style.width='500px';
	popup.style.height='270px';
	
	new Ajax('/buyStartingSubscription.php', { method:'post', update:'popup', evalScripts:true}).request();

}

function buyStartingSubscriptionCommit()
{
	new Ajax('/buyStartingSubscriptionCommit.php', { method:'post', evalScripts:true}).request();
}

function hidePopup()
{
	var shadow = $('shadow'), popup=$('popup');
	shadow.style.display='none';
	popup.innerHTML='';
	popup.style.display='none';
}

function scrollToContact()
{
}

function ajaxSubmit(formId)
{
	var form = $(formId);
	var data = form.toQueryString();
	new Ajax(form.action, { method:'post', data:data, evalScripts:true}).request();
	
}

function ajaxSubmit(button,action, progressBarId,formId)
{
	var button = $(button);
	if (button.hasClass('disabled'))
		return;
	button.setOpacity(0.5);
	button.addClass('disabled');
	$(progressBarId).style.display='';
	
	var form = $(formId);
	var data = form.toQueryString();
	new Ajax(action, { method:'post', data:data, evalScripts:true, 
			onComplete: function () { 
				button.setOpacity(1);
				button.removeClass('disabled');
				$(progressBarId).style.display='none';
			} 
	}).request();
	
}




function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function limitWritingKeydown (event, obj, limit, remainingId)
{
	obj = $(obj);
	var e = new Event(event);
	
	if (e.code == 8 || e.code==37 || e.code==39 || e.code==16 || e.code==36 || e.code==35 || e.code==38 || e.code==40 || e.code==46 || e.code==17)
		return true;
	var source=obj.value;	
	source=source.replace(/\n/gi,'');
	source=source.replace(/\r/gi,'');
	/*if (source.length >= limit)
		return false;
	return true;*/
}

function limitWritingKeyup (obj, limit, remainingId)
{
	var source=obj.value;	
	source=source.replace(/\n/gi,'');
	source=source.replace(/\r/gi,'');
	if (source.length > limit)
	{
		//obj.value = obj.value.substring(0,limit);
		$(remainingId).innerHTML = '<span style="color:red">('+(limit-source.length) +')</span>';
	} else 
		$(remainingId).innerHTML = '('+(limit-source.length) +')';
}


function removeOffer (id)
{
	new Ajax('/removeOffer.php?id=' + id, {  method:'post', evalScripts:true}).request();		
}

function prolongOffer (id)
{
	new Ajax('/prolongOffer.php?id=' + id, {  method:'post', evalScripts:true}).request();		
}


function saveNewsletterCategories()
{
	var radioButtons=$ES('input.rd');
	var categories='';
	for (var i=0; i != radioButtons.length;++i)
	{
		var rb = radioButtons[i];
		if (rb.checked)
		{
			var parts = rb.name.split('_');
			categories+=parts[1] + '|';
		}
	}

	$('newsletterCategories').value=categories;
	
}

function ads_updateDateFields()
{
	
	html='<div style="display: none; width: 320px;" class="errorMsg" id="buyAdvertisement_summarizedDatesError"></div><div class="block">';
	for (var i=0; i < dates.length;++i)
	{
		var date=dates[i];
		if (i % 3 == 0 && i > 0)
			html+= '</div><div class="block">';
								
		html+='<div class="line">';
		if (date.available)
			html+= '<input name="date_' + date.key + '" type="checkbox" class="chk" />' + date.date + '<br />';
		else
			html+= '<input name="date_' + date.key + '" type="checkbox" class="chk disabled" disabled="disabled" checked="checked" /><span class="disabled">'+ date.date + '</span>';
		html+='</div>';

	}
	
	html+='</div><div style="clear:both"></div>';
	$('adDates').innerHTML=html;
	fx.start({ 	'background-color': ['#FFFFAC','#FFFFFF'] });
}


function ads_changeDateFields(select)
{
	new Ajax('/buyAdvertisementGetDates.php?categoryId=' + select.value, { method:'post', evalScripts:true}).request();
}

function ajaxSaveAdvertisement (button,more)
{
	var buttons = $ES('a.button');
	for (var i=0; i != buttons.length;i++)
	{
		if (buttons[i].hasClass('disabled'))
			return;
		buttons[i].setOpacity(0.5);
		buttons[i].addClass('disabled');
			
	}
	
	var s='';
	var first=true;
	var dateCheckboxes = $ES('div#adDates input.chk');
	for (var i=0; i != dateCheckboxes.length; i++)
	{
		var dt = dateCheckboxes[i];
		if (dt.hasClass('disabled') || !dt.checked)
			continue;
		if (!first)
			s+='|';
			
		first=false;
		s+=dt.name.split('_')[1];
	}
	$('summarizedDates').value =s;
	
	$('saveMode').value = (more) ? 'more' : 'finish';
	
	
	
	$('progressBar').style.display='';
	
	var form = $('adForm');
	var data = form.toQueryString();
	new Ajax(form.action, { method:'post', data:data, evalScripts:true, 
			onComplete: function () { 
				
				buttons = $ES('a.button');
				for (var i=0; i != buttons.length;i++)
				{
					buttons[i].setOpacity(1);
					buttons[i].removeClass('disabled');
						
				}
				
				$('progressBar').style.display='none';
			} 
	}).request();

}

function removeAd(index, confirmMsg)
{
	if (confirm(confirmMsg))
		new Ajax('/buyAdvertisementRemove.php?index=' + index, { method:'post',  evalScripts:true }).request();
}

function showContact()
{
	var shadow = $('shadow'), popup=$('popup'), ww=Window.getScrollWidth(), wh=Window.getScrollHeight();
	shadow.style.width=ww+'px';
	shadow.style.height=wh+'px';
	shadow.setOpacity(0.8);
	shadow.style.display='';
	
	popup.style.left=(ww/2-650/2)+'px';
	popup.style.top=(Window.getHeight()/2-240/2+Window.getScrollTop())+'px';
	popup.style.width='650px';
	popup.style.height='240px';
	
	new Ajax('/contact.php', { method:'post', update:'popup', evalScripts:true}).request();

}

