var chargeMenuInfo=["Hot","Game","Mobile"];
var mobileInfo=["中国移动 充值卡","中国联通 充值卡","手机自动充值"];
var mobileZoneCU=["广东","北京","天津","浙江","江苏","山东","四川","辽宁","江西","山西","广西","宁夏","内蒙古"];
var propProvince={"广东":4,"北京":5,"上海":3,"浙江":2,"江苏":6,"山东":7,"山西":23,"福建":8,"辽宁":12,"河南":9,"四川":13,"湖北":10,"天津":16,"湖南":15,"河北":11,"云南":27,"重庆":14,"新疆":26,"西藏":32,"青海":30,"宁夏":31,"江西":21,"吉林":19,"海南":24,"贵州":28,"广西":20,"甘肃":29,"安徽":18,"陕西":17,"黑龙江":22,"内蒙古":25};
var propMobile={10:1,20:2,30:3,50:4,100:5,300:11};
var propUnion={10:1,20:2,30:3,50:4,100:5,300:8};
var propBrand={"如意通":1,"如意133":2,"一卡通":3}
//1：网游横；2：网游方；3：科技横；4：科技方；5：动漫横；6：动漫方；7：女性横；8：女性方；9：游戏横；10：游戏方；11：拍拍横；12：拍拍方；13：财付通横；14：财付通方；
var chargeFrom="12";
var nowGameFrom=0;
var chargeShowLink="#";
var chargeLoadLink="#";
function $(x){
    return document.getElementById(x);
}
function chargeLoadScript(rURL,rID) {
	var oldscript = document.getElementById(rID);
	var newscript = document.createElement("script");
	newscript.setAttribute("id", rID)
	newscript.setAttribute("src", rURL);
	oldscript.parentNode.replaceChild(newscript, oldscript);
	return true;
}
function changeChargeMenu(where,name){
	for(var key="" in chargeMenuInfo)
		$("charge"+where+"Menu"+chargeMenuInfo[key]).className="";
	$("charge"+where+"Menu"+name).className="chargeMenuHere";
	if(name=="Game")
		setChargeType(1,"QQ卡");
	else if(name=="Mobile")
		setChargeType(2,"中国移动 充值卡");
	else setChargeType(0,"中国移动 充值卡");
}

