﻿// AjaxRequest(element, handler, name, value)
function AR(element, handler, name, value){
	var isXML=(name.toLowerCase().indexOf(".xml")>0);
	var httpFile = null;
	if (window.XMLHttpRequest)				// code for IE7, Firefox, Mozilla, etc.
		httpFile = new XMLHttpRequest();
	else if (window.ActiveXObject)			// code for IE5, IE6
		httpFile = new ActiveXObject("Microsoft.XMLHTTP");
	if (httpFile == null){
		alert("Your browser does not support XMLHTTP.");
		return;
	}
	httpFile.onreadystatechange = function(){
		if (httpFile.readyState==4)			// 4 = "loaded"
		{
			if (httpFile.status==200)		// 200 = "OK"
			{
				if (handler){
					if (element){
						if(typeof(element) == "string" && element.indexOf(',') < 0)
							element=document.getElementById(element);
						if(isXML)
							handler(element, httpFile.responseXML);
						else
							handler(element, httpFile.responseText);
					}
					else{
						if(isXML)
							handler(httpFile.responseXML);
						else
							handler(httpFile.responseText);
					}
				}
			}
			else{
				alert("Ajax请求失败: " + httpFile.statusText);
			}
		}				
	}
	var url=name;
	if(!isXML){
		url = "/AH.ashx?name=" + url;
		if(value)
			url += "&value=" + value;
	}	
	httpFile.open("GET", url, true);
	httpFile.send(null);
}
function ARInnerHtml(element, name, value){
	AR(element, SetInnerHTML, name, value);
	return false;
}
function AROuterHtml(element, name, value){
	AR(element, SetOuterHTML, name, value);
	return false;
}
function ARClassName(element, name, value){
	AR(element, SetClassName, name, value);
	return false;
}


function SetInnerHTML(element, htmlText){
	element.innerHTML = htmlText;
}
function SetOuterHTML(element, htmlText){
	element.outerHTML = htmlText;
}
function SetClassName(element, className){
	element.className = className;
}


function RemoveClass(element, className){
	if (element.className.indexOf(className)>=0){
		var reg = new RegExp(className+"\\s|(^"+className+"$)|\\s"+className,"g");
		element.className=element.className.replace(reg,"");
	}
}
function AddClass(element, className){
	var reg = new RegExp(className+"\\s|(^"+className+"$)|\\s"+className,"g");
	if (!reg.test(element.className)){
		if (element.className.length>0)
			element.className+=" "+className;
		else
			element.className=className;
	}
}
function WaitRedirect(url, times){
	setTimeout("window.location='"+url+"';", times);
}
function RunFun(fun, p1, p2, p3, p4){
	if(p4)
		fun(p1,p2,p3,p4);
	else if(p3)
		fun(p1,p2,p3);
	else if(p2)
		fun(p1,p2);
	else if(p1)
		fun(p1);
	else
		fun();
}
function RunReturnFun(fun, p1, p2, p3, p4){
	if(p4)
		return fun(p1,p2,p3,p4);
	if(p3)
		return fun(p1,p2,p3);
	if(p2)
		return fun(p1,p2);
	if(p1)
		return fun(p1);
	return fun();
}
function GetElement(element){
	if(typeof(element)=="string")
		return document.getElementById(element);
	return element;
}

function GetCities(provinceCtrl){
	AR(provinceCtrl, SetCities, "/Cities.xml");
}

