﻿//myAjax.js

//通过ID获取控件
function $(objId)
{
    return document.getElementById(objId);
}

//控件内部输出值
function echo(objName, objValue, SetValue){
    var obj = typeof(objName) == "object" ? objName : $(objName);
	if (objValue == null) objValue = (SetValue == null ? '<img src="/images/loading.gif" style="border:0px" /> loading...' : 'loading...'); 
	SetValue == null ? obj.innerHTML = objValue : obj.value = objValue;
}

//通过name获取控件集合
function $N(objName)
{
    return document.getElementsByName(objName);
}

//通过tagName获取控件集合
function $T(objName)
{
    return document.documentElement.getElementsByTagName(objName);
}

//url参数
function $Q(urlId, splitStr, isTop)
{
	var Url = !isTop ? window.location.href : top.window.location.href;
	var u = Url.indexOf(!splitStr ? '?' : splitStr);
	var g = u >= 0 ? Url.substring(u + 1) : '';
	if(g != '')
	{
	    if(typeof(urlId) != "string") 
	    {
	        return g;
	    }

	      var gg = g.split('&');
		  var str = urlId + '=';
		  for(xm = 0; xm < gg.length; xm++)
		  {
			  if (gg[xm].indexOf(str) == 0) 
			  {
				  return ((gg[xm].replace(str, '')).split('#')[0]);
			  }
		  }
	}
	return '';
}

//除去首尾空格
function trim(str) {
	return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
}

//Ajax提交
function myAjax(WebUrl, FuncName, IsXmlHttpObject, postData)
{
    var xmlhttp = false;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml');
	}
	else if (window.ActiveXObject) {
        var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
        for (var i = 0; i < versions.length; i++) {
            try {
				xmlhttp = new ActiveXObject(versions[i]);
				if (xmlhttp) break;
			}
			catch(e){}
		}
	}
    if (xmlhttp) {
        xmlhttp.open(postData == null ? "GET" : "POST", WebUrl, true);
		xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status==200) {
				var Value = (!IsXmlHttpObject) ? xmlhttp.responseText : xmlhttp;
				switch(typeof(FuncName))
				{
				    case "function":
				        FuncName(Value);
				        break;
				    case "string":
				        eval(FuncName);
				        break;
				}
			}
		}
        if (postData != null)
		{
			xmlhttp.setRequestHeader("content-length",postData.length);
			xmlhttp.setRequestHeader("content-type","application/x-www-form-urlencoded");
		}
		xmlhttp.send(postData);
	}
	else
	{
		alert('你的浏览器不支持XMLHTTP,请升级');
	}
}

//添加收藏
function AddFavorite(urlStr, urlDes)
{
	if(urlStr == null)
	{
		urlStr = 'http://www.infocom.cn';
		urlDes = '中国市场信息网 - www.infocom.cn';
	}
	window.external.AddFavorite(urlStr, urlDes);
}

//设为首页
function SetHomePage(obj, urlStr)
{
	if(urlStr == null) urlStr = 'http://www.infocom.cn';
	obj.style.behavior='url(#default#homepage)';
	obj.setHomePage(urlStr);
}

//回车提交
function onSubmitKey(FunName)
{
    
    if (window.event.keyCode == 13)
    {
        switch (typeof(FunName))
        {
            case 'function' : FunName; break;
            case 'string' : eval(FunName); break;
        }
        return false;
    }
    
    return true;
}

//表单提交
function Ctlent(theForm){
//ctrl + Enter
if(event.ctrlKey && window.event.keyCode==13){
  if(ClcKcntr()){
    theForm.submit();
  }
}
//ctrl + s
else if(event.ctrlKey && window.event.keyCode==83){
  if(ClcKcntr()){
    theForm.submit();
  }
}
}

//关闭窗口
function CloseWindow(alertStr)
{
    if (alertStr != null) alert(alertStr);
    window.open('','_parent','');
    window.close();
}


//复制代码
function Copytxt(id)
{
 var txt = document.body.createTextRange();
 txt.moveToElementText($(id));
 txt.select();
 document.execCommand("Copy"); 
}


//删除双重BR
function delbr(id,num){
	var dobj = $(id);
	var tdobj = dobj.getElementsByTagName("td");
	var con,temcon,tcode;
	for(i=0;i<tdobj.length;i++){
		con = tdobj[i].innerHTML;
		if(num==1)
			tdobj[i].innerHTML = con.substring(0,con.length-4);
		else
			tdobj[i].innerHTML = con.replace(/(\<br\>)+/gi,"<br>");		
	}
}


//文本框获取焦点
function inputFocus(obj){
	$(obj).focus();
}

//拖动DIV
var ActiveDivID = '';

function toDiv_event_mousedown(e, divID){
    var e, obj, temp;
    ActiveDivID = divID;
    obj=$(divID);
    e=window.event?window.event:e;  
    obj.startX=e.clientX-obj.offsetLeft;  
    obj.startY=e.clientY-obj.offsetTop;    
    document.onmousemove=document_event_mousemove;  
    temp=document.attachEvent?document.attachEvent('onmouseup',document_event_mouseup):document.addEventListener('mouseup',document_event_mouseup,'');
}

