﻿// 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
    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) {
    	    return;
    	}
    	if (HTMLCode.indexOf('"') > 0) {
    	    HTMLCode = HTMLCode.replace(/"/g, '\\"');
    	}
    	//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,''); 
	},
	
	Trim : function(s) {
	    return s.replace(/^\s+|\s+$/g,'');
	},
	
	InitializeTimer: function () {
	    Kinsail.Formatting.timers = new Array();
	},

    PrivateFadeIn: function (ObjectName, Opacity, TimeToFade) {
        // Tidy up timer and register
        Kinsail.Formatting.StopTimeout(ObjectName);

        // Make visible
        if (Opacity==0) {
            eval(strGetItemPrefix + ObjectName + strGetItemSuffix + strStyle + strVisibilityCode + ' = "' + strShowKeyword + '"');
        }

        // Fade in one more step
        if (Opacity <= 100) {
            Kinsail.Formatting.SetOpactity(ObjectName, Opacity);
            Opacity += 10;
            window.setTimeout("Kinsail.Formatting.PrivateFadeIn(\'" + ObjectName + "\'," + Opacity + ")", TimeToFade);
        }

        // If fully visible, start countdown to fade out
        if (Opacity >= 100) {
            window.setTimeout("Kinsail.Formatting.PrivateFadeIn(\'" + ObjectName + "\', 100)", 5000);
        }
    },

    PrivateFadeOut: function (ObjectName, Opacity, TimeToFade) {
        // If still visible, fade down one further step
        if (Opacity > 0) {
            Kinsail.Formatting.SetOpacity(ObjectName, Opacity);
            Opacity -= 10;
            window.setTimeout("Kinsail.Formatting.PrivateFadeOut(\'" + ObjectName + "\'," + Opacity + ", " + TimeToFade + ")", TimeToFade);
        }

        // If close to invisible, hide object completely
        if (Opacity<10) {
            eval(strGetItemPrefix + ObjectName + strGetItemSuffix + strStyle + strVisibilityCode + ' = "' + strHideKeyword + '"');
        }
    },

    SetOpacity: function (ObjectName, Opacity) {
        // Get object
        obj = eval(strGetItemPrefix + ObjectName + strGetItemSuffix);

        Opacity=(Opacity==100) ? 99.999 : Opacity;

        // IE/Win
        obj.style.filter = "alpha(opacity:" + Opacity + ")";

        // Safari<1.2, Konqueror
        obj.style.KHTMLOpacity = Opacity/100;

        // Older Mozilla and Firefox
        obj.style.MozOpacity = Opacity/100;

        // Safari 1.2, newer Firefox and Mozilla, CSS3
        obj.style.opacity = Opacity/100;
    },
    
    FadeIn: function (ObjectName, TimeToFade) {
        var obj = document.getElementById(ObjectName);
        // Start timer
        Kinsail.Formatting.timers[ObjectName] = setTimeout("PrivateFadeIn(\'" + ObjectName + "\', 0, " + TimeToFade + ")", TimeToFade);
    },

    FadeOut: function (ObjectName, TimeToFade) {
        // Tidy up timer and register
        Kinsail.Formatting.StopTimeout(ObjectName);

        // Make sure we´re outside the whole fading div
        //var relTarg;
        //if (e.relatedTarget) {
        //    relTarg = e.relatedTarget;
        //}
        //else if (e.toElement) {
        //    relTarg = e.toElement;
        //}
        // If all the way out, fade out
        //if (relTarg.id=="") {
            Kinsail.Formatting.PrivateFadeOut(ObjectName, 100, TimeToFade);
        //}
    },

    StopTimeout: function (ObjectName) {
        if (Kinsail.Formatting.timers != undefined && typeof(Kinsail.Formatting.timers) != "undefined") {
            // Stop timer
            clearTimeout(Kinsail.Formatting.timers[ObjectName]);

            // Clear register
            Kinsail.Formatting.timers[ObjectName]=false;
        }
    },
    // function to set the width of an object
    SetWidth: function (ObjectName, ObjectWidth) {
        if (ObjectWidth != null) {
            obj = eval(strGetItemPrefix + ObjectName + strGetItemSuffix)
            obj.style.width = ObjectWidth + 'px';
        }
    },
    // function to set the width of an object
    SetHeight: function (ObjectName, ObjectHeight) {
        if (ObjectHeight != null) {
            obj = eval(strGetItemPrefix + ObjectName + strGetItemSuffix)
            obj.style.height = ObjectHeight + 'px';
        }
    },
    // function to toggle the grey out background
    ToggleGreyOutBackground: function (GreyOutDiv, Opacity) {
        // if no opacity is specified, set the default
        if (Opacity == null || isNaN(Opacity)) {
            Opacity = 50;
        }
        // size the div
        Kinsail.Formatting.SetWidth(GreyOutDiv, Kinsail.Positioning.getWindowWidth()); //availWidth acts strangly here due to hidden DIVs and the such
        Kinsail.Formatting.SetHeight(GreyOutDiv, screen.availHeight);//Kinsail.Positioning.getWindowHeight()+Kinsail.Positioning.getWindowScrollY());
        // reset to the top left
        Kinsail.Positioning.SetX(GreyOutDiv, 0);
        Kinsail.Positioning.SetY(GreyOutDiv, 0);
        // toggle the div
        Kinsail.Formatting.ShowHideElement(GreyOutDiv);
        // set the opacity
        Kinsail.Formatting.SetOpacity(GreyOutDiv, Opacity);
    },
    // function to swap images on an element
    SwapImage: function (ImageName, NewImagePath) {
    
    },

    StringLeft: function (str, n) {
	    if (n <= 0)
	        return "";
	    else if (n > String(str).length)
	        return str;
	    else
	        return String(str).substring(0,n);
    },
    StringRight: function (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);
        }
    },
    GetWidth: function(ObjectName) {
        obj = eval(strGetItemPrefix + ObjectName + strGetItemSuffix);
    	if (obj == null) {
    	    return;
    	}
    	intWidth = parseInt(obj.style.width.replace('px', ''), 10);
    	if (isNaN(intWidth)) {
    	    intWidth = 0;
    	}
    	return intWidth;
    },
    GetHeight: function(ObjectName) {
        obj = eval(strGetItemPrefix + ObjectName + strGetItemSuffix);
    	if (obj == null) {
    	    return;
    	}
    	intHeight = parseInt(obj.style.height.replace('px', ''), 10);
    	if (isNaN(intHeight)) {
    	    intHeight = 0;
    	}
    	return intHeight;
    },
    SetBackgroundColor: function(ObjectName, Color) {
        obj = eval(strGetItemPrefix + ObjectName + strGetItemSuffix);
    	if (obj == null) {
    	    return;
    	}
		// set the background color
		obj.style.backgroundColor = '#' + Color;
    },
    GetBackgroundColor: function(ObjectName) {
        obj = eval(strGetItemPrefix + ObjectName + strGetItemSuffix);
    	if (obj == null) {
    	    return;
    	}
		// set the background color
		return obj.style.backgroundColor;
    },
    StripHTML:  function(HTMLCode){
    	// replace breaks with a \n
    	HTMLCode = HTMLCode.replace(/<br \/>/i, "<br>");
    	HTMLCode = HTMLCode.replace(/<br>/i, "\n");
    	// create the regular expression
		var re= /<\S[^><]*>/g
		HTMLCode = HTMLCode.replace(re, "")
		return HTMLCode;
	}
}; // end Kinsail.Formatting