var isIE = navigator.userAgent.indexOf("MSIE") > 0;
function openWinModalWithWH(url, w, h)
{
    var result;
    result = window.showModalDialog(url, "", "dialogHeight:"+ h +"px; dialogWidth:"+ w +"px; center: 1; help:0; resizable:0; scroll:0; status:0; unadorned:1;")
    return result;
}

function openWinModalWithWHResizableScroll(url, w, h)
{
    var result;
    result = window.showModalDialog(url, "", "dialogHeight:"+ h +"px; dialogWidth:"+ w +"px; center: 1; help:0; resizable:1; scroll:1; status:0; unadorned:1;")
    return result;
}

function openWindowWithWH(url, w, h)
{
    var str = "height=" + h + ",innerHeight=" + h;
    str += ",width=" + w + ",innerWidth=" + w;
    if (window.screen)
    {
        var ah = screen.availHeight - 30;
        var aw = screen.availWidth - 10;

        var xc = (aw - w) / 2;
        var yc = (ah - h) / 2;

        str += ",left=" + xc + ",screenX=" + xc;
        str += ",top=" + yc + ",screenY=" + yc;
    }
    str += ",toolbar=0, titlebar=0, location=0, menubar=0, resizable=1, scrollbars=1"
    
    var oWin = window.open(url, "Elation"+ w + h, str)
}

function openWindowFullScreen(url, windowName)
{
    if(windowName == null)
   {
        windowName = "ElationFullScreen";
   } 
    var h = screen.availHeight - 60;
    var w = screen.availWidth - 60
    
    if (window.screen)
    {
        var ah = screen.availHeight - 30;
        var aw = screen.availWidth - 10;
    }
    var str  = "";
    str += "height=" + ah;
    str += ",width=" + aw;
    str += ",left=0, top=0, screenX=0, screenY=0";
    str += ",toolbar=no, titlebar=no, location=no, menubar=no, resizable=1, scrollbars=1, maximize=1"
    
    var oWin = window.open(url, windowName , str);
   if(oWin != null) 
   {
        oWin.resizeTo(screen.availWidth,screen.availHeight);
        return oWin; 
   }
}

function openWindowFullScreenWithMenubar(url, windowName)
{
    if(windowName == null)
   {
        windowName = "ElationFullScreen";
   } 
    var h = screen.availHeight - 60;
    var w = screen.availWidth - 60
    
    if (window.screen)
    {
        var ah = screen.availHeight - 30;
        var aw = screen.availWidth - 10;
    }
    var str  = "";
    str += "height=" + ah;
    str += ",width=" + aw;
    str += ",left=0, top=0, screenX=0, screenY=0";
    str += ",toolbar=0, titlebar=0, location=0, menubar=1, resizable=1, scrollbars=1, maximize=1"
    
    var oWin = window.open(url, windowName , str);
   if(oWin != null) 
   {
        oWin.resizeTo(screen.availWidth,screen.availHeight);
        return oWin; 
   }
}

function openWinModalFullScreen(url)
{
    var h = screen.availHeight - 60;
    var w = screen.availWidth - 60
    
    if (window.screen)
    {
        var ah = screen.availHeight - 30;
        var aw = screen.availWidth - 10;
    }
    var str = "";
    str += "dialogHeight:" + screen.availHeight;
    str += ";dialogWidth:" + screen.availWidth;
    str += ";left:0; top:0; screenX:0; screenY:0";
    str += ";toolbar:0; titlebar:0; location:0; menubar:0; status:0; resizable:1; scroll:1; maximize:1;"
    
    //result = window.showModalDialog(url, "", "dialogHeight:"+ h +"px; dialogWidth:"+ w +"px; center: 1; help:0; resizable:0; scroll:0; status:0; unadorned:1;")
    var result;
    result = window.showModalDialog(url, "ElationFullScreenModal", str);
    return result;
    //oWin.resizeTo(screen.availWidth,screen.availHeight);
}

function openCalendar(dateField, currentDateValue)
{
    openWindowWithWHWithoutScrollbars("../includes/Calendar.aspx?control="+ dateField +"&value="+ currentDateValue +"", 220, 220);
}

