function CheckAllowedData(in_str,elms,formname){
//    if (in_str.indexOf("href") != -1 || in_str.indexOf("frame") != -1){
//        alert("Запрещено использовать ссылки!");
//        return false;
//    }
//    document.forms[formname].submit();
//    $(elms).attr('disabled','disabled');
}

function CheckAllowedDataArray(in_class){
//    var elms = $("."+in_class);
//    for (i = 0; i < elms.length; i++){
//        if (elms[i].value.indexOf("href") != -1 || elms[i].value.indexOf("frame") != -1){
//            alert("Запрещено использовать ссылки!");
//            return false;
//        }
//    }
}

function CheckFormNotes(){
    var allSpacesRe = /\s+/g;

    var start_fishing_date_day = $("#start_fishing_date_day").val();
    if (start_fishing_date_day.replace(allSpacesRe, "") == '' || start_fishing_date_day.replace(allSpacesRe, "") == 'дд'){
        alert('Вы не ввели день начала рыбалки!')
        $("#start_fishing_date_day").focus();
        $("#start_fishing_date_day").css("background-color", "red");
        return false;
    }

    var start_fishing_date_month = $("#start_fishing_date_month").val();
    if (start_fishing_date_month.replace(allSpacesRe, "") == '' || start_fishing_date_month.replace(allSpacesRe, "") == 'мм'){
        alert('Вы не ввели месяц начала рыбалки!')
        $("#start_fishing_date_month").focus();
        $("#start_fishing_date_month").css("background-color", "red");
        return false;
    }

    var start_fishing_date_year = $("#start_fishing_date_year").val();
    if (start_fishing_date_year.replace(allSpacesRe, "") == '' || start_fishing_date_year.replace(allSpacesRe, "") == 'гг'){
        alert('Вы не ввели год начала рыбалки!')
        $("#start_fishing_date_year").focus();
        $("#start_fishing_date_year").css("background-color", "red");
        return false;
    }

    var reservoir_id = $("#reservoir_id option:selected").val();
    var new_reservoir = $("#new_reservoir").val();
    if (reservoir_id == -1 && new_reservoir == ''){
        alert('Вы не ввели название водоема!')
        $("#reservoir_id").focus();
        $("#reservoir_id").css("background-color", "red");
        return false;
    }

    var place_id = $("#place_id option:selected").val();
    if (place_id == -1){
        alert('Вы не ввели место рыбалки!')
        $("#place_id").focus();
        $("#place_id").css("background-color", "red");
        return false;
    }

    var method_id = $("#method_id option:selected").val();
    if (method_id == -1 || method_id == undefined){
        alert('Вы не выбрали способ ловли!')
        $("#method_id").focus();
        $("#method_id").css("background-color", "red");
        return false;
    }

    var catch_id = $("#catch_id option:selected").val();
    if (catch_id == -1 || catch_id == undefined){
        alert('Вы не ввели общий улов!')
        $("#catch_id").focus();
        $("#catch_id").css("background-color", "red");
        return false;
    }

    var fish_id = $("#fish_id option:selected").val();
    if (fish_id == -1 || fish_id == undefined){
        alert('Вы не выбрали какую рыбу ловили!')
        $("#fish_id").focus();
        $("#fish_id").css("background-color", "red");
        return false;
    }

    var total = $("#total").val();
    if (total.replace(allSpacesRe, "") == '' ){
        alert('Вы не подвели итога!')
        $("#total").focus();
        $("#total").css("background-color", "red");
        return false;
    }
}

function insert_text_cursor(_obj_name, _text)
{
var area=document.getElementsByName(_obj_name).item(0);
if ((area.selectionStart)||(area.selectionStart=='0'))
{
  var p_start=area.selectionStart;
  var p_end=area.selectionEnd;
  area.value=area.value.substring(0,p_start)+_text+area.value.substring(p_end,area.value.length);
}
if (document.selection)
{
  area.focus();
  sel=document.selection.createRange();
  sel.text=_text;
}
}


/**
 * DHTML date validation script.
 */
// Declaring valid date character, minimum year and maximum year
var date = new Date();
var dtCh= "/";
var minYear=date.getFullYear();
var maxYear=parseInt(minYear) + 1;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function validDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("Дата начала поездки должна быть в формате дд/мм/гггг")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Проверьте номер месяца")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Проверьте номер дня")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Номер года должен быть в пределах от "+minYear+" до "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Некорректная дата")
		return false
	}
