﻿var isFirefox;
var isIE;
// Get browser type
	isFirefox = ( navigator.appName == "Netscape" );
	isIE = (navigator.appName == "Microsoft Internet Explorer" ); 

//the following functions are used for combo drop down list
var dragapproved=false
var initialwidth,initialheight
var ie5=document.all&&document.getElementById
var ns6=document.getElementById&&!document.all
var animtimer; 
var CmbWindow;
var targetObj;
var positionObj;
this.divCmbContainer = getElement("divCmbContainer");

function loadCmbList(sobj,tobj,height,width,obj){    
    var displayNone = true;
    if (!ie5&&!ns6)
        window.open(url,"","width=width,height=height,scrollbars=1")
    else{
        
        var sourceObj = getElement(sobj);
        if (!sourceObj) sourceObj = getElementByTag(sobj,'div');// founction found in jsfiles/auxiliary.js
        
        targetObj = getElement(tobj);
        if (!targetObj) targetObj = getElementByTag(tobj,'input');// founction found in jsfiles/auxiliary.js
        if (!this.divCmbContainer) this.divCmbContainer = getElementByTag("divCmbContainer","div");// founction found in jsfiles/auxiliary.js
        
        CmbWindow = "divCmbWindow";
        divCmbMain =  getElement("divCmbWindow");
        
        if (divCmbMain.style.display == 'block') 
            displayNone = false;
 
        oWidth = width;
        oHeight = height;
        
        this.divCmbContainer.innerHTML= sourceObj.innerHTML;
        
        this.tables = this.divCmbContainer.getElementsByTagName("table");
		for (var i = 0; i < this.tables.length; i++)
		{	
			this.rows = this.tables[i].getElementsByTagName("tr");
			this.divCmbContainer.style.height=initialheight="100%";
			if (this.rows.length >= 50){
			    this.divCmbContainer.style.height=initialheight="300px";
			    this.divCmbContainer.style.overflow="auto";
			}
		}	
			
        //divCmbContainer.style.height=initialheight=(oHeight - 10)+"px"
        divCmbMain.style.width=initialwidth=oWidth+"px"
        //divCmbMain.style.height=initialheight=oHeight+"px"
        
        //if (divCmbMain.style.display != 'block') {
        if (!obj) {
            obj = getElementByTag(obj,'span');// founction found in jsfiles/auxiliary.js
        }
        temp = getPosition(obj)//getPosition is found in jsfiles/auxiliary_tools.js
        curx = (temp[0] - (oWidth - 15));
        cury = (temp[1] + 19);
        if (curx < 0) curx=oWidth;
        divCmbMain.style.display='block';
       // }
        if (displayNone)
            fadeInCmb(divCmbMain.id,50);
            
        divCmbMain.style.left = curx + "px";
		divCmbMain.style.top = cury + "px";
        //divCmbMain.style.top = ns6? window.pageYOffset*1+cury + "px" : iecompat().scrollTop*1+cury + "px"  
        
        //if targetobj value is not empty, find and highlight from the list
		for (var j = 0; j < this.rows.length; j++){
			if (this.rows[j].innerHTML.indexOf(targetObj.value) >= 0){
				rowChangeClass(this.rows[j],"cssRowSel");
				var rowsel = this.rows[j];
				if (rowsel)	{
				   temp1 = getPosition(this.divCmbContainer);//getPosition is found in jsfiles/auxiliary_tools.js 
				   //alert(temp1[1]);
                   temp = getPosition(rowsel);//getPosition is found in jsfiles/auxiliary_tools.js 
                   //alert(temp[1]);
		            this.divCmbContainer.scrollTop = temp[1] - temp1[1];
                } 
				break;
			}
		}
    }    
}

function executeCmbList(sobj,tobj,width,pobj){
    targetObj = getElement(tobj);
    positionObj = getElement(pobj);
        if (!targetObj) targetObj = getElementByTag(tobj,'input');// founction found in jsfiles/auxiliary.js
    
    if (targetObj.value == '')
        loadCmbList(sobj,tobj,300,width,positionObj);
}
function searchList(obj){
if (obj.value == '') {
    //executeCmbList(obj.id,targetObj.id,270,positionObj.id);
    return;
}
var foundme = false;
    this.tables = this.divCmbContainer.getElementsByTagName("table");
	this.rows = this.tables[0].getElementsByTagName("tr");
	for (var i = 0; i < this.rows.length; i++)
	{	
	    this.cells =this.rows[i].getElementsByTagName("td");
	    for (var j = 0; j < this.cells.length; j++)
	    {
	        //alert(this.cells[j].innerHTML);
		    if (this.cells[j].innerHTML.toLowerCase().indexOf(obj.value.toLowerCase()) == 0){
		        rowChangeClass(this.rows[i],"cssRowSel");
		        var cellsel= this.cells[j];
                temp = getPosition(cellsel);//getPosition is found in jsfiles/auxiliary_tools.js 
                this.divCmbContainer.scrollTop = temp[1] - 286;
                foundme = true;
		        break;
		    }else rowChangeClass(this.rows[i],"cssRow");
		    if (foundme) break;
	    }
	}
}// end FUNCTION 

