function setCookie(name, value, flag)	{
	var COOKIE;
  	if (flag) {
  		COOKIE = name + "=" + value + "; domain=" + location.protocol + "//" + location.hostname;
  	} else {
  		COOKIE = name + "=" + value + ";";  
  	}
  	document.cookie = COOKIE;
}


var isDHTML = 0;
var isLayers = 0;
var isAll = 0;
var isID = 0;
if (document.getElementByID) {isID=1; isDHTML=1;}
else {
        if (document.all) {isAll=1; isDHTML=1;}
        else { browserVersion = parseInt(navigator.appVersion);
              if ((navigator.appName.indexOf('Netscape') !=-1) && (browserVersion == 4)) {isLayers = 1; isDHTML = 1;}
}}

function findDOMNested(objectID1, objectID2, withStyle) {
	if (withStyle == 1) {
		if (isID) {return (document.getElementById(objectID2).style); }
		else {
			if (isAll) {return (document.all[objectID2].style);}
			else {
				if (isLayers) {
					if (objectID1) {return (document.layers[objectID1]);}
					else {return (document.layers[objectID2]);}
				}
			};}
		}
	else {
		if (isID) {return (document.getElementById(objectID2));}
		else {
			if (isAll) {return (document.all[objectID2]);}
			else {
				if (isLayers)
					if (objectID1) {return (document.layers[objectID1].layers[objectID2]);}
					else {return (document.layers[objectID2]);}
			}
		};}
}

function findDOM(objectID,withStyle) {
	if (withStyle == 1)
		return document.getElementById(objectID).style;
	else
		return document.getElementById(objectID);
}

function findDOMobj(objectID) {
if (isID)  {return(document.getElementById(objectID)); }
                 else { if (isAll) {return (document.all[objectID]); }
                 else { if (isLayers) {return(document.layers[objectID]); }
};}

}


function toggleClamShellSec(objectID) {
     if (isAll || isID) {
          domStyle = findDOM(objectID, 1);
          if (domStyle.display=='block') domStyle.display='none';
          else domStyle.display='block';
     }
     else {
          destination = objectID + '.html';
          self.location = destination;
     }
     return;
}

function RemovePage(filename) {
  		if (confirm('Вы уверены, что желаете удалить страницу?'))
			location.replace(filename); 
		}

function MessageWin(str) {
  			alert(str);
}


function SubRegForm(login, pass1, pass2, email)
{
var re, msg='', sub=1;
re = /([0-9a-zA-Z\.-_]+)@([0-9a-zA-Z\.-_]+)/;

if (login.length == 0) {msg=msg+"Введите логин!\r\n"; sub = 0;}
if (pass1.length == 0) {msg=msg+"Введите пароль!\r\n"; sub = 0;}
if (pass1 != pass2) {msg=msg+"Несовадающие пароли!\r\n"; sub = 0;}
if (email.match(re) == null) {msg=msg+"Неверен или отсутствует e-mail!\r\n"; sub = 0;}

if (sub == 0) window.alert(msg);
if (sub == 1) document.reguser.submit();
}

function MM_popupMsg(msg, filename) {
	if (msg != '') {
  	if (confirm(msg))
		location.replace(filename); 
	}
	else location.replace(filename);
}


function PopupMsgForm(msg, formname) {
  	if (confirm(msg))
		 document.forms[formname].submit();	
}


function OpenImage(src, title, width, height, url) {
		//window.alert('test');
		if (width < 400) width = 400;
		if (height < 400) height = 400;
		
		if (width > (screen.availWidth-20)) width = screen.availWidth-20;
		if (height > (screen.availHeight-50)) height = screen.availHeight-50;
		//window.alert(height);
		
		var Form = "<html>" +
			"<head>" +
			"<title>"+title+"</title>" +
			"</head>\n" +
			"<body style='margin: 0px; padding:0px; background:#ffffff;'>\n" +
			"<table cellspacing='0' cellpadding='0' width='100%' height='100%' border='0'>" +
			"<tr><td style='text-align:center; vertical-align:middle;'>" +
			"<img src='"+src+"' border=0>" +
			"</td></tr>" +
			"</table>" +
			"</body>" +
			"</html>";
		var TabPanel = window.open(url,"TabPanel","dependent=1,width="+width+",height="+height+",status=yes");
		TabPanel.document.open();
        TabPanel.document.write(Form);
        TabPanel.document.close();
}

