var arOnLoad = new Array();

function get(id)
{
    return document.getElementById(id);
}

function addArrayLoad(f)
{
    arOnLoad[arOnLoad.length] = f;
}

function doArrayLoad()
{
    for (i=0; i<arOnLoad.length; i++) {
        eval(arOnLoad[i]);
    }
}

function openClose(M) 
{
    if (get(M).style.display=='none'){
        get(M).style.display=''
    } else {
        get(M).style.display='none';
    }
}

function reloadNospam(id)
{
    document.getElementById('imgnum').innerHTML = '<img src="/scripts/nospam/image.jpg?id='+id+'&amp;rl='+Math.random()+'" alt="Контрольное число">';
}


function getClientHeight()
{
    return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function getClientWidth()
{
    return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function checkMailForm()
{
    email=document.getElementById('email').value;
    var exmail=/^[a-z\.\-_0-9]+@[a-z\.\-_0-9]+\.[a-z]{2,}$/i;
    if (email && !exmail.test(email)){
        alert("Адрес электронной почты был введен некорректно.");
        return false;
    }
    if( !document.getElementById('name').value ||
        !document.getElementById('text').value ||
        !document.getElementById('num').value
        ){
        alert("Не заполнены обязательные к заполнению поля!");
        return false;
    }
}


function showImages(p,i,a)
{
	checkRequest();
	if (request) {
		request.onreadystatechange = function()
		{
			if (request.readyState == 4){
				if (request.status == 200){
					response = request.responseText;
					switch (response)
					{
						case 'error':
							alert('Ошибка передачи параметров!');
						break;
						default:
                                                buildImades(response,p,a);
					}
				} else {
					alert('Ошибка работы фотоальбомов.\nПовторите попытку позже или обратитесь к администратору!');
				}
			}
		}
		request.open("GET", p+'?act=images&id='+i+'&idalbum='+a, true);
		request.send();
	}
}

function buildImades(r,p,a)
{
    var ai;
    ai = r.split('$');
    get('preview_img').innerHTML = '<img src="'+ai[0]+'"><img src="'+ai[4]+'"><img src="'+ai[6]+'">';
    
    var t = '<table class="preview_img"><tr><td class="lnk">'
    if(ai[3]){
        t += '<a href="'+ai[3]+'"><img onclick="showImages(\''+p+'\', '+ai[3]+', '+a+'); return false;" src="/templates/pictures/ar_prev.gif" alt="Предыдующая" title="Предыдующая"></a>';
    }else{
        t += '<img src="/templates/pictures/1x1.gif" width="19" alt="">';
    }
    if(ai[5]){
        t += '</td><td><a href="'+ai[5]+'"><img onclick="showImages(\''+p+'\', '+ai[5]+', '+a+'); return false;" alt="Следующая" title="Следующая" src="'+ai[0]+'"></td><td class="lnk"></a>'
        t += '<a href="'+ai[5]+'"><img onclick="showImages(\''+p+'\', '+ai[5]+', '+a+'); return false;" src="/templates/pictures/ar_next.gif" alt="Следующая" title="Следующая"></a>';
    }else{
        t += '</td><td><img src="'+ai[0]+'"></td><td class="lnk">'
        t += '<img src="/templates/pictures/1x1.gif" width="19" alt="">';
    }
    t += '</td></tr></table>'

    showDialog(t, (ai[1]*1+50) , ai[2]*1 );
}


function showDialog(html, w,  h)
{
    var wt = getClientWidth();
    var ht = getClientHeight();
    h += 84;
    w += 84;
    var L = Math.ceil((wt-w)/2);
    var T = Math.ceil((ht-h)/2)+eval(document.documentElement.scrollTop||document.body.scrollTop);

    if(L<0) L = 0;
    if(T<0) T = 0;

    parent.get('place_dialog').innerHTML = '<div id="fon_dialog" onclick="closeDialog();"></div><div id="dialog"><table id="dialog_content">\
    <tr><td class="dc" id="dc1">&nbsp;</td><td class="dc">&nbsp;</td><td class="dc" id="dc2">&nbsp;</td></tr>\
    <tr><td class="dc">&nbsp;</td><td id="pdc"><div class="rel"><img id="dclose" onclick="closeDialog();" title="Закрыть" src="/templates/pictures/close.gif">\
    ' + html + '</div></td><td class="dc">&nbsp;</td></tr>\
    <tr><td class="dc" id="dc3">&nbsp;</td><td class="dc">&nbsp;</td><td class="dc" id="dc4">&nbsp;</td></tr></table></div>';

    get('dialog').style.left = L + 'px';
    get('dialog').style.top = T + 'px';

    get('fon_dialog').style.width = wt + 'px';
    get('fon_dialog').style.height = eval(document.documentElement.scrollHeight||document.body.scrollHeight) + 'px';
}


function closeDialog()
{
    parent.get('place_dialog').innerHTML = '';
}

function resizeBody()
{
    var h = getClientHeight();
    if( h >= get('main_table').offsetHeight ) {
        get('in_place_content').style.height = (h - 641) + 'px';
    }
}

addArrayLoad('resizeBody()');

window.onload = doArrayLoad;
window.onresize = doArrayLoad;
