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 loading_overlay(id) {
	var top = findPosY(id);
	var left = findPosX(id);
	var width = ge(id).offsetWidth;
	var height = ge(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 += '</div>';

	ge(id).innerHTML = ge(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.');
        }
    }
}

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

function submit_form_g () {
	if(parent.form_count_g + 1 == parent.submit_form_num) {
		req(parent.urlis, function(http_request) {ge('pic_cont').innerHTML = http_request.responseText});
	} 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_g).style.display != 'none') {
		parent.form_count_g++;
	}
	ge('pic_div_'+form_count_g).style.display = 'block';
	if(form_count_g == 20) {
		ge('add_more_span').innerHTML = 'Input limit';
	}
}