function openCalendarModal(dateField, currentDateValue)
{
    openWinModalWithWH("../includes/Calendar.aspx?control="+ dateField +"&value="+ currentDateValue +"", 220, 220);
}

function openWindowWithWHWithoutScrollbars(url, w, h)
{
    var str = "height=" + h + ",innerHeight=" + h;
    str += ",width=" + w + ",innerWidth=" + w;
    if (window.screen)
    {
        var ah = screen.availHeight - 30;
        var aw = screen.availWidth - 10;

        var xc = (aw - w) / 2;
        var yc = (ah - h) / 2;

        str += ",left=" + xc + ",screenX=" + xc;
        str += ",top=" + yc + ",screenY=" + yc;
    }
    str += ",toolbar=0, titlebar=0, location=0, menubar=0, resizable=0, scrollbars=0"
    
    var oWin = window.open(url, "Elation"+ w + h, str)
}

function resizeWindowWH(ww, hh)
{
    var h = screen.availHeight;
    var w = screen.availWidth;
    window.resizeTo(ww, hh);
    window.moveTo((w - ww)/2, (h - hh)/2);
}

imgMinimize = new Image();
imgMinimize.src = "../images/minimize.gif";

imgMaximize = new Image();
imgMaximize.src = "../images/maximize.gif";

function MaximizeOrMinimizeData(objToMaxOrMin, imgToChange)
{
    if(objToMaxOrMin.style.display == "none")
    {
        imgToChange.src = "../images/minimizeBlue3D.gif";
        imgToChange.alt = "Minimize Window";
        objToMaxOrMin.style.display = "inline";
        //setTimeout("MaximizeOrMinimizeData2(objToMaxOrMin,imgToChange)", 200);
        //imgToChange = imgMinimize;
    }
    else
    {
        imgToChange.src = "../images/maximizeBlue3D.gif";
        imgToChange.alt = "Maximize Window";
        objToMaxOrMin.style.display = "none";
        //setTimeout("MaximizeOrMinimizeData2(objToMaxOrMin,imgToChange)", 200);
        //imgToChange = imgMaximize;
    }
}

function MaximizeOrMinimizeData2(objToMaxOrMin, imgToChange)
{
    if(objToMaxOrMin.style.display == "none")
    {
        imgToChange.src = "../images/minimizeBlue3D.gif";
        imgToChange.alt = "Minimize Window";
        //imgToChange = imgMinimize;
    }
    else
    {
        imgToChange.src = "../images/maximizeBlue3D.gif";
        imgToChange.alt = "Maximize Window";
        //imgToChange = imgMaximize;
    }
}


function CheckUncheckAllSimilarCheckBoxes(obj)
{
    checkBoxName = obj.id;
    newStatus = obj.checked;
    for(i=0; i < document.forms.length; i++)
    {
        for(j=0; j < document.forms[i].elements.length; j++)
        {
            try
            {
                if(document.forms[i].elements[j].id.indexOf(checkBoxName) > 0)
                {
                    document.forms[i].elements[j].checked = newStatus;
                }
            }
            catch(e)
            {
                if(alertCounter++ < 10)
                    alert(e);
            }
        }
    }
}

function CheckUncheckAllSimilarCheckBoxes(obj, compareString)
{
    newStatus = obj.checked;
    for(i=0; i < document.forms.length; i++)
    {
        for(j=0; j < document.forms[i].elements.length; j++)
        {
            try
            {
                if(document.forms[i].elements[j].id.indexOf(compareString) > 0)
                {
                    document.forms[i].elements[j].checked = newStatus;
                }
            }
            catch(e)
            {
                if(alertCounter++ < 10)
                    alert(e);
            }
        }
    }
}


