String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
Array.prototype.count = function() {
	return this.length;
};

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
	return false;
}

function moneyFormat(nStr, commas) {
	nStr = parseFloat(nStr);
	nStr = nStr.toFixed(2);
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '.00';
	if (typeof(commas) != 'undefined' && !commas) {
		return x1 + x2;
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top",
            ($(window).height() - this.height())/2+$(window).scrollTop()+"px");
    this.css("left",
            ($(window).width() - this.width())/2+$(window).scrollLeft()+"px");
    return this;
}
function loading(open) {
    if (open) {
        setOverlayDimensionsToCurrentDocumentDimensions();
        $("#loading img").center();
        $('#loading').slideDown();
        $('#loading').fadeIn();
    } else {
        $("#loading").hide();
    }
}
function setOverlayDimensionsToCurrentDocumentDimensions() {
    $('#loading').width($(document).width());
    $('#loading').height($(document).height());
    $("#loading img").center();
}
$(document).ready( function() {
    var $overlay = 
        $('<div id="loading" class="ui-widget-overlay">' +
            '<img src="/images/icon/loading-1.gif" width="100" height="100"' +
            'alt="loading" title="loading"/></div>')
                        .hide()
                        .appendTo('body');
    $(window).resize(function(){
        setOverlayDimensionsToCurrentDocumentDimensions();
    });
	toolbar_gbw_Simple =
    	[
    	    ['Source','-','Preview','-','Templates'],
    	    ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
    	    ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
    	    ['Maximize', 'ShowBlocks'],
    	    ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
    	    ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote','CreateDiv'],
    	    ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
    	    ['Link','Unlink','Anchor'],
    	    ['TextColor','BGColor'],
    	    ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
    	    ['Styles','Format','Font','FontSize'],
    	];
	$('.statuserror')
		.addClass('ui-corner-all ui-state-error')
		.prepend('<span class="ui-icon ui-icon-alert" ' +
						'style="float: left; margin-right: .3em;"></span>' + 
						'<strong>Alert:</strong>')
		.wrapInner('<p style="padding: .4em 2em;"/>');
	$('.statuswarning')
		.addClass('ui-corner-all ui-state-highlight')
		.prepend('<span class="ui-icon ui-icon-info" ' +
						'style="float: left; margin-right: .3em;"></span>')
		.wrapInner('<p style="padding: .4em 2em;"/>');
	$('.statussuccess')
		.addClass('ui-corner-all ui-state-active')
		.prepend('<span class="ui-icon ui-icon-check" ' +
						'style="float: left; margin-right: .3em;"></span>')
		.wrapInner('<p style="padding: .4em 2em;"/>');
	
	$("button, .button").not(".nojq").button();
	$('form.formStyle1 button, .buttonW')
		.wrap('<p style="text-align: center;"/>');
	$("div.radio").buttonset();
	$("div.accordion").accordion({
		collapsible:    true,
        active:         false,
        autoHeight:     false,
        header:         "h3"
	});
	$(".tabs").tabs();
	$("input[cookieName^=GIGABIT_]").each(function () {
		cookieValue = getCookie($(this).attr("cookieName"));
		if ((!$(this).val()) && (cookieValue)) {
			$(this).val(cookieValue);
		}
	});
});
function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
		    c_start=c_start + c_name.length+1;
		    c_end=document.cookie.indexOf(";",c_start);
	    	if (c_end==-1) c_end=document.cookie.length;
	    	return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
function isTouchDevice(){
    try{
        document.createEvent("TouchEvent");
        return true;
    }catch(e){
        return false;
    }
}
function touchScroll(id){
    if(isTouchDevice()){ //if touch events exist...
        alert('11');
        var el=document.getElementById(id);
        var scrollStartPos=0;

        document.getElementById(id).addEventListener("touchstart", function(event) {
            scrollStartPos=this.scrollTop+event.touches[0].pageY;
            event.preventDefault();
        },false);

        document.getElementById(id).addEventListener("touchmove", function(event) {
            this.scrollTop=scrollStartPos-event.touches[0].pageY;
            event.preventDefault();
        },false);
    }
}
