﻿// JavaScript Document
function $(id) { return (document.getElementById(id)||document.getElementsByTagName(all)[id]);}
// 重置页面容器高度
function resizeHeight(id,cutheight) {
	var bodyHeight = document.body.clientHeight;
	$(id).style.height = bodyHeight-cutheight < 115 ? "115px" : (bodyHeight-cutheight) + "px";
}
// 重置页面容器宽度
function resizeWidth(id,cutwidth) {
	var bodyWidth = document.body.clientWidth;
	$(id).style.width = (bodyWidth-cutwidth) + "px";
}
// 插入标签
function insertLabel(contentObj,labelContent)
{
	$(contentObj).focus();
    var str = document.selection.createRange();
  	str.text = labelContent; 
}
// 打开窗口
function openWindow(url,width,height,windowObj){
	var ReturnStr=showModalDialog(url,windowObj,'dialogWidth:'+width+'pt;dialogHeight:'+height+'pt;status:no;help:no;scroll:no;status:0;help:0;scroll:0;');
	return ReturnStr;
}
// 打开窗口 返回值给父窗口
function openThenSetValue(url,width,height,windowObj,setObj){
	if (document.all){
	var ReturnStr=showModalDialog(url,windowObj,'dialogWidth:'+width+'pt;dialogHeight:'+height+'pt;status:no;help:no;scroll:no;status:0;help:0;scroll:0;');
	if (ReturnStr!='') $(setObj).value=ReturnStr;
	if (ReturnStr==null) $(setObj).value='';
	return ReturnStr;
	}else{
		 
	 var obj = $(setObj);
	 width=width+180;
	 height=height+80;
	 window.open(wrl,'newWin','modal=yes,width='+width+',height='+height+',resizable=no,scrollbars=no');
	}
}

/* 验证表单 
   参数: idArray 表单元素数组,正则表达式数组,提示信息数组
*/
function checkFormByAlert(idArray,patternArray,errorMessage) {
	var flag = true;
	for(i = 0; i < idArray.length; i++) {
		if($(idArray[i]).value.match(patternArray[i])==null) {
			alert(errorMessage[i]);
			$(idArray[i]).focus();
			flag = false;
			break;
		}
	}
	return flag;
}

// 改变表格背景色
function changeBgColor(obj,classname) {
	var elements = obj.childNodes;
	for(var i=0;i<elements.length;i++)
	{
		elements[i].className = classname;
	}
	obj.onmouseout = function() {
		for(var i=0;i<elements.length;i++)
		{
			elements[i].className = "";
		}
	}
}

// 重置当前页面
function resetUrl(url,action,urlid) {
    location.href = url + "?action=" + action + "&id=" + urlid;
}

// 全选|反选
function selectAll(e,parentId,tag) {
	var checkboxs=document.getElementById(parentId).getElementsByTagName(tag);
    for (var i=0; i<checkboxs.length; i++) {              
    	checkboxs[i].checked = e.checked;
    }
}

// 全选|反选 <input onclick="selectAll('e','itemName')" /> e 触发事件checkbox,itemName为要全选的checkbox子项名称
function selectAll(e,itemName) {
	var checkboxs = document.getElementsByName(itemName);
	for (var i=0; i<checkboxs.length; i++) {              
    	checkboxs[i].checked = e.checked;
    }
}
// 获取表单内指定元素值(集合) form表单对象,ename元素名称
function getIds(form,ename) {
	var ids='';
	for (var i=0;i < form.elements.length;i++) {
		var e = form.elements[i];
		if (e.name == ename){
			if (e.checked==1){
				if (ids=='')
				ids=e.value;
				else
			    ids+=","+e.value;
			}
		}
	}
	return ids;
}
// 左侧控制(show or hidden,control为主控标记,bycontrol为受控标记)
function showHidden(control,bycontrol) {
	if($(bycontrol).style.display == "") {
		$(bycontrol).style.display = "none";
		$(control).src = "Images/Permissions/arrow_right.gif";
	}
	else if($(bycontrol).style.display == "none") {
		$(bycontrol).style.display = "";
		$(control).src = "Images/Permissions/arrow_left.gif";
	}
	else { 
		$(bycontrol).style.display=""; 
		$(control).src = "Images/Permissions/arrow_right.gif";
	}
}
// 左侧滑动菜单
function switchMenu(menu,number,count) {
	for(i=1; i <=count; i++) {
		if (i==number) { 
			$(menu + i).className = menu + "On"; 
			$(menu + "box" + i).style.display="block";
		}
		else { 
			$(menu + i).className = menu + "Off"; 
			$(menu + "box" + i).style.display="none";
		}
	}
}
// 开关
function showMenu(menu,css) {
	if($("box_" + menu).style.display == "") {
		$("box_" + menu).style.display = "none";
		$(menu).className= css + "Off";
	}
	else if($("box_" + menu).style.display == "none") {
		$("box_" + menu).style.display = "";
		$(menu).className= css + "On";
	}
	else {
		$("box_" + menu).style.display = "";
		$(menu).className= css + "On";
	}
}
// 显示元素
function showElement(id) {
	$(id).style.display = "";
}
// 隐藏元素
function hideElement(id) {
	$(id).style.display = "none";
}