function ShowHideDefaultPageProjectsPane(objectProperty)
{
    try
    {
		var tdLeftPane = document.getElementById("tdLeftPane");
		var tdLeftPaneHref = document.getElementById("tdLeftPaneHref");
		var tdSpacer = document.getElementById("tdSpacer");
		
		tdLeftPane.style.width = "18%";//Firefox must be set width percentage
        if(objectProperty == "")
        {			
            if(tdLeftPane.style.display == "none")
            {
                tdLeftPane.style.display = "";				
                tdLeftPaneHref.style.display = "none";
                tdSpacer.style.display = "";
            }
            else
            {
                tdLeftPane.style.display = "none";
                tdLeftPaneHref.style.display = "";
                tdSpacer.style.display = "none";
            }
        }
        else
        {    
            if(objectProperty == "none")
            {
                tdLeftPane.style.display = "";				
                tdLeftPaneHref.style.display = "none";
                tdSpacer.style.display = "";
            }
            else
            {
                tdLeftPane.style.display = "none";
                tdLeftPaneHref.style.display = "";
                tdSpacer.style.display = "none";
            }
        }
    }
    catch(e)
    {}
}


//UCp Related
function hideShowLeftPanel()
{
	if (document.all.ifLeft.width == "" || document.all.ifLeft.width <= 20)
	{
		document.all.imgHideShow.src = "Images/hideleft.jpg"
		document.all.tdLeft.width = 200;
		document.all.ifLeft.width = 200;
		document.all.ifLeft.style.display = "";
	}
	else
	{
		document.all.imgHideShow.src = "Images/hideright.jpg"
		document.all.ifLeft.width = 20;
		document.all.tdLeft.width = 20;
		document.all.ifLeft.style.display = "none";
	}
	//IF IE
	if (document.all)
    {
        if (screen.availHeight <= 800)
        {
            //document.all.tdLeft.height = 500;
            //document.all.tdRight.height = 500;
            //document.all.ifLeft.height = 500;
            //document.all.ifRight.height = 500;
        }
        else
        {
            //Set the heights of TD & iFrame. Less 230 because TOP portion of the screen requires that much
            //document.all.tdLeft.height = screen.availHeight - 230;
            //document.all.tdRight.height = screen.availHeight - 230;
            //document.all.ifLeft.height = screen.availHeight - 230;
            //document.all.ifRight.height = screen.availHeight - 230;
        }
    }
}

function setPage(strLeftPage, strRightPage, applicationType, additionalParam1)
{
	if (strLeftPage != "")
	{
		window.parent.document.all.ifLeft.src = strLeftPage;
	}
	
	if (strRightPage != "")
	{
	
	    if (strRightPage.indexOf("AvailableCertifications.aspx") >= 0 && (additionalParam1 == "NewApp" || additionalParam1 == null))
	    {
	        window.parent.document.all.ifRight.src = "../Certification/CAAgencyCertificationApplication.aspx?CompanyID=" + applicationType + "&NewApp=1"; //applicationType wud be agency id
	    }
	    else if (strRightPage.indexOf("CAAgencyCertificationApplication.aspx") >= 0 && parseInt(additionalParam1, 10) > 0)
	    {
		    window.parent.document.all.ifRight.src = "SetSessionVariables.aspx?TargetUrl=" + strRightPage + "&UcpApplicationType=" + applicationType + "&LocalAppID=" + additionalParam1; //Application ID of Local Application
		}
		else if (strRightPage.indexOf("ApplicationCommunication.aspx") >= 0 && parseInt(additionalParam1, 10) > 0)
	    {
		    window.parent.document.all.ifRight.src = "SetSessionVariables.aspx?TargetUrl=" + strRightPage + "&UcpApplicationType=" + applicationType + "&LocalAppID=" + additionalParam1; //Application ID of Local Application
		}
	    else
	    {
		    window.parent.document.all.ifRight.src = "SetSessionVariables.aspx?TargetUrl=" + strRightPage + "&UcpApplicationType=" + applicationType;
		}
	}
}
var agencyApplicationFrameID = null;
var UCPApplicationType = 0;