function rowChangeClass(obj,cname){
    obj.className = cname
}
function selectValue(obj){
    targetObj.value = trim(obj.innerHTML);
    closeItCmb();
}
function selectRecipient(obj){
    var recipient = obj.getElementsByTagName("div");
    var rarray = recipient[0].innerHTML.split("|");
    targetObj.value = trim(rarray[0]);
    getElementByTag("jobposition","input").value = trim(rarray[1]);// founction found in jsfiles/auxiliary.js
    getElementByTag("fname","input").value = rarray[2];
    getElementByTag("mname","input").value = rarray[3];
    getElementByTag("lname","input").value = rarray[4];
    getElementByTag("address","input").value = rarray[5];
    getElementByTag("City","input").value = rarray[6];
    getElementByTag("zipcode","input").value = rarray[7];
    getElementByTag("phone","input").value = rarray[8];
    getElementByTag("fax","input").value = rarray[9];
    //getElementByTag("email","input").value = rarray[10];
    getElementByTag("County","input").value = trim(rarray[11]);
    closeItCmb();
}
function selectFIPSCountyCode(obj){
    var County_Code = obj.getElementsByTagName("div");
    var rarray = County_Code[0].innerHTML.split("|");
    targetObj.value = trim(rarray[1]);
    closeItCmb();
}
function selectRecipient_Name(obj){
    var recipient = obj.getElementsByTagName("div");
    var rarray = recipient[0].innerHTML.split("|");
    getElementByTag("txtRecipient_ID","input").value = trim(rarray[0]);
    targetObj.value = trim(rarray[1]);
    getElementByTag("txtCOrganization","input").value = trim(rarray[1]);
    getElementByTag("txtRecipient_Type","input").value = trim(rarray[2]);
    getElementByTag("txtPopulation","input").value = rarray[8];
    getElementByTag("txtMHI","input").value = trim(rarray[10]);
    getElementByTag("txtRecipient_Address1","input").value = rarray[15];
    getElementByTag("txtRecipient_City","input").value = rarray[17];
    getElementByTag("txtRecipient_Zip","input").value = rarray[19];
    getElementByTag("txtCounty","input").value = rarray[3];
    getElementByTag("cmbRegion","select").value = rarray[4];
    getElementByTag("cmbSystem_Type","select").value = rarray[7];
    getElementByTag("txtConnections","input").value = trim(rarray[9]);
    getElementByTag("txtSystem_Number","input").value = trim(rarray[6]);
    getElementByTag("chkPUC_Regulated","input").checked = (trim(rarray[11])=='False')? false: true;
    getElementByTag("txtCCR_DUNS","input").value = trim(rarray[20]);
    getElementByTag("txtCCR_Business_Name","input").value = trim(rarray[21]);
    closeItCmb();
    //targetObj.disabled = true;
}
function closeItCmb(){
    divCmbMain =  getElement( CmbWindow);   
    fadeOutCmb(divCmbMain.id,20)
}

function iecompat(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function dragDropCmb(e){
    divCmbMain =  getElement( CmbWindow)
    if (ie5&&dragapproved&&event.button==1){
        divCmbMain.style.left=tempx+event.clientX-offsetx+"px"
        divCmbMain.style.top=tempy+event.clientY-offsety+"px"
        curx = tempx+event.clientX-offsetx
        cury = tempy+event.clientY-offsety
    }
    else if (ns6&&dragapproved){
        divCmbMain.style.left=tempx+e.clientX-offsetx+"px"
        divCmbMain.style.top=tempy+e.clientY-offsety+"px"
    }
}

function initDragCmb(e){
    offsetx=ie5? event.clientX : e.clientX
    offsety=ie5? event.clientY : e.clientY
    divCmbMain =  getElement(CmbWindow)
    tempx=parseInt(divCmbMain.style.left)
    tempy=parseInt(divCmbMain.style.top)
    
    dragapproved=true
    divCmbMain.onmousemove=dragDropCmb
}
function stopDragCmb(){
    dragapproved=false
    divCmbMain =  getElement(CmbWindow)
    divCmbMain.onmousemove=null
}
function fadeInCmb(objId,opacity) {
  if (document.getElementById(objId)) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 10;
      animtimer = window.setTimeout("fadeInCmb('"+objId+"',"+opacity+")", 50);
    }else clearTimeout(animtimer)
  }
}
function fadeOutCmb(objId,opacity) {
  if (document.getElementById(objId)) {
    obj = document.getElementById(objId);
    if (opacity >=5) {
      setOpacity(obj, opacity);
      opacity -= 5;
      animtimer = window.setTimeout("fadeOutCmb('"+objId+"',"+opacity+")", 5);
    }else {
        clearTimeout(animtimer);
        obj.style.display = '';
    }
  }
}

