﻿//browser detection
var strUserAgent = navigator.userAgent.toLowerCase(); 
var isIE = strUserAgent.indexOf("msie") > -1; 
var isNS6 = strUserAgent.indexOf("netscape6") > -1; 
var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5; 

// Manage window.onload scripts
var onloadScripts = new Array();

function onloadProcess()
{
   for(var i = 0;i < onloadScripts.length;i++) {
        eval(onloadScripts[i]);
    }
}

function onloadAdd(func){
   onloadScripts[onloadScripts.length] = func;
}

// Add onload
window.onload = onloadProcess;

/*
    Pages, tabs and lists
*/
var currenttab = "tabroot";

function initialisePage() {
    document.getElementById("linkssub").childNodes[0].childNodes[0].onclick();
}

function linkclick(ltype)
{
    greyoutContent(true);
    // Set link styles
    if (cList) {
        document.getElementById("sublink" + cList).className = "";
    }
    document.getElementById("sublink" + ltype).className = "current";
    // Set selected link
    cList = ltype;
    // set the current tab to the root
    currenttab = "tabroot";
    // Load the list
    populateList(1, "", "", "")
}

function listsuccess(value, ctx, methodName)
{
    // Check that a tab for this item exists
    if (document.getElementById("tab" + ctx.itemid)) {
        // Tab exists so remove the current content and add in the new
        var tabletoclose = document.getElementById("list" + ctx.itemid);
        tabletoclose.parentNode.removeChild(tabletoclose);
        // Create a table for the new content
        var newtable = document.createElement("div");
        newtable.innerHTML = value;
        document.getElementById("content").appendChild(newtable.firstChild);
        
        // If a form is currently visible set the tab style to not current
        if (document.getElementById("tab" + ctx.itemid).getAttribute("visiblecontent").substring(0, 4) == "form") {
            document.getElementById("tab" + document.getElementById("tab" + ctx.itemid).getAttribute("visiblecontent")).className = "";
        }

        // Set the visible content
        document.getElementById("tab" + ctx.itemid).setAttribute("visiblecontent", "list" + ctx.itemid);
        
        // Show the search form
        document.getElementById("listsearch").className = "";

        // Set search term
        if (ctx.searchterm) {
            document.getElementById(currenttab).setAttribute("searchterm", ctx.searchterm);
        }
        // Show the tab
        showtab("tab" + ctx.itemid);
    }
    else {
        // Tab does not exist - if this is the root then add to content div otherwise create a new div and tab
        if (ctx.itemid == "") {
            document.getElementById("content").innerHTML = value;
            // Set the visible content
            document.getElementById("tabroot").setAttribute("visiblecontent", "listroot");
        }
        else {
            // Create a new tab
            var newtab = document.createElement("li");
            newtab.id = "tab" + ctx.itemid;
            newtab.className = "current contain";
            
            var title = "";
            if (ctx.itemstatus && ctx.itemstatus == "b") {
                title = " set-up";
            }
            
            newtab.innerHTML = "<a href=\"#\" onclick=\"showtab('tab" + ctx.itemid + "');\">" + ctx.itemname + title + "</a><a href=\"#\" class=\"close\" onclick=\"closetab('tab" + ctx.itemid + "');\"><img src=\"/_resources/files/icons/close-icon.gif\" alt=\"Close\" title=\"Close Tab\" /></a>";
            document.getElementById("listtabs").appendChild(newtab);
            
            // Create a table for the content
            var newtable = document.createElement("div");
            newtable.innerHTML = value;
            document.getElementById("content").appendChild(newtable.firstChild);

            // Create a ul for the forms tabs and add to the forms container
            var newforms = document.createElement("ul");
            newforms.id = "tab" + ctx.itemid + "forms";
            newforms.className = "hidden";
            document.getElementById("formtabs").appendChild(newforms);
            
            // Hide the current forms tab container
            document.getElementById(currenttab + "forms").className = "hidden";
            
            // Hide the current table
            document.getElementById(currenttab.replace("tab", "list")).className = "hidden";
            
            // Set new tab as current
            document.getElementById(currenttab).className = "contain";
            currenttab = "tab" + ctx.itemid;

            // Set the visible content
            document.getElementById("tab" + ctx.itemid).setAttribute("visiblecontent", "list" + ctx.itemid);
        }
        // Set search term
        if (ctx.searchterm) {
            document.getElementById(currenttab).setAttribute("searchterm", ctx.searchterm);
            document.getElementById("txtsearchterm").value = document.getElementById(currenttab).getAttribute("searchterm");
        }
        else if (document.getElementById("txtsearchterm")) {
            document.getElementById("txtsearchterm").value = "";
        }
    }
    // Hide the loading notice
    if (ctx.greyoutcontent == "true") {
        greyoutContent(false);
    }
    // Set item
    document.getElementById(currenttab).setAttribute("itemid", ctx.itemid);
    document.getElementById(currenttab).setAttribute("itemname", ctx.itemname);
    // hide the search box
    if (document.getElementById(currenttab).getAttribute("visiblecontent") != "listroot") {
        document.getElementById("listsearch").className = "hidden";
    }
}