function setAgencyPage(strLeftPage, strRightPage, applicationType, additionalParam1)
{
    if (UCPApplicationType != applicationType && applicationType)
    {
        UCPApplicationType = applicationType;
        window.location = "SetAgencySession.aspx?TargetUrl=" + strRightPage + "&UcpApplicationType=" + applicationType + "&ReloadAgencyView=1";
        return;
    }

	if (strLeftPage != "")
	{
		//window.parent.document.all.ifLeft.src = strLeftPage;
	}

	if (strRightPage != "")
	{
		var strNameControlID = "";
		for (var i=0; i < document.all.length; i++)
        {
            strNameControlID = document.all.item(i).id;
            try
            {
                if (strNameControlID.length > 0)
                {
                    if (strNameControlID.indexOf("WebPartManager1") >= 0  && 
                        strNameControlID.indexOf("ifApplication") > 0 && additionalParam1 == null)
                    {
                        document.all.item(i).src = "SetAgencySession.aspx?TargetUrl=" + strRightPage + "&UcpApplicationType=" + applicationType;
                    }
                    else if (strNameControlID.indexOf("WebPartManager1") >= 0  && 
                        strNameControlID.indexOf("ifApplication") > 0 && additionalParam1 != null)
                    {
                        document.all.item(i).src = "SetAgencySession.aspx?TargetUrl=" + strRightPage + "&UcpApplicationType=" + applicationType + additionalParam1; //Param has the querystring name
                    }
                }
            }
            catch (exp)
            {}
        }
	}
}

var clientWindowHeight = 0;
var clientWindowWidth = 0;
var clientScreenHeight = 0;
var clientScreenWidth = 0;
var clientBitDepth = 0;
var applicationWindowWidth = "100%";

function getScreenParams()
{
	//IF IE
	if (document.all)
	{
		clientWindowHeight = document.body.clientHeight;
		clientWindowWidth = document.body.clientWidth;
		clientScreenHeight = screen.height;
		clientScreenWidth = screen.width;
		clientBitDepth = screen.colorDepth;
	}
	else if (document.layers)
	{
		clientWindowHeight = window.innerHeight;
		clientWindowWidth = window.innerWidth;
		clientScreenHeight = screen.height;
		clientScreenWidth = screen.width;
		clientBitDepth = screen.colorDepth;
	}
	
	if (clientScreenWidth <= 1024)
        applicationWindowWidth = "100%";
    else 
        applicationWindowWidth = "100%";
}

function showHideInstructionLinks(option)
{
    var imageSource = "";
    for (var i=0; i < document.images.length; i++)
    {
        if (document.images.item(i).src != "")
        {
            imageSource = document.images.item(i).src;
            imageSource = imageSource.toUpperCase();
            if (imageSource.indexOf("INSTRUCTIONICON") >= 0)
            {
                if (option == "hide")
                    document.images.item(i).style.visibility = "hidden";
                else
                    document.images.item(i).style.visibility = "visible";
            }   
        }
    }
}

    
    function hideDiv(id)
    {
        document.getElementById(id).style.display='none';
    }


//add by Zhonghai----Get the control's position of x and y.
function fGetXY(aTag)
{    
    var oTmp = aTag;   
    var pt = new Point(0,0);   
    do 
    {   
        pt.x += oTmp.offsetLeft;  
        pt.y += oTmp.offsetTop;  
        oTmp = oTmp.offsetParent;
    } while(oTmp.tagName!='BODY');  
    return pt; 
} 

function Point(iX, iY)
{   
    this.x = iX; 
    this.y = iY; 
} 


 function setCookie(c_name,value,expiredays)
{   var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate)
}

//=========================================Set frame height================================================

//frame must be a name of iframe
function SetFrameLocation(frame,href)
{
    frame.location.href = href;
}

function SetParentFrameHeight()
{
    try
    {
        
    
        if(window.parent == null || parent.SetFrameHeight == null)
        {
            return;
        }
        parent.SetFrameHeight(document.body.offsetHeight + 20);
    }
    catch(e)
    {
    }
}

function AutoSetFrameHeight(id)
{
    var iframe= document.getElementById(id);
    
    if (iframe.contentDocument && iframe.contentDocument.body.offsetHeight)
    {
        iframe.height = iframe.contentDocument.body.offsetHeight + 30; 
    }
    else if(iframe.contentWindow && iframe.contentWindow.document.body.scrollHeight)
    {
        iframe.height = iframe.contentWindow.document.body.scrollHeight + 30; 
    }
    else if(iframe.document && iframe.document.body.scrollHeight)
    {
        iframe.height = iframe.document.body.scrollHeight + 30;  
    }   
}



