/**
 * Frontend common JS styles
 *
 * Project: Tender.pro
 * File: common.js
 *
 * @author Dima <dima@altsolution.net>
 * @package css
 * note: table
 */

function fixPNG(element)
{
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
	{
		var src;
		
		if ((element.tagName=='IMG') || (element.tagName=='INPUT'))
		{
			if (/\.png$/.test(element.src))
			{
				src = element.src;
				element.src = "/images/blank.gif";
			}
		}
		else
		{
			src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i)
			if (src)
			{
				src = src[1];
				element.runtimeStyle.backgroundImage="none";
			}
		}
		
		if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
	}
}

function example(elem, text) {
	document.formName.elements[elem].value = text;
}

function print_ssl () {
	aa = location.host;
	
	if (location.protocol == 'https:')
		document.write('<!--noindex--><a rel="nofollow" href="http://' + aa + '/login-no-ssl.shtml" title="без SSL">без <strong>SSL</strong></a><!--/noindex-->');
	else
		document.write('<!--noindex--><a rel="nofollow" href="https://' + aa + '/" title="с SSL">с <strong>SSL</strong></a><!--/noindex-->');
}

function protected_send_form() {
	hideSelect();
	window.onunload = hide_progress;

	var el = document.getElementById('Progress');
	el.style.display = 'block';
	for ( var idx=0; idx < document.forms.length; idx++ ) {
		var r = document.forms[idx].send_form;
		if (r) {
			document.forms[idx].send_form.disabled=true;
		}
	}
}

function hideSelect() {
	bIsIe = (navigator.userAgent.indexOf('MSIE') > 0 ? true : false);
	if (bIsIe) {
		for ( var idx=0; idx < document.forms.length; idx++ ) {
			for (i=0; i < document.forms[idx].elements.length; i++) {
				if (document.forms[idx].elements[i].type == 'select-one') {
					document.forms[idx].elements[i].style.visibility = 'hidden';
				}
			}
		}
	}
}

function hide_progress() {
	if (document.getElementById('Progress'))
		document.getElementById('Progress').style.display = 'none';
}

function resetinputs(pointer) {
        if (pointer) {
            pointer.reset();
        }
        return false;
}

function start() {
	var HP = document.getElementById("HomePage");
	if (navigator.appName == "Microsoft Internet Explorer") {
		var HP = document.getElementById("HomePage");
		HP.style.behavior = 'url(#default#homepage)';
		HP.setHomePage('http://Tender.Pro/');
	} else {
		window.location.href = HP.href; 
	}
}

function help(i) {
	if (i) {
		window.open("/wk/index.php?oldid="+i);	
	}
	else {
		window.open("/wk/");	
	}
}

jQuery(document).bind('keydown', 'Ctrl+h',runhelplink);
jQuery(document).bind('keydown', 'f1',runhelplink);
	
function runhelplink() {
	window.open($('.blockStat a[title=Справка]').attr('href'));
	return false; 
}

function themsable() {
	if ( (document.getElementById('txtFilter').value == '') || (document.getElementById('txtFilter').value == ' ') ) {
		alert('Укажите тему обсуждения');
		return false;	
	}
}

function setCookie(name, value, options)
{
	options = options || {};
	if (value === null) {
		value = '';
		options.expires = -1;
	}
	var expires = '';
	if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
		var date;
		if (typeof options.expires == 'number') {
			date = new Date();
			date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
		} else {
			date = options.expires;
		}
		expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
	}

	// CAUTION: Needed to parenthesize options.path and options.domain
	// in the following expressions, otherwise they evaluate to undefined
	// in the packed version for some reason...
	var path = options.path ? '; path=' + (options.path) : '';
	var domain = options.domain ? '; domain=' + (options.domain) : '';
	var secure = options.secure ? '; secure' : '';
	document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
}

function getCookie (name)
{
	var cookieValue = null;
	if (document.cookie && document.cookie != '') {
		var cookies = document.cookie.split(';');
		for (var i = 0; i < cookies.length; i++) {
			var cookie = trim(cookies[i]);
			// Does this cookie string begin with the name we want?
			if (cookie.substring(0, name.length + 1) == (name + '=')) {
				cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
				break;
			}
		}
	}
	return cookieValue;
}

function trim ( text )
{
	return (text || "").replace( /^\s+|\s+$/g, "" );
}

