var xmlHttp;
var sheng="省/直辖市";
var shi="市/区";
var diqu="区/县级市/县";
var beijing="北京市";
var shanghai="上海市";
function createXmlHttpRequest()
{
if (window.ActiveXObject)
{
	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

else if (window.XMLHttpRequest)
{
	xmlHttp = new XMLHttpRequest();
}
}
function handleStateChange()
{
if(xmlHttp.readyState==4)
{
	if(xmlHttp.status==200)
	{	
		var xmltext=xmlHttp.responseText;
//		alert(xmltext);
		if(xmltext=="OK")
		{
			alert("添加成功");
		}
		else if(xmltext=="NO")
		{
			alert('已经添加过此商品');
		}
		else if(xmltext=="pass")
		{
			document.getElementById("login").style.visibility = "hidden";
			document.getElementById('point_label').style.display="block";
		}
		else if(xmltext=="nopass")
		{
			alert('用户名或密码错误');
		}
		else if(xmltext=="feeOK")
		{
			alert('运费设置成功');
		}
		else if(xmltext.substr(0,6)=="weight")
		{
			var weight=xmltext.substring(6).split("-");
			document.getElementById("weight1").style.background="";
			document.getElementById("weight1").style.border="1px solid #aaa";
			document.getElementById("weight2").style.background="";
			document.getElementById("weight2").style.border="1px solid #aaa";
			document.getElementById("weight3").style.background="";
			document.getElementById("weight3").style.border="1px solid #aaa";
			document.getElementById("weight1").value=weight[0];
			document.getElementById("weight2").value=weight[1];
			document.getElementById("weight3").value=weight[2];
		}
		else if(xmltext=="insertOk")
		{
			alert('运单号保存成功');
		}
		else if(xmltext=="error13"){
			alert('请输入13位运单号');
		}
		else if(xmltext=="error"){
			alert('此运单号已经存在');
		}
		else if(xmltext=="pderror"){
			alert("写入失败");
		}
		else if(xmltext=="pdsuccess"){
			alert('添加成功');
		}
		else if(xmltext=="notlogin"){
			alert('请先登录');
		}
		else if(xmltext=="not_delivery"){
			alert('液体商品和电池类快递暂时不能运输，敬请谅解');
			hideScreen();
			displaySubMenu('shdz'); 
		}
		else if(xmltext=="not_reach")
		{
			alert('该地区不支持此支付方式，请重新选择');
			hideScreen();
			displaySubMenu('shdz'); 
		}
		else if(xmltext.substr(0,9)=="deliv_fee")
		{
			var delivfee=xmltext.substring(9).split("-"); 
			document.getElementById("delivfees").innerHTML=delivfee[0];
			
			if(parseFloat(document.getElementById("pro_total").innerHTML)>300&&delivfee[0]==0)
			{
				document.getElementById("wnjsz").innerHTML="由于商品总额大于<span style='color:#D52300;font-weight:bold'>300</span>元，免除您的运费。";
			}
			else
			{
				document.getElementById("wnjsz").innerHTML="您的运费为<span id='delivfee3' style='color:#D52300;font-weight:bold'>￥"+delivfee[0]+" </span>";
			}
			hideScreen();
			document.getElementById("total").innerHTML=parseFloat(document.getElementById("pro_total").innerHTML)-parseInt(document.getElementById("num_point").value)+parseFloat(delivfee[0]);
			document.getElementById("total").innerHTML="￥"+document.getElementById("total").innerHTML;
			
		}
		else if(xmltext=="top5Success"){
			alert('TOP5类型保存成功');
		}
		else if(xmltext=="top5Error"){
			alert('TOP5类型保存失败');
		}
		else if(xmltext=="tagSuccess"){
			alert('模式保存成功');
		}
		else if(xmltext=="tagError"){
			alert('模式保存失败');
		}
		else if(xmltext=="cart_preview1")
		{
			window.open('../../cart/index.php?admin=onup');
		}
		else if(xmltext=="cart_preview2")
		{
			window.open('../../cart/index.php?admin=ondown');
		}
		else if(xmltext=="top_preview")
		{
			window.open('../../adv/topadv.php?admin=on');
		}
		else if(xmltext=="right_preview")
		{
			window.open('../../adv/rightadv.php?admin=on');
		}
		else if(xmltext=="news_preview")
		{
			window.open('../../new/index.php?admin=on');
		}
		else if(xmltext=="entry_preview")
		{
			window.open('../../entry/index.php?admin=on');
		}
		else
		{
			alert('执行错误');
		}
	}
}
}
function startRequest(target,params)
{
	createXmlHttpRequest();
	xmlHttp.onreadystatechange=handleStateChange;
	var parm=params;
	xmlHttp.open("POST",target,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");
	xmlHttp.send(parm);
	
}
//添加收藏
function add(id1,id2)
{
	
	params="product_child_id="+id1+"&customer_id="+id2;
	target="./add.php";
	
	startRequest(target,params);	
}
//积分用户验证
function Point_Check()
{
	var username=document.getElementById('username');
	var password=document.getElementById('point_password');
	if(username.value==""||password.value=="")
	{
		alert('请填写必要项目');
		return;
	}
	else
	{
		params="username="+username.value+"&password="+password.value;
		target="./check.php";
		startRequest(target,params);
	}
}
//运费信息保存
function save1()
{
	var deliv_id=document.getElementById('deliv_id');
	var unite=document.getElementById('unite');
	var province=document.getElementById('province_select');
	var city=document.getElementById('city_select');
	var county=document.getElementById('county_select');
	var weight1=document.getElementById('weight1');
	var weight2=document.getElementById('weight2');
	var weight3=document.getElementById('weight3');
	var err=document.getElementById('err');
	var err1=document.getElementById('err1');
	var delivfree3=document.getElementById('delivfree3');
	if(!(unite.checked))
	{
		unite.value="0";
	}
	if(deliv_id.value=="")
	{
		alert('请先注册进货商');
	}
	else if(!(delivfree3.checked))
	{
		alert('请先启用此方案');
	}
	else if(province.value==sheng)
	{		
		alert('请选择省/直辖市');
	}
	else if(!(unite.checked)&&(city.value==shi||county.value==diqu))
	{
		alert('请选择市/区/县级市/县');
	}
	else if(weight1.value==""||weight2.value=="")
	{
		alert('请填写运费信息');
	}
	else if(weight1.value!=""&&isNaN(weight1.value))
	{
		alert('运费请输入数字');
	}
	else if(weight2.value!=""&&isNaN(weight2.value))
	{
		alert('运费请输入数字');
	}
	else if(weight3.value!=""&&isNaN(weight3.value))
	{
		alert('运费请输入数字');
	}
	else
	{
		params="deliv_id="+deliv_id.value+"&delivfree=1&unite="+unite.value+"&province="+province.value+"&city="+city.value+"&county="+county.value+"&weight1="+weight1.value+"&weight2="+weight2.value+"&weight3="+weight3.value;
		target="./delive_fee.php";
		startRequest(target,params);
	}
}
//运费信息保存
function save2()
{
	var deliv_id=document.getElementById('deliv_id');
	var delivfree4=document.getElementById('delivfree4');
	var freepoint=document.getElementById('freepoint');
	
	if(deliv_id.value=="")
	{
		alert('请先注册进货商');
	}
	else if(!(delivfree4.checked))
	{
		alert('请先启用此方案');
	}
	else if(freepoint.value=="")
	{
		alert('请填写运费百分比');
	}
	else if(freepoint.value!=""&&isNaN(freepoint.value))
	{
		alert('运费百分比请输入数字');
	}
	else
	{
		params="deliv_id="+deliv_id.value+"&delivfree=2&freepoint="+freepoint.value;
		target="./delive_fee.php";
		startRequest(target,params);
	}
}
//运费方案保存
function save3()
{
	var deliv_id=document.getElementById('deliv_id');
	var delivfree3=document.getElementById('delivfree3');
	var delivfree4=document.getElementById('delivfree4');
	
	if(deliv_id.value=="")
	{
		alert('请先注册进货商');
	}
	else if(delivfree3.checked&&!(delivfree4.checked))
	{
		params="deliv_id="+deliv_id.value+"&delivfree=3";
		target="./delive_fee.php";
		startRequest(target,params);
	}
	else if(delivfree3.checked&&delivfree4.checked)
	{
		params="deliv_id="+deliv_id.value+"&delivfree=4";
		target="./delive_fee.php";
		startRequest(target,params);
	}
	else if(!(delivfree3.checked)&&delivfree4.checked)
	{
		params="deliv_id="+deliv_id.value+"&delivfree=5";
		target="./delive_fee.php";
		startRequest(target,params);
	}
	else if(!(delivfree3.checked)&&!(delivfree4.checked))
	{
		params="deliv_id="+deliv_id.value+"&delivfree=6";
		target="./delive_fee.php";
		startRequest(target,params);
	}
}
//运费信息显示
function detail()
{
	var deliv_id=document.getElementById('deliv_id');
	var province=document.getElementById('province_select');
	var city=document.getElementById('city_select');
	var county=document.getElementById('county_select');
	var weight1=document.getElementById('weight1');
	var weight2=document.getElementById('weight2');
	var weight3=document.getElementById('weight3');
	if(deliv_id.value!="")
	{
		weight1.style.background="url(/emon/html/user_data/packages/huimeilian/img/bg_line_loading.gif) center center no-repeat";
		weight1.style.border="0";
		weight2.style.background="url(/emon/html/user_data/packages/huimeilian/img/bg_line_loading.gif) center center no-repeat";
		weight2.style.border="0";
		weight3.style.background="url(/emon/html/user_data/packages/huimeilian/img/bg_line_loading.gif) center center no-repeat";
		weight3.style.border="0";
		params="deliv_id="+deliv_id.value+"&province="+province.value+"&city="+city.value+"&county="+county.value;
		target="./delive_fee_detail.php";
		startRequest(target,params);
	}
}
//添加运单号
function receive(deliv_id,deliv_num_id)
{
	var deliv_num = document.getElementById(deliv_num_id);
	if (deliv_num.value!=""){
		params="deliv_num="+deliv_num.value+"&deliv_id="+deliv_id;
		target="./delivId_Insert.php";
		startRequest(target,params);
	}
	else
	{
		alert('请输入运单号');
	}
}
//缺货登记
function pdnothing()
{
	var myreg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	var product_child_id =document.getElementById('product_child_id');
	var pdemail =  document.getElementById('pdemail');
	if (pdemail.value==""){
		alert('请输入邮件地址');
	}else if(!myreg.exec(pdemail.value))
	{
		alert('请输入有效的E_mail！');
	}else
	{
		params="product_child_id="+product_child_id.value+"&pdemail="+pdemail.value;
		target="./pdnothing.php";
		startRequest(target,params);
	}	
}
//购物车运费计算
function delivfee(payment_id)
{
	var province=document.getElementById('province_select');
	var city=document.getElementById('city_select');
	var county=document.getElementById('county_select');
	if(payment_id=="4"&&county.value!=diqu)
	{
//		if(province.value==shanghai)
//		{
			showScreen();
			params="payment_id="+payment_id+"&province="+province.value+"&city="+city.value+"&county="+county.value;
			target="./delivfee.php";
			startRequest(target,params);
//		}
//		else
//		{
//			var inputs = document.getElementById("payment").getElementsByTagName("input");		
//			for(i = 0;i<inputs.length;i++)
//			{
//				if(inputs[i].checked) 
//				{
//					inputs[i].checked=false;
//				}
//			}
//			alert('目前只有上海支持货到付款');			
//			return;
//		}
	}
	else if(payment_id!=""&&county.value!=diqu)
	{
		showScreen();
		params="payment_id="+payment_id+"&province="+province.value+"&city="+city.value+"&county="+county.value;
		target="./delivfee.php";
		startRequest(target,params);
	}
	else
	{
		return;
	}
}
function delivfee1()
{
	var province=document.getElementById('province_select');
	var city=document.getElementById('city_select');
	var county=document.getElementById('county_select');
	var inputs = document.getElementById("payment").getElementsByTagName("input");
	var payment_id="";
	for(i = 0;i<inputs.length;i++)
	{
		if(inputs[i].checked) 
		{
			payment_id=inputs[i].value;
		}
	}
	if(payment_id!="")
	{
		if(payment_id=="4"&&county.value!=diqu)
		{
//			if(province.value==shanghai)
//			{
				showScreen();
				params="payment_id="+payment_id+"&province="+province.value+"&city="+city.value+"&county="+county.value;
				target="./delivfee.php";
				startRequest(target,params);
//			}
//			else
//			{
//				var inputs = document.getElementById("payment").getElementsByTagName("input");		
//				for(i = 0;i<inputs.length;i++)
//				{
//					if(inputs[i].checked) 
//					{
//						inputs[i].checked=false;
//					}
//				}
//				alert('目前只有上海支持货到付款');
//				return;
//			}
		}
		else if(payment_id!=""&&county.value!=diqu)
		{
			showScreen();
			params="payment_id="+payment_id+"&province="+province.value+"&city="+city.value+"&county="+county.value;
			target="./delivfee.php";
			startRequest(target,params);
		}
		else
		{
			return;
		}
	}
	else
	{
		return;
	}
}
function deliv_onload(province,city,county,payment_id)
{
	if(payment_id!=""&&county.value!=diqu)
	{
		params="payment_id="+payment_id+"&province="+province+"&city="+city+"&county="+county;
		target="./delivfee.php";
		startRequest(target,params);
	}
	else
	{
		return;
	}
		
}
//获取宽度
function getWidth() 
{ 
    var strWidth,clientWidth,bodyWidth; 
    clientWidth = document.documentElement.clientWidth;    
    bodyWidth = document.body.clientWidth; 
    if(bodyWidth > clientWidth){ 
        strWidth = bodyWidth + 20; 
    } else { 
        strWidth = clientWidth; 
    }
    return strWidth; 
} 
//获取高度 
function getHeight() 
{ 
    var strHeight,clientHeight,bodyHeight; 
    clientHeight = document.documentElement.clientHeight; 
    bodyHeight = document.body.clientHeight; 
    if(bodyHeight > clientHeight){ 
        strHeight = bodyHeight + 30; 
    } else { 
        strHeight = clientHeight; 
    }     
    return strHeight; 
} 

function showScreen() 
{ 
    var Element = document.getElementById('Message'); 
    var Elements = document.getElementById('Screen'); 
    var strWight=getWidth();
    var strHeight=getHeight();
    Elements.style.width = strWight+'px'; 
    Elements.style.height = strHeight+'px';
    Element.style.display = 'block'; 
    Elements.style.display = 'block';
	if (navigator.appName.indexOf("Internet Explorer") != 10)
	{
		Elements.style.opacity =0.5;
	}
} 
function hideScreen() 
{ 
    var Element = document.getElementById('Message'); 
    var Elements = document.getElementById('Screen'); 
    Element.style.display = 'none'; 
    Elements.style.display = 'none'; 
} 
//保存TOP5排行榜类型
function savetop5type(img)
{
	params="top5type="+img;
	target="./top5typeset.php";
	startRequest(target,params);
}

//保存页首广告方式
function saveTopAdvTag(id,value)
{
	if(id=='onechange'){
		hideOne();		
	}
	if(id=='threechange'){
		hideThree();
	}
	
	params="topAdvTag="+value;
	target="./topAdvTag.php";
	startRequest(target,params);
}

function hideOne()
{
	
	var Element = document.getElementById('onechange'); 
    var Elements = document.getElementById('threechange');
    Element.style.display = 'none'; 
    Elements.style.display = 'block';
}

function hideThree()
{
	var Element = document.getElementById('threechange'); 
    var Elements = document.getElementById('onechange');
    Element.style.display = 'none'; 
    Elements.style.display = 'block';
}
//内容编辑预览
function comment_preview(comment,adv)
{
	parent.frames[comment].AttachSubmit();
	var comment=document.getElementById(comment).value;
	comment=comment.replace(/\&/g,'%26');
	params="comment="+comment+"&adv="+adv;
	target="./preview.php";
	startRequest(target,params);
}
function comment_preview1(comment,title)
{
	parent.frames[comment].AttachSubmit();
	var title=document.getElementById(title).value;
	var comment=document.getElementById(comment).value;
	comment=comment.replace(/\&/g,'%26');
	params="comment="+comment+"&adv=news&title="+title;
	target="./preview.php";
	startRequest(target,params);
}