return true
}

function OnlyLatin(e){
    if (e.charCode)
        var code = e.charCode;
    else if (e.keyCode)
        var code = e.keyCode;
    else{
        var code = 0;
    }
    if (code >= 65 && code <= 90){
        return true;
    }
    else if (code >= 97 && code <= 122){
        return true;
    }
    else if (code == 45 || code == 95 || code == 8 || code == 46 || code == 37 || code == 39){
        return true;
    }
    else{
        return false;
    }

}

function OnlyCyrillic(e){
    if (e.charCode)
        var code = e.charCode;
    else if (e.keyCode)
        var code = e.keyCode;
    else{
        var code = 0;
    }

    if (code >= 1072 && code <= 1103){
        return true;
    }
    else if (code >= 1040 && code <= 1071){
        return true;
    }

    else if (code == 1105 || code == 1025 || code == 45 || code == 8 || code == 46 || code == 37 || code == 39){
        return true;
    }
    else{
        return false;
    }

}

function DownCount(seconds){
    var last_sec = 60 - Number(seconds);
    $(".submit_id").val("Отправить "+last_sec);
    seconds = Number(seconds) + 1;
    if (seconds <= 60){
        setTimeout("DownCount("+seconds+")",1000);
    }
    else{
        $(".submit_id").val("Отправить");
        $(".submit_id").removeAttr("disabled");
    }
}

function CheckValidEmail(email){
    var check_email = email.split(' ').join('');
    var re = /^\w+([\.-]?\w+)*@(((([a-z0-9]{2,})|([a-z0-9][-][a-z0-9]+))[\.][a-z0-9])|([a-z0-9]+[-]?))+[a-z0-9]+\.([a-z]{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/i;
    if (check_email.length > 0) {
        if (!re.test(check_email)) {
            alert("Некорректный email!");
            return false;
        }
    }
    return true;
}

function Voting(){
    if ($("#vote0").attr("checked") != true && $("#vote1").attr("checked") != true){
        alert('Сделайте Ваш выбор!');
        return false;
    }
}

function ShowAnswerForm(id){
    $(".tr_answer").css('display','none');
    $(".forum_answer_data").val('');
    $("#tr_answer_"+id).css('display', '');
}

function CloseFormAnswer(){
    $(".tr_answer").css('display','none');
}

function CheckFillFieldReg(){
    var allSpacesRe = /\s+/g;

    var firstname = $("#firstname").val();
    if (firstname.replace(allSpacesRe, "") == ''){
        alert('Вы не ввели фамилию!')
        $("#firstname").focus();
        return false;
    }

    var lastname = $("#lastname").val();
    if (lastname.replace(allSpacesRe, "") == ''){
        alert('Вы не ввели имя!')
        $("#lastname").focus();
        return false;
    }

    var middlename = $("#middlename").val();
    if (middlename.replace(allSpacesRe, "") == ''){
        alert('Вы не ввели отчество!')
        $("#middlename").focus();
        return false;
    }

    var email = $("#email").val();
    if (email.replace(allSpacesRe, "") == ''){
        alert('Вы не ввели email!')
        $("#email").focus();
        return false;
    }
    if (!CheckValidEmail(email)){
        return false;
    }

    var city = $("#city").val();
    if (city.replace(allSpacesRe, "") == ''){
        alert('Вы не ввели место проживания!')
        $("#city").focus();
        return false;
    }

    var year = $("#year").val();
    if (year.replace(allSpacesRe, "") == ''){
        alert('Вы не ввели год рождения!')
        $("#year").focus();
        return false;
    }

    return true;
}

function SwitchSpoiler(id){
    if ($("#"+id).css('display') == 'none'){
        $("#"+id).css('display','block');
    }
    else{
        $("#"+id).css('display','none');
    }
}

function ShowHideElms(id){
    if ($("#"+id).css('display') == 'none'){
        $("#"+id).fadeIn();
    }
    else{
        $("#"+id).fadeOut();
    }
}

function ShowEditComment(id){
    $("#comment_"+id).fadeIn();
}

function HideEditComment(id){
    $("#comment_"+id).fadeOut();
}
