//彩票业务类
function Lottery_3d(name){
	var self = this;
	this.name = name;  //实例名称
	this.img_path = '';
	this.api_url = "/html_cn/js/";
	//this.api_url = "http://www2.huacai.com/getIssueNum.do";

	this.T3D_ZX_DS = 103001; // 3D直选单式
	this.T3D_ZX_FS = 103002; // 3D直选复式
	this.T3D_ZX_HZ = 103004; // 3D直选和值
	this.T3D_ZX_BH = 103005; // 3D直选包号

	this.T3D_ZS_DS = 103006; // 3D组三单式
	//this.T3D_ZS_FS = 103007; // 3D组三复式
	//this.T3D_ZS_HZ = 103008; // 3D组三和值
	//this.T3D_ZS_BH = 103009; // 3D组三包号

	this.T3D_ZL_DS = 103010; // 3D组六单式
	//this.T6D_ZL_FS = 103011; // 3D组六复式
	//this.T6D_ZL_HZ = 103012; // 3D组六和值
	//this.T6D_ZL_BH = 103013; // 3D组六包号
	this.play_info = new Array();
	var i = 0;
	this.play_info[i++] = new Array(this.T3D_ZX_DS, "直选单式");
	this.play_info[i++] = new Array(this.T3D_ZX_FS, "直选复式");
	this.play_info[i++] = new Array(this.T3D_ZX_HZ, "直选和值"); 
	this.play_info[i++] = new Array(this.T3D_ZX_BH, "直选包号");
	this.play_info[i++] = new Array(this.T3D_ZS_DS, "组三单式");
	this.play_info[i++] = new Array(this.T3D_ZL_DS, "组六单式");

	//保存已经选择的号码
	this.sel_num = new Array();
	this.sel_init();

	//保存数量
	this.sel_num_total = this.sel_num;
	this.total = new Array();
	this.total_sum = 0;
	this.timesby_total_amount = 0;

	//追期列表
	this.repeat_issue_num = new Array();

	
	this.amount = 2;//单注价格
	this.limit_timesby = 9999;//倍数限制
	
	//彩种ID
	this.play_id = '103';
	this.play_name = '福彩3D';
	//期次
	this.currIssue = '';
	
	//是否是合买
	this.isUnion = false;

	//选中号码时要打开的玩法的文件
	this.play_file = new Array();
}
//初始化
Lottery_3d.prototype.init = function(){
	
	getID("win_stop").checked = false;
	getID("is_repeat").checked = false;
	getID("order_timesby").value = "1";
	
	getID("order_timesby_msg").innerHTML = "（最高"+this.limit_timesby+"倍）";	  
	this.load_lot_info();
}
//设置销售状态
Lottery_3d.prototype.load_lot_info = function(){
	load_script(this.api_url + "lot_info.js?_t=" + new Date().getTime());

}
Lottery_3d.prototype.check_lot_info = function(arr){
	try{
		if(!arr || !arr[this.play_id])return;
		var lot_info = arr[this.play_id];
		if(lot_info['valid'] == "0"){
			if(getID2("lot_status_img")){
				getID2("lot_status_img").src="images/curr_no_info_stop.jpg";
			}
			if(getID2("submit_button")){
				getID2("submit_button").src="images/submit_button_stop.png";
				getID2("submit_button").onclick=function (){alert('暂停销售');return false};
				getID2("submit_button").onmouseover=function (){return false};
				getID2("submit_button").onmouseout=function (){return false};
			}
			if(getID2("def_sms_link")){
				getID2("def_sms_link").onclick=function (){alert('暂停销售');return false};
			}

		}
		if(lot_info['addPrize'] == "1"){
			if(getID2("lot_title_img")){
				getID2("lot_title_img").src="images/lot_title_3d_add.jpg";
			}
		}
	}catch(e){
		//alert(e)
	}


}
//初始化
Lottery_3d.prototype.sel_init = function(){
	this.sel_num = new Array();
	for(var i in this.play_info){
		this.sel_num[this.play_info[i][0]] = new Array();
	}
}
Lottery_3d.prototype.get_play_info = function(v, is_bracket){
	var r = "";
	for(var i in this.play_info){
		if(this.play_info[i][0] == v)r = this.play_info[i][1];
	}
	return is_bracket ? "["+r+"]" : r;
}
//将被选中的投注内容在号码区高亮
Lottery_3d.prototype.content_select = function(obj){
	var selIndex = obj.selectedIndex;
	//var text = obj.options[selIndex].text;
	var val = obj.options[selIndex].value;
	var arr = val.split("#");
	if(this.play_file[arr[1]]){
		_xtab_play.select_item(this.play_file[arr[1]][0]);
		window.lottery_frame.location.href = this.play_file[arr[1]][1] + "?type=" + arr[1] + "&num=" + arr[0];
	}
}


