/*
'FileName:main.js
'CreateDate:2006-4-20
'LastModifyDate:2008-2-20
'Purpose:common javascript library(in ff1.5+,ie6.0+)
'Author:Jaying(jayingju#163.com)
'Version:1.1
*/

//设置域
//document.domain="soosou.com";
//写当前日期
function writeCurDate(){
	var sDate=new Date();
	var x=new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
	var thisYear=sDate.getFullYear();
	var thisMonth=sDate.getMonth()+1;
	var thisDate=sDate.getDate();
	var thisDay=sDate.getDay();
	document.write(thisYear+"年"+thisMonth+"月"+thisDate+"日"+" "+x[thisDay]);

}
//获得对对象ID的引用
function $(objid){return document.getElementById(objid);}
function $F(objid) {return document.getElementById(objid).value};
function $$(node){return document.getElementsByTagName(node)};

//判断是否是正整数
isInte = function(s){
	var para = /^[0-9]*[1-9][0-9]*$/;
	if (!para.exec(s)) {return false;}
	return true;
}

//去除对象虚线框
function fHideFocus(tName){
	aTag=document.getElementsByTagName(tName);
	for(i=0;i<aTag.length;i++)aTag[i].onfocus=function(){this.blur();};
}

function bbimg(o){
	var zoom=parseInt(o.style.zoom, 10)||100;zoom+=event.wheelDelta/12;if (zoom>0) o.style.zoom=zoom+'%';
	return false;
}

function openScript(url, width, height){
	var Win = window.open(url,"openScript",'width=' + width + ',height=' + height + ',resizable=1,scrollbars=yes,menubar=no,status=yes' );
}

function getpara(){
	var str,pos,parastr
	str = window.location.href;
	pos = str.indexOf("?");
	parastr = str.substring(pos+1);
	return parastr;
}

//全选，取消
function checkAll(e, itemName){
  var aitem = document.getElementsByName(itemName);
  for (var i=0; i<aitem.length; i++){
   aitem[i].checked = e.checked;
   changebg(aitem[i],aitem[i].parentNode.parentNode);
  }
}
//执行操作确认
function doPostBack(formObj,actionFile){
	if(!isSelected()){
	    alert("请选择项目记录！");
		return false;
	}
	if(confirm("确定要执行的操作吗？")){
		formObj.action=actionFile;
		formObj.submit();
		return true;
	}
	return false;
}
function isSelected(){
	var objID = document.getElementsByName("selectID");		
	var type = typeof(objID.name); 
	if ( type != 'undefined' ){
		if( objID.checked ){
			return true;
		}
	}else{
		for(var i=0; i< objID.length; i++){
			if(objID[i].checked ){
				return true;
			}
		}
	}
	return false;
}
function hasSelected(aobj){
	var objID = document.getElementsByName(aobj);		
	var type = typeof(objID.name); 
	if ( type != 'undefined' ){
		if( objID.checked ){
			return true;
		}
	}else{
		for(var i=0; i< objID.length; i++){
			if(objID[i].checked ){
				return true;
			}
		}
	}
	return false;
}
function getobjvalue(aobj){
	var objID = document.getElementsByName(aobj);		
	var type = typeof(objID.name); 
	if ( type != 'undefined' ){
		if( objID.checked ){
			return objID.value;
		}
	}else{
		for(var i=0; i< objID.length; i++){
			if(objID[i].checked ){
				return objID[i].value;
			}
		}
	}
	return false;
}
//选择slt_City
function chkSel(formOjb,formField){
	with (formOjb){
		if(formField.value) {
			slt_City.options.length=0;
			for(var i=0;i<selects[formField.value].length;i++){
				slt_City.add(selects[formField.value][i]);
			}
		}
	}
}
//
function chgSize(num,objname)
{
	var obj=document.getElementById(objname)
	if (parseInt(obj.rows)+num>=6) {
		obj.rows = parseInt(obj.rows) + num;	
	}
	if (num>0)
	{
		obj.width="90%";
	}
}
function strLen(key){
	var l=escape(key),len;
	len=l.length-(l.length-l.replace(/\%u/g,"u").length)*4;
	l=l.replace(/\%u/g,"uu");
	len=len-(l.length-l.replace(/\%/g,"").length)*2;
	return len;
}

//显示/隐藏
showdiv = function(obj){document.getElementById(obj).style.display = "block";}
hidediv = function(obj){document.getElementById(obj).style.display = "none";}
//字符全部替换
String.prototype.ReplaceAll = stringReplaceAll;
function  stringReplaceAll(AFindText,ARepText){
  raRegExp = new RegExp(AFindText,"g");
  return this.replace(raRegExp,ARepText);
}
//转义字符
String.prototype.ESC = function(){return this.ReplaceAll("<","&lt").ReplaceAll(">","&gt");}
//去掉左右空格
String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"")};


