// preload images

var myimages = new Array();
function preloadimages(){
	for (i = 0; i < preloadimages.arguments.length; i++) {
		myimages[i] = new Image();
		myimages[i].src = preloadimages.arguments[i];
	}
}

preloadimages(
	'/common/img/calendar_2.png',
	'/common/img/input_active.gif',
	'/common/img/ico_open_up_black.gif',
	'/common/img/reg_input_active.gif',
	'/common/img/reg_textarea_active.png',
	'/common/img/short_input_bg_active.gif',
	'/common/img/alert_small.png',
	'/common/img/input_passive_4sym.png',
	'/common/img/input_active_4sym.png',
	'/common/img/input_short_ext_active.gif',
	'/common/img/input_short_ext_passive.gif',
	'/common/js/jquery/images/date/gui_calendar_bg_top.png',
	'/common/js/jquery/images/date/gui_calendar_bg_bottom.png',
	'/common/img/orange_marker_left.gif',
	'/common/img/orange_marker_right.gif',
	'/common/img/orange_oval.gif',
	'/common/img/orange_close.gif',
	'/common/img/orange_square_border.gif',
	'/common/img/orange_square.gif',
	'/common/img/way-1-a.gif',
	'/common/img/way-2-a.gif',
	'/common/img/way-3-a.gif',
	'/common/img/way-4-a.gif',
	'/common/img/way-5-a.gif',
	'/common/img/m-payments-a.gif',
	'/common/img/m-payments-h.gif',
	'/common/img/input_left_bg.gif',
	'/common/img/input_right_bg.gif',
	'/common/img/input_center_bg.gif',
	'/common/img/input_left_bg_active.gif',
	'/common/img/input_right_bg_active.gif',
	'/common/img/input_center_bg_active.gif',
	'/common/img/corners_active.png'
);

// form inputs style & menu of comments

function input_focus(elem, field, node_name){
	if (document.getElementById('comment_0')){
	document.getElementById('comment_0').style.display = 'none';
	}
	border = document.getElementById(elem);
	pref = (typeof(node_name)!='undefined')? (node_name+'_'):'';
	border.className = (/passive/.test(border.className))? border.className.replace(pref+'passive', pref+'active') : border.className ;
	
	if (document.getElementById(field)) {
		note = document.getElementById(field);
		note.className = (/hidden/.test(note.className))? note.className.replace(/hidden/,'showed') : note.className ;
	}

	// подсказки внизу страницы должны перекрывать ссылки в подвале
	if(document.getElementById('footer_menu'))
		document.getElementById('footer_menu').style.zIndex = '1';
}

function input_blur(elem, field, node_name){
	if (document.getElementById('comment_0')){
	document.getElementById('comment_0').style.display = 'block';
	}

	border = document.getElementById(elem);
	pref = (typeof(node_name)!='undefined')? (node_name+'_'):'';
	border.className = (/active/.test(border.className))? border.className.replace(pref+'active', pref+'passive') : border.className ;

	if (document.getElementById(field)) {
		note = document.getElementById(field);
		note.className = (/showed/.test(note.className))? note.className.replace(/showed/,'hidden') : note.className;
	}

	// activate footer
	fff = document.getElementById('footer_menu');
	if(fff)fff.style.zIndex = '11';
}

// onmouseover, onmoouse out для чекбоксов, радиокнопок и файл инпутов, чтобы подсказку выводить при наведении можно было
	function tiptoolover(it){
		$('input.text, .textarea, .select').blur();
		$('#'+it).show();
	}

	function tiptoolout(it){
		$('#'+it).hide();
	}

function blue_dotted_td_open (td_id, tarea_id) {
	var blue_dotted_td = 'blue_dotted_td_' + td_id + '_' + tarea_id;
	$("#" + blue_dotted_td).addClass("blue_dotted_td");
}
function blue_dotted_td_close (td_id, tarea_id) {
	var blue_dotted_td = 'blue_dotted_td_' + td_id + '_' + tarea_id;
	$("#" + blue_dotted_td).removeClass("blue_dotted_td");
}
//	GUI table drop down


 

