
"use strict"; 

/////////////////////////////////////////////////////////////////////
// function name: browserDetect()
var browser = {};
function browserDetect() {
	var version = 0;
	var x = navigator.userAgent.toLowerCase();
	browser.chrome = /chrome/.test(x);
	browser.msie = /msie/.test(x);
	browser.safari = /safari/.test(x);
	browser.notChrome = /version/.test(x);
	browser.firefox = /firefox/.test(x);
	browser.opera = /opera/.test(x);
	browser.current = "unknown";
	browser.offsetTop = 0;
	browser.offsetLeft = 0;	
	
	//cDebug( x );
	
	// Is this a version of IE?
	if(browser.msie) {
		version = x.split("msie")[1].split(";")[0].replace(/ /g,'');
		browser.current = "msie";
		if (version.substr(0,1) == "7") {
			browser.offsetTop = 143;
			browser.offsetLeft = 214;
		}
	}
	
	// Is this a version of Chrome?
	if(browser.chrome) {
		version = x.split("chrome/")[1].split(" ")[0];
		browser.safari = false;
		browser.current = "chrome";
	}
	
	// Is this a version of Safari?
	if(browser.safari && browser.notChrome) {
		version = x.split("version/")[1].split(" ")[0];
		browser.current = "safari";
	}
	
	// Is this a version of Mozilla?
	if(browser.firefox) {
		version = x.split("firefox/")[1].split(" ")[0];
		browser.current = "firefox";
	}
	
	// Is this a version of Opera?
	if(browser.opera) {
		version = x.replace(/ .*/i, '').replace(/opera\//, '');
		browser.current = "opera";
	}
	browser.version = version;
	return version;
} 
browserDetect(); 





/////////////////////////////////////////////////////////////////////
// function name: nInt(_str)

function nInt(_str) {
    if (_str === null || _str === "" || _str === " " || _str === undefined) {
        _str = 0;
    }
	_str = _str.toString().replace(/[^0-9|\-|\.]/gi, '');
	_str = Math.round(_str);
	if (_str.toString() == "NaN" || _str.toString() === "undeifined" || _str.toString() === "") {
		_str = 0;
	}
    return (_str);
}

/////////////////////////////////////////////////////////////////////
// function name: get_time()

function get_time() {
    var t = new Date();
    return t.getTime();
}







////////// DIV
var DIV = {

	params: {},
	
	id: function () {
		if (!this.params.time) {
			this.params.time = get_time();
			this.params.order = 0;
		}
		this.params.order++;
		
		if (!DIV.params.width) { DIV.params.width = 0; }
		if (!DIV.params.height) { DIV.params.height = 0; }
		if (!DIV.params.top) { DIV.params.top = 0; }
		if (!DIV.params.width) { DIV.params.width = 0; }
		
		this.params.id = "D_" + this.params.time + "_" + this.params.order;
        return this.params.id
    },	
	
	page: function(_src, _title, _class, _w, _h, _top, _left) {
			if (_class === undefined || !_class) { _class = "bgNoise30blk"; this.AddCss(_class, "http://op.chamberlandhosting.com/image/bg.trans.noise.30B.png"); }
			if (_title === undefined || !_title) { _title = ""; }
			if (_w) { DIV.params.width = _w; }
			if (_h) { DIV.params.height = _h; }
			if (_top) { DIV.params.top = _top; }
			if (_left) { DIV.params.left = _left; }			
			
			top.DIV.iframe(DIV.id(), _src, _class, _title);
	},
	
	
	iframe: function(_id, _src, _class, _title) {
			if (!_id) { _id = DIV.id(); }
			this.overlay(_id, _class);			
		
			var html = '' +
					'<div align="center" id="O'+ _id +'" class="bg80blk" style="position:absolute; '+
							'top:'+ DIV.params.top +'px; left:'+ DIV.params.left +'px; margin:0px; padding:0px;">' +
						'<div align="center" style="height: 24px;">' +
							'<div align="center" class="popTitle marginL20" style="float: left; padding-top: 3px;">'+ _title +'</div>' +
							'<div align="right" style="float: right;" class="padR5">' +
								'<span id="close-'+ _id +'" onclick="DIV.Close(\''+ _id +'\')" class="ui-icon ui-icon-circle-close marginT5 pointer closeBtn"> </span>' +
							'</div>' +
						'</div>' +
						'<div align="center" id="M'+ _id +'" style="background-color: #FFF; margin: 10px;">' +
							'<iframe width="0" height="0" frameborder="no" scrolling="no" id="IF'+ _id +'" name="IF'+ _id +'" src="'+ _src +'" onload=""></iframe>' +
						'</div>' +
					'</div>';
			//$(document).ready( function() { });
			top.$("#C"+ _id).append( html );
			top.$("div[id^=OD_], div[id^=MD_]").addClass("ui-corner-all");
			$("#close-"+ _id).button();
			
			if (DIV.params.width) { $("#IF"+ _id).attr("width", DIV.params.width) }
			if (DIV.params.height) { $("#IF"+ _id).attr("height", DIV.params.height) }			
	},	
	
	overlay: function(_id, _class) {			
			
			var div = ''+
					'<div id="'+ _id +'" order="'+ this.params.order +'" align="center" '+
					'style="position:absolute; margin:0px; padding:0px; width:100%; height:'+ top.$(document).height() +'px; top:0px; left:0px; text-align: center; z-index: 10000;"'+
					'class="'+ _class +'">'+
						'<table align="center" width="100%" height="" cellpadding="0" cellspacing="0"><tbody>'+
						'<tr>'+
							'<td id="C'+ _id +'" align="center" valign="middle">'+
							'</td>'+
						'</tr>'+				
						'</tbody></table>'+
				'</div>';
			
			top.$("body").append($(div));
			return _id;
	},
	

	
	
	overlay2: function(_id, _class) {
			if (!_class) { _class = ""; }
			
			var div = ''+
					'<div id="'+ _id +'" align="center" '+
					'style="position:absolute; width:100%; height:'+ top.$(document).height() +'px; top:0px; left:0px; z-index: 10000;"'+
					'class="'+ _class +'"></div>';
			top.$("body").append($(div));
			return _id;
	},
	
	iframeShadow: function(_id, _src, _class, _title) {
			
			this.overlay(_id, _class);			
			if (!_title) { _title = ""; }
			
			var html = '' +
					'<table id="O'+ _id +'" style="margin-bottom:15%;" align="center" cellpadding="0" cellspacing="0"><tbody>'+
					'<tr>'+
						'<td width="20" class="TL-80-000"></td>' +
						'<td algin="left" class="TH-80-000">'+
							'<div align="left" style="float:left; position:relative; top:19px; left:5px; width:60%;">'+
								'<span id="popTitle" class="ui-color fs10 font-verdana bold">'+ _title +'</span>'+
							'</div>'+
						'</td>'+
						'<td align="left" valign="top" class="TR-80-000">'+
							'<div style="float:left; position:relative; display:inline-block; top:19px; right:16px; width:16px; height:16px;">'+
								'<span id="popClose" class="ui-icon ui-icon-circle-close pointer"></span>'+
							'</div>'+
						'</td>'+				
					'</tr>'+
					'<tr>'+
						'<td width="20" class="VL-80-000"></td>' +
						'<td  align="center" id="M'+ _id +'" style="background-color:#fff; padding:10px;">'+
							'<iframe id="IF'+ _id +'" name="IF'+ _id +'" width="0" height="0" frameborder="no" scrolling="no"'+
							' src="'+ _src +'" onload="$(window).ready( function() { DIV.resize(\''+ _id +'\'); DIV.resize(\''+ _id +'\'); });"></iframe>' +				
						'</td>'+
						'<td class="VR-80-000"></td>'+				
					'</tr>'+				
					'<tr>'+
						'<td width="20" class="BL-80-000"></td>' +
						'<td class="BH-80-000"></td>'+
						'<td class="BR-80-000"></td>'+				
					'</tr>'+				
					'</tbody></table>';
				
			top.$("#"+ _id +" > table tbody > tr > td").append( html );	
			
			top.$("#popClose").live("click",
				function() {
					top.$("#"+ _id).empty().empty().remove();
				});
						

			
	},
	
	
	
	pageShadow: function(_src, _title, _class) {
			if (_class === undefined) { _class = "bgNoise30blk"; }
			this.iframeShadow(this.id(), _src, _class, _title);
	},
	
	
	
	resize: function (_id, _w, _h) { 
		
		
		if (!_id) { _id = window.name;  }
		if (!_id) { return false; }
		_id = _id.toString().replace("IFD_", "D_");
		
		if (!_h) { _h = 0; }
		if (!_w) { _w = 0; }
		
		var F = top.document.getElementById("IF"+ _id);	
		if (!F) { return false; }
		
			if (!_h) {
				_h = top.$("#IF"+ _id).contents().find('body').attr("offsetHeight");
				if (!_h) {
					_h = top.$("#IF"+ _id).contents().find('body').attr("scrollHeight");
				}
				if (!_h) {
					top.$("#IF"+ _id).contents().find("body > *").each(
					function() {
						_h += nInt($(this).height());
					});						
				}
			}
			
			if (!_w) { 
			
				_w = top.$("#IF"+ _id).contents().find("body > *:first").width();
				if (!_w) { _w = F.contentWindow.document.body.scrollWidth+30; }
			}			
			
			F.height = _h; //FF 3.0.11, Opera 9.63, and Chrome
			F.width = _w; //FF 3.0.11, Opera 9.63, and Chrome
	},
	
	
	centerPopin: function(_id) {
		
			if (!_id) {
				_id = DIV.params.id;
			}
		
			var _w = $("#O"+ _id).width() / 2;
			var _h = $("#IF"+ _id).height();
			var _bh = $(window).height() -100;
			
//			console.log("IF: "+ _h);
//			console.log("DOC: "+ _bh);
			
			_t = Math.floor($(window).height() * 0.5 - _h);
			if (_t < 0) { _t = 20; }
			$("#O"+ _id).css({'top': _t +'px','left': Math.floor($(window).width() * 0.5 - _w) +'px'});
						
			if (_h > _bh) { 
				$("#M"+ _id).css({'height': (_bh - 30) +'px','overflow-y':'scroll'});
			}

	},
	
	Center: function() {
			var _w = top.$("#O"+ DIV.params.id).width() / 2;
			var _h = top.$("#O"+ DIV.params.id).height();
			top.$("#O"+ DIV.params.id).css({'top': Math.floor($(window).height() * 0.5 - _h) +'px','left': Math.floor($(window).width() * 0.5
			 - _w) +'px'});
	},
	
	GetParent: function(_id) {
		var _time = _id.split("_")[1];
		var _order = _id.split("_")[2];
		var _target = _order;
		top.$("div[id*="+ _time +"][order]").each(
			function() {
				var n = $(this).attr("order");
				if (n < _target ) {
					_target = n;	
				}
			});
		//return "IFD_"+ _time +"_"+ _target;
		return top.document.getElementById("IFD_"+ _time +"_"+ _target).contentWindow;
	},
	
	AddCss: function(_id, _url) {
		$("head").append('<style type="text/css">\n'+
			'.'+ _id +' {\n'+
			'	background-image:url('+ _url +');\n'+
			'}\n');
	},
	
	Close: function (_id) {
       setTimeout( function() {  top.$('#' + _id.replace("IFD_", "D_")).empty().remove(); }, 200);
    }
	
};







////////// SPINNER
var SPINNER = {
    id: function () {
        return 'SPINNER';
    },
    show2: function () {
        var id = SPINNER.id();
		$("#"+ id).empty().remove();
        DIV.overlay(id, false);
		
		$('#' + id).css({'background-image':'url(http://op.chamberlandhosting.com/image/bg.trans.40W.diag.png)'});
		$('#' + id +" > table tbody > tr > td").append('<div id="SpinnerContent"></div>');
		
        $.ajax({
            type: "GET",
            url: "http://op.chamberlandhosting.com/js/jquery/plugin/swfobject.js",
            dataType: "script",
            success: function () {
                $(document).ready(function () {
                    if (swfobject.hasFlashPlayerVersion("8")) {
                        var att = {
                            data: "http://op.chamberlandhosting.com/swf/spinner_v1.swf",
                            width: 150,
                            height: 150,
                            align: "middle"
                        };
                        var par = {
                            menu: "false",
                            wmode: "transparent"
                        };
                        var id = "SpinnerContent";
                        var myFlashContent = swfobject.createSWF(att, par, id);
                    }
					
					
                });
            }
        });
    },	
	
    show: function (_page) {
        var id = SPINNER.id();
		$("#"+ id).empty().remove();
        DIV.overlay2(id, "ui-widget-overlay");
//        $('#' + id +" > table tbody > tr > td").append('<div id="SpinnerContent"></div>');
		$('#' + id).append('<div id="'+ SPINNER.id() +'-content"></div>');
		
		if (!$("head script[src*=swfobject.js]").html()) {
			$.ajax({
				type: "GET",
				url: "http://op.chamberlandhosting.com/js/jquery/plugin/swfobject.js",
				dataType: "script",
				success: function () {
					$(document).ready(function () {
						if (swfobject.hasFlashPlayerVersion("9")) {
							var att = {
								data: "http://op.chamberlandhosting.com/swf/loading.circles.swf",
								width: "55",
								height: "55",
								align: "middle"
							};
							var par = {
								menu: "false",
								wmode: "transparent"
							};
							var id = SPINNER.id() +"-content";
							var myFlashContent = swfobject.createSWF(att, par, id);
							//if (_page) { setTimeout(function() { DIV.page(_page); }, 300); }
							
							$("#"+ SPINNER.id() +"-content").css({"margin-top": (top.$(window).height() * 0.5) +"px"});
						}
					});
				}
			});
		} else {
			
			if (swfobject.hasFlashPlayerVersion("8")) {
				var att = {
					data: "http://op.chamberlandhosting.com/swf/loading.circles.swf",
					width: "55",
					height: "55",
					align: "middle"
				};
				var par = {
					menu: "false",
					wmode: "transparent"
				};
				var idWait = "SpinnerContent";
				var myFlashContent = swfobject.createSWF(att, par, idWait);
				if (_page) { setTimeout(function() { DIV.page(_page); }, 1500); }
			}			
			
		}
    },	
	
	
    hide: function () {
        var id = SPINNER.id();
        $('#' + id).empty().remove();
    }
};


function Nx() { return "\n"; }
function Br() { return "<br />"; }


////////// PROTOTYPES
Array.prototype.Add = function (_value) {
    this.push(_value);
    return this;
};
Array.prototype.Remove = function (_value) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == _value) {
            this.splice(i, 1);
            break;
        }
    }
    return this;
};
Array.prototype.RemoveAll = function (_value) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == _value) {
            this.splice(i, 1);
			this.RemoveAll(_value);
            break;
        }
    }
    return this;
};
Array.prototype.maxCol = function (n) {
    var v = 0;
    for (var i = 0; i < this.length; i++) {
        if (this[i][n] * 1 > v * 1) {
            v = this[i][n];
        }
    }
    return v;
};
Array.prototype.MatchPattern = function (_pattern) {
    if (_pattern) { 
        for (var i = 0; i < this.length; i++) {
			if (this[i]) {
				if (this[i].toString() === _pattern.toString()) {
					return true;
				}
			}
        }
    }
    return false;
};
Array.prototype.RemovePattern = function (_pattern) {
    if (_pattern) { 
        for (var i = 0; i < this.length; i++) {
			if (this[i]) {
				if (this[i].toString() === _pattern.toString()) {
                    this.splice(i, 1);
                    this.RemovePattern(_pattern);					
					return true;
				}
			}
        }
    }
    return false;	
};
Array.prototype.SortNumbers = function() {
  var x, y, holder;
  for(x = 0; x < this.length; x++) {
    for(y = 0; y < (this.length-1); y++) {
      if(this[y] > this[y+1]) {
        holder = this[y+1];
        this[y+1] = this[y];
        this[y] = holder;
      }
    }
  }
};
Array.prototype.Clear = function() {
	while (this.length) {
		this.pop();
	}
};