function SetIframeHeightOnLoad(id)//for set iframe height in div window which process application
{
    try
    {
        var iframe=document.getElementById(id);
        if (iframe && !window.opera)
        {
            var height = 0;
            if (iframe.contentDocument && iframe.contentDocument.body.offsetHeight)
                height = iframe.contentDocument.body.offsetHeight; 
            else if(iframe.Document && iframe.Document.body.scrollHeight)
                height = iframe.Document.body.scrollHeight;
           var windowHeight = window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
          
           if (height < 400) height = 400;
           
           if(height < windowHeight - 100) {
               iframe.height = height + "px";   
           }
           else
           {
                if(windowHeight - 100 > 100)
                {
                    iframe.height = (windowHeight - 100)+"px";  
                }
           }
        }
    }
    catch(e)
    {
    }
}

function SetFrameHeightById(id,height)
{  
    try
    {
        var iframe=document.getElementById(id);
        if(height)
        {
            iframe.height = (parseInt(height) < 500) ? 500 : parseInt(height);
        }
        else
        {
            if (iframe && !window.opera)
            {
            
                if (iframe.contentDocument && iframe.contentDocument.body.offsetHeight)
                    iframe.height = iframe.contentDocument.body.offsetHeight + 20; 
                else if(iframe.Document && iframe.Document.body.scrollHeight)
                    iframe.height = iframe.Document.body.scrollHeight + 20;
             
                if(iframe.height < 500)
                    iframe.height = 500;
            }
        }
    }
    catch(e)
    {
    }
}
//=================================================end==================================================

//set the position of showing div, if positionY or PositionX is null,div will be set on certer of page.
function ShowDivOnPageCerter(id,positionY,positionX)
{
    var objDIV = document.getElementById(id);
    objDIV.style.display = '';
    //alert(objDIV.style.top + '  ' + document.body.offsetHeight + '  ' +objDIV.offsetHeight );
    if(document.body.offsetHeight < objDIV.offsetHeight)
    {
        objDIV.style.top = 0;
    }
    else
    {
        objDIV.style.top = (document.body.offsetHeight - objDIV.offsetHeight) / 2;
    }
    
    if(document.body.offsetWidth < objDIV.offsetWidth)
    {
        objDIV.style.left = 0;
    }
    else
    {
        objDIV.style.left = (document.body.offsetWidth - objDIV.offsetWidth) / 2;
    }
    
    if(positionX != null)
    {
        objDIV.style.left = positionX;
    }
    if(positionY != null)
    {
        objDIV.style.top = positionY;
    }
    
    //alert(objDIV.style.top);
}

//Show div at it's default position, only positionY and PositionX equal 0, the position will be set center.
function ShowDivOnDefaultPosition(id,positionY,positionX)
{
    var objDIV = document.getElementById(id);
    objDIV.style.display = '';
    if(document.body.offsetHeight < objDIV.offsetHeight)
    {
        objDIV.style.top = 0;
    }
    else
    {
        if(positionY == 0)
            objDIV.style.top = (document.body.offsetHeight - objDIV.offsetHeight) / 2;
    }
    
    if(document.body.offsetWidth < objDIV.offsetWidth)
    {
        objDIV.style.left = 0;
    }
    else
    {
        if(positionX == 0)
            objDIV.style.left = (document.body.offsetWidth - objDIV.offsetWidth) / 2;
    }
    
    if(positionX != null)
    {
        objDIV.style.left = positionX;
    }
    if(positionY != null)
    {
        objDIV.style.top = positionY;
    }
}

