// cookie functions
var cookieItems = new Array();
var seperator = '&';
var savedImage = '';

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*86400000));
		var expires = "; expires=" + date.toGMTString();
	} else var expires = "";
	document.cookie = name+"="+escape(value)+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
	}
	return null;
}

function initSaveRadioBox()
{
	var cookieValue=readCookie("saveList_pv");
	if(cookieValue && cookieValue != "empty"){
      cookieItems = cookieValue.split(seperator);
   }  
}

initSaveRadioBox();

function addSaveRadioBoxItem(elem) {
	if(elem.className=='safeRadioBoxOff') {
		if(cookieItems.length<20) {
		cookieItems[cookieItems.length] = elem.id;
		elem.className='safeRadioBoxOn';
		elem.src = '/images/remove_saved_list' + savedImage + '.gif';
		}
		else {
			alert('Bitte nicht mehr als 20 Artikel !')
		}
	}
	else {
		for(i in cookieItems) {
			if(cookieItems[i]==elem.id) {
				cookieItems.splice(i, 1)
				break;
			}
		}
		elem.className='safeRadioBoxOff';
		elem.src = '/images/add_saved_list' + savedImage + '.gif';
	}
	
	var string = '';
	if(cookieItems.length>0) {
		for(i in cookieItems) {
			string += cookieItems[i];
			if(i<cookieItems.length-1)
				string += seperator;	
		}
	
	}
	createCookie("saveList_pv",string,30);
}


function resetItem(id) {
	for(i in cookieItems) {
		if(cookieItems[i]==id) {
			cookieItems.splice(i, 1)
			break;
		}
	}
	var string = '';
	if(cookieItems.length>0) {
		for(i in cookieItems) {
			string += cookieItems[i];
			if(i<cookieItems.length-1)
				string += seperator;	
		}
	
	}
	createCookie("saveList_pv",string,30);
}

function sendFrmListCmp()
{
	var productList = '';
	var countItems = 0;
	for (var i = 0; i > document.forms[1].listCmp.length; ++i) {
		if(document.forms[1].listCmp[i].checked == true) {
			productList += document.forms[1].listCmp[i].value + '-';
			countItems++;
		}
	}
	if(productList=='' || countItems<2)
		alert('Sie müssen mindestens 2 Artikel auswählen!');
	else
		location.href = "/produkte/compare," + productList + ".html";
}

var countListCmp = 0;
function initCheckListCmp() {
	for (var i = 0; i < document.forms[1].listCmp.length; ++i) {
		if(document.forms[1].listCmp[i].checked == true) {
			countListCmp++;
		}
	}
}


function checkListCmp(elem) {
	if(elem.checked==true) {
		if(countListCmp==3)
		{
			alert("Bitte maximal 3 Artikel !")
			return false;
		}
		else
			countListCmp++;
	}
	else {
		countListCmp--;
	}
	

}

function saveItem(productId,picture) {
	if(picture=='bg')
		savedImage = "_" + picture;
		
	var cssClass = 'safeRadioBoxOff';
	for(i in cookieItems)
	{
		if(cookieItems[i]==productId) {
			cssClass = 'safeRadioBoxOn';
			break;
		}	
	}
	
	if(cssClass == 'safeRadioBoxOn')
		document.write('<img style="cursor: pointer" src="/images/remove_saved_list' + savedImage + '.gif" id="' + productId + '" onclick="addSaveRadioBoxItem(this)" class="' + cssClass +'">');
	else
		document.write('<img style="cursor: pointer" src="/images/add_saved_list' + savedImage + '.gif" id="' + productId + '" onclick="addSaveRadioBoxItem(this)" class="' + cssClass +'">');
}
