
var is_visible = 0;   
var container  = null;


function addOnLoad(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


setTimeout("chatRefresher()", 5*60000);
function chatRefresher() { 
	var myAjax = new Ajax.Updater('chat_box_list', '/chat', {asynchronous:true, evalScripts:true, parameters:'', requestHeaders:['X-Update', 'chat_box_list']}); 
    setTimeout("chatRefresher()", 5*60000);
}


function countChars() {  
	if($('ModChatText')) {
		str_max = 200;
		len = $('ModChatText').value.length;
		if(len >= str_max)	$('ModChatText').value = $('ModChatText').value.substr(0, str_max);
		$('ModChatCounter').value = str_max-len < 0 ? 0:str_max-len;
	}
}


function showProcessBox() {
	Effect.toggle('loading_box','appear');
}

function getGalleryImage(url) {   
	if(!is_visible) {
		is_visible = 1;  
		$('dropdown_gallery').innerHTML = '';  
		$('dropdown_gallery').style.display = 'block';
		//Effect.toggle('dropdown_gallery','BLIND');
	}  
	myRequest('dropdown_gallery', url);
}    
function closeGallery() {   
	//Effect.toggle('dropdown_gallery','BLIND');
	is_visible = 0;  
	$('dropdown_gallery').hide();
}  


function myRequest(cont, url) { 
	container  = cont;
 	var url    = url; 
	var pars   = '';  
	var myAjax = new Ajax.Request( url, { method: 'post', parameters: pars, onComplete: showTemplateResponse, asynchronous: 'true' });
}
function myRequest2(cont, url) { 
	container  = cont;
	if($(container).style.display == 'block') {
		$(container).style.display = 'none';
	} else {
		$(container).style.display = 'block';
	 	var url    = url; 
		var pars   = '';  
		var myAjax = new Ajax.Request( url, { method: 'post', parameters: pars, onComplete: showTemplateResponse, asynchronous: 'true' });
	}
}
function showTemplateResponse(originalRequest) {  
	//alert(originalRequest.responseText);  
	if($(container)) {
		$(container).innerHTML = originalRequest.responseText;  
	}
}   


function bindSession(sess) {   
 	var url    = '/session/'+sess; 
	var pars   = '';  
	var myAjax = new Ajax.Request( url, { method: 'post', parameters: pars, asynchronous: 'true' });
	setTimeout("bindSession('"+sess+"')", 60*1000);	
}


function showLogging() {
} 

function showSearching() {
}    


function changeDisplay(id) {
	if($(id)) {
		if($(id).style.display == 'none')
				$(id).style.display = ''; 
		else	$(id).style.display = 'none'; 
	}
	
}


function displayContainer(id, item) {    
	if($(id)) {
		if($(id).style.display == 'none') {
			$(id).style.display = 'block'; 
			item.innerHTML = 'Paslėpti';
		} else {
			$(id).style.display = 'none'; 
			item.innerHTML = 'Kas tai?';
		}
	}
}



function getWeather(){                                                
    myRequest('weathers_box_container', '/weathers/'+($('ModWeatherCity').value));
}   

function getHoroscope(){                                                
    myRequest('horoscope_box_container', '/horoscope/'+($('ModHoroscopeSign').value));
}   


String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


function trim(str) {
	var s = new String(str);
    return s.trim();
}

function select_cal_day() {       
	if($('tmp_selected_date') && $F('tmp_selected_date')) {
		dat = $F('tmp_selected_date');
		lin = $A($('cal_days_container').getElementsByTagName('A'));
		
		lin.each(function(s) {
			mat = s.href.match(/(\d+)\/(\d+)\/(\d+)/);
			if(mat[1]+mat[2]+mat[3] == dat) {
				s.style.background = 'red';
				s.style.color = 'white';
			}
		});
	}  
} 

function periodicalRequest(request_link, tm) {   
	new Ajax.Request(request_link, {method: 'get', asynchronous : true}); 
	setTimeout("periodicalRequest('"+request_link+"', "+tm+")", tm);
}

function emailCheck (emailStr) {

    var emailPat=/^(.+)@(.+)$/
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
    var validChars="\[^\\s" + specialChars + "\]"
    var quotedUser="(\"[^\"]*\")"
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
    var atom=validChars + '+'
    var word="(" + atom + "|" + quotedUser + ")"
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

    var matchArray=emailStr.match(emailPat)

    if (matchArray==null) {
        return false
    }

    var user=matchArray[1]
    var domain=matchArray[2]
    if (user.match(userPat)==null) {
        return false
    }

    var IPArray=domain.match(ipDomainPat)
    if (IPArray!=null) {
          for (var i=1;i<=4;i++) {
            if (IPArray[i]>255) {
                return false
            }
        }
        return true
    }

    var domainArray=domain.match(domainPat)
    if (domainArray==null) {
        return false
    }

    var atomPat=new RegExp(atom,"g")
    var domArr=domain.match(atomPat)
    var len=domArr.length
    if (domArr[domArr.length-1].length<2 ||
        domArr[domArr.length-1].length>4) {
       return false
    }

    if (len<2) {
       return false
    }

    return true;

}
