﻿// JScript File
     /*this function can be called by the onKeyDown event of a textbox, 
	and will trigger a specific button to fire when enter key is pressed*/
	function fnTrapKD(btn, event)
	{
	try 
	{
		if (event.keyCode == 13)
		{
			event.returnValue=false;
			event.cancel = true;
			if ( btn )
			{
				if ( btn.setActive )
				   btn.setActive();
				if ( btn.click )
				   btn.click();
			}
		}
	}
	catch (er)
	{
	;
	}
	}//fnTrapKD
   
   
    var debug=0;
    
    function log(s) {
        if (debug==0) {
            return;
        }
        try {
            if (Components.classes) {
                var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
                consoleService.logStringMessage(msg);
            } else {
                alert(s);
            }
        } catch(e) {
            alert(s);
        }
        
    }    
    function populateFrame(u) {
        try {
            $get("containerFrame").src=u;
        } catch(ee) {
            try {
                document.getElementById('containerFrame').src=u;
            } catch (e) {
                try {
                    document.getElementById('containerFrame').setAttribute("src",u);
                } catch (e2) {
                    location.href=u;
                }
            }
        }    
    }
        
    function onWSError(msg,context,functionName) {
        var s=msg.get_message();
        checkSessionExpired(s);
        try {
            if (msg.get_timedOut()) {
                // timed out.
                alert("Timed out calling: " + functionName  + ", please report the following message to support: " + s);
            } else {
                alert("Error calling: " + functionName  + ", please report the following message to support: " + s);
            }
        } catch(e) {
            alert(e);
        }
    }
    
    function onLoadComplete(result,id) {
        try {
            $get(id).innerHTML=result;
        } catch (e) {
            alert(e);
        }
    }
    
    function addDropDownEntryComplete(res) {
        try {
            var o = document.createElement("option");
            o.text=res.newName;
            o.value=res.newValue;
            try {
                $get(res.dropDownId).disabled=false;
            } catch (e3) {
            }
            try {
                $get(res.dropDownId).add(o,null);
            } catch (e2) {
                // IE is brain dead.
                try {
                    $get(res.dropDownId).add(o);
                } catch (e3) {
                    alert("Failed, to add new value: " + e3);
                }
            }
        } catch (e) {
            alert("Error in completing the addition of new value: " + e);
        }
        
    }
        
    function addDropDownEntry(tableName,dropDownId,keyName,keyValue) {
        var name=prompt("Please enter new value:","");
        if (name!=null && name!="") {
            var p=new populateDropDownCall();
            p.dropDownId=dropDownId;
            p.tableName=tableName;
            p.newName=name;
            p.keyName="";
            
            if ((keyValue==null)||(keyValue==undefined)||(keyValue=='undefined')) {
                p.keyValue=0;
            } else {
                p.keyName=keyName;
                p.keyValue=keyValue;
            }
            p.sequence=0;
            
            var res=CRMWS.addDropDownEntry(p,addDropDownEntryComplete,onWSError);
        } else {
        }  
    }
    
    
        
        
   function findElement( id, tagName ) {
        // see if we can get it simply
        var elem = getElementById(id);
        if ( elem != null ) { return elem; }

        // find it if it’s in a naming container
        var elems = document.getElementsByTagName(tagName);
        var elemId;
        for (var i = 0; i <= elems.length; i++) {
            elem = elems[i];
            if ( (elem.attributes) && (elem.attributes["id"])) {
                elemId = elem.attributes["id"].value;
                if (elemId.match(id) != null) { return elem; }
            }
        }
   }
   
   function jsDate2MaskedDate(timestamp) {
    if (timestamp) {
        var m = 1+ timestamp.getMonth();
        if ( m<10) {m="0" + m};
        return (m +"/" + timestamp.getDate() + "/" + timestamp.getFullYear());
    } else {
        return "";
    }
    
  }
  
  function mysqlTimeStampToDate(timestamp) {
    //function parses mysql datetime string and returns javascript Date object
    //input has to be in this format: 2007-06-05 15:26:02
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1],parts[2],parts[3],parts[4],parts[5]);
  }
  
  function loadDropDownComplete(result,dd) {
    var i=0;
    var dropDown=$get(dd);
    try {
        try {
            dropDown.disabled=false;
        } catch (e3) {
        }
         try {
             if (clearDropDown) {
                clearDropDown(dd);
             }
         } catch(ex) {
         }
         
        if (result==null)     {
            return;
        } else {
            // add a default drop down item.
            addDropDownItem(dropDown,"","( select )");
            var def=dropDown.defaultvalue||dropDown.getAttribute("defaultvalue");
            
            for (i=0;i<result.length;i++) {
                addDropDownItem(dropDown,result[i].value,unescape(result[i].caption,def));     
                if (def==result[i].value) {
                    dropDown.selectedIndex=i+1;
                }
            }
        }
        
    } catch (e) {
        alert("LoadDropDownComplete: " + e);
    }
    
  }
  
  function addDropDownItem(dropDown,value,caption,def) {
    var o = document.createElement("option");
    o.text=caption;
    o.value=value;
    o.selected=value==def;
     try {
        dropDown.add(o,null);
     } catch (e2) {
        // IE is brain dead.
        try {
            dropDown.add(o);
        } catch (e3) {
         alert("Failed, to add new value: " + dropDown.id + " : " + e3);
        }
     }
  }
  
  function checkSessionExpired(s) {
    if (s=="Session Expired") {
        location.href="<%#db.localPrefix%>logout.aspx";
    }
  }
  
  
  // custom events.
  function Event() {
    this.eventHandlers=new Array();
  }
  
  Event.prototype.addHandler = function (eventHandler) {
    this.eventHandlers.push(eventHandler);
  }
  
  Event.prototype.raise = function (args) {
    for(var i=0;i<this.eventHandlers.length;i++) {
        this.eventHandlers[i](args);
    }
  }
  
  if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();