Array.prototype.RemoveMulti = function (_value, _col) {
    for (var i = 0; i < this.length; i++) {
        if (this[i][_col] == _value) {
            this.splice(i, 1);
            break;
        }
    }
    return this;
};


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}





/////////////////////////////////////////////////////////////////////
// function name: iTimer()

function iTimer() {
    this.func = null;
    this.delay = 3000;
    this.tID = null;
	this.isRunning = false;
}
iTimer.prototype.start = function () {
	this.isRunning = true;
    this.clearInterval();
    this.setInterval(this.func, this.delay);
};
iTimer.prototype.setInterval = function (func, delay) {
    this.func = func;
    this.delay = delay;
    this.tID = window.setInterval(func, delay);
};
iTimer.prototype.clearInterval = function () {
	this.isRunning = false;
    if (this.tID) {
        clearInterval(this.tID);
    }
};
/////////////////////////////////////////////////////////////////////
// function name: dialog_message( _title, _msg, _bool_modal, _width, _height)

function dialog_message(_title, _msg, _bool_modal, _width, _height) {
	
	setScript("http://op.chamberlandhosting.com/js/jquery/ui/latest.js");
	
	
    if (!_title) {
        _title = "";
    }
	var _opt = "{autoOpen: true, bgiframe: true";
    if (_bool_modal) {
        _opt += ", modal: true";
    } else {
        _opt += "";
    }
    if (_width) {
        _opt += ", width: " + _width;
    } else {
        _opt += "";
    }
    if (_height) {
        _opt += ", height: " + _height;
    } else {
        _opt += "";
    }
	_opt += "}";
	
	$("#dialogMSG").empty().remove();
	$('body').append('<div id="dialogMSG"  title="' + _title + '">' + _msg + '</div>');
	setTimeout(function() { eval('$("#dialogMSG").dialog('+ _opt +' )'); }, 50);	
	
	/*
	
    if (!$("head link[href*=jquery-ui.css]").html() && !$("head link[href*=ui.dialog.css]").html()) {
        $('<link rel="stylesheet" type="text/css" href="http://op.chamberlandhosting.com/js/jquery/themes/sunny/jquery-ui.css" />').appendTo("head");
    }
	
	
	
    if (!$("head script[src*=ui.core.js]").html()) {
        $.ajax({
            type: "GET",
            url: "http://op.chamberlandhosting.com/js/jquery/ui/ui.core.js",
            dataType: "script",
            success: function () {
                $.ajax({
                    type: "GET",
                    url: "http://op.chamberlandhosting.com/js/jquery/ui/jquery-ui.js",
                    dataType: "script",
                    success: function () {
                        makeDialog(_title, _msg);
                        eval('$("#dialogMSG").dialog({autoOpen: true,bgiframe: true' + _bool_modal + _width + _height + '});');
                    }
                });
            }
        });
    } else if (typeof($().dialog) != "function") {
        $.ajax({
            type: "GET",
            url: "http://op.chamberlandhosting.com/js/jquery/ui/jquery-ui.js",
            dataType: "script",
            success: function () {
                makeDialog(_title, _msg);
                eval('$("#dialogMSG").dialog({autoOpen: true,bgiframe: true' + _bool_modal + _width + _height + '});');
            }
        });
    } else {
        makeDialog(_title + " ###", _msg);
        eval('$("#dialogMSG").dialog({autoOpen: true,bgiframe: true' + _bool_modal + _width + _height + '});');
    }
	
	*/
}
/////////////////////////////////////////////////////////////////////
// function name: text2ascii(_string)

