﻿// JScript File

function Mid(str, start, len)
{

    if (start < 0 || len < 0) 
        return '';
    var iEnd=0;
    var iLen = String(str).length;
    if (start + len > iLen)
          iEnd = iLen;
    else
          iEnd = start + len;
    return String(str).substring(start,iEnd);
}
function InStr(strSearch, charSearchFor)
{
    for (i=0; i < strSearch.length; i++)
    {
         if (charSearchFor == Mid(strSearch, i, 1))
         {
             return i+1;
         }
    }
    return 0;
}
function Left(str, n)
{
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n)
{
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
function pKeyPressCurrency(KeyAscii, sCurrency, nAmtSize)
{
        var nDecPos=0;
        var nDollarPos=0;
        var nCurrencySize=0;
        var nKeyPos=0;
        var nActualSize=0;
        var nI=0;
        var strChar='';
        var strCurrntChar='';
        
        nKeyPos=sCurrency.length;
        nDecPos=InStr(sCurrency,".");
        nDollarPos=InStr(sCurrency,"$");
        
        nCurrencySize=sCurrency.length;
        
        if (KeyAscii==44) 
        {
            strChar=Mid(sCurrency,nKeyPos-1,1);
            strCurrntChar=String.fromCharCode(KeyAscii);
         
            if (strChar==strCurrntChar)
                return 0;
        }
        for(nI=0;nI<nCurrencySize; nI++)
        {
            if (Mid(sCurrency, nI, 1)==".")
                break;
            strChar=Mid(sCurrency, nI, 1);
            if (strChar=="-" || (strChar>="0" && strChar<="9"))
                nActualSize=nActualSize+1;
        }
        
        if (InStr(sCurrency, "$")>0)
        {
            if (nKeyPos=0)
                return 0;
        }

        if (KeyAscii==8)
            return 1;
        else if (KeyAscii==45) // For "-"
        {
            if (nKeyPos>0)
                return 0;
        }
        else if (KeyAscii==44)  // For ","
        {
            if (nDecPos>0)
            {
                if (nKeyPos>=nDecPos)
                    return 0;
            }
        }
        else if (KeyAscii==46)  // For "."
        {
            if (InStr(sCurrency,".")>0)
                return 0;
        }
        else if (KeyAscii>=48 && KeyAscii<=57)  // For 0-9
        {
            if (nDecPos>0) 
            {
                if (nKeyPos>=nDecPos) // Number entered after decimal
                {
                    if (Mid(sCurrency,nDecPos, sCurrency.length).length>1)
                        return 0;
                }
                else // Number entered before decimal, if greater then allowed size then beep
                {
                     if (nActualSize>=nAmtSize)
                        return 0;
                }
            }
            else
            {
                if (nActualSize>=nAmtSize)
                    return 0;
            }
        }
        else
            return 0;
}
function val(strValue)
{
    var IdValue=0;
    if (strValue=="" || strValue==null)
        IdValue=0;
    else
        IdValue=strValue;
        
    return IdValue;
}
function Trim(strValue)
{
    if (strValue==null)
        return '';
    var intLength=strValue.length;
    var intStringLength=strValue.length;
    var intCntr=0;
    var strSpace='';

    for (intCntr=0;intCntr<intLength;intCntr++)
    {
        strSpace=strValue.substring(0,1);
        if (strSpace!=' ')
           break;
        strValue=strValue.substring(1,intStringLength);
        intStringLength=strValue.length;
    }

    intLength=strValue.length;
    for (intCntr=intLength;intCntr>0;intCntr--)
    {
        strSpace=strValue.substring(intStringLength-1,intLength);
        if (strSpace!=' ')
           break;
        strValue=strValue.substring(0,intStringLength-1);
        intStringLength=strValue.length;
    }
    return strValue;    	
}

// 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;
     }
}
function fnClearCombo(strComboName)
{
	for (var i=document.getElementById(strComboName).options.length -1; i>=0;i--)
	{
    	document.getElementById(strComboName).options[i]=null;
	}
}
function IfNull(strValue)
{
    if (strValue==null || strValue=='')
        return 0;
    else
        return strValue;
}
function fnSetComboText(objComboControl,strValue)
{
    var intCntr=0;
    var intLength=objComboControl.options.length;
    var strTempValue='';
    
    for(intCntr=0;intCntr<intLength;intCntr++)
    {
        strTempValue=objComboControl.options[intCntr].value;
        if(strTempValue==strValue)
            break;
    }
    objComboControl.selectedIndex=intCntr;
}
function fnValidEmail(strEmailId)
{
    var intPosition=0;
    var strRestEmailString='';
     
    if(strEmailId.length!=0)
    {
        intPosition=InStr(strEmailId,'@');
        if(intPosition==0 || intPosition==strEmailId.length)
            return false;
    }
    strRestEmailString=Mid(strEmailId,(intPosition),strEmailId.length);
    intPosition=0;
    intPosition=InStr(strRestEmailString,'@');
    if(intPosition>0)
        return false;
        
    intPosition=0;
    intPosition=InStr(strRestEmailString,'.');
    if((intPosition==0) || (intPosition==1) || (intPosition==strRestEmailString.length))
        return false;

    return true;
}
function pKeyPressNumeric(KeyAscii)
{
    if ((KeyAscii==8) || (KeyAscii>=48 && KeyAscii<=57))
        return 1;
    else
        return 0;
}
function pKeyPressAlphaNumeric(KeyAscii)
{
    if ((KeyAscii==8) || (KeyAscii==32) || (KeyAscii>=48 && KeyAscii<=57) || (KeyAscii>=65 && KeyAscii<=90) || (KeyAscii>=97 && KeyAscii<=122))
        return 1;
    else
        return 0;
}

function SetProgressPosition(e) 
{
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY) {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        posx = e.clientX + document.documentElement.scrollLeft;
        posy = e.clientY + document.documentElement.scrollTop;
    }
    document.getElementById('divProgress').style.left = posx - 8 + "px";
    document.getElementById('divProgress').style.top = posy - 8 + "px";
}