$(function() {
	$('.eventCell img[width!=16]').wrap('<table class="baget"><tr><td></td></tr></table>');
});



// -------------------------------------------------------------------------------
	// ---  открыть окно с изображением
	var wrImageWindow;
	function rOpenImageWindow(src,width,height){ 
		wWidth = (width)?width+4:470;
		wHeight = (height)?height+4:470;
		wrImageWindow=window.open("","blankImageWindow", 'status=no,scrollbars=no,resizable=yes,width='+(wWidth)+',height='+(wHeight)+'');
			wrImageWindow.document.write("<html><head><title>Изображение</title>");			
			wrImageWindow.document.write("<style>.visible{display:block;} .hide{display:none;} div,a{font:10px verdana,tahoma,sans-serif;text-decoration:none;}</style>");


			wrImageWindow.document.write("<script>function printshow(over){document.getElementById('menu').setAttribute(classFix,(over)?'visible':'hide');}</script>");


			wrImageWindow.document.write("<body class=popupWnd topmargin=0 marginheight=0 leftmargin=0 marginwidth=0 bgcolor=white text=black link=black alink=black vlink=black>");

			wrImageWindow.document.write("<script>var classFix = (document.body && document.body.getAttribute('className'))?'className':'class';</script>");

			wrImageWindow.document.write("<table border=0 cellspacing=0 cellpadding=0 width=100% height=100%><tr><td align=center onmouseover='printshow(true);' onmouseout='printshow(false);'>");
			wrImageWindow.document.write("<table border=0 cellspacing=1 cellpadding=1 bgcolor=#333333><tr><td bgcolor=white>");
			wrImageWindow.document.write("<div id='menu' class='hide' style='position:absolute;z-index:1;background:white;top:2px;left:2px;padding:5px;border:#333333 1px solid;'>");
				wrImageWindow.document.write("[<a href='javascript:;' onclick='printshow(false);self.print();' title='распечатать'>распечатать</a>] &nbsp;");
				wrImageWindow.document.write("[<a href='javascript:;' onclick='self.close();' title='закрыть окно'>закрыть</a>]");
			wrImageWindow.document.write("</div>");
			wrImageWindow.document.write("<img src='"+src+"' width='"+width+"' height='"+height+"' border=0>");
			wrImageWindow.document.write("</td></tr></table>");
			wrImageWindow.document.write("</td></tr></table>");
			wrImageWindow.document.write("</body></html>");
			wrImageWindow.document.close();
		wrImageWindow.resizeTo(wWidth+30,wHeight+40);
		wrImageWindow.focus();
	}
// -------------------------------------------------------------------------------






var imageWindow; 
function ImgWin(src,width,height,print,title){ 
	var scroll='no';
	if (print==true)
	{	var w=20;	var h=20;	}
	else{ var w=20;	var h=0;		}

	if( height>600 || width>800 ){
		h = ( height>600 ) ? 600 : h+height;
		w = ( width>800 ) ? 800 : w+width;
		scroll = 'yes';
	}else{
		h = h + height;
		w = w + width;
		scroll = 'no';
	}

	title = (!title) ? "Иди и смотри" : "Иди и смотри :: " + title;

	if(imageWindow)
		imageWindow.close(); 
	imageWindow=window.open("","blankImageWindow", 'status=no,scrollbars='+scroll+',resizable=0,width='+w+',height='+h+''); 
	imageWindow.document.write("<html><head><title>"+title+"</title><link rel='stylesheet' type='text/css' href='./inc/normal.css'></head>"); 
	imageWindow.document.write("<body topmargin=0 marginheight=0 leftmargin=0 marginwidth=0 bgcolor=white text=black link=black alink=black vlink=black>"); 
	imageWindow.document.write("<center>"); 
	imageWindow.document.write("<table border=0 cellspacing=0 cellpadding=0 bgcolor=#FFFFFF>"); 
	imageWindow.document.write("<tr><td align=center bgcolor=white>"); 
	imageWindow.document.write("<a href='javascript:self.close();' title='закрыть окно'><img src='"+src+"' width="+width+" height="+height+" border=0></a>"); 
	if (print==true)
		imageWindow.document.write("</td></tr><tr><td align='center'><a class='cat' href='javascript:self.print();'>Распечатать</a></td></tr>"); 
	else
		imageWindow.document.write("</td></tr>"); 
	imageWindow.document.write("</table>"); 
	imageWindow.document.write("</body></html>"); 
	imageWindow.document.close(); 
	imageWindow.focus(); 
} 


