﻿// JScript File

//Open Edit Window with URL
function OpenEditWin(url)
{
	var height = 700;
	var width = 1000;
	var name = "EditForm";
	OpenWin(url, name, height, width);	
}
//Open Tiny Window with URL
function OpenTinyWin(url)
{
	OpenWin(url,'Tiny', '240', '480');
}
//Open Small Window with URL
function OpenSmallWin(url)
{
	OpenWin(url,'small', '350', '600');
}
//Open Big Window with URL
function OpenBigWin(url)
{
	OpenWin(url,'big', '600', '800');
}
//Open Free-Controlled Window
function OpenWinASSiz(url,name,height,width)
{
	OpenWin(url, name, height, width);
  // OpenDialog(url, name, height, width);
}
function Get_Center(size, side)
{
	self.y_center=(parseInt(screen.height/2));
	self.x_center=(parseInt(screen.width/2));
	center = eval('self.'+side+'_center-('+size+'/2);');
	return(parseInt(center));
}
function OpenWinWithoutScrollbars(url, name, height, width)
{
    //OpenDialog(url, name, height, width);
	var left = Get_Center(width,'x');	
	var top = Get_Center(height,'y');
	if (top > 30)
	{
		top = top - 30;
	}
	var win = window.open(url,name,"height=" + height + ",width=" + width + ",left=" + left + ",top=" + top + ",scrollbars=no,toolbar=no,menubar=no,location=no,resizable=no");
    window.onfocus = function() {
			if(win != null && win.closed == false)
			{
                win.focus();
             }
             else
             {
                win = null;
             }
    }
}

function OpenWin(url, name, height, width)
{
	var left = Get_Center(width,'x');	
	var top = Get_Center(height,'y');
	if (top > 30)
	{
		top = top - 30;
	}
	var win = window.open(url,name,"height=" + height + ",width=" + width + ",left=" + left + ",top=" + top + ",scrollbars=yes,toolbar=no,menubar=no,location=no,resizable=no");
    window.onfocus = function() 
    {
	    if(win != null && win.closed == false)
	    {
            win.focus();
         }
         else
         {
            win = null;
         }
    }
//    win.onblur = function()
//    {
//         if(win != null && win.closed == false)
//         {
//            win.focus();
//         }
//    }
}
//用div模拟模态窗口
var isClsoeWin = true;
function OpenDialog(url, name, height, width)
{
    var divBlack = document.getElementById('divBlack');
    if(divBlack)
    {
        var isOpera = window.navigator.appName.toUpperCase().indexOf('OPERA') > -1;
        if(!isOpera)
        {
            divBlack.style.height = getPageHeight() +'px';
            divBlack.style.display = 'block';
//            alert(divBlack.style.height);
        }
        
	    var left = Get_Center(width,'x');	
	    var top = Get_Center(height,'y');
	    if (top > 30)
	    {
		    top = top - 30;
	    }
	    win = window.open(url,name,"height=" + height + ",width=" + width + ",left=" + left + ",top=" + top + ",scrollbars=yes,toolbar=no,menubar=no,location=no,resizable=no");
        
        window.onfocus = function() {setfocus(win,divBlack);}
        
        if(!isOpera)
        {
            if(window.addEventListener)
            {
                win.onunload = function (){isClsoeWin = !isClsoeWin;if(isClsoeWin)divBlack.style.display = 'none';};
                divBlack.onclick = function(){setfocus(win,divBlack);};
            }
            else
            {
                divBlack.onfocus = function(){setfocus(win,divBlack);};
            }
        }
    }
}
function setfocus(win,divBlack) 
{
    if(win != null && win.closed == false)
        win.focus();
     else
     {
        win = null;
        divBlack.style.display = 'none';
     }
}

function openNewWin(url, name)
{
  window.open(url,name);
  return false;
}

function WindowOpen(url)
{
    window.open(url);
    return false;
}

function OpenViewWin(url)
{
    if (url.indexOf('?') > 0)
        url = url  + "&rdm=" + Math.random();
    else
        url = url  + "?rdm=" + Math.random();
    var win = window.open(url, "view");
    win.focus();
}

