﻿var http=null;
var agentHandler="PostHandler.ashx"
var POST_URL="http://miscapi0.idc2/supergame/PostCampaign.ashx"; 
//var POST_URL=unescape(window.location.href).replace( "file:///",""); 
//POST_URL =POST_URL.substring(0,POST_URL.lastIndexOf("/")+1).replace("\\","/"); 
//POST_URL+="PostCampaign.ashx"; 
var loadNum=false;
var subPrice=false;
var windowState=false;
var windowAlert=false;
function postAgent(_method,_parameter,_result){

   // $("research_result").innerHTML = "提交中...";

    if(window.XMLHttpRequest) // Mozilla
    { 
        http=new XMLHttpRequest();
        if(http.overrideMimeType)
            http.overrideMimeType("text/html;");
    }
    else if(window.ActiveXObject) // IE
    {
        try
        {
            http=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                http=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {
            }
        }
    }
	if(http==null) return;
    http.onreadystatechange = _result;
    http.open("POST", agentHandler, true);
    //HttpRequest.setRequestHeader("if-Modified-Since","0");
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); 
    try
    {
        http.send("request_url="+POST_URL+"&method="+_method+_parameter);
    }
    catch (e)
    {
    }
}

function getCombineNum(){
    if(!checkIsLoad())   
    {
        loadNum=true;
        postAgent('getCombineNum','',postGetCombineNumProcessResponse);
        $("tbProId1").value="请输入商品ID";
        pf1=true;
        $("tbProId2").value="请输入商品ID";
        pf2=true;

    }
}

function postGetCombineNumProcessResponse(){
    if(http!=null){
        if(http.readyState == 4){
            if(http.status != 200)
                return;
            var result = new Array();
            result = eval(http.responseText);
            if(result!="false")
            {
                if(result[0] == "-1")
                {
                    create_bg();
                    showMsgBox('divLogin','','',overLogin,getCombineNum);
                }
                else if(result=="unlogin")
                {
                    create_bg();
                    showMsgBox('divLogin','','',overLogin,getCombineNum);
                }
                else
                {
                    $("lbCombineNum").innerHTML=result[1];
                    //$("lbCombineTypeName").innerHTML=result[3];
                    $("hdCombineNum").value=result[1];
                    $("hdCombineTypePath").value=result[2];
                    $("btGetNum").onclick= function(){doOpenDivState(1,"您的操作过快。请5秒后再进行操作。");}
                    setTimeout("$(\"btGetNum\").onclick= function(){getCombineNum();}",5000);
                }
            loadNum=false;
            if(windowAlert)
               doCloseDivState();
            }
        }
    }
}

function iframeLoaded(){
                 if($('MsgBoxFrame').src!=""&&$('MsgBoxFrame').src!=window.location.href)
               { $('MsgBoxFrame').contentWindow.document.getElementById('txtUsername').autocomplete="off";
                $('MsgBoxFrame').contentWindow.document.forms[0].autocomplete="off";
                $('MsgBoxFrame').contentWindow.document.getElementById('txtPassword').focus();
                $('MsgBoxFrame').contentWindow.document.getElementById('txtUsername').focus();
                $('MsgBoxFrame').contentWindow.document.getElementById('txtUsername').onclick=null;
                
              //  $('MsgBoxFrame').contentWindow.document.getElementById('handle').getElementsByTagName("div")[5].style.display="none";
               }

                    }
                    
