var ar = new Array();
//по кукам построить коллекцию элементов формы	
function bild(o){
	var string = SimpleCookie.get('ss');
	
	if('string' == typeof string && '' !== string)
	{
		document.getElementById("selected_label").style.display="inline";
		var array = string.split(',');
			var s= '<table><tr><th>Название</th><th>Добавить</th></tr>';//<th>Количество</th>
		for(i in array){
			if('' !== array[i])
			{
				var value = array[i].split(':');
				
				s += "<tr>";
				s += "<td><label>"+value[1]+"</label></td>";
				//s += "<td><input name=count_"+value[0]+" id="+value[0]+" value="+value[2]+" maxlength='10'></td>";
				s += "<td><input type='checkbox' name='"+value[0]+"' value='"+value[1]+"' checked=''><input name=count_"+value[0]+" id="+value[0]+" value="+value[2]+" maxlength='10' style='display:none'><br></td>";
				s += "</tr>"			
			}
				
		}
				s +="</table>";
		if('' !== s)
				o.innerHTML = s+"<a href='#' onclick='uSubmit(document.order);' class='submit save' id='selected_link'>Зарезервировать</a>";
	}
	
	if(string == ""){
		document.getElementById("cr").innerHTML = "<p>Ваша корзина пуста. Чтобы добавить товар в корзину перейдите в <a href='/store/search.html'>каталог</a></p>";
	document.getElementById("selected_label").style.display="none!important";
	}
}

// добваление/удаление отмеченного элемента в/из массива
	function selected(o)
	{
		if(o.getAttribute('rel') == null)
		{
		init_array();
		checked_box();
		}
		for(var i=0; i<o.length; i++)
		{
			if(o[i].type == 'checkbox')
				o[i].onchange= function(){
					var name = this.name;
					if('string' == typeof ar[name]){
						ar[name] = null;
					}
					else if(this.checked){
						var pattern = new RegExp("^\\d+$");
						var count = document.getElementById(name).value;
						if(pattern.test(count))
							ar[name] = this.value+':'+count;
						else
						{
							alert('Необходимо корректно указать количество товара');
							this.checked = false;
						}
					}
				if(o.getAttribute('rel')==null)
					wr();
				}
			if(o[i].type == 'text')
				o[i].onchange= function(){
					var id = this.id;
					var pattern = new RegExp("^\\d+$");
					if(pattern.test(this.value))
					{
						ar[id] = document.getElementsByName(id)[0].value+':'+this.value;
						document.getElementsByName(id)[0].checked = true;
					}
					else
						{
							alert('Необходимо корректно указать количество товара');
							document.getElementsByName(id)[0].checked = false;
						}
				if(o.getAttribute('rel')==null)
					wr();
				}
		}
	}
// инициализация массива уже выбранных элементов по данным куки
function init_array(){
		var string = SimpleCookie.get('ss');
		if('string' == typeof string && '' !== string){
			var array = string.split(',');
			for(var i in array){
				
				if(array[i]!='')
				{
				var value = array[i].split(':');
				ar[value[0]] = value[1]+':'+value[2];
				}
			}
		}
	}
// при загружке формы отметить те флажки которые были выбраны ранее
function checked_box(){
		for(var i in ar)
		{	
			var  count = ar[i].split(':');
			document.getElementsByName(i)[0].checked = true;
			document.getElementById(i).value = count[1];
		}
	}
// записать полученный массив в куки в виде строки		
	function wr(){
		var x;
		var ss = '';		
		for(x in ar){
			ss += ar[x]!=null ?x+":"+ar[x]+',':'';
		}		
		SimpleCookie.set('ss',ss);
	}
	
// user submit function, clear coockie, check and submit form
function uSubmit(o){
		SimpleCookie.set('ss','');
		o.submit(); 
		return false;
		}

		
