
/****************************************************************************/
/*		Following code is used all over in MRM Web Client					*/
/*																			*/
/*																			*/
/****************************************************************************/

//ModalWindow
function openModalWindow(id) {
    el = document.getElementById(id);
    el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
}

//Frame open
function openframe() {
    self.document.body.cols = "*, 200";
}

function closeframe() {
    this.top.document.body.cols = "*, 0";
}

//Window Open
function popCalendar(frmName, fldName, format, winName, event) {

    var fldValue = eval('document.' + frmName + '.elements[\'' + fldName + '\'].value');
    var popup;
    /*if(bOutlook) {
    winName = "mrmpopup";
    openframe();
    }*/
    //Updated By Hardik
    //Date :17-May-2010
    //Reason : To open popupo    
    popup = window.open('Calendar_Popup.aspx?format=' + format + '&formname=' + frmName + '&fieldname=' + fldName + '&date=' + fldValue, winName, 'height=196,width=181,menubar=0,status=0,titlebar=0,toolbar=0,scrollBars=no,resizable=no,screenX=' + (event.screenX - 180) + ',left=' + (event.screenX - 180) + ',screenY=' + event.screenY + ',top=' + event.screenY);
    popup.focus();
}

//Window Open
function popCalendarForACM(frmName, fldName, format, winName, event) {
    var fldValue = eval('document.' + frmName + '.elements[\'' + fldName + '\'].value');
    var popup;
    /*if(bOutlook) {
    winName = "mrmpopup";
    openframe();
    }*/
    //Updated By Hardik
    //Date :17-May-2010
    //Reason : To open popupo
    popup = window.open('../calendar_popup.aspx?format=' + format + '&formname=' + frmName + '&fieldname=' + fldName + '&date=' + fldValue, winName, 'height=196,width=181,menubar=0,status=0,titlebar=0,toolbar=0,scrollBars=no,resizable=no,screenX=' + (event.screenX - 180) + ',left=' + (event.screenX - 180) + ',screenY=' + event.screenY + ',top=' + event.screenY);
    popup.focus();
}




function popSystemValue(FrmName, FldName, ValType, CurVal, winName, event) {
    //var w = 200;
    //var h = 250;
    //var move = screen ? 'left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '';
    var popup;
    if (winName.length == 0) winName = 'ValueList';
    popup = window.open('SystemValueList.aspx?FrmName=' + FrmName + '&FldName=' + FldName + '&ValType=' + ValType + '&CurVal=' + CurVal, winName, 'width=200,height=250,menubar=0,status=0,titlebar=0,toolbar=0,scrollBars=no,resizable=no,screenX=' + (event.screenX - 180) + ',left=' + (event.screenX - 180) + ',screenY=' + event.screenY + ',top=' + event.screenY)
    popup.focus();
}

function popCustomValue(FrmName, FldName, Key, CurVal, winName, event) {
    var w = 285;
    var h = 357;
    var move = screen ? 'left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '';
    var popup;
    if (winName.length == 0) winName = 'ValueList';
    popup = window.open('valuelist.aspx?fld=' + FldName + '&frm=' + FrmName + '&key=' + Key + '&CurVal=' + CurVal, winName, move + ',width=285,height=357,menubar=0,status=0,titlebar=0,toolbar=0,scrollBars=no,resizable=yes'); //,screenX='+(event.screenX-180)+',left='+(event.screenX-180)+',screenY='+event.screenY+',top='+event.screenY);
    popup.focus();
}

function popOpenWindowWithMenu(win, winName, param, w, h, scroll) {
    var move = screen ? 'left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '';
    var url;
    if (param.length == 0) {
        url = win;
    } else {
        url = win + "?" + param;
    }
    window.open(url, winName, move + ",width=" + w + ",height=" + h + ",scrollBars=" + scroll + ",resizable=yes,menubar=1");
}

function popOpenWindow(win, winName, param, w, h, scroll, bResize) {
    var move = screen ? 'left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '';
    var url;
    if (param.length == 0) {
        url = win;
    } else {
        url = win + "?" + param;
    }
    if (bResize) {
        window.open(url, winName, move + ",width=" + w + ",height=" + h + ",scrollBars=" + scroll + ",resizable=yes");
    } else {
        window.open(url, winName, move + ",width=" + w + ",height=" + h + ",scrollBars=" + scroll + ",resizable=no");
    }
}

