function trim(str) 
{
   return str.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.replaceAll = function(src,desc)
{
	var tmp = this;
	var re=new RegExp(src,"g");
	tmp = tmp.replace(re,desc);
	return tmp;
}

String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.ltrim = function()
{
	return this.replace(/(^\s*)/g, "");
}

String.prototype.rtrim = function()
{
	return this.replace(/(\s*$)/g, "");
}

function isNumber(anum1,len) 
{
	if (anum1 == "")
	{
		return false;
	}
    var numstr = "0123456789";
	if (len != null && anum1.length != len)
	{
		return false;
	}
    for(var i=0; i < anum1.length; i++)
    {
        if(numstr.indexOf(anum1.charAt(i)) == -1)
        {
            return false;
        }
    }   
    return true;
}

function isEmail(email)
{
    var bad_email_chars = "`\"\\ /(){}[]|<>/,&+=*'%?!~#$^:;";

    if (email == "" || email==null)
    {
       return false;
    }
	if (email.length < 5) 
	{
	    return false;
    }

	if (check_string(email, bad_email_chars) > -1) 
	{
	    return false;
    }
	//Check for a String "####@.com".
	var at_sign =email.indexOf("@.");
	if (at_sign>=0) 
	{
	    return false;
    }
	//Check for a String "####.@com".
	var at_sign =email.indexOf(".@");
	if (at_sign>=0) 
	{
        return false;
	}
	//Check for that there are more two "@" in email string.
	var at_sign  =email.indexOf("@");
	var at_sign2 =email.indexOf("@", at_sign+1);
	if (at_sign2>=0) 
	{
	    return false;
    }
	// Check for an @ sign
	var at_sign =email.indexOf("@");
	if (at_sign<=0) 
	{
	    return false;
    }
	// Check for a domain
	var dot = email.indexOf(".");
	if (dot < 1) 
	{
	    return false;
    }

    return true;
}

function isIdNumber(idNumber) 
{
    if (idNumber == "" || idNumber==null) {
        return false;
    }
    if (typeof(idNumber)!='string') 
    	return false;
    	
    if (idNumber.length == 15) 
    {
    	
        return isNumber(idNumber);
    } 
    else if (idNumber.length == 18) 
    {
        var lastchar = idNumber.substring(17,18);
        return ( isNumber(idNumber.substring(0,17)) && (isNumber(lastchar) || lastchar=="X" || lastchar=="x") );
    } 
    else 
    {
        return false;
    }
}


function isMobile(mobile)
{
	if (mobile.length==11 && mobile.substr(0,2)=="13" && isNumber(mobile))
	{
		
		return true;
	}
	else
	{
		return false;
	}

}


function returnfalse(p_input) 
{	
	try
	{
		p_input.focus();
		p_input.select();
	}
	catch (e)
	{	
	}
	event.returnValue=false;
}



Date.prototype.setDateStr = function(str)
{
	try
	{
		str += "";
		date=str.split(" ");
		date1=date[0].split("-");	
		date2=date[1].split(":");
		this.setFullYear(date1[0]);
		this.setMonth(date1[1]-1);
		this.setDate(date1[2]);
		this.setHours(date2[0]);
		this.setMinutes(date2[1]);
		this.setSeconds(date2[2]);
		return true;
	}
	catch (ex)
	{
		//alert (ex);
		return false;
	}
}

Date.prototype.getDateStr = function()
{
	var str ="";
	//*
	str += this.getFullYear()+"-";
	if (this.getMonth() < 9)
	{
		str += "0";
	}
	str += (this.getMonth()+1)+"-";
	
	if (this.getDate() < 10)
	{
		str +="0";
	}
	str += this.getDate();	
	//*/

	return str;
}

function setCookie(cookieName,cookievalue,path,domainstr,expires)
{
	document.cookie=cookieName+"="+escape(cookievalue);
	if (path!="" && path!=null)
	{
		document.cookie +=";path="+path;
	}
	if (domainstr!="" && domainstr!=null)
	{
		document.cookie +=";domain="+domainstr;
	}
	if (expires!="" && expires!=null)
	{
		document.cookie +=";expires="+expires;
	}
}

function getCookie(cookieName)
{
	var cookieString = document.cookie;
	var start = cookieString.indexOf(cookieName + '=');
	if (start == -1)
	{
		return null;
	}
	else
	{
		start += cookieName.length + 1;
		var end = cookieString.indexOf(';', start);
		if (end == -1) 
		{
			return unescape(cookieString.substring(start));
		}
		else
		{
			return unescape(cookieString.substring(start, end));
		}
	}
}

// asynchronous = false是同步
function loadXML(url, handler,args,starhardly,endhardly,asynchronous)
{
	url = encodeURI(url);

	if (asynchronous == null)
	{
		asynchronous = true;
	}
	if (window.ActiveXObject && !window.XMLHttpRequest)
	{
		window.XMLHttpRequest = function() 
		{
			return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') != -1) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP');
		};
	}
	
	if (!window.ActiveXObject && window.XMLHttpRequest)
	{
		window.ActiveXObject = function(type) 
		{
			switch (type.toLowerCase())
			{
				case 'microsoft.xmlhttp':
				case 'msxml2.xmlhttp':
				return new XMLHttpRequest();
			}
			return null;
		};
	}
		
	var req = new XMLHttpRequest();
	if (req)
	{
		if (starhardly != null)
		{
			starhardly(this);
		}
		req.onreadystatechange = function()
		{
			if (asynchronous && req.readyState == 4 && req.status == 200)
			{
				//alert(req.readyState)
				handler(req.responseXML,args);
				if (endhardly != null)
				{
					endhardly(this);
				}
			}
		};
		req.open('GET', url,asynchronous);
		req.send(null);
		if (asynchronous==false)
		{
			handler(req.responseXML,args);
			if (endhardly != null)
			{
				endhardly(this);
			}
		}
	}
}

