var list_currentLetters = new Array();
var warning;
var is_go_interval = false;
var interval_id;
var old_site_title = document.title;
var valid_school = false;

/* startList = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}
window.onload=startList;
 */

function getEl (element) {

	return document.getElementById(element);

}
function makeVoid(el,val){
    if(el.value==val) el.value = '';
}
function checkStudent(element,val1,val2,val3,val4){ 
	h = document.getElementById('school_info');
	h.className = '';
	//document.getElementById('school_info_desc').style.display = 'block';
	document.getElementById('block_sity').style.display = 'block';
	    if(element.value == 2) {
	    	$("#class_div3").fadeIn("slow");
	    } else {	
	    	$("#class_div3").fadeOut("fast");
	    }	
	    if(element.value == 1){
	        $("#class_div").fadeIn("slow");
	        $("#class_div2").fadeOut("fast");        
	        h.innerHTML = val1;
	    }
	    else if(element.value == 2){
	        $("#class_div").fadeOut("fast");
	        $("#class_div2").fadeOut("fast");
	        h.innerHTML = val2;
	    }
	    else if(element.value == 3){
	        $("#class_div2").fadeOut("fast");    
	        $("#class_div").fadeOut("fast");
	        h.innerHTML = val4;
	    }
	    else if(element.value == 4){
	        $("#class_div2").fadeIn("slow");
	        $("#class_div").fadeOut("fast");
	        h.innerHTML = val1;
	    }
	    else{
	        $("#class_div").fadeOut("fast");
	        $("#class_div2").fadeOut("fast");
	        h.innerHTML = val3;
	        h.className = notactive;
	        document.getElementById('school_info_desc').style.display = 'none';
	        document.getElementById('block_sity').style.display = 'none';
	    }


}
/*function checkStudent(){
    if(document.getElementById('student').checked){
        $("#class_div").fadeIn("slow");
    }
    else{
        $("#class_div").fadeOut("fast");
    }
}*/
function validateFormRecoverPass(form,mes1,mes2,mes3){
    if(form.pass.value == '' || form.pass2.value == ''){
        alert(mes1);
        return false;
    }
    else if (form.pass.value != form.pass2.value){
        alert(mes2);
        return false;
    }
    else if(form.pass.value.length < 3 || form.pass2.value.length < 3){
        alert(mes3);
        return false;
    }
    else return true;
}

function setSchool(city,after_error,known_school_id,isset_new_school){
	if(city != "choose_city"){
	if(city != "other"){
		$.ajax({
			beforeSend: function(){
				$("#school_loader").show();
			},
			type: "post",
			url: "/schools/getCitySchool/",
			data: {city_id: city},
			dataType: "json",
			success: function(answ){
				document.getElementById('block_sity').style.display = 'block';
				$("#school_loader").hide();
				if(answ.length > 0){
					document.getElementById('block_sity').style.display = 'block';
					var text = "<span class='field'>Школа</span><select name='known_school' id='known_school' class='register4' onChange='hideNewSchool()'>";
					for(var i = 0; i< answ.length; i++){
						if(answ[i].id == known_school_id) var selected = " selected = 'selected' "; else var selected = "";
						text += "<option value="+answ[i].id+selected+">"+answ[i].name+"</option>";
					}
					text += "</select><span></span>";
					if($("#register_school").val() == "false") text += "&nbsp;&nbsp;&nbsp;<a href='javascript: addSchool()'>Я не знайшов своєї школи серед списку</a>";
					if($("#register_school").val() == "true") text +="<script>document.getElementById('find').disabled = false;</script>";
					$("#known_schools").html(text).fadeIn("slow");
					$("#school_div").fadeOut("fast");
					valid_school = true;
					if(!after_error) $("#new_school").val("");
					if(after_error == true && known_school_id == 0) $("#school_div").fadeIn();
					if(isset_new_school)$("#school_div").fadeIn("fast");
				}
				else{
					//$("#known_schools").hide();
					//$("#school_div").show();
					//$("#school").val("");
					var txt = "В нашій базі даних нема шкіл з вашого населеного пункту.";
					if($("#register_school").val() == "false") txt +=" <a href='javascript: addSchool()'>Додати</a>";
					if($("#register_school").val() == "true") txt +=" <span style='color: red;'>Реєстрація неможлива.</span><script>document.getElementById('find').disabled = true;</script>";
					$("#known_schools").html(txt).show("slow");
					$("#school_div").fadeOut("fast");
					if(!after_error) $("#new_school").val("");
					else $("#school_div").fadeIn();
					valid_school = false;
				}
			}
		});
	}
	}
}
function addSchool(){
	if(document.getElementById("school_div").style.display == "none") $("#school_div").fadeIn("slow");
	//valid_school = true;
	//$("#known_schools").hide();
}
function hideNewSchool(){
	$("#school_div").hide();
}

//==================browser checking

	var u = navigator.userAgent.toLowerCase();
	is = function(t){return (u.indexOf(t)!=-1)};
	addClass = function(el,val){
		if(!el.className) {
			el.className = val;
		} else {
			var newCl = el.className;
			newCl+=(" "+val);
			el.className = newCl;
		}
	}
	var el = document.getElementsByTagName('html')[0];
	if(is('chrome')) addClass(el,'chrome');
	if(is('opera/9')) addClass(el,'opera9');
	if(is('opera')) addClass(el,'opera');
	if(is('firefox/2')) addClass(el,'gecko ff2');
	if(is('firefox/3')) addClass(el,'gecko ff3');
	if(is('gecko/')) addClass(el,'gecko');
	if(is('mozilla/')) addClass(el,'gecko');
	if(is('msie')) addClass(el,'msie');
	if(is('msie 7')) addClass(el,'msie7');
	if(is('msie 6')){ 
		if(!is('msie 7')){ 
			ie6=true;
			addClass(el,'msie6');
		}
	}