function menuSel(menu) { menu.className = "window_menu_sel"; }
function menuOut(menu) { menu.className = "window_menu_out"; }

function rowsSel(rows) { rows.className = "window_table_rows_sel"; }
function rowsOut(rows) { rows.className = "window_table_rows_out"; }

function SubAction (select_content, pageid, pagename, location, pagechild) {
	if (select_content == 'delete') {
		if (pagechild == 1) alert ('Нельзя удалить страницу «'+pagename+'».\r\nСначала удалите все вложенные страницы!');
		else MM_popupMsg('Удалить страницу «'+pagename+'» ?', location+'&del='+pageid); 
	}
	else if (select_content == 'move') {
		//if(pagechild == 1) alert ('Нельзя переместить страницу «'+pagename+'», пока она имеет вложенные страницы!');
		//else if(pagechild == 0) document.location.replace(location+'&move='+pageid);
		document.location.replace(location+'&move='+pageid);
	}
}

function SubmitSearchForm()
{
var searchwords, sub=1;
searchwords = document.sf.searchwords.value;

if (searchwords.length == 0) {sub = 0;}
if ((searchwords.length != 0) && (searchwords.length <3)) {window.alert('Длина поисковой фразы должна составлять не менее 3 символов'); sub = 0;}

if (sub == 1) document.sf.submit();
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}
function printVer(css, obj, title_tag, title, http_url, copyrights, site, links) {
	copyrights = '<a href="#">info@medvegatniki.com</a><br />Харьков ул.Загородная, 68<span class="ignorep" style="padding-left:27px;"><img src="http://medvezhatniki.rgr/templates/ru/images/ic_map.gif" style="margin-bottom:-6px;"> <a href="?page=contacts">Карта проезда</a></span><br />+38(057)';
	copyrights = copyrights + '715-22-08';
	var previewWin = window.open('', 'PrintVersion', 'dependent=1,status=0,scrollbars=yes,resizable=no,menubar=yes,location=no,titlebar=1');
	var prTop = '<html><head><title>'+title_tag+'</title><link href="'+css+'" rel="STYLESHEET" type="text/css" /><script type=\'text/javascript\' src=\''+http_url+'javascript/print.js\'></script></head><body>';
	var prTitle = '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td class="title">'+title+'</td><td class="host"><a href="'+http_url+'">'+site+'</a> ['+http_url+']</td></tr></table><div style="height:7px; width:100%; background-color:#dadbde; font-size:0px;">&nbsp;</div>';
	var prCopyrights = '<div style="height:7px; width:100%; clear:both; margin-top:10px; background-color:#dadbde; font-size:0px;">&nbsp;</div><div style="float:right; padding-top:10px; font-size:10px;"><a href=# onClick="window.print()">Распечатать</a></div><div style="padding-top:10px; font-size:10px;">'+copyrights+'</div>';
	var prBottom = '</body></html>';
	var col = findDOM(obj);
	var content = '<div id="content" style="padding:5px 0px 5px 0px;">'+col.innerHTML+'</div>';
	var links = '<div id="links" style="clear:both;"></div>';
	previewWin.document.open();
    previewWin.document.write(prTop+prTitle+content+links+prCopyrights+prBottom);
    previewWin.document.close();
}

function iconHover(ob, fold) {
		ob.style.background = 'transparent url('+fold+'images/icons_hover.gif) no-repeat 15px 25px';
}