var docWindow; 
function DocWin(src){ 
	var scroll='yes';
	var xAll=document.body.clientWidth;
	var yAll=document.body.clientHeight;
	var w=710; 
	var h=560;
	var l=(xAll-w)/2;
	var t=(yAll-h)/2;

	//if(docWindow)	docWindow.close();
	docWindow=window.open(src,"blankDocWindow", 'status=no,scrollbars='+scroll+',resizable=0,top='+t+',left='+l+',width='+w+',height='+h+''); 
	docWindow.focus(); 
} 


var popWindow; 
function PopupWin(src){ 
	var scroll='yes';
	var w=800; 
	var h=600;

	popWindow=window.open(src,"blankPopupWin",'status=no,scrollbars='+scroll+',resizable=1,width='+w+',height='+h+'');
	popWindow.focus();
} 

var shWindow; 
function ShowWin(src){ 
	var scroll='yes';
	var w=500; 
	var h=400;

	shWindow=window.open("","blankDocWindow", 'status=no,scrollbars='+scroll+',resizable=0,width='+w+',height='+h+''); 
	shWindow.document.location="./control/show.php?id="+src;
	shWindow.focus(); 
} 

function GoToSite(src)
{
	window.open(src,"general"); 
	self.close();
}

function mailTo(oHref,atString) {
	if(atString){
		oHref.oldHref = oHref.href;
		oHref.href = oHref.href.replace(atString,"@");
	}else{
		oHref.href = oHref.oldHref;
	}
		return oHref.href;
}

var bookmarkurl="http://www.strblik.ru/"
var bookmarktitle="СтарБлик - Жалюзи, рулонные шторы и рольставни"

function addbookmark()
{
	if (document.all) window.external.AddFavorite(bookmarkurl,bookmarktitle)
}

/*function myHomePage()
{
	this.style.behavior='url(#default#homepage)';
	document.setHomePage(bookmarkurl);
}
*/
// ........................................................
// Конвертор языков
	function convertInputValue(idInput,idOut){
		var oInput = (idInput.tagName=="INPUT")?idInput:document.getElementById(idInput);
		var oOut = (idOut.tagName=="INPUT")?idOut:document.getElementById(idOut);
		var match  = new Array(" ","А","Б","В","Г","Д","Е","Ё","Ж","З","И","Й","К","Л","М","Н","О","П","Р","С","Т","У","Ф","Х","Ц","Ч","Ш","Щ","Ъ","Ы","Ь","Э","Ю","Я","а","б","в","г","д","е","ё","ж","з","и","й","к","л","м","н","о","п","р","с","т","у","ф","х","ц","ч","ш","щ","ъ","ы","ь","э","ю","я");
		var replace= new Array("_","A","B","V","G","D","E","E","Zh","Z","I","J","K","L","M","N","O","P","R","S","T","U","F","H","Tc","Ch","Sh","Sch","","Y","","E","Ju","Ya","a","b","v","g","d","e","e","zh","z","i","y","k","l","m","n","o","p","r","s","t","u","f","h","tc","ch","sh","sch","","y","","e","ju","ya");
		var value = oInput.value;
		for(var i=0; i<match.length;i++){
			var re = eval("/"+match[i]+"/g");
			value	= value.replace(re,replace[i]);
		}
		value	= value.replace(/(_+)/g,"_");
		value	= value.replace(/([^0-9a-zA-Z_])/g,"");
		oOut.value = value;
	}