function gui_drop_down(num, el){
	link = document.getElementById('more_'+num);
	head = el;
	info = document.getElementById('more_info_'+num);
	
	// check comment gradient
	gcut = 0;
	if (document.getElementById('gui_cut_' + num)) {
		gcutbg = document.getElementById('gui_cut_' + num);
		gcut = 1;
	}
	
	// change indicator
	link.className = (/passive/.test(link.className)) ? link.className.replace(/passive/, 'active') : ((/active/.test(link.className)) ? link.className.replace(/active/, 'passive') : link.className);

	// open-close info
	if (/closed/.test(info.className) && /head_closed/.test(head.className)) {
		
		$('#more_info_'+num).slideDown(700);
		info.className = info.className.replace(/closed/, 'opened');
		head.className = head.className.replace(/head_closed/, 'head_opened');
		/*if (gcut){
			gcutbg.className='';
			gcutbg.getElementsByTagName('div')[0].className='cut_bg_off';
		}
		*/
	}
	else if (/opened/.test(info.className) && /head_opened/.test(head.className)) {
		$('#more_info_'+num).slideUp(350);
		head.className = head.className.replace(/head_opened/, 'head_closed');
		info.className = info.className.replace(/opened/, 'closed');
		/*
		if (gcut){
			gcutbg.className='cut';
			gcutbg.getElementsByTagName('div')[0].className='cut_bg';
		}
		*/
	}
}

// GUI bock drop down

function gui_ddrop(ID,el){
	block = document.getElementById(ID);
	block.className = (/closed/.test(block.className)) ? block.className.replace('closed', 'opened'):(/opened/.test(block.className))?block.className.replace('opened','closed'):block.className;
}


/* faq hide'n'show */
function hns(id){
  el = document.getElementById(id);
	if(el.style.display == '' || el.style.display == 'none'){
	  	$(el).slideDown(300);
		}
		else{
	  	$(el).slideUp(300);
	}
}
/* /faq hide'n'show */

// ui popup

function ui_popup(el){
	popup = $("#" + el)
	if (document.getElementById(el).style.display=='none'){
		popup.fadeIn(400)
	}
	else{
		popup.fadeOut(400);
		if( $('#maincss') || $('#maincss_merchant') ){
			$('#maincss').attr("href", ""); 
			$('#maincss_merchant').attr("href", "");
			$('#maincss').attr("media", ""); 	
			$('#maincss_merchant').attr("media", ""); 
		}
	}
}

// ui popup

function ui_popup_css(src_popup, mode){
	jQuery("#" + src_popup).appendTo("body");  
	if (document.getElementById(src_popup).style.display=='none'){	
	$("#" + src_popup).fadeIn(400);
	}else{
	$("#" + src_popup).fadeOut(400);	 
	}
	
	$('#maincss').attr("href", "/common/css/print_secret.css"); 
  $('#maincss_merchant').attr("href", "/common/css/merchant/print_popup.css");
	$('#maincss').attr("media", "print"); 	
  $('#maincss_merchant').attr("media", "print"); 
	if (mode == 1) 	 window.print();	 		
}

// Если кликнуть по ячейке, в которой input[text], то фокус автоматически попадет в input[text]

function labelform(){
	formz = $('.form .input');
	for(i=0;i<formz.length;i++){
		formz[i].onclick = function(){
			inputz = this.getElementsByTagName('input');
			for (j=0;j<inputz.length;j++){
				if (inputz[j].type=='text' || inputz[j].type=='password'){
					inputz[j].focus();
				}
			}
		}
	}
}
 
// для элементов всех форм на странице добавить обработки кнопки Enter, чтобы отправлять без input[submit], кроме формы заключения договора организации 'first.vtmpl'
function enterForm(){
	formz = $('form');
	// проходим по всем формам на странице
	for (i=0;i<formz.length;i++){
		inputz = formz[i].getElementsByTagName('input');
		// проверяем наличие ID='do_not_submit_on_enter'
		miss = 0;
		if(/do_not_submit_on_enter/.test(formz[i].id))
			miss = 1;
		else
			miss = 0;
		// проверяем наличие input[submit] или input[image] у формы Или ID='do_not_submit_on_enter'
		flag = 1;
		for(k=0; k<inputz.length; k++){
			if(inputz[k].type == 'submit' || inputz[k].type == 'image' || miss){
				flag = 0;
			}
		}

		if(flag){
				fm = formz[i];
			/* если нет input[submit] или input[image], тогда добавляем обработчики на все input
			for(j=0; j<inputz.length; j++){

					inputz[j].onkeyup = function(event){
						event = event || window.event;
						if (event.keyCode == 13){
							fm.submit();
						}
					}
				}	*/
			// а я просто добавлю скрытый input type="submit" в эти формы, вместо onkeyup обработчика, который обламывает все остальные onkeyup в форме
				
			sb = document.createElement('input');
			sb.type = 'submit';
			sb.style.display = 'none';
			sb.className = 'formSenderOnEnter';
			fm.appendChild(sb);
		}
	}
}

