var ClassSale = Class.create();
ClassSale.construct = function() {
    this._HostRoomIndex = 0 ;
	this._ProductID ='' ;
	this._PaymentIndex = '' ;
    this._IncrementIndex = '' ;
	this._PayModel = "" ;
	
	var HostRoomPrice = 0 ;
	var IncrementPrice = 0 ;
	var TotalPrice = 0 ;
	var XmlHttpRequest = "" ;
	var PayModelName = '' ;

    this.initialize = function(iniHostRoomIndex,iniProductID){
		this._HostRoomIndex = iniHostRoomIndex ;
		this._ProductID = iniProductID ;
		this._PaymentIndex = 0 ;
		//alert(iniHostRoomIndex);
		$("input[name=HostRoomId]")[iniHostRoomIndex].checked = true; 
		GetSaleHtml(this);
    }
	
    GetSaleHtml = function(_this){
		var HostRoomID = $("input[name=HostRoomId]")[_this._HostRoomIndex].value ;
		$.get("Server_Sale.asp",{hostroomid:HostRoomID,productid:_this._ProductID},
			function(data){
				XmlHttpRequest = data ;
				GetPaymentHtml(data) ;
				_this.ChangePaymentIndex(_this._PaymentIndex);
				ChangePageLeftHeight() ;
				document.getElementById("SaleButton").disabled = false ;
			}
		);
	}

    GetPaymentHtml = function(){
		var data = XmlHttpRequest ;
		var PaymentHtml = "" ;
		var Product = data.getElementsByTagName("Product")[0];
		var HostRoom = data.getElementsByTagName("HostRoom")[0];
		
		var P_YearPrice  = parseInt(Product.getAttribute("YearPrice")) ;
		var P_MonthPrice = parseInt(Product.getAttribute("MonthPrice")) ;
		var P_QuarterPrice  = parseInt(Product.getAttribute("QuarterPrice")) ;
		var P_HalfYearPrice = parseInt(Product.getAttribute("HalfYearPrice")) ;

		var H_YearPrice  = parseInt(HostRoom.getAttribute("YearPrice")) ;
		var H_MonthPrice = parseInt(HostRoom.getAttribute("MonthPrice")) ;
		var H_QuarterPrice  = parseInt(HostRoom.getAttribute("QuarterPrice")) ;
		var H_HalfYearPrice = parseInt(HostRoom.getAttribute("HalfYearPrice")) ;
		
		var Product_YearPrice  = P_YearPrice + H_YearPrice;
		var Product_MonthPrice = P_MonthPrice + H_MonthPrice;
		var Product_QuarterPrice  = P_QuarterPrice + H_QuarterPrice;
		var Product_HalfYearPrice = P_HalfYearPrice + H_HalfYearPrice;

		PaymentHtml = PaymentHtml + "<input type='radio' name='Payment' value='"+ Product_MonthPrice +"' onclick='javascript:Sale.ChangePaymentIndex(0)'/>" ;
		PaymentHtml = PaymentHtml + "月付："+ Product_MonthPrice +"元　" ;
//		PaymentHtml = PaymentHtml + "<input type='radio' name='Payment' value='"+ Product_QuarterPrice +"' onclick='javascript:Sale.ChangePaymentIndex(1)'/>" ;
//		PaymentHtml = PaymentHtml + "季付："+ Product_QuarterPrice +"元　" ;
//		PaymentHtml = PaymentHtml + "<input type='radio' name='Payment' value='"+ Product_HalfYearPrice +"' onclick='javascript:Sale.ChangePaymentIndex(2)'/>" ;
//		PaymentHtml = PaymentHtml + "半年付："+ Product_HalfYearPrice +"元　" ;
//		PaymentHtml = PaymentHtml + "<input type='radio' name='Payment' value='"+ Product_YearPrice +"' onclick='javascript:Sale.ChangePaymentIndex(3)'/>" ;
		PaymentHtml = PaymentHtml + "<input type='radio' name='Payment' value='"+ Product_YearPrice +"' onclick='javascript:Sale.ChangePaymentIndex(1)'/>" ;
		PaymentHtml = PaymentHtml + "年付："+ Product_YearPrice +"元" ;

		document.getElementById("PaymentHtml").innerHTML = PaymentHtml ;	
	}

    GetIncrementHtml = function(_this){
		var data = XmlHttpRequest ;
		var IncrementHtml = "",IProductPrice="" ;
		var IProduct = data.getElementsByTagName("IProduct");
		switch (_this._PaymentIndex) {
		   case 0 :
			   _this._PayModel = "MonthPrice" ;
			   PayModelName = " 元/月" ;
			   break ;
//		   case 1 :
//			   _this._PayModel = "QuarterPrice" ;
//			   PayModelName = " 元/季" ;
//			   break ;
//		   case 2 :
//			   _this._PayModel = "HalfYearPrice" ;
//			   PayModelName = " 元/半年" ;
//			   break ;
		   default :
			   _this._PayModel = "YearPrice" ;
			   PayModelName = " 元/年" ;
		} 
		for(var i=0;i<IProduct.length;i++){
			IProductID    = IProduct[i].getAttribute("ID");
			IProductPrice = IProduct[i].getAttribute(_this._PayModel);
			IncrementHtml = IncrementHtml + "<tr><td width='23%' height='25' class='td_a'>" ;
			IncrementHtml = IncrementHtml + "<input type='checkbox' name='IProduct' id='"+ IProductID +"' value='"+ IProductPrice +"' onclick='Sale.ChangeIncrementIndex()'/>" ;
			IncrementHtml = IncrementHtml + IProduct[i].getAttribute("Name") ;
			IncrementHtml = IncrementHtml + "</td><td width='77%' height='25' class='td_b'>" ;
			IncrementHtml = IncrementHtml + "价格：" +  IProductPrice + PayModelName;
			IncrementHtml = IncrementHtml + "</td></tr>" ;
		}
		$("#IncrementProduct").html(IncrementHtml);
	}

    this.ChangeHostRoomIndex = function(iniHostRoomIndex){
		document.getElementById("SaleButton").disabled = true ;
		this._HostRoomIndex = iniHostRoomIndex ;
		$("input[name=HostRoomId]")[iniHostRoomIndex].checked = true; 
		this._PaymentIndex = 0 ;
		IncrementPrice = 0 ;
		this._IncrementIndex = '' ;
		HostRoomPrice = '' ;
		GetSaleHtml(this);
		document.getElementById("SaleButton").disabled = false ;
	}
	
	this.ChangePaymentIndex = function(iniPaymentIndex){
		document.getElementById("SaleButton").disabled = true ;
		IncrementPrice = 0 ;
		this._PaymentIndex = iniPaymentIndex ;
		GetIncrementHtml(this) ;
		document.getElementsByName("Payment")[this._PaymentIndex].checked = true;
	    HostRoomPrice = document.getElementsByName("Payment")[iniPaymentIndex].value;
		ChangePrice(HostRoomPrice,IncrementPrice);
		document.getElementById("SaleButton").disabled = false ;
	}
	
	this.ChangeIncrementIndex = function(){
		document.getElementById("SaleButton").disabled = true ;
		GetIncrementPrice() ;
		ChangePrice(HostRoomPrice,IncrementPrice);
		document.getElementById("SaleButton").disabled = false ;
	}

    GetIncrementPrice = function(){
		IncrementPrice = 0 ;
		$("#IncrementIDs").val("")
		$("input[name='IProduct']:checked").each(
		    function(){
				DgIncrementIDs = document.getElementById("IncrementIDs").value ;
				IncrementPrice = parseInt(IncrementPrice) + parseInt($(this).val()) ;
				document.getElementById("IncrementIDs").value = DgIncrementIDs + this.id + '|1|||' ;
			}
		);
	}

	ChangePrice = function(strHostRoomPrice,strIncrementPrice){
		iniTotalPrice = parseInt(HostRoomPrice) + parseInt(IncrementPrice) ;
		document.getElementById("HostRoomPrice").value = strHostRoomPrice ;
		document.getElementById("IncrementPrice").value = strIncrementPrice ;
		document.getElementById("TotalPrice").value = iniTotalPrice ;
		$("label[name='c_price']").html(PayModelName);
	}

    ChangePageLeftHeight = function(){
		$("#left").height($("#right").height())
	}
}
