function setSTEP(n){
	$('#frame').css("width", $('#big_pic').width());
	$('#frame').html($('#frame'+n).html());
}

function getCookie(name) {
	var prefix = name + "=";
	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex == -1) return null;
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length;
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function setCookie(name, value, expires) {
	if (expires == 'permanent'){
		expires = 'Fri, 25 Dec 2099 23:59:59 GMT';
	} else {
		((expires) ? "; expires=" + expires.toGMTString() : "");
	}
	var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires : "") + "; path=/";
	document.cookie = curCookie;
}

function delCookie(name)
{
	document.cookie = name + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT; path=/";
}

function logout(){
	delCookie('login');
	delCookie('password');
	location.href="/";
}
function CookiesEnabled(){
	if (!document.cookie){
		aleft('Для работы полноценного просмотра сайта необходимо разрешить использование cookie в Вашем браузере.');
		return false;
	} else {
		return true;
	}
}
//Chek forms

function validateFormReg(){
  var form_object = document.forms.registerForm;
    if(form_object.elements.login.value == ''|| form_object.elements.login.value=='Логин'){
    	alert('Вы не ввели логин!');
    	return false;
 } 
 	else if(form_object.elements.password.value == ''|| form_object.elements.password.value=='Пароль'){
    	alert('Вы не ввели свой пароль!');
    	return false;
 } 
 	else if(form_object.elements.email.value == ''|| form_object.elements.email.value=='E-mail'){
    	alert('Вы не ввели свой E-mail!');
    	return false;
 } 
 	if (!isEmail(form_object.elements.email.value)) {
    	form_object.elements.email.focus();
    	form_object.elements.email.select();
    	return false;
   }
  return true;
}

function validateFormRem(){
  var form_object = document.forms.rememberform;
    if(form_object.elements.email.value == ''|| form_object.elements.email.value=='E-mail'){
    alert('Вы не ввели свой E-mail!');
    return false;
  }
  if (!isEmail(form_object.elements.email.value)) {
    form_object.elements.email.focus();
    form_object.elements.email.select();
    return false;
   }	 
  return true;
}

function validateFormLogin(){
  var form_object = document.forms.loginForm;
    if(form_object.elements.login.value == ''|| form_object.elements.login.value=='Логин'){
    alert('Вы не ввели логин!');
    return false;
  } else if(form_object.elements.password.value == ''|| form_object.elements.password.value=='Пароль'){
    alert('Вы не ввели свой пароль!');
    return false;
  }
  return true;
}

function isEmail(strEmail) {
	var letters="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
	var numbers="0123456789";
	var name="";
	var server="";
	var strings=new Array();

	if (strEmail=="") {
		alert("Введите E-mail, пожалуйста");
		return false;
	}
	strings=strEmail.split("@");
	if (strings.length==1) {
		alert("E-Mail должен содержать символ @");
		return false;
	} else if (strings.length==2) {
		name=strings[0];
		server=strings[1];
	} else {
		alert("E-Mail должен содержать только один символ @");
		return false;
	}
	
	if (!isValid(name,letters+numbers+"._-")) {
		alert("Incorrect E-Mail address");
		return false;
	}
	
	strings=server.split(".");
	if (strings.length<2) {
		alert("Имя домена второго уровня должно присутствовать в E-Mail");
		return false;
	}

	for (i=0;i<strings.length;i++) {
		if (!isValid(strings[i],letters+numbers)) {
			alert("Неверные символы в адресе E-Mail");
			return false;
		}
	}	
	return true;	
}
function isValid(what,chars) {
	if (what=="") {
		return false;
	}
	bool1=false;
	for (count1=0; (count1<what.length)&&(!bool1);count1++) {
		bool2=false;
		for (count2=0; (count2<chars.length)&&(!bool2);count2++) {
			if (what.charCodeAt(count1)==chars.charCodeAt(count2)) {
				bool2=true;
			}
		}
		if (!bool2) {
			bool1=true;
		}
	}
	return !bool1;
}

var win; 
function ImagePopUp(url){  
    if (url){ 
        if (win) win.close(); 
        win = window.open('about:blank', '', 'width=300,height=300,status=0,toolbar=0,location=0,scrollbars=0,menubar=0,resizable=0');
        win.document.write('<IMG style="DISPLAY: none" src="'+url+'" onload=window.opener.resize()>');
        win.document.write('<TABLE id=maintable style="DISPLAY: block" height=300 width=300>');
        win.document.write('<TR><TD style="FONT: 10pt Tahoma" vAlign=center align=middle>Подождите, идет загрузка изображения...</TD></TR>');
        win.document.write('</TABLE>'); 
    } 
} 

function resize(){ 
    win.document.images[0].style.display = 'block'; 
    win.document.all.maintable.style.display = 'none'; 
    var w = win.document.images[0].width+0; 
    var h = win.document.images[0].height+0; 
    win.resizeTo(w, h); 
    win.focus(); 
}


