﻿// JScript File

function fotoForms(){
fonImg = document.getElementById('bkBg')
fonFrame = document.getElementById('bkFrame')
formBlock = document.getElementById('alBlock')
Dwidth = document.body.clientWidth
Dheight = document.body.scrollHeight
fonImg.style.width = Dwidth+'px'
fonImg.style.height = Dheight+'px'
fonFrame.style.width = Dwidth+'px'
fonFrame.style.height = Dheight+'px'
fonImg.style.display = 'block'
formBlock.style.display = 'block'
fonFrame.style.display = 'block'
	leftMax = formBlock.offsetWidth/2
	leftMin = Dwidth/2
	topMin = formBlock.offsetHeight/2
	docHeight = document.body.offsetHeight/2
	topMax = document.body.scrollTop+docHeight
	formBlock.style.left = leftMin -= leftMax
	formBlock.style.top = topMax -= topMin
}

function closeBg(){
document.getElementById('bkBg').style.display = 'none'
document.getElementById('bkFrame').style.display = 'none'
document.getElementById('alBlock').style.display = 'none'
}

function thisForm(){
	if(fotoForm.accept.checked == true)
	fotoForm.gogogo.disabled = false
	else
	fotoForm.gogogo.disabled = true
}

function _wGetEl(id)
{
	return document.getElementById(id);
}

function _wGetTxt(obj)
{
    if (obj != null)
	    return (obj.textContent? obj.textContent : ( obj.firstChild.nodeValue ? obj.firstChild.nodeValue : obj.nodeValue ) );
    else 
        return '';
}

function _wGetAttr(el,attr)
{
	return el.getAttribute(attr);
}

var pemail = "";
var pname = "";

function setNameEmail(flag123)
{
abc = flag123;

if (abc == 'user1')
{
    pname = document.getElementById("pname").innerHTML;
    pemail = document.getElementById("pemail").innerHTML;
}
else
{
    pname = fotoForm.name.value;
    pemail = fotoForm.mail.value;
}
}

function checkData()
{
	var fotoname = fotoForm.foto.value;
    if (fotoname.substr(fotoname.lastIndexOf("."), fotoname.length).toLowerCase() != ".jpg" && fotoname.substr(fotoname.lastIndexOf("."), fotoname.length).toLowerCase() != ".jpeg"  )
    {
        alert ('Неправильный формат картинки.');
        return false;
    }
	
    if (fotoForm.objDescr.value.length > 200)
    {
        alert ('Слишком большое описание. Пожалуйста, оставьте не более 200 символов');
        return false;
    }

        if (!checkEmail(pemail))
        {
            alert ('Поле e-mail введено не полностью или с ошибкой');
            return false;
        }
        
    return true;
}

function formSubmit()
{
    _wGetEl("fotoForm").submit();
    
    showLoading();
    //getPath();
    checkPath2();
}

function checkPath2()
{
    checkTags();
    
    var dt = new Date(); dt.getTime();
    var rnd = dt.getSeconds()*dt.getMilliseconds();
            
    //var name = fotoForm.name.value;
    //var email = fotoForm.mail.value;
    var descr = fotoForm.objDescr.value;   

    var sxml = '<konkurs><name>'+pname+'</name><email>'+pemail+'</email><descr>'+descr+'</descr><bid>'+obj_bid+'</bid></konkurs>';
    AjaxXmlRequest(sxml,'mod/addphoto/core.aspx?option=insert&rnd='+Math.floor(Math.random()*9999),ffunc);
    
}

function ffunc (text, xml) 
{
    str = text;
    if (str == 'wrong size')
    {
        clearForm();
        alert(markupList['err1']); //alert('Вы пытаетесь загрузить файл больше 10 Мб, так нельзя');
        return false;
    }
    
    if (str == 'no image')
    {
        clearForm();
        alert(markupList['err2']); //alert('Выберите файл для загрузки');
        return false;
    }
    
    if (str == 'wrong ext')
    {
        clearForm();
        alert(markupList['err3']); //alert('Файл не является jpeg изображением, изучите требования');
        return false;
    }
    
    if (str == 'too small') 
    {
        clearForm();
        alert(markupList['err4']); //alert('Размер изображения не соответствует минимальным требованиям');
        return false;
    }
    
    if (str == '')
    {
        checkPath2();
        return false;
    }

    clearForm(); closeBg(); alert('Фотография успешно отправлена!');
    AjaxRequest (AjaxCreate(), "ajax/reloadphotos.aspx?rnd="+Math.floor(Math.random()*9999)+"&id="+obj_id+"&bid="+obj_bid+"&obj_type="+obj_type, reloadPhotos);
}

function reloadPhotos (text, xml)
{
    var el = document.getElementById("photoBlock");
    if (text.length < 10) 
    	text = '';
    if (text.length > 0)
        el.innerHTML = text;
        
    //updateImageList();
    initLightbox();
}

function checkEmail(value)
{
    return value.replace(/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i, '') == '';
}

function clearForm()
{
    fotoForm.foto.value = "";
    fotoForm.accept.checked = false;
    fotoForm.objDescr.value = "";
    //fotoForm.objName.value = "";
    
    hideLoading();
}
 
function checkTags() 
{
    //fotoForm.name.value = fotoForm.name.value.replace(/</g,'"').replace(/>/g,'"');
    pname = pname.replace(/</g,'"').replace(/>/g,'"');
    fotoForm.objDescr.value = fotoForm.objDescr.value.replace(/</g,'"').replace(/>/g,'"');
}

function showLoading()
{
    var el = document.getElementById("krutilko");
    el.innerHTML = '<img src="content/img/loading.gif">';
}

function hideLoading()
{
    var el = document.getElementById("krutilko");
    el.innerHTML = '';
}