function showtab(tabid, tabclick) {

    // If this is a tab click set the visible content to the list
    if (tabclick == 'true' && document.getElementById(tabid).getAttribute("visiblecontent") != "list" + tabid.replace("tab", "")) {
        // Set the tab for the currently visible content to not current
        document.getElementById("tab" + document.getElementById(tabid).getAttribute("visiblecontent")).className = "";
        // hide the currently visible content
        document.getElementById(document.getElementById(tabid).getAttribute("visiblecontent")).className = "hidden";
        // Set the visible content to the list
        document.getElementById(tabid).setAttribute("visiblecontent", "list" + tabid.replace("tab", ""))
    }

    // Hide the current tab
    document.getElementById(currenttab).className = "contain";
    document.getElementById(document.getElementById(currenttab).getAttribute("visiblecontent")).className = "hidden";

    // Show the selected one
    document.getElementById(tabid).className = "current contain";
    document.getElementById(document.getElementById(tabid).getAttribute("visiblecontent")).className = "";
    
    // Hide the current forms container
    document.getElementById(currenttab + "forms").className = "hidden";
    // Show the selected one if it has children
    if (document.getElementById(tabid + "forms").childNodes.length > 0) {
        document.getElementById(tabid + "forms").className = "contain";
    }

    // Set the selected tab
    currenttab = tabid;
    
    // If displaying a list show the search box
    if (document.getElementById(tabid).getAttribute("visiblecontent") == "listroot" && document.getElementById("listsearch")) {
        document.getElementById("listsearch").className = "";
    }
    else if (document.getElementById("listsearch")) {
        document.getElementById("listsearch").className = "hidden";
    }
    
    // Set the search term
    if (document.getElementById(currenttab).getAttribute("searchterm")) {
        document.getElementById("txtsearchterm").value = document.getElementById(currenttab).getAttribute("searchterm");
    }
    else if (document.getElementById("txtsearchterm")) {
        document.getElementById("txtsearchterm").value = "";
    }
}

function closetab(tabid) {
    // If we are removing the current tab set the previous one as the current
    if (tabid == currenttab) {
        var ctab = document.getElementById(currenttab).previousSibling;
        showtab(ctab.id);
    }
    // Remove the selected tabs table
    var tabletoclose = document.getElementById(tabid.replace("tab", "list"));
    tabletoclose.parentNode.removeChild(tabletoclose);
    // Remove the selected tab
    var tabtoclose = document.getElementById(tabid);
    tabtoclose.parentNode.removeChild(tabtoclose);
    
    // Remove any open forms
    var i;
    for (i = 0; i < document.getElementById(tabid + "forms").childNodes.length; i++) {
        document.getElementById("content").removeChild(document.getElementById(document.getElementById(tabid + "forms").childNodes[i].id.replace("tab", "")))
    }
    // Remove the forms container ul for this tab
    document.getElementById("formtabs").removeChild(document.getElementById(tabid + "forms"))
}