function initRowList(xmldoc,arg)
{
	arg[0] = xmldoc.getElementsByTagName('r');
}


function getRowList(url)
{
	var r = new Array();
	loadXML(url, initRowList,r,null,null,false);
	return r[0];
}

function checkobj(p_input,errstr) 
{
	var isok = true;
	if (p_input.type=="text" || p_input.type=="password" || p_input.type=='textarea' || p_input.type=='file' || p_input.type == 'hidden')
	{
		if (p_input.value.trim() == "")
		{
			p_input.focus();
			isok = false;
		}
	}
	else if (p_input.type.indexOf("select") == 0)
	{
		isok = false;
		try
		{
				for (var i=0;i<p_input.length;i++)
				{
					if (p_input[i].selected == true)
					{
						isok = true;
						break;
					}
				}
				p_input.focus();
		}
		catch (e)
		{
		}
	}
	else
	{
		alert (p_input.type)
		return false;
	}
	
	if (isok)
	{
		return true;
	}
	else
	{
		alert (errstr);
		return false;
	}
}

function changecheckall(obj,id)
{
	var f = eval(id);
	if (f.length == null)
	{
		f.checked = obj.checked;
	}
	else
	{	
		for (var i=0,len=f.length; i<len ;i++)
		{
			f[i].checked = obj.checked;
		}
	}
}

function getAllcheckString(id) 
{
	return "<input type='checkbox' onclick=\"changecheckall(this,'"+id+"')\" />";
}

function checkids(str)
{	
	return "<input name='ids[]' id='ids' type='checkbox'  value='"+str+"'/>";
}

function getaction(str)
{	
	var buf = "";
	buf += "<a href='edit.php?id="+str+"'>[编辑]</a> ";
	return buf;
}


function listdel(f)
{
	var ids = "";
	var c = false;
	
	if (f.ids != null && f.ids.checked == true)
	{
		c = true;
	}
	
	for (var i=0,len=f.ids.length; i<len; i++)
	{
		if (f.ids[i].checked == true)
		{
			c = true;
			break;
		}
	}
	if (c==false)
	{
		alert ("Please selete delete record.")
		return;
	}
	
	if (confirm ("Are you sure to delete?"))
	{
		f.submit();
	}
}
function initform(xmldoc,tablepkformendhandly)
{
	var t = tablepkformendhandly.split("|");
	var table = t[0];
	var pk = t[1];
	var form = t[2];
	var handly = t[3];
	if (form != null)
	{
		form = document.getElementById(form);
	}
	
	if (table == null || pk== null || form == null)
	{
		return false;
	}
	
	document.getElementById("cmt_isupdate").value="true";
	//alert (form.elements.length);
	var list = xmldoc.getElementsByTagName('r');
	r = list[0];
	for (var i=0,len=form.elements.length; i<len ;i++)
	{
		var obj = form.elements[i];
		//alert (obj.name + " "+obj.type);
		var t = obj.name.split("_",3);
		if (t.length == 3 && t[1]==table)
		{
			if (t[0] == "cmt")
			{
				if (obj.type.indexOf("select")==0)
				{
					setSelected (obj.name,r.getAttribute(t[2]))
				}
				else if (obj.type == "radio" )
				{
					setChecked (obj.name,r.getAttribute(t[2]))
				}
				else
				{
					obj.value = r.getAttribute(t[2]);
					try
					{
						tinyMCE.get(obj.id).show();
					}
					catch(e)
					{
					}
				}
			}
			else if (t[0] == "cmtw" && pk == t[2])
			{
				obj.value = r.getAttribute(t[2]);
			}
			else if (t[0] == "cmts")
			{
				obj.value = r.getAttribute(t[2]);
			}
		}
	}
	if (handly != null)
	{
		eval(handly);
	}
}
function loadform(url,tablename,pk,form,handly)
{
	if (handly  == null)
	{
		loadXML(url, initform,tablename+"|"+pk+"|"+form,startload,endload);
	}
	else
	{
		loadXML(url, initform,tablename+"|"+pk+"|"+form+"|"+handly,startload,endload);
	}
}