//显示追号列表
Lottery_3d.prototype.show_repeat = function(is_repeat){

	if(!is_repeat.checked){
		//getID("hall_account_info").style.display = "";
		this.hide_repeat();
		try{
			var select_all_obj = getID("select_all");
			select_all_obj.checked = false;
			lot.repeat_issue_num_to_all(select_all_obj);	
			
			getID("win_stop").checked = false;
			getID("win_stop").disabled = true;
		}catch(e){}
		return;
	}
	//getID("hall_account_info").style.display = "none";
	getID("win_stop").checked = true;
	getID("win_stop").disabled = false;

	var order_repeat_div = getID("order_repeat_div");
	order_repeat_div.style.display = "";
	var order_timesby_div = getID("order_timesby");
	order_timesby_div.disabled = true;
	order_timesby_div.style.backgroundColor = "#efefef";

	this.get_repeat_list();


}

//隐藏追号列表
Lottery_3d.prototype.hide_repeat = function(){
	var order_repeat_div = getID("order_repeat_div");
	order_repeat_div.style.display = "none";
	var order_timesby_div = getID("order_timesby");
	order_timesby_div.disabled = false;
	order_timesby_div.style.backgroundColor = "";

}
//读取追号期次列表
Lottery_3d.prototype.get_repeat_list = function(arr){
	this.clear_repeat_list();
	load_script(this.api_url + "lot_repeat_issue_"+this.play_id+".js?_t=" + new Date().getTime());
}
//清空追号列表数据
Lottery_3d.prototype.clear_repeat_list = function(){
	var table = getID("order_repeat_table");
	while(table.rows.length>1){
		table.deleteRow(1);
	}
}
//向追号列表添加数据
Lottery_3d.prototype.add_repeat_list = function(arr){
	var table = getID("order_repeat_table");
	
	var row = table.insertRow(-1);
	var index = row.rowIndex;
	
	this.currIssue = getID('curIssue').innerHTML;
	var timeby = 1;
	if(this.currIssue == arr['issue_num']){
		var checked = " checked";
		var checked_txt = " <font class='text_red4_12px'>[当前期]</font>";
		timesby = parseInt(getID('order_timesby').value, 10);
		if(!timesby || isNaN(timesby) || timesby > this.limit_timesby){
			timesby = 1;
		}
	}else{
		var checked = "";
		var checked_txt = "";
		timesby = 1;
	}
	
	row.className = 'list_item1';

	arr['begin_time'] = get_date("Y-m-d H:i", arr['begin_time']);
	var arr2 = new Array();
	arr2['checkbox'] = '<input type="checkbox" onclick="' + this.name + '.repeat_issue_num_sel(this);" name="repeat_issue_num[' + index + ']" id="repeat_issue_num' + index + '" value="' + arr['issue_num'] + '" ' + checked + '/> ' ;
	arr2['issue_num'] = "<strong>" + arr['issue_num'] + "</strong>" + checked_txt;
	arr2['timeby'] = '倍数：<strong>' +
                         ' <input name="repeat_timesby[' + index + ']" type="text" class="input" id="repeat_timesby' + index 
				+ '" value="' + timesby + '" style="font-weight:normal" size="6" maxlength="4" onfocus="input_onfocus(this);" onblur="input_onblur(this);replaceNum(this, 1);' + this.name + '.repeat_stat(this, ' + index + ')" onkeyup="' + this.name + '.repeat_stat(this, ' + index + ')" />' +
                        '</strong>';
	arr2['amount'] = '<strong class="text_red_12px" id="repeat_total_amount' + index + '">0</strong>元';
	arr2['begin_time'] = arr['begin_time'];

	for (var i in arr2) {
		var objCell = document.createElement("td");
		if(i== "issue_num"){
			objCell.align = "left";
		}else if(i== "checkbox"){
			objCell.style.paddingRight="4px";
			objCell.align = "right";
		}else{
			objCell.align = "center";
		}
		objCell.valign = "middle";
		objCell.innerHTML = arr2[i];
		row.appendChild(objCell);
	}
	
	
}
//选择所有期
Lottery_3d.prototype.repeat_issue_num_to_all = function(obj){
	var form = obj.form;
	var sel = "repeat_issue_num";
	var s = obj.checked;
	for (var i=0;i<form.elements.length;i++)
	{
		var e = form.elements[i];
		//if (e.value == this.currIssue){
		//	continue;
		//}
		if (e.name.substring(0, sel.length) == sel)
			e.checked = s;
	}
	this.stat_selected();
	
}
//选择当前期
Lottery_3d.prototype.repeat_issue_num_sel = function(obj){
	//if (obj.value == this.currIssue){
	//	obj.checked=true;
	//}
	this.stat_selected();
	
}
//更新所有倍数
Lottery_3d.prototype.repeat_timesby_to_all = function(obj){
	var form = obj.form;
	var sel = "repeat_timesby";
	var val = toInt(obj.value);
	if(val > this.limit_timesby){
		val = obj.value = this.limit_timesby;
	}
	if(val < 1){
		val = obj.value = 1;
	}
	for (var i=0;i<form.elements.length;i++)
	{
		var e = form.elements[i];
		if (e.name.substring(0, sel.length) == sel){
			e.value = val;
		}
	}
	this.stat_selected();
}
//更新当前期次金额
Lottery_3d.prototype.repeat_stat = function(obj, index){
	var form = obj.form;
	var total_id = getID("repeat_total_amount" + index);
	var timesby = toInt(obj.value);

	if(timesby > this.limit_timesby){
		timesby = obj.value = this.limit_timesby;
	}
	if(timesby < 1){
		timesby = 1;
		obj.value = "";
	}
	this.stat_selected();
}
//更新所有期次金额
Lottery_3d.prototype.repeat_all_stat = function(){
	var timesby_total = 0;
	var table = getID("order_repeat_table");
	this.repeat_issue_num = new Array();
	for(var i = 1 ;i < table.rows.length; i++){
		var index = table.rows[i].rowIndex;
		var is_repeat = getID("repeat_issue_num" + index).checked;
		var issue_num = getID("repeat_issue_num" + index).value;
		var total_id = getID("repeat_total_amount" + index);
		var timesby = toInt(getID("repeat_timesby" + index).value);
		if(timesby==0)timesby=1;
		total_id.innerHTML = this.amount*this.total_sum*timesby;
		if(is_repeat){
			timesby_total += timesby;
			this.repeat_issue_num.push(issue_num + "#" + timesby);
		}
	}
	return timesby_total > 0 ? timesby_total : 1;
}