var winMain;
var winNameMain;
var wMain;
var hMain;
var scrollMain;
var bResizeMain;
var moveMain;
var windowAttribsMain;
function popOpenWindowEncrypted(win, winName, param, w, h, scroll, bResize) {
    winMain = win;
    winNameMain = winName;
    wMain = w;
    hMain = h;
    scrollMain = scroll;
    bResizeMain = bResize;
    var move = screen ? 'left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '';
    moveMain = move;
    var url;
    if (param.length == 0) {
        url = win;
        if (bResize) {
            window.open(url, winName, move + ",width=" + w + ",height=" + h + ",scrollBars=" + scroll + ",resizable=yes");
        } else {
            window.open(url, winName, move + ",width=" + w + ",height=" + h + ",scrollBars=" + scroll + ",resizable=no");
        }
    } else {        
            MRMService.EncryptQueryString(param, EncryptQueryString);
    }
}
function EncryptQueryString(EncryptedParam) {
    try {
        var url = winMain + "?" + EncryptedParam;
        if (bResizeMain) {
            window.open(url, winNameMain, moveMain + ",width=" + wMain + ",height=" + hMain + ",scrollBars=" + scrollMain + ",resizable=yes");
        } else {
            window.open(url, winNameMain, moveMain + ",width=" + wMain + ",height=" + hMain + ",scrollBars=" + scrollMain + ",resizable=no");
        }
    }
    catch (e)
    { }
}

function popOpenWindowRetObj(win, winName, param, w, h, scroll) {
    var move = screen ? 'left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '';
    var url, popup;
    if (param.length == 0) {
        url = win;
    } else {
        url = win + "?" + param;
    }
    popup = window.open(url, winName, move + ",width=" + w + ",height=" + h + ",scrollBars=" + scroll + ",resizable=yes");
    return popup;
}

function popOpenWindowAtCursor(win, winName, param, w, h, event) {
    var popup;
    //var move = '';//screen ? 'left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '';
    popup = window.open(win + "?" + param, winName, "width=" + w + ",height=" + h + ",scrollBars=1,resizable=no,screenX=" + (event.screenX - 180) + ",left=" + (event.screenX - 180) + ",screenY=" + event.screenY + ",top=" + event.screenY);
    popup.focus();
}

function OpenGridPage(sFrame) {
    window.open('mainPage.aspx', sFrame);
}

//Form Check
function isBlank(val) {
    if (val == null) { return true; }
    for (var i = 0; i < val.length; i++) {
        if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t") && (val.charAt(i) != "\n") && (val.charAt(i) != "\r")) { return false; }
    }
    return true;
}

//Delete Uploaded file
function DeleteUploadedFile(sform, sfield, smsg) {
    var w = 300;
    var h = 200;
    var sValue = eval("document." + sform).elements[sfield].value;
    if (sValue.length > 0) {
        if (confirm(smsg)) {
            var move = screen ? 'left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '';
            window.open('FileUpload.aspx?sfrm=' + sform + '&sfld=' + sfield + '&sDel=' + sValue, 'DeleteFile', move + ',width=' + w + ',height=' + h + ',scrollBars=' + scroll + ',resizable=yes');
        }
    }
}


//Open the given form and field name's value in different browser
function OpeninOtherWindow(sform, sField, winName, param, w, h, scroll) {
    var move = screen ? 'left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '';
    var sValue = eval("document." + sform).elements[sField].value;
    var popup;

    popup = window.open(sValue, winName, move + ",width=" + w + ",height=" + h + ",scrollBars=" + scroll + ",resizable=yes");
    popup.focus();
}


//Prompt user for image url
function getimageurl(sform, sField) {
    var ImageURL = prompt("Please enter the image URL (eg. http://www.acme.com/room1.gif)", eval("document." + sform).elements[sField].value)
    var imgSrc = new Image();

    if (ImageURL != null) {

        //Check if ImageURL is image file
        if ((ImageURL.toUpperCase()).indexOf(".GIF") > 0 || (ImageURL.toUpperCase()).indexOf(".JPG") > 0 || (ImageURL.toUpperCase()).indexOf(".BMP") > 0) {
            imgSrc.src = ImageURL;
            document[sField + "_image"].src = imgSrc.src;
            //eval("document." + sform).elements[sField+"_image"].src=imgSrc.src;
        } else {
            if (ImageURL != "") {
                if ((ImageURL.toUpperCase()).indexOf(".DOC") > 0) {
                    //word document
                    imgSrc.src = "images/word.gif";
                } else if ((ImageURL.toUpperCase()).indexOf(".HTM") > 0 || (ImageURL.toUpperCase()).indexOf(".HTML") > 0) {
                    //html page (Link)
                    imgSrc.src = "images/html.gif";
                } else if ((ImageURL.toUpperCase()).indexOf(".XLS") > 0) {
                    //Excell document
                    imgSrc.src = "images/excel.gif";
                } else if ((ImageURL.toUpperCase()).indexOf(".PPT") > 0) {
                    //Power Point document
                    imgSrc.src = "images/powerpoint.gif";
                } else if ((ImageURL.toUpperCase()).indexOf(".ZIP") > 0) {
                    //Zip File
                    imgSrc.src = "images/winzip.gif";
                } else {
                    //Use generic image
                    imgSrc.src = "images/document.jpg";
                }
                document[sField + "_image"].src = imgSrc.src;
            }
        }

        if (ImageURL != "") {
            eval("document." + sform).elements[sField].value = ImageURL;
        }
    }
}