function doRaffle(){
    if($("hdCombineNum").value.length==0||$("hdCombineTypePath").value.length==0)
    {
        doOpenDivState(1,"请先点击开始游戏按钮获得商品价格。");
    }
    else if($("tbProId1").value.length==0||$("tbProId2").value.length==0)
    {
        doOpenDivState(1,"请填写商品ID");
    }
    else if(!checkIsInteger($("tbProId1").value)||!checkIsInteger($("tbProId2").value))
    {
        doOpenDivState(1,"您输入的商品ID号不符合规则。商品ID号为阿拉伯数字！");
    }
//    else if($("tbProId1").value.length!=8||$("tbProId2").value.length!=8)
//    {
//        doOpenDivState(1,"您输入的商品ID号不符合规则。商品ID号为8位阿拉伯数字！");
//    }
    else if($("tbProId1").value==$("tbProId2").value)
    {
        doOpenDivState(1,"您输入的两种商品ID号不能重复！");
    }
    else
    {
        if(!checkIsLoad())   
        {
            subPrice=true;
            var param;
            param="&pid1="+$("tbProId1").value+"&pid2="+$("tbProId2").value+"&num="+$("hdCombineNum").value
            postAgent('doRaffle',param,postDoRaffleProcessResponse);
        }
    }
}
function postDoRaffleProcessResponse(){
    if(http!=null){
        if(http.readyState == 4){
            if(http.status != 200)
                return;
            var result = new Array();
            result = eval(http.responseText);
            if(result!="false")
            {
                if(result.length != 2)
                {               
                         doOpenDivState(3,"正在为您进行抽奖，请稍候...");
                         setTimeout("overSubmit(0)",2000);
                }
                else
                {
                     if(result[0] == "-1")
                        {
                            create_bg();
                            showMsgBox('divLogin','','',overLogin,doRaffle);
                        }
                        if(result[0] == "-2")
                        {
                            doOpenDivState(1,"很遗憾您回答错误，再接再厉哦！");
                        }
                        else
                        {
                            switch(result[1])
                            {
                                case "-1":
                                 doOpenDivState(1,"您今天已经参加了10次，请明天再来！");
                                 //getCombineNum();
                                break;
                                case "-2":
                                 doOpenDivState(1,"很遗憾您回答错误，再接再厉哦！");
                                break;
                                case "-3":
                                 doOpenDivState(3,"正在为您进行抽奖，请稍候...");
                                 setTimeout("overSubmit(0)",2000);
                                break;
                                default:
                                if(result[1]>0)
                                {
                                 doOpenDivState(3,"正在为您进行抽奖，请稍候...");
                                 $("Button1").onclick= function(){window.location.reload();}
                                 $("btCloseWindow").style.display="none";
                                 setTimeout("overSubmit("+result[1]+")",2000);
                                }
                                else
                                {
                                    getCombineNum();
                                }
                                break;
                            }
                        }
                }
            }
            subPrice=false;
        }
    }
}

function overLogin(f)
{
    f();
    close_bg();
}

function overSubmit(types)
{
    if(types==0)
    {
        doCloseDivState()
        doOpenDivState(1,"很遗憾您未中奖，不要泄气再来一次吧！");
    }
    else
    {
        doCloseDivState()
        doOpenDivState(2,types); //getCombineNum();
    }
   // $("divState_Loading").style.display="none";
    getCombineNum();
}


function doOpenDivState(types,sessions){
    if(windowState)
        doCloseDivState();
    if(types==1)
    {
        $("notice").innerHTML=sessions;
        $("notice").style.backgroundImage="url(images/no.gif)";
        $("Button1").style.display="";
    }
    else if(types==2)
    {
        $("notice").innerHTML="恭喜您回答正确，您已获得"+sessions+"元礼券奖励！<br/>请进入<a href=\"http://order.dangdang.com/mycoupons.aspx\" target=\"_blank\">我的当当</a>查询。";
        $("notice").style.backgroundImage="url(images/yes.gif)";
        $("Button1").style.display="";

    }
    else if(types==3)
    {
        $("notice").innerHTML=sessions;
        $("notice").style.backgroundImage="url(images/loading.gif)";
        $("Button1").style.display="none";
    }
    else if(types==4)
    {
        $("notice").innerHTML=sessions;
        $("notice").style.backgroundImage="url(images/loading.gif)";
        $("Button1").style.display="";
    }
     create_bg();
     $("divState").style.display="";
     center($("divState"));
     windowState=true;
}

function doCloseDivState(){
     close_bg();
     $("divState").style.display="none";
     windowState=false;
        if(windowAlert)
               windowAlert=false;
}


function create_bg(){
	var bg=document.createElement("DIV");
	with(bg.style){
        zIndex = 2;
        position = "absolute";
        top = 0;
        left = 0;
        border = "0px";
        backgroundColor = "#ffffff";
	    if (isIE())
        {
		    width=document.body.scrollWidth;
		    height=document.body.scrollHeight;
            filter = " Alpha(Opacity=50)";
	        //document.body.style.overflow="hidden";
        }
        else
        {
		    width=document.documentElement.scrollWidth+"px";
		    height=document.documentElement.scrollHeight+"px";
            opacity = 0.5;
            //document.documentElement.style.overflow = "hidden";
        } 
        overflow = "hidden";
        display = "";
    }
    bg.id = "divCover";
	document.body.appendChild(bg);
	}

function close_bg(){
	$("divCover").parentNode.removeChild($("divCover"));
	}
	
	

