// Функция установки значения cookie.

function Class(obj,off,on) {
	(obj.className==off)?obj.className=on:obj.className=off;
}


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;
	var prods1 = document.prods;
	if (prods) {
	for(i=1; i<=prods.length; i++) {
		//prods[i].checked = false;
		document.getElementById('dc'+i).className='checkbox';
		if(document.getElementById('gl'+i)) document.getElementById('gl'+i).className='checkbox';
	} }
	
	if(prods1.length==undefined) document.getElementById('dc2').className='checkbox';
	
	document.prods.cnt_prd.value = "0";
}

function delProd(val) {
	document.getElementById('compmax').style.display='none';
	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) {
				document.getElementById('comp2atlist').style.display='none';
				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!");
				obj.checked = false;
				document.prods.cnt_prd.value--;
				document.getElementById('compmax').style.display='block';
			}
		} 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')<2) {
	//	alert("Please, choose at least 2 dating websites!");
		document.getElementById('comp2atlist').style.display='block';
		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_on';
			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,s1,s2,cm) {
	with(document) {
		var count = (cm)?28:27;
		getElementById(s1).className='int_tch';
		getElementById(s2).className='int_tch';
		for(i=1; i<7; i++) {
			if(getElementById('ch'+i)) {
				if(getElementById('ch'+i).innerHTML!=sod) {
					if(obj.className=='int_tch_on') {
						for(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) {
		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);
		document.prods.cnt_prd.value = document.prods.cnt_prd.value-1;
}

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') {
			document.prods.cnt_prd.value++;
			if(getCookie('cnt_prods')<6) {
				document.getElementById('comp2atlist').style.display='none';
				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--;
				document.getElementById('compmax').style.display='block';
				document.getElementById('compmax').style.top=100 + getBodyScrollTop() + 'px';
			}
		}
		else {
			document.prods.cnt_prd.value--;
			delProd(document.getElementById('ih'+ih).value);
		}
			
	} else {
	//	alert("for use of tool enable cookies");
		obj.className=cl;
	}
}

function getBodyScrollTop() {
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}