change_plant = function(obj,hpv){
	hpv = unescape(hpv);
	hidediv("showAllPlant");
	if(sjid != jid){
		//alert(mMessage);
		return;
	}
	else{
		if(hpv == hasNoPlant){
			hpv = "";
		}
	var html = "<textarea id='editPlant' name='editPlant' class='editPlant' onkeydown=resetP(event,this) onblur=goDef(this); onfocus='this.select()'>"+hpv+"</textarea>";
	obj.parentNode.innerHTML = html;
	document.getElementById("editPlant").focus();
	}
}
showAllPlant = function(obj,event){
	obj.className = "nkov";
	showdiv("showAllPlant");	
}
hideAllPlant = function(obj){
	obj.className = "";
	hidediv("showAllPlant");
}

resetP = function(event,obj){
	if(event.keyCode == 13){
		def_Plant('',obj.value);
	}
}
def_Plant = function(action,hv){
	hv = Trim(hv);
	var html = "";
	if(hv == ""){
		hv = hasNoPlant;
	}
	html += "<span id='defPlant' onmouseover='showAllPlant(this,event)' onmouseout='hideAllPlant(this)' onclick=change_plant(this,'"+escape(hv)+"')>";
	if(action != "def"){
		html += document.getElementById("editPlant").value.substring(0,30);
		if(document.getElementById("editPlant").value.length >30){
			html += "...";
		}
		if(Trim(document.getElementById("editPlant").value) == ""){
			html += hasNoPlant;
		}
		var url = burl+"?action=updatePlant&jid="+sjid+"&plant="+encodeURIComponent(document.getElementById("editPlant").value);
		updatePlant(url,escape(document.getElementById("editPlant").value));
	}
	else{
		html += hv;
	}
	html += "</span>";
	document.getElementById("plant").innerHTML = html;
}
 goDef = function(obj){
 	def_Plant('',obj.value);
}
/* --------------------
 *	更新签名
 * --------------------
 */
 updatePlant = function(url,plant){
	createXMLHttp();
	http_request.onreadystatechange = function(){updatePlantResult(plant);};
    authBrows(http_request,url, true);
    http_request.send(null);
 }
 updatePlantResult = function(plant){
 	if (http_request.readyState == 4) {
 		if (http_request.status == 200) {
 			if (plant != "") {
 				document.getElementById("showAllPlant").innerHTML = unescape(plant);
 			}
 			else {
 				document.getElementById("plant").firstChild.innerHTML = hasNoPlant;
            	document.getElementById("showAllPlant").innerHTML = hasNoPlant;
 			}
 		}
 	}
 }
//表格行选中的背景
function changebg(_self,_obj){
	if(_self.checked){_obj.className="selbg";}
	else{_obj.className="";listfrm.chk_all.checked=false;}
}
function strLen(key){
	var l=escape(key),len;
	len=l.length-(l.length-l.replace(/\%u/g,"u").length)*4;
	l=l.replace(/\%u/g,"uu");
	len=len-(l.length-l.replace(/\%/g,"").length)*2;
	return len;
}
//对Cookies进行操作的Cookie对象
var Cookie={
get:function(n){
  var c=document.cookie,v="";
  n=n+"=";
  if(c.length>0){ 
    var a=c.indexOf(n);
    if(a!=-1){ 
      a+=n.length;
      var b=document.cookie.indexOf(";",a);
      if (b==-1) b=c.length;
      v=unescape(c.substring(a,b))
    }
  }
  return v;
},
set:function(n,v,h){
  var e = "";
  if(h!=null){
    e=new Date((new Date()).getTime() + h * 3600000);
    e="; expires=" + e.toGMTString();
  }
  document.cookie=n+"="+escape(v)+";path=/"+e;
},
cn:function(n){
	var s="",l=Cookie.get(n).split("|");
	for(var i=0;i<l.length;i++){
		s+=String.fromCharCode(parseInt(l[i],16));
	}
	return s;
}};

