var addfeedopen = false;

function ge (name) {
	return document.getElementById(name);
}

function limit_symbols (input_name, output_name, max_symbols) {
	field = document.getElementById(input_name);

	if (field.value.length > max_symbols) {
		field.value = field.value.substring(0, max_symbols);
	}else{
		document.getElementById(output_name).innerHTML = max_symbols - field.value.length;
	}
}

function popup(url, img_x, img_y) {
	window.open(url, '_blank', 'toolbar=0,scrollbars=0,location=0,statusbar=0, width='+img_x+', height='+img_y+',menubar=0,resizable=0,left = 100,top = 100');
	return false;
}

function findPosX(objname) {
    obj = document.getElementById(objname);
	var curleft = 0;
	while (obj.offsetParent) {
		curleft += obj.offsetLeft
		obj = obj.offsetParent;
	}

	return curleft;
}

function findPosY(objname) {
    obj = document.getElementById(objname);
	var curtop = 0;
	while (obj.offsetParent) {
		curtop += obj.offsetTop
		obj = obj.offsetParent;
	}

	return curtop;
}

function curTop(obj){
	toreturn = 0;
	while(obj){
		toreturn += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return toreturn;
}

function curLeft(obj){
	toreturn = 0;
	while(obj){
		toreturn += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return toreturn;
}

function loading_overlay(id) {
	var top = curTop($(id));
	var left = curLeft($(id));
	var width = $(id).offsetWidth;
	var height = $(id).offsetHeight;
	var html='';

	html += '<div style="background: url(/img/loading_overlay.gif); position: absolute; top: '+top+'px; left: '+left+'px; width: '+width+'px; height: '+height+'px;" />';
	html += '<img style="position: relative; top: '+parseInt((height/2)-16)+'px; left: '+parseInt((width/2)-16)+'px;" src="/img/loading_big.gif" alt="Iel─üde.." />';
	html += '</div>';

	$(id).innerHTML = $(id).innerHTML + html;
}

function change_pic (pic, pic_x, pic_y, margin_top) {
	ge('img_cont').src=pic;
	ge('img_cont').width=pic_x;
	ge('img_cont').height=pic_y;
	ge('img_cont').style='margin-top: '+margin_top+'px';
	
}

function req(url, user_function) {
    var http_request = false;

    if (window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) {
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        return false;
    }
    http_request.open('GET', url, true);
    http_request.onreadystatechange = function() { statechange(http_request, user_function); };
    http_request.send(null);

}

function statechange(http_request, user_function) {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
    	    user_function(http_request);
        } else {
            alert('There was a problem with the request.');
        }
    }
}

function ajax_update (url, obj) {
	req(
		url, 
		function(xml) 
			{ 
				ge(obj).innerHTML = xml.responseText;
			}
		);
} 

var form_count_g = 5;
var submit_form_num = 1;
var added_pics = 0;

function submit_form_g () {
	if(parent.form_count_g + 1 == parent.submit_form_num) {
		parent.form_count_g = 5;
		parent.submit_form_num = 1;
		parent.album_id = 'none';
		window.location=urlis;
	} else {
		if(ge('gall_and_submit').style.display != 'none') {
			ge('gall_and_submit').style.display = 'none';
		}
		ge('pic'+submit_form_num).submit();
		ge('pic_diva_'+submit_form_num).style.display = 'none';
		ge('pic_divb_'+submit_form_num).innerHTML = '<div class="uploading">Uploading...</div>';
		ge('pic_divb_'+submit_form_num).style.display = 'block';
	}
}

function add_more_g() {
	parent.form_count_g = 5;
	while(ge('pic_div_'+form_count).style.display != 'none') {
		form_count_g++;
	}
	ge('pic_div_'+form_count).style.display = 'block';
	if(form_count_g == 20) {
		ge('add_more_span').innerHTML = 'Input limit';
	}
}

function addfeedback () {
	if(addfeedopen == false){
		document.getElementById('feedback-leave').style.display = 'block';
		document.getElementById('feedback-link').className = 'sel';
		addfeedopen = true;
	}else{
		document.getElementById('feedback-leave').style.display = 'none';
		document.getElementById('feedback-link').className = '';
		addfeedopen = false;
	}
	return false;
}