function text2ascii(_string) {
    var out = "";
    for (var i = 0; i < _string.toString().length; i++) {
        out += _string.toString().charCodeAt(i) + ".";
    }
    return out;
}
/////////////////////////////////////////////////////////////////////
// function name: ascii2text(_string)

function ascii2text(_string) {
    var out = "";
    var arr = _string.toString().split(".");
    for (var i = 0; i < arr.length; i++) {
        if (arr[i]) {
            out += String.fromCharCode(arr[i]);
        }
    }
    return out;
}
/////////////////////////////////////////////////////////////////////
// function name: removeCData(_value)

function removeCData(_value) {
    _value = _value.toString().replace(/\r\n/g, '');
    _value = _value.toString().replace(/<!.*\[CDATA\[/g, '');
    _value = _value.toString().replace(/\]\].*>/g, '');
    return jQuery.trim(_value);
}

/////////////////////////////////////////////////////////////////////
// function name: randNum(_n)
function randNum(_n) {
	return Math.floor( Math.random() * nInt(_n) );
}
/////////////////////////////////////////////////////////////////////
// function name: ajax_loading(_id)

function ajax_loading(_id) {
    var time_id = get_time();
    $(_id).append('<div _id="LOADING_' + time_id + '" class="loading"></div>');
    return time_id;
}
/////////////////////////////////////////////////////////////////////
// function name: ajax_complete(_id)