// Common Search Function
function searchList(cpage) {

    // Check for a tab
    if (!document.getElementById(currenttab)) {
        return false;
    }
    // If a page isn't passed then try and get from the tab
    if (!cpage) {
        if (document.getElementById(currenttab).getAttribute("page")) {
            cpage = document.getElementById(currenttab).getAttribute("page");
        }
        else {
            cpage = 1;
        }
    }
    
    var itemid, itemname;
    // If the item details aren't passed try and get them from the tab
    if (document.getElementById(currenttab).getAttribute("itemid")) {
        itemid = document.getElementById(currenttab).getAttribute("itemid");
        itemname = document.getElementById(currenttab).getAttribute("itemname");
    }
    else {
        itemid = "root";
        itemname = "";
    }
    // Get the search term
    var searchterm = "";
    if (document.getElementById("txtsearchterm")) {
        searchterm = document.getElementById("txtsearchterm").value.toLowerCase();
    }
    // Set the search term on the current tab
    document.getElementById(currenttab).setAttribute("searchterm", searchterm);
    // Set the page on the current tab
    document.getElementById(currenttab).setAttribute("page", cpage);
    // Call the populate list function
    populateList(cpage, itemid, itemname, searchterm);
}

/*
    Forms
*/

function formsuccess(value, ctx, methodName) {
    // Add a tab to the form tab container if one does not already exist
    if (!document.getElementById("tabform" + ctx.ftype.replace(/\s/g, "").toLowerCase() + ctx.itemid)) {
        // If this is the first form to be opened on this tab add a list tab
        if (document.getElementById(currenttab + "forms").childNodes.length == 0) {
            var pagelisttab = document.createElement("li");
            pagelisttab.id = "tabformroot" + currenttab.replace("tab", "").toLowerCase();
            pagelisttab.className = "pagelist";
            pagelisttab.innerHTML = "<a href=\"#\" onclick=\"showtab('" + currenttab + "', 'true');\">" + ctx.itype + " List</a>";
            document.getElementById(currenttab + "forms").appendChild(pagelisttab);
        }
        // Create a tab
        var newtab = document.createElement("li");
        newtab.id = "tabform" + ctx.ftype.replace(/\s/g, "").toLowerCase() + ctx.itemid;
        newtab.className = "current";
        var tabtitle;
        if (ctx.itemid == "add") {
            tabtitle = "Add " + ctx.ftype;
        }
        else if (ctx.itemname.toLowerCase() == "report") {
            tabtitle = ctx.ftype;
        }
        else if (ctx.itemname.toLowerCase().indexOf("valuation") > -1 ) {
            tabtitle = ctx.itemname;
        }
        else {
            tabtitle = "Edit " + ctx.ftype + " - " + ctx.itemname;
        }
        newtab.innerHTML = "<a href=\"#\" onclick=\"showformtab('tabform" + ctx.ftype.replace(/\s/g, "").toLowerCase() + ctx.itemid + "');\">" + tabtitle + "</a><a href=\"#\" class=\"close\" onclick=\"closeformtab('tabform" + ctx.ftype.replace(/\s/g, "").toLowerCase() + ctx.itemid + "');\"><img src=\"/_resources/files/icons/close-icon.gif\" alt=\"Close\" title=\"Close Tab\" /></a>";
        document.getElementById(currenttab + "forms").appendChild(newtab);
        // Show the tabs
        document.getElementById(currenttab + "forms").className = "contain";
        
        // Create a form container for this form
        var newform = document.createElement("div");
        newform.id = "form" + ctx.ftype.replace(/\s/g, "").toLowerCase() + ctx.itemid;
        newform.className = "form contain";
        newform.innerHTML = value;
        document.getElementById("content").appendChild(newform);
        
        // Hide the table and search box
        document.getElementById(document.getElementById(currenttab).getAttribute("visiblecontent")).className = "hidden";
        if (document.getElementById("listsearch")) {
            document.getElementById("listsearch").className = "hidden";
        }
        
        // Set the visible content
        document.getElementById(currenttab).setAttribute("visiblecontent", "form" + ctx.ftype.replace(/\s/g, "").toLowerCase() + ctx.itemid);
    }
    else {
        // for valuations and reports close and reopen the table
        if (ctx.ftype.toLowerCase().indexOf("valuation") > -1 || ctx.itemname.toLowerCase() == "report") {
            // Tab exists so remove the current content and add in the new
            var tabletoclose = document.getElementById("form" + ctx.ftype.replace(/\s/g, "").toLowerCase() + ctx.itemid);
            tabletoclose.parentNode.removeChild(tabletoclose);
            // Create a table for the new content
            var newtable = document.createElement("div");
            newtable.innerHTML = value;
            document.getElementById("content").appendChild(newtable.firstChild);
        }
        showformtab("tabform" + ctx.ftype.replace(/\s/g, "").toLowerCase() + ctx.itemid);
    }
    // Hide the loading notice
    if (ctx.greyoutcontent == "true") {
        greyoutContent(false);
    }
    else if (ctx.greyout) {
        greyOut(false,ctx.greyout);
    }
}