//go to window with params
function gotoWin(url)
{
    if (url.indexOf('?') > 0)
        window.location.href = url  + "&rdm=" + Math.random();
    else
        window.location.href = url  + "?rdm=" + Math.random();
  return false;
}

//go to window without params
function gotoWin2(url)
{
    if (url.indexOf('?') > 0)
        window.location.href = url;
    else
        window.location.href = url;
  return false;
}

//Trim all space
function TrimAll(str)
{
    str = str.replace(/\ /g, '');
    return str;
}
//Trim both sides space
function Trim(str)
{
    str = str.replace(/(^\s*)|(\s*$)/g, "");
    return str;
}
//Set CheckBox with <check> by <id> which is the part ID of CheckBoxes 
function CheckAll(check, id)
{
	var e = document.forms[0].elements;
	var l = e.length;
	var o;
	for(var i = 0; i < l; i++)
	{
		o = e[i];
		if (o.type == "checkbox" && o.id.indexOf(id) > -1)
		{
			if(o.disabled!=true)
			{
			    o.checked = check;
			}
		}
	}
}
//validate length of textbox
function ValidateLength(oSrc, args)
{
	args.IsValid = (GetByteLength(args.Value) <= oSrc.getAttribute("limit")); 
}
function GetByteLength(str)
{
	return Trim(str).length;
}

// showHideTooltip = function ()
function showHideTooltip(ddlClientID,e)
{//show the tooltip
   //var obj = document.getElementById(ddlClientID);
  var obj = document.getElementById(ddlClientID);
  //var objs = document.getElementById(imgShow);
   var str =obj.options[obj.selectedIndex].text;
    e = e ? e : event;
   var count = 0;
   var length =53;
   var strResult = '';
   if(str.length>length)
   {
         count = Math.ceil(str.length/length);
   
        for(i = 0;i<count;i++)
       {         
           strResult  = strResult.concat(str.substr(i * length, [length]).concat("<br />"));

        }
   	    if(str.length % length != 0)
	    {
		    strResult= strResult.concat(str.substr(count * length));
	    } 
          str = strResult;
   }
  
    if(str != "[Select]")
   {               
        document.getElementById(ddlClientID+"_tooltip").innerHTML =str ;
        if(navigator.appName == 'Microsoft Internet Explorer')
        { 

        if(e.type == "mouseout")
        {
            document.getElementById(ddlClientID+"_tooltip").style.visibility = "hidden";
           document.getElementById(ddlClientID+"_tooltip").style.display = "none"; 
        }
        else if (e.type == "mouseover")
        {            
           document.getElementById(ddlClientID+"_tooltip").style.visibility = "visible";
           document.getElementById(ddlClientID+"_tooltip").style.display = ""; 
            document.getElementById(ddlClientID+"_tooltip").style.top = e.y;
            document.getElementById(ddlClientID+"_tooltip").style.left = e.x;
        }
       }
       else //if(navigator.appName == 'Netscape')
       {  
          if(e.type == "mouseout")
        {
            document.getElementById(ddlClientID+"_tooltip").style.visibility = "hidden";
           document.getElementById(ddlClientID+"_tooltip").style.display = "none"; 
        }
        else if (e.type == "mouseover")
        {            
           document.getElementById(ddlClientID+"_tooltip").style.visibility = "visible";
           document.getElementById(ddlClientID+"_tooltip").style.display = ""; 
            document.getElementById(ddlClientID+"_tooltip").style.top = '350px';
            document.getElementById(ddlClientID+"_tooltip").style.left = '350px';
        }
       } 
    }
    else
   {
    document.getElementById(ddlClientID+"_tooltip").innerHTML ="";
   }
     
}
function CheckOne(stralert,varform)
{
	var j=0;
	for ( var i =0; i<varform.elements.length;i++)
	{
		var e = varform.elements[i];
		if(e.type=="radio"&&e.checked==true)
		{
			j++
		}
	}
	if(j == 0)
	{
		alert(stralert);	
		return false; 
	}
	else
	{
	    return true;
	}
}

