﻿var doesImageExist = false;
var loggedUserID="";
var isInvited=false;
var iMousePosX;
var iMousePosY;

var personInd=-1;
var newsYear=-1;
var newsMonth=-1;
var date=new Date();

function $el(id){
    var source = document.getElementById(id);
    if (!source){
        source = document.getElementsByName(id)[0];
    }
    return source;
}

function $clr(id){
    $el(id).innerHTML = '';
}

function $rst(id){
    $el(id).value = "";
}

var r={
    'notletters':/[^a-zA-Z]/g,
    'notlettersNSp':/[^a-zA-Z\u0430-\u044F\u0410-\u042F\s\u0406\u0456\u040E\u045E]/g
}

function valid(o,w){
    o.value = o.value.replace(r[w],'');
}

function $noselect(id) {
    var el = $el(id);
    el.onselectstart = function() {return false;}
    el.onmousedown = function() { return false;}
}


function $curPos(e){
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    }
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX +
                   (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY +
                   (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

function getMouseXYPos(e) {
    if (!e) e = window.event;
    if (e){
  	    iMousePosX = document.all ? (e.clientX + document.body.scrollLeft) : e.pageX;
  	    iMousePosY = document.all ? (e.clientY + document.body.scrollTop) : e.pageY;
    }
}

function trim(str){
    return str.replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ') ;
}

function $key(e){
    var code;
    if (!e) e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    return code;
}

function onSubmit( func, e ) {
    if($key(e)=="13") eval(func);
}

function $over(el, over){
    if(over){
        el.style.opacity = 0.8;
        el.style.filter = "alpha(opacity=80)";
    }else{
        el.style.opacity = 1;
        el.style.filter = "alpha(opacity=100)";
    }
}

function setOnOverImages(){
    var imgs = document.getElementsByTagName("img");
    for(var i=0;i<imgs.length;i++){
        if(imgs[i].style.opacity == 'op'){
            imgs[i].onmouseover = function(){$over(this,true);};
            imgs[i].onmouseout = function(){$over(this,false);};
        }
    }
}

function select(item,ind){
    var oldInd;
    var itemdiv;
    switch(item){
        case "alfa":
            oldInd = personInd;
            itemdiv = item;
            break;
        case "year":
            oldInd = newsYear;
            itemdiv = "event";
            break;
        case "month":
            oldInd = newsMonth;
            itemdiv = "event";
            break;
    }

    if(oldInd==ind) return;

    var tab = $el(item+"_"+ind);
    tab.style.backgroundColor = '#ffffff';
    tab.style.color = '#000000';
    tab.style.borderColor = '#000000';

    if(oldInd > -1 ){
        var old_tab = $el(item+"_"+oldInd);
        old_tab.style.backgroundColor = '#000000';
        old_tab.style.color = '#ffffff';
        old_tab.style.borderColor = '#ffffff';        
        if(oldInd==31){
            hideAll("div_alfa_");
        }else{
            var old_div = $el("div_"+itemdiv+"_"+(item=="alfa"?oldInd:newsYear+"-"+newsMonth));
            old_div.style.display = 'none';
        }
    }

    switch(item){
        case "alfa":
            personInd = ind;
            break;
        case "year":
            newsYear = ind;
            var curYear = date.getFullYear();
            var curMonth = date.getMonth()+1;
            for(var m=1;m<13;m++){
                    $el("month_"+m).style.visibility = "visible";
                }
            if(curYear==ind){
                for(m=curMonth+1;m<13;m++){
                    $el("month_"+m).style.visibility = "hidden";
                }
            }
            if(ind==2009){
                $el("month_1").style.visibility = "hidden";
                $el("month_2").style.visibility = "hidden";
            }
            select("month",curYear==ind?curMonth:1);
            break;
        case "month":
            newsMonth = ind;
            break;
    }

    if(ind==31){
        showAll("div_alfa_");
    }else{
        var div = $el("div_"+itemdiv+"_"+(item=='alfa'?ind:newsYear+"-"+newsMonth));
        div.style.display = 'block';
    }    
}

function over(item,ind,over){
    var tab = $el(item+"_"+ind);
    var oldInd;

    switch(item){
        case "alfa":
            oldInd = personInd;
            break;
        case "year":
            oldInd = newsYear;
            break;
        case "month":
            oldInd = newsMonth;
            break;
    }

    if(oldInd != ind){
        if(over){
            tab.style.backgroundColor = '#ffffff';
            tab.style.color = '#000000';
        }else{
            tab.style.backgroundColor = '#000000';
            tab.style.color = '#ffffff';
        }
    }
}

function blockOver(item,fontel,over){
    if(over){
        item.style.backgroundColor='#000000';
        $el(fontel).style.color='#ffffff';
    }else{
        item.style.backgroundColor='#ffffff';
        $el(fontel).style.color='#000000';
    }
}

function showLabel(id,show){
    $el(id).style.visibility = show ? "visible" : "hidden";       
}

function collapseEmptyAuthors(divPattern){
    var div;
    for(var i=1; i<31; i++){
        div = $el(divPattern+i);
        if(trim(div.innerHTML)==""){
            div.style.paddingTop='0';
        }
    }
}
function showAll(divPattern){
    for(var i=1; i<31; i++){
        $el(divPattern+i).style.display = 'block';
    }
}

function hideAll(divPattern){    
    for(var i=1; i<31; i++){
        $el(divPattern+i).style.display = 'none';
    }
}

function updateTitle(title) {
    //var te = document.getElementsByTagName("title")[0];
    //te.innerHTML = te.innerHTML + title;
	document.title += " " + title;
	
	var de = document.getElementsByTagName("meta")[1];    
    de.setAttribute('content', title + ' - www.prajdzisvet.org');
}


function updateKeywords(keywords) {    
    var ke = document.getElementsByTagName("meta")[0];
    var content = ke.getAttribute('content');	
    ke.setAttribute('content', keywords + ', ' + content);	
}











/////////////////////// travel

var objNames = new Array();
objNames['resort'] = 'курорт';
objNames['tour'] = 'тур';
objNames['hotel'] = 'отель';
objNames['accommodation'] = 'номер';
objNames['meals'] = 'питание';
objNames['attribute'] = 'дополнительные сведения';


if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function (obj, fromIndex) {
    if (fromIndex == null) {
        fromIndex = 0;
    } else if (fromIndex < 0) {
        fromIndex = Math.max(0, this.length + fromIndex);
    }
    for (var i = fromIndex, j = this.length; i < j; i++) {
        if (this[i] === obj)
            return i;
    }
    return -1;
  };
}

String.prototype.ReplaceAll = function(stringToFind,stringToReplace){
    var temp = this;
    var index = temp.indexOf(stringToFind);
    while(index != -1){
        temp = temp.replace(stringToFind,stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}

function isOptionPresent(sbox,name){
    for(var i=0; i<sbox.options.length; i++){
        if(sbox.options[i].text == name) return true;
    }
    return false;
}

function addOption(sbox,text,value){
    var optn = document.createElement("option");
    optn.text = text;
    optn.value = value;
    sbox.options.add(optn);
}

function removeSelected(sbox){    
    for(var i=0; i<sbox.options.length; i++){
        if(sbox.options[i].selected){
            sbox.remove(i);
            break;
        }
    }
}

function countSelected(sbox){
    var count = 0;
    for (var i=0; i<sbox.options.length; i++) {
        if (sbox.options[i].selected) {
            count++;
        }
    }
    return count;
}

function capitalize(string){
    return string.charAt(0).toUpperCase() + string.slice(1);
}