﻿
var gblAnimalID;
var refNumb = 1;
var gblPopDiv;
var gblWarp;
var gblImgPhoto;
var toHndl;
function doSearch(txt) {
    document.getElementById('ctl00_ContentPlaceHolder1_hiddSearchText').value = txt;
    ig$("ctl00_ContentPlaceHolder1_WARPGrid").refresh();
}
function WARP_RefreshRequest(oPanel, oEvent, id) {
    if (toHndl == null) {
        //toHndl = setTimeout(function() { window.location = 'SessionExp.aspx' }, 30000);
        toHndl = setTimeout(function() { alert('The server is not responding, please try again later.'); HideWait() }, 30000);
    }
}
function WARP_RefreshResponse(oPanel, oEvent) {
    clearTimeout(toHndl);
    toHndl = null;
}
function displayPopupDiv(AnimalID, IsServerRequest) {
    if (!IsServerRequest)
        if (AnimalID != gblAnimalID) { alert('aborting: ' + IsServerRequest); return }
    
    if (AnimalID.indexOf('P') > -1) {
        gblPopDiv = document.getElementById('PopupDivLF');
        gblImgPhoto = document.getElementById('ctl00_ContentPlaceHolder1_PopImgLF');
    } else {
        gblPopDiv = document.getElementById('PopupDiv');
        gblImgPhoto = document.getElementById('ctl00_ContentPlaceHolder1_PopImg');
    }
    refNumb = refNumb + 1;
    var posx;
    var posy;
//    posx = 270;
    posy = Math.round(((getWindowHeight() / 2) - 260));
    posx = Math.round(((getWindowWidth() / 2) - 300));
   
    if (posy + 480 > getWindowHeight()) {
        posy = getWindowHeight() - 480;
    }
    gblPopDiv.style.top = posy + 'px';
    gblPopDiv.style.left = posx + 'px';
    document.getElementById('ctl00_ContentPlaceHolder1_txtID').value = AnimalID;
    gblAnimalID = AnimalID;
    if (AnimalID.indexOf('P') > -1) {
        gblWarp = ig$("ctl00_ContentPlaceHolder1_WebAsyncRefreshPanel2");
    } else {
        gblWarp = ig$("ctl00_ContentPlaceHolder1_WebAsyncRefreshPanel1");
    }

    if (!gblWarp)
        return;
    gblWarp.refresh();
    ShowWait();
}
// remove pop-up DIV element
function hidePopupDiv() {
    if (!gblPopDiv) { return };
    gblPopDiv.style.display = 'none';
    document.getElementById('darkBackgroundLayer').style.display = 'none';
    gblImgPhoto.src = null;
    gblAnimalID = null;
    if (window.event.stopPropagation) {window.event.stopPropagation();} else {window.event.cancelBubble = true;}
}
//var timeOutHandle;
function makeVisible() {
//    if (!timeOutHandle == null ){clearTimeout(timeOutHandle);}
    //    if (isImageOk(gblImgPhoto)) {
    HideWait();
    if (BrowserDetect.browser == 'Explorer') { if (BrowserDetect.version > 6) document.getElementById('ctl00_ContentPlaceHolder1_txtID').style.display = 'block'; }
        gblPopDiv.style.display = 'block';
        document.getElementById('darkBackgroundLayer').style.display = 'block';
        ig$("ctl00_ContentPlaceHolder1_WARP_BigPic").refresh(); //loads big picture in the background while displaying the folder
//    } else { timeOutHandle = setTimeout('makeVisible()', 1000)}
}


function isImageOk(img) {

    if (!img.complete) {
        return false;
    }

    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
        return false;
    }

    // No other way of checking: assume it's ok.
    return true;
}
var origY;
var origScroll;
var stop = false;
var div;
function UltraWebGrid1_MouseDownHandler(gridName, id, button) {
    stop_iner();
    div = igtbl_getDocumentElement("ctl00xContentPlaceHolder1xUltraWebGrid1_div");
    document.body.onmousemove = doScroll;
    origY = event.clientY;
    origScroll = div.scrollTop;
}

function stop_iner() {
    clearTimeout(inrtHndl);
    stop = true;
    ClearVars();
}
function ClearVars() {
    x = 0;
    y = 0;
    inc = 0;
    speed = 0;
}
var inrtHndl;