function GetDecimal(DecimalValue,DecimalBit)
{
    var tempDate=parseFloat(DecimalValue);
    var Result=0;
    var Bit="1";
    for(i=0;i<DecimalBit;i++)
    {
        Bit+="0";
        tempDate = tempDate*10;
    }    
    var intBit = parseInt(Bit);  
    tempDate = Math.round(tempDate);    
    tempDate=tempDate/intBit;
    return tempDate;    
}

function GetDecimalAdv(DecimalValue,DecimalBit)
{
    var tempDate=parseFloat(DecimalValue);
    var Result=0;
    var Bit="1";
    for(i=0;i<DecimalBit;i++)
    {
        Bit+="0";
        tempDate = tempDate*10;
    }    
    var intBit = parseInt(Bit);  
    tempDate = Math.round(tempDate);    
    tempDate=tempDate/intBit;
    
    var tempStr = tempDate.toString().split('.');
    
    if (tempStr.length==1)
    {
        tempDate=tempDate.toFixed(DecimalBit);
    }
    else
    {
        var strRepl=tempStr[1];
        
        for(i=0;i<DecimalBit-tempStr[1].toString().length;i++)
        {
            strRepl=strRepl+'0';
        }
        
        tempDate=tempStr[0].toString()+"."+strRepl.toString();
    }
    return tempDate;    
}

function GetDecimalAdv(DecimalValue,DecimalBit,DefaultValue)
{
   var tempDate=parseFloat(DecimalValue);
   if(isNaN(tempDate))
   {
      tempDate = DefaultValue;
      return tempDate;
   } 
    var Result=0;
    var Bit="1";
    for(i=0;i<DecimalBit;i++)
    {
        Bit+="0";
        tempDate = tempDate*10;
    }    
    var intBit = parseInt(Bit);  
    tempDate = Math.round(tempDate);    
    tempDate=tempDate/intBit;
    
    var tempStr = tempDate.toString().split('.');
    
    if (tempStr.length==1)
    {
        tempDate=tempDate.toFixed(DecimalBit);
    }
    else
    {
        var strRepl=tempStr[1];
        
        for(i=0;i<DecimalBit-tempStr[1].toString().length;i++)
        {
            strRepl=strRepl+'0';
        }
        
        tempDate=tempStr[0].toString()+"."+strRepl.toString();
    }
    return tempDate;    
}

function formatNum(theObj)
{ 
    var digit = theObj.indexOf("."); // 取得小数点的位置 
    var int = theObj.substr(0,digit); // 取得小数中的整数部分 
    var i; 
    var mag = new Array(); 
    var word; 
    if (theObj.indexOf(".") == -1) 
    { 
        // 整数时 
        i = theObj.length; // 整数的个数 
        while(i > 0) 
        { 
            word = theObj.substring(i,i-3); // 每隔3位截取一组数字 
            i-= 3; 
            mag.unshift(word); // 分别将截取的数字压入数组 
        } 
        theObj = mag; 
    } 
    else
    { 
        // 小数时 
        i = int.length; // 除小数外，整数部分的个数 
        while(i > 0) 
        { 
            word = int.substring(i,i-3); // 每隔3位截取一组数字 
            i-= 3; 
            mag.unshift(word); 
        } 
        theObj = mag + theObj.substring(digit); 
    }
    return theObj;
} 

function AutoBreakWord(str,len)
{
    var Result="";
    var m=1;
    for(i=0;i<str.toString().length;i++)
    {
        if(m%len==0)
            Result=Result+str.substring(i,i+1)+"<br>";
        else
            Result=Result+str.substring(i,i+1);
    	 m++;           
     } 
    if (Result.substring(Result.toString().length-4,Result.toString().length)=="<br>")
    {
    	Result = Result.substring(0,Result.toString().length-4)
    }
   
    return Result;
}
function IsValidNumber(str)
{
	var IsValid = true;
	for(i = 0; i < str.length; i ++)
	{
		if(str.charCodeAt(i) > 57 || str.charCodeAt(i) < 48)
		{
			IsValid = false;
			break;
		}
	}
	return IsValid;
}
function TrimContrls(str)
{
    str.value = str.value.replace(/\ /g, '');
    return str.value;
}

