﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var invoiceStatus = 0;
var couponsStatus = 0;
var invoiceLogistics = 0;


String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#InvoiceInfoBack").css({
			"opacity": "0.7",
			"width":bWidth,
			"height":bHeight
		});
		$("#InvoiceInfoBack").fadeIn("slow");
		$("#InvoiceInfo").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#InvoiceInfoBack").fadeOut("slow");
		$("#InvoiceInfo").fadeOut("slow");
		popupStatus = 0;
	}
	
	if(invoiceStatus == 1){
		//$("#InvoiceRadiodiv").html(language["showpayinvoice"][lang][json.status]);
		$("#InvoiceRadio").val(1);
		$("#invoiceForm1").show();
		$("#invoiceForm2").hide();
	}
	
	if(invoiceStatus == 0){
		$("#InvoiceIsuse1").attr("checked",'0');
		$("#invoiceForm1").show();
		$("#invoiceForm2").hide();
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#InvoiceInfo").height();
	var popupWidth = $("#InvoiceInfo").width();
	//centering
	$("#InvoiceInfo").css({
		"position": "absolute",
		"top": windowHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#InvoiceInfoBack").css({
		"height": windowHeight
	});
	if(invoiceStatus == 0){
		$("#invoiceForm1").show();
		$("#invoiceForm2").hide();
	}
	if(invoiceStatus == 1){
		$("#invoiceForm1").hide();
		$("#invoiceForm2").show();
	}
	
}

//
function couponsPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#couponsOKInfo").height();
	var popupWidth = $("#couponsOKInfo").width();
	//centering
	$("#couponsOKInfo").css({
		"position": "absolute",
		"top": windowHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#InvoiceInfoBack").css({
		"height": windowHeight
	});
		
}

//
function loadCouponsPopup(){
	//loads popup only if it is disabled
	if(couponsStatus==0){
		$("#InvoiceInfoBack").css({
			"opacity": "0.7",
			"width":bWidth,
			"height":bHeight
		});
		$("#InvoiceInfoBack").fadeIn("slow");
		$("#couponsOKInfo").fadeIn("slow");
		couponsStatus = 1;
	}
}