function document_event_mousemove(e){  
    if(ActiveDivID == '') return false;
    var e, obj;
    obj=$(ActiveDivID);  
    e=window.event?window.event:e;
    with(obj.style){
        position='absolute';
        left=e.clientX-obj.startX+'px';   
	    top=e.clientY-obj.startY+'px';  
	}
}

function document_event_mouseup(e){ 
	var temp; 
	document.onmousemove='';
	ActiveDivID = '';
	temp=document.detachEvent?document.detachEvent('onmouseup',document_event_mouseup):document.removeEventListener('mouseup',document_event_mouseup,'');
}

function openWithIframe(tit,url,w,h,scrol){
$('massage_box').style.left = ((screen.width - w) / 2)+'px';
$('massage_box').style.top = ((screen.height - h) / 2-50)+'px';
$('massage_box').style.screenx = ((screen.width - w) / 2)+'px';//仅适用于Netscape
$('massage_box').style.screeny = ((screen.height - h) / 2-50)+'px';//仅适用于Netscape
$('massage_box').style.width = w+"px";
$('massage_box').style.height = h+"px";
echo('pop_title',tit);
$('mask').style.display='inline';
$('massage_box').style.display='inline'
var popiframe='<iframe src="'+url+'" width="'+(w-12)+'px"  height="'+(h-37)+'px" frameborder="0" scrolling="'+(!scrol ? 'no':'yes')+'"></iframe>';
echo('pop_iframe',popiframe);
$('mask').style.height = document.body.scrollHeight+'px';
$('mask').style.width = document.body.scrollWidth+'px';
}

function closeWithIframe(){
$('massage_box').style.display="none";
$('mask').style.display="none";
}

//防重复提交判断
var clckcnt = 0;
function ClcKcntr(){
  clckcnt++;
  if(clckcnt > 1){
    alert('请求已经发出，请等待片刻！\n\n'+'不要重复提交，谢谢！');
	return false;
  }
  return true;
}

//设置select值
function setSelectedIndex(objName, selectedValue)
{
    var obj = $(objName);
    for(var i=0; i< obj.options.length; i++)
    {
        if(obj.options[i].value == selectedValue)
        {
            obj.selectedIndex = i;
            break;
        }
    }
}

function onCheckAll(checkboxID, checkboxsName, labelName)
{
	var allcb = $N(checkboxsName); 
	if (allcb.length == 0) return false; 
	var ischecked = $(checkboxID).checked; 
	for(var ii = 0; ii < allcb.length; ii++) { allcb[ii].checked = ischecked; }
	if(labelName!=null && labelName != '') echo($(labelName), ischecked ? '取消全选' : '全选');
}

function SetData(ObjName, Content, AlertInfo)
{
	var meintext = !ObjName ? Content : $(ObjName).value;
	if (window.clipboardData) 
	{

	// the IE-manier
	window.clipboardData.setData('Text', meintext);

	// waarschijnlijk niet de beste manier om Moz/NS te detecteren;
	// het is mij echter onbekend vanaf welke versie dit precies werkt:
	}
	else if (window.netscape) 
	{ 

	// dit is belangrijk maar staat nergens duidelijk vermeld:
	// you have to sign the code to enable this, or see notes below 
	netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

	// maak een interface naar het clipboard
	var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
	if (!clip) return;

	// maak een transferable
	var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
	if (!trans) return;
   
	// specificeer wat voor soort data we op willen halen; text in dit geval
	trans.addDataFlavor('text/unicode');

	// om de data uit de transferable te halen hebben we 2 nieuwe objecten 
	// nodig om het in op te slaan
	var str = new Object();
	var len = new Object();
	
	var str = Components.classes['@mozilla.org/supports-string;1'].createInstance(Components.interfaces.nsISupportsString);
   
	var copytext=meintext;
	
	str.data=copytext;
	
	trans.setTransferData("text/unicode",str,copytext.length*2);
	
	var clipid=Components.interfaces.nsIClipboard;
	
	if (!clip) {alert('可能是浏览器原因导致失败，请点击右键复制吧^_^');return;}
	
	clip.setData(trans,null,clipid.kGlobalClipboard);
	}
	if(AlertInfo != null) alert(AlertInfo);
}


//设定图片显示尺寸
var flag=false; 
function DrawImage(ImgD,w,h){ 
var image=new Image(); 
image.src=ImgD.src; 
if(image.width>0 && image.height>0){ 
  flag=true; 
  if(image.width/image.height>= w/h){ 
    if(image.width>w){ 
      ImgD.width=w; 
      ImgD.height=(image.height*w)/image.width; 
    }else{ 
      ImgD.width=image.width; 
      ImgD.height=image.height; 
    } 
  }else{ 
    if(image.height>h){ 
      ImgD.height=h; 
      ImgD.width=(image.width*h)/image.height; 
    }else{ 
      ImgD.width=image.width; 
      ImgD.height=image.height; 
    } 
  } 
} 
} 