//
function keyvalue(evt) 
{
    evt = evt ? evt : (window.event ? window.event : null);
    var code = 0;
    code = evt.keyCode||evt.which;
    var source = evt.srcElement||evt.target;
    
    if(code=="13") 
    {
        var btngoforgridpager = "_buttongoforgridpager";
        var btna = document.getElementsByTagName("input");
        if(btna.length > 0)
        {
            for(i=0; i < btna.length; i ++)
            {
                if(btna[i].type == "submit" && btna[i].id.indexOf(btngoforgridpager) != -1)
                {
                    btna[i].click();
                    return false;
                }
            }
        }
    }        
}

//获取页面高度
function getPageHeight()
{

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) 
    {
        yScroll = window.innerHeight + window.scrollMaxY;
    } 
    else if (document.body.scrollHeight > document.body.offsetHeight)
    { // all but Explorer Mac
        yScroll = document.body.scrollHeight;
    } 
    else 
    { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) 
    { // all except Explorer
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) 
    { // Explorer 6 Strict Mode
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) 
    { // other Explorers
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight)
    {
        pageHeight = windowHeight;
    } 
    else 
    {
        pageHeight = yScroll;
    }
    return pageHeight;
}
//Div和IFrame模拟模态窗口
var maxHeight=0,maxWidth=0;

//打开封住页面的Div
function openBlackDiv()
{
    var divBlack = document.getElementById('divBlack');
    if(divBlack)
    {
        var offHeight =  getPageHeight();
//        alert("scroll:" + document.body.scrollHeight + "  offh:" + offHeight);
//        maxHeight = document.body.scrollHeight > offHeight ? document.body.scrollHeight :offHeight ;
//        maxWidth  = window.screen.availWidth;
        maxHeight = window.screen.availHeight > offHeight ? window.screen.availHeight :offHeight ;
        maxWidth = window.screen.availWidth;
        divBlack.style.height = maxHeight + 'px';
        divBlack.style.width = maxWidth + 'px';
        divBlack.style.display = 'block';
    }
    return divBlack;
}

// 打开div窗口
function openModalDialog(url,height,width,title)
{
   //设置Url随机参数
   if (url.indexOf('?') > 0)
        url = url  + "&rdm=" + Math.random();
    else
        url = url  + "?rdm=" + Math.random();
    
    if(title && title != '')
        document.getElementById('winTitle').innerText = title;
        
    //打开封住页面的Div
    openBlackDiv();    
    var divOpenWin = document.getElementById('divOpenWin');
    var frame = document.getElementById('frame');    
    
    if(divOpenWin && frame)
    {
        divOpenWin.style.display = 'block';
        
        //设置显示高度
	   
//        if (top > 100)
//		    top = top - 100;
//		    
      
        var position = GetPopUpPosition(width,height);
        
        divOpenWin.style.posTop = position[0];
        divOpenWin.style.posLeft = position[1];
        divOpenWin.style.posWidth = width;
        divOpenWin.style.posHeight = height;
        
        frame.style.posWidth = width - 4;
        frame.style.posHeight = height - 22;
//        
//        if(loadingPage && loadingPage == '')
//            loadingPage = frame.src;
        
        //加载开启的页面
        frame.src = url;
    }
}
//获取弹出对象的位置
function GetPopUpPosition(width,height)
{
     var left = Get_Center(width,'x')  + document.documentElement.scrollLeft;	
	 var top = Get_Center(height,'y') + document.documentElement.scrollTop - 100;
	 return new Array(top,left);
}

//关闭窗口
function closeModalDialog()
{
    document.getElementById('divBlack').style.display = document.getElementById('divOpenWin').style.display = "none";
        if(loadingPage && loadingPage != '')
    document.getElementById('frame').src = loadingPage;
}
//关闭窗口
function closeWin()
{ 
   if(window.opener)
      window.close();
   else
   {
      if(window.parent)
      {
          window.parent.document.getElementById('divBlack').style.display = window.parent.document.getElementById('divOpenWin').style.display = "none";
          if(loadingPage)
            window.parent.document.getElementById('frame').src = loadingPage? loadingPage:"";
      }
      else
         window.close();
   }  
}