//show div follow the position of mouse clicking
//control: the object you clicked
//id:  the id of div whom you want to show
function ShowDivWithMouse(control,divID)
{
    if(control == null)
    {
        control = event.srcElement;
    }

    var divData = document.getElementById(divID);
    divData.style.display = '';
    
    var x = fGetXY(control).x; 
    var y = fGetXY(control).y; 
    
    var rightedge;
    if(event != null)
    {
        rightedge = document.body.clientWidth - event.clientX;
    }
    else
    {
        rightedge = document.body.clientWidth - x - control.offsetWidth;
    }
    var bottomedge = document.body.clientHeight - y;
    
    if (rightedge < divData.offsetWidth)
    {
        divData.style.left = x - divData.offsetWidth + control.offsetWidth; 
    }
    else
    {
        divData.style.left = x ; 
    }
    
    if(bottomedge < divData.offsetHeight)
    {
        divData.style.top = y - divData.offsetHeight; 
    }
    else
    {
        divData.style.top = y + control.offsetHeight; 
    }
    
    if(divData.style.top.toString().replace('px','')-0 < 0)
    {
        divData.style.top = 0;
    }
}

//id: div's id
//display the div on page's center,like the position of window's
function ShowDivOnScreenCenter(id)
{
    var divData = document.getElementById(id);

    var topWindow = window;
    var tmpWindow = window;
    
    while(tmpWindow.top != tmpWindow.self)
    {
        
        tmpWindow = tmpWindow.parent.window;
        topWindow = tmpWindow;
    }
    
    //(document.body.offsetWidth - objDIV.offsetWidth) / 2;
 
    
    var pageX = GetScrollLeft(window);
    var pageY = GetScrollTop(topWindow);
    var pageW = window.document.body.offsetWidth-40;   
    var pageH = topWindow.document.body.offsetHeight-20; 
    
    var windowWidth, windowHeight; 
    if (topWindow.innerHeight) {  // all except Explorer 
        windowWidth = topWindow.innerWidth; 
        windowHeight = topWindow.innerHeight; 
    } else if (topWindow.document.documentElement && topWindow.document.documentElement.clientHeight) { // Explorer 6 Strict Mode 
        windowWidth = topWindow.document.documentElement.clientWidth; 
        windowHeight = topWindow.document.documentElement.clientHeight; 
    } else if (topWindow.document.body) { // other Explorers 
        windowWidth = topWindow.document.body.clientWidth; 
        windowHeight = topWindow.document.body.clientHeight; 
    }   
  

    divData.style.display = '';
    
    if(document.body.offsetHeight < 450)
    {
        if(document.body.offsetHeight < divData.offsetHeight)
        { 
            divData.style.top = "0px"; 
        } 
        else 
        { 
            divData.style.top = ((document.body.offsetHeight-divData.offsetHeight)/2) + "px" ; 
        } 
    }
    else
    {
        divData.style.top = ((windowHeight - divData.offsetHeight) / 2 + pageY - 85) + "px";
    }
    
    
    
    divData.style.left = ((pageX + pageW - divData.offsetWidth) / 2) + "px"; 
    
    
    //alert(divData.style.top + '  ' +  divData.offsetHeight + '  ' + window.document.body.offsetHeight);
    
    //if bottom is not visible, set div display on bottom
//    if(parseInt(divData.style.top.toString().replace('px','')) < document.body.offsetHeight - divData.offsetHeight)
//    {
//        divData.style.top = document.body.offsetHeight - divData.offsetHeight;
//    }
    
    //if top is not visible, set div display on top
    if(parseInt(divData.style.top.toString().replace('px','')) < 0)
    {
        divData.style.top = "0px";
    }
    if(parseInt(divData.style.left.toString().replace('px','')) < 0)
    {
        divData.style.left = "0px";
    }
    
    
    
    
}

function fGetXY(aTag){    
    var oTmp = aTag;   
    var pt = new Point(0,0);   
    do {   
        pt.x += oTmp.offsetLeft;   
        pt.y += oTmp.offsetTop;  
        oTmp = oTmp.offsetParent;  
    } while(oTmp.tagName!='BODY');
    return pt;  
}

function Point(iX, iY){  
    this.x = iX; 
        this.y = iY; 
}