function upload (reid,handly)
{
	window.open("/common/upload.php?reid="+reid+"&handly="+handly,"_blank","width=400,height=100,scrollbars=no");
}

function reportError(msg,url,line) {
	var str = "You have found an error as below: \n\n";
	str += "Err: " + msg + " on line: " + line;
	alert(str);
	return true;
}

function startload()
{
	var str = "<img src=\"/common/loading.gif\" alt=\"loading\"  align=\"absmiddle\" style=\"padding:0px;margin:0px;float:none;width:16px;height:16px;display:inline;\" />Loading...";
	var msgw,msgh,bordercolor;
	msgw=400;//提示窗口的宽度
	msgh=100;//提示窗口的高度
	titleheight=25 //提示窗口标题高度
	bordercolor="#336699";//提示窗口的边框颜色
	titlecolor="#99CCFF";//提示窗口的标题颜色
	
	var sWidth,sHeight;
	sWidth=document.documentElement.offsetWidth;
	sHeight=document.documentElement.offsetHeight;
	if (sHeight<screen.height)
	{
		sHeight=screen.height;
	}

	var bgObj=document.createElement("div");
	bgObj.setAttribute('id','bgDiv');
	bgObj.style.position="absolute";
	bgObj.style.top="0";
	bgObj.style.background="#777";
	bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
	bgObj.style.opacity="0.6";
	bgObj.style.left="0";
	bgObj.style.width=sWidth + "px";
	bgObj.style.height=sHeight + "px";
	bgObj.style.zIndex = "10000";
	document.body.appendChild(bgObj);
			
	var msgObj=document.createElement("div")
	msgObj.setAttribute("id","msgDiv");
	msgObj.setAttribute("align","center");
	msgObj.style.background="white";
	msgObj.style.border="1px solid " + bordercolor;
	msgObj.style.position = "absolute";
    msgObj.style.left = "50%";
    msgObj.style.top = "50%";
    msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
    msgObj.style.marginLeft = "-225px" ;
    msgObj.style.marginTop = -75+document.body.scrollTop+"px";
    msgObj.style.width = msgw + "px";
    msgObj.style.height =msgh + "px";
    msgObj.style.textAlign = "center";
    msgObj.style.lineHeight = (msgh-titleheight) + "px";
    msgObj.style.zIndex = "10001";
   
	var title=document.createElement("h4");
	title.setAttribute("id","msgTitle");
	title.setAttribute("align","right");
	title.style.margin="0";
	title.style.padding="3px";
	title.style.background=bordercolor;
	title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
	title.style.opacity="0.75";
	title.style.border="1px solid " + bordercolor;
	title.style.height="18px";
	title.style.font="12px Verdana, Geneva, Arial, Helvetica, sans-serif";
	title.style.color="white";
	title.style.cursor="pointer";

	document.body.appendChild(msgObj);
	document.getElementById("msgDiv").appendChild(title);
	var txt=document.createElement("p");
	txt.style.margin="1em 0"
	txt.setAttribute("id","msgTxt");
	txt.innerHTML=str;
    document.getElementById("msgDiv").appendChild(txt);
}
function endload()
{
	try
	{
		document.body.removeChild(document.getElementById("bgDiv"));
		document.getElementById("msgDiv").removeChild(document.getElementById("msgTitle"));
		document.body.removeChild(document.getElementById("msgDiv"));
	}
	catch(e)
	{
		alert(e)
	}
}
/*
function alert(str)
{
	myalert(str);
}
*/
function myalert(str)
{
	var msgw,msgh,bordercolor;
	msgw=400;//提示窗口的宽度
	msgh=100;//提示窗口的高度
	titleheight=25 //提示窗口标题高度
	bordercolor="#336699";//提示窗口的边框颜色
	titlecolor="#99CCFF";//提示窗口的标题颜色
	
	var sWidth,sHeight;
	sWidth=document.documentElement.offsetWidth;
	sHeight=document.documentElement.offsetHeight;
	if (sHeight<screen.height)
	{
		sHeight=screen.height;
	}

	var bgObj=document.createElement("div");
	bgObj.setAttribute('id','bgDiv');
	bgObj.style.position="absolute";
	bgObj.style.top="0";
	bgObj.style.background="#777";
	bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
	bgObj.style.opacity="0.6";
	bgObj.style.left="0";
	bgObj.style.width=sWidth + "px";
	bgObj.style.height=sHeight + "px";
	bgObj.style.zIndex = "10000";
	document.body.appendChild(bgObj);
			
	var msgObj=document.createElement("div")
	msgObj.setAttribute("id","msgDiv");
	msgObj.setAttribute("align","center");
	msgObj.style.background="white";
	msgObj.style.border="1px solid " + bordercolor;
	msgObj.style.position = "absolute";
    msgObj.style.left = "50%";
    msgObj.style.top = "50%";
    msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
    msgObj.style.marginLeft = "-225px" ;
    msgObj.style.marginTop = -75+document.documentElement.scrollTop+"px";
    msgObj.style.width = msgw + "px";
    msgObj.style.height =msgh + "px";
    msgObj.style.textAlign = "center";
    msgObj.style.lineHeight = (msgh-titleheight) + "px";
    msgObj.style.zIndex = "10001";
   
	var title=document.createElement("h4");
	title.setAttribute("id","msgTitle");
	title.setAttribute("align","right");
	title.style.margin="0";
	title.style.padding="3px";
	title.style.background=bordercolor;
	title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
	title.style.opacity="0.75";
	title.style.border="1px solid " + bordercolor;
	title.style.height="18px";
	title.style.font="12px Verdana, Geneva, Arial, Helvetica, sans-serif";
	title.style.color="white";
	title.style.cursor="pointer";
	title.innerHTML="Close";
	title.onclick=function()
	{
		document.documentElement.removeChild(bgObj);
	    document.getElementById("msgDiv").removeChild(title);
        document.documentElement.removeChild(msgObj);
    }
	document.documentElement.appendChild(msgObj);
	document.getElementById("msgDiv").appendChild(title);
	var txt=document.createElement("p");
	txt.style.margin="1em 0"
	txt.setAttribute("id","msgTxt");
	txt.innerHTML=str;
    document.getElementById("msgDiv").appendChild(txt);
}
function setSelected (selectid,selectvalue)
{
	var obj = document.getElementById(selectid);
	if (obj == null)
	{
		alert ("can't find "+selectid);
		return false;
	}
	for (var i=0;i < obj.length ; i++)
	{
		if (obj[i].value == selectvalue)
		{
			obj[i].selected = true;
		}
	}
}

