﻿// JScript File
function formatPhone(elm, e) {
    var keychar;

    if (e) {
        var keynum;
        if (window.event) {
            keynum = e.keyCode
        }
        else if (e.which) {
            keynum = e.which
        }

        keychar = String.fromCharCode(keynum)
    }   

    // Allow a backspace to go through, so the user
    // can correct any typos.
    if (/[\b]/.exec(keychar) || keynum == null) {
        return true;
    } else {
        var p = elm.value + keychar;

        // Don't allow a leading 1 or 0. We also strip out all
        // non-numeric characters here to make the formatting
        // easier later on. This could be modified to allow
        // letters if you consider them valid.
        p = p.replace(/^[01]/,"");
        p = p.replace(/\D+/g, "");

        // You can easily change the formatting of the phone
        // number by editing the conditionals below.
        if (p.length > 0 && p.length < 3) {
            p = "("+p;
        }
        else if (p.length >= 3 && p.length < 7) {
            p = "("+p.substring(0,3)+") "+p.substring(3);
        }
        else if (p.length >= 7 && p.length < 10) {
            p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6);
        }
        else if (p.length) {
            p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6,10);
        }
        elm.value = p;

        return false;
    }
}

function formatCode(elm, e) {
    var keychar;

    if (e) {
        var keynum;
        if (window.event) {
            keynum = e.keyCode
        }
        else if (e.which) {
            keynum = e.which
        }

        keychar = String.fromCharCode(keynum)
    }   

    // Allow a backspace to go through, so the user
    // can correct any typos.
    if (/[\b]/.exec(keychar) || keynum == null) {
        return true;
    } else {
        var p;        
        if(/\s/.exec(keychar))
        {
            keychar=keychar.replace(/\s/,"");  
            p = elm.value + keychar;
        }
        else       
        {
            p = elm.value + keychar;        
        }
        
        
        // Don't allow a leading 1 or 0. We also strip out all
        // non-numeric characters here to make the formatting
        // easier later on. This could be modified to allow
        // letters if you consider them valid.
        //p = p.replace(/^[01]/,"");
        //p = p.replace(/\D+/g, "");
        
          
        // You can easily change the formatting of the phone
        // number by editing the conditionals below.
        if (p.length == 3) 
        {
            p = p + " " ;
        }
        
//        else if (p.length >= 3 && p.length < 7) {
//            p = "("+p.substring(0,3)+") "+p.substring(3);
//        }
//        else if (p.length >= 7 && p.length < 10) {
//            p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6);
//        }
//        else if (p.length) {
//            p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6,10);
//        }
        elm.value = p;

        return false;        
    }
}

