﻿function showDelivery(selidx){
	if(selidx==1){
		document.getElementById('address').parentNode.style.display='block';
		var par = document.getElementById('cart');
		var Row = document.createElement("tr");
		var Cei1 = document.createElement("td");
		Cei1.setAttribute('colspan','8');
		Cei1.innerHTML = "Доставка курьером";
		Row.appendChild(Cei1); delete Ceil;
		var Cei1 = document.createElement("td");
		Cei1.innerHTML = "300";
		Row.appendChild(Cei1); delete Ceil;
		par.appendChild(Row);
		delete Row;
		document.getElementById('ttpprea').innerHTML = 300 + parseInt(document.getElementById('ttpprea').innerHTML);
		
	} else {
		document.getElementById('address').parentNode.style.display='none';
		var par = document.getElementById('cart');
		par.deleteRow(par.rows.length-1);
		document.getElementById('ttpprea').innerHTML = parseInt(document.getElementById('ttpprea').innerHTML)-300;
	}
}

function isValidEmail(strEmail)
	{
		var atpos=strEmail.indexOf("@");
		var dotpos=strEmail.lastIndexOf(".");
		if (atpos<4 || dotpos<atpos+2 || dotpos+2>=strEmail.length)	{
			alert("Укажите корректный e-mail");
			return false;
		}
		return true;
	}
	
function CheckCartForm(){
	if(!isValidEmail(document.form1.email.value))return false;
	if(document.form1.phone.value.length<4){alert("Укажите корректный телефон");return false;}
	if(document.form1.deliverytype.selectedIndex==1){if(document.form1.address.textLength<5){alert("Укажите адрес доставки");return false;}}
	document.form1.submit();
	return true;
}

$(document).ready(function(){
	$("#replr a").bind('click', function() {
		$.ajax({
		  type: "POST",
		  url: "/json.php",
		  data: "action=clear_cart",
		  success: function(html){
					window.location = "/cart.html";
			}
		})
	});
	
	$("#calccart").click(function(){
		$("table#cart input[name*=cnt]").each(function(){
			if($(this).val()==0){
				$(this).parent().parent().remove();
				if($("table#cart tr").length == 1){
					$("table#cart").remove();
					$("div#cartfooter").remove();
					$("div#carthh").html('<p>Ваша корзина пуста</p>');
					return;
				}
			}
		});
		$.post("/cart.php", "action=aswqa1"+$("#cartform").serialize());
		location.reload();
	});
	
});	