//提交投注号码
Lottery_3d.prototype.submit = function(frm){
/*
	for(var i in this.sel_num){
		var arr = this.sel_num[i];
		for(var j in arr){
			alert(arr[j]['val']);
			alert(arr[j]['txt']);
		}
	}
return false;
*/


	if(!getID("license").checked){
		this.msg_show("必须同意协议才可以购买");
		return false;
	}
	try{
		if(!lot_time.currIssue){
			this.msg_show("服务器故障，暂时无法投注");
			return false;
		}
		if(lot_time.close == "1" && lot_time.close_msg.length > 0){
			this.msg_show(lot_time.close_msg);
			return false;
		}
		if(!lot_time.getPickStatus()){
			this.msg_show("第 " + lot_time.currIssue + " 期投注已经结束");
			return false;
		}
	}catch(e){ }


	var repeat = 1;
	var issue_num = "";
	var timesby = parseInt(getID('order_timesby').value, 10);
	if(timesby > this.limit_timesby){
		this.msg_show("请选择" + this.limit_timesby + "以下的倍数");
		return false;
	}
	if(getID("is_repeat").checked){
		timesby = this.repeat_all_stat();
		repeat = this.repeat_issue_num.length;
		repeat = repeat < 1 ? 1 : repeat;
		issue_num = this.repeat_issue_num.join("|");
		if(repeat < 2){
			this.msg_show("请至少选择2期追号");
			return false;
		}

	}else{
		timesby = parseInt(getID('order_timesby').value, 10);
	}

	var sel_box = getID('select_order_content');
	var content = "";
	var content_show = "";
	var len = sel_box.length;
	if(len == 0){
		this.msg_show("请选择投注号码");
		return false;
	}
	if(this.isUnion){
		try{
			if(!submit_check())return false;
		}catch(e){}
	}
	for(var i = 0; i < len ; i++){
		var str = sel_box.options[i].value;
		content += (str + "|");

		var str = sel_box.options[i].text;
		content_show += (str + "\n");
	}
	content = content.substr(0, content.length - 1);
	getID('order_content').value = content;

	this.currIssue = getID('curIssue').innerHTML;

	var total = 0;
	for(var i in this.total){
		total += this.total[i];
	}
	this.total_sum = total;



	var timesby_total_amount = this.amount*total*timesby;
	var total_amount = this.amount*total;


	getID('is_union').value = this.isUnion ? "Y" : "N";
	getID('lottery_id').value = this.play_id;
	getID('order_issue_num').value = this.currIssue;
 
	getID('order_repeat_issue_num').value = issue_num;
	//getID('order_content_show').value = content_show;

	getID('stat_total').value = total;
	getID('stat_repeat_total').value = repeat;
	getID('stat_total_amount').value = timesby_total_amount;

	x_open('请确认您的支付信息',x_open_loading_page, 630,390,'center','middle');
	frm.target='x_open_frame';
	frm.submit();
	
	//disable_button("submit_button", true);
	//disabled_button("submit_button", false);

	//清空所有号码
	//this.clear_all();
 
	return false;
}

