﻿var hexunAPI = {};
hexunAPI.callMethod = function(APIMethod, params, listener, testingURL, attempts) {
	//debugger;
	if (typeof params != 'object') params = {}; // because we are going to stick a few things in even if no params are passed
	params.method = APIMethod; // see? And this also makes sure a method parameter is not passed
	
	var RESTURLROOT = '/ResponseClient.aspx';
	var RESTURL='';
	for (var p in params) {
		if(RESTURL=='')
			RESTURL=p + '=' + escape_utf8(params[p]);
		else
			RESTURL+= '&' + p + '=' + escape_utf8(params[p]);
	}
	
	params.RESTURL = RESTURL; // again. we stick this in here because we pass params to the callback, and it might want to see the URL
	
	var attempts = (attempts == undefined) ? 1 : attempts;
	var req = new XMLHttpRequest();
	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.responseText == '' && attempts<2) {
					attempts++;
					req.abort();
					hexunAPI.callMethod(APIMethod, params, listener, testingURL, attempts);
				} else {
					hexunAPI.handleResponseForTitle(req.responseXML, APIMethod, params, req.responseText, listener);
				}
			}
		}
		
		if (testingURL) RESTURLROOT = testingURL;
		req.open('POST', RESTURLROOT);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		req.send(RESTURL);
	}
}


hexunAPI.getCallBackName = function (dotted) {
	return dotted.split('.').join('_')+'_onLoad';
}

hexunAPI.handleResponseForTitle = function(responseXML, APIMethod, params, responseText, listener) {
	if (!responseXML) { //OPERA!
		var success = (responseText.indexOf('stat="ok"') > -1) ? true : false;
	} else {
		var success = (responseXML.documentElement && responseXML.documentElement.getAttribute('stat') == 'ok') ? true : false;
	}
	listener = (listener) ? listener : this;
	listener[this.getCallBackName(APIMethod)](success, responseXML, responseText);
}