/*
function PrintThisPage() 
{ 
var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
sOption+="scrollbars=yes,width=750,height=600,left=100,top=25"; 
	
if(document.getElementById('PrintContent')) {
var sHTML = document.getElementById('PrintContent').innerHTML; 
}else{
return false;
}
	   
var winPrint=window.open("blank.asp", "PrinterFriendly", sOption); 
winPrint.document.open(); 
winPrint.document.write('<html><body>'); 
winPrint.document.write(sHTML);          
winPrint.document.write('</body></html>'); 
winPrint.document.close(); 
winPrint.focus(); 
}
*/

// CaretPosition object
function CaretPosition() {
    var start = null;
    var end = null;
}

function getCaretPosition(oField) {
    // Initialise the CaretPosition object
    var oCaretPos = new CaretPosition();

    // IE support
    if (document.selection) {
        // Focus on the text box
        oField.focus();

        // This returns us an object containing
        // information about the currently selected text
        var oSel = document.selection.createRange();

        // Find out the length of the selected text
        // (you'll see why below)
        var selectionLength = oSel.text.length;

        // Move the selection start to 0 position.
        //
        // This is where it gets interesting, and this is why
        // some have claimed you can't get the caret positions
        // in IE.
        //
        // IE has no 'selectionStart' or 'selectionEnd' property,
        // so we can not get or set this value directly. We can
        // only move the caret positions relative to where they
        // currently are (this should make more sense when you read
        // the next line of code).
        //
        // Note, that even though we have moved the start
        // position on our object in memory, this is not reflected
        // in the browser until we call oSel.select() (which we're
        // not going to do here).
        //
        // Also note, the start position will never be a negative
        // number, no matter how far we try to move it back.
        oSel.moveStart('character', -oField.value.length);

        // This is where it should start to make sense. We now know
        // our start caret position is the length of the currently
        // selected text minus the original selection length
        // (think about it).
        oCaretPos.start = oSel.text.length - selectionLength;

        // Since the start of the selection is at the start of the
        // text, we know that the length of the selection is also
        // the index of the end caret position.
        oCaretPos.end = oSel.text.length;
    }
    // Firefox support
    else if (oField.selectionStart || oField.selectionStart == '0') {
        // This is a whole lot easier in Firefox
        oCaretPos.start = oField.selectionStart;
        oCaretPos.end = oField.selectionEnd;
    }

    // Return results
    return (oCaretPos);
}

function setCaretPosition(oField, iCaretStart, iCaretEnd) {
    // IE Support
    if (document.selection) {
        // Focus on the text box
        oField.focus();

        // This returns us an object containing
        // information about the currently selected text
        var oSel = document.selection.createRange();

        // Since we don't know where the caret positions
        // currently are (see comments in getCaretPosition() for
        // further information on this), move them to position 0.
        //
        // Note, the caret positions will never be a negative
        // number, no matter how far we try to move them back.
        oSel.moveStart('character', -oField.value.length);
        oSel.moveEnd('character', -oField.value.length);

        // Now we know the caret positions are at index 0, move
        // them forward to the desired position (move end caret
        // position first - actually not sure if moving start
        // caret position first affects the end caret position -
        // it might).
        //
        // Note, we allow for a null end caret position and just
        // default it to the same as the start caret position.
        if (iCaretEnd != null)
            oSel.moveEnd('character', iCaretEnd);
        else
            oSel.moveEnd('character', iCaretStart);

        oSel.moveStart('character', iCaretStart);

        // Everything thus far has just been on our object in
        // memory - this line actually updates the browser
        oSel.select();
    }
    // Firefox support
    else if (oField.selectionStart || oField.selectionStart == '0') {
        oField.selectionStart = iCaretStart;

        if (iCaretEnd != null)
            oField.selectionEnd = iCaretEnd;
        else
            oField.selectionEnd = iCaretStart;

        oField.focus();
    }
}

