// JavaScript Document
function $(sId){
	if( !sId ){
		return null;
	}
	var returnObj = document.getElementById( sId );
	if( !returnObj && document.all ){
		returnObj = document.all[sId];
	}
	return returnObj;
	//return document.getElementById(id);
}

//checkbox全选或全部消除
function selectAll(Name,Base){
	var allid = new Array;
	allid = document.getElementsByName(Name);
	for( var i = 0; i < allid.length; i++ ){
		allid[i].checked = Base.checked;
	}
}

function ImgShow(divnode,imgUrl,tunode,linknode,initUrl){
	if( imgUrl == "" )
		imgUrl = initUrl;
	var post = resetImg(imgUrl);
	tunode.src = imgUrl;
	tunode.style.width=post[0];
	divnode.style.width=post[0]+15;
	divnode.style.height=post[1]+25;
	tunode.style.height=post[1];
	divnode.style.top=linknode.style.bottom;
	divnode.style.left=linknode.style.right;
	divnode.style.display='';
}
function resetImg(imgUrl){
	var Img = new Image();
	var lang,short,rt,temp,maxlang=100;
	Img.src = imgUrl;
	var width = Img.width;
	var height = Img.height;
	//alert(Img.src+"\n"+"宽："+width+"\n高："+height);
	if( width > height ){
		lang = width;
		short = height;
		rt = true;
	}else{
		lang = height;
		short = width;
		rt = false;
	}
	if( lang > maxlang ){
		short = Math.round(short*maxlang/lang);
		lang = retk = maxlang;
	}
	if( rt ){
		temp = Array(lang,short);
	}else{
		temp = Array(short,lang);
	}
	return temp;
}

function checkImg(OBJ){
	if( OBJ.value == "" )
		return false;
	var fileext=OBJ.value.substring(OBJ.value.length,OBJ.value.length-4);
	fileext=fileext.toLowerCase();
	if (!(fileext==".jpg" || fileext==".png" || fileext==".gif")){
		OBJ.outerHTML = OBJ.outerHTML;
		alert(" 对不起，起无效的图片格式！ ");
		return false;
	}
	return true;
}
function showImg(OBJ,IdName,MRurl,Showstr,Attr) {
	if( OBJ.value == "" ){
		if( MRurl != "" ){
			showPhoto(IdName,MRurl,Attr);
			return true;
		}else{
			setPicShow(IdName,Showstr);
			return false;
		}
	}
	
	if (!checkImg(OBJ)){
		if( MRurl != "" ){
			showPhoto(IdName,MRurl,Attr);
			return true;
		}else{
			setPicShow(IdName,Showstr);
			return false;
		}
	}else{ 
		showPhoto(IdName,OBJ.value,Attr);
	}
}
function showPhoto(ID,URL,width,height){
	var widthstr = "",heightstr = "";
	/*if( Attr == "bt" ){
		width = 760;
		height = 200;
	}else{
		width = 200;
		height = 200;
	}*/
	
	if( typeof( width ) != "undefined" && width != "" && width != 0 && width != "0" ) widthstr = ' width="'+width+'" ';
	if( typeof( height ) != 'undefined' && height != "" && height != 0 && height != "0" ) heightstr = ' height="'+height+'" ';
	ID.innerHTML='<img src="'+URL+'" border="0"'+widthstr+heightstr+' />';
}
function setPicShow(ID,STR){
	ID.innerHTML = STR;
}
function changeBG(IM){
	checkImg(IM);
	document.body.style.backgroundImage="url("+IM.value+")";
}
function in_array(searchVar,marray){
	for( var i = 0; i < marray.length; i++ ){
		if( searchVar == marray[i] )
			return true;
	}
	return false;
}
function sub_str(str,len,place,expstr){
   if(str.replace(/[^\x00-\xff]/gi,'xx').length<=len) return str; 
   
   if(place=='left'){
      var temp_len=len;  
      str=str.substr(0,len);
      while(str.replace(/[^\x00-\xff]/gi,'xx').length>len){
         str=str.substr(0,--temp_len);
      }
   }
   if(place=='right'){
      var temp_startn=0;
      str=str.substr(str.length-len,len);
      while(str.replace(/[^\x00-\xff]/gi,'xx').length>len){      
         str=str.substr(1,len);
      }     
   }
   return str+expstr;
}

//删除空白节点
function cleanWhitespace(element){
	for( var i = 0; i < element.childNodes.length; i++ ){
		var node = element.childNodes[i];
		if( node.nodeType == 3 && !/\S/.test(node.nodeValue) )
			node.parentNode.removeChild(node);
	}
}

//获得某节点的第N个子节点
function getNsun(node,N){
	cleanWhitespace(node);
	return node.childNodes[N];
}

