var Tabmin =
{
	_suggestTimeout: null,
	
	showFormTab: function(tab_num, querystring)
	{
		var tab_open=false;
		
		if(!querystring)
			querystring='';
		
		if(!tabSet.tabClosed(tab_num))
			tab_open=true;
		
		tabSet.showTab(tab_num);
		if(tab_open)
		{
			if(!confirm('There is a form already open. Are you sure you want to load another one over it? Any changes you have made will be lost.'))
				return;
		}
		
		tabSet.tabs[tab_num].reload(querystring);
	},
	
	appendTooltips: function()
	{
		var action_buttons, button, span, ie6;
		
		action_buttons=document.getElementsByClassName('action_button');
		
		for(var i=0; i<action_buttons.length; i++)
		{
			button = action_buttons[i];
			hasChildren = false;
			for(j=0; j<button.childNodes.length; j++)
			{
				if(button.childNodes[j].nodeType!=3) //Is it a textnode?
				{
					hasChildren = true;
				}
			}
			if(hasChildren && button.title.length > 0)
			{
				
				button.className+=' tool';
				
				span = document.createElement('span');
				span.className='tip';
				span.innerHTML=button.title;
				button.appendChild(span);
				
				ie6=('\v'=='v' && navigator.appVersion.match(/MSIE [56]\./));
				if(ie6)
				{
					button.onmouseover=function(tip)
					{
						tip.className='tip_hover';
					}.partial(span);
					
					button.onmouseout=function(tip)
					{
						tip.className='tip';
					}.partial(span);
				}
			}
			
			button.title='';
		}
	},
	
	suggestLocation: function(value)
	{
		
		if(!!this._suggestTimeout)
			clearTimeout(this._suggestTimeout);
			
		this._suggestTimeout=setTimeout(function()
		{
			var a, json, i, locationSuggest, resp, err, option, options;
			this._suggestTimeout = null;
			
			if(value=='')
			{
				gebi('locationSuggestContainer').style.display='none';
				return;
			}
			
			
			a=new Ajax(true, 'txt');
			a.get('/tabmin/modules/complexes/ajax.php?verb=suggest&q='+value, function()
			{
				if(a.ready())
				{
					if(a.status()==200)
					{
						resp = a.response();
						//alert(resp);
						try
						{
							json = JSON.parse(resp);
						}
						catch(err)
						{
							alert('Error parsing JSON: '+resp);
							return;
						}
						
						locationSuggest = gebi('locationSuggest');
						
						while(locationSuggest.options.length > 0)
							locationSuggest.remove(0);
						
						locationSuggest.size = 10;//(json.length > 1 ? json.length : 2);
						query = json['query'];
						if(query!=value)
							return;
						
						for(var i in json.placemarks)
						{
							option = document.createElement('option');
							option.appendChild(document.createTextNode(json.placemarks[i].address));
							option.value = json.placemarks[i].address;
							
							
							try
							{
								locationSuggest.add(option);
							}
							catch(err)
							{
								locationSuggest.appendChild(option);
							}
							
						}
						locationSuggest.onchange = function(addressObj)
						{
							addressObj = addressObj.placemarks[this.selectedIndex+1];
							gebi('complexes_form').elements['address'].value = addressObj.street;
							gebi('complexes_form').elements['city'].value = addressObj.city;
							gebi('complexes_form').elements['state'].value = addressObj.state;
							gebi('complexes_form').elements['zip'].value = addressObj.zip;
							gebi('complexes_form').elements['country'].value = addressObj.country_name;
							gebi('complexes_form').elements['latitude'].value = addressObj.latitude;
							gebi('complexes_form').elements['longitude'].value = addressObj.longitude;
							
							gebi('complexes_form').elements['city'].disabled = false;
							gebi('complexes_form').elements['state'].disabled = false;
							gebi('complexes_form').elements['zip'].disabled = false;
							gebi('complexes_form').elements['country'].disabled = false;
							
							gebi('locationSuggestContainer').style.display='none';
						}.partial(json);
						
						if(locationSuggest.options.length==0)
						{
							option = document.createElement('option');
							option.appendChild(document.createTextNode('Could not find address'));
							try
							{
								locationSuggest.add(option);
							}
							catch(err)
							{
								locationSuggest.appendChild(option);
							}
						}
						gebi('locationSuggestContainer').style.display='';
						
						
					}
					else if(a.status()!=0)
						alert('HTTP Error: '+a.status()+' -- '+a.response());
				}
			});
		}.bind(this), 500);
		
	},
	
	movePicture: function(li, step)
	{
		var i = 0;
		var sibling = li;
		
		for(i=0; i<li.parentNode.childNodes.length; i++)
		{
			if(li.parentNode.childNodes[i].nodeType==3) //delete text nodes
				li.parentNode.removeChild(li.parentNode.childNodes[i], true);
		}
		
		if(step>0)
		{
			if(li.nextSibling==null)
				sibling = li.parentNode.childNodes[0];	
			else
				sibling = li.nextSibling.nextSibling;
		}
		else
			sibling = li.previousSibling;
			
		li.parentNode.insertBefore(li.parentNode.removeChild(li, true), sibling);
		Tabmin.sendPictureSortOrder();
	},
	
	sendPictureSortOrder: function()
	{
		var i=0, sort_order = [];
		var a;
		var ul = document.getElementById('pics');
		for(i=0; i<ul.childNodes.length; i++)
		{
			if(ul.childNodes[i].nodeType==3)
				continue;
			sort_order.push(ul.childNodes[i].id.replace(/\D/g, ''));
		}
		a=new Ajax(true, 'txt');
		a.post('/tabmin/modules/pictures/ajax.php','verb=sort&order='+sort_order.join(','), function()
		{
			if(a.ready())
			{
				if(a.status()==200)
				{
					resp = a.response();
				}
				else if(a.status()!=0)
					alert('HTTP Error: '+a.status()+' -- '+a.response());
			}
		});
		
	}
	
};