//移动窗口
function moveStart(obj)
{
//    obj.setCapture();
//    var ox = event.clientX;
//    var oy = event.clientY;
//    function obj.onmousemove()
//    {
//          if(obj)
//          {
//              //设置左边位置
//              var nx = event.clientX;
//              var changeX = nx - ox;
//              if((obj.style.posLeft > 0 && changeX < 0) || (((obj.style.posLeft + obj.clientWidth) < maxWidth) && changeX > 0))
//              {
//                  var left = obj.style.posLeft + changeX;
//                  if(left < 0)
//                    obj.style.posLeft = 0;
//                  else
//                  {
//                    if((obj.style.posLeft + obj.clientWidth + changeX) > maxWidth)
//                         obj.style.posLeft = maxWidth - obj.clientWidth;
//                    else
//                        obj.style.posLeft = left;
//                  }
//                   ox = nx;
//              }
//              //设置顶部位置
//              var ny = event.clientY;
//              var changeY = ny - oy;
//              if((obj.style.posTop > 0 && changeY < 0) || (((obj.style.posTop + obj.clientHeight) < maxHeight) && changeY > 0))
//              {
//                  var top = obj.style.posTop + changeY;
//                  if(top < 0)
//                    obj.style.posTop = 0;
//                  else
//                  {
//                    if((obj.style.posTop + obj.clientHeight + changeY) > maxHeight)
//                         obj.style.posTop = maxHeight - obj.clientHeight;
//                    else
//                        obj.style.posTop = top;
//                  }
//                  oy = ny;
//              }
//           }
//    }
//     function obj.onmouseup()
//     {
//          obj.releaseCapture();
//          obj = false;
//      }
}
//设置图片大小（保持高宽比）
function SetImgSize(img,maxWidth,maxHeight)
{
    var tempImg = new Image();
    tempImg.src = img.src;
    var width = tempImg.width;
    var height = tempImg.height;
    var scale = width / height;
    if(width > 0 && height > 0)
    {
        if(width > height)
        {
            img.style.posWidth = width > maxWidth ? maxWidth :width;
            img.style.posHeight = img.style.posWidth / scale;
        }
        else
        {
            img.style.posHeight = height > maxHeight ? maxHeight :height;
            img.style.posWidth = img.style.posHeight * scale;
        }
    }
    else
    {
        img.style.posWidth = maxWidth;
        img.style.posHeight = maxHeight;
    }
    var arr = new Array(2);
    arr[0] = width;
    arr[1] = height;
    return arr;
}
//////////////////////////////////////////////////////////////////
///检查上传控件的后缀名是否在许可的type中
///2007-12-24_15:48:39_by_Jie
///inputId:上传控件id
///type:允许的反缀(如"jpg,gif,png")
//////////////////////////////////////////////////////////////////
function checkExt(inputId, type)
{
    
    var obj = document.getElementById(inputId);

    var fileOutHTML = obj ? obj.outerHTML : ""; 
    
    if (obj == null) 
        return false;
    var tp = type.toUpperCase();
        
    if(obj.value.indexOf(".") == -1)
    {
    
        alert("Only filename with suffix are allowed.");
        obj.FileName= null;
        obj.FileContent = null;
        obj.outerHTML = fileOutHTML;
        obj.value=""; 
        return false;
    }
    
    if (type == "*") 
        return true;
        
    var filepath = obj.value;
    var re = /(\\+)/g;
    var filename = filepath.replace(re, "#");
    var one = filename.split("#");
    var two = one[one.length - 1];
    var three = two.split(".");
    var last = three[three.length - 1].toUpperCase();
    
    var rs = tp.indexOf(last);
    if (rs >= 0) 
    {
        return true;
    }
    else 
    {
        obj.FileName= null;
        obj.FileContent = null;
        obj.outerHTML = fileOutHTML;
        obj.value=""; 
        alert("Allowed file type: " + tp);
        return false;
    }                
}