var xdr;
var sorttypegbl;
var cookieEnabled;
function isCookieEnabled(){
//	cookieEnabled=(navigator.cookieEnabled)? true : false;
//	if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
//		document.cookie="testcookie=0";
//		cookieEnabled=(document.cookie=="testcookie=0")? true : false;
//		document.cookie=""; //erase dummy value
//	}
	document.cookie="testcookie=0";
	cookieEnabled=(document.cookie.indexOf("testcookie=0", 0)>=0)? true : false;
	return cookieEnabled;
}

function getCookie(sName) {
	var curvalue = null;
	if (isCookieEnabled() == false) 
		return "pcuidnocookie";
	var aCookie = document.cookie.split("; ");
	for (var j=0; j < aCookie.length; j++){
		var aCrumb = aCookie[j].split("=");
		if (escape(sName) == aCrumb[0])
			curvalue = unescape(aCrumb[1]);
	}
	if (curvalue == "pcuidnocookie"){
		curvalue == "";
	}
	return curvalue;
}
//
function SetCookie(name,value){
  var Days = 30; //此 cookie 将被保存 30 天
  var exp   = new Date(); //new Date("December 31, 9998");
  exp.setTime(exp.getTime() + Days*24*60*60*1000);
  document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function loadAd(sorttype)
{
	sorttypegbl = sorttype;
//	alert("sorttype:"+ sorttype);
	var uid = getCookie("pcuid");
	var handler = new Davinci.RequestHandler(getAdSuc, getAdFail);
//	var req = new Davinci.Request(0,"http://192.168.1.43:8080/LJAdv/getadvs.action?sorttype="+sorttype+"&pcuid="+uid,handler,"POST");
	var req = new Davinci.Request(0,"http://adv.livejapan.cn/LJAdv/getadvs.action?sorttype="+sorttype+"&pcuid="+uid,handler,"POST");
	req.fire();
}
function getAdFail()
{
	//alert("errAfterAddProd. fail or time over");
	req_init(sorttypegbl);
}
function getAdSuc(responseXML,request)
{
//	alert("AdSuc");
	var ret = responseXML.selectNodes("results/adv");
	var pcuid = responseXML.selectNodes("results/pcuid");
	SetCookie("pcuid", pcuid[0].firstChild.nodeValue);
//	alert("ret.length: " + ret.length);
	var addiv = "";
	var adurl = "";
	var adpic = "";
	var adid  = "";
	var adwid = "";
	var adhig = "";
	var adtxt = "";
	var isflash = 0;
	var page_addiv;
	for(var i = 0; i < ret.length; i++){
		if (ret[i] == null){
			continue;
		}
//		alert(i+" : "+ret[i].childNodes.length);
		for(var j=0;j<ret[i].childNodes.length;j++){
			if (ret[i].childNodes[j].nodeName == 'id'){
				adid = ret[i].childNodes[j].firstChild.nodeValue;
			}
			if (ret[i].childNodes[j].nodeName == 'postion'){
				addiv = ret[i].childNodes[j].firstChild.nodeValue;
				
			}
			if (ret[i].childNodes[j].nodeName == 'url'){
				adurl = ret[i].childNodes[j].firstChild.nodeValue;
				
			}
			if (ret[i].childNodes[j].nodeName == 'picpath'){
				if(addiv.indexOf("_txt_") == -1){
					adpic = ret[i].childNodes[j].firstChild.nodeValue;
				}
			}
			if (ret[i].childNodes[j].nodeName == 'width'){
				adwid = ret[i].childNodes[j].firstChild.nodeValue;
			}
			if (ret[i].childNodes[j].nodeName == 'height'){
				adhig = ret[i].childNodes[j].firstChild.nodeValue;
			}
			if (ret[i].childNodes[j].nodeName == 'adtxt'){
				if(addiv.indexOf("_txt_") != -1){
					adtxt = ret[i].childNodes[j].firstChild.nodeValue;
				}
			}
		}
//		alert(adid+";"+addiv+";"+adurl+";"+adpic);
		if (adpic.indexOf('.swf') > 0)
			isflash = 1;
		else
			isflash = 0;
		page_addiv = document.getElementById(addiv);
		
		if(page_addiv!=null)
		{
//			alert("xx:"+page_addiv.firstChild.nodeName);
			if (isflash == 1){
				page_addiv.innerHTML=
					'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" '
					+' width="'+adwid+'" height="'+adhig+'">'
					+'<param name="movie" value=http://adv.livejapan.cn/LJAdv/Images/adv/adtoday/'+adpic+'" />'
					+'<param name="quality" value="high" />'
					+'<param name="FlashVars" value="url='+ adurl +'" />'
					+'<embed src="http://adv.livejapan.cn/LJAdv/Images/adv/adtoday/'+adpic+'" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" '
					+'FlashVars="url='+ adurl +'"'
					+'width="'+adwid+'" height="'+adhig+'"></embed>'
					+'</object>';
			}
			else if (page_addiv.firstChild.nodeName == 'A'){
				if(addiv.indexOf("_txt_") != -1){
					page_addiv.innerHTML="<a href=http://adv.livejapan.cn" + adurl + ">" + adtxt + "</a>";
				}else{
					page_addiv.firstChild.href = "http://adv.livejapan.cn" + adurl;
					page_addiv.firstChild.firstChild.src = 'http://adv.livejapan.cn/LJAdv/Images/adv/adtoday/' + adpic;
				}
			}else if(page_addiv.firstChild.nextSibling.nodeName == 'A'){
//				alert(page_addiv.firstChild);
				if(addiv.indexOf("_txt_") != -1){
					page_addiv.innerHTML="<a href=http://adv.livejapan.cn" + adurl + ">" + adtxt + "</a>";
				}else{
					page_addiv.firstChild.nextSibling.href = "http://adv.livejapan.cn" + adurl;
					page_addiv.firstChild.nextSibling.firstChild.src = 'http://adv.livejapan.cn/LJAdv/Images/adv/adtoday/' + adpic;
				}
			}	
		}
	}
	return true;
}

function alert_error()
{
    //alert("XDR onerror");
}
function alert_timeout()
{
    //alert("XDR ontimeout");
}
function alert_loaded()
{
    //alert("XDR onload");
    var res01 = xdr.responseText;
    //alert("Got: " + res01);
    
    var tmpdom = new ActiveXObject("Microsoft.XMLDOM");
    tmpdom.async = false;
    tmpdom.loadXML(res01);
    getAdSuc(tmpdom);
}
function alert_progress()
{
    //alert("XDR onprogress");
    //alert("Got: " + xdr.responseText);
}

function req_abort()
{
    if(xdr)
    {
        xdr.abort(); // Abort XDR if the Stop button is pressed. 
    }
}

function req_init(sorttype)
{
    var uid = getCookie("pcuid");
	var url = "http://192.168.1.66:8081/LJAdv/getadvs.action?sorttype="+sorttype+"&pcuid="+uid;
		
    var timeout = 1000;
    if (window.XDomainRequest) // Check whether the browser supports XDR. 
    {
        xdr = new XDomainRequest(); // Create a new XDR object.
        if (xdr)
        {
        	
            // There is an error and the request cannot be completed. 
            // For example, the network is not available.
            xdr.onerror     = alert_error;
            // This event is raised when the request reaches its timeout. 
            xdr.ontimeout   = alert_timeout;
            // When the object starts returning data, the onprogress event 
            // is raised and the data can be retrieved by using responseText.
            xdr.onprogress  = alert_progress;
            // When the object is complete, the onload event is raised and 
            // the responseText ensures the data is available. 
            xdr.onload      = alert_loaded;
            xdr.timeout     = timeout;
            // The URL is preset in the text area. This is passed in the 
            // open call with a get request.
            xdr.open("get", url);
                    
            // The request is then sent to the server.  
            xdr.send();
        }
        else
        {
            //alert('Failed to create new XDR object.');
        }
    }
    else
    {
        //alert('XDR does not exist.');
    }
}