function setChargeGame(from,define){
	nowGameFrom=from;
	for(var i=0;i<gameName.length;i++){
		$("chargeType").options[i+from] = new Option(gameName[i],gameName[i]);
		if(gameName[i]==define){
			$("chargeType").selectedIndex=i+from;
			chooseChargeType(define,i+from);
		}
	}
}
function setChargeMobile(define){
	var showLength=mobileInfo.length;
	if(chargeFrom=="13"||chargeFrom=="14")
		showLength=showLength-1;
	for(var i=0;i<showLength;i++){
		$("chargeType").options[i] = new Option(mobileInfo[i],mobileInfo[i]);
		if(mobileInfo[i]==define){
			$("chargeType").selectedIndex=i;
			chooseChargeType(define,i);
		}
	}	
}
function chooseChargeType(typeValue,typeSelected){
	$("chargeType").selectedIndex=typeSelected;
	$("chargeZoneShow").style.display="none";
	$("chargeBrandShow").style.display="none";
	$("chargePrixShow").style.display="none";	
	$("chargeAutoShow").style.display="none";	
	$("chargeGameShow").style.display="none";	
	$("chargeLook").style.display="none";	
	$("chargeNotePass").style.display="none";
	$("chargeCheck").style.display="none";	
	$("chargeNoteAuto").style.display="none";
	$("chargeLoad").style.display="none";
	$("chargeNow").style.display="none";	
	$("chargeAll").style.display="none";
	$("chargeGet").style.display="none";	
	if(typeValue=="中国移动 充值卡"){
		$("chargeZoneShow").style.display="";
		$("chargePrixShow").style.display="";
		$("chargeLook").style.display="";	
		$("chargeNotePass").style.display="";
	} else if(typeValue=="中国联通 充值卡"){
		$("chargeZoneShow").style.display="";
		$("chargeBrandShow").style.display="";
		$("chargePrixShow").style.display="";
		$("chargeLook").style.display="";
		$("chargeNotePass").style.display="";
	} else if(typeValue=="手机自动充值"){
		$("chargeAutoShow").style.display="";	
		$("chargeCheck").style.display="";	
		$("chargeNoteAuto").style.display="";	
	} else{
		$("chargeGameShow").style.display="";
		$("chargeLook").style.display="";
		$("chargeNotePass").style.display="";
		nowChoose= typeSelected-nowGameFrom;
		$("chargeGamePrix").options.length = 0;
		for(var i=0;i<gamePoint[nowChoose].length;i++)
			$("chargeGamePrix").options[i] = new Option(gamePoint[nowChoose][i]+"元",gamePoint[nowChoose][i]);
	}
}
//参数：默认值，显示控制（0：全显示；1：只显示游戏；2：只显示充值卡）
function setChargeType(isMore,define){
	var objChargeType=$("chargeType");
	objChargeType.options.length = 0; 
	if(isMore==2)		
		setChargeMobile(define);
	else if(isMore==1)
		setChargeGame(0,define);
	else{
		setChargeMobile(define);
		setChargeGame(mobileInfo.length,define);
	}
}
function clearChargePrix(objCard,selCard){
	chooseChargeType($("chargeType").value,$("chargeType").selectedIndex);
	if(objCard)
		objCard.selectedIndex=selCard;
}
function autoLoad(){
	chooseChargeType($("chargeType").value,$("chargeType").selectedIndex);
	loadChargePrix();
}
function loadChargePrix(){
	var nowChargeType=$("chargeType").value;
	var nowPrice=$("chargePrix").value;
	if(nowChargeType=="中国移动 充值卡"){
		clearChargePrix();
		chargeCountPTag(chargeFrom,5);
		$("chargeLook").style.display="none";
		$("chargeLoad").style.display="";
		var keyword=$("chargeZone").value + " OR 全国";		
		var beginPrice=nowPrice*0.979;
		var endPrice=nowPrice*1.2;
		var newProp=propMobile[nowPrice];	
		chargeLoadLink="http://kc1.paipai.com/js?NewProp=12,"+(propProvince[$("chargeZone").value]+256)+"-11," + newProp + "-13,5&BeginPrice="+beginPrice+"&EndPrice="+endPrice+"&sClassid=3120&Property=136&ac=1&OrderStyle=6&PageSize=10&onlineState=0&Appid=8";	
		chargeShowLink="http://search.paipai.com/cgi-bin/comm_search?KeyWord="+keyword+"&BeginPrice="+beginPrice+"&EndPrice="+endPrice+"&sClassid=3120&OrderStyle=6&OnlineState=2&NewProp1="+newProp+"&PTAG=30053."+chargeFrom+".9";
	} else if(nowChargeType=="中国联通 充值卡"){
		clearChargePrix();
		chargeCountPTag(chargeFrom,5);
		$("chargeLook").style.display="none";
		$("chargeLoad").style.display="";
		var keyword=$("chargeZone").value + " OR 全国";	
		var beginPrice=nowPrice*0.97;
		var endPrice=nowPrice*1.2;			
		var newProp=propUnion[nowPrice];
		var newProp4=propBrand[$("chargeBrand").value];		
		chargeLoadLink="http://kc1.paipai.com/js?NewProp=16,"+(propProvince[$("chargeZone").value]+256)+"-15," + newProp + "-17,2-18," + newProp4 + "&BeginPrice="+beginPrice+"&EndPrice="+endPrice+"&sClassid=3121&Property=136&OrderStyle=6&ac=1&PageSize=10&onlineState=0&Appid=8";
		chargeShowLink="http://search.paipai.com/cgi-bin/comm_search?KeyWord="+keyword+"&sClassid=3121&OrderStyle=6&OnlineState=2&NewProp1="+newProp+"&PTAG=30053."+chargeFrom+".9";
	} else if(nowChargeType!="手机自动充值"){
		clearChargePrix($("chargeGamePrix"),$("chargeGamePrix").selectedIndex);
		chargeCountPTag(chargeFrom,1);
		$("chargeLook").style.display="none";
		$("chargeLoad").style.display="";
		var keyword=nowChargeType;
		var gameIndex=$("chargeType").selectedIndex-nowGameFrom;
		var pointIndex=$("chargeGamePrix").selectedIndex;
		var beginPrice=$("chargeGamePrix").value*gameDiscount[gameIndex][pointIndex][0];
		var endPrice=$("chargeGamePrix").value*gameDiscount[gameIndex][pointIndex][1];    
		chargeLoadLink="http://kc1.paipai.com/js?KeyWord="+keyword+"&BeginPrice="+beginPrice+"&EndPrice="+endPrice+"&sClassid="+gameSort[gameIndex]+"&Property=136&ac=1&OrderStyle=6&PageSize=10&Appid=8&onlineState=0";
		chargeShowLink="http://search.paipai.com/cgi-bin/comm_search?KeyWord="+keyword+"&sClassid=12001&OrderStyle=6&OnlineState=2&PTAG=30053."+chargeFrom+".4";
	}
	chargeLoadScript(chargeLoadLink,"chargePrixSearch");
}

