// Variables for cookies
var expDays = 30;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function attachEvents(event, func) {
 // Attach event
  if(window.addEventListener)
    window.addEventListener(event, func, false);
  else if(window.attachEvent)
    window.attachEvent("on" + event, func);
  else
    window.onload = func;
}


function adjust() {
	var screenW = clientWidth();
	var s = window.location.pathname;
	alb = s.search(/album.asp/);
	if(alb != -1){
		loadPhoto(0);
		resize_photo();
	}
	else
	{
		var menuWidth = 150;
		var margin = 30;
		var W = (screenW>900?900-menuWidth:screenW-menuWidth)-2*margin;
		document.getElementById("maindiv").style.width = W;
		document.getElementById("maindiv").style.left = (screenW+menuWidth-W)/2;

	}
	if(screenW >772) 
		document.getElementById("horfon").style.width = screenW -772;	
	document.getElementById("maindiv").style.visibility = "visible";	
}

function selectlang(ln)
{
	var loc = new String (window.location);
	var rg1 = /(\?)/;
	var rg = /(&lang=ru)|(&lang=lt)|(&lang=en)|(\?lang=ru)|(\?lang=lt)|(\?lang=en)/ ;
	var loc1 ;
	loc1= new String (loc.split(rg));

	if(loc1.search(rg1) == -1)
		window.open(loc1 + "?lang=" + ln, "_self");
	else
		window.open(loc1 + "&lang=" + ln, "_self");	
}

function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)    
	endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));
	}
	
function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
	var j = i + alen;    
	if (document.cookie.substring(i, j) == arg)      
	return getCookieVal (j);    
	i = document.cookie.indexOf(" ", i) + 1;    
	if (i == 0) break;   
	}  
	return null;
}
function SetCookie (name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 1);  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}



// function's start for album.asp


function clientHeight(){
	return document.all? document.body.clientHeight : window.innerHeight;
}
function clientWidth(){
	return document.all? document.body.clientWidth : window.innerWidth;
}
function resize_photo(){
	var ratio = f.width / f.height;	
	var h = clientHeight();
	var w = clientWidth();
	var img = document.getElementById("photo");
	img.width	= f.width;
	img.height  = f.height;
	
	if ((img.width +200 > w) | (h < img.height+200)){
		img.height = h-220;
		img.width = img.height * ratio;
	}		
	if(img.width > w-250){
		img.width = w-270;
		img.height = img.width / ratio;
	}
}

// function's end for album.asp



function window_onbeforeunload(count) {
	var id,el,str=""
	for(i=1;i<count;i++){
		id = "Lb" + i + "s";
		el = document.getElementById(id);
		if(el){
			if(el.style.display == ""){
				str = str + "" + "Lb" + i + "i#";
			}
		}
	}
	DeleteCookie('menuId');	
	if(str != ""){
		SetCookie('menuId', str);
	}
	
	loading();
}

function loading(){
var screenW = clientWidth();
var w = screenW>200?screenW/2-100:0;
var h=300;	
var o=document.getElementById("loadingMsg");	
o.style.top=h;	
o.style.left=w;		
o.style.visibility = "visible";
}

/*function SetMenuState(){
	var cookStr = new String(GetCookie('menuId'));
	var itm="";
	for(i=0;i<cookStr.length;i++){
		var ch = cookStr.charAt(i)
		if (ch != "#")
			itm = itm + ch
		else{
			doOutline(itm); 
			itm = "";
		}
	}
}

function doOutline(id){
  var targetId, srcElement, targetElement;
  srcElement = document.getElementById(id)
  if (srcElement.className == LevelClass){
     srcElement = srcElement.id;
     srcElement = srcElement.substr(0, srcElement.length-1);
     targetId = srcElement + "s";
     srcElement = srcElement + "i";
     srcElement = document.getElementById(srcElement);
     targetElement = document.getElementById(targetId);
     if (targetElement.style.display == "none"){
        targetElement.style.display = "";
        if (srcElement.className == LevelClass)
           srcElement.src = img2.src;
     }
     else{
       targetElement.style.display = "none";
       if (srcElement.className == LevelClass)
          srcElement.src = img1.src;
     }
  }
}
*/


// GUESTBOOK.asp

function EMail_Validator(email_addr) {
	var re, re2;
	re = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)/ 
	re2 = /^.+\@\[?(\w|[-.])+\.[a-zA-Z]{2,3}|[0-9]{1,3}\]?$/
	if ( (email_addr.search(re) == -1) &   (email_addr.search(re2) != -1) )
		return(true);
	return(false);
}
function Http_Validator(strHttp) {
	var r = new RegExp("http://");
	if(!r.test(strHttp)){
		alert("Please, add prefix 'http://' before your address");
		return(false); 		
    } 
	var rx = new RegExp("http://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?", "ig");
	if (rx.test(strHttp))
	    return(true);
	    
	alert("Not valid http link!");
    return(false);
}
function Validate(vemail, link) {
	var mail = vemail.value.toString();
	if (!EMail_Validator(mail)){
		alert("Некорректный E-Mail адрес!");
		return false;
	}
	var shttp = link.value.toString();
	if(shttp == ""){
		return  true;
	}
	if (Http_Validator(shttp))
		return true;
	return false;
}