function ajax_complete(_id) {
    $(_id).find('.loading').empty().remove();
}
/////////////////////////////////////////////////////////////////////
// function name: qsValue(_id)

function qsValue(_id, qs) {
    if (!qs) { qs = window.location.search.substring(1); }
    qs = qs.split("&");
    for (var i = 0; i < qs.length; i++) {
        var x = qs[i].split("=");
        if (x[0] == _id) {
			if (x[1] && x[1] !== undefined) {
	            return x[1];
			} else {
				return "";	
			}
        }
    }
	return "";
}
/////////////////////////////////////////////////////////////////////
// function name: qsValueReplace(_id, _v, _qs)

function qsValueReplace(_id, _v, qs) {
	var qsOut = [];
	if (!qs) { qs = window.location.search.substring(1); }
	if (qs) {
		if ( qs.indexOf(_id) < 0) { qs = qs + "&"+ _id +"="+ _v; }
	} else {
		return _id +"="+ _v;
	}
    qs = qs.split("&");
    for (var i = 0; i < qs.length; i++) {
		var x = qs[i].split("=");
//cDebug( x );
		if (x) {
			if (x[0] == _id) {
				x[1] = _v;
			}
		}
		qsOut.push( x[0] +"="+ x[1] );
    }
	return qsOut.join("&");
}