var colorArray=null;
function WinPrice(action,id){
	if(action != 'del'){
		var form_color = document.getElementById("form_color");
		var price = 0;
		if(action == 'upd'){
			var priceid = document.getElementById("price"+id);
			var colorid = document.getElementById("color"+id);
			price = priceid.innerText;
			color = colorid.bgColor;
		}
		var eprice = document.getElementById("price");
		var ecolor = document.getElementById("color");
		form_color.style.display = '';
		eprice.value = price;
		/////////////////////////////////////////////////
		for (i=0;i<colorArray.length;i++){
			ecolor.options[i] = new Option ("", colorArray[i]);
			ecolor.options[i].style.background=colorArray[i];
			if(action == 'upd' && color.toUpperCase() == ecolor.options[i].value){
				ecolor.options[i].selected = true;
				ecolor.selectedIndex = i;
			}
		}
	}

	var color_id = document.getElementById("color_id");
	color_id.value = id;
}

// ........................................................
// Проверка введенных данных
	function checkInputValue(idInput,type){
		var classFix = (document.all)?"className":"class";
		var oInput = document.getElementById(idInput);
		var iclass = oInput.getAttribute(classFix);
		var pos = iclass.indexOf("errorfield");
		if(pos>=0){ 
			if(pos==0) oInput.removeAttribute(classFix);
			else			 oInput.setAttribute(classFix,iclass.substr(0,pos-1));
		}
		switch(type){
			case 0: // положительное целое
				if(oInput.value != oInput.value*1 || !(oInput.value>=0)){
					alert("Ошибка заполнения:\nВведите положительное целое число!");
					oInput.value = 0;
					oInput.focus();
					if(oInput.getAttribute(classFix).length>0)	oInput.setAttribute(classFix,oInput.getAttribute(classFix)+" errorfield");
					else																				oInput.setAttribute(classFix,"errorfield");
					return false;
				}else{
					oInput.value = Math.abs(Math.round(oInput.value));
					return true;
				}
			break;
			case 1: // email
				var re_mail = /([\w\.\-_]+@[\w\.\-_]+)/;
				if(oInput.value.match(re_mail)!=null){
					return true;
				}else{
					alert("Ошибка заполнения:\nВведите email адрес!");
					oInput.focus();
					if(oInput.getAttribute(classFix).length>0)	oInput.setAttribute(classFix,oInput.getAttribute(classFix)+" errorfield");
					else																				oInput.setAttribute(classFix,"errorfield");
					return false;
				}
			break;
			case 2: // пароли
				var oReInput = document.getElementById("re"+idInput);
				iclass = oReInput.getAttribute(classFix);
				var pos = iclass.indexOf("errorfield");
				if(pos>=0){ 
					if(pos==0) oReInput.removeAttribute(classFix);
					else			 oReInput.setAttribute(classFix,iclass.substr(0,pos-1));
				}
				if(oInput.value.length>=3 && oReInput.value==oInput.value){
					return true;
				}else{
					alert("Ошибка заполнения:\nВведенные пароли не совпадают!");
					oInput.value = ""; 
					oReInput.value = "";
					oInput.focus();
					if(oInput.getAttribute(classFix).length>0)	oInput.setAttribute(classFix,oInput.getAttribute(classFix)+" errorfield");
					else																				oInput.setAttribute(classFix,"errorfield");
					if(oReInput.getAttribute(classFix).length>0)	oReInput.setAttribute(classFix,oReInput.getAttribute(classFix)+" errorfield");
					else																				oReInput.setAttribute(classFix,"errorfield");
					return false;
				}
			break;
			default: // не пустое
				if(oInput.value.length>=3){
					return true;
				}else{
					alert("Ошибка заполнения:\nВведите текст!");
					oInput.focus();
					if(oInput.getAttribute(classFix).length>0)	oInput.setAttribute(classFix,oInput.getAttribute(classFix)+" errorfield");
					else																				oInput.setAttribute(classFix,"errorfield");
					return false;
				}
			break;
		}
	}

