﻿// 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.name.value != '' && fotoForm.mail.value != '' && fotoForm.foto.value != '' && fotoForm.accept.checked == true)
	fotoForm.gogogo.disabled = false
	else
	fotoForm.gogogo.disabled = true
}


var activeEl;
var cityId = null;
//var path = '';
function hideMyPants()
{
    var el = _wGetEl("div_citySelectText");
    el.innerHTML = "";
    el.style.display = "none";
    
    _wGetEl("iframeCL").style.display = 'none';
}
function clearMyPants()
{
    var el = _wGetEl("cityInput")
    el.value = "";
    cityId = 0;
}
function activateLi(el)
{
   el.style.background = "#d00101";  
   el.style.color = "#fff";
   activeEl = el;
   
}
function deactivateLi(el)
{
   el.style.background = "#ffffff";  
   el.style.color = "#000";
   activeEl = null;
}

function fill_citySelect(text, xml)
{
	    var intxt = "";
	    var curIndex = null;
		
		var items = xml.getElementsByTagName("item");
	    var el = _wGetEl("div_citySelectText");
	    el.innerHTML = "";
	    el.style.display = "block";
	    
        intxt += "<ul class=\"citySelectList\">";
	  	for(var i = 0; i < items.length; i++)
		{		    
		    var loc_id =  _wGetAttr(items[i],"id");
			intxt += "<li id=\"li_"+loc_id+"\" onmouseover=\"activateLi(this);\" onmouseout=\"deactivateLi(this)\" onmousedown=\"cityId='"+loc_id+"'; cityClick();\" onfocus=\"cityClick();\">"+_wGetTxt(items[i])+"</li>";
		}		
        intxt += "</ul>";
        
        _wGetEl("div_citySelectText").innerHTML += intxt;	
        
    var clist = _wGetEl("div_citySelectText");
    var width = clist.offsetWidth;
    var height = clist.offsetHeight;
    _wGetEl("iframeCL").style.width = width;
    _wGetEl("iframeCL").style.height = height;
    _wGetEl("iframeCL").style.display = 'block';	
}

function cityList()
{
    var c_id = _wGetEl("mod_search_country_select");
    c_id = c_id.options[c_id.selectedIndex].value;
    var name = _wGetEl("cityInput").value;
    var sxml = "<item><name>"+name+"</name><country_id>"+c_id+"</country_id></item>";
    AjaxXmlRequest(sxml, "mod/contest/core.aspx?option=get", fill_citySelect);
}

function cityClick(id)
{
    if(activeEl)
        _wGetEl("cityInput").value = activeEl.innerHTML;
    hideMyPants();
}

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);
}

function checkData()
{
    
    if (fotoForm.objDescr.value.length > 200)
    {
        alert ('Слишком большое описание. Пожалуйста, оставьте не более 200 символов');
        return false;
    }
    if (!checkEmail(fotoForm.mail.value))
    {
        alert ('Поле e-mail введено не полностью или с ошибкой');
        return false;
    }
        
    return true;
}

function formSubmit()
{
    _wGetEl("fotoForm").submit();
    
    showLoading();
    //getPath();
    checkPath2();
}

//function getPath()
//{
//    var dt = new Date(); dt.getTime();
//    var rnd = dt.getSeconds()*dt.getMilliseconds();
//    AjaxRequest(AjaxCreate(),"mod/contest/core.aspx?option=getpath&rnd="+rnd,checkPath);
//}

//function checkPath(txt, xml)
//{   
//    var path = txt;
//    
//    if (path != '') 
//    {
//        if (!checkPathErr(path))
//            return;
//            
//        checkTags();
//        
//        var dt = new Date(); dt.getTime();
//        var rnd = dt.getSeconds()*dt.getMilliseconds();
//                
//        var name = fotoForm.name.value;
//        var email = fotoForm.mail.value;
//        var country_id = _wGetEl("countryInput");
//        country_id = country_id.options[country_id.selectedIndex].value;
//       
//        var type_id = _wGetEl("objectTypeInput");
//        type_id = type_id.options[type_id.selectedIndex].value;
//        
//        var cityname = fotoForm.cityInput.value;
//        var objname = fotoForm.objName.value;
//        var descr = fotoForm.objDescr.value;   
//            
//        var sxml = '<konkurs><name>'+name+'</name><email>'+email+'</email><cityname>'+cityname+'</cityname><path>' + path + '</path><country>'+country_id+'</country><city>'+cityId+'</city><type>'+type_id+'</type><objname>'+objname+'</objname><descr>'+descr+'</descr></konkurs>';
//        AjaxXmlRequest(sxml,'mod/contest/core.aspx?option=insert&rnd='+Math.random(),ffunc);
//        clearForm(); closeBg(); alert('Фотография успешно отправлена!');
//        
////        if (document.location.href.match("gallery"))
////            document.location.reload();   
//          
//    }
//    else setTimeout("getPath()",1000);
//}

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 country_id = _wGetEl("countryInput");
    country_id = country_id.options[country_id.selectedIndex].value;
   
    var type_id = _wGetEl("objectTypeInput");
    type_id = type_id.options[type_id.selectedIndex].value;
    
    var cityname = fotoForm.cityInput.value;
    var objname = fotoForm.objName.value;
    var descr = fotoForm.objDescr.value;   
        
    var sxml = '<konkurs><name>'+name+'</name><email>'+email+'</email><cityname>'+cityname+'</cityname><country>'+country_id+'</country><city>'+cityId+'</city><type>'+type_id+'</type><objname>'+objname+'</objname><descr>'+descr+'</descr></konkurs>';
    AjaxXmlRequest(sxml,'mod/contest/core.aspx?option=insert&rnd='+Math.random(),ffunc);
    
}

function ffunc (text, xml) 
{
    str = text;
    if (str == 'wrong size')
    {
        clearForm();
        alert('Вы пытаетесь загрузить файл больше 2 Мб, так нельзя');
        return false;
    }
    
    if (str == 'no image')
    {
        clearForm();
        alert('Выберите файл для загрузки');
        return false;
    }
    
    if (str == 'wrong ext')
    {
        clearForm();
        alert('Файл не является картинкой');
        return false;
    }
    
    if (str == '')
    {
        checkPath2();
        return false;
    }

    clearForm(); closeBg(); alert('Фотография успешно отправлена!');
}

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,'"');
    fotoForm.cityInput.value = fotoForm.cityInput.value.replace(/</g,'"').replace(/>/g,'"');
    fotoForm.objName.value = fotoForm.objName.value.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 = '';
}
