
       function toggle(id) {
                d=document.getElementById(id); d.style.display=(d.style.display=='none')?'block':'none';	
       }
		 
		 window.onunload = function(event) {
		        event = event || window.event;
				if ( $('#print-order').css('display') == 'block' ) {
				     //alert('bal');
					 window.location = document.URL;
				} 
		 }
		 
		 $(document).ready(function(){ 
		             
					 $('#cart-icon').click(function(){ 
					   $.ajax({
					     'url': '/cart.php',
						 'type': 'POST',
						 'data': {'action': 'show'},
						 'dataType': 'text',
						 'success': function(data, status) {
						                     //alert(data);
											 if (data == '')
											     return;
												 
						                     eval('var cart =' + data + ';');
									         //alert(cart);
											 $("#cart-background").css({'display':'block'});
									         $("#cart-container").css({'display':'block'});
											 
											 var txt = '<table cellspacing="0" width="100%" class="t-cart-list">';
											 txt += '<tr><th width="20">№</th><th style="text-align: left">&nbsp;Наименование</th>';
											 txt += '<th width="40"><!--[if ie]><nobr><![endif]-->Кол-во<!--[if ie]></nobr><![endif]--></th>';
											 txt += '<th width="40">Цена</th><th width="40">Сумма</th><th width="1">&nbsp;</th></tr>'; 											 
											 var n = 1;
											 for (i in cart.goods) {
												  txt += '<tr><td>'+n+'&nbsp;</td><td style="text-align: left">&nbsp;'+cart.goods[i].name+'</td>';
												  txt += '<td><input type="text" name="'+i+'" value="'+cart.goods[i].count+'" size="3" /></td>';
												  txt += '<td>'+cart.goods[i].price+'</td>';
												  txt += '<td>'+cart.goods[i].total+'</td>';
												  txt += '<td><a href="javascript:void(0)" onclick="deleteItem(\''+i+'\')"><img src="/images/delete.gif" alt="x" /></a></td></tr>';
											      n++;
											 }
											 txt += '</table>';  
											 txt += '<div style="text-align: right; padding: 10px 0px 0px 0px; position: relative">';
											 txt += '<span id="c-list-total">Количество товаров: <strong>'+cart.count+'</strong> шт. на сумму <strong>'+cart.total+'</strong> руб.&nbsp;</span>'; 
											 txt += '<input type="button" value="Изменить" onclick="changeCart()" /></div>';
											 $('#cart-list').html(txt);
											 if (cart.count > 0) $('#cart-order').html('Оформить заказ'); else $('#cart-order').html('');
						            }
					   })
					 });
					 
					 $('#cart-info').click(function(){ 
					   $.ajax({
					     'url': '/cart.php',
						 'type': 'POST',
						 'data': {'action': 'show'},
						 'dataType': 'text',
						 'success': function(data, status) {
						                     //alert(data);
						                     eval('var cart =' + data + ';');
									         //alert(cart);
											 $("#cart-background").css({'display':'block'});
									         $("#cart-container").css({'display':'block'});
											 
											 var txt = '<table cellspacing="0" width="100%" class="t-cart-list">';
											 txt += '<tr><th width="20">№</th><th style="text-align: left">&nbsp;Наименование</th>';
											 txt += '<th width="40"><!--[if ie]><nobr><![endif]-->Кол-во<!--[if ie]></nobr><![endif]--></th>';
											 txt += '<th width="40">Цена</th><th width="40">Сумма</th><th width="1">&nbsp;</th></tr>'; 											 
											 var n = 1;
											 for (i in cart.goods) {
												  txt += '<tr><td>'+n+'&nbsp;</td><td style="text-align: left">&nbsp;'+cart.goods[i].name+'</td>';
												  txt += '<td><input type="text" name="'+i+'" value="'+cart.goods[i].count+'" size="3" /></td>';
												  txt += '<td>'+cart.goods[i].price+'</td>';
												  txt += '<td>'+cart.goods[i].total+'</td>';
												  txt += '<td><a href="javascript:void(0)" onclick="deleteItem(\''+i+'\')"><img src="/images/delete.gif" alt="x" /></a></td></tr>';
											      n++;
											 }
											 txt += '</table>';  
											 txt += '<div style="text-align: right; padding: 10px 0px 0px 0px; position: relative">';
											 txt += '<span id="c-list-total">Количество товаров: <strong>'+cart.count+'</strong> шт. на сумму <strong>'+cart.total+'</strong> руб.&nbsp;</span>'; 
											 txt += '<input type="button" value="Изменить" onclick="changeCart()" /></div>';
											 $('#cart-list').html(txt);
											 if (cart.count > 0) $('#cart-order').html('Оформить заказ'); else $('#cart-order').html('');
						            }
					   })
					 });
					 
					 $('#cart-clear').click(function(){
					   $.ajax({ 
					     'url': '/cart.php',
						 'type': 'POST',
						 'data': {'action':'clear'},
						 'dataType': 'text',
						 'success': function(data, status) { 
						                   //alert(data);
						                   eval('var cart =' + data + ';'); 
										   $('#cart-info').html('');
										   $('#cart-clear').css({'display':'none'});
								  }
					   })
					 });
					 
					 $('a.cart-order').click(function(){
					    $('#edit-plate').css({'display': 'none'});
						$('#order-plate').css({'display': 'block'});
						$('#cart-title').html('Оформление заказа');
					 });
					 
					 $('#proceed').click(function(){
					    var pdata = {};
						//alert(document.forms.namedItem('order')); 
						var l = document.forms.namedItem('order').ctype.length;
						var val = 0; 
						for (i=0; i < l; i++) {
						     var r = document.forms.namedItem('order').ctype.item(i);
							 if ( r.checked ) { val = r.value; break; }
						}
						//alert(val);
						var error = '';
						switch ( val ) 
						{
						   case '1': //alert('1');
						           $('#fizform input').each(function(){
								      if (this.name == 'fio')
									  {
									      if ( !/^\S[\S\s]+$/.test(this.value) ) 
               							  {
                                               error +='Необходимо заполнить поле \'ФИО\'\n'; 
 										  }
									  }
									  else if (this.name == 'phone')
									  {
									      if ( !/^\S[\S\s]+$/.test(this.value) ) 
               							  {
                                               error +='Необходимо заполнить поле \'Телефон\'\n'; 
 										  }
									  }
									  else if (this.name == 'address')
									  {
									      if ( !/^\S[\S\s]+$/.test(this.value) ) 
               							  {
                                               error +='Необходимо заполнить поле \'Адрес\'\n'; 
 										  }
									  }
									  pdata[this.name] = this.value;
								   });
						           pdata['wishes'] = $('#fwishes').html();
								   break;
						   case '2': //alert('2');
						             $('#yurform input').each(function(){
									  if (this.name == 'name')
									  {
									      if ( !/^\S[\S\s]+$/.test(this.value) ) 
               							  {
                                               error +='Необходимо заполнить поле \'Наименование организации\'\n'; 
 										  }
									  }
									  else if (this.name == 'phone')
									  {
									      if ( !/^\S[\S\s]+$/.test(this.value) ) 
               							  {
                                               error +='Необходимо заполнить поле \'Телефон\'\n'; 
 										  }
									  }
									  else if (this.name == 'address')
									  {
									      if ( !/^\S[\S\s]+$/.test(this.value) ) 
               							  {
                                               error +='Необходимо заполнить поле \'Адрес\'\n'; 
 										  }
									  }
									  else if (this.name == 'person')
									  {
									      if ( !/^\S[\S\s]+$/.test(this.value) ) 
               							  {
                                               error +='Необходимо заполнить поле \'Контакное лицо\'\n'; 
 										  }
									  }
									  pdata[this.name] = this.value;
								   });
								   pdata['wishes'] = $('#uwishes').html();
								   break;
						}
						if ( error != '' ) {
						     alert(error);
							 return
						}
						pdata['action'] = 'order';
						$.ajax({ 
					     'url': '/cart.php',
						 'type': 'POST',
						 'data': pdata,
						 'dataType': 'text',
						 'success': function(data, status) 
						         { //alert(data); return;
						            $('#container').css({'display':'none'});
									$(document.createElement('div')).html(data).css({'padding': '20px'})
									.appendTo($(document.createElement('div')).attr('id', 'print-order').appendTo(document.body));

								  }
					   }) 
					   
					 });
					 

					 
					 $('#order input').click(function(){
					    switch (this.value) 
						{
						   case '1': //alert('1');
						           $('#fizform').css({'display': 'block'});
						           $('#yurform').css({'display': 'none'});
								   break;
						   case '2': //alert('2');
						           $('#fizform').css({'display': 'none'});
						           $('#yurform').css({'display': 'block'});
								   break;
						}
					 });
		 });
		 
		 function add2cart(id, gid) {
			      
				  $.ajax({
					     'url': '/cart.php',
						 'data':{'action': 'add',
						         'id': id,
								 'groupid': gid,
								 'count': $('#count-'+id).attr('value')},
						 'type': 'POST',
						 'dataType': 'text',
						 'success': function(data, status) {
						                     //alert(data); 
											 eval('var cart =' + data + ';');
									         if (cart.count == 1) {
									             $('#cart-info').html('В корзине 1 товар на сумму '+cart.total+' рублей.');
									         }else if (cart.count > 1 && cart.count < 5) {
									             $('#cart-info').html('В корзине '+cart.count+' товара на сумму '+cart.total+' рублей.');
									         }else if (cart.count > 4){
									             $('#cart-info').html('В корзине '+cart.count+' товаров на сумму '+cart.total+' рублей.');
									         }
											 $('#cart-clear').css({'display':'block'});
						            }
						})
		 }
		 
		 function deleteItem(id) {
		          //alert(id);
				  $.ajax({
					     'url': '/cart.php',
						 'data':{'action': 'delete',
						         'cart_id': id
								},
						 'type': 'POST',
						 'dataType': 'text',
						 'success': function(data, status) {
						                     //alert(data);
											 eval('var cart =' + data + ';');
									         if (cart.count == 1) {
									             $('#cart-info').html('В корзине 1 товар на сумму '+cart.total+' рублей.');
									         }else if (cart.count > 1 && cart.count < 5) {
									             $('#cart-info').html('В корзине '+cart.count+' товара на сумму '+cart.total+' рублей.');
									         }else if (cart.count > 4){
									             $('#cart-info').html('В корзине '+cart.count+' товаров на сумму '+cart.total+' рублей.');
									         }
											 
											 if (cart.count == 0) {
											    $('#cart-info').html(''); $('#cart-clear').css({'display':'none'});
											 } 
											 
											 var txt = '<table cellspacing="0" width="100%" class="t-cart-list">';
											 txt += '<tr><th width="20">№</th><th style="text-align: left">&nbsp;Наименование</th>';
											 txt += '<th width="40"><!--[if ie]><nobr><![endif]-->Кол-во<!--[if ie]></nobr><![endif]--></th>';
											 txt += '<th width="40">Цена</th><th width="40">Сумма</th><th width="1">&nbsp;</th></tr>'; 											 
											 var n = 1;
											 for (i in cart.goods) {
												  txt += '<tr><td>'+n+'&nbsp;</td><td style="text-align: left">&nbsp;'+cart.goods[i].name+'</td>';
												  txt += '<td><input type="text" name="'+i+'" value="'+cart.goods[i].count+'" size="3" /></td>';
												  txt += '<td>'+cart.goods[i].price+'</td>';
												  txt += '<td>'+cart.goods[i].total+'</td>';
												  txt += '<td><a href="javascript:void(0)" onclick="deleteItem(\''+i+'\')"><img src="/images/delete.gif" alt="x" /></a></td></tr>';
											      n++;
											 }
											 txt += '</table>';  
											 txt += '<div style="text-align: right; padding: 10px 0px 0px 0px; position: relative">';
											 txt += '<span id="c-list-total">Количество товаров: <strong>'+cart.count+'</strong> шт. на сумму <strong>'+cart.total+'</strong> руб.&nbsp;</span>'; 
											 txt += '<input type="button" value="Изменить" onclick="changeCart()" /></div>';
											 $('#cart-list').html(txt);
											 if (cart.count > 0) $('#cart-order').html('Оформить заказ'); else $('#cart-order').html('');
									}
					    })
		 } 
		 
		 function changeCart() {
		          var data = {};
		          $('#edit-cart input[type=text]').each(
				    function (n, element) {
					          data[$(element).attr('name')] = $(element).attr('value');
					}
				  )
				  data.action = 'update'
				  $.ajax({
					     'url': '/cart.php',
						 'data': data,
						 'type': 'POST',
						 'dataType': 'text',
						 'success': function(data, status) {
						                     //alert(data);
											 eval('var cart =' + data + ';');
											 
											 if (cart.count == 1) {
									             $('#cart-info').html('В корзине 1 товар на сумму '+cart.total+' рублей.');
									         }else if (cart.count > 1 && cart.count < 5) {
									             $('#cart-info').html('В корзине '+cart.count+' товара на сумму '+cart.total+' рублей.');
									         }else if (cart.count > 4){
									             $('#cart-info').html('В корзине '+cart.count+' товаров на сумму '+cart.total+' рублей.');
									         }
											 
											 if (cart.count == 0) {
											    $('#cart-info').html('');
											 } 
											 
											 var txt = '<table cellspacing="0" width="100%" class="t-cart-list">';
											 txt += '<tr><th width="20">№</th><th style="text-align: left">&nbsp;Наименование</th>';
											 txt += '<th width="40"><!--[if ie]><nobr><![endif]-->Кол-во<!--[if ie]></nobr><![endif]--></th>';
											 txt += '<th width="40">Цена</th><th width="40">Сумма</th><th width="1">&nbsp;</th></tr>'; 											 
											 var n = 1;
											 for (i in cart.goods) {
												  txt += '<tr><td>'+n+'&nbsp;</td><td style="text-align: left">&nbsp;'+cart.goods[i].name+'</td>';
												  txt += '<td><input type="text" name="'+i+'" value="'+cart.goods[i].count+'" size="3" /></td>';
												  txt += '<td>'+cart.goods[i].price+'</td>';
												  txt += '<td>'+cart.goods[i].total+'</td>';
												  txt += '<td><a href="javascript:void(0)" onclick="deleteItem(\''+i+'\')"><img src="/images/delete.gif" alt="x" /></a></td></tr>';
											      n++;
											 }
											 txt += '</table>';  
											 txt += '<div style="text-align: right; padding: 10px 0px 0px 0px; position: relative">';
											 txt += '<span id="c-list-total">Количество товаров: <strong>'+cart.count+'</strong> шт. на сумму <strong>'+cart.total+'</strong> руб.&nbsp;</span>'; 
											 txt += '<input type="button" value="Изменить" onclick="changeCart()" /></div>';
											 $('#cart-list').html(txt);
											 if (cart.count > 0) {
											    $('#cart-order').html('Оформить заказ'); 
												$('#cart-clear').html('Очистить');
											 }
											 else 
											 { 
											    $('#cart-order').html('');
												$('#cart-clear').html('');
										     }
									}
						})
		 } 
		 
		 
		 
