var productid;
var isSelect;
function init(){
//初始化对象
if($('select[name="productid"] option:selected').length>0){
		productid=$('select[name="productid"] option:selected');
		isSelect=true;
	}else{
		productid=$('input[name="productid"][checked]');
		isSelect=false;
	}
	//初始化对象结束
}

function changeprice(){
			init();
			var aa=$(productid).attr("title");
			var rebate=$('input[name="pay_type"][type="radio"][checked]').attr("title");
			var number=$('#number').val();
			var priceStr=Math.floor(aa*rebate)*number;
			var listPrice=Math.floor(aa*1)*number;
			if(isNaN(priceStr)){
				priceStr=0;
			}
			if(!isNaN(listPrice)&&listPrice>priceStr){
				$('#dazhearea').show();
				$('#youhuiprice').text(listPrice-priceStr);
			}else{
				$('#dazhearea').hide();
			}
			$('#price').val(priceStr);
			$('#price2').val(priceStr);
			$('#price3').text(priceStr+' 元');
}
function dazhecolor(){
	var color=$('#fontflag').css("color"); 
	var color2=$('#heightlight').css("color"); 
	if(color=='rgb(0, 196, 0)'||color=='#00c400'){
		$('#fontflag').css({color: "#FF0000","font-weight": ""}); 
		$('#dazhe0').css({color: "#FF0000","font-weight": ""}); 
		$('#dazhe1').css({color: "#FF0000","font-weight": ""}); 
		$('#dazhe2').css({color: "#FF0000","font-weight": ""}); 
		$("#heightlight").css({color: "#FF0000","font-weight": ""}); 
	}else{
		$('#fontflag').css({color: "#00c400","font-weight": ""}); 
		$('#dazhe0').css({color: "#00c400","font-weight": ""}); 
		$('#dazhe1').css({color: "#00c400","font-weight": ""}); 
		$('#dazhe2').css({color: "#00c400","font-weight": ""}); 
		$("#heightlight").css({color: "#00c400","font-weight": ""}); 
	}
}


function checkform(){
	 var re =/^1[3|4|5|7|8][0-9]\d{8}$/;
	if(typeof($(productid).val())=="undefined"){
		alert('请选择购买的产品！');
		$('#productid0').focus();
		return false;
	}else if(document.myform.contact.value.length<2){
		alert('请确认姓名，长度不能少于2位！');
		document.myform.contact.focus();
		return false;
	}else if(!re.test(document.myform.phone.value)){
		alert('请正确输入您的11位手机号码，以确保能联系到您！');
		document.myform.phone.focus();
		return false;
	}else if(typeof($('#phone2').val())!="undefined"&&document.myform.phone.value!=document.myform.phone2.value){
		alert('确认手机号码为空或两次输入的手机号码不一致，请重新检查！');
		document.myform.phone2.focus();
		return false;
	}else if(typeof($('#County').val())!="undefined"&&document.myform.County.value==''){
		alert('请依次选择收货的省份、城市、地区！');
		document.myform.County.focus();
		return false;
	}else if(typeof($('#address').val())!="undefined"&&document.myform.address.value.length<3){
		alert('请正确输入您的详细地址，以确保您能收到货品！');
		document.myform.address.focus();
		return false;
	}else if(typeof($('input[name="pay_type"][type="radio"][checked]'))=="undefined"){
		alert('请选择支付方式！');
		document.myform.address.focus();
		return false;
	}else{
		if($('#remark').val()=="如备用联系电话"){
			$('#remark').val('');
		}
		var product;
		if(isSelect){
			product=$(productid).text();
		}else{
			product=$(productid).attr("alt");
		}
		var contact=$('#contact').val();
		var phone=$('#phone').val();
		var Province=$('#Province').val();
		var City=$('#City').val();
		var County=$('#County').val();
		var address='';
		if(typeof(County)=="undefined"||typeof($('#address').val())=="undefined"){
			address='';
		}else{
			address=' \n收货地址：'+Province+City+County+$('#address').val();
		}
		var price=$('#price2').val();
		var payn=$('input[name="pay_type"][type="radio"][checked]').attr("alt");
		if(typeof(payn)=="undefined"){
			alert('请选择支付方式！');
			document.myform.address.focus();
			return false;
		}
		if(confirm('订单内容：'+product+' \n联 系 人：'+contact+' \n联系手机：'+phone+address+' \n付款方式：'+payn+'  \n\n请确认')){	
		    var temppay=$('input[name="pay_type"][type="radio"][checked]').val();
			if(temppay=="2"){//支付宝
				$('#myform').attr("target","_blank");
			}else{
				$('#myform').attr("target","");
			}
			return true;
		}else{
			return false;
		}
		
	}
}
function addnumber(){
	$('#number').val(parseInt($('#number').val())+1);
	changeprice();
}

function minnumber(){
	if($('#number').val()>1){
	$('#number').val(parseInt($('#number').val())-1);
	changeprice();
	}
}
function inputnumber(){
	var number=parseInt($('#number').val());
	if(isNaN(number)||number<1){
		$('#number').val('1');
	changeprice();
	}else{
		$('#number').val(number);
	}
}

$(document).ready(function(){
init();
if(isSelect){
	$('select[name="productid"]').change(function(){
			changeprice();
});
}else{
$('input[name="productid"]').click(function(){
			changeprice();
});
}

$('#contact').focus(function(){
			changeprice();
});

$('#pay_type0').click(function(){
			$('#paydiv0').show();
			$('#paydiv1').hide();
			$('#paydiv2').hide();
			changeprice();
});
$('#pay_type1').click(function(){
			$('#paydiv0').hide();
			$('#paydiv1').show();
			$('#paydiv2').hide();
			changeprice();
});
$('#pay_type2').click(function(){
			$('#paydiv0').hide();
			$('#paydiv1').hide();
			$('#paydiv2').show();
			changeprice();
});
//变换字体颜色
		var speed=500;
		setInterval(dazhecolor,speed);
//price
		changeprice();
});