function showformtab(ftid) {
    // Hide the current tab
    document.getElementById(document.getElementById(currenttab).getAttribute("visiblecontent")).className = "hidden";
    // If this is a form then set the tab to unselected
    if (document.getElementById(currenttab).getAttribute("visiblecontent").substring(0, 4) == "form") {
        document.getElementById("tab" + document.getElementById(currenttab).getAttribute("visiblecontent")).className = "";
    }
    // Hide the search box
    if (document.getElementById("listsearch")) {
        document.getElementById("listsearch").className = "hidden";
    }

    // Show the selected tab and form
    document.getElementById(ftid).className = "current contain";
    document.getElementById(ftid.replace("tab", "")).className = "";
    
    // Set the selected content
    document.getElementById(currenttab).setAttribute("visiblecontent", ftid.replace("tab", ""));
}

function closeformtab(formtabid) {

    // If we are removing the current tab set the previous one as the current
    if (formtabid == "tab" + document.getElementById(currenttab).getAttribute("visiblecontent")) {
        var cftab = document.getElementById("tab" + document.getElementById(currenttab).getAttribute("visiblecontent")).previousSibling;
        if (cftab.id.substring(0, 11) == "tabformroot") {
            showtab(currenttab, 'true');
        }
        else {
            showformtab(cftab.id);
        }
    }
    // Remove the selected tabs form
    var formtoclose = document.getElementById(formtabid.replace("tab", "").toLowerCase());
    formtoclose.parentNode.removeChild(formtoclose);
    // Remove the selected tab
    var formtabtoclose = document.getElementById(formtabid);
    formtabtoclose.parentNode.removeChild(formtabtoclose);
    // If there is only the page list tab left then remove it
    if (document.getElementById(currenttab + "forms").childNodes.length == 1) {
        document.getElementById("tabformroot" + currenttab.replace("tab", "")).parentNode.className = "hidden";
        document.getElementById("tabformroot" + currenttab.replace("tab", "")).parentNode.removeChild(document.getElementById("tabformroot" + currenttab.replace("tab", "")));
    }
}

/*
    Tools
*/
function showtools(o) {
    // Show the tools container
    o.nextSibling.className = "";
}

var toolsTimeout, toolsobj;
function hidetools(o, forced) {
    if (forced == 'true') {
        // Forcing closed so close the tools container now and ensure timeout is null (in ie the tools container stays open when we open a new tab)
        o.firstChild.nextSibling.className = "hidden";
        toolsTimeout = undefined;
    }
    else {
        // Set a timeout to close if the container is not already closed
        if (toolsTimeout == undefined && o.firstChild.nextSibling.className != "hidden") {
            toolsobj = o;
            toolsTimeout = window.setTimeout(function(){
                o.firstChild.nextSibling.className = "hidden";
                toolsTimeout = undefined;
            }, 500);
        }
    }
}

function keeptools(o) {
    // Keep the tools container open
    if (toolsTimeout != undefined && o == toolsobj) {
        window.clearTimeout(toolsTimeout);
        toolsTimeout = undefined;
    }
}

// Diable form items
function disableform(o) {

    var inputs = o.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; i++) {
        inputs[i].readOnly = true;
        inputs[i].disabled = true;
    }
    var texts = o.getElementsByTagName("TEXTAREA");
    for (var j = 0; j < texts.length; j++) {
        texts[j].readOnly = true;
        texts[j].disabled = true;
    }
    var selects = o.getElementsByTagName("SELECT");
    for (var k = 0; k < selects.length; k++) {
        selects[k].readOnly = true;
        selects[k].disabled = true;
    }
}
// Enable form items
function enableform(o) {

    var inputs = o.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; i++) {
        inputs[i].readOnly = false;
        inputs[i].disabled = false;
    }
    var texts = o.getElementsByTagName("TEXTAREA");
    for (var j = 0; j < texts.length; j++) {
        texts[j].readOnly = false;
        texts[j].disabled = false;
    }
    var selects = o.getElementsByTagName("SELECT");
    for (var k = 0; k < selects.length; k++) {
        selects[k].readOnly = false;
        selects[k].disabled = false;
    }
}

