﻿var _scrollpos;
var _divUploadFrame;
var _divUploadMessage;
var _divUploadProgress;
var _ifrFile;
var mouseX, mouseY;

function setMousePosition(e)
{
    if (!e){
        var e = window.event||window.Event;
    }
    
    if ('undefined'!=typeof e.pageX){
        mouseX = e.pageX;
        mouseY = e.pageY;
    } else {
        mouseX = e.clientX;
        mouseY = e.clientY;
    }
}

function TextBox_OnFocus(ctl_Id)
{   
    var divs = new Array('tlc', 't', 'trc', 'l', 'r', 'blc', 'b', 'brc');
    var div;
    var x;
    
    for (x in divs){
        div = document.getElementById(ctl_Id + 'div' + divs[x]);
        div.className = "bluebox" + divs[x];
    }
}

function TextBox_OnBlur(txt, ctl_Id, className)
{   
    if (txt.autoCompleteFocus == 1) {return 0};
    var divs = new Array('tlc', 't', 'trc', 'l', 'r', 'blc', 'b', 'brc');
    var div;
    var x;
    
    for (x in divs) {
        div = document.getElementById(ctl_Id + 'div' + divs[x]);
        div.className = className + "box" + divs[x];
    }
}

function CheckBox_OnClick(img, id)
{
    var box = document.getElementById(id);
    if (box.value == '1'){
        img.src = 'images/checkbox_unchecked.png';
        box.value = '0';
    } else {
        img.src = 'images/checkbox_checked.png';
        box.value = '1';
    }
}

function showModal()
{   
    document._scrollpos = document.documentElement.scrollTop;
    scroll(0,0);
    
    var ar = getPageSize();
    var bg = document.getElementById('modalbackground');
    if (bg) {
        bg.style.width = ar[0] > 966 ? '100%' : '966px';
        bg.style.height = ar[1] + 'px';  
        bg.style.display ='block';
    }

    var fra = document.getElementById('modalform');
    fra.style.display = 'block';
    
    window.onresize = Window_OnResize;
}

function hideModal()
{
    var fra =  parent.document.getElementById('modalform');
    fra.style.display = 'none';
    document.body.innerHTML = '';
    
    var bg = parent.document.getElementById('modalbackground');
    if (bg) {
        bg.style.display = 'none';
    }
    window.onresize = '';
    window.onunload = '';
    
    parent.document.documentElement.scrollTop = parent.document._scrollpos;
    parent.updateAjax();
}

function Window_OnResize()
{ 
    var ar = getPageSize();
    var bg = document.getElementById('modalbackground');
    if (bg){
        bg.style.width = ar[0] > 966 ? '100%' : '966px';
        bg.style.height = ar[1] + 'px'; 
    }
}

function getPageSize()
{
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) { 
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
        xScroll = document.documentElement.scrollWidth;
        yScroll = document.documentElement.scrollHeight;
    } else { // Explorer Mac...would also work in Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    
    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    } 
    
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else { 
        pageHeight = yScroll;
    }
    
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){ 
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }
    
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight); 
    return arrayPageSize;
}

function initAutoComplete()
{
    txtAutoComplete = document.getElementById(txtAutoCompleteId);
    lstAutoComplete = document.getElementById(lstAutoCompleteId);   
}

function txtSearch_OnKeyUp(e, txt){
    var characterCode;
    
    if(e && e.which){ //if which property of event object is supported (NN4)
        e = e;
        characterCode = e.which; //character code is contained in NN4's which property
    } else {
        e = event;
        characterCode = e.keyCode; //character code is contained in IE's keyCode property
    }

    if (characterCode != 13){
        autoCompleteSearch(txt);
    }
}

function OnAutoComplete(result){
    lstAutoComplete.options.length=0
    for (i=0; i<result.length; i++){
        lstAutoComplete.options[i] = new Option(result[i], result[i], false, false);
    }
    lstAutoComplete.style.display = '';
}

function OnAutoCompleteError(result){
    alert('Error: ' + result.get_message());
}

function lstAutoCompleteOnMouseOver(lst){
    txtAutoComplete.autoCompleteFocus = 1;
}

function lstAutoCompleteOnMouseOut(lst){
    txtAutoComplete.autoCompleteFocus = 0;
    txtAutoComplete.focus();
}

function lstAutoCompleteOnBlur(lst){
    txtAutoComplete.value = lst.options[lst.selectedIndex].value;
    txtAutoComplete.autoCompleteFocus = 0;
    txtAutoComplete.focus();
    lst.style.display = 'none';
}
    
function updateAjax()
{
    __doPostBack(ajax, '');
}

function checkBoxOnClick(c, id)
{
    if (c.checked) { 
        chkSelCount ++;
        checkedIds += id + '|'
        if (chkCount == chkSelCount){
            chkSelAll.checked = true;
        }
    } else {
        chkSelCount --;
        checkedIds.replace(id + '|', '');
        chkSelAll.checked = false;
    }            
}    

function checkSelAllOnClick()
{
    for (i = 1; i <= chkCount; i++){ 
        var c = document.getElementById(chkBaseId + i);
        if (c){
            c.checked = chkSelAll.checked;
        }
    }
    if (chkSelAll.checked) {
        chkSelCount = chkCount;
        checkedIds = allIds;
    } else {
        checkedIds = '';
        chkSelCount = 0;
    }
}