// This section is used to fade in and out the window
function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

function getElement( id ) 
{
	if(document.layers) { // Netscape 4
	    return document.id 
	}
	else if (document.all) { // IE 4
	    if (document.all[ id ]){
		    return document.all[ id ];
		}
		else {
		    return window.parent.document.all[ id ];
		}
	}
	else if (document.getElementById){ // IE 5 and up, NS 6 and up
	    if (document.getElementById( id )){
		    return document.getElementById( id );
		}
		else {
		    return window.parent.document.getElementById( id );
		}
	}
}
function viewProjectList(objname){
    var obj = getElement( objname ); 
    alert(obj.id);
}

//-----------------------------------------------------------------------------------------------
// this function is used/invoked in admins/ucxfiles/recipients.ascx
//-----------------------------------------------------------------------------------------------
var SearchRecordCtr=0
var tempStrSearchValue=''
var strSearchContinue = false;
var foundRecord = false;
function searchRecordList(obj,tlist){
    this.divListContainer = document.getElementById(tlist);
    //reset SearchRecordCtr to 0 if str search value is changed
    if (obj.value!=tempStrSearchValue) {
        foundRecord = false;
        SearchRecordCtr = 0;
        tempStrSearchValue = obj.value;
    }
    if (obj.value == '') {
        alert("Search string is required!");
        //executeCmbList(obj.id,targetObj.id,270,positionObj.id);
        return;
    }
    var rowStyle = 'gvRow'
    this.tables = this.divListContainer.getElementsByTagName("table");
    if (!this.tables[0]) return;
    this.rows = this.tables[0].getElementsByTagName("tr");
    //alert(tempStrSearchValue + ' ' + SearchRecordCtr + ' ' + this.rows.length);
    if (this.rows.length > SearchRecordCtr){    
        for (var i = SearchRecordCtr; i < this.rows.length; i++)
        {	           
            this.cells =this.rows[i].getElementsByTagName("td");
            if (this.cells.length > 1)
            {
                //alert(this.cells[1].innerHTML);
                var searchValue; 
                for (var c = 1;c < this.cells.length; c++)
                {
                    searchValue += this.cells[c].innerHTML;
                }      
                //alert(searchValue);      
	            if (searchValue.toLowerCase().indexOf(obj.value.toLowerCase()) >= 0){
	                rowChangeClass(this.rows[i],"gvRowSel");
	                var cellsel= this.cells[1];
                    temp = getPosition(cellsel); //getPosition is found in jsfiles/auxiliary_tools.js
                    this.divListContainer.scrollTop = temp[1] - 286;
                    strSearchContinue = true;                            
                    var expireDate = new Date;
                    var cookiePath = "/";
                    // set cookie to expire in 12 months
                    expireDate.setMonth(expireDate.getMonth()+12)
                    //set cookie
                    document.cookie = obj.id+tlist+"="+obj.value+";expires=" + expireDate.toGMTString()+cookiePath;
                    SearchRecordCtr = i+1;
                    foundRecord = true;
	                break;
	            }else {
	                strSearchContinue = false;
	                rowChangeClass(this.rows[i],rowStyle);
	                if (rowStyle == 'gvRow')
	                    rowStyle = 'gvAlternatingRow';
	                else
	                    rowStyle = 'gvRow';
	            }
	        }
        }
        if (!strSearchContinue) SearchRecordCtr = 0;
    } else SearchRecordCtr = 0;    
    if (!foundRecord) alert ('No record found!');
}
function initSearchRecordList(obj,tlist){
    if (document.getElementById(tlist)){   
        foundRecord = true;     
        obj.value = readCookie(obj.id+tlist);
        tempStrSearchValue = obj.value;
        searchRecordList(obj,tlist);
    }
}

ProcessKeypress = function(e) {
    if( !e ) { 
        if( window.event ) { 
            //DOM 
            e = window.event; 
        } else { 
            //TOTAL FAILURE, WE HAVE NO WAY OF REFERENCING THE EVENT 
            return (false); 
        } 
    } 

    if( typeof( e.which ) == 'number' ) { 
        //NS 4, NS 6+, Mozilla 0.9+, Opera 
        e = e.which;  
    } else if( typeof( e.keyCode ) == 'number' ) { 
        //IE, NS 6+, Mozilla 0.9+ 
        e = e.keyCode;  
    } else if( typeof( e.charCode ) == 'number' ) { 
        //also NS 6+, Mozilla 0.9+ 
        return (false);
        e = e.charCode; 
    } else { 
        //TOTAL FAILURE, WE HAVE NO WAY OF OBTAINING THE KEY CODE 
        return (false); 
    } 
    if (e=='13') {  
        document.getElementById('btnSearchList').click(); 
        return (false); 
    } 
} 