function set_current_tab (cur_id)
{
	// текущие и таб который нужно подсветить
	var old = $('#search_menu a.active');
	var cur = $('#search_menu a#'+cur_id);
	
	// текущая и новая формы
	var old_form = $('form.'+old.attr('id'));
	var cur_form = $('form.'+cur.attr('id'));
	
	// форма которая отображается
	var form = cur.parents('form');
	var input = $('#form_filter');
	
	if ((cur.length > 0) && !cur.hasClass('active'))
	{
		var html = $('strong', old).html();
		if (html)
			old.html(html);
//		cur.html('<strong>'+cur.html()+'</strong>');
		cur.addClass ('active');
		if (old.length > 0)
			old.removeClass ('active');
		
		if (input.val()=='' || input.val() == old_form.attr('title')) input.val (cur_form.attr('title'));
		
		var href = cur.attr('href');
		href = href.replace (/\?(.)*/, '');
		form.attr('action', href);
		setCookie ('tab', cur_id, {path: '/'});
	}
	
}

function checkfield(){
	var flag = false;
	$('#SearchBlock form').each(function(){	
		if ($('#form_filter').val() == $(this).attr('title')) flag = true; 	
	});
	if (flag) {
		$('#SearchBlock a#form_clear').css('visibility','hidden'); 
	} else {
		$('#SearchBlock a#form_clear').css('visibility','visible');
	}	
}

function load_menu() {
	var tab = getCookie('tab');
	if (tab)
		set_current_tab (tab);
	
	$('#search_menu a').click(function(){
		var filter = $('#form_filter').val();
		var cur = $('#search_menu a.active');
		var form = $('form.'+cur.attr('id'));
		
		if (filter && (filter != form.attr('title'))) {
		set_current_tab ($(this).attr('id'));
		checkfield();
		$('#form_search').submit();
		return false;
		} 	
	});
	
	$('#form_search').submit(function(){
		var filter = $('#form_filter').val();
		var cur = $('#search_menu a.active');
		var form = $('form.'+cur.attr('id'));
		
		setCookie ('value', filter, {path: '/'});
		if (filter && (filter != form.attr('title')))
		{
			$('input.form_filter', form).val(filter);
			form.submit();
			return false;
		}
		
		var href = $('#search_menu a.active').attr('href');
		href = href.replace (/^(.)*?\?/, '');
		var parts = href.split('&');
		
		for (var i = 0; i < parts.length; i++)
		{
			var param = parts[i].split('=');
			var input = $('<input type="hidden" />').attr('name', param[0]);
			if (param.length > 1)
				input.val(param[1]);
			input.prependTo ($(this));
		}
		
		$('#form_filter').val('');
	});
	
	$('#form_filter').focus(function(){
		var cur = $('#search_menu a.active');
		var form = $('form.'+cur.attr('id'));
		
		if ($(this).val() == form.attr('title'))
			$(this).val('');
	});
	
	var cur = $('#search_menu a.active');
	var form = $('form.'+cur.attr('id'));
	var input = $('#form_filter');
	if ( getCookie('value') && ((input.val() == '') || (input.val() == form.attr('title'))))
		input.val(getCookie('value'));
}

var timer = 0;

$(document).ready(function(){
	var cur = $('#search_menu a.active');
	var form = $('form.'+cur.attr('id'));
	var input = $('#form_filter');
	
	if (input.val() == '') 
		input.val(form.attr('title'));	
checkfield();

$('#SearchBlock #form_filter').keyup(function(){  
		$('#SearchBlock #form_filter').val() != '' ? $('#SearchBlock a#form_clear').css('visibility','visible') : $('#SearchBlock a#form_clear').css('visibility','hidden');	
	});

	$('#SearchBlock a#form_clear').live('click', function() {
		$('#SearchBlock #form_filter').val('');	
		$('#SearchBlock a#form_clear').css('visibility','hidden');
		return false;
	});
	
});

var $buoop = {
	vs:{i:7,f:0,o:0.0,s:0,n:0},  
	reminder: 336, 
	test: false,                    
	newwindow: true               
}
$buoop.ol = window.onload; 
window.onload=function(){

 // тригер наличия включенного джс
 document.body.id = 'js';
 
 if ($buoop.ol) $buoop.ol(); 
 var e = document.createElement("script"); 
 e.setAttribute("type", "text/javascript"); 
 e.setAttribute("src", "/js/update.js"); 
 document.body.appendChild(e); 
} 

function size_ok(sIdTextString, iStringLen) {
	if (document.getElementById(sIdTextString).value.length > iStringLen) {
		alert('Текст не может быть длинее '+iStringLen+' символов!');
		return false;
	}
	return true;
}