function UltraWebGrid1_MouseOutHandler(gridName, id, objectType) {
   if (objectType == 1) {
    stop = false; document.body.onmousemove = null; 
   }
}
function UltraWebGrid1_MouseUpHandler(gridName, id, button) {
    stop = false;
    document.body.onmousemove = null;
    if (Math.abs(speed) > 50) {
        div = igtbl_getDocumentElement("ctl00xContentPlaceHolder1xUltraWebGrid1_div");
        inrtHndl = setTimeout('Inertia()', 4);

    }
}
var T1;
var T2;
var Y1;
var Y2;
var speed;
function doScroll() {
    var d = new Date();
    T2 = d.getTime();
    Y2 = event.clientY;
    div.scrollTop = origScroll + (origY - event.clientY);
    if (T2 - T1 > 0) { speed = 800 * ((Y2 - Y1) / (T2 - T1)) }
    T1 = d.getTime();
    Y1 = event.clientY;
    //   window.status = speed; 
}
var div;
var x = 0;
var y = 0;
var start;
var inc = 0;

function Inertia() {
    y = div.scrollTop;
    if (stop) { return }
    inc = Math.round((Math.abs(speed)) / ((x + 10) ^ 2));
    if (speed > 0) { y = y - inc } else { y = y + inc }
    x = x + 5;
    div.scrollTop = y;
    if (div.scrollTop != y) { Bounce(); return }
    if (inc < 3) { x = 0; y = 0; return }
    setTimeout('Inertia()', 5);
}
function Bounce() {
    var amp = Math.log(inc);
    if (speed > 0) {
        for (i = 0; i < amp; i++) {
            div.scrollTop += i;
        }
        for (i = 0; i < amp; i++) {
            div.scrollTop -= i;
        }
    } else {
        for (i = 0; i < amp; i++) {
            div.scrollTop -= i;
        }
        for (i = 0; i < amp; i++) {
            div.scrollTop += i;
        }
    }
    x = 0;
    y = 0;
}
function WebAsyncRefreshPanel_Error(oPanel, oEvent, flags) {
    if (flags == 1) {
        alert("An error has ocurred on the server while processing an asynchronous call. If the error persist, please restart the application.");
        oEvent.Cancel = true;
    }
}
function WebAsyncRefreshPanel1_RefreshComplete(oPanel) {
    makeVisible();
}

function WARPGrid_RefreshComplete(oPanel) {
    
    //var capt = document.getElementById('divCaption');
//    var aniType = document.getElementById('ctl00_ContentPlaceHolder1_hiddAnimalType').value;
//    var selType;
    //if (document.getElementById('hiddSelection').value == 'Found') { selType = 'Found' } else { selType = 'Adoptable' }
//    selType = document.getElementById('ctl00_ContentPlaceHolder1_hiddSelection').value;
//    if (aniType != 'Other') {

//        setText(capt, selType + ' ' + aniType + 's');
//    } else {
//        setText(capt, 'Other ' + selType + ' Pets');
//    }

//    var imgDog = document.getElementById('imgAdoptDog');
//    var imgCat = document.getElementById('imgAdoptCat');
//    var imgOther = document.getElementById('imgAdoptOther');
//    imgDog.alt = selType + ' Dogs';
//    imgCat.alt = selType + ' Cats';
//    imgOther.alt = 'Other ' + selType + ' Pets';
    //alert(selType);
    resizeGrid();
}
function UltraWebGrid1_DblClickHandler(gridName, cellId) {
    var cell = igtbl_getCellById(cellId);
    if (cell != null) {
        if (cell.Column.Index > 0) {
            fnSelect(cell.getElement().id);
        }
    }
}
function UltraWebGrid1_CellClikHandler(gridName, cellId, button) {
    var cell = igtbl_getCellById(cellId);
    if (cell != null) {
        if (cell.Column.Index == 0) {
            gblAnimalID = cell.getRow().getCellFromKey('ID').getValue();
            //display = true;
            //if (cell.getRow() == null) return;
            var AnimID = cell.getRow().getCellFromKey('ID').getValue();
            //setTimeout(function() { displayPopupDiv(AnimID) }, 400);
            displayPopupDiv(AnimID);
        } else {
        if (button == 2) fnSelect(cell.getElement().id);
        }
    }
}
function UltraWebGrid1_ClickCellButtonHandler(gridName, cellId, button) {
    var cell = igtbl_getCellById(cellId);
    if (cell != null) {
        //window.location = 'ClaimForm.aspx?id=' + cell.getRow().getCellFromKey('ID').getValue();
        showPopup('MoreInfo.aspx?id=' + cell.getRow().getCellFromKey('ID').getValue(), 465, 575, false, true, true);
    }
}

