// Функция установки значения cookie.
var action = 0;
var stat = 0;
function setCookie(name, value, path, expires, domain, secure) {
  var curCookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "; path=/") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// Функция чтения значения cookie.
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 stripChar(str) {
	return str.replace(/[^0-9.]+/, "");
}

function clear() {
	setCookie('cnt_prods', '');
	setCookie('prods', '');
	var prods = document.prods.prod;
	for(i=1; i<=prods.length; i++) {
		//prods[i].checked = false;
		if(document.getElementById('dc'+(i+1)))document.getElementById('dc'+(i+1)).className='checkbox';
		if(document.getElementById('gl'+i)) document.getElementById('gl'+i).className='checkbox';
	}
	
	if(prods.length==undefined) document.getElementById('dc2').className='checkbox';
	
	document.prods.cnt_prd.value = "0";
}
function delProd(val) {
	var prods=getCookie('prods');
	if(prods.indexOf(val)>0) {
		var re = new RegExp("sep"+val);
	} else {
		if(getCookie('cnt_prods')<2) {
			var re = new RegExp(val);
		} else {
			var re = new RegExp(val+"sep");
		}
	}
	prods = prods.replace(re,'');
	if (prods) {
		setCookie('prods', prods);
	} else {
		setCookie('prods', '');
	}
	var cnt_prods=getCookie('cnt_prods');
	cnt_prods--;
	if (cnt_prods) {
		setCookie('cnt_prods', cnt_prods);
	} else {
		setCookie('cnt_prods', '');
	}
}

//Функция добавления или удаления продукта в/из Comparison Box
function addProd(obj) {
	if(document.cookie){
		if(obj.checked==true) {
			document.prods.cnt_prd.value++;
			if(getCookie('cnt_prods')<6) {
				if(getCookie('prods')) {
					var prods=getCookie('prods');
					prods += ((prods!='')?'sep':'')+obj.value;
					setCookie('prods', prods);
				} else {
					setCookie('prods', obj.value);
				}
				if(getCookie('cnt_prods')) {
					var cnt_prods=getCookie('cnt_prods')
					cnt_prods++;
					setCookie('cnt_prods', cnt_prods);
				} else {
					setCookie('cnt_prods', 1);
				}
			} else {
				alert("max 6 products!")
				obj.checked = false;
				document.prods.cnt_prd.value--;
			}
		} else {
			document.prods.cnt_prd.value--;
			delProd(obj.value);
		}
	} else {
		alert("for use of tool enable cookies");
		obj.checked = false;
	}
}

//Функция проверки выбран ли хотя бы один продукт
function is_checked() {
	if(getCookie('cnt_prods')<1) {
		alert("please check products");
		return false;
	}
}

//функция скрытия/показа групп атрибутов
function show_hide(className) {
	var elms = document.getElementsByClassName(className);
	for(i=0; i<elms.length; i++) {
		if (elms[i].style.display) {
			elms[i].style.display = '';
		} else {
			elms[i].style.display = 'none';
		}
	}
}
/////////////////////////////////////////////////////////////////////////////////
function ccc_top2(i,cl) {
	with(document) {
		var elms = getElementsByClassName(cl);
		if(getElementById(i).style.display=='none') {
			getElementById(i+'_ch').className='inf_h_ch';
			getElementById(i).style.display='';
			for(i=0; i<elms.length; i++) {
				elms[i].style.display = '';
			} 
		}
		else {
			getElementById(i).style.display='none';
			for(i=0; i<elms.length; i++) {
				elms[i].style.display = 'none';
			}
		}
		
	}
}


function CH(sod,obj,cm) {
	with(document) {
		var count = (cm == 0)?28:27;
		var bx = getElementsByName('box');
		for (var i = 0; i < bx.length; i++) {
			bx[i].className = 'int_tch';
		}
		if (action == 0) {
			action = obj;
			stat = 'on';
		} else {
			if (action == obj)
				if (stat == 'on') stat = 'off';
				else stat = 'on';
			else {
				action = obj;
				stat = 'on';
			}
		}
		for(i=1; i<=3; i++) getElementById('his'+i).className = 'int_tch';
		if (stat == 'on') getElementById('his'+obj).className = 'int_tch_on';
		else getElementById('his'+obj).className = 'int_tch';
		for(i=1; i<7; i++) {
			if(getElementById('ch'+i)) {
				var ch_t = getElementById('ch'+i).innerHTML;
				if (ch_t == 'Good' || ch_t == 'Excellent') ch_t = 'Excellent / Good';
				else if (ch_t == 'Average') ch_t = 'Fair';
				else if (ch_t == 'Bad' || ch_t == 'No') ch_t = 'Bad / No';
				else ch_t = ch_t;
				if(ch_t!=sod) {
					if (stat == 'on') {
							for(var j=1; j<count; j++) getElementById('td'+i+j).style.display='none';
							getElementById('ch'+i).style.display='none';
						}
						else {
							for(j=1; j<count; j++) getElementById('td'+i+j).style.display='';
							getElementById('ch'+i).style.display='';
						}
				}
				else {
					for(j=1; j<count; j++) getElementById('td'+i+j).style.display='';
					getElementById('ch'+i).style.display='';	
				}
			}
		}
	}
}
/////////////////////////////////////////////////////////////////////////////////


