﻿//---------------------------------------------------------------------------------------------------------------------
//----------------------------------------Number Formatter by On3Galleries.com----------------------------------------
//----------------------------------------  You are free to use this in your   ----------------------------------------
//----------------------------------------  scripts, but you must leave this   ----------------------------------------
//----------------------------------------   Copyright notice intact. Enjoy!   ----------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------- You can call this function with only----------------------------------------
//----------------------------------------   the 'num' object (reference your  ----------------------------------------
//----------------------------------------   field you want to format). If no  ----------------------------------------
//----------------------------------------formats are sent, it will use default----------------------------------------
//----------------------------------------             formats.                ----------------------------------------
//---------------------------------------------------------------------------------------------------------------------
// Modifications by Jarrett Fisher & Alfonzo Carson Precis-EBuisness

function trimNumber(s) {
    while (s.substr(0, 1) == '0' && s.length > 1) { s = s.substr(1, 9999); }
    return s;
}

function FormatPhoneNumber(num, format, shortformat) {

    num.value = num.value.replace("(", "");
    num.value = num.value.replace(")", "");
    num.value = num.value.replace("-", "");
    num.value = num.value.replace(" ", "");

    if (isNaN(num.value)) {
        alert("This Field Only Accepts Numeric Values");
        num.focus();
    }
    else {

        if (NumberContainsInvalidDecimal(num.value, format)) {
            alert("This Field Only Accepts Whole Numbers");
            num.focus();
            return false;
        }

        if (NumberContainsInvalidDollarSymbol(num.value, format)) {
            alert("The Value Entered Contains An Invalid Dollar Symbol");
            num.focus();
            return false;
        }

        if (NumberContainsInvalidParen(num.value, format)) {
            alert("The Value Entered Contains An Invalid Paren");
            num.focus();
            return false;
        }
         if (NumberContainsInvalidDash(num.value, format)) {
            alert("The Value Entered Contains An Invalid Dash");
            num.focus();
            return false;
        }
        
        num.value = trimNumber(num.value);
        FormatNumber(num, format, shortformat);

    }
}

function FormatMoney(num, format, shortformat) {

    num.value = num.value.replace(/,/g, "");
    num.value = num.value.replace("$", "");
    if (num.value == "0.00" || num.value == ".00") num.value = "0";
    if (isNaN(num.value)) {
        alert("This Field Only Accepts Numeric Values");
        num.focus();
    }
    else {

        if (NumberContainsInvalidDecimal(num.value, format)) {
            alert("This Field Only Accepts Whole Numbers");
            num.focus();
            return false;
        }

        if (NumberContainsInvalidDollarSymbol(num.value, format)) {
            alert("The Value Entered Contains An Invalid Dollar Symbol");
            num.focus();
            return false;
        }

        if (NumberContainsInvalidParen(num.value, format)) {
            alert("The Value Entered Contains An Invalid Paren");
            num.focus();
            return false;
        }
           if (NumberContainsInvalidDash(num.value, format)) {
            alert("The Value Entered Contains An Invalid Dash");
            num.focus();
            return false;
        }
        FormatNumber(num, format, shortformat);

    }
}

function FormatNumberEx(num, format, shortformat) {

    num.value = num.value.replace(/,/g, "");
    num.value = num.value.replace(" ", "");
    
    if (isNaN(num.value)) {
        alert("This Field Only Accepts Numeric Values");
        num.focus();
    }
    else {

        if (NumberContainsInvalidDecimal(num.value, format)) {
            alert("This Field Only Accepts Whole Numbers");
            num.focus();
            return false;
        }

        if (NumberContainsInvalidDollarSymbol(num.value, format)) {
            alert("The Value Entered Contains An Invalid Dollar Symbol");
            num.focus();
            return false;
        }

        if (NumberContainsInvalidParen(num.value, format)) {
            alert("The Value Entered Contains An Invalid Paren");
            num.focus();
            return false;
        }
        if (NumberContainsInvalidDash(num.value, format)) {
            alert("The Value Entered Contains An Invalid Dash");
            num.focus();
            return false;
        }
        num.value = trimNumber(num.value);
        FormatNumber(num, format, shortformat);
                        
    }    
}
function NumberContainsInvalidDash(num, mask) {
  if (mask.indexOf('-') == -1) {
      if(num.indexOf('-') != -1)
        return true;
    }
    else 
        return false;
}

function NumberContainsInvalidParen(num, mask) {

    
    if (mask.indexOf('(') == -1 || mask.indexOf(')') == -1) {
        if (num.indexOf('(') != -1 || num.indexOf(')') != -1)
            return true;
    }
   else
    return false;
}

function NumberContainsInvalidDecimal(num, mask) {

    // If mask.indexOf('.') == -1 then the number should not contain decimals.
    if (mask.indexOf('.') == -1) {

        // If the number contains a decimal return true. 
        if (num.indexOf('.') != -1)
            return true;
    }
    else {
        return false;
    }
}

function NumberContainsInvalidDollarSymbol(num, mask) {

    // If mask.indexOf('$') == -1 then the number should not contain a dollar symbol.
    if (mask.indexOf('$') == -1) {

        // If the number contains a decimal return true. 
        if (num.indexOf('$') != -1)
            return true;
    }
    else {
        return false;
    }
}