function OnPageLoad() {

    if (BrowserDetect.browser != 'Explorer') {
        document.getElementById('darkBackgroundLayer').style.position = 'fixed';
        document.getElementById('darkBackgroundLayer1').style.position = 'fixed';
        document.getElementById('PopupDiv').style.position = 'fixed';
        document.getElementById('PopupDivLF').style.position = 'fixed';
        document.getElementById('divBigPic').style.position = 'fixed';
    }
    //document.getElementById('PopupDiv').style.display = 'none';
    ig_getWebControlById('ctl00_ContentPlaceHolder1_WebAsyncRefreshPanel1').getProgressIndicator().setTemplate('<div/>');
    ig_getWebControlById('ctl00_ContentPlaceHolder1_WebAsyncRefreshPanel2').getProgressIndicator().setTemplate('<div/>');
    ig_getWebControlById('ctl00_ContentPlaceHolder1_WARP_BigPic').getProgressIndicator().setTemplate('<div/>');
    ig_getWebControlById('ctl00_ContentPlaceHolder1_WARPGrid').getProgressIndicator().setTemplate('<img src=Images/progress.gif />');
   // if (document.getElementById('hiddSelection').value == 'Found' || document.getElementById('hiddSelection').value == 'Lost') { ChangeButtons() }
    resizeGrid();
}
function ChangeButtons() {

    cube_list1 = [
    "images/LostDog.png",
    "images/AdoptDog.png"
];
    cube_list2 = [
    "images/LostCat.png",
    "images/AdoptCat.png"

];
    cube_list3 = [
    "images/LostOther.png",
    "images/AdoptOther.png"
];

}
function imgFolder_onclick() {

}
function ActiveButton(img) {
    img.src = img.src.replace('.png', 'Hover.png');
    img.src = img.src.replace('.jpg', 'Hover.jpg');
}
function InactiveButton(img) {
    img.src = img.src.replace('Hover.png', '.png');
    img.src = img.src.replace('Hover.jpg', '.jpg');
}
function Navigate(Target) {
    stop_iner();
    document.getElementById('ctl00_ContentPlaceHolder1_hiddAnimalType').value = Target;
    searchStart();
//    var div = document.getElementById('divArrow');
//    if (!div) return;
//    switch (Target) {
//        case 'Dog':
//            div.style.top = 178;
//            break;
//        case 'Cat':
//            div.style.top = 328;
//            break;
//        case 'Other':
//            div.style.top = 477;
//            break;
//    }
    self.focus();
}

var expandGridVertically;
function resizeGrid() {
    if (BrowserDetect.browser != 'Explorer') { return; }
    var grid = igtbl_getGridById('ctl00_ContentPlaceHolder1_UltraWebGrid1');
    //var w = f_clientWidth();
    var w = 907;
    var offset = 235;
    var sel = document.getElementById('ctl00_ContentPlaceHolder1_hiddSelection').value;
    //alert(sel);
    if (sel == 'Adoptable' || sel == 'Lost') {  } else { offset = 310}
   // if (afterRefresh == true) { offset = offset + 49; grid.Bands[0].Columns[0].setWidth(103) } else { grid.Bands[0].Columns[0].setWidth(110) }
    grid.Bands[0].Columns[0].setWidth(110)
    var efecGrWi = w - offset;   //386
    
    var colWidth = Math.round(efecGrWi * 0.25);
    //colWidth = 100;
    //alert(colWidth);
    
    grid.Bands[0].Columns[2].setWidth(colWidth);
    grid.Bands[0].Columns[3].setWidth(colWidth);
    grid.Bands[0].Columns[4].setWidth(colWidth);
    grid.Bands[0].Columns[5].setWidth(colWidth);
    grid.Bands[0].Columns[6].setWidth(100);
    //alert(grid.MainGrid.style.top);
    if (expandGridVertically) { document.getElementById('tblOuter').style.height = (grid.Rows.length * 86) + 230 };
}
window.onresize = resizeGrid;
function setSelection(div) {
    var sel = getText(div);
    var capt = document.getElementById('divCaption');

    if (sel.indexOf('ound') > -1) {
        sel = 'Found'
        //capt.innerText = 'Found Pets';
        //setText(div, 'Show me adoptable pets');
    } else {
        sel = 'Adoptable'
        //capt.innerText = 'Adoptable Pets';
        //setText(div, 'Show me found pets');
    }
    document.getElementById('ctl00_ContentPlaceHolder1_hiddSelection').value = sel;

    cube_start();

}
function suppInner() { return (BrowserDetect.browser == 'Explorer' || BrowserDetect.browser == 'Safari') }
function setText(div, text) {
    if (suppInner()) { div.innerText = text } else { div.textContent = text }
}
function getText(div) {
    if (suppInner()) { return div.innerText } else { return div.textContent }
}
function spin_end() {

    Navigate(document.getElementById('ctl00_ContentPlaceHolder1_hiddAnimalType').value)
}

