// Keep user from entering more than maxLength characters
function doKeypress(control) {
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
    if (maxLength && value.length > maxLength - 1) {
        event.returnValue = false;
        maxLength = parseInt(maxLength);
    }
}
// Cancel default behavior
function doBeforePaste(control) {
    maxLength = control.attributes["maxLength"].value;
    if (maxLength) {
        event.returnValue = false;
    }
}
// Cancel default behavior and create a new paste routine
function doPaste(control) {
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
    if (maxLength) {
        event.returnValue = false;
        maxLength = parseInt(maxLength);
        var oTR = control.document.selection.createRange();
        var iInsertLength = maxLength - value.length + oTR.text.length;
        var sData = window.clipboardData.getData("Text").substr(0, iInsertLength);
        oTR.text = sData;
    }
}

// left trim
function ltrim(str) {
    return str.replace(/^\s+/, '');
}

// right trim
function rtrim(str) {
    return str.replace(/\s+$/, '');
}

// trim
function trim(str) {
    return str.replace(/^\s+|\s+$/g, '');
}

// pad left
function PadLeft(val, ch, num) {
    var re = new RegExp(".{" + num + "}$");
    var pad = "";
    if (!ch) ch = " ";
    do {
        pad += ch;
    } while (pad.length < num);
    return re.exec(pad + val)[0];
}

// pad right
function PadRight(val, ch, num) {
    var re = new RegExp("^.{" + num + "}");
    var pad = "";
    if (!ch) ch = " ";
    do {
        pad += ch;
    } while (pad.length < num);
    return re.exec(val + pad)[0];
}

// find control
function FindControl(ObjectName, ObjectType)
{
	var Control = null;
	var ObjectsArray = document.getElementsByTagName(ObjectType);
	var ObjectsFound = ObjectsArray.length;

	if(ObjectsFound > 0)
	{
	    for (var i=0; i < ObjectsFound; i++)
	    {
		    var ObjectID = ObjectsArray[i].id;
        		    
		    if (ObjectID.length > 0)
		    {				    
			    if (ObjectID.toLowerCase().substring(ObjectID.length - ObjectName.length) == ObjectName.toLowerCase())
			    {
				    Control = ObjectsArray[i];
				    break;
			    }	
		    }	
	    }	
	}
		
    return Control;
}

function Submit(e, ButtonName)
{	
    var KeyCode = (window.event) ? event.keyCode : e.which;
    var KeyChar = (window.event) ? String.fromCharCode(event.keyCode) : String.fromCharCode(e.which);
            
    if (KeyCode == 13)
    {   
        var btnSubmit = FindControl(ButtonName, "input");
        
        if(btnSubmit != null)
        {
             btnSubmit.click();
        }
        
        if (window.event)
        {
            window.event.returnValue = false;
        }
        else
        {
            e.preventDefault();
        }
    }
}

function PopUp(PageName, Width, Height)
{
    var MyWindow = window.open(PageName, "PopUp", "menubar=0,location=0,status=0,resizable=0,scrollbars=1,width=" + Width + ",height=" + Height);
    MyWindow.focus();
    return false;
}

function ShowModal(ID) {
    var Modal = $find(ID);
    Modal.show();
}

function HideModal(ID) {
    var Modal = $find(ID);
    Modal.hide();
}
