function informNewMessage()
{
	var informed = getCookie('inm');
	if(informed) return;
	
	var hider = document.getElementById('hider');
	hider.style.display = 'block';
	
	var div 		= document.createElement('DIV');
	div.id 			= 'infoPopup';
	div.innerHTML 	= 'Имате ново лично съобщение!<br> <a href="?page=messages&box=in" onClick="hideInfoPanel(2);">Прочети</a> | <a href="javascript:hideInfoPanel(1);">Затвори</a> | <a href="javascript:hideInfoPanel(2);">Не ме безпокой</a>';
	div.style.top 	= ((window.screen.height / 2) - 100)+'px';
	div.style.left 	= ((window.screen.width / 2) - 125)+'px';
	
	document.body.appendChild(div);
}

function hideInfoPanel(sc)
{
	if(sc == 1){
		deleteCookie('inm');
	}
	else if(sc == 2){
		setCookie('inm', 1);	
	}
	
	var hider = document.getElementById('hider');
	hider.style.display = 'none';
	var div = document.getElementById('infoPopup');
	if(div){
		document.body.removeChild(div);	
	}
}

function addPhotoToShared(id)
{
	var place = window.opener.document.getElementById('attachedContainer');
	var added = window.opener.document.getElementById('attachedIds');
	var list  = (added.value != '' ? added.value.split(',') : new Array());
	
	var html  = '<a href="javascript:void[0];" title="Премахни снимката" onClick="removeAttachedPhoto(this, '+id+');"><img src="photo.php?id='+id+'&t=1" width="95" height="95" border="0" style="border:1px solid #333333; margin:0px 15px 15px 0px;" /></a>';
	
	var exists = false;
	for(var i=0; i<list.length; i++){
		if(list[i] == id){
			exists = true;	
		}
	}
	if(!exists){
		if(list.length < 10)
		{
			place.innerHTML += html;
			list.push(id);
			added.value = list.join(",");
		}
		else{
			alert('Можете да добавите до 10 снимки');	
		}
	}
	else{
		alert('Тази снимка вече е добавена към излета');	
	}
}

function removeAttachedPhoto(el, id)
{
	if(confirm('Премахване на снимката от излета?'))
	{
		el.parentNode.removeChild(el);
		
		var added = document.getElementById('attachedIds');
		var list  = (added.value != '' ? added.value.split(',') : new Array());
		var clean = new Array();
		
		for(var i=0; i<list.length; i++){
			if(list[i] != id){
				clean.push(list[i]);
			}
		}
		added.value = clean.join(",");
	}
}

function showGmap(lid)
{
	var url = 'gmap.php?lid='+lid;
	popWindow(url, 760, 480)
}

function showSmap(sid, lid)
{
	var url = '/smap.php?sid='+sid+'&lid='+lid;
	popWindow(url, 760, 480)
}

function showGPS(frm)
{
	if(frm.S_latitude.value == ''){
		alert('Въведете Latitude стойност!');
		frm.S_latitude.focus();
		return;
	}
	if(frm.S_longitude.value == ''){
		alert('Въведете Longitude стойност!');
		frm.S_longitude.focus();
		return;
	}
	
	var url = 'http://maps.google.com/maps?f=q&hl=en&q='+frm.S_latitude.value+','+frm.S_longitude.value+'&ie=UTF8&z=12&om=1&iwloc=addr&t=h';
	popWindow(url, 800, 600, 1)
}
function openGPS(S_latitude, S_longitude)
{	
	var url = 'http://maps.google.com/maps?f=q&hl=en&q='+S_latitude+','+S_longitude+'&ie=UTF8&z=12&om=1&iwloc=addr&t=h';
	popWindow(url, 800, 600, 1)
}

function showMap()
{
	window.open('map.html','Map','resizable=yes,width=429,height=516,top=250,left=350,scrollbars=no');	
}

function popWindow(winLink, wWidth, wHeight, resizable)
{
    var scrollbar = false;
    var randomNum = Math.random()*1000000; var winWidth = wWidth + 20; var winHeight = wHeight + 20;
    randomNum = parseInt(randomNum); if(isNaN(randomNum)) randomNum = 344544;
    var winName = 'popup_'+randomNum;
    if (scrollbar) { scrollbar = ',scrollbars'; } else { scrollbar=''; }; if (resizable) { resizable = ',resizable'; }
    else { resizable=''; }; var winX = (window.screen.width / 2) - (winWidth / 2);
    var winY = (window.screen.height / 2) - (winHeight / 2);
    popup=window.open("", winName,
    "width="+winWidth+" ,height="+winHeight+resizable+scrollbar+",left="+winX+",top="+winY);
    popup.document.location = winLink; popup.self.focus();
};

function addElement()
{
	var tbody = document.getElementById('elemsContainer');
	tr = document.createElement("tr");
	

	td = document.createElement("td");
	tr.appendChild(td);
	tbody.appendChild(tr);
	td.innerHTML = '<input name="sub[field_label][]" type="text" size="35" />';

	td = document.createElement("td");
	td.align='center';
	tr.appendChild(td);
	td.innerHTML = '<input name="sub[field_position][]" type="text" size="3" />';

	td = document.createElement("td");
	td.align='center';
	tr.appendChild(td);
	td.innerHTML = '<input name="sub[field_checked][]" type="checkbox" value="1" />';

	td = document.createElement("td");
	td.align='center';
	tr.appendChild(td);
	td.innerHTML = '<img src="images/delete.gif" onclick="delRow(this.parentNode.parentNode)" width="11" height="12" style="cursor:pointer;margin-top:3px;" />';
}

function delRow(row)
{
	var tbody = document.getElementById('elemsContainer');
	for(var i=0; i<tbody.childNodes.length; i++){
		if(tbody.childNodes[i] == row){
			tbody.removeChild(tbody.childNodes[i]);
		}
	}
}

function checkForm(frm)
{
	var elems = frm.elements;
	for(var i=0; i<elems.length; i++){
		var rule = elems[i].getAttribute('rule');
		var msg	 = elems[i].getAttribute('msg');
		if(rule && msg){
			switch(rule){
				case "empty":
					if(elems[i].value == ''){
						elems[i].focus();
						alert(msg);
						return false;
					}
				break;
				case "email":
					if(!elems[i].value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)){
						elems[i].focus();
						alert(msg);
						return false;
					}
				break;
				case "numbers":
					if(!elems[i].value.match(/^[0-9]/)){
						elems[i].focus();
						alert(msg);
						return false;
					}
				break;
				case "checked":
					if(elems[i].checked == false){
						elems[i].focus();
						alert(msg);
						return false;
					}
				break;
				case "select":
					if(elems[i].options[elems[i].selectedIndex].value == ''){
						elems[i].focus();
						alert(msg);
						return false;
					}
				break;
				case "group":
					var is_checked = false;
					var group = frm[''+elems[i].name+''];
					for(var e=0; e<group.length; e++){
						if(group[e].checked == true){
							is_checked = true;
						}
					}
					if(!is_checked){
						elems[i].focus();
						alert(msg);
						return false;
					}
				break;
				default:
					var reg = new RegExp(rule);
					if(!elems[i].value.match(reg)){
						elems[i].focus();
						alert(msg);
						return false;
					}
				break;
			}
		}
	}
	return true;
}