function dw(str){
	return document.write(str);
}
/*列表框options添加/修改*/
function swithOptions(oldobj,newObj){
	var isrepeat;
	oldobj.ondblclick = function(){
		isrepeat=false;
	//if(oldobj.options[oldobj.selectedIndex].isSelected){
	//    isrepeat=true;
	//}
	for(var i=0;i<newObj.options.length;i++){
		if(newObj.options[i].value==this.value){isrepeat=true;break;}
	}
	if (!isrepeat){
			//oldobj.options[oldobj.selectedIndex].isSelected=true;
			var newOption = new Option(oldobj.options[this.selectedIndex].text,this.value);
			//newOption.setAttribute("selindex",this.selectedIndex);
			newObj.options[newObj.options.length] = newOption;
			//newObj.add(newOption,newObj.selectedIndex);
			//oldobj.options[oldobj.selectedIndex] = null;
		}
	}
}
function removeOptions(thisObj){
	thisObj.ondblclick = function(){
		thisObj.options[this.selectedIndex]=null;
	}
}
function removeAllOptions(thisObj){
	thisObj.options.length=0;
}
function switchAllOptions(oldObj,newObj){
	newObj.options.length=0;
	for (var i=0;i<oldObj.options.length;i++){
		newObj.options[newObj.options.length]=new Option(oldObj.options[i].text,oldObj.options[i].value);
	}
	//oldObj.options.length=0;
}
//图像缩放
function resizeImg(thisImg,FitW,FitH){
	var imgObj,width,height,k;
	imgObj = new Image();
	imgObj.src=thisImg.src;
	if(typeof(imgObj) == "object"){
		if((imgObj.width != 0) && (imgObj.height != 0)){
			width=imgObj.width;
			height=imgObj.height;
			k=width/height;
			if(k>=1){
				if(width>=FitW){width=FitW;height=width/k;}
			}else{
				if (height>=FitH){height=FitH;width=k*height;}
			}
			thisImg.setAttribute("width",width);
			thisImg.setAttribute("height",height);
		}
	}
}
//判断是否是正确的格式，未完成
function isRightPattern(pattern,getstr){
	switch(pattern){
		case "date":
			if(/^\d{4}-[0-12]-[0-3][0-9]$/gi.test("2007-12-99")){return true;}else{return false;}
	}
}
//判断是否是正确的日期格式
function isRightDate(sDate,datesplit){
	var MonDays = [31,28,31,30,31,30,31,31,30,31,30,31]
	var arrDate = new Array(3);
	var year, month, day;
	if (arguments.length != 2) return false;
	arrDate = sDate.toString().split(datesplit);
	if (arrDate.length != 3) return false;
	if (arrDate[1].length > 4 || arrDate[1].length > 2 || arrDate[2].length > 2) return false;
	if (isNaN(arrDate[0])||isNaN(arrDate[1])||isNaN(arrDate[2])) return false;		
	year = parseFloat(arrDate[0]);
	month = parseFloat(arrDate[1]);
	day=parseFloat(arrDate[2]);	
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) MonDays[1]=29;
	if (month < 1 || month > 12) return false;
	if (day < 1 || day > MonDays[month - 1]) return false;
	return true;
}
//把字符串转制为一个日期对象
function strToDate(sDate,datesplit){
	var year,month,day;
	var arrDate = new Array(3);
	if (isRightDate(sDate,"-"))	{
		arrDate = sDate.toString().split(datesplit);
		year = parseFloat(arrDate[0]);
		month = parseFloat(arrDate[1])-1;
		day=parseFloat(arrDate[2]);
		return (new Date(year,month,day));
	}else{return (new Date());}
}
//比较日期是否在某个范围内
function DateInScope(orgdate,mindate,maxdate){
	if(strToDate(orgdate,"-").getTime()<strToDate(mindate,"-").getTime() || strToDate(orgdate,"-").getTime()>strToDate(maxdate,"-").getTime()) return false;
	return true;
}

//获取字符长度
function strLen(key){
	var l=escape(key),len;
	len=l.length-(l.length-l.replace(/\%u/g,"u").length)*4;
	l=l.replace(/\%u/g,"uu");
	len=len-(l.length-l.replace(/\%/g,"").length)*2;
	return len;
}

function showt(tid){
	for(i=1;i<20;i++){
		if(tid==i){
			document.getElementById("child"+i).style.display=(document.getElementById("child"+i).style.display =='none')?'':'none';
		}else{
			if(document.getElementById("child"+i)){
			document.getElementById("child"+i).style.display="none";
			}
		}
	}
}
function fadeIn(color){
	this.style.backgroundColor=color;
}
function fadeOut(){
	this.style.backgroundColor='';
}
//图片自动调整的模式，1为按比例调整 ，2 按大小调整。
var resizemode=1;
function imgresize(o){
	if (resizemode==2 || o.onmousewheel){
		if(o.width > 500 ){
			o.style.width='500px';
		}
		if(o.height > 800){
			o.style.height='800px';
		}
	}
	else{
		var parentNode=o.parentNode.parentNode
		if (parentNode){
			if (o.offsetWidth>=parentNode.offsetWidth) o.style.width='50%';
		}
		else{
			var parentNode=o.parentNode
			if (parentNode){
				if (o.offsetWidth>=parentNode.offsetWidth) o.style.width='50%';
			}
		}
	}
}
/*选项卡切换*/
function tab_switch(blockid,showidx){
var tab_switch_type=document.getElementById(blockid);
var ref_dl=tab_switch_type.getElementsByTagName("dl")[0];
var ref_dl_dt=ref_dl.getElementsByTagName("dt");
for (var i=0;i<ref_dl_dt.length;i++){
	ref_dl.getElementsByTagName("dd")[i].style.display="none";
	ref_dl.getElementsByTagName("dt")[i].setAttribute("idx",i);
	ref_dl.getElementsByTagName("dt")[i].onmouseover=function(){
	for(var j=0;j<ref_dl_dt.length;j++){ref_dl.getElementsByTagName("dd")[j].style.display="none";ref_dl.getElementsByTagName("dt")[j].className="";}
	this.className="focus";
	ref_dl.getElementsByTagName("dd")[this.getAttribute("idx")].style.display="block";
	}
}
ref_dl.getElementsByTagName("dd")[showidx].style.display="block";
ref_dl.getElementsByTagName("dt")[showidx].className="focus";
}