function initFileUpload()
{
    _divUploadFrame = document.getElementById('divUploadFrame');
    _divUploadMessage = document.getElementById('divUploadMessage');
    _divUploadProgress = document.getElementById('divUploadProgress');
    _ifrFile = document.getElementById('ifrFile');
}

function uploadOnClick()
{
    var filFile = _ifrFile.contentWindow.document.getElementById('filFile');

    //Baisic validation for File
    _divUploadMessage.style.display = 'none';

    if (filFile.value.length == 0) {
        _divUploadMessage.innerHTML = '<span style=\"color:#ff0000\">Please specify the file.</span>';
        _divUploadMessage.style.display = '';
        filFile.focus();
        return;
    }

    _divUploadProgress.style.display = '';
    _ifrFile.contentWindow.document.getElementById('fileUpload').submit();
    _divUploadFrame.style.display = 'none';
}

function fileUploadComplete(message, isError)
{
    _divUploadProgress.style.display = 'none';
    _divUploadMessage.style.display = 'none';
    _divUploadFrame.style.display = '';

    if (message.length) {
        if (isError) {
            _divUploadMessage.innerHTML = '<span style=\"color:#ff0000\;font-weight:bold">' + message + '</span>';
            _divUploadMessage.style.display = '';
            _ifrFile.contentWindow.document.getElementById('filfile').focus();
            alert(message);
        } else {
            processUploadedFile(message);           
        }
    }
}

function fileUploadComplete2(message, isError) {
    _divUploadProgress.style.display = 'none';
    _divUploadMessage.style.display = 'none';
    _divUploadFrame.style.display = '';

    if (message.length) {
        if (isError) {
            _divUploadMessage.innerHTML = '<span style=\"color:#ff0000\;font-weight:bold">' + message + '</span>';
            _divUploadMessage.style.display = '';
            _ifrFile.contentWindow.document.getElementById('filfile').focus();
            alert(message);
        } else {
            processUploadedFile2(message);
        }
    }
}

function switchAjaxTab(tabId){
    hdnView.value = tabId;
    hdnAction.value = '';
    __doPostBack(ajaxTab, '');
    return false;
}

function fitRounded(ctl_Id, h, w){
    var x;
    
    if ( !(document.getElementById(ctl_Id + 'div') ))
    {
        return false;
    }
            
    if (h){
        var divh = new Array('l', 'r'); 
        var height = document.getElementById(ctl_Id + 'div').clientHeight;
        for (x in divh){
            div = document.getElementById(ctl_Id + 'div' + divh[x]);
            div.style.height = height + "px";
        }
    }
    
    if (w){
        var divw = new Array('t', 'b');
        var width = document.getElementById(ctl_Id + 'div').clientWidth;
        for (x in divw){
            div = document.getElementById(ctl_Id + 'div' + divw[x]);
            div.style.width = width + "px";
        }
    }
}  

function showPopup(title, msg){
    var helpbox = parent.document.getElementById('ctl00_pnlHelpTip');
    var rndId = 'ctl00_rndHelpTip';
    if (!helpbox){
        helpbox = parent.document.getElementById('ctl00_ctl00_pnlHelpTip');
        rndId = 'ctl00_ctl00_rndHelpTip';
    }
    var ar = getPageSize();
    var left = (ar[0] - helpbox.clientWidth) / 2;
    var top = 75;
    var helptiphdr = parent.document.getElementById('helptipheader');
    var helptip = parent.document.getElementById('helptip');
    
    helptiphdr.innerHTML = title;
    helptip.innerHTML = msg;
    
    fitHelpRounded(rndId, true, false);
    
    helpbox.style.left = left + 'px';
    helpbox.style.top = top + 'px';
    helpbox.style.visibility = 'visible';
    helpbox.style.zIndex = 0;
}

function hideHelpTip(){
    var helpbox = parent.document.getElementById('ctl00_pnlHelpTip');
    if (!helpbox){
        helpbox = parent.document.getElementById('ctl00_ctl00_pnlHelpTip');
    }
    helpbox.style.visibility = 'hidden';
    Window_OnResize();
    return false;
}

function popHelpTip(id, lang){
    Help.GetHelpTip(id, lang, OnHelpTipComplete,OnHelpTipError);
    return false;
}  

function OnHelpTipComplete(result){
    var msg = result[1];
    var title;
    if (result[0] == ''){
        title = 'Help';
    } else {
        title = 'Help for "' + result[0] + '"';
    }
    
    showPopup(title, msg);
}

function OnHelpTipError(result){
    alert('Error: ' + result.get_message());
}

function fitHelpRounded(ctl_Id){
    var x;
    
    if ( !(parent.document.getElementById(ctl_Id + 'div') ))
    {
        return false;
    }

    var divh = new Array('l', 'r'); 
    var height = parent.document.getElementById(ctl_Id + 'div').clientHeight;
    for (x in divh){
        div = parent.document.getElementById(ctl_Id + 'div' + divh[x]);
        div.style.height = height + "px";
    }
    
}

function insertAtCursor(myField, myValue) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
                  + myValue
                  + myField.value.substring(endPos, myField.value.length);
  } else {
    myField.value += myValue;
  }
}