/////////////////////////////////////////////////////////////////////
// function name: change_rows(_id, _bool)

function change_rows(_id, _bool) {
    $("#" + _id + " div").each(function () {
        if (_bool) {
            $(this).addClass('sizingChartOver');
        } else {
            $(this).removeClass('sizingChartOver');
        }
    });
}
/////////////////////////////////////////////////////////////////////
// function name: refresher(_elem, _patten)

function refresher(_elem, _patten) {
    $(_elem + "[id^='" + _patten + "']", top.document).each(function () {
        var id = this.id;
        if (window.name != id) {
            top.window.frames[id].location.reload();
        }
    });
}
/////////////////////////////////////////////////////////////////////
// function name: check_cboxes(_patten, _values)

function check_cbs(_patten, _values) {
    _values = _values.split(',');
    $("input[type=checkbox]"+ _patten).each(function () {
        for (var i = 0; i < _values.length; i++) {
            if (_values[i] == this.value) {
                this.click();
            }
        }
    });
}
/////////////////////////////////////////////////////////////////////
// function name: uncheck_cb(_patten)

function uncheck_cb(_patten) {
    $("input[type=checkbox]" + _patten).each(
		function () {
			if ($(this).is(":checked")) {
				$(this).click();
			}
		}
	);
}
/////////////////////////////////////////////////////////////////////
// function name: checkall_cb(_patten)