function FormatNumber(num, format, shortformat)
{
	if(format==null)
		format = "";

    if(shortformat==null)
		shortformat = "";
	
	
	var validchars = "0123456789";
	if (format.indexOf('-')== 0) //allow negative values.
	    validchars += "-";
	var CursorPosition=num.SelectionStart;
	var tempstring = "";
	var returnstring = "";
	var extension = "";
	var tempstringpointer = 0;
	var returnstringpointer = 0;
	var isMoney=false;
	var DecimalValue = "";

    // Remove leading zeros
	num.value = trimNumber(num.value);
	//check for money formating
	if(format.indexOf('$') != -1)
	{
	    isMoney=true;
	    format=format.substring(1,format.length);
	}
	
	//Check for decimal places
	if(format.indexOf('.') !=-1)
	{
	    var decIndex=format.indexOf('.');
	    //Remove the decimal from the format string
	    format=format.substring(0,decIndex);
	    
	    //Get the decimal value from the value
	    decIndex=num.value.indexOf('.');
	    
	    if(decIndex !=-1)
	    {
	        var tempReplace="";
	    
	        tempReplace=num.value.substring(0,decIndex);
	        
	        DecimalValue=num.value.substring(decIndex,num.value.length);
	        
	        if(DecimalValue.length>3)
	            DecimalValue= DecimalValue.substring(0,3);
	        else
	            DecimalValue= DecimalValue;
	            
	       num.value=tempReplace;     
	    }
	    
	}
	
	count = 0;

	// Get the length so we can go through and remove all non-numeric characters
	var length = num.value.length;
	var formatLength = format.length;

	if (isMoney)
	    formatLength += 1;
		
	if (length > formatLength)
	{
		length = formatLength;
	};
	
	// scroll through what the user has typed
	for (var x=0; x<length; x++)
	{
		if (validchars.indexOf(num.value.charAt(x))!=-1)
		{
		tempstring = tempstring + num.value.charAt(x);
		};
	};
	
	if (num.value.length > formatLength)
	{
		length = formatLength;
		extension = num.value.substr(format.length, (num.value.length-format.length));
	};
	
	for (x=0; x<shortformat.length;x++)
	{
		if (shortformat.substr(x, 1)=="9")
		{
			count++;
		};
	}
	if (tempstring.length <= count)
	{
		format = shortformat;
	};


    if(format.indexOf(',') != -1)
    {	
        var backWardFormat="";
        var backWardValue="";
        
        //Reverse the string and mask
        for(x=format.length-1;x>=0;x--)
        {
            backWardFormat=backWardFormat + format.charAt(x);
            
            if(x <= tempstring.length)
                backWardValue=backWardValue + tempstring.charAt(x);    
        }
        
        format=backWardFormat;
        tempstring=backWardValue;
    }
    
    //Apply the mask
    for (x=0; x<format.length;x++)
    {
	    if (tempstringpointer <= tempstring.length)
	    {
		    if (format.substr(x, 1)=="9")
		    {
			    returnstring = returnstring + tempstring.substr(tempstringpointer, 1);
			    tempstringpointer++;
		    }else{
			    returnstring = returnstring + format.substr(x, 1);
		    }
	    }
		
    }
    
    //Put the string back in the correct order
    if(format.indexOf(',') != -1)
    {	
        var fowardValue="";
        
        //Reverse the string and mask
        for(x=returnstring.length-1 ;x>=0;x--)
        {
            fowardValue=fowardValue + returnstring.charAt(x);    
        }
        
        //check to see if there is a leading , and if so remove it
        if(fowardValue.charAt(0)==",")
            fowardValue=fowardValue.substring(1,fowardValue.length);
        
        returnstring=fowardValue;
    }
    
  	// We have gone through the entire format, let's add the extension back on.
		returnstring = returnstring;
	
	//Add money formating if this is a money value	
	if(isMoney && returnstring.length>0)
	    returnstring="$" + returnstring;
	
	if(DecimalValue!='')
	    returnstring=returnstring + DecimalValue;
	
	//we're done - let's return our value to the field.
	num.value = returnstring;
	num.SelectionStart=CursorPosition;
}	
function AutoCompleteNumber(num) {

    if(isNaN(num.value.replace("$","").replace(",","")))
        return;
        
    //Get the decimal value from the value
    var tempstring;
    var decIndex=num.value.indexOf('.');
    
    if(decIndex !=-1)
    {
        tempstring=num.value.substring(0,decIndex);  
        DecimalValue=num.value.substring(decIndex,num.value.length);
        
        if(DecimalValue.length==1)
            DecimalValue=DecimalValue + "00";
        else if(DecimalValue.length==2)
            DecimalValue=DecimalValue + "0";

    }
    else
    {
        tempstring=num.value;
        DecimalValue = ".00";
    }
	        
	//Add a leading 0 if needed
	if(tempstring.length==0 && DecimalValue.length!=0)
	    tempstring=="0";        
	        
	if(tempstring.length!=0 && tempstring !='$')
    	num.value=tempstring + DecimalValue;        
}