product_id = 0;
payment_method = 0;
user_name = '';
user_mask1 = '';
user_mask2 = '';
addtion_product_id = 0;

function checkMobile(mobile_num)
{
	var reg0= /^13\d{9}$/;
	var reg1= /^153\d{8}$/;
	var reg2= /^159\d{8}$/;
	var reg3= /^152\d{8}$/;
	my = false;
	if (reg0.test(mobile_num)) my=true;
	else if (reg1.test(mobile_num)) my = true;
	else if (reg2.test(mobile_num)) my = true;
	else if (reg3.test(mobile_num)) my = true;
	return my;
}


function showwating()
{
	$('#Warting').show();
	$('#WaitingImage').show();
	return true;
}

$(document).ready(function(){
	// 修改特定连接输出目标
	$('a[rel=tlink]').live('click', function () {
		$('#Warting').show();
		$('#WaitingImage').show();
		
		/*$('#PaymentConfirm').html('');
		$.ajax(
		{
			type: 'GET',
			url: $(this).attr('href'),
			success: function (htmldata) {
				if ($('#CardDetail').length < 1) $('#Warting').append('<div id="CardDetail"></div>');
				$('#CardDetail').css('z-index', $('#PaymentConfirm').css('z-index') + 1);
				$('#CardDetail').html(htmldata);
				$('#CloseButton, .CloseButton').click(function () {
					$('#CardDetail').hide();
					if ($('#PaymentConfirm:visible').length < 1) {$('#Warting').hide();}
					$('.ProductDescription').hide();
				});
				$('#CardDetail').css('padding-top', '300px');
				$('#CardDetail').show();
				// 隐藏处理状态层
				$('#WaitingImage').hide();
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert(XMLHttpRequest.responseText);
			}
		});
		return false;*/
	});
	// 取消按钮事件
	$('#ButtonCancel').live('click', function () {
		if (history.length > 0) history.back();
		else
		{
			$('#Warting').hide();
			$('#AddtionProductsList').hide();
		}
	});
	set_product = true;
	set_method = true;
	if (document.body.scrollHeight < window.screen.height) height = window.screen.height;
	else height = document.body.scrollHeight;
	$('#Warting').css('height', height);
	// 重载所有效验码
	var reloadAllVertifyCode = function () {
		tmp_date = new Date();
		current_time = tmp_date.getTime();
		$('.VertifyCodeImage').attr('src', '/index.php/imageoutput/VertifyCode/' + current_time);
	};
	// 函数列表
	// 屏蔽表单提交
	var disableFormSubmit = function () { return false; };
	// 显示订单详细内容
	var showOrderContent = function (htmldata) {
		$('#PaymentConfirm').css('paddingTop', '212px');
		
		$('#PaymentConfirm').show();
		$('#PaymentConfirm').html(htmldata);
		$('#WartingImage').hide();
		$('#PaymentConfirmClone').hide();
		
		$('#BtnSendConfirmBack').click(function () {
			$('#PaymentConfirm').hide();
			$('#PaymentConfirmClone').hide();
			$('#PaymentForm').show();
			reloadAllVertifyCode();
			$('select').show();
			$('#Warting').hide();
			$('#Warting .notice').show();
		});
	};
	var changePaymentButton = function () {
		$('#BtnSendToGatePayment').hide();
		$('#PaymentCompleteTips').show();
		$('#BtnSendToGatePaymentConfirm').show();
		// 更换顶部文字
		if (payment_method != '1' && payment_method != '98' && payment_method != '99') $('#HeaderTitle').html('请您在新弹出窗口中完成支付操作');
		// 更换图片
		$('#BtnSendConfirmBack').attr('src', '/images/cz_qr_04_5.jpg');
		// 更换动作
		$('#BtnSendConfirmBack').unbind('click');
		$('#BtnSendConfirmBack').click(function () {
			$('#PaymentConfirm').hide();
			$('#PaymentConfirmClone').hide();
			$('#Warting').hide();
			$('#PaymentForm').show();
			$('select').show();
			window.open('/help/index.htm', '_blank');
		});
	};
	var showPaymentConfirm = function (html_data) {
			// 显示支付表单
			
			//$('#PaymentConfirm').html(html_data);
			//$('#PaymentConfirm').show(); //sxw为了解决对话框透明的问题
			
			$('#PaymentConfirmClone').html(html_data);
			$('#PaymentConfirmClone').show(); //sxw为了解决对话框透明的问题
			
			// 隐藏支付方式列表
			$('#PaymentForm').hide();
			$('#frmPaymentConfirm').submit(changePaymentButton);
			/*if (payment_method != 24) */$('img[id=BtnSendToGatePayment]').click(changePaymentButton);
//			else changePaymentButton();
//			else changePaymentButton();
			$('#BtnSendToGatePaymentConfirm').click(function () {
				if (payment_method == 98 || payment_method == 99 || payment_method == 1)
					location.href = '/index.php/payment';
				else
				{
					$.ajax(
					{
						type: 'GET',
						url: '/index.php/payment/showOrderContent/' + $(this).attr('value') + '/' + $(this).attr('name'),
						success: showOrderContent,
						error: function (XMLHttpRequest, textStatus, errorThrown) {
							alert(XMLHttpRequest.responseText);
						}
					});
				}
			});
			$('#BtnSendConfirmBack').click(function () {
				$('#PaymentConfirm').hide();
				$('#PaymentConfirmClone').hide(); //此行可能具有意义，
				
				$('#PaymentForm').show();
				reloadAllVertifyCode();
				$('select').show();
				$('#Warting').hide();
				$('#Warting .notice').show();
			});
			// 隐藏处理状态层
			$('#WaitingImage').hide();
//			$('#Warting').hide();
	};
	
	var btnSendPaymentClick = function () {
		// 显示处理状态层
		$('select').hide();
		$('#Warting').show();
		$('#WaitingImage').show();
//		$.post('/index.php/payment/confirmPayment', $('#SendPayment').formToArray(), showPaymentConfirm);
		$.ajax(
		{
			type: 'POST',
			url: '/index.php/payment/confirmPayment',
			data: $('#SendPayment').formToArray(),
			success: showPaymentConfirm,
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert(XMLHttpRequest.responseText);
			}
		}
		);
		
		$('#isconversion').attr('value', '2'); //重置选择状态
		return disableFormSubmit();
	};
	var PaymentFormBtnBackClick = function () {
		$('#PaymentForm').hide();
		$('#PaymentMethodsList').show();
		// 更改步骤栏显示
		$('#StepPosition li').each(function(i) {
			$(this).removeClass();
			if (i == 1) $(this).addClass('Current Step' + (i + 1));
			else $(this).addClass('Step' + (i + 1) + 'Dark');
		});
	};
	$('#BtnSendPayment').click(btnSendPaymentClick);
	var changeStepBar = function (j) {
		$('#StepPosition li').each(function(i) {
			$(this).removeClass();
			if (i == j) 
			{
				 $(this).addClass('Current Step' + (i + 1));
				 if(i ==2)
				 {
					 /*if($('input[name=product_id]').eq(0).attr('value')==3 && (typeof toAVIP!='undefined' && toAVIP==1))
					 {
						 var tempChangeVip	= function()
						 {
							 $('#Warting').show();
							 var boardDiv = "<div id='allmsg' style='position:absolute;z-index:999;text-align:center;display:block; width:expression(document.body.offsetWidth); height:expression(document.body.offsetHeight); top:100px; left:0px; right:0px; bottom:0px;vertical-align: middle;'><table border=0 align='center' cellpadding=0 cellspacing=0><tr><td width=463 height=39 background='/images/pass_login_login_01.gif'>温馨提示<\/td><\/tr><\/table><table width='463' border='0' align='center' cellpadding='0' cellspacing='0' background='/images/pass_login_login_02.jpg'><tr><\/tr><td ><img src='/images/ajax_03_01.gif'  /><\/td><td>您目前可以将“迅游VIP”转成“迅游国际VIP”（按价格比例转换）。<p align='center'><input type=\"image\" src='/images/pass_bt_01.jpg' border='0' onclick='window.location=\"http://$_SERVER['SERVER_NAME']/index.php/tmanager/charge\"' />&nbsp;&nbsp;<a href=''><img src='/images/pass_bt_02.jpg' border='0' onclick='aa();return false;' /><\/a><\/p><\/td><\/table><table border='0' align='center' cellpadding='0' cellspacing='0'><tr><td><img src='/images/pass_login_login_03.gif' width='463' height='10' /><\/td><\/tr><\/table><\/div>"; 
							$("#Main").append(boardDiv);
						 }
						setTimeout(tempChangeVip, 500);
					 }else
					 {*/
					 	     $.ajax({
							 type: "POST",
							 url: "/index.php/memberpanel/cardInfoAll",
							 success: function(msg){ 
					
								arr=msg.split("|");
								//alert(arr[0]);
								if(arr[0]!=6&&arr[0]!=''&&(arr[1]==''||arr[2]==''))
								{	
								  if($('#allmsg').length==0 && false) //不显示补充资料对话框(sxw修改)
								  {
									   if($('#NoticeRemittance').length==0){
									    //$('#Warting').show();
										back_url = '/payment/index/' + product_id;
										if (product_id != 4 && payment_method != 0)
											back_url += '/' + payment_method;
										var boardDiv = "<div id='allmsg' style='position:absolute;z-index:999;text-align:center;display:block; width:expression(document.body.offsetWidth); height:expression(document.body.offsetHeight); top:100px; left:0px; right:0px; bottom:0px;vertical-align: middle;'><form action=\"/index.php/memberpanel/printModifyInfo\" method=\"POST\"><input type=\"hidden\" name=\"return_url\" value=\"" + back_url + "\"><table border=0 align='center' cellpadding=0 cellspacing=0><tr><td width=480 height=10 background='/images/pass_login_login_01.gif'>安全提示<\/td><\/tr><\/table><table width='480' border='0' align='center' cellpadding='0' cellspacing='0' background='/images/pass_login_login_02.jpg'><tr><\/tr><td ><img src='/images/ajax_03_01.gif'  /><\/td><td>您的密保资料填写不完整，将无法使用帐号找回等功能<p align='center'><input type=\"image\" src='/images/bc_bt.jpg' border='0'  />&nbsp;&nbsp;<a href=''><img src='/images/cz_bt.jpg' border='0' onclick='aa();return false;' /><\/a><\/p><\/td><\/table><table border='0' align='center' cellpadding='0' cellspacing='0'><tr><td><img src='/images/pass_login_login_03.gif' width='480' height='10' /><\/td><\/tr><\/table></form><\/div>"; 
										$("#Main").append(boardDiv);
								   }
								  }
								}
								else if(arr[0]!=6&&arr[0]!=''&&arr[1]!=''&&arr[2]!='')
								{
									$("#tips_show").show();
								}

								
							 } 
							}); 
					 /*}*/
				 }
			}
			else $(this).addClass('Step' + (i + 1) + 'Dark');
			if (i > j)
			{
				$(this).unbind('click');
				$(this).css('cursor', 'auto');
			}
			if (j == 1) set_method = true;
			else if (j == 0)
			{
				set_product = true;
				set_method = true;
				addtion_product_id = 0;
			}
		});
	};
	var PaymentMethodClick = function () {
		
		
		// 显示处理状态层
		$('#Warting').show();
		$('#WaitingImage').show();
		// 设置支付方式ID
		if (set_method) payment_method = $(this).attr('name');
		product_id =$(this).attr('id');
		addition_id = $(this).attr('idx');
		//alert(product_id);
		
		// 获取支付表单元素列表
		url = '/index.php/payment/getPaymentForm/' + product_id;
		if (user_mask1 != '' && user_mask2 != '') url += '/' + user_mask1 + '/' + user_mask2;
        url += '/product_id/' + product_id + '/payment_method/' + payment_method;
		if(addition_id!='')
		url += '/addition_id/' + addition_id ;
	   // if(product_id ==4)
		//{
				//$.post(url, {product_id : product_id, payment_method : payment_method, addition_product_id : addtion_product_id}, function (html_data) {
																																		
				
				// 显示支付表单
				 //$('#PaymentForm').html(html_data);
				//reloadAllVertifyCode();
				//$('#PaymentForm').show();
	
				// 隐藏产品列表
				//$('#ProductsList').hide();
				// 隐藏支付方式列表
				//$('#PaymentMethodsList').hide();
	
				// 更改步骤栏显示
				/*changeStepBar(2);
			/*	$('#StepPosition li').each(function(i) {
					if (i == 0)
					{
						$(this).css('cursor', 'pointer');
						$(this).click(function () {
							reloadAllVertifyCode();
							$('#ProductsList').show();
							$('#PaymentMethodsList').hide();
							$('#PaymentForm').hide();
							$('#PaymentConfirm').hide();
							$('#Warting').hide();
							changeStepBar(0);
						});
					}
					else if (i == 1)
					{
							$(this).css('cursor', 'pointer');
							$(this).click(function () {
							$('#PaymentMethodsList').show();
							$('#PaymentForm').hide();
							$('#ProductsList').hide();
							$('#PaymentConfirm').hide();
							$('#Warting').hide();
							changeStepBar(1);
						});
					}
					else $(this).unbind('click');
				});*/
			
				// 添加提交按钮动作
				/*$('#BtnSendPayment').click(btnSendPaymentClick);
				$('#BtnSendBack').click(PaymentFormBtnBackClick);
				if ('' != user_name)
				{
					if (('24' == payment_method || '25' == payment_method) && checkMobile(user_name)) $("input[name='mobile_num']").val(user_name);
					else
					{
						$("input[name='payment_login']").val(user_name);
						$("input[name='payment_login_confirm']").val(user_name);
					}
				}*/
	
				// 隐藏处理状态层
				//$('#WaitingImage').hide();
				//$('#Warting').hide();
			  //});
				////
		//}
		//else
		//{
				//alert(product_id);
				window.location = url;
		//}
		
			/*$.post(url, {product_id : product_id, payment_method : payment_method, addition_product_id : addtion_product_id}, function (html_data) {
			
			// 显示支付表单
			 //$('#PaymentForm').html(html_data);
			//reloadAllVertifyCode();
			//$('#PaymentForm').show();

			// 隐藏产品列表
			//$('#ProductsList').hide();
			// 隐藏支付方式列表
			//$('#PaymentMethodsList').hide();

			// 更改步骤栏显示
			/*changeStepBar(2);
		/*	$('#StepPosition li').each(function(i) {
				if (i == 0)
				{
					$(this).css('cursor', 'pointer');
					$(this).click(function () {
						reloadAllVertifyCode();
						$('#ProductsList').show();
						$('#PaymentMethodsList').hide();
						$('#PaymentForm').hide();
						$('#PaymentConfirm').hide();
						$('#Warting').hide();
						changeStepBar(0);
					});
				}
				else if (i == 1)
				{
						$(this).css('cursor', 'pointer');
						$(this).click(function () {
						$('#PaymentMethodsList').show();
						$('#PaymentForm').hide();
						$('#ProductsList').hide();
						$('#PaymentConfirm').hide();
						$('#Warting').hide();
						changeStepBar(1);
					});
				}
				else $(this).unbind('click');
			});*/
		
			// 添加提交按钮动作
			$('#BtnSendPayment').click(btnSendPaymentClick);
			$('#BtnSendBack').click(PaymentFormBtnBackClick);
			if ('' != user_name)
			{
				if (('24' == payment_method || '25' == payment_method) && checkMobile(user_name)) $("input[name='mobile_num']").val(user_name);
				else
				{
					$("input[name='payment_login']").val(user_name);
					$("input[name='payment_login_confirm']").val(user_name);
				}
			}

			// 隐藏处理状态层
			//$('#WaitingImage').hide();
			//$('#Warting').hide();
		//});
	};
	
	
	var showPaymentMethods = function (product_id) {
		$.get('/index.php/payment/getPaymentMethod/' + product_id + '/' + addtion_product_id, function (html_data) {
				// 显示支付方式列表
				$('#PaymentMethodsList').html(html_data);
				//reloadAllVertifyCode();
				$('#PaymentMethodsList').show();

				// 隐藏产品列表
				$('#ProductsList').hide();

				// 更改步骤栏显示
				/*changeStepBar(1);
				$('#StepPosition li').each(function(i) {
					if (i == 0)
					{
						$(this).css('cursor', 'pointer');
						$(this).click(function () {
							reloadAllVertifyCode();
							$('#ProductsList').show();
							$('#PaymentMethodsList').hide();
							$('#PaymentForm').hide();
							$('#PaymentConfirm').hide();
							$('#Warting').hide();
							changeStepBar(0);
						});
					}
					else $(this).unbind('click');
				});*/

			

				// 为错误返回按钮添加点击事件
				$('#BtnSendConfirmBack').click(function () {
					addtion_product_id = 0;
					// 游戏点卡
					if (product_id == 4 || product_id == 5)
					{
						if ($('#AddtionProductsList').length < 1)
						{
							$('#WaitingImage').show();
							if (4 == product_id) url = '/index.php/payment/getAddtionProductsList/' + product_id;
							AdditionProductsListShow(url);
						}
						$('#AddtionProductsList').show();
						$('#Warting').show();
					}
					product_id = 0;
					set_product = true;
					$('#PaymentMethodsList').hide();
					$('#ProductsList').show();
				});

				// 隐藏处理状态层
				$('#WaitingImage').hide();
				$('#Warting').hide();
			});
	};
	
	// 关闭按钮
	$('#CloseButton').live('click', function () {
		addtion_product_id = 0;
		$('#AddtionProductsList').hide();
		$('body').remove('#AddtionProductsList');
		$('#Warting').hide();
		$('.ProductDescription').hide();
		$('#SelectDate').show();
	});
	var AdditionProductsListShow = function (url) {
//		url = '/index.php/payment/getAddtionProductsList/' + product_id;
		if (url.substr(0, 7) == 'http://')
			url = '/proxy.php?url=' + url.replace(/&/, '|');
		$.get(url, InsertIntoAddtionProductsList);
	};

	/*var AdditionProductsListShow = function () {
		url = '/index.php/payment/getAddtionProductsList/' + product_id;
		$.get(url, function (html_data) {
			$('body').append(html_data);
			$('#AddtionProductsList').css('top', 200 + 'px');
			$('#AddtionProductsList').css('left', ($(document).width() / 2 - $('#AddtionProductsList').width() / 2) + 'px');
			$('#AddtionProductsList .products_in_catalog a, #AddtionProductsList .products_in_catalog a').live('click', function () {
				$('#WaitingImage').show();
				addtion_product_id = $(this).attr('name');
				$('#AddtionProductsList').hide();
				showPaymentMethods(product_id);
				return false;
			});
			// 关闭按钮
			$('#CloseButton').click(function () {
				addtion_product_id = 0;
				$('#AddtionProductsList').hide();
				$('#Warting').hide();
				$('.ProductDescription').hide();
			});
			// 隐藏处理状态层
			$('#WaitingImage').hide();
	//		$('#Warting').hide(); 
		});
	};*/
	
	var InsertIntoAddtionProductsList = function (html_data) {
		if ($('#AddtionProductsList').length < 1)
			$('body').append('<div id="AddtionProductsList"></div>');
		$('#AddtionProductsList').html(html_data);
		$('#AddtionProductsList').css('top', 200 + 'px');
		$('#AddtionProductsList').css('left', ($(document).width() / 2 - $('#AddtionProductsList').width() / 2) + 'px');
		/*$('#AddtionProductsList .products_in_catalog a, #AddtionProductsList .products_in_catalog a').live('click', function () {
			$('#WaitingImage').show();
			addtion_product_id = $(this).attr('name');
			$('#AddtionProductsList').hide();
			showPaymentMethods(product_id);
			return false;
		});*/
		$('#AddtionProductsList').show();
		// 隐藏处理状态层
		$('#WaitingImage').hide();
//		$('#Warting').hide(); 
	};

	var ProductsListClick = function () {
		
//		if ($(this).html() == '&nbsp;') return;
		// 显示处理状态层
		$('#Warting').show();
		$('#WaitingImage').show();
		// 设置产品ID
		if (set_product) product_id = $(this).attr('name');
		//alert(product_id);
		//if (4 != product_id || (4 == product_id && 0 != addtion_product_id))
		//{
			// 获取支付方式列表
			//showPaymentMethods(product_id);
		//}
		//else if (4 == product_id)
		if (4 == product_id)
		{	// 产品为游戏点卡并且未选择点卡
			AdditionProductsListShow('/index.php/payment/getAddtionProductsList/' + product_id);
			return;
			if ($('#AddtionProductsList').length > 0)
			{
				$('#AddtionProductsList').show();
				$('#WaitingImage').hide();
			}
			else AdditionProductsListShow('/index.php/payment/getAddtionProductsList/' + product_id);
		}
	};
	
	
	$('#SendPayment input').keydown(function (event) {
			switch(event.keyCode) {
				case 13: $('#SendPayment').submit(); break;
			}
		});

	$('#SendPayment').submit(btnSendPaymentClick);
	// 为支付方式添加点击事件
	$('#ulPaymentMethodsList li').each(function () {
												 
		if ('WangBa' != $(this).attr('name') && 'JunWang' != $(this).attr('name') && 'TB' != $(this).attr('name')) $(this).click(PaymentMethodClick);
	});
	// 为产品图片添加点击事件
	$('#ProductsList td.Product').click(ProductsListClick);

	$('#btnGetRecordsByDate').css('cursor', 'pointer');
	$('#btnGetRecordsByDate').click(function () {
		tmp = $('#SelectDate').val().split('|');
		new_location = '/index.php/payment/getPaymentList/1';
		if (tmp[0] != '2006-06-01' && tmp[1] != '2050-06-01') new_location += '/' + tmp[0] + '/' + tmp[1];
		window.location.href = new_location;
	});
	$('#PageNum').change(function () {
		old_location = window.location.pathname.split('/');
		new_location = '';
		for (var i = 0; i < old_location.length; i++)
		{
			if (i != 4)
			{
				if ('' != old_location[i]) new_location += '/' + old_location[i];
			}
			else new_location += '/' + $(this).val();
		}
		if (old_location.length < 5) new_location = window.location.pathname + '/' + $(this).val();
		if (window.location.hostname.substr(-1) == '/') hostname = window.location.hostname.substr(0, -1);
		else hostname = window.location.hostname;
		window.location.href = window.location.protocol  + '//' + hostname + new_location;
	});

	tmp = window.location.pathname.split('/');
	tmp_str = tmp[tmp.length - 1];
	tmp = $.grep(tmp, function (n, i) { return (n != 'skin' && n != 'KingSoft'); });
	if ((tmp_str == '0' || tmp_str == '2' || tmp_str == '3' || tmp_str == '21' || tmp_str == '1') && 'index' == tmp[tmp.length - 2])
	{
		set_product = false;
		product_id = tmp_str;
		ProductsListClick();
	}
	else if (
		tmp.length > 6
		&& 'index' == tmp[3]
	)
	{
		set_product = false;
		if (tmp.length > 10) addtion_product_id = tmp[10];
		product_id = tmp[4];
		if (tmp.length < 10 && tmp.length > 6)
		{
			user_mask2 = tmp[8];
			user_mask1 = tmp[7];
		}
		else if (tmp.length > 10)
		{
			user_mask2 = tmp[9];
			user_mask1 = tmp[8];
		}
		user_name = tmp[6];
		ProductsListClick();
		if ('10000' != tmp[5])
		{
			set_method = false;
			payment_method = tmp[5];
			PaymentMethodClick();
		}
		else set_method = true;
	}
	else if (
		tmp.length > 4
		&& 'index' == tmp[tmp.length - 4]
	)
	{
		set_product = false;
		set_method = false;
		product_id = tmp[tmp.length - 3];
		user_name = tmp_str;
		ProductsListClick();
		if ('10000' != tmp[tmp.length - 2])
		{
			payment_method = tmp[tmp.length - 2];
			PaymentMethodClick();
		}
		else set_method = true;
	}
	else if (
		tmp.length > 3
		&& 'index' == tmp[tmp.length - 3]
	)
	{
		set_product = false;
		set_method = false;
		product_id = tmp[tmp.length - 2];
		ProductsListClick();
		if ('10000' != tmp_str)
		{
			payment_method = tmp_str;
			PaymentMethodClick();
		}
		else set_method = true;
	}
});