/*
Author: Arturas Piksrys(arturas@ring.lt)
*/
function changeImg(image){
				
	var sparr = image.src.split("/");
	if(sparr[sparr.length-1] == "overlay.gif"){
		image.src = '/images/trasparent.gif';
	}
	else{
		image.src = '/images/overlay.gif';
	}
}

function changeBubbles(){
	if(Prototype.Browser.IE) return;
	var top_menu_ul = $('top_menu');
	var top_menu_imgs = top_menu_ul.getElementsByTagName('img');
	for(var i = 0; i < top_menu_imgs.length; i++){
		top_menu_imgs[i].src = '/images/' + top_menu_imgs[i].className + '.png';
	}
}	

function leftMenuTextPositioning(){
	var left_menu = $('left_menu');
	if(!left_menu) return;
	var as = left_menu.getElementsByTagName('a');
	for(var i = 0; i < as.length; i++){
		if(as[i].innerHTML.length < 22 && as[i].className != 'b'){
			as[i].style.top = "6px";
		}
	}
}

function onloadEvents(){
	changeBubbles();
	leftMenuTextPositioning();
}
function onloadEventsCh(){
    changeBubbles();
    leftMenuTextPositioning();
    var Bank = new BankLoansCalculator(['hansa','seb','sampo','nordea']);
    
    if($('bank_loan_calculator')){
        Event.observe('debt_price', 'keyup',function(event){Bank.minCalculate()},false);
        Event.observe('debt_term', 'change',function(event){Bank.minCalculate()},false);
        Event.observe('start_debt_payment', 'change',function(event){Bank.minCalculate()},false);
        Bank.minCalculate();
    }
    
    if($('bank_calculator_button')){
        Event.observe(document, 'load', function(event){Bank.countStartingFee();}, false);
        Event.observe('bank_calculator_button', 'click', function(event){Bank.detailedCalculate()},false);
        Event.observe('request_to_bank', 'click', function(event){ Bank.validateSenderData()},false);
    }
}



function showProjectInfo(what){
    $('data').innerHTML = $(what).innerHTML;
}

function doQuickSearch(form){
    var object_type = $('offer_t').options[$('offer_t').selectedIndex].value;
    //if(object_type == 'flats') return true;
    form.action = form.action.replace(/butai/g, object_type)
    return true;
}

function openZuvintas(){
	/*var rnd = (Math.round((Math.random()*999)+1));
	var width=995;
	var height=715;
	var top=Math.round((screen.height-height)/2);
	var left=Math.round((screen.width-width)/2);
	window.open('http://www2.inreal.lt/lt.php/24726;zuvintas;1', "w"+rnd, "top="+top+",left="+left+",width="+width+",height="+height+",buttons=no,scrollbars=yes,location=no,menubar=no,resizable=yes,status=no,directories=no,toolbar=no");
	*/
	return false;
}

function newsletterHiddenFields(what)
{
    var type = ['flat', 'house', 'commercial', 'site'];
    for(var i = 0; i < type.length; i++){
        $(type[i] + '_title').style.display = 'none';
        $(type[i] + '_param').style.display = 'none';
    }
    
    $(what + '_title').style.display = 'block';
    $(what + '_param').style.display = 'block'
}

function municipalityChange(select)
{
    var municipality_id = parseInt(select.options[select.selectedIndex].value);
    new Ajax.Updater('city_id', '/main/rcdb_cities/' + municipality_id, {asynchronous:true, evalScripts:true}); 
   
    if(municipality_id == 461){
		cityChange(1);
	}
	else if(municipality_id == 43){
		cityChange(6);
	}
	else if(municipality_id == 112){
		cityChange(7);
	}
    
}

function cityChange(select){
    if(typeof select == 'object'){
        var city_id = parseInt(select.options[select.selectedIndex].value);
    }
    else{
        var city_id = parseInt(select);
    }
    new Ajax.Updater('block_id', '/main/rcdb_blocks/' + city_id, {asynchronous:true, evalScripts:true}); 
}

function showLeft(id){
    var img_b = $('b_' + id);
    var childs = $('child_' + id);
    
    if(childs.style.display == 'block'){
        img_b.src = '/images/left_menu/gr.jpg';
        childs.style.display = 'none';
    }
    else{
        img_b.src = '/images/left_menu/or.jpg';
        childs.style.display = 'block';      
    }
}

function debug(object){
        var result = "Debug info:\n";
        for(i in object){
            result += i + ' : ' + object[i] + "\n";
        }
        
        return result;
}

function filterChangeCity(select, type){
    var url = '/main/update_city_options/';
    url += select.options[select.selectedIndex].value;
    url += '?type=' + type;
    new Ajax.Updater('city', url);
    
}

function filterChangeBlock(select, type){
    var url = '/main/update_block_options/';
    url += select.options[select.selectedIndex].value;
    url += '?type=' + type;
	new Ajax.Updater('block', url);
}

function checkNumeric( field, max_length ) {
	var temp_value = new String();
	var regexp = /#+/i
	temp_value = field.value;
	temp_value = temp_value.replace( regexp, '' );
	if( max_length > 0 ) {
		temp_value = temp_value.match( '[0-9]{0,' + max_length.toString() + '}' );
	}
	if( field.value != temp_value ) {
		field.value = temp_value;
	}
	return false;
}

function checkValue(field_id, length){
    if($F(field_id).length != length) return false;
    return true;
}

var inreal_contact_mail = function(prefix){
  document.write('<a href="mailto:' + prefix + '@inreal.lt" style="color:#434343;">' + prefix + '@inreal.lt</a>');
}

var set_object_stats = function(uni_id){
    new Ajax.Request('/main/set_object_stats/' + uni_id);
}

function validateForm(form){
	var i = 0;
	$$('.required').each(function(input){
		if(input.value.blank()){
			i++;
			Element.show('calc_error');
	        Effect.Pulsate('calc_error');
			return false;
		}
	})
	
	if(i) return false;
	else form.submit();
}


