function form(action, method, target, submit)
{
	if ('undefined' == typeof(method)) {
		method = 'post';
	}
	var targ = '';
	if (false != target && 'undefined' != typeof(target)) {
		targ = ' target="' + target + '"';
	}
	var subm = '';
	if ('undefined' != typeof(submit)) {
		subm = ' onsubmit="'+submit+'" '
	}
	document.write('<form method="'+method+'" action="'+action+'" '+targ+' '+subm+' enctype="multipart/form-data">');
	return;
}

function searchFocus(field, mode)
{
	var word = 'Поиск по каталогу';
	switch(mode) {
		case 0:	if ('' == field.value) {field.value=word;}
			break
		case 1:	if (word == field.value) {field.value='';}
			break;
		default: // 3
			if (word != field.q.value && '' != field.q.value) {return true;}
	}
	return false;
}

/*
var gl = false, gr = false, main = false;
var setwid = function()
{
    if (false == gl) {
        gl = document.getElementById('glft');
        gr = document.getElementById('grgt');
    }

    var w = parseInt(document.body.clientWidth);
    if (w >= 1200) {
        gl.style.width = '30%';
        gr.style.width = '70%';
    } else {
        gl.style.width = '220px';
        gr.style.width = '780px';
    }
        gl.style.width = '30%';
        gr.style.width = '70%';
    return;
}

//window.onload = setwid;
//window.onresize = setwid;

*/



function sendBasket(n)
{

	if ('' == $F('count'+n)) {return false};

	var s = 'sending'+n;
	new Ajax.Request('/basket/request/', {
		method: 'post',
		onCreate: function() {
		         $('req'+n).style.display = 'block';
			$('req'+n).innerHTML = '<p class="js_border">Товар добавляется, пожалуйста подождите...</p>';
			$(s).disabled	= 'disabled';
			$(s).setAttribute('disabled', 'disabled');
		},
		onSuccess: function(transport) {
                          eval(transport.responseText);

			if (1 == $req) {
				$('req'+n).innerHTML = '<p class="js_border">Товар добавлен в корзину. Вы можете сразу перейти к <a href="/basket/" onclick="return winBasket();">оформлению заказа</a></p>';
				$(s).disabled	= '';
				$(s).removeAttribute("disabled");

			} else {
				$('req'+n).innerHTML = '<b>Произошла непредвиденная ошибка<br />Служба временно отключена.</b>';
			}

		},
		onFailure: function() {
			$('req'+n).innerHTML = '<b>Произошла непредвиденная ошибка<br />Служба временно отключена.</b>';
		},
		parameters: {
		         id: $F('id' + n),
			count: $F('count' + n),
			material: $F('matid' + n)
		}
	});
}

function tr(tr, m)
{
	if (m) {
	   tr.style.backgroundColor = '#f1f1f1';
	} else {
	    tr.style.backgroundColor = '';
	}
	return;
}

function pm(o, a)
{
	var x = o.parentNode.firstChild;
	var val = parseInt(x.value);

	if (a) {
		x.value = !isNaN(++val)? val : 1;
	} else {
		x.value = !isNaN(--val)? val : 0;
		if (val <= 0) x.value = 0;
	}
	return;
}

function winBasket()
{
    var winOpen = window.open('/basket/', 'basket', 'width=900,height=600,screenX=100,screenY=100,left=100,top=100,scrollbars,resizible');
    if ('object' != typeof(winOpen)) {
        return true;
    }
    return false;
}

function formFeedBack(t,z)
{
    var obj = document.getElementById('formFeedBack'+t);

    if ('' == obj.style.display || 'none' == obj.style.display) {
        var w = document.body.clientWidth;
        var h = document.body.clientHeight;

        obj.style.top = '50px';
        obj.style.left = ((parseInt( w / 2 )) - (''==t?250:260)) + 'px';
        obj.style.display = 'block';
        if (z) {try {document.getElementById('formFeedBackE').style.display='none';} catch(e){}}
    } else {
        obj.style.display = 'none';
    }

    return;
}

function setMaterial(id, percent, cena, mid)
{
    percent = parseInt(percent);
    var cena = Math.round( (percent / 100) * cena);

    var price = document.getElementById('matid' + id);
    var setp = document.getElementById('setMat' + id);

    price.value = mid;
    if (0 == percent) {
        setp.innerHTML = '';
    } else {
        setp.innerHTML = '(+'+cena+')';
    }

    return;
}

function imgs(n, cnt)
{
    for (i=-1;++i<=cnt;) {
        $('imgs'+i).style.display='none';
    }
    $('imgs'+n).style.display='block';
    return false;
}