function getAbsolutePos(el)
{
    var SL = 0, ST = 0;
    var is_div = /^div$/i.test(el.tagName);
    if (is_div && el.scrollLeft)
        SL = el.scrollLeft;
    if (is_div && el.scrollTop)
        ST = el.scrollTop;
    var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
    if (el.offsetParent)
    {
        var tmp = this.getAbsolutePos(el.offsetParent);
        r.x += tmp.x;
        r.y += tmp.y;
    }
    return r;
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

String.prototype.replaceAll = function(
strTarget, // The substring you want to replace
strSubString // The string you want to replace in.
) {
    var remainText = this;
    var returnText = '';
    var intIndexOfMatch = remainText.indexOf(strTarget);

    // Keep looping while an instance of the target string still exists in the string.
    while (intIndexOfMatch != -1) {
        returnText += remainText.substring(0, intIndexOfMatch) + strSubString; 
        remainText = remainText.substring(intIndexOfMatch + strTarget.length); 
        // Get the index of any next matching substring.
        intIndexOfMatch = remainText.indexOf(strTarget);
    }

    returnText += remainText;
    return (returnText);
}

//get window's scroll position
function GetScrollLeft(tmpWindow) {return typeof tmpWindow.pageXOffset != 'undefined' ? tmpWindow.pageXOffset:tmpWindow.document.documentElement && tmpWindow.document.documentElement.scrollLeft? tmpWindow.document.documentElement.scrollLeft:tmpWindow.document.body.scrollLeft? tmpWindow.document.body.scrollLeft:0;}
function GetScrollTop(tmpWindow) {return typeof tmpWindow.pageYOffset != 'undefined' ? tmpWindow.pageYOffset:tmpWindow.document.documentElement && tmpWindow.document.documentElement.scrollTop? tmpWindow.document.documentElement.scrollTop: tmpWindow.document.body.scrollTop ? tmpWindow.document.body.scrollTop:0;}  



function InitDefaultPage()
{
    var mainSrc =document.getElementById('frameMain').src.toLowerCase();
    if(mainSrc.indexOf("loadingpage.aspx")!=-1)
    {
        var tdFirst = document.getElementById('tbMenu').rows[0].cells[0];
        ExecuteClickIEFF(tdFirst);
    }
    
}

function SetBodyHeight()
{
    var windowHeight = pageHeight() - 20;
    if(document.getElementById("tblHeader"))
        windowHeight = windowHeight - document.getElementById("tblHeader").scrollHeight;
    if(document.getElementById("tblFooter"))
        windowHeight = windowHeight - document.getElementById("tblFooter").scrollHeight;   
   
    document.getElementById("frameMain").style.height = windowHeight;
}

function ClickFirstTab(objCell, url)
{
    var firstTabRow = objCell.parentNode;
    for(var i = 0; i < firstTabRow.cells.length; i++)
    {
        firstTabRow.cells[i].className = 'tabFace';
    }
    
    objCell.className = 'tabFaceSelected';
    
    if(url == '')
    {
        url = '../LoadingPage.aspx';
    }
    
    document.getElementById('frameMain').src = url;
}

function getFrameHeight()
{
    var heightStr = document.getElementById("frameMain").style.height;
    heightStr = heightStr.substring(0, heightStr.length - 2);
    return heightStr;
}

function StringBuffer()
{
      this._strings_ = new Array;
}

StringBuffer.prototype.append = function(str)
{
      this._strings_.push(str);
}

StringBuffer.prototype.toString = function()
{
      return this._strings_.join("");
}


function ShowHideDefaultLeftPane(objectProperty)
{
    try
    {
		var tdLeftPane = document.getElementById("tdLeftPane");
		var tdLeftPaneHref = document.getElementById("tdLeftPaneHref");
		var tdSpacer = document.getElementById("tdSpacer");
		
		tdLeftPane.style.width = "18%";//Firefox must be set width percentage
        if(objectProperty == "")
        {			
            if(tdLeftPane.style.display == "none")
            {
                tdLeftPane.style.display = "";				
                tdLeftPaneHref.style.display = "none";
                tdSpacer.style.display = "";
            }
            else
            {
                tdLeftPane.style.display = "none";
                tdLeftPaneHref.style.display = "";
                tdSpacer.style.display = "none";
            }
        }
        else
        {    
            if(objectProperty == "none")
            {
                tdLeftPane.style.display = "";				
                tdLeftPaneHref.style.display = "none";
                tdSpacer.style.display = "";
            }
            else
            {
                tdLeftPane.style.display = "none";
                tdLeftPaneHref.style.display = "";
                tdSpacer.style.display = "none";
            }
        }
    }
    catch(e)
    {}
}

function GetFileApplicationTypeImageName(extensionName)
{
    switch(extensionName)
    {
        case ".pdf": return "pdf.gif";
        case ".doc": return "msword.gif";
        case ".xls": return "msexcel.gif";
        case ".txt": return "text.gif";
        case ".zip": return "archive.gif";
        case ".rar": return "archive.gif";
        
        case ".bmp" : return "image.gif"; 
        case ".gif" : return "image.gif"; 
        case ".ief" : return "image.gif"; 
        case ".jpeg" : return "image.gif"; 
        case ".jpg" : return "image.gif"; 
        case ".jpe" : return "image.gif"; 
        case ".png" : return "image.gif"; 
        case ".tiff" : return "image.gif"; 
        case ".tif" : return "image.gif"; 
        case ".djvu" : return "image.gif"; 
        case ".djv" : return "image.gif"; 
        case ".wbmp" : return "image.gif"; 
        case ".ras" : return "image.gif"; 
        case ".pnm" : return "image.gif"; 
        case ".pbm" : return "image.gif"; 
        case ".pgm" : return "image.gif"; 
        case ".ppm" : return "image.gif"; 
        case ".rgb" : return "image.gif"; 
        case ".xbm" : return "image.gif"; 
        case ".xpm" : return "image.gif"; 
        case ".xwd" : return "image.gif"; 
        
        default: return "unknown.gif";
    }
}
function ExecuteClickIEFF(obj) {
    if (isIE)
        obj.click();
    else {
        var evt = document.createEvent("MouseEvents");
        evt.initEvent("click", true, true);
        obj.dispatchEvent(evt);
    }
}
function changeDateToString(date) {
    if (date == null || date == '') {
        return "";
    } else {
        try{
            var xYear = date.getYear();
            xYear = xYear;
            var xMonth = date.getMonth() + 1;
            if (xMonth < 10) {
                xMonth = "0" + xMonth;
            }
            var xDay = date.getDate();
            if (xDay < 10) {
                xDay = "0" + xDay;
            }
            var xHours = date.getHours();
            if (xHours < 10) {
                xHours = "0" + xHours;
            }
            var xMinutes = date.getMinutes();
            if (xMinutes < 10) {
                xMinutes = "0" + xMinutes;
            }
            var xSeconds = date.getSeconds();
            if (xSeconds < 10) {
                xSeconds = "0" + xSeconds;
            }
            return xMonth + "/" + xDay + "/" + xYear;
            }
            catch(e)
            {
            return  "";
            }
    }
}

//There are other pages already defined CloseDiv function for different use.
//If we define the function here, will make corresponding function in other pages do not work, such as DashBoard.
//So I changed this function name to JCloseDivWindow
function JCloseDivWindow(divName) {
    $('#' + divName).hide();
}

function CapitalizeFirstLetterInNames(name) {
    var retVal = '';
    if (name.length > 0) {
        retVal = name;
        var reg = /\b[A-Z][A-Z]\w+/g;
        var r = retVal.match(reg);
        if (r != null) {
            for (var i = 0; i < r.length; i++) {
                retVal = retVal.replace(r[i], function() {
                    return r[i].substring(0, 1).toUpperCase() + r[i].substring(1).toLowerCase();
                });
            }
        }
    }
    return retVal;
}

if (typeof $ != "undefined") {
    $(document).ready(function() {
        var originalFontSize = 0;
        $('div[name=LeftTab]').hover(function() { //mouseover
            originalFontSize = parseInt($(this).css('font-size'));
            $(this).css('font-weight', 'bold').css('text-decoration', 'underline').css('color', '#cc5511');
            $(this).stop().animate({
                'paddingLeft': '10px',
                'fontSize': parseInt(1.2 * parseInt($(this).css('font-size'))) + 'px'
            }, 200);
        }, function() { //mouseout
            $(this).css('font-weight', 'normal').css('text-decoration', 'none').css('color', 'black');
            $(this).stop().animate({
                'paddingLeft': '0px',
                'fontSize': originalFontSize + 'px'
            }, 1000);
        });
    })
}