function changePayment()
{
	var yes = gebi('payment_yes');
	var no = gebi('payment_no');
	if(no.checked)
	{
		gebi('member_price').style.display = 'none';
		gebi('non_member_price').style.display = 'none';
	}
	else
	{
		gebi('member_price').style.display = '';
		gebi('non_member_price').style.display = '';
	}
}

function blurTr(trID)
{	
	gebi(trID).style.backgroundColor = '#ee8888';
}

function unblurTr(trID)
{	
	gebi(trID).style.backgroundColor = '';
}

function removeTr(trID)
{
	var tr = gebi(trID);
	tr.style.display = "none";
}

function blurPictureTds(id)
{	
	blurTr('picturePhoto'+id);
	blurTr('pictureCaption'+id);
}

function unblurPictureTds(id)
{	
	unblurTr('picturePhoto'+id);
	unblurTr('pictureCaption'+id);
}

function formatPhoneNumber(num) 
{
    num = num.replace(/^[01]|\D/g, "");
    if (num.length > 10) 
	{
        num = num.substr(0, 10);
    }
    if (num.length > 6) 
	{
        num = num.replace(/^(\d{6})/, "$1-");
    }
    if (num.length > 3) 
	{
        num = num.replace(/^(\d{3})/, "$1-");
    }
    return num;
}

function formatBirthdate(num) 
{
	
    if (num.replace(/\D/g, "").length >= 4)
	{
		num = num.replace(/\D/g, "");
		num = num.substr(0, 4);
        num = num.replace(/^(\d{2})(\d{2})/, "$1/$2");
    }
    return num;
}

function expandRegistration(div)
{
	var timeout = setInterval(function(div)
	{
		div.style.height = (parseInt(div.style.height)+50)+'px';
		if(parseInt(div.style.height)>=div.scrollHeight)
			clearTimeout(timeout);
	}.partial(div), 10);
}

/*
function removePictureTds(id)
{
	removeTr('picturePhoto'+id);
	removeTr('pictureCaption'+id);
}

function showAddFamilyForm(action)
{
	var trs, i;
	
	trs=document.getElementsByClassName('users_form_family');
	for(i=0; i < trs.length; i++)
	{
		if(action=='add')
			trs[i].style.display='';
		else
			trs[i].style.display='none';
	}
}*/
