var isDOM=document.getElementById; //DOM1 browser (MSIE 5+, Netscape 6, Opera 5+)
var isOpera=isOpera5=window.opera && isDOM; //Opera 5+
var isOpera6=isOpera && window.print; //Opera 6+
var isOpera7=isOpera && document.readyState; //Opera 7+
var isMSIE=document.all && document.all.item && !isOpera; //Microsoft Internet Explorer 4+
var isMSIE5=isDOM && isMSIE; //MSIE 5+
var isNetscape4=document.layers; //Netscape 4.*
var isMozilla=isDOM && navigator.appName=="Netscape"; //Mozilla или Netscape 6.*

var Checked=new Array();
var Checkbox=new Array();

var numCheck = 0;

Checkbox[0]=new Image(11,11);
Checkbox[0].src='/img/button_uncheck.gif';
Checkbox[1]=new Image(11,11);
Checkbox[1].src='/img/button_check.gif';

function CheckboxClicked (id, hid, compare)
{
	var compareTbl = document.getElementById('compare_table');
	if (hid != '') {
		var helement = document.getElementById(hid);
		Checked[id] = helement.value;
	}
	if (Checked[id] == 1) {
		Checked[id] = 0;
		document['check'+id].src = Checkbox[0].src;
		if (hid != '') { helement.value = 0; }
		deleteCookie('check'+id, '/');
		numCheck--;
	}
	else {
		Checked[id] = 1;
		document['check'+id].src = Checkbox[1].src;
		if (hid != '') { helement.value = 1; }
		setCookie('check'+id, 'on', false,'/');
		numCheck++;
	}

	if (compare != false) {
		if ((id != 0)&&(id != -1)&&(id != -2)) {
			var si = 0;
			for (var i=0; i<document.compare_form.elements.length; i++)
				if (document.compare_form.elements[i].type == 'hidden' &&
						Checked[document.compare_form.elements[i].value] && si<3)
					si++
			if(compareTbl)
			if (si > 1 || numCheck > 1) {
				compareTbl.style.display = '';
			}
			else {
				compareTbl.style.display = 'none';
			}
		}
	}
}

function delCheckboxClicked(id) {
	var si = 0;
	var url = '/compare.php?';
	var tmp = new Array();
	numCheck--;

	for (var i=0; i<document.compare_form.elements.length; i++)
		if (document.compare_form.elements[i].type == 'hidden' &&
				document.compare_form.elements[i].value != id && si<160)
		tmp[si++] = 'id[]='
				+ document.compare_form.elements[i].value;

	url = url + tmp.join('&');

	if (si > 0 || numCheck > 0) {
		deleteCookie('check'+id);
		window.location.href = url;
	}
	else
		window.alert("Сравнение возможно для не менее чем 2-х товаров");
}

function CheckChecked() {
	var mycookie=document.cookie;
	var checkid;
	var compareTbl = document.getElementById('compare_table');
	var i=1;
	var num=0;
	var cookiePart;
	while(true){
		cookiePart=unescape(token(mycookie,";",i));
		if(cookiePart=="") break;
		if(token(cookiePart,"=",1) != " check0" && token(cookiePart,"=",2) == "on"){
			num++;
			checkid = checkInt(token(cookiePart,"=",1));
			Checked[checkid] = 1;
		}
		i++;
		if(i==100)break;
	}

	if(num > 1) {
		compareTbl.style.display = '';
	}
	numCheck = num;
}

function Compare() {
	var si = 0;
	var url = '/compare.php';
	var tmp = new Array();

	for (var i=0; i<document.compare_form.elements.length; i++)
		if (document.compare_form.elements[i].type == 'hidden' &&
				Checked[document.compare_form.elements[i].value] && si<160)
		tmp[si++] = 'id[]='
				+ document.compare_form.elements[i].value;

	//url = url + tmp.join('&');

	if (si > 0 || numCheck > 0)
		window.open(url,'Compare','resizable=yes,width=990,height=540,toolbar=no,scrollbars=yes,location=no,directories=no,status=no,menubar=no,top=no,left=no,help=no');
	else
		window.alert("Выберите товары для сравнения");
}


// name - имя cookie
// value - значение cookie
// [expires] - дата окончания действия cookie (по умолчанию - до конца сессии)
// [path] - путь, для которого cookie действительно (по умолчанию - документ, в котором значение было установлено)
// [domain] - домен, для которого cookie действительно (по умолчанию - домен, в котором значение было установлено)
// [secure] - логическое значение, показывающее требуется ли защищенная передача значения cookie

function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
	if ((name + "=" + escape(value)).length <= 4000)
		document.cookie = curCookie;
	else
		if (confirm("Cookie превышает 4KB и будет вырезан !"));
	document.cookie = curCookie;
}

function getCookie(name) {
	var prefix = name + "=";
	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex == -1)
		return null;
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1)
		cookieEndIndex = document.cookie.length;
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie(name, path, domain) {
	if (getCookie(name))
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT"
}

function token(str, symbol, n){
//alert(str);
	var newstr="";
	var j=1;
	var i;
	for(i=0;i<str.length;i++){
		if(str.charAt(i)==symbol){
			j++;
			i++;
			if(str.charAt(i)==symbol)j++;
		}
		if(j==n){
			if(str.charAt(i)!=symbol)
				newstr=newstr+str.charAt(i);
		}
	}
	return newstr;
}

function nw() {
	var winWidth;
	var winHeight;
	var resize;
	if(nw.arguments[1]) winWidth = nw.arguments[1];
	else  winWidth=570;
	if(nw.arguments[2]) winHeight = nw.arguments[2];
	else  winHeight=600;
	if(nw.arguments[3]) resize = nw.arguments[3];
	else  resize="yes";
	wc=window.open(nw.arguments[0], "Digital","height="+ winHeight +",width="+ winWidth +",screenX=200,screenY=200,left=200,top=200,toolbar=0,scrollbars=no,menubar=0,status=yes,resizable="+resize);
	return false;
}

function showList(id) {
	var list_div = document.getElementById(id);
	if (list_div.style.display == '') list_div.style.display = 'none';
	else list_div.style.display = '';
}

function hideItem(id)
{
  document.getElementById(id).style.display='none';
}

function checkInt(inputS)
  // Убирает из строки все символы, не являющиеся цифрами,
  // и возвращает измененную строку.
{
	var sample = '0123456789';
	var outputS = '';
	var c;

	for ( i=0; i<inputS.length; i++ )
	{
		c = inputS.substr(i,1);

		if ( sample.indexOf(c)>=0 )
			outputS = outputS + c;
	};

	return(outputS);
}

