function InsertSmile(obj, i)
{
	var o=document.getElementById(obj);
	
	if(o.disabled==false)
	{
		var caretPos=getCaretPos(o);
		var l=o.value.length;
	
		o.value=o.value.substr(0,caretPos) + ' :'+i+': ' + o.value.substr(caretPos, l);
	}
}

function getCaretPos(obj)
{
	obj.focus();
  
	if(obj.selectionStart) return obj.selectionStart;//Gecko
	else if (document.selection)//IE
	{
		var sel = document.selection.createRange();
		var clone = sel.duplicate();
		sel.collapse(true);
		clone.moveToElementText(obj);
		clone.setEndPoint('EndToEnd', sel);
		return clone.text.length;
	}
	
	return 0;
}

var map;

function Init()
{
}

function pngdisplay(image, width, height, alt) 
{
	if(navigator.appName=='Microsoft Internet Explorer') 
	{ 
		document.write('<img src="images/empty.gif" style="width: ' + width + '; height: ' + height + '; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' + image + ', sizingMethod=scale)" alt="' + alt + '" title="' + alt + '">');
	} 
	else 
	{
		document.write('<img src="' + image + '" style="width: ' + width + '; height: ' + height + ';" alt="' + alt + '" title="' + alt + '">');
	}
}

function CheckBrowserComp()
{
	var br=document.getElementById("browser-name").value;
	var bv=parseFloat(document.getElementById("browser-ver").value);
	
	if((br=='MSIE' && bv < 7) || (br=='Firefox' && bv < 2) || (br=='Opera' && bv < 9) || (br=='Safari' && bv < 2) || br=='') return true;
	
	return false;
}

function SetCheckbox(ids)
{
	if(document.getElementById(ids).checked==true) document.getElementById(ids).checked=false;
	else document.getElementById(ids).checked=true;
}

function SetRadio(ids)
{
	document.getElementById(ids).checked=true;
}

function ShowPhoto(ids)
{
	var o=document.getElementById(ids);
	var temp=document.createElement("div");
	var cf=document.getElementById(ids+"-current-files");

	o.appendChild(temp) ;
	o.lastChild.innerHTML='<table cellspacing="0" style="margin-top:10px"><tr><td style="padding:0px 0px 0px 0px;font-size:11px">Фотография</td><td style="padding:0px 0px 0px 5px"><input type="file" name="file[]"></td></tr></table>';
	cf.value=parseFloat(cf.value)+1;
}

function SetCurrentFilesValues()
{
	document.getElementById("current-files").value=document.getElementById("photos").innerHTML;
}

function loadPrint()
{
	if(CheckBrowserComp())
	{
		if(document.getElementById('map')) document.getElementById('map').innerHTML='<p style="margin-top:200px" align="center">Ваш браузер устарел.<br>Вы не сможете воспользоваться картами.</p>';
	}
	else
	{
		if(document.getElementById('map'))
		{
			if(GBrowserIsCompatible())
			{
				if(document.getElementById('map'))
				{
					map=new GMap2(document.getElementById('map'));
					
					map.setCenter(new GLatLng(lat,lng), zoom);
					map.enableScrollWheelZoom();
					map.addControl(new GSmallMapControl());
					//map.addControl(new GMapTypeControl());
	
					PlaceMarker(lat,lng,'', 0);
				}
			}
		}
	}
	
	//window.print();
}

function ShowGoogle_form()
{
	if(CheckBrowserComp())
	{
		alert("Ваш браузер устарел.\nВы не сможете воспользоваться картами.");
	}
	else
	{
		if(document.getElementById("google"))
		{
			ShowGShadow();
			OpenUserWnd("google");
			load_form();
		}
	}
	
	return false;
}

function load_form() 
{
	if(GBrowserIsCompatible())
	{
		map=new GMap2(document.getElementById('map'));
		
		map.setCenter(new GLatLng(lat,lng), zoom);
		map.enableScrollWheelZoom();
		map.addControl(new GSmallMapControl());
		map.disableDoubleClickZoom();

		PlaceMarker(lat,lng,'',0)
		
		cg=new GClientGeocoder();
		
		GEvent.addListener(map, "click", function(marker, point) 
		{
			PlaceMarker('','',point,0);
		});

		GEvent.addListener(map, "zoomend", function(newLevel, oldLevel) 
		{
			document.getElementById('zoom').value=map.getZoom();
		});
	}
}

