

var activeElement = null;
var lastActive = null;

function updateField(key, value){
	var url = tmplwebpath+'/profile.jsp?updatefield='+key+'&value='+value;
	xmlLoadUrl(url, function(){});
}
var clickHack = false;
var deactivationActive = false;

function deactivateActiveElement(){
	deactivateElement(activeElement);
}

function deactivateElement(item){
	var deactivateItem = item;
	activeElement = null;
	setTimeout(function(){deactivationActive = false; deactivateElementReal(deactivateItem);}, 100);
}

function deactivateElementReal(item){
	updateField(item.attributes['dbkey'].value, item.attributes['value'].value);
	item.className = "clearfix datarow";	
	var valueNode = getFirstChild('value', item);
	if(valueNode!=null)
		valueNode.innerHTML = item.attributes['value'].value;
	
	var child = getFirstChild('inputDiv', item);
	
	if(child){
		child.style.display = 'none';
//		activeElement = null;
	}
	
}

function getFirstChild(title, item){
	if(item==null)
		return null;
	var childs = item.childNodes;
	for(i in childs){
		var child = childs[i];
		if(child.nodeType==1)
			if(child.attributes['name'])
				if(child.attributes['name'].value == title)
					return child;
	}
	return null;
}

function activateEdit(item){
	item.className = "active clearfix datarow";	
	if(activeElement==item || lastActive==item)
		return;
	
	if(activeElement!=null)
		deactivateActiveElement();
	
	var child = getFirstChild('inputDiv', item);
	
	if(child){
		child.style.display = 'inline';
		var inputField = getFirstChild('inputField', child);
		if(inputField)
			inputField.focus();
		if(item.attributes['edit'].value=='SELECT')
			setSpanValue(child);
	}
	activeElement = item;
}

function returnHiddenSelect(item){
	var key = item.attributes['dbkey'].value;
	var hidden = document.getElementById('hidden_select_'+key);
	var id = 'hidden_select_'+key+'_value';
	var selectHolder = document.getElementById(id).parentNode.parentNode;
	hidden.innerHTML = selectHolder.innerHTML;
	selectHolder.innerHTML = '';
	selectHolder.parentNode.removeChild(selectHolder);
}

function setFakeSelectValue2(id, val, id2){
	document.getElementById(id).innerHTML = val;
	activeElement.attributes['value'].value = document.getElementById(id).innerHTML;
//	document.getElementById(id).parentNode.onclick();
	deactivateActiveElement();
}

function setFakeSelectValue3(id, val, id2){
	document.getElementById(id).innerHTML = val;
	document.getElementById(id2).value = val;
}

function findChild(name, item, callback){
	var childs = item.childNodes;
	for(i in childs){
		if(childs[i].tagName && childs[i].tagName.toLowerCase()==name.toLowerCase())
			callback(childs[i]);
	}
}

function openAvatarForm(){
	document.getElementById('avatarFormDiv').style.display='block';
}
function closeAvatarForm(){
	document.getElementById('avatarFormDiv').style.display='none';
}

function submitMessageForm(form){
	var messageValue = form.messageText.value;
	document.getElementById('messageFormDiv').innerHTML = 'Sending message';
	xmlLoadUrl(tmplwebpath+'/profile.jsp?profile='+profileUsername+'&sendmessage='+messageValue, function(xmlHttp){
		document.getElementById('messageFormDiv').innerHTML = xmlHttp.responseText;
	});
}

function setSpanValue(node){
	findChild('span', node, function(child){
		if(child.innerHTML == '')
			child.innerHTML = 'Please Select';
	});
}

function openCloseDiv(node){
	if(node.style.display=='none'){
		node.style.display='block';
		return false;
	}
	node.style.display='none';
	return true;
}

function getSearchString(form, from){
	var ret = "";
	if(from >= 3 && form.DISTRICT.value != ""){
		if(ret!="")
			ret = ret + ', ';
		ret += form.DISTRICT.value;
	}
	if(from >= 1 && form.CITY.value != ""){
		if(ret!="")
			ret = ret + ', ';
		ret += form.CITY.value;
	}
	if(from >= 0 && form.STATE.value != ""){
		if(ret!="")
			ret = ret + ', ';
		ret += form.STATE.value;
	}
	return ret;
}

function registrationUpdateSenderForm(form){
	form.D_LAT.value = marker.getLatLng().lat();
	form.D_LNG.value = marker.getLatLng().lng();
	form.D_ZOOMLEVEL.value = map.getZoom();
}

function searchGMaps(form){
	showAddress(getSearchString(form, 3), 3);
}

function deleteVideo(url){
	if(confirm("Do you wish to delete photo/video?"))
		window.location.href=url;
	else
		return false;
}

function sendVideo(url){
		window.location.href=url;
}