function SendKissAndFlower(userID,userType)
        {               
            //debugger;
            SaveKissAndFlower(userID,userType);
        }
        SaveKissAndFlower = function(userID,userType)
        {
           var listener = document.getElementById("FormKissFlower");
           userType = String(userType);
           listener.hexun_FinanceGirl_KissFlower_onLoad = function(success, responseXML, responseText) 
	       {
	            if (success) 
		        {
			        var actStr = "";
			         if (userType == '1')
			         {
			            actStr = "献花";
			         }
			         if (userType == '0')
			         {
			            actStr = "献吻";
			         }

			        if(responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="1")
			        {
				         //debugger;
				         if (userType == '1')
				         {
				            document.getElementById("formFlowerCount").innerHTML = Number(document.getElementById("formFlowerCount").innerHTML)+1;
				            if (document.getElementById("formFlowerCount2"+userID)!= null)
			                    document.getElementById("formFlowerCount2"+userID).innerHTML = Number(document.getElementById("formFlowerCount2"+userID).innerHTML)+1;
				         }
				         if (userType == '0')
				         {
				            document.getElementById("formKissCount").innerHTML = Number(document.getElementById("formKissCount").innerHTML)+1;
				         	if (document.getElementById("formKissCount2"+userID)!= null)
			                    document.getElementById("formKissCount2"+userID).innerHTML = Number(document.getElementById("formKissCount2"+userID).innerHTML)+1;

				         }
				         alert(actStr + "成功！");
			        }
			        else if(responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="2")
			        {
			            alert("请选登陆再来"+actStr+"！");
			        }
		            else 
		            {
			            alert(actStr+"失败！");
		            }
		        }
		        else
		        {
		             alert("错误！");
		        }
		        //closeWindow();
           }
           hexunAPI.callMethod('hexun.FinanceGirl.KissFlower', {userIDParm:userID,userTypeParm:userType}, listener,'/ResponseClient.aspx');
           return;
        }
        function addVote(event,userID,userName,voteUserState,KissCount,FlowerCount)
        {
            //debugger;
            //if (loginUser == 0)
            //{
                //window.document.location.href ="http://reg.hexun.com/login.aspx?gourl=http%3a%2f%2flady.hexun.com%2f/"+userID+"/default.html";
                //return;
            //}
            SaveAddVote(event,userID,userName,voteUserState,KissCount,FlowerCount);
        }
        SaveAddVote = function (event,userID,userName,voteUserState,KissCount,FlowerCount)
        {
            //debugger;
            var listener = document.getElementById("FormVote");
		    var messContent="";
		    var messBot = "<div class='bottom'></div>";
		    var objPos = mousePosition(event);
            listener.hexun_FinanceGirl_AddVote_onLoad = function(success, responseXML, responseText) 
	        {
	            //debugger;
	            if (success) 
		        {
			        if(responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="0")
			        {
				        //debugger;
			            document.getElementById("FormVoteDayCount"+userID).innerHTML = Number(document.getElementById("FormVoteDayCount"+userID).innerHTML)+1;
			            document.getElementById("FormVoteCount"+userID).innerHTML = Number(document.getElementById("FormVoteCount"+userID).innerHTML)+1;
				        //alert("投票成功！");
		            //    var objPos = mousePosition(event);
                        messContent="<div class='middle'><p class='tpfont'>感谢您为 <b><a href='http://lady.hexun.com/" + userID + "/default.html'>"+userName+"</a></b> 投票加油！</p><div><img src='/img/window_img03.gif' /></div><div align='center'><font color='#9D9D9D'>你还可以送她</font><a href=\"javascript:SendKissAndFlower('"+userID+"',0)\" >香吻     <img src='/img/game_img03_1.gif' width='18' height='19' align='absmiddle' border='0'/></a> ：<font class='org'><b id='formKissCount2"+userID+"' name='formKissCount2"+userID+"'>"+KissCount+"</b></font>&nbsp;&nbsp;&nbsp;<a href=\"javascript:SendKissAndFlower('"+userID+"',1)\" >送花     <img src='/img/game_img03.gif' width='10' height='19' align='absmiddle' border='0'/></a> ：<font class='org'><b id='formFlowerCount2"+userID+"' name='formFlowerCount2"+userID+"'>"+FlowerCount+"</b></font></div><div><img src='/img/window_img03_1.gif' /></div></div><div class='bottom'><p align='center' style='padding-top:50px'>　<a href='http://lady.hexun.com/money.html' target=_blank><img src='/img/window_img05.gif' border='0' align='absmiddle'/></a>&nbsp;&nbsp;<a href='http://lady.hexun.com/Invitation.html' target=_blank >邀请好友</a> | <a href='http://lady.hexun.com/game.aspx' target=_blank >股市淘金</a> </p></div>";
                        showMessageBox('友情提示：',messContent,objPos,335);
			        }
			        else if (responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="1")
			        {
			            //alert("感谢您的支持，您在一天内已经为她投过票了！");
		                
                        messContent="<div class='middle'><p class='tpfont'>感谢您为 <b><a href='http://lady.hexun.com/" + userID + "/default.html'>"+userName+"</a></b> 投票加油！<br>您在一天内已经为她投过票了,明天再投吧！</p></div>" + messBot;
                        showMessageBox('友情提示：',messContent,objPos,335);

			        }
			        else if (responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="2")
			        {
			            //alert("感谢您的支持，但是该用户目前不符合投票条件！");
		            //    var objPos = mousePosition(event);
                        messContent="<div class='middle'><p class='tpfont'>感谢您的支持，但是该用户目前不符合投票条件！</p></div>"+messBot;
                        showMessageBox('友情提示：',messContent,objPos,335);

			        }
                    else if(responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="4")
                    {
                        //alert("感谢您的支持，请选登录再给您喜欢的选手投票！");
		                //var objPos = mousePosition(event);
                        messContent="<div class='middle'><p class='tpfont2'>　　为了进一步规范大赛投票管理制度，提高参赛选手的投票质量和安全系数，确保本次大赛公开、公平、公正性和严肃性进行.网友登录才可投票,如您还不是我们会员,请立刻<font class='org'><a target='_blank' href='http://reg.hexun.com/?urlreferrerhost=http%3a%2f%2flady.hexun.com%2findex.aspx'>注册</a></font>。</p></div>  <div class='bottom'>  	<div class='windowlogin'><div><img src='/img/window_img04.gif' /></div><form id='form1' name='form1' method='post' action='http://reg.hexun.com/login.aspx?gourl=http://lady.hexun.com/" + userID  + "/default.html' onsubmit='javascript:return userLogin();'><span>用户名 <input name='TextBoxUserName' id='TextBoxUserName' tabindex='1'  type='text' class='login_namebtn'/>密码 <input  name='TextBoxPassword' id='TextBoxPassword' tabindex='2'  type='password' class='login_namebtn'/><input type='hidden' name='LoginStateName' id='LoginStateName' value='1'/><input type='hidden' name='LoginStateAuto' id='LoginStateAuto' value='1'/><input type='hidden' name='submitsign' id='submitsign' value='0' /><input type='hidden' name='hiddenReferrer' id='hiddenReferrer' value=''/><input type='hidden' name='TextGpic' id='TextGpic' value='' /></span><input type='image' src='/img/game_btn01.gif' border='0' align='absmiddle'/><a href='http://reg.hexun.com/?urlreferrerhost=http%3a%2f%2flady.hexun.com%2findex.aspx'><img src='/img/game_btn02.gif' border='0' align='absmiddle' style='margin-left:8px;'/></a> <a href='http://reg.hexun.com/GetPasswordPre.aspx' target=_blank><img src='/img/game_btn03.gif' border='0' align='absmiddle'/></a><div><img src='/img/window_img04_1.gif' /></div></div></form></div>";
                        showMessageBox('友情提示：',messContent,objPos,335);
                        //window.document.location.href ="http://reg.hexun.com/login.aspx?gourl=http%3a%2f%2flady.hexun.com%2f/"+userID+"/default.html";
                        
                    }
                    else if(responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="5")
                    {
                       var answer=conform("当前登录用户还未成功激活！要去激活此帐号吗？"); 
                       if (!answer)
                       {
                            window.location = "http://reg.hexun.com/queren.aspx";
                       }

                    }
		            else 
		            {
			            alert("失败！");
		            }
		        }
		        else
		        {
		             alert("错误！");
		        }
           }
           hexunAPI.callMethod('hexun.FinanceGirl.AddVote', {userIDParm:userID,voteUserStateParm:voteUserState}, listener,'/ResponseClient.aspx');
           return;
        }
        function joinGame(event)
        {
            //alert('df');
            SaveJoinGame(event)
        }        
        SaveJoinGame = function (event)
        {
            var listener = document.getElementById("joinGameDiv");
		    var messContent="";
		    var objPos = mousePosition(event);
            listener.hexun_FinanceGirl_JoinGame_onLoad = function(success, responseXML, responseText) 
	        {
	            if (success) 
		        {
			        if(responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="0")
			        {
				        //alert("报名成功！");
                        messContent="<div class='middle'><p class='tpfont'>感谢您的参与，报名成功！</p><div align='center'></div></div><div class='bottom'></div>";
                        showMessageBox('友情提示：',messContent,objPos,335);
			        }
			        else if (responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="1")
			        {
                        messContent="<div class='middle'><p class='tpfont'>财经丽人是女性才有资格参加的活动！</p><div align='center'></div></div><div class='bottom'></div>";
                        showMessageBox('友情提示：',messContent,objPos,335);
			        }
			        else if (responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="2")
			        {
                        messContent="<div class='middle'><p class='tpfont'>您已经报过名，正在审核，请稍候！</p><div align='center'></div></div><div class='bottom'></div>";
                        showMessageBox('友情提示：',messContent,objPos,335);
			        }
			        else if (responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="3")
			        {
                        messContent="<div class='middle'><p class='tpfont'>您已经报名了，并通过审核，无需重复报名！</p><div align='center'></div></div><div class='bottom'></div>";
                        showMessageBox('友情提示：',messContent,objPos,335);
			        }
			        else if (responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="4")
			        {
                        messContent="<div class='middle'><p class='tpfont'>无此用户！</p><div align='center'></div></div><div class='bottom'></div>";
                        showMessageBox('友情提示：',messContent,objPos,335);
			        }
			        else if (responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="E20")
			        {
                        messContent="<div class='middle'><p class='tpfont2'>　　为了进一步规范大赛投票管理制度，提高参赛选手的投票质量和安全系数，确保本次大赛公开、公平、公正性和严肃性进行.网友登录才可投票,如您还不是我们会员,请立刻<font class='org'><a target='' href='http://reg.hexun.com/?urlreferrerhost=http%3a%2f%2flady.hexun.com%2findex.aspx'>注册</a></font>。</p></div>  <div class='bottom'>  	<div class='windowlogin'><div><img src='img/window_img04.gif' /></div><form id='form1' name='form1' method='post' action='http://reg.hexun.com/login.aspx?gourl=http%3a%2f%2flady.hexun.com%2findex.aspx' onsubmit='javascript:return userLogin();'><span>用户名 <input name='TextBoxUserName' id='TextBoxUserName' tabindex='1'  type='text' class='login_namebtn'/>密码 <input  name='TextBoxPassword' id='TextBoxPassword' tabindex='2'  type='password' class='login_namebtn'/><input type='hidden' name='LoginStateName' id='LoginStateName' value='1'/><input type='hidden' name='LoginStateAuto' id='LoginStateAuto' value='1'/><input type='hidden' name='submitsign' id='submitsign' value='0' /><input type='hidden' name='hiddenReferrer' id='hiddenReferrer' value=''/><input type='hidden' name='TextGpic' id='TextGpic' value='' /></span><input type='image' src='img/game_btn01.gif' border='0' align='absmiddle'/><a href='http://reg.hexun.com/?urlreferrerhost=http%3a%2f%2flady.hexun.com%2findex.aspx'><img src='img/game_btn02.gif' border='0' align='absmiddle' style='margin-left:8px;'/></a> <a href='http://reg.hexun.com/GetPasswordPre.aspx' target=_blank><img src='img/game_btn03.gif' border='0' align='absmiddle'/></a><div><img src='/img/window_img04_1.gif' /></div></div></form></div>";
                        showMessageBox('友情提示：',messContent,objPos,335);
			        }
			        else if (responseXML.documentElement.getElementsByTagName('friend')[0].firstChild.nodeValue=="E21")
			        {
                        messContent="<div class='middle'><p class='tpfont'>当前登录的用户还未激活！</p><div align='center'></div></div><div class='bottom'></div>";
                        showMessageBox('友情提示：',messContent,objPos,335);
			        }
			        else
			        {
                        messContent="<div class='middle'><p class='tpfont'>报名失败！</p><div align='center'></div><div class='bottom'></div>";
                        showMessageBox('友情提示：',messContent,objPos,335);
			        }
	            }
            }
            hexunAPI.callMethod('hexun.FinanceGirl.JoinGame', {joinGameParm:'add'}, listener,'/ResponseClient.aspx');
            return;
        }
        //登陆
        function userLogin()
        {
            var username = document.getElementById("TextBoxUserName").value;
            var password = document.getElementById("TextBoxPassword").value;
            if (username == "")
            {
                alert("用户名不能为空！");
                return false;
            }
            if (password == "")
            {
                alert("密码不能为空！");
                return false;
            }
            document.getElementById("submitsign").value = 1;
            //form1.submitsign.value=1;
            //form1.submit();
            return true;
        }
        function Click_Name()
        {
            if(!document.getElementById("LoginStateName").checked == true)
            {	
                //alert("选中可能产生密码安全问题，建议仅限于非公共场所使用。");	
                document.getElementById("LoginStateAuto").checked	= false;	
            } 
        }
        function Click_Auto()
        {
            if(document.getElementById("LoginStateAuto").checked == true)
            {
                //alert("选中可能产生密码安全问题，建议仅限于非公共场所使用。");
                //document.getElementById("LoginStateName").checked	= false;
                document.getElementById("LoginStateName").checked = true;
            }
        }
        ///