//在指定的节点中加入文本
function addText(setNode,textvalue){
  var node = document.createTextNode(textvalue);
  return setNode.appendChild(node);
}

//向tr节点中插入TD
function td2tr(setNode,textvalue){
	var tdNode = document.createElement("td");
	addText(tdNode,textvalue);
	setNode.appendChild(tdNode);
}

function announcement(ID) {
	$(ID).innerHTML = '<marquee style="margin: 0px 8px" direction="left" scrollamount="2" scrolldelay="1" onMouseOver="this.stop();" onMouseOut="this.start();">' +
		$(ID).innerHTML + '</marquee>';
	$(ID).style.display = 'block';
}

//去左空格; 
function ltrim(s){ 
	return s.replace( /^\s*/, ""); 
} 
//去右空格; 
function rtrim(s){ 
	return s.replace( /\s*$/, ""); 
} 
//去左右空格; 
function trim(s){ 
	return rtrim(ltrim(s)); 
}

function addCookie(name,value,expireHours){
	var cookieString = name+"="+escape(value);
	if( expireHours > 0 ){
		var date = new Date();
		date.setTime(date.getTime()+expireHours*3600*1000);
		cookieString = cookieString+"; expires="+date.toUTCString();
	}
	document.cookie = cookieString;
}

/*function getCookie(name){
	var strCookie = document.cookie;
	var arrCookie = strCookie.split("; ");
	for( var i = 0; i < arrCookie.length; i++ ){
		var arr = arrCookie[i].split("=");
		if( arr[0] == name ) return arr[1];
	}
	return "";
}*/

function deleteCookie(name){
	var date = new Date();
	date.setTime(-10000);
	document.cookie = name+"=0; expires="+date.toUTCString();
}
//获得Cookie的原始值
function GetCookie(name){
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen){
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return GetCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return '';
}
//获得Cookie解码后的值
function GetCookieVal(offset){
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
  endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

//验证E-mail
function verifyAddress(EMAIL){
　　var pattern = /^[a-zA-Z0-9_][\.\-_a-zA-Z0-9]*@([a-zA-Z0-9_\-])+(\.[a-zA-Z0-9_\-])+/;
　　flag = pattern.test(EMAIL);
　　if(flag){
　　　return true;
　　}else{
　　　return false;
　 }
}

//检测数字
function checkNum(e){
	if(e.value=="")
		return;
	/^([0-9]*)/.exec(e.value);
	if(e.value!=RegExp.$1)
		e.value=RegExp.$1;
}

//限制只允许输入数字，ifpoint是是否可以输入小数
function getNum(evkey,ifpoint){
	//alert(evkey);
	if( !ifpoint && (evkey == 190 || evkey == 110 ) ){
		return false;
	}
	if((evkey >= 48 && evkey <= 57) || (evkey >= 96 && evkey <= 105) || evkey == 190 || evkey == 8 || evkey == 46 || evkey == 110)
		return true;
	else
		return false;
}

//通过给定的维修商ID返回维修商页面地址
function getMaintainerUrl(fid,fstatus){
	var g_out = "http://www.txiu.com.cn/";
	if( fid == "" || fid == 0 || fid == null ) return g_out+"index.htm";
	var pagename = "";
	switch( fstatus ){
		case 2:
			pagename = "aboutus";			//公司简介
			break;
		case 3:
			pagename = "jscs";					//技术实力
			break;
		case 4:
			pagename = "employee";			//人员简介
			break;
		case 5:
			pagename = "contact";			//联系我们
			break;
		case 6:
			pagename = "service";			//服务质量
			break;
		case 7:
			pagename = "wxkind";				//维修类别
			break;
		case 8:
			pagename = "news";					//咨询
			break;
		case 9:
			pagename = "price";				//报价
			break;
		
		default:
			pagename = "index";				//默认首页
	}
	return g_out+fid+"/"+pagename+".html";
	//return $g_out['htp']."mtself/{$pagename}.php?fid=$fid";
}

function correctPNG()
{
   for(var i=0; i<document.images.length; i++)
   {
   var img = document.images[i]
   var imgName = img.src.toUpperCase()
   if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
   {
   var imgID = (img.id) ? "id='" + img.id + "' " : ""
   var imgClass = (img.className) ? "class='" + img.className + "' " : ""
   var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
   var imgStyle = "display:inline-block;" + img.style.cssText
   if (img.align == "left") imgStyle = "float:left;" + imgStyle
   if (img.align == "right") imgStyle = "float:right;" + imgStyle
   if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle 
   var strNewHTML = "<span " + imgID + imgClass + imgTitle
   + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
   + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
   + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
   img.outerHTML = strNewHTML
   i = i-1
   };
   };
};

if(navigator.userAgent.indexOf("MSIE")>-1)
{
window.attachEvent("onload", correctPNG);
};