﻿function removeChar(formItem, charValue)
{
	var regex = new RegExp(charValue);
	var s = new String(formItem.value);
	formItem.value = s.replace(regex, '');
}    

var timerId = 0;

function createHTTPObject() 
{
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try 
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
	catch (e) 
	{
		try 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		catch (E)
			{
				xmlhttp = false;
			}
	}
	@else
	xmlhttp = false;
	@end @*/

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try 
			{
				xmlhttp = new XMLHttpRequest();
			} 
		catch (e) 
		{
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

var http = createHTTPObject();	
    
function OnKeyPress(elem)
{
    if (timerId != 0)
    {
        clearTimeout(timerId);
        timerId = 0;
    }
}

function trim(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


function createDivDialog(dialogTitle, dialogDivID, dialogWrapperDivID, dialogWidth, dialogTop, iframeheight, basePath)
{
    var dWrap = document.getElementById(dialogWrapperDivID);
    var hF = document.getElementById(dialogWrapperDivID + 'Frame');
    var isIE = navigator.appName.indexOf("Internet Explorer");

    if(dWrap.style.visibility == 'visible'){
        dWrap.style.visibility = 'hidden';
        dWrap.style.display = 'none';    
        hF.style.visibility = 'hidden';
        hF.style.display = 'none';          
    }
    var dialogContent = document.getElementById(dialogDivID).innerHTML;
    var a = '<div class="dialogBox" style="width:' + dialogWidth + 'px;"><table border="0" cellspacing="0" width="' + dialogWidth + '" cellpadding="2"><tr class="dialogTitle" style="background: url(' + basePath + 'images/dialogbox_titlebackground.gif) repeat-x;" onmousedown=myMouseDown("'+dialogWrapperDivID+'")>';
    a += '<td><div style="padding:2px;padding-left:5px;" >' + dialogTitle + '</div></td><td align="right"><a href="javascript:closeDivDialog(\''+ dialogWrapperDivID +'\');"><img src="' + basePath + 'images/icon_red_x.gif" border="0" hspace="6"></a></td>';
    a += '</tr><tr><td colspan="2"><div style="padding:5px;z-index:200;">' + dialogContent;
    a += '</div></td></tr></table></div>';

    if(isIE != -1) dialogWidth -= 6;

    if(iframeheight) 
    {
        a += '<div id="' + dialogWrapperDivID + 'shadow" class="dialogBoxShadow" style="width:' + dialogWidth + 'px;height:' + (parseInt(document.getElementById("sIframe").height) + 47) + 'px;"><table border="0" cellspacing="0" width="' + dialogWidth + '" cellpadding="2"><tr class="dialogTitleShadow">';        
        dialogContent = '<img id="shadowPadImg" src="/images/pixel.gif" height="' + document.getElementById("sIframe").height + '" width="' + dialogWidth + '" />';
    }
    else
    {
        a += '<div id="' + dialogWrapperDivID + 'shadow" class="dialogBoxShadow" style="width:' + dialogWidth + 'px;"><table border="0" cellspacing="0" width="' + dialogWidth + '" cellpadding="2"><tr class="dialogTitleShadow">';
    }
    a += '<td><div style="padding:2px;padding-left:5px;">' + dialogTitle + '</div></td><td align="right"><div style="padding:2px;"></div></td>';
    a += '</tr><tr><td colspan="2"><div style="padding:5px;">' + dialogContent;
    a += '</div></td></tr></table></div>';
    dWrap.innerHTML = a;

    dWrap.style.left = document.getElementById('mainCanvas').offsetLeft + (document.getElementById('mainCanvas').offsetWidth / 2) - (dialogWidth / 2) + 'px';

    if(isIE != -1)
    {
        dWrap.style.top = document.documentElement.scrollTop + dialogTop + 'px';
        document.getElementById(dialogWrapperDivID + 'shadow').style.top = 1;
        document.getElementById(dialogWrapperDivID + 'shadow').style.left = 1;
    }
    else
    {
        dWrap.style.top = window.pageYOffset + dialogTop + 'px';    
    }
    dWrap.style.display = 'block';
    if(isIE != -1)
    {
        if(dialogDivID == 'sliderDialog') 
        {
            hF.style.height = 50;
            hF.style.width = dialogWidth + 13;
        }
        else
        {
            hF.style.height = dWrap.offsetHeight - 11;
            hF.style.width = dialogWidth + 8;
        }
        hF.style.left = dWrap.offsetLeft;
        hF.style.top = dWrap.offsetTop;
        hF.style.visibility = 'visible';
        hF.style.display = 'block';
    }
    dWrap.style.visibility = 'visible';
}

function closeDivDialog(dialogWrapperDivID)
{
    var hF = document.getElementById(dialogWrapperDivID + 'Frame');
    hF.style.visibility = 'hidden';
    hF.style.display = 'none'; 
    var dWrap = document.getElementById(dialogWrapperDivID);
    dWrap.style.visibility = 'hidden';
    dWrap.style.display = 'none';
    dWrap.innerHTML = "";
}

function closeDivDialogParent(dialogWrapperDivID)
{
    var hF = window.parent.document.getElementById(dialogWrapperDivID + 'Frame');
    hF.style.visibility = 'hidden';
    hF.style.display = 'none'; 
    window.parent.document.getElementById(dialogWrapperDivID).style.visibility = 'hidden';
    window.parent.document.getElementById(dialogWrapperDivID).style.display = 'none';
}


function convertToLocalTime(str)
{
    str = str.replace(/Z/," GMT");
    str = str.replace(/-/,"/");
    str = str.replace(/-/,"/");
	
    d = new Date();
    d.setTime(Date.parse(str));
    document.write(d.toLocaleString());
}

function openTour(URL,title)
{
    var leftPos = document.getElementById('mainCanvas').offsetLeft + (document.getElementById('mainCanvas').offsetWidth / 2) - (720 / 2) + 'px';
    tourWindow = window.open(URL, "tinyWindow", 'left=' + leftPos + 'px,width=720px,height=515px,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0') 
}

    function getCookieVal(offset)
    {
        var endstr = document.cookie.indexOf(";", offset);
        if(endstr == -1)
        {
            endstr = document.cookie.length;
        }

        return unescape(document.cookie.substring(offset, endstr));
    }
    function getCookie(name)
    {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        //alert(document.cookie);
        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;
    }

/*
function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}
	else
	{
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	{
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}
*/
function changeElementHeight(id, height)
{
    var element = document.getElementById(id);
    if (element)
    {
	    element.style.height = height;
    }
}

var Obj=null;    
function myMouseDown(obj)    
{
    Obj=document.getElementById(obj);       
    Obj.setCapture();
    Obj.l=event.x-Obj.style.pixelLeft;
    Obj.t=event.y-Obj.style.pixelTop;
}

function myMouseMove()
{
    if(Obj!=null && event.y-Obj.t > 0 && event.x-Obj.l > 0)
    {
        Obj.style.pixelLeft=event.x-Obj.l;
        Obj.style.pixelTop=event.y-Obj.t;
        var hF = document.getElementById(Obj.id + 'Frame');  
        hF.style.left = Obj.style.pixelLeft;
        hF.style.top = Obj.style.pixelTop;
    }
}

function myMouseUp()
{
    if(Obj!=null)
    {           
        Obj.releaseCapture();
        Obj=null;
    }
}

function resizeThumbnailImage(imageid, dwidth, dheight)
{
    var vImage = document.getElementById(imageid);
	var iWidth = vImage.width;
	var iHeight = vImage.height;
	if(iWidth > dwidth || iHeight > dheight)
	{
		if(iWidth/dwidth > iHeight/dheight)
		{
			iHeight = iHeight * (dwidth/iWidth);
			iWidth = dwidth;
		}
		else
		{
			iWidth = iWidth * (dheight/iHeight);
			iHeight = dheight;
		}
		vImage.width = iWidth>1?iWidth:1;
		vImage.height = iHeight>1?iHeight:1;
	}
}

function writeTimezoneOffset()
{
    if(getCookie("timezoneOffset") == null)
    {
        var d= new Date();
        document.cookie="timezoneOffset=" + d.getTimezoneOffset();
    }
}



var calendar_DayNames = new Array("日","一","二","三","四","五","六");
var calendar_MonthNames = new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");

function SetCalendarDataByLangId()
{
   var langid=0;//getCookie("LanguagePref");
   //alert(langid + ":test")
   if (langid==0)
   {
         calendar_DayNames = new Array("日","一","二","三","四","五","六");
         calendar_MonthNames = new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
   }
   if (langid==1)
   {
         calendar_DayNames = new Array("Su","Mo","Tu","We","Th","Fr","Sa");
         calendar_MonthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
   }   
   if (langid==2)
   {
         calendar_DayNames = new Array("日","一","二","三","四","五","六");
         calendar_MonthNames = new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
   }   
}

function showCalendar(targetID, targetURL, dMonth, dYear, dDay)
{
    if(dMonth < 0)
    {       
        dMonth = 11;
        dYear--;
    }
    if(dMonth > 11)
    {
        dMonth = 0;
        dYear++;
    }
    SetCalendarDataByLangId();
    var dNow = new Date();
    if(dDay == 90 && dMonth == dNow.getMonth() && dYear == dNow.getYear()) dDay = dNow.getDate();
    var dCurFD = new Date(dYear, dMonth, 1);
    var dGivenDate = new Date(dYear, dMonth, dDay);  
    var dCurrentMonthFirstSunday = dCurFD.getDay();
    var dCurrentViewDate = new Date(dCurFD);
    var iDays = 0;
    var weekMilliseconds = 1000*60*60*24*7;
    if(dCurrentMonthFirstSunday > 0) iDays = -dCurrentMonthFirstSunday;
    
    var fTrgt = targetURL;
    while (fTrgt.indexOf("'") >= 0) fTrgt = fTrgt.replace("'", "`");
    while (fTrgt.indexOf("`") >= 0) fTrgt = fTrgt.replace("`", "\\'");
    
    var oBuf = '<TABLE BORDER="0" WIDTH="100%" CELLSPACING="0" CELLPADDING="0">';       
    oBuf += '<TR><TD CLASS="calendarhd"><A HREF="javascript:showCalendar(\'' + targetID + "','" + fTrgt + "'," + (dMonth - 1) + ',' + dYear + ',90);"><img src="Images/leftArrow.gif" /></A></TD>';
    oBuf += '<TD COLSPAN="5" CLASS="calendarhd">' + dYear + (calendar_MonthNames[dMonth]) + ' ' + '</TD>';
    oBuf += '<TD CLASS="calendarhd"><A HREF="javascript:showCalendar(\'' + targetID + "','" + fTrgt + "'," + (dMonth + 1) + ',' + dYear + ',90);"><img src="Images/rightArrow.gif" /></A></TD></TR>';
    oBuf += '<TR style="calendarhdrow">';
    
    for(var e=0; e < 7; e++) oBuf += '<TD CLASS="calendarDays">' + calendar_DayNames[e] + '</TD>';          
    oBuf += '</TR>';

    var nextMonthFirstDay = new Date(dYear, dMonth+1, 1);
    var IsNextMonthFirstDaySunday = nextMonthFirstDay.getDay()==0;
        
    for(var iRows=0; iRows < 6; iRows++)
    {
        dCurrentMonthDate = new Date(dCurFD.getFullYear(), dCurFD.getMonth(), dCurFD.getDate() + iDays);
        //if(dCurrentMonthDate.getMonth() <= dCurFD.getMonth()) // old style
        if(1==1)         
        {
        // skipping extra row at the end of the table if needed (new month) 
        if(iRows==5 && dCurrentMonthDate.getMonth() > dCurFD.getMonth()) continue;        
        if(iRows==5 && IsNextMonthFirstDaySunday) continue;
                
        oBuf += '<TR CLASS="calendarContent">';
        for(var iCols=0; iCols < 7; iCols++)
        {
            dCurrentViewDate = new Date(dCurFD.getFullYear(), dCurFD.getMonth(), dCurFD.getDate() + iDays);

            if((dGivenDate.toDateString() == dCurrentViewDate.toDateString()) && (dDay != 90))
            {
                oBuf += '<TD CLASS="selectedDate">';
            }
            else
            {
                if(dCurrentViewDate.getMonth() != dMonth || iDays < 0) 
                {
                    oBuf += '<TD CLASS="negDate">';
                }
                else
                {
                    oBuf += '<TD CLASS="posDate">';
                }
            }
            
            var args;
            
            if (targetURL.substr(0,11) == 'javascript:')
            {
              args = dCurrentViewDate.valueOf() + ")";
            }
            else
            {
              var sep = targetURL.indexOf('?') < 0 ? '?' : '&';
              args = sep + 'startDate=' + (dCurrentViewDate.getMonth()+1)+ '%2F' + dCurrentViewDate.getDate() + '%2F' + dCurrentViewDate.getFullYear();
            }
            oBuf += '<A HREF="' + targetURL +  args + '">' + dCurrentViewDate.getDate() + '</A></TD>';
            iDays++;
        }
        oBuf += '</TR>';
        }
    }
    oBuf += '</TABLE>';
    document.getElementById(targetID).innerHTML = oBuf;
}

function fnVoid()
{
    return false;
}

function checkAllCheckbox(parentObj, checked)
{
    if (parentObj == null)
        parentObj = document;
    var inputs = parentObj.getElementsByTagName("input");
    for(var i=0; i<inputs.length; i++)
    {
        var ipt = inputs[i];
        if (ipt.type.toLowerCase() == "checkbox")
        {
            ipt.checked = checked ? true : false;
        }
    }
}

function checkboxChecked(parentObj)
{
    if (parentObj == null)
        parentObj = document;
    var inputs = parentObj.getElementsByTagName("input");
    for(var i=0; i<inputs.length; i++)
    {
        var ipt = inputs[i];
        if (ipt.type.toLowerCase() == "checkbox")
        {
            if (ipt.checked)
                return true;
        }
    }
    return false;
}

function adjustFrameHeight(frmName, frmId)
{
	var frm = document.getElementById(frmId);
	var subWeb = document.frames ? document.frames[frmName].document : frm.contentDocument;
	if(frm != null && subWeb != null)
		frm.height = subWeb.body.scrollHeight;
}



function progressBar()
{
	var r = "传输({1}): {0} 还未完成";
    var s = "文件上传完成, 处理中...";
	var t = "上传失败";
	var i = "正在初始化...";
   var langid=0;//getCookie("LanguagePref");
   if (langid==1)
   {
	    r = "Transmission ({1}): {0} has not completed";
        s = "The document upload in processing… ";
	    t = "File Upload defeat ";
	    i = "Is initializing… ";   
    }   
   if (langid==2)
   {
	    r = "傳輸({1}): {0} 還未完成 ";
        s = "文件上傳完成, 處理中... ";
	    t = "上傳失敗 ";
	    i = "正在初始化... ";     
	}   

	
	this.oProgressInfo = document.getElementById("progressInfo");
	this.oProgress = document.getElementById("progress");
	
	this.setSize = function(percent, remainingLengthText)
	{
		var percentDone = percent + "%";
		var progressInfoText = r.replace("{0}", remainingLengthText);
		progressInfoText = progressInfoText.replace("{1}", percentDone);

		this.setProgress(progressInfoText, percentDone);
	}
	
	this.uploadError = function()
	{
		this.setProgress(t, "0");
	}
	
	this.uploadComplete = function()
	{
		this.setProgress(s, "100%");
	}
	
	this.init = function()
	{
		this.setProgress(i, "0");
	}
	
	this.setProgress = function(msg, percent)
	{
		if (this.oProgressInfo != null)
			this.oProgressInfo.innerHTML = msg;
		if (this.oProgress != null)
			this.oProgress.style.width = percent;
	}
}

function BuildUrl(url, qname, qvalue)
{
	var r;
	var t = qname + "=";
	if (url.indexOf(t) > -1)
	{
		var re = new RegExp("([?]|[&])" + t + "([^&]*)", "ig");//gi;
		r = url.replace(re, "$1" + t + qvalue);
	}
	else
	{
		var sep = "&";
		if (url.indexOf("?") == -1)
		{
			sep = "?";
		}
		r = url + sep + t + qvalue;
	}
	return (r);
}


if (window.addEventListener)
	window.addEventListener('load', ol_Init, true);
else
	window.attachEvent('onload', ol_Init);

function ol_Init()
{
	var maxWidth = 550;
	var maxHeight = 350;
	var imgs = document.getElementsByTagName("img");
	for(var i=0; i<imgs.length; i++)
	{
		var img = imgs[i];
		if (img.width > maxWidth)
		{
			img.width = maxWidth;
			if (img.parentElement.tagName != "A")
			{
				img.onclick = function(){window.open(this.src)}
				img.style.cursor = "pointer";
				img.style.cursor = "hand";
			}
		}
	}
}