function checkall_cb(_patten) {
    $("input[type=checkbox]" + _patten).each(
		function () {
			if (!$(this).is(":checked")) {
				$(this).click();
			}
		}
	);
}

/////////////////////////////////////////////////////////////////////
// function name: ischecked_cb(_patten)

function ischecked_cb(_patten) {
	var ret = 0;
    $("input[type=checkbox]" + _patten).each(
		function () {
			if ($(this).is(":checked")) {
				ret = 1;
			}
		}
	);
	return ret;
}


/////////////////////////////////////////////////////////////////////
// function name: get_values_cb(_patten)

function get_values_cb(_patten) {
	var ret = "";
    $("input[type=checkbox]" + _patten).each(
		function () {
			if ($(this).is(":checked")) {
				ret += $(this).val() +",";
			}
		}
	);
	return ret;
}




/////////////////////////////////////////////////////////////////////
// function name: hl_text(_id, _bool, _color)

function hl_text(_id, _bool, _color) { console.debug("ID: "+ _id);//DEBUG
    if (!_color) {
        _color = '#F60';
    }
    if (document.getElementById(_id)) {
        if (_bool) {
            document.getElementById(_id).style.color = _color;
        } else {
            document.getElementById(_id).style.color = '#000';
        }
    }
}
/////////////////////////////////////////////////////////////////////
// function name: textarea_rows(_id, increment)