function disableCouponsePopup(){
	//disables popup only if it is enabled
	if(couponsStatus==1){
		$("#InvoiceInfoBack").fadeOut("slow");
		$("#couponsOKInfo").fadeOut("slow");
		couponsStatus = 0;
	}
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	initMoney = $("#money").val();
	try{
		if(parseInt($("#InvoiceId").val()) > 0){
			$("#InvoiceRadiodiv").hide();
			$("#InvoiceCancaldiv").show();
			invoiceStatus = 1;
			var oldMoney1 = $.actullyMoney();
			$("#actuallymoney").html(oldMoney1);

		}else{
			$("#InvoiceRadiodiv").show();
			$("#InvoiceCancaldiv").hide();
		}
	}catch(e){
		$("#InvoiceRadiodiv").show();
		$("#InvoiceCancaldiv").hide();
	}
	$("#isuse").attr("checked",'1');
	
	
	//LOADING POPUP
	//Click the button event!
	$("#InvoiceIsuse2").click(function(){
		//centering with css
		
		if($("#InvoiceIsuse2").val() == 1){
			centerPopup();
			//load popup
			loadPopup();
		}else{
			disablePopup();
		}
	});
	
	$("#InvoiceIsuse1").click(function(){
		//centering with css
		
		disablePopup();
		
		var oldMoney1 = $.actullyMoney();
		
		$("actuallymoney").html(oldMoney1);
		
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#InvoiceInfoClose").click(function(){
		
		disablePopup();
	});
	//Click out event!
	$("#InvoiceInfoBack").click(function(){
		
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	$("#cancalInvoice").click(function(){
		
		//$("#invoiceForm1").hide();
		//$("#invoiceForm2").html(language["showpayinvoice"][lang][5]);
		//$("#invoiceForm2").show();
		$.cancalInvoice();
		
	});
	
	$("#invoiceSubmit").click(function(){
		$("#invoiceForm1").hide();
		$("#invoiceForm2").html(language["showpayinvoice"][lang][5]);
		$("#invoiceForm2").show();
		var invoiceType= $('input[@name=invoiceType][@checked]').val();
		
		invoiceLogistics=$('input[@name=logistics][@checked]').val();
		$.ajax({
			type: "POST",
			url:  "/user/userInvoice.trans",
			data: {method: "saveAjaxInvoice",invoiceType:invoiceType,
			invoiceContent:$("#invoiceContent").val(),
			invoiceTitle:$("#invoiceTitle").val(),
			invoiceRecipients:$("#invoiceRecipients").val(),
			invoiceAddress:$("#invoiceAddress").val(),
			invoicePost:$("#invoicePost").val(),
			numbers:$("#numbers").val(),
			cellphone:$("#cellphone").val(),
			invoiceMoney:$("#money").val(),
			P:$("#P").val(),
			id:$("#id").val(),
			transNum:$("#transNum").val(),
			logistics:invoiceLogistics},
			success: function(json){
				alert(language["showpayinvoice"][lang][json.status]);
				
				if(json.status == 0){
					
					disablePopup();
					$("#invoiceForm2").html(language["showpayinvoice"][lang][json.status]);
					$("#InvoiceRadiodiv").html(language["showpayinvoice"][lang][json.status]).append("<a href=\"javascript:\" id=\"cancalInvoice\" onclick=\"cancalInvoice()\">"+language["showpayinvoice"][lang][6]+"</a>");
					invoiceStatus = 1;
					
					try{
						//var oldMoney = $("#actuallymoney").html();
						//var oldMoney1 = 0.0;
						//oldMoney1 = parseFloat(oldMoney) - 10.0;
						var oldMoney1 = $.actullyMoney();
						$("#actuallymoney").html(oldMoney1);
					}catch(e){
						
					}
					$("#InvoiceRadio").val(1);
					return true;
				} else {
					$("#invoiceForm1").show();
					$("#invoiceForm2").hide();
					return false;
				}
			},
			dataType:"json"
		}); 
		
	});
	
	
	$("#couponsOKInfoClose").click(function(){
		disableCouponsePopup();
	});
	
	$("#UseCouponsA").click(function(){
		$.showMyCoupons();
	});
	
	$("#Payimg3").click(function(){
		$("input[@name=payType][@value=account]").attr("checked",true);
		document.getElementById("form1").action="/translation/pay/accountpay!QuickPay.do";
	});
	
	$("#Payimg4").click(function(){
		$("input[@name=payType][@value=paypal]").attr("checked",true);
		document.getElementById("form1").action="/translation/pay/paypal!QuickPay.do";
	});
	
	$("#Payimg5").click(function(){
		$("input[@name=payType][@value=tenpay]").attr("checked",true);
		document.getElementById("form1").action="/translation/pay/tenpay!Quickay.do";
	});
	
	$("#Payimg6").click(function(){
		$("input[@name=payType][@value=month]").attr("checked",true);
		document.getElementById("form1").action="/translation/pay/monthServicePay!SecurePay.do";
	});
	
	$("#payWay1").click(function(){
		//document.getElementById("form1").action="/translation/pay/alipay!QuickPay.do";
	});
	
	$("#payWay2").click(function(){
		//document.getElementById("form1").action="/translation/pay/alipay!QuickPay.do";
	});
	
	$("#payWay3").click(function(){
		//document.getElementById("form1").action="/translation/pay/accountpay!QuickPay.do";
	});
	
	$("#payWay4").click(function(){
		//document.getElementById("form1").action="/translation/pay/paypal!QuickPay.do";
	});
	
	$("#payWay5").click(function(){
		//document.getElementById("form1").action="/translation/pay/tenpay!QuickPay.do";
	});
	
});

$.cancalInvoice = function(){
	$.ajax({
		type: "POST",
		url:  "/user/userInvoice.trans",
		data: {method: "delInvoice",P:$("#P").val(),
		id:$("#id").val(),
		transNum:$("#transNum").val()},
		success: function(json){
			//alert(language["showpayinvoice"][lang][json.status]);
			
			if(json.status == 0){
				$("#InvoiceRadiodiv").show();
				invoiceStatus = 0;
				$("#InvoiceRadio").val(1);
				$("#InvoiceCancaldiv").hide();
				try{
					//var oldMoney = $("#actuallymoney").html();
					//var oldMoney1 = 0.0;
					//oldMoney1 = parseFloat(oldMoney) - 10.0;
					var oldMoney1 = $.actullyMoney();
					$("#actuallymoney").html(oldMoney1);
				}catch(e){
					
				}
				$("#invoiceForm1").show();
				return true;
			} else {
				$("#invoiceForm1").hide();
				$("#invoiceForm2").html(language["showpayinvoice"][lang][7]+"<a href=\"javascript:\" id=\"cancalInvoice\">"+language["showpayinvoice"][lang][6]+"</a>");
				$("#invoiceForm2").show();
				return false;
			}
		},
		dataType:"json"
	});
}

function checkCoupons(minimumMoney) {
	var couponsNum =document.getElementById("form1").couponsNum;
	//var money =document.getElementById("form1").money;
	
	var oldMoney = $("#actuallymoney").html();
	var money = 0.0;
	try{
		money = parseFloat(oldMoney);
	}catch(e){
		money = document.getElementById("form1").money;
		if(invoiceStatus == 1){
			money = parseFloat(money) + 10.0;
		}
	}
	//alert("minimumMoney="+minimumMoney+"    money="+money);
	if(couponsNum.value==null || couponsNum.value.Trim().length < 3){
		return false;
	} else {
		var f1 = parseFloat(money);
		var f2 = parseFloat(minimumMoney);
		if(minimumMoney==null || f1 >= f2 ){
			document.getElementById("couponsInfo").innerHTML = "";
			document.getElementById("oButton").disabled = true;
			document.getElementById("ajaxcoupons").innerHTML = "<img src=\"/img/loading.gif\" /><font color=\"red\">"+language["coupons"][lang][1]+"</font>";
			//sendRequest_reg("/ajaxCoupons.trans?method=checkCoupons&couponsNum="+couponsNum.value+"&money=" + money);
			
			$.ajax({
				type: "GET",
				url:  "/translation/coupons!ajaxCheckCoupons.do",
				data: { couponsNum:couponsNum.value, money:money},
				success: function(json){
					document.getElementById("ajaxcoupons").innerHTML = "";
					var flag = json.flag;
					if (flag == 0) {
						couponsDiscount = parseInt(json.discount);
						couponsMoney = parseFloat(json.couponsMoney);
						if(couponsDiscount == 0 && couponsMoney > 0){
							document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+
							language["coupons"][lang][14]+json.couponsMoney+language["coupons"][lang][15]+json.money+language["coupons"][lang][9];
						}else if(couponsMoney == 0 && couponsDiscount > 0){
							document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+
							language["coupons"][lang][12]+json.discount+language["coupons"][lang][13]+json.money+language["coupons"][lang][9];
						}else{
							document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][6]+json.discount+
							language["coupons"][lang][7]+json.couponsMoney+
							language["coupons"][lang][8]+json.money+
							language["coupons"][lang][9]+"</font>";
						}
						if(json.money!='' ){
							//document.getElementById("actuallymoney").innerHTML = json.money;
							try{
								//var oldMoney = $("#actuallymoney").html();
								//var oldMoney1 = 0.0;
								//oldMoney1 = parseFloat(oldMoney) - 10.0;
								var oldMoney1 = $.actullyMoney();
								$("#actuallymoney").html(oldMoney1);
							}catch(e){
								
							}
						}
						document.getElementById("oButton").disabled = false;
					} else if(flag == -1){
						document.getElementById("form1").couponsNum.focus();
						document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][3]+"</font>";
					} else if(flag == -2){
						document.getElementById("form1").couponsNum.focus();
						document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][2]+"</font>";
					} else if(flag == -3){
						document.getElementById("form1").couponsNum.focus();
						document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][4]+"</font>";
					} else if(flag == -4){
						document.getElementById("form1").couponsNum.focus();
						document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][4]+"</font>";
					} else if(flag == -5){
						document.getElementById("form1").couponsNum.focus();
						document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][5]+"</font>";
					} else if(flag == -6){
						document.getElementById("form1").couponsNum.value="";
						//document.getElementById("oButton").disabled = false;
						document.getElementById("form1").couponsNum.focus();
						document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][17]+json.minimumMoney+"</font>";
					}
				},
				dataType:"json"
			});
			
		}else{
			document.getElementById("form1").couponsNum.value="";
			document.getElementById("UseCoupons").innerHTML = "<a id=\"UseCouponsA\" href=\"javascript:\" onclick=\"showMyCoupons()\">"+language["sys"][lang][1]+"</a>";
			alert(language["coupons"][lang][17]+minimumMoney);
		}
	}
	return ;
}