// супер фильтр для цифр 
	function digitsFilter(e)	{
		// Make sure to use event.charCode if available
		var key = (typeof e.charCode == 'undefined' ? e.keyCode : e.charCode);

		// Ignore special keys
		if (e.ctrlKey || e.altKey || key < 32)
			return true;

		key = String.fromCharCode(key);
		return /\d/.test(key);
	}

// и автофокус-покус для Input с заданным maxlength на другой input с заданным ID

	function stepFilter(it, toElem){
		if(it.value.length == it.maxLength && document.getElementById(toElem)){
			document.getElementById(toElem).focus();
			document.getElementById(toElem).select();
		}
	}


$(document).ready(function(){
	enterForm();
	if($('.form')){
		labelform();
	}
	
/*-----append_flash-ugolka-----------*/	
 $("#u_g").appendTo("body"); 	
 $('#u_g').css( "position", "absolute" ); 
 $('#u_g').css( "top", "0" ); 
 $('#u_g').css( "right", "0" ); 

/* hover emulation for IE6 */  
  if($.browser.msie && jQuery.browser.version < "7.0"){
    $(".payments .unactive, .smallpayments .unactive").hover(
      function () {
        $(this).addClass(" hover");
      }, 
      function () {
        $(this).removeClass(" hover");
      }
    );
  }
/* /hover emulation for IE6 */
/* last-child emulation for all */
 $(".payments tr td:last-child").addClass(" last-child");
/* /last-child emulation for all */
});

/* print styles */

function printme(){
  //jQuery('a.close_left, a.close').hide();
  window.print();
  }

/* /print styles */
 


/*----------разделение браузеров----------*/



var cssFix = function(){
  var u = navigator.userAgent.toLowerCase(),
  addClass = function(el,val){
    if(!el.className) {
      el.className = val;
    } else {
      var newCl = el.className;
      newCl+=(" "+val);
      el.className = newCl;
    }
  },
  is = function(t){return (u.indexOf(t)!=-1)};
  addClass(document.getElementsByTagName('html')[0],[
    (!(/opera|webtv/i.test(u))&&/msie (\d)/.test(u))?('ie ie'+RegExp.$1)
      :is('firefox/2')?'gecko ff2'
      :is('firefox/3')?'gecko ff3'
      :is('gecko/')?'gecko'
      :is('opera/9')?'opera opera9':/opera (\d)/.test(u)?'opera opera'+RegExp.$1
	  :is('opera/10')?'opera opera10':/opera (\d)/.test(u)?'opera opera'+RegExp.$1
      :is('konqueror')?'konqueror'
      :is('applewebkit/')?'webkit safari'
      :is('mozilla/')?'gecko':'',
    (is('x11')||is('linux'))?' linux'
      :is('mac')?' mac'
      :is('win')?' win':''
  ].join(" "));
}();


function print_inv(param) 
	{ 
	if (!param){
		jQuery("#ui_popup_22").appendTo("body");  
	}
	else {
		print_inv = document.createElement('div');
		print_inv.innerHTML = jQuery(".print_inv").html();
		document.body.appendChild(print_inv);
	}

 $('#maincss').attr("href", "/common/css/print_secret.css"); 
 $('#maincss').attr("media", "print"); 
   window.print();	  
}


/*-----append_flash-ugolka-----------*/	 

function sizeUp() {
 $('#u_g').css( "height", "650px" ); 
 $('#u_g').css( "z-index", "111" );  
}

function sizeDown() {
 $('#u_g').css( "height", "100px" ); 
 $('#u_g').css( "z-index", "1" );  
}

			
									