function textarea_rows(_id, increment) {
    var h = $("#" + _id).height();
    $("#" + _id).attr('style', 'height: ' + (increment + h) + 'px;');
    DIV.resize();
}
/////////////////////////////////////////////////////////////////////
// function name: indent_img(_n)

function indent_img(_n) {
    var out = "";
    for (var i = 0; i < nInt(_n); i++) {
        out = out + '<img src="http://op.chamberlandhosting.com/image/spacer.gif" border="0" width="30" height="1"/>';
    }
    return out;
}
/////////////////////////////////////////////////////////////////////
// function name: toggleShowHide(_elmID, _bool)

function toggleShowHide(_elmID, _bool) {
    if (_elmID) {
		var _status = $('#' + _elmID).css("display");
		if (_status == undefined || _status == "none") {
			$('#' + _elmID).show("slow");
		} else {
			$('#' + _elmID).hide("fast");	
		}
		  
        if (_bool) {
            DIV.resize();
        }
    } else {
        alert("NO Toggle ID!");
    }
}


/////////////////////////////////////////////////////////////////////
// function name: img_constrain(_w, _h, _id)

function img_constrain(_w, _h, _id) {
    var pic = document.getElementById(_id);
    if (pic) {
        var ih = pic.offsetHeight;
        var iw = pic.offsetWidth;
        if (iw) {
            var r = 0;
            if (_w > _h) {
                r = ih / iw;
            } else {
                r = iw / ih;
            }
            iw = iw * r;
            ih = ih * r;
            if (iw > ih) {
                iw = _w;
                ih = _w * r;
            } else {
                iw = _h * r;
                ih = _h;
            }
            pic.width = iw;
            pic.height = ih;
        }
    }
}

/////////////////////////////////////////////////////////////////////
// function name: img(_src, _w, _h)

function img(_src, _w, _h) {
    var out = '<img src="' + _src + '"';
    if (_w) {
        out = out + ' width="' + _w + '"';
    }
    if (_h) {
        out = out + ' height="' + _h + '"';
    }
    return out + ' border="0"/>';
}

/////////////////////////////////////////////////////////////////////
// function name: getImage(_id, _src, _fade, _func, _imgID, _prepend)

function getImage(_id, _src, _fade, _func, _imgID, _prepend) {
    $(_id).addClass('loading');
    var img = new Image();
    $(img).load(function () {
        $(this).hide();
        if (_imgID) {
            $(this).attr('id', _imgID);
        }
        $(_id).removeClass('loading');
        if (_prepend) {
            $(_id).prepend(this);
        } else {
            $(_id).append(this);
        }
        if (_fade) {
            $(this).fadeIn(_fade);
        } else {
            $(this).show();
        }
        $(this).attr('border', '0');
		
        if (_func) {
            eval(_func);
        }
    }).attr('src', _src);
}


/////////////////////////////////////////////////////////////////////
// function name: sec2hms(_sec)
function sec2hms(_sec) {
	_sec = nInt(_sec);
    var hours = "00" + Math.floor(_sec / 3600); 
	var minutes = "00" + Math.floor((_sec / 60) % 60);
	var seconds = "00" + Math.floor(_sec % 60); 
    var hms = hours.substr(hours.length-2, 2) +
		':'+ minutes.substr(minutes.length-2, 2) +
	    	':'+ seconds.substr(seconds.length-2, 2);
    return hms;
}

