function getHttpRequest() {
	var xmlreq = false;
	if (window.XMLHttpRequest) {
		xmlreq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
		  xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
		  try {
		    xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
		  } catch (e2) {
		     xmlreq = false;
		  }
		}
	}
	return xmlreq;
}
function createAdCode(uid, jsOrFlash){
	var req = getHttpRequest();
	if(req){
		req.onreadystatechange = organiseData(req, jsOrFlash);
		var url = "/createAdCode.do?";
		var adForm = document.getElementById("adForm");
		
		var show = document.getElementById("show").value;         //显示数据的类型 0为发布的商品 1为推荐的商品
		url += "show="+show;
		var sel = document.getElementById("sel").value;
		url += "&sel="+sel;
		var totalNum = document.getElementById("totalNum").value; //显示数据的个数
		url += "&tn="+totalNum;
		var colNum = document.getElementById("colNum").value;  //显示的列数
		url += "&cn="+colNum;
		//var adWidth = document.getElementById("adWidth").value; //广告显示的宽度
		//url += "&aw="+adWidth;
		//var adHeight = document.getElementById("adHeight").value; //广告显示的高度
		//url += "&ah="+adHeight;
		url += "&uid="+uid;
		req.open("POST",url,true );
		req.send(null);
	}
}

function organiseData(request, jsOrFlash){
	return function(){
		if (request.readyState == 4) {
			if (request.status == 200) {
				var cn = 2;
				var adCode = "<iframe name='egouadcode' id='egouadcode' frameborder='0' marginwidth='0' marginheight='0' vspace='0' hspace='0' allowtransparency='true' scrolling='no' ";
				var previewAd = "<table border='0' align='center'>";
				var codeXml = request.responseXML.getElementsByTagName("code");
				for(var j=0; j<codeXml.length; j++){
					/*var aw = codeXml[j].childNodes[0].firstChild.nodeValue;
					adCode += "width=\""+aw+"\" ";
					var ah = codeXml[j].childNodes[1].firstChild.nodeValue;
					adCode += "height=\""+ah+"\" ";
					*/
					var show = codeXml[j].childNodes[0].firstChild.nodeValue;
					var sel = codeXml[j].childNodes[1].firstChild.nodeValue;
					var tn = codeXml[j].childNodes[2].firstChild.nodeValue;
					cn = codeXml[j].childNodes[3].firstChild.nodeValue;
					var uid = codeXml[j].childNodes[4].firstChild.nodeValue;
					var w = (cn==1)? 90 : 180;
					adCode += " width='"+w+"' ";
					var h = Math.ceil(tn/cn) * 112 + 29;
					adCode += " height='"+h+"' ";
					adCode += " src='http://www.egou.com/show.do?show="+show+"&uid="+uid+"&sel="+sel+"&tn="+tn+"&cn="+cn+"'></iframe>";
				}
				var articlesXml =request.responseXML.getElementsByTagName("article");
				var previewadObj = document.getElementById("previewad");
				if(articlesXml.length > 0){
					for(var i=0;i< articlesXml.length;i++){
						if( 0 ==(i%cn) ){
							previewAd += "<tr border='0'>";	
						}
						var artId = articlesXml[i].childNodes[0].firstChild.nodeValue;
						var uid = articlesXml[i].childNodes[1].firstChild.nodeValue;
						var picSrc = "";
						if(null!=articlesXml[i].childNodes[2].firstChild){
							picSrc = articlesXml[i].childNodes[2].firstChild.nodeValue;	
						}
						var artName = "";
						if(null!=articlesXml[i].childNodes[3].firstChild){
							artName = articlesXml[i].childNodes[3].firstChild.nodeValue;
						}
						previewAd += "<td border='0'><div class='sc_p'><a href='http://www.egou.com/product/"+uid+"/"+artId+".html' target='_blank'>";
						previewAd += "<img title=\""+artName+"\" alt=\""+artName+"\" src=\""+picSrc+"\" width=\"80\" height=\"107\" border='0'/>";
						previewAd += "</a></div></td>";
						if( (cn-1) == (i%cn) ){
							previewAd += "</tr>";
						}
					}
					if( (cn-1) != (i%cn) ){
						previewAd += "<td>&nbsp;</td></tr>";
					}					
				}
				previewAd += "<tr border='0'><td colspan='"+cn+"' align='center'><a href='http://www.egou.com' target='_blank'><img src='http://www.egou.com/images/front/i_8.jpg' border='0' /></td></tr></a>"
				previewAd += "</table>";
				if(jsOrFlash){
					document.getElementById("adcode").value=adCode;
				}
				previewadObj.innerHTML = previewAd;
			}
		}
	}
}
function flashAdCode(uid){
	var adForm = document.getElementById("adForm");
	var show = document.getElementById("show").value;         //显示数据的类型 0为发布的商品 1为推荐的商品
	var sel = document.getElementById("sel").value;           //显示最新添加(0)的还是随机显示(1)
	var totalNum = document.getElementById("totalNum").value; //显示数据的个数
	var colNum = document.getElementById("colNum").value;  //显示的列数
	//计算flash的高和宽 高=行数 * 80 + logo的高度 + 行间距 + 边距
	//宽=列数 * 110 + 列间距 + 边距
	var rows = Math.ceil(totalNum / colNum);
	var height = rows * 110 + 27 + 4 * rows + 5;
	var width = colNum * 80 + 3 * colNum + 3;
	
	
	//生成flash的广告代码
	var flashCode = "<div><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" ";
	flashCode += "width=\"" + width + "\" height=\"" + height + "\" id=\"passing\" >";
	flashCode += "<param name=\"movie\" value=\"http://www.egou.com/holyfly/"+ uid +"_" + totalNum + "_" + colNum + "_" + sel + "_" + show + "/egoushow.swf\" />";
	flashCode += "<param name=\"quality\" value=\"high\" /><param name=\"scale\" value=\"noscale\"/><param name=\"align\" value=\"tl\"/><param name=\"wmode\" value=\"transparent\"/>";
	flashCode += "<embed src=\"http://www.egou.com/holyfly/"+ uid +"_" + totalNum + "_" + colNum + "_" + sel + "_" + show + "/egoushow.swf\" ";
	flashCode += "wmode=\"transparent\" quality=\"high\" name=\"passing\" scale=\"noscale\" align=\"tl\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" ";
	flashCode += "width=\"" + width + "\" height=\"" + height + "\"/>";
	flashCode += "</object></div>"
	document.getElementById("adcode").value = flashCode;
}