function setChecked (selectid,selectvalue)
{
	var obj = document.getElementsByName(selectid);
	if (obj == null)
	{
		alert ("can't find "+selectid);
		return false;
	}
	for (var i=0;i < obj.length ; i++)
	{
		if (obj[i].value == selectvalue)
		{
			obj[i].checked = true;
		}
	}
}
function initxmlselect(xmldoc,phandly)
{
	var obj = document.getElementById(phandly.selectid);
	var list = xmldoc.getElementsByTagName('r');
	//alert (list.length);
	if (phandly.isclear == true)
	{
		var c = 0;
		for (var i=0,len=obj.length; i < len; i++)
		{
			if (obj[c].value == "")
			{
				c++;
			}
			else
			{
				obj.removeChild(obj.options[c]);

				//obj.options.remove(c);
			}
		}
	}
	for (var i=0; i < list.length; i++)
	{
		if (navigator.appName.indexOf("Internet Explorer") > 0 )
		{
			obj.add(new Option(list[i].getAttribute(phandly.textfield),list[i].getAttribute(phandly.valuefield))); 
		}
		else
		{
			var opnObj = document.createElement("option"); 
			opnObj.value = list[i].getAttribute(phandly.valuefield); 
			opnObj.text = list[i].getAttribute(phandly.textfield); 
			obj.appendChild(opnObj);
		}
	}
	if (phandly.selected != null)
	{
		setSelected (phandly.selectid,phandly.selected)
	}
	if (phandly.handly != null)
	{
		eval (phandly.handly);
	}
}
function EXmlSelet(selectid,textfield,valuefield,selected,isclear,handly)
{
	this.selectid = selectid;
	this.textfield = textfield;
	this.valuefield = valuefield;
	this.selected = selected;
	this.isclear = isclear;
	this.handly = handly;	
}
function loadxmlselect (selectid,url,textfield,valuefield,selected,isclear,handly)
{
	var t = new EXmlSelet(selectid,textfield,valuefield,selected,isclear,handly);
	var obj = document.getElementById(selectid);
	
	if (obj == null)
	{
		alert ("can't find "+selectid);
		return false;
	}
	loadXML(url, initxmlselect,t);
}