//回调函数
function backChargeNow(backPrix,backLink){
	var nowChargeType=$("chargeType").value;
	var nowPrice=$("chargePrix").value;
	$("chargeLoad").style.display="none";
	if(backPrix==""){
		$("chargeAll").style.display="";
		$("chargeAllLink").href=chargeShowLink;
	} else{
		chargeLoadLink=chargeShowLink=$("chargeAllLink").href="#";
		$("chargeGet").style.display="";
		$("chargeGetPrix").innerHTML=backPrix;
		if(nowChargeType=="中国移动 充值卡"||nowChargeType=="中国联通 充值卡")
			backLink=backLink+"&PTAG=30053."+chargeFrom+".6";
		else if(nowChargeType!="手机自动充值")
			backLink=backLink+"&PTAG=30053."+chargeFrom+".2";
		$("chargeGetLink").href=backLink;
	}	
	if(nowChargeType=="中国移动 充值卡"){
		if(nowPrice==50||nowPrice==100||nowPrice==300){
			var nowChargeZone=$("chargeZone").value;			
			if(nowChargeZone=="广东"){
				if(nowPrice==50) tenpayPrice="49.25";
				else if(nowPrice==100) tenpayPrice="98.50";
				else if(nowPrice==300) tenpayPrice="295.50";
			} else {
				if(nowPrice==50) tenpayPrice="49.10";
				else if(nowPrice==100) tenpayPrice="98.20";
				else if(nowPrice==300) tenpayPrice="294.60";
			}
			$("chargeNowPrix").innerHTML=tenpayPrice;
			if(chargeFrom!="13"&&chargeFrom!="14")
				$("chargeNow").style.display="";
			$("chargeNotePass").style.display="none";
		}
	} else if(nowChargeType=="中国联通 充值卡"){
		if(nowPrice==50||nowPrice==100){
			var nowChargeZone=$("chargeZone").value;			
			var isAllowCU=false;
			for(var key="" in mobileZoneCU){
				if(mobileZoneCU[key]==nowChargeZone){
					isAllowCU=true;
					break;
				}
			}
			if(!isAllowCU) return;
			if(nowChargeZone=="广东")
				tenpayPrice=nowPrice;
			else {
				if(nowPrice==50) tenpayPrice="49.25";
				else if(nowPrice==100) tenpayPrice="98.50";
			}			
			$("chargeNowPrix").innerHTML=tenpayPrice;
			if(chargeFrom!="13"&&chargeFrom!="14")
				$("chargeNow").style.display="";
			$("chargeNotePass").style.display="none";			
		}	
	}
}

function loadChargeAuto(){
	chargeCountPTag(chargeFrom,7);
	if($("chargePrix").value==50)
		chargeAutoPrix.selectedIndex=0;
	else if($("chargePrix").value==100)
		chargeAutoPrix.selectedIndex=1;
	else chargeAutoPrix.selectedIndex=2;
	chooseChargeType("手机自动充值",2);
}
function checkMobile(){
    var chargeAutoNum1 = $("chargeAutoNum1");
	var chargeAutoNum2 = $("chargeAutoNum2");
	if(!chargeAutoNum1.value.match(/^(13[0-9]{9}|15[0-9]{9}|18[0-9]{9})$/) || chargeAutoNum1.value != chargeAutoNum2.value)	{
		chargeAutoNum1.select();
		chargeAutoNum1.focus();
		if(chargeAutoNum1.value != chargeAutoNum2.value)
			alert("您两次输入的手机号码不一致，请重新输入！");
		else alert("您输入的手机号码不正确，请重新输入！");
		return false;
	}
	return true;
}
function checkAmount(){
	var amount=$("chargeAutoPrix").value;
	var validAmount=[50,100,300];
	var phoneNumber=$("chargeAutoNum1").value;
	if( 300 == amount && phoneNumber.match(/^(13[0-3][0-9]{8}|153[0-9]{8})$/)){
		alert("联通手机暂不支持300元充值！");
		return false;
	}	
	for(var i in validAmount){
		if( amount == validAmount[i])
			return true;
	}
	alert("请您选择充值金额！");
	return false;
}
function intoChargeAuto(){
	if(!checkMobile())
		return false;
	if(!checkAmount())
		return false;
	chargeCountPTag(chargeFrom,8);
	//window.open("http://digital.paipai.com/sale/mobile2/step2.shtml?pageNo=01&mobile="+$("chargeAutoNum1").value+"&money="+$("chargeAutoPrix").value+"&ADTAG=104.33.2",null,'height=175,width=520');
	window.open("http://pay.paipai.com/cgi-bin/pay_mobile_card?uin="+$("chargeAutoNum1").value+"&chgmobile="+$("chargeAutoNum1").value+"&amount="+($("chargeAutoPrix").value*100)+"&chargeway=1&ADTAG=104.33.2");
	return false;
}
function chargeInit(initMore,initDefine){
	setChargeType(initMore,initDefine);
	loadChargePrix();
}

function chargeCountPTag(domainId,linkId){
	pUrl="http://service.paipai.com/cgi-bin/go?pageId=30053&domainId="+domainId+"&linkId="+linkId+ "&url=" + escape("http://auction.paipai.com/null.shtml");
	chargeLoadScript(pUrl,"chargeCountPTagScript");
}