Lottery_3d.prototype.ball_rand = function(){

}
Lottery_3d.prototype.insert = function(){
	
}
//向号码区插入号码
Lottery_3d.prototype.select_insert = function(play, sel_text, sel_value, total){
	var sel_box = getID('select_order_content');
	var total_index = sel_box.options.length;
	sel_box.options[sel_box.options.length] = new Option(sel_text,sel_value);
	this.total[total_index] = total;

	var sel_index = this.sel_num[play].length;
	this.sel_num[play][sel_index] = new Array();
	this.sel_num[play][sel_index]['txt'] = sel_text;
	this.sel_num[play][sel_index]['val'] = sel_value;
	
	this.stat_selected();
}
//删除号码区已经选择的行
Lottery_3d.prototype.select_del_line = function(){
	var sel_box = getID('select_order_content');

	var index = sel_box.selectedIndex;
	if(index == -1){
		this.msg_show("请选择需要删除的项！");
		return;
	}
	while(index != -1){
		sel_box.options[index] = null;
		this.total.splice(index, 1);
		var index = sel_box.selectedIndex;
	}
	this.stat_selected();
	
}

//统计已选号码
Lottery_3d.prototype.stat_selected = function(isBlur){
	var total = 0;
	for(var i in this.total){
		total += this.total[i];
	}
	this.total_sum = total;
	var is_repeat = false;
	try{
		is_repeat = getID("is_repeat").checked;
	}catch(e){}

	if(is_repeat){
		var timesby = this.repeat_all_stat();
	}else{
		var timesby = parseInt(getID('order_timesby').value, 10);
		if(!timesby || isNaN(timesby) || timesby > this.limit_timesby){
			timesby = 1;
		}
		if(isBlur)getID('order_timesby').value = timesby;
	}



	
	var total_money = this.amount*total*timesby;
	getID('count_order_total').innerHTML = total;
	getID('count_order_total_amount').innerHTML = total_money;
	this.timesby_total_amount = total_money;
	//if(this.isUnion){
		set_order_amount_total(total_money);
		check_union_total2();
	//}
}
//清空所有号码
Lottery_3d.prototype.clear_all = function(){
	getID('select_order_content').options.length=0;
	this.sel_init();
	this.total = new Array();
	try{
		getID('order_timesby').value='1';
		getID('is_repeat').checked=false;
		this.show_repeat(getID('is_repeat'));
		getID('win_stop').checked=false;
	}catch(e){
		getID('order_timesby').value='1';
		getID('union_title').value='';
		getID('union_comment').value='';
		getID('union_total').value='2';
		getID('together_rate').value='0';
		getID('union_zigou').value='1';
		getID('union_is_baodi').checked=false;
		var union_gongkai = document.getElementsByName('union_gongkai');
		union_gongkai[0].checked=true;
		union_gongkai[1].checked=false;
		getID("per_amount").innerHTML = "0.00";
		check_union_show_baodi();
	}
	this.clear_all_sub();
	this.stat_selected();
}
Lottery_3d.prototype.clear_all_sub = function(){
}


//向输入框显示结果
Lottery_3d.prototype.show_number = function(str){
	var select_order_content_id = getID('select_order_content')
	select_order_content_id.value=str;

}


//显示提示
Lottery_3d.prototype.msg_show = function(str){
	alert(str);

}
function set_lot_content(_content, _text, _total){
	if(_content.length<1)return;
	_content = _content.split("!");
	_text = _text.split("!");
	_total = _total.split("!");
	var sel_value = "";
	var sel_text = "";
	var total = "";
	var play = "";
	for(var i in _content){
		if(!_content[i])continue;
		sel_value = _content[i];
		play = sel_value.split("#");
		play = parseInt(play[1], 10);
		if(_text[i]){
			sel_text = _text[i];
		}else{
			sel_text = sel_value.replace("#"+play, "#"+lot.get_play_info(play, true));
		}
		total = parseInt(_total[i], 10);

		lot.select_insert(play, sel_text, sel_value, total);
	}
}


lot = new Lottery_3d('lot');