function CheckBrowserComp()
{
	var br=document.getElementById("browser-name").value;
	var bv=parseFloat(document.getElementById("browser-ver").value);
	
	if((br=='MSIE' && bv < 7) || (br=='Firefox' && bv < 2) || (br=='Opera' && bv < 9) || (br=='Safari' && bv < 2) || br=='') return true;
	
	return false;
}

function OpenUserWnd(ids)
{
	ShowGShadow();
	document.getElementById(ids).style.display='block';
	EjectUserWnd(ids);
	return false;
}

function CloseUserWnd(ids)
{
	unEjectUserWnd(ids);
	document.getElementById("g-shadow").style.display='none';
	
	return false;
}

function ShowGShadow()
{
	var arrayPageSize=getPageSize();
	
	document.getElementById('g-shadow').style.height=arrayPageSize[1];
	document.getElementById('g-shadow').style.width=arrayPageSize[0];
	document.getElementById("g-shadow").style.display='block';
}

function EjectUserWnd(ids)
{
	var h=-1;
	var height=parseFloat(document.getElementById(ids).style.left);

	if(height < h - 100)
	{
		document.getElementById(ids).style.left=height + 100;
	}
	else
	{
		if(height >= h - 100 && height <= h - 10)
		{
			document.getElementById(ids).style.left=height + 10;
		}
		else
		{
			if(height >= h - 10 && height <= h)
			{
				document.getElementById(ids).style.left=height + 1;
			}
		}
	}

	if(height!=h) setTimeout('EjectUserWnd("'+ids+'")', 0.5);
}

function unEjectUserWnd(ids)
{
	var h=-780;
	var height=parseFloat(document.getElementById(ids).style.left);

	if(height >= h + 100)
	{
		document.getElementById(ids).style.left=height - 100;
	}
	else
	{
		if(height >= h + 10)
		{
			document.getElementById(ids).style.left=height - 10;
		}
		else
		{
			if(height >= h)
			{
				document.getElementById(ids).style.left=height - 1;
			}
		}
	}

	if(height!=h) setTimeout('unEjectUserWnd("'+ids+'")', 0.5);

	if(height==h)
	{
		document.getElementById(ids).style.display='none';
	}
}

function getPageSize()
{
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) 
	{	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ 
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} 
	else 
	{ 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) 
	{	
		if(document.documentElement.clientWidth)
		{
			windowWidth = document.documentElement.clientWidth; 
		} 
		else 
		{
			windowWidth = self.innerWidth;
		}
		
		windowHeight = self.innerHeight;
	} 
	else if (document.documentElement && document.documentElement.clientHeight) 
	{ 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} 
	else if (document.body) 
	{ 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	} 
	else 
	{ 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth)
	{	
		pageWidth = xScroll;		
	} 
	else 
	{
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 

	return arrayPageSize;
}

function setLocation() 
{
	cg.getLocations(document.getElementById('location-id').value, CGCallback);
    return false;
}

function CGCallback(responce) 
{
	if(responce.Status.code != 200) 
	{
		alert('Ошибка поиска места. Попробуйте найти его вручную.');
		return false;
	}
	else
	{
		lat=responce.Placemark[0].Point.coordinates[1];
		lng=responce.Placemark[0].Point.coordinates[0];
		
		PlaceMarker(lat,lng,'',0);
	}
}

function PlaceMarker(lat, lng, point, p)
{
	if(point!='')
	{
		lat=point.lat();
		lng=point.lng();
	}
	
	var baseIcon = new GIcon(G_DEFAULT_ICON);
	baseIcon.iconSize = new GSize(30, 35);
	baseIcon.shadowSize = new GSize(0, 0);
    baseIcon.image = "images/marker_prud.png";
    markerOptions = { icon:baseIcon };
		
	map.clearOverlays();
	map.addOverlay(new GMarker(new GLatLng(lat,lng), markerOptions));
	map.panTo(new GLatLng(lat,lng));
	
	if(document.getElementById('lat')) document.getElementById('lat').value=lat;
	if(document.getElementById('lng')) document.getElementById('lng').value=lng;
	if(document.getElementById('zoom')) document.getElementById('zoom').value=map.getZoom();
}

function HideGoogle()
{
	document.getElementById("g-shadow").style.display='none';
	CloseUserWnd("google");
	
	return false;
}