// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

//var catlink = "/categories/browse?own=1";

var curent_table_row = null;

function refresh_page() {
	//alert(document.getElementsByClassName("inline-adapter").length);
	//alert(document.getElementsByClassName("create-view").length);
	//alert(document.getElementsByClassName("update-view").length);
	//alert(document.getElementById("backend__item_requests-search-input").value);
	s1 = document.getElementById("backend__item_requests-search-input");
	s2 = document.getElementById("backend__trial_requests-search-input");
	s3 = document.getElementById("backend__log_records-search-input");
	if((s1 && s1.value != "Meklēšanas teksts" && s1.value != "")) {
		//alert("no");
		return false;
	}
	if((s2 && s2.value != "Meklēšanas teksts" && s2.value != "")) {
		//alert("no");
		return false;
	}
	if((s3 && s3.value != "Meklēšanas teksts" && s3.value != "")) {
		//alert("no");
		return false;
	}
	
	if (document.getElementsByClassName("inline-adapter").length > 0 
		|| document.getElementsByClassName("create-view").length > 0
		|| document.getElementsByClassName("update-view").lenght > 0
		) {
		//alert("no");
		return false;
	}
	else {
		return true;
	}
	return true;
}

function set_curent_table_row(element) {
  if (curent_table_row) {
    if($(curent_table_row)) {
      $(curent_table_row).removeClassName('current_active');
    }
  }
  curent_table_row = element.id;
  if($(curent_table_row)) {
    $(curent_table_row).addClassName('current_active');
  }
}

function test(e) {
  if (e.keyCode == 40) {
    if (curent_table_row) {
      if ($(curent_table_row).next('tr')) {
        $(curent_table_row).removeClassName('current_active');
        $(curent_table_row).next('tr').getElementsByClassName('focuser')[0].focus();
        $(curent_table_row).next('tr').addClassName('current_active');
        curent_table_row = $(curent_table_row).next('tr').id
      }
    }
  }
  if (e.keyCode == 38) {
    if (curent_table_row) {
      if ($(curent_table_row).previous('tr')) {
        $(curent_table_row).removeClassName('current_active');
        $(curent_table_row).previous('tr').getElementsByClassName('focuser')[0].focus();
        $(curent_table_row).previous('tr').addClassName('current_active');
        curent_table_row = $(curent_table_row).previous('tr').id
      }
    }
  }
  //alert( 'keycode ' + e.keyCode + ' shiftkey ' + e.shiftkey );
} 
Event.observe(window, 'load', function() {
    if(navigator.userAgent.indexOf("MSIE ") != -1) {
      if(ibox_text != "")
        iBox.show(ibox_text,'',{});
    }
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if ((theInput.type == 'text' || theInput.type == 'password')&& (theInput.className.match(/\blogin_input\b/) || theInput.className.match(/\megaselect\b/)) ) {  
            /* Add event handlers */
            Event.observe(theInput, 'focus', clearDefaultText);
            Event.observe(theInput, 'blur', replaceDefaultText);          
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }  
});

function switch_cat_link() {
	//alert(catlink);
	if(catlink == "/categories/browse") {
		catlink = "/categories/browsex";
	}
	else {
		catlink = "/categories/browse";
	}
	//alert(catlink);
}

function switch_cat_select() {
	if($('show_only_own_categories')) {
		if($('show_only_own_categories').checked) {
			$('cat_all').hide();
			$('cat_own').show();
		}
		else {
			$('cat_own').hide();
			$('cat_all').show();
		}
	}
}

function switch_aditional_field(filter, show_field) {
	if ($(filter).value == show_field) {
		$('aditional_field_'+filter).show();
	}
	else {
		$('aditional_field_'+filter).hide();
	}
	
	
	
	if($('show_only_own_categories')) {
		if($('show_only_own_categories').checked) {
			$('cat_all').hide();
			$('cat_own').show();
		}
		else {
			$('cat_own').hide();
			$('cat_all').show();
		}
	}
}



function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}

function switch_body_type(legal) {
	//$('id_code_field').style.display 	= legal ? 'none' : 'block';
	if(legal) {
    $('title_field').show();
    $('reg_nr_field').show()
  }
  else {
    $('title_field').hide();
    $('reg_nr_field').hide();
  }
}

function switch_form(legal) {
	switch_body_type(legal);
}


function getMultiple(ob) {
	ob = $(ob)
	var arSelected = new Array();
	while (ob.selectedIndex != -1) { 
		//if (ob.selectedIndex != 0) 
			arSelected.push(ob.options[ob.selectedIndex].value); 
		ob.options[ob.selectedIndex].selected = false;
	}
	return arSelected.join(',');
}

function select_filter(filter_name, filter_no) {
  $(filter_name).value = filter_no;
  var filter_images = $(filter_name+"_container").getElementsByClassName('linkimage');
  for (var i = 0; i < filter_images.length; i++) {
    var image = filter_images[i];
      image.src = "/images/unchecked.jpg";
      //alert("aditional_field_"+image.id);
      $("aditional_field_"+image.id).hide();
  }
  $(filter_name+"_"+filter_no).src = "/images/checked.jpg";
  $("aditional_field_"+filter_name+"_"+filter_no).show();
}

function check_dates_and_submit(msg) {
	if($('filter0').value == "4" && $('start_date').value == "" && $('end_date').value == "") {
		iBox.show(msg,'',{});
	}
	else {
		$('js_submit').value = 1;
		document.filtron.submit();
	}
}

function js_reset() {
    $('js_reset').name = "reset";
		$('js_reset').value = 1;
		document.filtron.submit();
}

function slepeni2(pirmais,otrais,tresais,ceturtais) {
	simbols='%';
	masa='ilt';
	//djkfhwkje108
	var svars_robots='.'
	+//asads sadhg3 jkd 2k as280x asdhj d
	ceturtais;
	document.write(tresais+otrais+'@'+pirmais+svars_robots);
}

function slepeni3(pirmais,otrais,tresais,ceturtais) {
	simbols='%';
	masa='ilt';
	//djkfhwkje108
	var svars_robots='.'
	+//asads sadhg3 jkd 2k as280x asdhj d
	ceturtais;
	var kkas = tresais+otrais+'@'+pirmais+svars_robots;
	document.write('<a href="mailto:'+kkas+'">'+kkas+"</a>");
}


function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.innerHTML = limitNum - limitField.value.length;
	}
}


function show_hide_mail_image(e) {
    //alert(e.value);
    if(e.value == 3 || e.value == 4) {
        $('mail_imagage_row').show();
    } else {
        $('mail_imagage_row').hide();
    }
}