//===========================================
//  Подсветка поиска
//===========================================
function searchWithinNode(node, te, len){
//===========================================
	var pos, skip, spannode, middlebit, endbit, middleclone;
	skip=0;
	if( node.nodeType==3 ){
		pos=node.data.toUpperCase().indexOf(te);
		if(pos>=0){
			spannode=document.createElement("SPAN");
			spannode.style.backgroundColor="#E93E24";
			spannode.style.color="white";
			middlebit=node.splitText(pos);
			endbit=middlebit.splitText(len);
			middleclone=middlebit.cloneNode(true);
			spannode.appendChild(middleclone);
			middlebit.parentNode.replaceChild(spannode,middlebit);
			skip=1;
		}
	}else if( node.nodeType==1&& node.childNodes && node.tagName.toUpperCase()!="SCRIPT" && node.tagName.toUpperCase!="STYLE"){
		for (var child=0; child < node.childNodes.length; ++child){
			child=child+searchWithinNode(node.childNodes[child], te, len);
		}
	}
	return skip;
}

function cnter(MaxLen,idText) {
	Otext = document.getElementById(idText);
	if (Otext){
		if(Otext.value.length > MaxLen) alert("Извините, это поле не может быть длиннее "+MaxLen+" символов");
	}
}

function CheckForm(form) {
    var Message = "Необходимо заполнить поля \n\n";
    var focus = null;
    var result = true;
    for (var i = 0; i < form.elements.length; i++) {
        if (form.elements[i].getAttribute("require") == "true") {
            var doc = document.getElementById(form.elements[i].name);
            var match = "";
            if(null != form.elements[i].getAttribute("match")) {
                match = form.elements[i].getAttribute("match");
            }
            if(!CheckValue(form.elements[i].value, match)) {
                if(focus == null) {
                      form.elements[i].focus();
                      focus = true;
                }
                Message += form.elements[i].getAttribute("info") + "\n";
                result = false;
           }
       }
    }
    if(!result) {
        alert(Message);
    }
    return result;
}


function CheckValue(Value, Match) {
    switch(Match) {
        case "email":
            if(!(Value.length>0&&Value.match(new RegExp("^[a-z0-9_]+@([a-z0-9_]+\.)+[a-z]+$","i")))){
                return false;
            }
            return true;
        break;

        case "kpp":
          if(Value.length!=9) return false;
          if(!Value.match(new RegExp("^[0-9]+$","i"))) {
              return false;
          }
          return true;
        break;

        case "zip":
          if(Value.length!=6) return false;
          if(!Value.match(new RegExp("^[0-9]+$","i"))) {
              return false;
          }
          return true;
        break;

        case "rs":
            if(Value.length < 20) {
                if(Value.match(new RegExp("^[0-9]+$", "i"))) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        break;

        case "phone":
            if(!Value.match(new RegExp("^[0-9 \(\)\+\-]+$","i"))) {
                return false;
            }
        return true
        break;

        case "digit":
            if(!Value.match(new RegExp("^[0-9]+$","i"))) {
                return false;
            } else {
                return true;
            }
        break;

        case "inn":
            var checkValueArray = Value.split("");
            if(checkValueArray.length==10) {
                cn = chekSum(checkValueArray, [2,4,10,3,5,9,4,6,8,0]);
                if(cn!=checkValueArray[9]){
                    return false;
                }
            } else if(checkValueArray.length==12) {
                cn1 = chekSum(checkValueArray, [7,2,4,10,3,5,9,4,6,8,0]);
                cn2 = chekSum(checkValueArray, [3,7,2,4,10,3,5,9,4,6,8,0]);
                if(cn1!=checkValueArray[10]&&cn2!=checkValueArray[11]){
                    return false;
                }
            } else {
              return false;
            }
            return true;
        break;

        default:
            if(Value == "") {
                return false;
            } else {
                return true;
            }
        break;
    }
}

function chekSum(checkValue,map){
  out = 0;
  for(c=0;c<map.length;c++){out+=map[c]*checkValue[c];}
  return ((cn=out%11)>9)?cn%10:cn;
}
//---------------------------------------------