//滚动提示
var Sroll={
scrollInfo:[],
isStoped :false, 
oScroll:[],
preTop : 0, 
curTop : 0, 
stopTime : 0, 
oScrollMsg :[],
si:[],

init: function(){
try{  
                 Sroll.oScroll = document.getElementById("scrollWrap"); 
                 Sroll.oScrollMsg = document.getElementById("scrollMsg");
                 
                 
                    Sroll.oScrollMsg.innerHTML=Sroll.scrollInfo;
                 
                 with(Sroll.oScroll){  noWrap = true;  } 
                 Sroll.oScroll.onmouseover = new Function('Sroll.isStoped = true'); 
                 Sroll.oScroll.onmouseout = new Function('Sroll.isStoped = false'); 
                // Sroll.oScroll.appendChild(Sroll.oScrollMsg.cloneNode(true)); 
                 Sroll.si= setInterval('clearInterval(Sroll.si);Sroll.init_srolltext(); ', 5000); 
                }
                catch(e) {} 
},
init_srolltext:function(){ 
 Sroll.oScroll.scrollTop = 0; 
 setInterval('Sroll.scrollUp()', 15); 
},
 
scrollUp:function(){ 
     if(Sroll.isStoped) return; 
     Sroll.curTop += 1; 
     if(Sroll.curTop == 101) { 
         Sroll.stopTime += 1; 
         Sroll.curTop -= 1; 
         if(Sroll.stopTime == 180) { 
             Sroll.curTop = 0; 
             Sroll.stopTime = 0; 
         } 
     }
     else{
         Sroll.preTop = Sroll.oScroll.scrollTop; 
         Sroll.oScroll.scrollTop += 1; 
         if(Sroll.preTop == Sroll.oScroll.scrollTop){ 
             Sroll.oScroll.scrollTop = 0; 
             Sroll.curTop = 100; 
         } 
     } 
} 

}
   var pf1=true;
       var pf2=true;
       function doFocus(num)
       {
       if(num==1)
       {
           if(pf1)
            {
                $("tbProId1").value="";
                pf1=false;
           }
       }
       else
       {
           if(pf2)
           {
                $("tbProId2").value="";
                pf2=false;
           }
           
       }
       
       }
        function doBlur(num)
       {
       if(num==1)
       {
           if($("tbProId1").value=="")
            {
                $("tbProId1").value="请输入商品ID";
                pf1=true;
           }
       }
       else
       {
           if($("tbProId2").value=="")
           {
                $("tbProId2").value="请输入商品ID";
                pf2=true;
           }
           
       }
       
}



function initPage(){
    $("tbProId1").value="请输入商品ID";
    pf1=true;
    $("tbProId2").value="请输入商品ID";
    pf2=true;
    $("hdCombineNum").value="";
    $("hdCombineTypePath").value="";
    if(isIE())
    {
        var browser=navigator.appName 
        var b_version=navigator.appVersion 
        var version=b_version.split(";"); 
        var trim_Version=version[1].replace(/[ ]/g,""); 
        if(browser=="Microsoft Internet Explorer" && trim_Version=="MSIE6.0") 
        { 
          $("Button1").style.marginTop="10px";
        } 
    }

}

function checkIsLoad()
{
    if(loadNum)
    {
        doOpenDivState(4,"系统正在为您获取组合金额，请稍候");
        windowAlert=true;
        return true;
    }
    else if(subPrice)
    {
        doOpenDivState(4,"系统正在为您进行抽奖，请稍候");
        //windowAlert=true;
        return true;
    }
    else
        return false;
        
}

//ComFunction
function isIE(){ 
   return (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1) ;
} 

function checkIsInteger(str) { 
    if(str == "") 
        return false; 
    if(/^(\-?)(\d+)$/.test(str)) 
        return true; 
    else 
        return false; 
} 


function getposOffset(what, offsettype)
{ 
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop; 
    var parentEl=what.offsetParent; 
    while (parentEl!=null)
    { 
        totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop; 
         parentEl=parentEl.offsetParent; 
    } 
    return totaloffset; 
}

function $(id){
     return document.getElementById(id);
}

function center(win)//将主窗口置于网页正中
{ 
    var body_width;
    if (isIE())
            {
		        body_width=document.body.scrollWidth;
            }
            else
            {
		        body_width=document.documentElement.scrollWidth;
            } 
    var s = parseInt((body_width -win.offsetWidth)/2);
    win.style.left =s+"px";
}
function center_login(win)//将主窗口置于网页正中
{ 
    var body_width;
    if (isIE())
            {
		        body_width=document.body.scrollWidth;
            }
            else
            {
		        body_width=document.documentElement.scrollWidth;
            } 
    var s = parseInt(win.offsetWidth+(body_width -win.offsetWidth)/2);
    win.style.left =s+"px";
}

function  showMsgBox(a,b,c,d,e){close_bg();}