// Tracker options
var optTimeout;
function trackeroptions() {
    document.getElementById("trackeroptionslist").className = "";
}

function hidetrackeroptions() {
    optTimeout = window.setTimeout("document.getElementById(\"trackeroptionslist\").className = \"hidden\";", 500);
}

function showtrackeroptions() {
    if (optTimeout != undefined) {
        window.clearTimeout(optTimeout);
    }
}

function changetracker(tid, ctype) {
    PageMethods.Change_Tracker(tid, ctype, changetrackersuccess, ajaxfailure);
}

function changetrackersuccess(value, ctx, methodName)
{
    // Set the status
    window.location.reload();
}

// Sign out
function signout() {
    PageMethods.User_Signout(signoutsuccess, ajaxfailure);
}

function signoutsuccess(value, ctx, methodName) {
    window.location.href = "/default.aspx?so=True";
}

/*
    Cookie scripts
*/
function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

// Paging
function paging(cpage, clist) {
    eval(clist + "(" + cpage + ")");
}

/* Key Press */
function keypressmanager(func, e) {

    var iKeyCode, strKey;
    
    if (!e) var e = window.event;

    if (isIE) {
        iKeyCode = e.keyCode;
    }
    else {
        iKeyCode = e.which;
    }

    if (iKeyCode == 13) {
        eval(func);
        return false;
    }
    
    return true;

}

/*
    AJAX Method Failure
*/
function ajaxfailure(ex, ctx, methodName) { 
    if (ex.get_message() == "Session Timeout" || ex.get_message() == "Authentication failed.") {
        window.location.href = "/default.aspx?to=true";
        return false;
    }
    // If this is a server method failed message do not alert the user. Occurs in FF when changing pages before data loads
    //if (!(ex.get_message().startswith("The server method '") && ex.get_message().endswith("' failed."))) {
        alert(ex.get_message()); 
    //}
    // get_stackTrace(), get_message(), 
    // get_statusCode(), get_timedOut() 

    // show lists and tools and hide add form
    /*
    switch (methodName) {
        case "Signin_User":
            enableform(document.getElementById("signinform"));
    }
    */
    // Hide any passed loader animations
    if (ctx) {
        if (ctx.greyoutcontent == "true") {
            greyoutContent(false);
        }
        if (ctx.greyout) {
            greyOut(false,ctx.greyout);
        }
        if (ctx.divid) {
            enableform(document.getElementById(ctx.divid));
        }
        if (ctx.animid) {
            document.getElementById(ctx.animid).className = "hidden";
        }
    }
} 

// Grey out page
function greyoutContent(vis) {

  var cdark = document.getElementById('greyoutcontent');
  var ldark = document.getElementById('greyoutlinks');

  if (vis) {
    //set the shader to cover the entire page and make it visible.
    cdark.style.opacity = 0.7;                      
    cdark.style.MozOpacity = 0.7;                   
    cdark.style.filter = 'alpha(opacity=' + 70 + ')'; 
    cdark.className = 'greyout';		
    		 
    ldark.style.opacity = 0.7;                      
    ldark.style.MozOpacity = 0.7;                   
    ldark.style.filter = 'alpha(opacity=' + 70 + ')'; 
    ldark.className = 'greyout';		
  } else {
     cdark.className = 'hidden';
     ldark.className = 'hidden';
  }
}

function greyoutScreen(vis) {

  var cdark = document.getElementById('greyoutscreen');

  if (vis) {
    //set the shader to cover the entire page and make it visible.
    cdark.style.opacity = 0.7;                      
    cdark.style.MozOpacity = 0.7;                   
    cdark.style.filter = 'alpha(opacity=' + 70 + ')'; 
    cdark.className = 'greyout';		
  } else {
     cdark.className = 'hidden';
  }
}