/* 菜单控制(绝对层) 外部可重置数组
   controlLayer 菜单ID
   contentBox 内容容器
   contents 菜单内容
   left 菜单左边位置

var subArr = new Array(
"<a href=\"#\">公司简介</a><a href=\"#\">企业文化</a><a href=\"#\">企业荣誉</a><a href=\"#\">招贤纳士</a>",
"<a href=\"#\">公司新闻</a><a href=\"#\">行业新闻</a>",
"<a href=\"#\">咸味香味精</a><a href=\"#\">甜味香味精</a><a href=\"#\">香料</a><a href=\"#\">速冻食品</a>",
"<a href=\"#\">咨询建议</a><a href=\"#\">联系方式</a>",
"<a href=\"#\">咨询建议</a><a href=\"#\">联系方式</a>"
);*/
function showSub(controlLayer,contentBox,contents,left)
{
	$(controlLayer).style.display='';
	$(controlLayer).style.left=left;
	$(contentBox).innerHTML=value;
	$(controlLayer).onmouseover=function(){$(controlLayer).style.display=''; $(controlLayer).style.left=left;}
	$(controlLayer).onmouseout=function(){hiddenSub(controlLayer);}
}
// 隐藏层
function hiddenSub(controlLayer){$(controlLayer).style.display='none';}

// ajax object
function ajaxObj() {
	var httpRequest;
　　　　try{
　　　　　　　httpRequest=new ActiveXObject("Msxml2.XMLHTTP");
　　　　	}catch(e){
　　　　　　　try{
　　　　　　　　　　httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
　　　　　　　}catch(e1){
　　　　　　　　　httpRequest=new XMLHttpRequest();
　　　　　　　　}
　　　　}
　　　　return httpRequest;
}
// ajax 请求
function sendRequest(posturl) { 
  try{
	var ajax = new ajaxObj(); 
	ajax.open("get",posturl,true);
	ajax.onreadystatechange = function(){
		if(ajax.readyState == 4) {
			if (ajax.status == 200) {
				if(ajax.responseText!=""&&ajax.responseText!=null)
				{ 
					alert(ajax.responseText);
					window.location.reload();
				}
			}
		}
	}
	ajax.send(null);  
  }
  catch(e)
  {}
}

function sendRequestQ(posturl) { 
  try{
	var ajax = new ajaxObj(); 
	ajax.open("get",posturl,true);
	ajax.onreadystatechange = function(){
		if(ajax.readyState == 4) {
			if (ajax.status == 200) {
				return true;
			}
			else { return false; }
		}
	}
	ajax.send(null);  
  }
  catch(e)
  {}
}

// 下载请求
function sendDown(posturl) { 
  try{
	var ajax = new ajaxObj(); 
	ajax.open("get",posturl,true);
	ajax.onreadystatechange = function(){
		if(ajax.readyState == 4) {
			if (ajax.status == 200) {
				if(ajax.responseText!=""&&ajax.responseText!=null)
				{ 
				    if(ajax.responseText.split('=')[0]=="true")
				    {
				        window.open(ajax.responseText.split('=')[1],'_blank');
				    }
				    else
				    {alert(ajax.responseText.split('=')[1]);}
					
				}
			}
		}
	}
	ajax.send(null);  
  }
  catch(e)
  {}
}

