﻿// Declare the namespaces
if (Kinsail.Formatting == null || typeof(Kinsail.Formatting) != "object") {Kinsail.Formatting = new Object();}

Kinsail.Formatting = {
    
    FormatCurrency: function (NumberToFormat) {
	    NumberToFormat = NumberToFormat.toString().replace(/\$|\,/g,'');
	    if (isNaN(NumberToFormat)) {
		    NumberToFormat = "0";
	    }
	    sign = (NumberToFormat == (NumberToFormat = Math.abs(NumberToFormat)));
	    NumberToFormat = Math.floor(NumberToFormat*100+0.50000000001);
	    intCents = NumberToFormat%100;
	    NumberToFormat = Math.floor(NumberToFormat/100).toString();
	    if(intCents<10)
	        intCents = "0" + intCents;
	    for (var i = 0; i < Math.floor((NumberToFormat.length-(1+i))/3); i++)
	        NumberToFormat = NumberToFormat.substring(0,NumberToFormat.length-(4*i+3))+','+NumberToFormat.substring(NumberToFormat.length-(4*i+3));
	    return (((sign)?'':'-') + '$' + NumberToFormat + '.' + intCents);
    },
    
    // function to determine if a particular element is visible
    isVisible: function (ElementName) {
        if (eval(strGetItemPrefix + ElementName + strGetItemSuffix + strStyle + strVisibilityCode) == strShowKeyword) {
            return true;
        }
        else {
            return false;
        }
    },
    
    
    // function to show/hide elements via toggle or forcing display or not
    /* NOTE ----- IMPORTANT NOTE -----
        YOU MUST set in code the following...
            <script type="text/javascript">
        <!--
        //this is for browser config
        var strImagePath = '/images';
        //this is for the record set
        //var strDelimiter = '~';
     -->
    </script>
        ... prior to the includes 
        <script type="text/javascript" src="/includes/Kinsailnamespace.js"></script>
    <script type="text/javascript" src="/includes/browserconfig.js"></script>
    <script type="text/javascript" src="/includes/formatting.js"></script>
        ...or you get the error
        strGetItemSuffix is not defined as a javascript error
       */
    ShowHideElement: function (ElementName, ForcedVisibility) {
    	if (eval(strGetItemPrefix + ElementName + strGetItemSuffix) == null) {return;}

        // if we don't have a forced visibility, then do a simple toggle of visibility
        if (ForcedVisibility == null) {
            if (eval(strGetItemPrefix + ElementName + strGetItemSuffix + strStyle + strVisibilityCode + ' == "' + strShowKeyword + '"')) {
                eval(strGetItemPrefix + ElementName + strGetItemSuffix + strStyle + strVisibilityCode + ' = "' + strHideKeyword + '"');
            }
            else {
                eval(strGetItemPrefix + ElementName + strGetItemSuffix + strStyle + strVisibilityCode + ' = "' + strShowKeyword + '"');
            }
        }
        // if we have a forced visibility, show/hide accordingly.
        else {
                eval(strGetItemPrefix + ElementName + strGetItemSuffix + strStyle + strVisibilityCode + ' = "' + ForcedVisibility + '"');
        }
    },
    
    // write to an element's inner html
    WriteHTML: function (ElementName, HTMLCode) {
    	if (eval(strGetItemPrefix + ElementName + strGetItemSuffix) == null)
    	{
    	    if (Kinsail.debugLevel > 2) 
    	    {
    	        alert('Null Element!');
    	    }
    	    return;
    	}
    	else if (Kinsail.debugLevel > 7)
    	{
    	    alert('Non-Null Element');
    	}
    	if (Kinsail.debugLevel > 5)
        	alert(strGetItemPrefix + ElementName + strGetItemSuffix + strWritePrefix + HTMLCode + strWriteSuffix);
        eval(strGetItemPrefix + ElementName + strGetItemSuffix + strWritePrefix + HTMLCode + strWriteSuffix);
    },
    
    // get the inner html of an element
    GetHTML: function (ElementName) {
    	if (eval(strGetItemPrefix + ElementName + strGetItemSuffix) == null) {return;}
        return eval(strGetItemPrefix + ElementName + strGetItemSuffix + strInnerHTML);
    },
    
    // write to an element's .value attribute
    WriteFormValue: function (ElementName, strValue) {
    	if (eval(strGetItemPrefix + ElementName + strGetItemSuffix) == null) {return;}
        eval(strGetItemPrefix + ElementName + strGetItemSuffix + '.value =\'' + strValue + '\'');
    },
    
    // get the .value attribute of an element
    GetFormValue: function (ElementName) {
    	if (eval(strGetItemPrefix + ElementName + strGetItemSuffix) == null) {return;}
        return eval(strGetItemPrefix + ElementName + strGetItemSuffix + '.value');
    }, 
	
	// Remove all HTML tags from a string
	StripHtml: function (s) {	
		return s.replace(/<([^>]+)>/g,''); 
	}
	
}; // end Kinsail.Formatting