function greyOut(vis, divid) {

    if (!divid) {
        divid = "greyout";
    }

    var cdark = document.getElementById(divid);

    if (vis) {
        //set the shader to cover the entire page and make it visible.
        cdark.style.opacity = 0.7;                      
        cdark.style.MozOpacity = 0.7;                   
        cdark.style.filter = 'alpha(opacity=' + 70 + ')'; 
        cdark.className = 'greyout';		
    } else {
        cdark.className = 'hidden';
    }
}

// Positioning
// get the object position
function getPosition (o, pid)
{
    // Get the passed object
    var cobj = o;
    
    // Find the left position
	var curleft = 0;
	if (o.offsetParent)
	{
		while (o.offsetParent && o.id != pid)
		{
			curleft += o.offsetLeft;
			o = o.offsetParent;
		}
	}
	else if (o.x) {
		curleft += o.x;
    }

    // reset the object to the passed object
    o = cobj;
    
    // Find the top position
	var curtop = 0;
	if (o.offsetParent)
	{
		while (o.offsetParent && o.id != pid)
		{
			curtop += o.offsetTop
			o = o.offsetParent;
		}
	}
	else if (o.y) {
		curtop += o.y;
	}

    // return the position
	return {x:curleft, y:curtop}
}

/*
    Exports
*/
function exportdata(dtype, did) {
    window.open("/tracker/exports.aspx?dtype=" + dtype + "&did=" + did);
}

/*
    New Password
*/
function showpasswordform() {
    greyoutScreen(true);
    document.getElementById("passwordform").className = "changepassword";
}

function changepassword() {

    var pass, cpass;
    pass = document.getElementById("newpassword").value;
    cpass = document.getElementById("confirmpassword").value;
    // Validate form
    var valForm = new Array();
    if (pass == "") {
        valForm[valForm.length] = '\r\n New pasword is a required field';
    }
    if (cpass == "") {
        valForm[valForm.length] = '\r\n Confirm password is a required field';
    }
    if (cpass != pass) {
        valForm[valForm.length] = '\r\n Passwords do not match';
    }

    // Check for invalid entries
    if (valForm.length > 0) {
        alert('Some errors occurred in your submission: \r\n' + valForm.toString());
    }
    else {
        // Show the loading notice
        disableform(document.getElementById("passwordform"));
        document.getElementById("cpajaxloader").className = "left";

        // Save details
        var ctx = {
            divid: "passwordform",
            animid: "cpajaxloader"
        };
        PageMethods.Change_Password(pass, changepasswordsuccess, ajaxfailure, ctx);
    }

}

function changepasswordsuccess(value, ctx, methodName) { 
    // Get the response value
    greyoutScreen(false);
    document.getElementById("passwordform").className = "hidden";
} 


/*
    Lookup Property Codes
*/

function lookuppropertycodes(type) {
    document.getElementById(type + "propertylookup").className = "";
}

function findpropertycodes(type) {
    greyOut(true, "greyoutpropertylookup" + type);
    if (document.getElementById("txt" + type + "PLAddress").value == "") {
        alert("Please enter the address you are looking for")
        greyOut(false, "greyoutpropertylookup" + type);
    }
    var ctx = {
        greyout: "greyoutpropertylookup" + type,
        type: type
    };
    PageMethods.Property_Code_Find(document.getElementById("txt" + type + "PLAddress").value, findpropertycodessuccess, ajaxfailure, ctx);
}

function findpropertycodessuccess(value, ctx, methodName) {
    document.getElementById(ctx.type + "propertylookupresults").innerHTML = value;
    greyOut(false, "greyoutpropertylookup" + ctx.type);
}

/*
    Form scripts
*/
// Submit form keypress
function submitform(objEvent, btnid)
{

    var iKeyCode, strKey;

    if (isIE) {
        iKeyCode = objEvent.keyCode;
    }
    else {
        iKeyCode = objEvent.which; 
    }

    if (iKeyCode == 13) {
        document.getElementById(btnid).click();
        return false;
    }
    
    return true;

}

// Ends With
if (!String.prototype.endswith) {
  String.prototype.endswith = function(suffix) {
    var startPos = this.length - suffix.length;
    if (startPos < 0) {
      return false;
    }
    return (this.lastIndexOf(suffix, startPos) == startPos);
  };
}
// Starts With
if (!String.prototype.startswith) {
  String.prototype.startswith = function(prefix) {
    return (this.indexOf(prefix) == 0);
  };
}