function SetCities(provinceCtrl, city){
	var cityCtrl=document.getElementById(provinceCtrl.id+"2");
	cityCtrl.options.length=0;
	city=city.getElementsByTagName("City")[provinceCtrl.selectedIndex];
	
	var cities=city.childNodes[0].nodeValue.split(",");
	var opt;
	for(var c=0;c<cities.length;c++){
		opt=document.createElement('option');
		opt.text=cities[c];
		try{
			cityCtrl.options.add(opt,null);
		}
		catch(ex){
			 cityCtrl.options.add(opt); // IE only
		}
	}
}
function SetDayOption(day, days){
	var dayCount=day.options.length;
	if(day.selectedIndex+1>days)
		day.selectedIndex=days-1;
	if(dayCount==days)
		return;
	if(dayCount>days){
		day.options.length=days;
	}else{
		for(var c=dayCount;c<days; c++){
			var option=document.createElement("option");
			option.text=c+1;
			day.options[c]=option;			
		}
	}	
}
function SetDateOption(year, month){
	if(year){
		month=document.getElementById(year.id+2);
	}else{
		year=month.id.substr(0,month.id.length-1);
		year=document.getElementById(year);
	}
	var day=document.getElementById(year.id+3);
	year=parseInt(year.options[year.selectedIndex].text);
	month=month.selectedIndex+1;
	switch(month){
		case 4:case 6:case 9:case 11:
			SetDayOption(day,30);
			break;
		case 2:
			if(year % 4==0 && (year %100!=0 || year%400==0))
				SetDayOption(day,29);
			else
				SetDayOption(day, 28);
			break;
		default:
			SetDayOption(day, 31);
			break;
	}	
}
function FocusImages(imgsParent, pics, links, open){	
	if(!pics)
		return;
	// 容器元素
	var ip=GetElement(imgsParent);
	var imgs=ip.getElementsByTagName("img");
	

	this.Fore=imgs[0];
	this.Fore.Index=0;
	this.Fore.Open=open;
	this.Fore.Back=imgs[1];		
	// 图像元素
	pics=pics.split(",");
	if(links){
		this.Fore.style.cursor="pointer";
		links=links.split(",");
	}

	this.Images=new Array();
	var img;
	// 创建图像
	for(var c=0;c<pics.length; c++){
		img=document.createElement("img");
		img.style.display="none";
		img.src=pics[c];
		if(links)
			img.Link=links[c];
		this.Images[c]=img;
	}
	
	this.Fore.Images=this.Images;
	if(links)
		this.Fore.onclick=FocusImagesClick;
	this.Fore.src=this.Images[0].src;
	
	if(this.Images.length>1){
		SetOpacity(ip.getElementsByTagName("table")[0],70);
		this.Fore.TDS=ip.getElementsByTagName("td");
		var tds=this.Fore.TDS;
		for(var cc=0; cc<tds.length; cc++){
			var td=tds[cc];
			td.Fore=this.Fore;
			td.Index=cc;
			td.onclick=FocusImagesTDClick;
			td.style.fontFamily="Arial Unicode MS";
			td.style.fontSize="9px";
			td.style.fontWeight="600";
			td.style.height="12px";
			td.style.width="12px";
			td.style.backgroundColor="black";
			td.style.color="white";
			td.style.cursor="pointer";
			td.style.border="1px solid red";
		}
		tds[0].style.backgroundColor="#cc0000";
		this.Fore.style.padding="1px";
		this.Fore.style.border="1px solid gray";
		this.Fore.Back.src=this.Images[1].src;
		this.Fore.Back.style.padding="1px";
		this.Fore.Back.style.border="1px solid gray";
		this.Fore.k=100;
		//this.SetOpacity=(typeof(this.Last.style.opacity)=="string")?SetFFOpacity:SetIEOpacity;
		this.SetOpacity=(typeof(this.Fore.style.opacity)=="string")?
			function(){ this.Fore.style.opacity=this.Fore.k/100; }:
			function(){ this.Fore.style.filter="alpha(opacity="+this.Fore.k+")"; };
		
		SetTimeout(FocusImagesPlay,3000,this);
	}
	
}
function FocusImagesTDClick(){
	var fore=this.Fore;
	if(fore.k<100)
		return;
	if(fore.Index==this.Index)
		FocusImagesGoToPage(fore)
	else{
		var len=fore.Images.length;
		var index=(this.Index-1+len)%len;
		fore.TDS[fore.Index].style.backgroundColor="black";
		fore.TDS[this.Index].style.backgroundColor="#cc0000";
		fore.Index=(this.Index-1+len)%len;
		fore.Back.src=fore.Images[this.Index].src;		
		if(fore.k>100)
			fore.k=100;
	}
}
function FocusImagesClick(){
	if(this.k<100)
		return;
	FocusImagesGoToPage(this);
}
function FocusImagesGoToPage(fore){
	var index=(fore.Index-1+fore.Images.length)%fore.Images.length;
	var img=fore.Images[fore.Index];
	if(img.Link=="#")
		return;
	if(fore.Open)
		window.open(img.Link);
	else
		window.location=img.Link;
}
function FocusImagesPlay(imgs){
	var fore=imgs.Fore;
	if(fore.k>100){
		fore.k--;
	}

	else if(fore.k<5){
		fore.k=100;			
		var next=(fore.Index+1)%fore.Images.length;			
		fore.Back.src=imgs.Images[next].src;
		fore.src=imgs.Images[fore.Index].src;		
		imgs.SetOpacity();
		fore.k=175;
	}else{	
		if(fore.k==100){
			fore.TDS[fore.Index].style.backgroundColor="black";
			fore.Index=(fore.Index+1)%fore.Images.length;
			fore.TDS[fore.Index].style.backgroundColor="#cc0000";
		}			
		fore.k-=4;
		imgs.SetOpacity();		
	}	
	SetTimeout(FocusImagesPlay,40,imgs);
}
function SetTimeout(fun,timeout,param){     
    var callback= function(){ fun(param); }     
    setTimeout(callback,timeout);     
} 
function SetOpacity(element, opacity){
	element=GetElement(element);
	if(typeof(element.style.opacity)=="string")
		element.style.opacity=opacity/100;//FF
	else
		element.style.filter="alpha(opacity="+opacity+")";//IE
}
function ScrollNewsTitle(wrap, w, h, fontsize){
	wrap=GetElement(wrap);	
	wrap.style.width=w+"px";
	wrap.style.height=h+"px";
	wrap.style.overflow="hidden";
		var H=wrap.offsetHeight;
	var ps=wrap.getElementsByTagName("p");
	wrap.style.position="relative";
	wrap=wrap.getElementsByTagName("div")[0];
	wrap.Count=ps.length;
	wrap.Slide=true;
	wrap.style.position="absolute";
	wrap.style.height=ps.length*H+"px";
	if(fontsize)
		fontsize=12;
	var size=fontsize+"px";
	wrap.H=H;
	wrap.T=0;
	wrap.TopDis=(H-fontsize)/2;
	var topDis=wrap.TopDis+"px";
	var downDis=H-fontsize-wrap.TopDis+"px";
	for(var c=0;c<ps.length;c++){
		ps[c].style.width="100%";
		ps[c].style.overflow="hidden";
		ps[c].style.fontSize=size;		
		ps[c].style.lineHeight=size;
		ps[c].style.height=size;
		ps[c].style.paddingTop=topDis;
		ps[c].style.paddingBottom=downDis;
		ps[c].style.margin="0px";
		ps[c].onmouseover=ScrollNewsPMouseOver;
		ps[c].onmouseout=ScrollNewsPMouseOut;		
	}
	if(ps.length>1)
	SetTimeout(ScrollNewsPlay, 3000, wrap);
}
function ScrollNewsPlay(wrap){
	if (wrap.Slide)
	{		
		wrap.T-=1;
		if (wrap.T<=wrap.H-wrap.offsetHeight)
			wrap.T=0;			
		wrap.style.top=wrap.T+"px";
	}
	if (wrap.T%wrap.H ==0){
		SetTimeout(ScrollNewsPlay,3000,wrap);		
	}
	else
		SetTimeout(ScrollNewsPlay,40,wrap);
}
function ScrollNewsPMouseOver(){
	this.parentNode.Slide=false;
}
function ScrollNewsPMouseOut(){
	this.parentNode.Slide=true;
}

function OpenCenter(url,title, w, h){	
	var x=(window.screen.width-w)/2;
	var y=(window.screen.height-h)/2;
	var s="left="+x+",top="+y+",width="+w+",height="+h;	
	s+=",menubar=no,titlebar=no,toolbar=no,status=no,scrollbars=no,location=no";
	window.open(url, title,s);
}