function setCouponsBefour(nums, minimumMoney){
	disableCouponsePopup();
	setCoupons(nums,minimumMoney);
}

function setCoupons(nums, minimumMoney){
	document.getElementById("form1").couponsNum.value=nums;
	document.getElementById("UseCoupons").innerHTML = "<a id=\"UseCouponsA\" href=\"javascript:\" onclick=\"$.showMyCoupons()\">"+language["sys"][lang][1]+"</a>";
	checkCoupons(minimumMoney);
	//closeWindow();
}

//发送请求函数
function sendRequest_reg(url) {
	createXMLHttpRequest();
	XMLHttpReq.open("GET", url, true);
	XMLHttpReq.onreadystatechange = processResponse_reg;//指定响应函数
	XMLHttpReq.send(null);  // 发送请求
}

// 处理返回信息函数
function processResponse_reg() {
	if (XMLHttpReq.readyState == 4) { // 判断对象状态
		if (XMLHttpReq.status == 200) { // 信息已经成功返回，开始处理信息
			var res = XMLHttpReq.responseText;
			document.getElementById("ajaxcoupons").innerHTML = "";
			var json = eval("(" + res + ")");

			var flag = json.flag;
			if (flag == 0) {
				couponsDiscount = parseInt(json.discount);
				couponsMoney = parseFloat(json.couponsMoney);
				if(couponsDiscount == 0 && couponsMoney > 0){
					document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+
					language["coupons"][lang][14]+json.couponsMoney+language["coupons"][lang][15]+json.money+language["coupons"][lang][9];
				}else if(couponsMoney == 0 && couponsDiscount > 0){
					document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+
					language["coupons"][lang][12]+json.discount+language["coupons"][lang][13]+json.money+language["coupons"][lang][9];
				}else{
					document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][6]+json.discount+
					language["coupons"][lang][7]+json.couponsMoney+
					language["coupons"][lang][8]+json.money+
					language["coupons"][lang][9]+"</font>";
				}
				if(json.money!='' ){
					//document.getElementById("actuallymoney").innerHTML = json.money;
					try{
						//var oldMoney = $("#actuallymoney").html();
						//var oldMoney1 = 0.0;
						//oldMoney1 = parseFloat(oldMoney) - 10.0;
						var oldMoney1 = $.actullyMoney();
						$("#actuallymoney").html(oldMoney1);
					}catch(e){
						
					}
				}
				document.getElementById("oButton").disabled = false;
			} else if(flag == -1){
				document.getElementById("form1").couponsNum.focus();
				document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][3]+"</font>";
			} else if(flag == -2){
				document.getElementById("form1").couponsNum.focus();
				document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][2]+"</font>";
			} else if(flag == -3){
				document.getElementById("form1").couponsNum.focus();
				document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][4]+"</font>";
			} else if(flag == -4){
				document.getElementById("form1").couponsNum.focus();
				document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][4]+"</font>";
			} else if(flag == -5){
				document.getElementById("form1").couponsNum.focus();
				document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][5]+"</font>";
			} else if(flag == -6){
				document.getElementById("form1").couponsNum.value="";
				//document.getElementById("oButton").disabled = false;
				document.getElementById("form1").couponsNum.focus();
				document.getElementById("couponsInfo").innerHTML = "<img src=\"/img/alert.gif\" /><font color=\"green\">"+language["coupons"][lang][17]+json.minimumMoney+"</font>";
			}
		}
	}
}