function formatAge(e)
{
        if (navigator.appName == "Microsoft Internet Explorer")
        {
            if((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode == 8))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            if ((e.charCode >= 48 && e.charCode <= 57) || (e.charCode == 0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
}

function formatDate(elm, e) {
    var keychar;
    
    if (e) {
        var keynum;
        if (window.event) {
            keynum = e.keyCode
        }
        else if (e.which) {
            keynum = e.which
        }

        keychar = String.fromCharCode(keynum)
    }
           
    // Allow a backspace to go through, so the user
    // can correct any typos.
    if (/[\b]/.exec(keychar) || keynum == null) {
        return true;
    } else {
        var p = elm.value + keychar;

        // Don't allow a leading 1 or 0. We also strip out all
        // non-numeric characters here to make the formatting
        // easier later on. This could be modified to allow
        // letters if you consider them valid.
//        p = p.replace(/^[01]/,"");
        p = p.replace(/\D+/g, "");

        // You can easily change the formatting of the phone
        // number by editing the conditionals below.
        if (p.length > 0 && p.length < 3) {
            //p = "("+p;
        }
        else if (p.length >= 2 && p.length < 4) {
            p = p.substring(0,2)+"/"+p.substring(2);
        }
        else if (p.length >= 2 && p.length < 8) {
            p = p.substring(0,2)+"/"+p.substring(2,4)+"/"+p.substring(4);
        }
        else if (p.length) {
            p = p.substring(0,2)+"/"+p.substring(2,4)+"/"+p.substring(4,8);
        }
        elm.value = p;

        return false;
    }
}


function formatCurrency(elm, e) {
    var keychar;
    
    if (e) {
        var keynum;
        if (window.event) {
            keynum = e.keyCode
        }
        else if (e.which) {
            keynum = e.which
        }

        keychar = String.fromCharCode(keynum)
    }
    
    
           
    // Allow a backspace to go through, so the user
    // can correct any typos.
    if (/[\b]/.exec(keychar) || keynum == null) {
        return true;
    } else {
        var p = elm.value + keychar;

        // Don't allow a leading 1 or 0. We also strip out all
        // non-numeric characters here to make the formatting
        // easier later on. This could be modified to allow
        // letters if you consider them valid.
//        p = p.replace(/^[01]/,"");
        p = p.replace(/\D+/g, "");

        // You can easily change the formatting of the phone
        // number by editing the conditionals below.
        if (p.length > 0 && p.length < 3) {
            //p = "("+p;
        }
        else if (p.length >= 2 && p.length < 4) {
            p = p.substring(0,2)+"/"+p.substring(2);
        }
        else if (p.length >= 2 && p.length < 8) {
            p = p.substring(0,2)+"/"+p.substring(2,4)+"/"+p.substring(4);
        }
        else if (p.length) {
            p = p.substring(0,2)+"/"+p.substring(2,4)+"/"+p.substring(4,8);
        }
        elm.value = p;

        return false;
    }
}

/*---------------------------------------------------------*/
function CapWords(str){ 
   str = str.toLowerCase();
   var words = str.split(" "); 
   for (var i=0 ; i < words.length ; i++){ 
      var testwd = words[i]; 
      var firLet = testwd.substr(0,1); 
      var rest = testwd.substr(1, testwd.length -1) 
      words[i] = firLet.toUpperCase() + rest 
   } 
   return words.join(" ");
} 


	var theform;
	var isIE;
	var isNS;

	function detectBrowser() {
		if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) 
			theform = document.forms["Form1"];
		else 
			theform = document.Form1;
			
		//browser detection
		var strUserAgent = navigator.userAgent.toLowerCase();
		isIE = strUserAgent.indexOf("msie") > -1;
		isNS = strUserAgent.indexOf("netscape") > -1;
	}
		function FormatAmtControlCheckNull(ctl){
		var vMask ;
		var vDecimalAfterPeriod ;
		var ctlVal;
		var iPeriodPos;
		var sTemp;
		var iMaxLen 
		var ctlVal;
		var tempVal;
		ctlVal = ctl.value;
		vDecimalAfterPeriod  = 2
		iMaxLen  = ctl.maxLength;

		if (isNaN(ctlVal) || ctlVal == "") {            //this function checks for empty values. FormatAmtControl does not
			// clear the control as this is not a num
			ctl.value=""
		} else {
			ctlVal =  ctl.value;
			iPeriodPos =ctlVal.indexOf(".");
			if (iPeriodPos<0) {
				if (ctl.value.length > (iMaxLen-3)) {
					sTemp = ctl.value
					 tempVal = sTemp.substr(0,(iMaxLen-3)) + ".00";
				} else {0
					tempVal = ctlVal + ".00"
				}
			} else {
				if ((ctlVal.length - iPeriodPos -1)==1)
					tempVal = ctlVal + "0"
				if ((ctlVal.length - iPeriodPos -1)==0)
					tempVal = ctlVal + "00"
				if ((ctlVal.length - iPeriodPos -1)==2)
					tempVal = ctlVal;
				if ((ctlVal.length - iPeriodPos -1)>2){
					tempVal = ctlVal.substring(0,iPeriodPos+3);
				}
			}
			ctl.value=tempVal;
		}
	}
	function FormatAmtControl(ctl){
		var vMask ;
		var vDecimalAfterPeriod ;
		var ctlVal;
		var iPeriodPos;
		var sTemp;
		var iMaxLen 
		var ctlVal;
		var tempVal;
		ctlVal = ctl.value;
		vDecimalAfterPeriod  = 2
		iMaxLen  = ctl.maxLength;

		if (isNaN(ctlVal)) {
			// clear the control as this is not a num
			ctl.value=""
		} else {
			ctlVal =  ctl.value;
			iPeriodPos =ctlVal.indexOf(".");
			if (iPeriodPos<0) {
				if (ctl.value.length > (iMaxLen-3)) {
					sTemp = ctl.value
					 tempVal = sTemp.substr(0,(iMaxLen-3)) + ".00";
				} else {0
					tempVal = ctlVal + ".00"
				}
			} else {
				if ((ctlVal.length - iPeriodPos -1)==1)
					tempVal = ctlVal + "0"
				if ((ctlVal.length - iPeriodPos -1)==0)
					tempVal = ctlVal + "00"
				if ((ctlVal.length - iPeriodPos -1)==2)
					tempVal = ctlVal;
				if ((ctlVal.length - iPeriodPos -1)>2){
					tempVal = ctlVal.substring(0,iPeriodPos+3);
				}
			}
			ctl.value=tempVal;
		}
	}
	function HandleAmountFiltering(ctl) {
		var iKeyCode, objInput;
		var iMaxLen 
		var reValidChars = /[0-9.]/;
		var strKey;
		var sValue;
		var event = window.event || arguments.callee.caller.arguments[0];
		iMaxLen  = ctl.maxLength;
		sValue = ctl.value;
		detectBrowser();

		if (isIE) {
			iKeyCode = event.keyCode;
			objInput = event.srcElement;
		} else {
			iKeyCode = event.which;
			objInput = event.target;
		}

        //hack to allow "submission on enter"/"cancel on esc" functionality		
		if (iKeyCode == 13 || iKeyCode == 27)
		{
		    return true;
		}

		strKey = String.fromCharCode(iKeyCode);
	
		if (reValidChars.test(strKey))
		{
			if(iKeyCode==46) {
				if(objInput.value.indexOf('.')!=-1)
					if (isIE)
						event.keyCode= 0;
					 else
					 {
						if(event.which!=0 && event.which!=8)
						return false;
					 }
			} else {
				if(objInput.value.indexOf('.')==-1) {
					if (objInput.value.length>=(iMaxLen-3))
					{
						if (isIE) {
							event.keyCode= 0;
						 } else {
						 	if(event.which!=0 && event.which!=8)
							return false;
						 }
					}
				}
				if ((objInput.value.length==(iMaxLen-3)) && (objInput.value.indexOf('.')==-1)) {
					objInput.value = objInput.value +'.';
				}	
			}
		} else {
			if (isIE) {
				event.keyCode= 0;
			} else {
				if(event.which!=0 && event.which!=8)
				return false;
			}
		}
	}

	function HandleIntFiltering(ctl) {
		var iKeyCode, objInput;
		var iMaxLen 
		var reValidChars = /[0-9]/;
		var strKey;
		var sValue;
		var event = window.event || arguments.callee.caller.arguments[0];
		iMaxLen  = ctl.maxLength;
		sValue = ctl.value;
		detectBrowser();

		if (isIE) {
			iKeyCode = event.keyCode;
			objInput = event.srcElement;
		} else {
			iKeyCode = event.which;
			objInput = event.target;
		}

        //hack to allow "submission on enter"/"cancel on esc" functionality		
		if (iKeyCode == 13 || iKeyCode == 27)
		{
		    return true;
		}

		strKey = String.fromCharCode(iKeyCode);
	
		if (!reValidChars.test(strKey))
		{
			if (isIE) {
				event.keyCode= 0;
			} else {
				if(event.which!=0 && event.which!=8)
				return false;
			}
		}
	}
	
function fncSelectDropDownListText(varFieldName,varValue)
{
    //alert(document.all.item(varFieldName).length);
    varField="";    
    for(var j=0;j<document.all.item(varFieldName).length;j++)
    {
        varField=document.all.item(varFieldName).options(j).text; 
        
        if(varField==varValue)
            document.all.item(varFieldName).selectedIndex=j; 
    }
}