function checkAll(ckb, form) {
	var f = (form) ? document.getElementById(form) : document.forms[0];
		
    for(j = 0; j < f.elements.length; j++) {
        var e = f.elements[j];
		if ((e.type == 'checkbox') && (e != ckb)) {
			if (ckb.checked == true) {
				e.checked = true;
				e.disabled = true;
			}
			else {
				e.checked = false;
				e.disabled = false;
			}
		}
	}
}

function visBlock(a, obj) {
	var obj = findDOM(obj, 1);
	if (obj.display == 'block') {
		obj.display = 'none';
		a.title = 'Развернуть список';
		a.firstChild.title = 'Развернуть список';
		a.firstChild.src = 'admin/images/plus.png';
	}
	else {
		obj.display = 'block';
		a.title = 'Свернуть список';
		a.firstChild.title = 'Свернуть список';
		a.firstChild.src = 'admin/images/minus.png';
	}
}

function checkValidForm(f, subm) {
	indOf();
	var types = new Array('text', 'password', 'checkbox', 'radio', 'file');
	var tags = new Array('INPUT', 'SELECT', 'TEXTAREA');
	var s = true;
	
	if (!f)
		var f = document.forms[0];
		
	// Цикл по всем элементам текущей формы
       for(j = 0; j < f.elements.length; j++) {
           var e = f.elements[j];  //текущий элемент
		if (e.type != 'file') e.style.borderColor = "#5895CF";
		if (e.disabled == true) continue;
		if ((tags.indexOf(e.tagName) != -1) || (types.indexOf(e.type) != -1)) {
			var valid = true;
			var patt = e.getAttribute("patt");
			var req = e.getAttribute("req");
			if ((patt) && (e.value.length != 0)) {
				switch(patt) {
					case "mail":{ 
						valid = validateEmail(e.value);
						break;             
					}
					case "int":{
						valid = validateRegExp(e.value, '^[0-9]+$');
						break;
					}
					case "float":{
						valid = validateRegExp(e.value, '^[0-9]+(\\.)?[0-9]*$');
						break;
					}
					case "date(d.m.Y)":{
						valid = validateRegExp(e.value, '^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$');
						break;
					}
					case "url": {
						valid = validateRegExp(e.value, '^(http:\/\/|https:\/\/)?([^\.\/]+\.)*([a-zA-Z0-9])([a-zA-Z0-9-]*)\.([a-zA-Z]{2,4})(\/.*)?$');
					}
				}
			}
			if (req) {
				if (e.value.length == 0) valid = false;
			}
			if (valid == false) {
				s = false;
				if (e.type != 'file') e.style.borderColor = "#cc0000";
				
			}
		}
	}
	if (s == false)
			window.alert ("Некорректно заполнены поля!");
	return s;
}
	
	function validateEmail(email)
	{
		var splitted = email.match("^(.+)@(.+)$");
		if(splitted == null) return false;
		if(splitted[1] != null )
		{
		  var regexp_user=/^\"?[\w-_\.]*\"?$/;
		  if(splitted[1].match(regexp_user) == null) return false;
		}
		if(splitted[2] != null)
		{
		  var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
		  if(splitted[2].match(regexp_domain) == null) 
		  {
			var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			if(splitted[2].match(regexp_ip) == null) return false;
		  }// if
		  return true;
		}
	return false;
	}
	
	function validateRegExp(val, regexp) {
		if (val.match(regexp) == null) return false;
		else return true;
	}
	
function indOf() {
	if (typeof Array.prototype.indexOf == "undefined") {
	  Array.prototype.indexOf = function(value) {
		for (var i = 0; i < this.length; i++)
		  if (this[i] == value)
			return i;
	
		return -1;
	  }
	}
}

function partnersFilterSubmit(f) {
	f.action = 'http://' + document.domain + '/?page=partners';
	
	cn = document.getElementById('cn').value;
	ct = document.getElementById('ct').value;
	s = document.getElementById('s').value;
	
	if (cn.length != 0)
		f.action += '&cn=' + cn;
		
	if (ct.length != 0)
		f.action += '&ct=' + ct;
		
	if (s.length != 0)
		f.action += '&s=' + s;
}