$.showMyCoupons = function(){
	$.ajax({
		type: "POST",
		url:  "/translation/coupons!ajaxShowCoupons.do",
		data: {},
		success: function(xmldatas){
			$length = $(xmldatas).find("coupon").length;
			var wr = "<table width='450px'>";
			$(xmldatas).find("coupon").each(function(i){ 
				var $coupons = $(this);
				
				wr += "<tr>";
				var wrr = "<td width=\"14%\">"+$coupons.text()+"</td>";
				wr += wrr;
				var wrr_money = "<td width=\"40%\">"+language["coupons"][lang][14]+$coupons.attr("money")+language["coupons"][lang][9]+"</td>";
				wr += wrr_money;
				var wrr_minimum = "<td width=\"40%\">"+language["coupons"][lang][18]+$coupons.attr("minimumMoney")+language["coupons"][lang][19]+"</td>";
				wr += wrr_minimum;
				wr += "<td width=\"6%\"><a href=\"javascript:\"  onclick=\"setCouponsBefour('"+$coupons.text()+"',"+$coupons.attr("minimumMoney")+")\">"+language["sys"][lang][0]+"</a></td>";
				wr += "</tr>";
			});
			
			
			if($length == 0){
				wr += "<tr><td>";
				wr += language["sys"][lang][2];
				wr += "</td></tr>";
			}
			wr += "</table>";
			
			couponsPopup();
			loadCouponsPopup();
			
			$("#couponsForm1").html(wr);
			$("#couponsOKInfo").show();
			return true;
			
		},
		dataType:"xml"
	}); 
}