//=========================== show large picture ====================

function showLargePic (picUrl) {
	$("#full_cover").css("display","block");
	$("#largePic").css("display","block");
	getEl('largePicIMG').src = picUrl;
}
function hideLargePic () {
	$("#full_cover").css("display","none");
	$("#largePic").css("display","none");
}

function checkUsername(username,mess_is_required,mess_is_short,mess_is_long,mess_enter_latin){
	if(username.value == ''){
		$("#username").next("span").removeClass("agree").addClass("info").text(mess_is_required);
		$("#username_valid").val('');
		return false;
	}
	if(username.value.length < 2){
		$("#username").next("span").removeClass("agree").addClass("info").text(mess_is_short);
		$("#username_valid").val('');
		return false;
	}
	if(username.value.length > 50){
		$("#username").next("span").removeClass("agree").addClass("info").text(mess_is_long);
		$("#username_valid").val('');
		return false;
	}
	var rg = /[а-я]/i;
    if (rg.test(username.value)){
    	$("#username").next("span").removeClass("agree").addClass("info").text(mess_enter_latin);
    	$("#username_valid").val('');
    	return false;
    }
	$("#username_valid").val('1');
    $("#username").next("span").removeClass("info").empty().addClass("agree");
}
function checkUsernameServer(full_url,mess_is_present,username){
	$.ajax({
		type: "get",
		url: full_url+"users/checkusername/",
		data: {username: username},
		dataType: "html",
		beforeSend: function() {
			$("#username_loading").show();
		},
		success: function(answ) {
			$("#username_loading").hide();
			if(answ == "true"){
				$("#username").next("span").removeClass("info").empty().addClass("agree");
				$("#username_valid").val('1');
			}
			else if(answ == "false"){
				$("#username").next("span").removeClass("agree").addClass("info").text(mess_is_present);
				$("#username_valid").val('');
			}
		}
	});
}
function detectEnter(event){
	if(event.keyCode==13){
		$("#search_input_arrow").click();
	}
}

function checkEmail(email,mess_is_required,mess_is_not_mail) {
	if(email.value == ''){
		$("#email").next("span").removeClass("agree").addClass("info").text(mess_is_required);
		$("#email_valid").val('');
		return false;
	}
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email.value)==false) {
		$("#email").next("span").removeClass("agree").addClass("info").text(mess_is_not_mail);
		$("#email_valid").val('');
		return false;
	}
	$("#email_valid").val('1');
	$("#email").next("span").removeClass("info").empty().addClass("agree");
}
function checkEmailServer(full_url,mess_is_present,email){
	$.ajax({
		type: "get",
		url: full_url+"users/checkuseremail/",
		data: {email: email},
		dataType: "html",
		beforeSend: function() {
			$("#email_loading").show();
		},
		success: function(answ) {
			$("#email_loading").hide();
			if(answ == "true"){
				$("#email").next("span").removeClass("info").empty().addClass("agree");
				$("#email_valid").val('1');
			}
			else if(answ == "false"){
				$("#email").next("span").removeClass("agree").addClass("info").text(mess_is_present);
				$("#email_valid").val('');
			}
		}
	});
}


var dialogWin = null;

function popup_window (url) {
	//if (dialogWin != null) dialogWin.close();
	dialogWin = window.open (url, "ShkolaOnline.net","status=1,toolbar=0,location=0,menubar=0,resizable=1,height=599,width=910");
	dialogWin.focus();
	return false;
}


function checkMail(new_site_title){
	$.ajax({
		type: "post",
		url: "/users/checkAjaxMail/",
		success: function(rdata){
			//alert(rdata.id);
			var arrdata = JSON.parse(rdata);//alert(arrdata);
			var data = arrdata['count'];
			
			if(data > 0){
			//alert(arrdata['id']);
				$("#message_count").text(data);
				$("#dialogs").attr("class", "active");
				$("#dialogs").attr("onclick", "");
				$("#dialogs").click(function () {
					popup_window("/dialogs/main/id/"+arrdata['id']);
				});
				//alert($("#dialogs").attr("onclick"));
				var i = 0;
				if(!is_go_interval){
					is_go_interval = true;
					interval_id = setInterval(function(){
						if(i==0){
							document.title = new_site_title
							i = 1;
						}
						else{
							document.title = old_site_title;
							i=0;
						}
					},1000);
				}
			}
			else{
				$("#message_count").text("");
				$("#dialogs").removeAttr("class");
				clearInterval(interval_id);
				document.title = old_site_title;
				is_go_interval = false;
			}
		}
	});
}
function trim(string){
	return string.replace(/(^\s+)|(\s+$)/g, "");
}

function setCookie(name, value, expires, path, domain, secure) {
	expires instanceof Date ? expires = expires.toGMTString() : typeof(expires) == 'number' && (expires = (new Date(+(new Date) + expires * 1e3)).toGMTString());
	var r = [name + "=" + escape(value)], s, i;
	for(i in s = {expires: expires, path: path, domain: domain}){
		s[i] && r.push(i + "=" + s[i]);
	}
	return secure && r.push("secure"), document.cookie = r.join(";"), true;
}
function popup_windowWH(url,w,h){
	newWin = window.open (url, "ShkolaOnline.net","status=1,toolbar=0,location=0,scrollbars=1, menubar=0,height="+h+",width="+w+",resizable=1");
	newWin.focus();

	return false;

}