function handleSelect(type,args,obj)
{
	var dates = args[0];
	var date = dates[0];
	var year = date[0], month = date[1], day = date[2];

	var txtDate1 = document.getElementById(obj.objname);
	txtDate1.value = year+"-"+month + "-" + day;
	obj.hide();
}

function initCal(caldivname,objname,datestr)
{
	var cal = new YAHOO.widget.Calendar("cal",caldivname, { title:"Choose a date:", close:true } );
	cal.render();
	
	// Listener to show the 1-up Calendar when the button is clicked
	//YAHOO.util.Event.addListener("show1up", "click", cal.show, cal, true);
	if (objname != null)
	{
		cal.objname = objname;
		cal.selectEvent.subscribe(handleSelect, cal, true);
	}
	
	if (datestr != null)
	{
		 updateCal(cal,datestr);
	}
	return cal;
}

function handleSelect1(type,args,obj)
{
	var dates = args[0];
	var date = dates[0];
	var year = date[0], month = date[1], day = date[2];

	var txtDate1 = document.getElementById(obj.objname);
	txtDate1.value = month +"/"+ day+ "/" + year;
	obj.hide();
}

function initCal1(caldivname,objname,datestr)
{
	var cal = new YAHOO.widget.Calendar("cal",caldivname, { title:"Choose a date:", close:true } );
	cal.render();
	
	// Listener to show the 1-up Calendar when the button is clicked
	//YAHOO.util.Event.addListener("show1up", "click", cal.show, cal, true);
	if (objname != null)
	{
		cal.objname = objname;
		cal.selectEvent.subscribe(handleSelect1, cal, true);
	}
	
	if (datestr != null)
	{
		 updateCal(cal,datestr);
	}
	return cal;
}

function updateCal(cal,datestr)
{
	cal.select(datestr);

	var firstDate = cal.getSelectedDates()[0];

	// Set the Calendar's page to the earliest selected date
	cal.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear());

	cal.render();
}

function del(url)
{
	if (confirm ("是否确认删除?"))
	{
		location.href = url;
	}
}
//window.onerror = reportError;

function getmediastr (url,width,height)
{
	var tmp = url.split(".");
	var ext = tmp[tmp.length-1];
	buf = ""
	if (ext == "rm" || ext == "rmvb" || ext == "ra")
	{
		buf += "<object classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA id=msplay width="+width+" height="+height+">";
		buf += "<param name=AutoRewind value=1>";
		buf += "<param name=SRC value="+url+">";
		buf += "<param name=Filename value="+url+">";		
		buf += "<param name=AutoStart value=1></object>";	
	}
	else if (ext == "swf")
	{
		buf += "<object classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0 id=msplay width="+width+" height="+height+">";
		buf += "<param name=movie value="+url+">";
		buf += "</object>";	
	}
	else if (ext == "mov")
	{
		buf += "<object type=video/quicktim width="+width+" height="+height+">";
		buf += "data="+url+">";
		buf += "</object>";	
	}
	else
	{
		buf += "<object classid=clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6 id=msplay width="+width+" height="+height+">";
		buf += "<param name=AutoRewind value=1>";
		buf += "<param name=URL value="+url+">";
		buf += "<param name=Filename value="+url+">";		
		buf += "<param name=AutoStart value=1></object>";	
	}
	return buf;
}

function showmeida (url,width,height)
{
	getmediastr (url,width,height)
	document.write (buf);
}

function reSize(i,w,h)
{
	var ow = i.width;
    var oh = i.height;
    var rw = w/ow;
    var rh = h/oh;
	//alert(ow+ "  "+oh)
    var r = Math.min(rw,rh);
    if (w ==0 && h == 0)
	{
        r = 1;
    }
	else if (w == 0)
	{
        r = rh<1?rh:1;
    }else if (h == 0)
	{
        r = rw<1?rw:1;
    }
    if (ow!=0 && oh!=0)
	{
		i.width = ow * r;
		i.height = oh * r;
    }
	else
	{
		//window.setTimeout("reSize("+i+","+w+","+h+")" ,200);
		//*
		//var __method = this;, args = $A(arguments);
        window.setTimeout(function() 
		{
          //reSize.apply(this, arguments);
		  reSize(i,w,h)
        }, 200);
		//*/
    }
    i.onload = function(){}
}