//Функция удаления продукта из Comparison Box
function del(obj, id) {
	if (confirm("Are you sure?")) {
		var table = $("compare");
		var ncell = obj.parentNode.cellIndex;
		for (i = 0; i < table.rows.length; i++) {
			table.rows[i].deleteCell(ncell);
		}
		recount_best();
		delProd(id);
	}
}

var best_val = new Array(
	"min",
	"max",
	"min",
	"max",
	"min",
	"null",
	"yes",
	"yes",
	"yes",
	"yes",
	"yes",
	"yes",
	"null",
	"min",
	"min",
	"min",
	"min",
	"min"
);

//Функция пересчета наилучших атрибутов
function recount_best() {
	var table = $("compare");
	var n = new Array();
	for(i=7; i<table.rows.length-1; i++) {
		if(best_val[i-7]!=null) {
			n[i] = new Array(false,false,false,false,false,false);
			row = table.rows[i];
			switch(best_val[i-7]) {
				case "min":
					min = 1000;
					for(j=1; j<row.cells.length; j++) {
						cell = row.cells[j];
						if(parseFloat(stripChar(cell.innerHTML))<=min && stripChar(cell.innerHTML)!="") {
							if(parseFloat(stripChar(cell.innerHTML))<min) {
								n[i] = new Array(false,false,false,false,false,false);
								min = parseFloat(stripChar(cell.innerHTML));
							}
							n[i][j] = true;
						} else n[i][j] = false;
					}
				break;
				case "max":
					max = -1;
					for(j=1; j<row.cells.length; j++) {
						cell = row.cells[j];
						if(parseFloat(stripChar(cell.innerHTML))>=max && stripChar(cell.innerHTML)!="") {
							if(parseFloat(stripChar(cell.innerHTML))>max) {
								n[i] = new Array(false,false,false,false,false,false);
								max = parseFloat(stripChar(cell.innerHTML));
							}
							n[i][j] = true;
						} else n[i][j] = false;
					}
				break;
				default:
					for(j=1; j<row.cells.length; j++) {
						cell = row.cells[j];
						if(cell.innerHTML==best_val[i-7]) {
							n[i][j] = true;
						} else n[i][j] = false;
					}
				break;
			}
			all = true;
			for(j=1; j<row.cells.length; j++) {
				if(n[i][j]==false) {
					all = false;
					break;
				}
			}
			if(all)
			n[i] = new Array(false,false,false,false,false,false);
		}
	}
	/*for (i = 7; i < table.rows.length - 1; i++) {
		for (j = 1; j < table.rows[i].cells.length; j++) {
			if (n[i][j]) {
				table.rows[i].cells[j].style.background='#D4C5A5';
			} else {
				table.rows[i].cells[j].style.background='';
			}
		}
	}*/
}
/*===---===---===---===---===---===---===checkbox->div===---===---===---===---===---===---===---===---===*/
function addProdD(obj,ih,cl) {
	if(document.cookie){
		if(obj.className==cl+'_on') {
			
			if(getCookie('cnt_prods')<6) {
				document.prods.cnt_prd.value++;
				if(getCookie('prods')) {
					var prods=getCookie('prods');
					prods += ((prods!='')?'sep':'')+document.getElementById('ih'+ih).value;
					setCookie('prods', prods);
				} else {
					setCookie('prods', document.getElementById('ih'+ih).value);
				}
				if(getCookie('cnt_prods')) {
					var cnt_prods=getCookie('cnt_prods')
					cnt_prods++;
					setCookie('cnt_prods', cnt_prods);
				} else {
					setCookie('cnt_prods', 1);
				}
			} else {
				alert("max 6 products!")
				obj.className=cl;
				//document.prods.cnt_prd.value--;
			}
		}
		else {
			document.prods.cnt_prd.value--;
			delProd(document.getElementById('ih'+ih).value);
		}
			
	} else {
		alert("for use of tool enable cookies");
		obj.className=cl;
	}
}

//----------------------------------------------------------------------------------------------------------------------------
function getBounds(element) {
	var left = element.offsetLeft;
	var top = element.offsetTop;
	for (var parent = element.offsetParent; parent; parent = parent.offsetParent) {
    	left += parent.offsetLeft;
    	top += parent.offsetTop;
	}
	return {left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
}


var v = document.createElement('DIV');
var over_ch = 0;
function ViewDescr(obj, text) {
	//alert(obj.innerHTML.indexOf('...', 0)+'-'+obj.innerHTML.length);
	if ((obj.innerHTML.indexOf('...', 0)+3) == obj.innerHTML.length) {
		obj.style.cursor = 'pointer';
		obj.title = text;
		/*var in_d = text;	
		v.className = 'lc_ul';
		v.id = 'o_descr';
		v.style.position = 'absolute';
		v.style.width = '150px';
		v.style.height = '300px';
		var objCoord = getBounds(obj);
		var left = objCoord.left;
		var top = objCoord.top;
		v.style.left = left+157+'px';
		v.style.top = top+'px';	
		if (over_ch < 1) {
			document.body.appendChild(v);
			v.innerHTML = in_d;
			over_ch++;
		}*/
	}
}

function HideDescr(obj) {
	if (document.getElementById('o_descr')) {
		v.innerHTML = '';
		document.body.removeChild(v);
		over_ch = 0;
	}
}
