﻿// JScript 文件

function FireControlEvent(controlObj,eventName)
{
    if(document.all)   
    {
        if(eventName=="click")
        {
            controlObj.click();
        }
        else
        {
            controlObj.fireEvent("on"+eventName);
        }
    }
    else
    {
        var evt=document.createEvent("MouseEvent");   
        evt.initEvent(eventName,true,true);   
        controlObj.dispatchEvent(evt);
    }
}
function addControlEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
//===========================================================================================
/// <summary>
/// 显示遮罩层时隐藏掉不能被遮住的html控件
/// </summary>
function HideElement(elmID)
{
	if(document.all) 
	{
		for(i = 0; i < document.all.tags( elmID ).length; i++) 
		{
			obj = document.all.tags( elmID )[i];
			if(!obj||obj.style.display=="none"||obj.className.toLowerCase()) continue;
			if(obj.parentNode.firstChild.tagName!=null&&obj.parentNode.firstChild.tagName.toLowerCase()=="span")
			{
			    obj.parentNode.firstChild.innerText=obj.options[obj.selectedIndex].text;
			}
			obj.style.visibility  = "hidden";
		}
	}
}
//===========================================================================================

//===========================================================================================
/// <summary>
/// 隐藏遮罩层时显示隐藏的不能被遮住的html控件
/// </summary>
function ShowElement(elmID) 
{
	if(document.all) 
	{
		for(i = 0; i < document.all.tags( elmID ).length; i++)
		 {
			obj = document.all.tags(elmID)[i];
			if(!obj) continue;
			if(obj.parentNode.firstChild.tagName!=null&&obj.parentNode.firstChild.tagName.toLowerCase()=="span")
			{
			    obj.parentNode.firstChild.innerText="";
			}
			obj.style.visibility = "";
		}
	}
}
//===========================================================================================
//===========================================================================================
/// <summary>
/// 设置Repeater中一组RadioButton单选
/// </summary>
function SetUniqueRadioButton(nameregex, current)
{
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i]
      if (elm.type == 'radio')
      {
         if (re.test(elm.name))
         {
            elm.checked = false;
         }
      }
   }
   current.checked = true;
}
//===========================================================================================

function PreviewUploadImage(uploader,container)
{    
//    alert("uploader is :" + uploader);
//    alert("uploader.value is :" + uploader.value);
//    alert("container is :" + container);
//    alert("container.src is :" + container.src);
    container.src = uploader.value;
    SetUploadImage(container);
}
function SetUploadImage(container)
{   
    container.style.display="";
    var picWidth=120;
    var picHeight=70;
    var imgWidth = container.width;
    var imgHeight = container.height;
    var percentageWidth =imgWidth/picWidth;
    var percentageHeight =imgHeight/picHeight;
    if(percentageWidth>percentageHeight)
    {
        container.width=picWidth;
    }
    else
    {
        container.height=picHeight;
    }    
}
function OpenPopWin(width, height,winObj,maskObj) {
    winObj.style.display="";
    maskObj.style.display="";
    CenterPopWin(width, height,winObj,maskObj);
    HideElement("SELECT");
}
function ClosePopWin(winObj,maskObj) {
    winObj.style.display="none";
    maskObj.style.display="none";
    ShowElement("SELECT");
}
function CenterPopWin(width, height,winObj,maskObj) {
		if (width == null || isNaN(width)) {
			width = winObj.offsetWidth;
		}
		if (height == null) {
			height = winObj.offsetHeight;
		}
		
		//var theBody = document.documentElement;
		var theBody = document.getElementsByTagName("BODY")[0];
		theBody.style.overflow = "hidden";
		
		var scTop = parseInt(theBody.scrollTop,10);
		var scLeft = parseInt(theBody.scrollLeft,10);
		
		maskObj.style.top = scTop + "px";
		maskObj.style.left = scLeft + "px";
		
	
		SetMaskSize(maskObj);
		

		var fullHeight = GetViewportHeight();
		var fullWidth = GetViewportWidth();
		
		winObj.style.top = (scTop + ((fullHeight - (height)) / 2)) + "px";
		winObj.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
		
		//alert(fullWidth + " " + width + " " + gPopupContainer.style.left);

}
function SetMaskSize(maskObj) {
	var theBody = document.getElementsByTagName("BODY")[0];
			
	var fullHeight = GetViewportHeight();
	var fullWidth = GetViewportWidth();
	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}
	
	if (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}
	
	maskObj.style.height = popHeight + "px";
	maskObj.style.width = popWidth + "px";
	
}
/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function GetViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 
	return window.undefined; 
}
function GetViewportWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
	return window.undefined; 
}

 /// <summary>
/// 限制文本输入框的文字长度
/// </summary>
/// <param name="obj">文本输入框的对象实例</param>
/// <param name="count">限制的字符长度</param>
function LimitTextBoxLength(obj,count)
{
    if(obj.createTextRange){ //for ie
        obj.focus();
        var slct = document.selection;
        var rng = slct.createRange();//obj.createTextRange();
        obj.select();
        rng.setEndPoint("StartToStart", slct.createRange());
        var cursor = rng.text.length;
        rng.collapse(false);
        rng.select();

        if(obj.value.length>count)
        {
            var overLength = (obj.value.length - count);
            var strart = cursor-overLength;
            var result = obj.value.substring(0, strart);
            result += obj.value.substring(cursor, obj.value.length);
            obj.value = result;
        }
    }else{ //for mozilla
        var iStart = obj.selectionStart;
        var iEnd = obj.selectionEnd;
        if(obj.value.length>count)
        {
            iStart-=obj.value.length-count;
        }
        obj.value=obj.value.substring(0,iStart)+obj.value.substring(iEnd,obj.value.length)
        endRange=obj.value.length;
        obj.setSelectionRange(endRange,endRange)
    }
}

function LimitInput(obj,dataType)
{
    switch(dataType.toLowerCase())
    {
        case "int":
            validValue=obj.value.replace(/[^\d]/g,'');
            break;
    }
    if(obj.value!=validValue)
    {
        obj.value=validValue;
    }
}