/////////////////////////////////////////////////////////////////////
// function name: hms2sec(_hms)
function hms2sec(_hms) {
	var sec = 0;
	if (_hms.toString().match(/:/g).length < 2) { return sec; }
	_hms = _hms.split(":");
	sec += nInt(_hms[0]) * 60 * 60;
	sec += nInt(_hms[1]) * 60;
	sec += nInt(_hms[2]);
	//console.debug("hms2sec: "+ sec);//DEBUG
	//console.debug("hms2sec: "+ sec * 1);//DEBUG
    return nInt(sec);
}

/////////////////////////////////////////////////////////////////////
// function name: email(_name, _user, _domain)
function email(_name, _user, _domain) {
	if (_name) {
		document.write('<a href="mailto:'+ _user +'@'+ _domain +'" title="'+ _name +'">'+ _name +'</a>');
	} else {
		document.write('<a href="mailto:'+ _user +'@'+ _domain +'" title="'+ _user +'@'+ _domain +'">'+ _user +'@'+ _domain +'</a>');
	}
}


/////////////////////////////////////////////////////////////////////
// function name: roundNumber(num, dec)
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

/////////////////////////////////////////////////////////////////////
// function name: roundNumber(num, dec)



/////////////////////////////////////////////////////////////////////
// function name: change_style(_find, _change) 
function change_style(_find, _change) {
	$("link").each(
	function() {
		if ( $(this).attr("href") ) {
			if ( $(this).attr("href").toString().indexOf(_find) > -1 ) {
				var _src = $(this).attr("href").toString().replace(_find, _change);
				$(this).attr("href", _src);
			}
		}
	});
}

/////////////////////////////////////////////////////////////////////
// function name: set_style(_path)
function setStyle(_path) {
	var _ok = 1;
	var _head = document.getElementsByTagName('head').item(0);
	var _script = _head.getElementsByTagName('link');
	for (var i=0; i < _script.length; i++) {
		if (_script[i].href) {
			var _src = _script[i].href.split('/');
			if (_src) {
				if (_src[_src.length -1].toString().indexOf(_path) > -1) {
					_ok = 0;
				}
			}
		}
	}	
	if (_ok) {
		var Node = document.createElement('link');
		Node.type = 'text/css';
		Node.rel = 'stylesheet';
		Node.href = _path;
		Node.media = 'screen';
		_head.appendChild(Node);
	}
	return true;
}
function set_style(_path) {
	return setStyle(_path);
}

/////////////////////////////////////////////////////////////////////
// function name: setScript(_path)
function setScript(_path) {
	var _ok = 1;
	var _head = document.getElementsByTagName('head').item(0);
	var _script = _head.getElementsByTagName('script');
	for (var i=0; i < _script.length; i++) {
		if (_script[i].src) {
			var _src = _script[i].src.split('/');
			if (_src) {
				if (_src[_src.length -1].toString().indexOf(_path) > -1) {
					_ok = 0;
				}
			}
		}
	}
	if (_ok) {
		_path += '?' + Math.random(0, 1000) + '=' + Math.random(0, 1000);
		var Node = document.createElement('script');
		Node.setAttribute('language', 'javascript');
		Node.setAttribute('type', 'text/javascript');
		Node.setAttribute('src', _path);
		_head.appendChild(Node);
	}
    return true;
}


function set_script(_path) {
	return setScript(_path);
}


/////////////////////////////////////////////////////////////////////
// function name: set_link_image(_path)
function set_link_image(_path) {
	var _ok = 1;
	var _head = document.getElementsByTagName('head').item(0);
	var _script = _head.getElementsByTagName('link');
	for (var i=0; i < _script.length; i++) {
		if (_script[i].rel) {
			var _rel = _script[i].rel;
			if (_rel) {
				if (_rel == "image_src") {
					_script[i].href = _path;
					_ok = 0;
				}
			}
		}
	}	
	if (_ok) {
		var Node = document.createElement('link');
		Node.rel = 'image_src';
		Node.href = _path;
		_head.appendChild(Node);
	}
	return true;
}


function cDebug(_msg) {
	try {
		if (console) {
			console.debug(_msg);		
		}
	} catch (e) { 
		//alert( _msg ); 
		//$("#MSG").html( $("#MSG").html() + "<BR>"+ _msg);
		
	}
}
			