function doZoom(zoomIcon) {
    var popDiv;
    var refObj;
    if (zoomIcon.id.indexOf('LF') > -1) {
        refObj = document.getElementById('ctl00_ContentPlaceHolder1_PopImgLF');
        popDiv = document.getElementById('PopupDivLF');
    } else {
        refObj = document.getElementById('ctl00_ContentPlaceHolder1_PopImg');
        popDiv = document.getElementById('PopupDiv');
    }

    document.getElementById('darkBackgroundLayer1').style.display = 'block';
    var bigPic = document.getElementById('divBigPic');
    if (refObj && popDiv) {
        bigPic.style.top = parseInt(refObj.style.top.replace('px', '')) + parseInt(popDiv.style.top.replace('px', '')) - 15;
        bigPic.style.left = parseInt(refObj.style.left.replace('px', '')) + parseInt(popDiv.style.left.replace('px', '')) - 20;
    } else {
         bigPic.style.top = 100;
         bigPic.style.left = 280;
    }
     bigPic.style.display = 'block';
}
function killZoom() {
    //document.getElementById('imgBigPic').src = '';
    document.getElementById('divBigPic').style.display = 'none';
    document.getElementById('darkBackgroundLayer1').style.display = 'none';
}
function petcardPrint() {
    //document.body.style.cursor = 'wait';
    //window.open('Petcard.aspx?id=' + 'A1446871', 'mywin', '');
    winopen('Petcard.aspx?id=' + gblAnimalID, 720, 620, gblAnimalID, 0, true);
    document.body.style.cursor = 'default';
}
function flyerPrint() {
    //document.body.style.cursor = 'wait';
    //winopen('Flyer.aspx?id=' + gblAnimalID + '&lf=' + document.getElementById('hiddSelection').value, 720, 620, gblAnimalID, 0, true);
    window.location = 'download.aspx?id=' + gblAnimalID + '&type=' + document.getElementById('ctl00_ContentPlaceHolder1_hiddSelection').value;
     document.body.style.cursor = 'default';
}
window.onload = OnPageLoad;
function UltraWebGrid1_AfterFilterApplied(gridName, oCol) {
    var grid = igtbl_getGridById('ctl00_ContentPlaceHolder1_UltraWebGrid1');
    hideClaim(grid);
}
function hideClaim(grid) {
    if (document.getElementById('ctl00_ContentPlaceHolder1_hiddSelection').value != 'Found') {
        grid.Bands[0].Columns[1].setHidden(true);
    }
}


function winopen(arg, w, h, wname, offset, resizable) {
    if (offset == null) { offset = 0; }
    var l = 0;
    var t = 0;
    try {
        l = ((screen.availWidth - w) / 2) + offset;
        t = ((screen.availHeight - h) / 2) - offset;
    } catch (err) { }
    var cfg = "width=" + w;
    cfg += ",height=" + h;
    cfg += ",top=" + t;
    cfg += ",left=" + l;
    cfg += ",status=no";
    cfg += ",scrollbars=yes";
    if (resizable == true) cfg += ",resizable=yes";
    window.open(arg, wname, cfg);

}

function SetTabImage(activeTab) {
    tblTab.style.backgroundImage = 'url(Images/Tab' + activeTab + '.png)';
}