$.countMoney = function(){
	$.ajax({
		type: "POST",
		url:  "/translation/coupons!ajaxCoupons.do",
		data: {method:"countMoney", money:initMoney, couponsMoney:couponsMoney, discount:couponsDiscount, isInvoice:invoiceStatus},
		success: function(json){
			return json.acMoney;
		},
		dataType:"json"
	});
}

$.actullyMoney = function(){
	var acMoney = 0.0;
	
	if(invoiceStatus == 1 && invoiceLogistics==0){
		acMoney = parseFloat(initMoney) + 10.0;
	}else{
		acMoney = parseFloat(initMoney);
	}
	
	try{
		//alert("0000=="+acMoney);
		if(couponsDiscount > 1){
			acMoney = parseFloat(acMoney)*couponsDiscount/10 - parseFloat(couponsMoney) ;
		}else{
			acMoney = parseFloat(acMoney) - parseFloat(couponsMoney) ;
		}
		
		//alert("1111=="+acMoney);
		acMoney = getFloat(acMoney,2);
		///alert("2222=="+acMoney);
	}catch(e){
		acMoney = $.countMoney();
	}
	return acMoney;
}

function getFloat( value, len ){
	//return value.toFixed(len);
	return Math.round(value*100)/100;
	/*
	//如果value为数值型,需要先将value转换成字符串型
	//否则,使用indexOf和substr时会报错
	var str = value.toString();
	//若len不为int型,则小数部分将被忽略
	var i = parseInt( len );
	var index = str.indexOf( "." );
	var int_length = str.substr(0, index).length;
	 
	//value值必须为数值
	if ( isNaN ( str ) ){
		return "";
	}
	
	//精度值必须为数值
	if ( isNaN ( i ) ){
		return "";
	}
	 
	//精度值不能小于0,允许等于0,等于0返回value的整数部分
	if ( i < 0 ){
		return "";
	}
	
	//如果value为整数,直接返回;
	if ( index == -1 ){
		return str;
	}
	
	var temp_value = 0.0;
	//精度等于0返回value的整数部分
	if ( i == 0 ){
		return str.substr( 0, int_length);
	}else{//精度大于零返回value值小数点后i位数值,或i大于value小数后位数,则返回整个value
		temp_value = parseFloat( str.substr( 0, int_length + len + 2 ) );
	}
	 
	//进行四舍五入动作
	//如果value中小数位数<=精度值,则直接返回,不需要做四舍五入处理
	if ( ( str.length - index - 1 ) <= len ){
		return temp_value.toString();
	}
	 
	var decimal = 0.5;
	//计算
	for ( j = 0; j < i; j++ ){
		decimal = decimal / 10;
	}
	return ( temp_value + decimal ).toString().substr( 0, temp_value.toString().length - 1 );
	
	//调用示例
	var temp="0.23654";
	var temp2 = 0;
	var temp3 = getFloat (temp, temp2);
	if ( temp3 != "" )
	{
	 alert(temp3);
	}
	else
	{
	 alert("false.");
	}
	*/
}
