var expDays = 30;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function cookieSaveID(id) {
	if (id==null){return;}
	var c=GetCookie('id_note');
	if (c!=null) {id=c+id;}
	if (id!=null) {
		id=checkC(id);
		SetCookie('id_note',id,exp);
		alert('Предложение добавлено в блокнот.'); 
	}

}

// (c) Kasatkin V.
function cookieExclude() {
	var excludelist = new Array();
	var newcookiestr='';
	// gettin current cookie
	var current;
	var reallength;
	current=GetCookie('id_note');
	// alert('current cars'+current);
	var c=new Array();
	c=current.split(/_/);
	//alert ('current cars '+current);
	// reading exclude list from form a (checkboxes: exclude) into array excludelist
	// bugz 8( why if only one elements presents , there is no possibility to use LENGTH ???
	// for (var i=0;i<document.a.exclude.length;i++) {
	if (document.a.elements.length == 1){
		if (document.a.exclude.checked == true) {
			excludelist[0] = document.a.exclude.value;}
		}
	else {
		for (var i=0;i<document.a.elements.length;i++) { 
			if (document.a.exclude[i].checked==true) {
				excludelist[excludelist.length]=document.a.exclude[i].value;
			}
		}
	}
	//for (var i=0;i<excludelist.length;i++)
	//{
	// alert ('want 2 exclude: '+ excludelist[i]);
	//}

	// list current cookie and compare its elemenrs with excludelist (renames 'skip' value)
	for (i=0;i<c.length;i++) {
		flag=0;
		for (var j=0;j<excludelist.length;j++) {
			if (c[i]==excludelist[j]) {flag=1;}
		}
		if (flag == 0) {
			newcookiestr=newcookiestr+c[i]+'_'; 
		}
}
// alert ('new cookie string: '+newcookiestr);
// clear cookie
SetCookie('id_note','');

// set new cookie list
SetCookie('id_note',newcookiestr,exp);
}


function cookieSave() {
	var str='';
	// FIXED !!!!  (c) Kasatkin
	if (document.a.elements.length == 1) {
		if (document.a.note.checked == true) {
			str=document.a.note.value + '_';
		}
	}
	else {
		for (var i=0;i<document.a.note.length;i++) {
			if (document.a.note[i].checked==true) {
				str=str+document.a.note[i].value+'_';
			}
		}
	}
	// FIXED !!!!

	var c=GetCookie('id_note');
	if (!str.match(/[0-9]{1,}/)) {
		alert('Не выбрано ни одной модели.');
	}
	else
	{
		if (c!=null) {str=c+str;}
		str=checkC(str);
//alert(str);
		SetCookie('id_note',str,exp); 
		alert('Предложения добавлены в блокнот.');
	}
}

function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg)      
			return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

function SetCookie(name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : '/';  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 100000);
	var cval = GetCookie (name);  
//	alert(name + ' ' + cval);
	SetCookie('id_note',''); 
//	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
	//SetCookie('id_note',null,exp); 
//	cval=null;
//	document.cookie = name + "=" + cval+
//	((exp == null) ? "" : ("; expires=" + exp.toGMTString())) + 
//	"; path=" +"/";
//	cval = GetCookie (name);  

//	alert(name + ' ' + cval);
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function checkC(strn) {
	var c=new Array();
	c=strn.split(/_/);
	c.sort();
	var tmp;
	var newString='';
	for (var i=0;i<c.length;i++) {
		if ( (c[i]!=tmp) ) {newString=newString+c[i]+'_';}
		tmp=c[i];
	}
	return newString;
}
