var Cookie = {
	get: function ( name ) {
		var start = document.cookie.indexOf( name + '=' );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ';', len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	},
	set: function ( name, value, expires, path, domain, secure ) {
		var today = new Date(); today.setTime( today.getTime() );
		if ( expires ) {expires = expires * 1000 * 60 * 60 * 24;}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name+'='+escape( value ) +
			( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) +
			( ( path ) ? ';path=' + path : '; path=/' ) +
			( ( domain ) ? ';domain=' + domain : '' ) +
			( ( secure ) ? ';secure' : '' );
	},
	del: function ( name, path, domain ) {
		if ( Cookie.get( name ) ){
			 document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : ';path=/') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
		}
	}
}
function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {elm.addEventListener(evType, fn, useCapture);return true;}
     else if (elm.attachEvent) {var r = elm.attachEvent('on' + evType, fn);return r;}
     else {elm['on' + evType] = fn;}
}

function removeEvent( elm, evType, fn ) {
	if (elm.detachEvent) {return elm.detachEvent('on' + evType, fn);}
	  else {elm.removeEventListener(evType, fn, false);return true;}
}
function setFilter(frm){
	frm.action = location.pathname;
	frm.submit();
}
function setVal(prm,inp){
	if(inp.type=='checkbox' && prm){ inp.checked=true;}
	 else if(inp.type=='select-one' && prm){inp.value=prm;}
}
function addToFav(n,obj){
	var f;
	new Ajax.Request(favUrl, {
	  method: 'post',
	  parameters: {opr:'favorites', id: n, r: Math.random() },
	  onSuccess: function(transport) {
		alert('Товар добавлен к выбраным моделям');
		if(favUrl && window.location.href.indexOf(favUrl)>=0){window.location.reload();}
		if(false!=(f=$$('div.mod'))){f[0].show();}
		if(!obj){
			var ls = $$('td.cattd5>a');if(ls)ls.each(function(s){
				if(new RegExp(n).test(s.readAttribute('onclick'))){obj=s;return;}
			})
		}
		if(obj) obj.replace('<a onclick="GoToFav()"><span class="sel" style="color: #CC0000;">Посмотреть избранное</span></a>');
	  }
	});
}
function DelFav(n){
	var f;
	new Ajax.Request(favUrl, {
	  method: 'post',
	  parameters: {opr:'delfavorites', id: n, r: Math.random() },
	  onSuccess: function(transport) {
	  	location.reload();
	  }
	});
}
function GoToFav(){
	location.href = favUrl;
}
function sendForm(frm){
    var param = $(frm).serialize();
	new Ajax.Request(frm.action, {
	  method: 'post',
	  parameters: param,
	  onSuccess: function(transport) {
		alert('Товар добавлены в заказ');
		window.location.reload();
		//if(false!=(f=$$('div.mod'))){f[0].show();}
	  }
	});
	return false;
}
function delGood(id){
	var parameters = {opr:'add2card'};
	parameters['toCard['+id+']'] = null;
	new Ajax.Request(location.href, {
	  method: 'post',
	  parameters: parameters,
	  onSuccess: function(transport) {
		window.location.reload();
	  }
	});
}
function setColor(cName,cUrl){
	$('Bcolor').src=cUrl;
    $('Bcolor').title=cName;
}