//post请求的方法，参数传在send方法中
function sendForm(posturl,sendData){	 
  try{
	var ajax = new ajaxObj(); 
	ajax.open("POST",posturl,true);
	ajax.onreadystatechange=function(){ 
		if (4==ajax.readyState){ 
			if (ajax.status == 200){return true;}
			else{return false;}
		} 
	}
	ajax.setrequestheader("content-length",sendData.length); 
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
        ajax.send(sendData);
  }
  catch(e)
  {}
}

// ajax 用于左侧滑动菜单
function getResponse(posturl,box) { 
  try{
	var ajax = new ajaxObj(); 
	ajax.open("get",posturl,true);
    ajax.onreadystatechange = function(){
		if(ajax.readyState == 1) { 
			$(box).innerHTML="正在加载数据...";
		}
	    else if(ajax.readyState == 2 || ajax.readyState == 3) {
			$(box).innerHTML="正在读取数据...";
		}
		else if(ajax.readyState == 4) {
			if (ajax.status == 200) {
				$(box).innerHTML = ajax.responseText;
			}
		}
	}
	ajax.send(null);  
  }
  catch(e)
  {}
}
// ajax 用于左侧滑动菜单
function getTree(posturl,treeBox) { 
  try{
	var ajax = new ajaxObj(); 
	ajax.open("get",posturl,true);
    ajax.onreadystatechange = function(){
		if(ajax.readyState == 1) { 
			$(treeBox).innerHTML="<span style='margin:12px;'><img src='images/ico/loading.gif'> 正在加载数据...</span>";
		}
	    else if(ajax.readyState == 2 || ajax.readyState == 3) {
			$(treeBox).innerHTML="<span style='margin:12px;'><img src='images/ico/loading.gif'> 正在读取数据...</span>";
		}
		else if(ajax.readyState == 4) {
			if (ajax.status == 200) {
				$(treeBox).innerHTML = ajax.responseText;
			}
		}
	}
	ajax.send(null);  
  }
  catch(e)
  {}
}
// 获取当前系统时间
function myTime(timebox){
    $(timebox).innerHTML = new Date().toLocaleString();
    var mytimer = setTimeout("myTime('"+timebox+"')",1000);
}
//window.onload = function(){ resizeBox('leftbox',128); resizeBox('togglebox',128); resizeBox('rightbox',128);}
//window.onresize = function(){ resizeBox('leftbox',128); resizeBox('togglebox',128); resizeBox('rightbox',128); }

function editTab()   
{   
    var code, sel, tmp, r   
    var tabs=""  
    event.returnValue = false  
    sel =event.srcElement.document.selection.createRange()   
    r = event.srcElement.createTextRange()   
  
    switch (event.keyCode)   
    {   
        case (8)    :   
            if (!(sel.getClientRects().length > 1))   
            {   
                event.returnValue = true  
                return  
            }   
            code = sel.text   
            tmp = sel.duplicate()   
            tmp.moveToPoint(r.getBoundingClientRect().left, sel.getClientRects()[0].top)   
            sel.setEndPoint("startToStart", tmp)   
            sel.text = sel.text.replace(/^\t/gm, "")   
            code = code.replace(/^\t/gm, "").replace(/\r\n/g, "\r")   
            r.findText(code)   
            r.select()   
            break  
        case (9)    :   
            if (sel.getClientRects().length > 1)   
            {   
                code = sel.text   
                tmp = sel.duplicate()   
                tmp.moveToPoint(r.getBoundingClientRect().left, sel.getClientRects()[0].top)   
                sel.setEndPoint("startToStart", tmp)   
                sel.text = "\t"+sel.text.replace(/\r\n/g, "\r\t")   
                code = code.replace(/\r\n/g, "\r\t")   
                r.findText(code)   
                r.select()   
            }   
            else  
            {   
                sel.text = "\t"  
                sel.select()   
            }   
            break  
        case (13)   :   
            tmp = sel.duplicate()   
            tmp.moveToPoint(r.getBoundingClientRect().left, sel.getClientRects()[0].top)   
            tmp.setEndPoint("endToEnd", sel)   
  
            for (var i=0; tmp.text.match(/^[\t]+/g) && i<tmp.text.match(/^[\t]+/g)[0].length; i++)   tabs += "\t"  
            sel.text = "\r\n"+tabs   
            sel.select()   
            break  
        default     :   
            event.returnValue = true  
            break  
    }   
} 