function showImage(name) {
  params = "top=50, left=200, width=470, height=500, Status=no, toolbar=no, menubar=no, location=no, directories=no, scrollbars=no";
  popupWin = window.open("","MaratKa",params);
  popupWin.document.open();
  popupWin.document.write("<html><head><title></title></head>");
  popupWin.document.write("<sc"+"ript language = JavaScript>");
  popupWin.document.write("function closeWindow(){");
  popupWin.document.write("parent.window.close() };");
  popupWin.document.write("</sc"+"ript>");
  popupWin.document.write("<link rel='stylesheet' href='/Sites/maratka_ru/Cache/popup.css' media='all'>");
  popupWin.document.write("<body bgcolor='#FFFFFF' leftmargin='0' topmargin='0' marginwidth='0' marginheight='0' style='text-align: center;'>"); 
  popupWin.document.write("<div align='center'><table border='0' cellspacing='0' cellpadding='0' style='padding: 0 0 0 4'><tr><td>");
  popupWin.document.write("<div class='logo_violet_orange'><a title='Закрыть окно' href='' onClick='closeWindow()'>закрыть окно</a></div>");
  popupWin.document.write("</td></tr><tr valign=top><td>"); 
  popupWin.document.write("<div class='btb-dsl-tc'><div class='btb-dsr-tc'><div class='btb-dsl-bc'><div class='btb-dsr-bc'><div class='wrap'>");
  popupWin.document.write("<a title='Закрыть окно' href='' onClick='closeWindow()'><img src='"+name+"' alt='Закрыть окно' border='0' hspace='20' vspace='20';></a>");
  popupWin.document.write("</div></div></div></div></div></td></tr></table></div>");
  popupWin.document.write("<BR style='LINE-HEIGHT: 20px'></body></html>");
  popupWin.document.close();
  popupWin.focus();
};

function showImageCat(name) {
  params = "top=50, left=200, width=390, height=415, Status=no, toolbar=no, menubar=no, location=no, directories=no, scrollbars=no";
  popupWin = window.open("","MaratKa",params);
  popupWin.document.open();
  popupWin.document.write("<html><head><title></title></head>");
  popupWin.document.write("<sc"+"ript language = JavaScript>");
  popupWin.document.write("function closeWindow(){");
  popupWin.document.write("parent.window.close() };");
  popupWin.document.write("</sc"+"ript>");
  popupWin.document.write("<link rel='stylesheet' href='/Sites/maratka_ru/Cache/popup.css' media='all'>");
  popupWin.document.write("<body bgcolor='#FFFFFF' leftmargin='0' topmargin='0' marginwidth='0' marginheight='0' style='text-align: center;'>");
  popupWin.document.write("<div align='center'><table border='0' cellspacing='0' cellpadding='0' style='padding: 0 0 0 4'><tr><td>");
  popupWin.document.write("<div class='logo_material'><a title='Закрыть окно' href='' onClick='closeWindow()'>закрыть окно</a></div>");
  popupWin.document.write("</td></tr><tr><td>"); 
  popupWin.document.write("<div class='btb-dsl-tc'><div class='btb-dsr-tc'><div class='btb-dsl-bc'><div class='btb-dsr-bc'><div class='wrap'>");
  popupWin.document.write("<a title='Закрыть окно' href='' onClick='closeWindow()'><img src='"+name+"' alt='Закрыть окно' border='0' hspace='20' vspace='20';></a>");
  popupWin.document.write("</div></div></div></div></div></td></tr></table></div>");
  popupWin.document.write("<BR style='LINE-HEIGHT: 20px'></body></html>");
  popupWin.document.close();
  popupWin.focus();
};

var windowopened = false;
var windowopenedid = '';

function ShowSpecialWindow(id){
	var obj = document.getElementById(id);
	obj.style.display='block';
	
	ResizeDark('dark');
	document.getElementById('dark').style.display='block';

	RepositionSpecialWindow(id);
	windowopened = true;
	windowopenedid = id;
}

function HideSpecialWindow(id){
	document.getElementById(id).style.display='none';
	document.getElementById('dark').style.display='none';
	windowopened = false;
}

function RepositionSpecialWindow(id){
	var obj = document.getElementById(id);

   	if($.browser.opera) {
   		windowHeight = window.innerHeight;
   		windowWidth = window.innerWidth;
   	}else{
   		windowHeight = $(window).height();
   		windowWidth = $(window).width();
   	};

	scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
	scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;

	obj.style.top = scrollTop + windowHeight/2 - $('#'+id).height()/2;
	obj.style.left = scrollLeft + windowWidth/2 - $('#'+id).width()/2;
}

function ResizeDark(id){
	$('#'+id).css({
		'height':$(document).height(),
		'width':$(window).width()
	});
}

$(document).ready(function(){
	$(window).bind('resize', function() {
		if (windowopenedid) {
			ResizeDark('dark');
			RepositionSpecialWindow(windowopenedid);
		}
	});
	$(window).bind('scroll', function() {
		if (windowopenedid) RepositionSpecialWindow(windowopenedid);
	});
});

