﻿

function RemoveCrossScripting(str) {
	if (str)
		return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")
	else
		return "";
}

function ResizeImage(img, size) {
	if (img && size && img.width > size)
		img.width = size;
	if (img && size && img.height > size)
		img.height = size;
}
var toggle = 0;
var menuClick = 0;
function toggleOff() {
	if (toggle == 1 && menuClick == 0) {
		toggle = 0;
		hideMenus(-1);
	}
	menuClick = 0;

}
function toggleClick(divID) {

	if (toggle == 0)
		toggle = 1;
	else
		toggle = 0;
	var menu = document.getElementById("LocalMenu" + divID)
	if (menu) {

		if (toggle == 1) {
			menu.style.display = 'block';
		}
		else {
			menu.style.display = 'none';			
		}
	}
	menuClick = 1;
}
function hideMenus(divID) {
	var i = 0;
	while (1) {
		var menu = document.getElementById("LocalMenu" + i)		
		if (!menu)
			break;
		if (menu.style && i != divID) {
			menu.style.display = 'none';						
		}
		i++;
	}
	showMenu(divID);
}
function showMenu(divID) {
	var menu = document.getElementById("LocalMenu" + divID)
	

	if (toggle == 1 && menu) {
		menu.style.display = 'block';
	}
}


function ShowElementByID(ID, HideObject) {
	var object = document.getElementById(ID);
	if (object && object.style) {
		if (object.style.display == 'none')
			object.style.display = '';
		else
			object.style.display = 'none';
	}
	if (HideObject)
		HideObject.style.display = 'none';
}

function FixHeight() {
	var left = document.getElementById("left");
	var right = document.getElementById("right");
	var content = document.getElementById("content");
	if (left && right && content) {
		var height = 500;
		if (left.offsetHeight >= right.offsetHeight && left.offsetHeight > 500)
			height = left.offsetHeight;
		else if (right.offsetHeight >= left.offsetHeight && right.offsetHeight > 500)
			height = right.offsetHeight;


		right.style.height = height + "px";
		left.style.height = height + "px";
		content.style.height = height + "px";
	}
}
function utilWindow(url, W, H, toolbar, scroll) {
	OpenWindow(url, W, H, toolbar, scroll);
}
function OpenWindow(url, W, H, toolbar, scroll) {
	var uniqueId = 'NewWindow_' + new Date().getTime();
	var width = "700", height = "450";
	if (W)
		width = W;
	if (H)
		height = H;

	var myToolBar = "yes";
	if (toolbar)
		myToolBar = toolbar;
	var scrollbars = "yes";
	if (scroll)
		scrollbars = scroll;


	var left = (screen.width / 2) - width / 2;
	var top = (screen.height / 2) - height / 2;
	var styleStr = 'toolbar=' + myToolBar + ',location=no,directories=no,status=no,menubar=no,scrollbars=' + scrollbars + ',resizable=' + scrollbars + ',copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',screenX=' + left + ',screenY=' + top;
	open(url, uniqueId, styleStr);
}
function ConvertToInt(Input, Default) {
	var re = new RegExp('-?[0-9]+');
	var m = re.exec(Input);
	if (m == null || isNaN(m))
		m = Default;
	return parseInt(m);
}
function ShowHideRoot(rootID) {
	var menu = document.getElementById("RootCategory" + rootID);
	var link = document.getElementById("categLink" + rootID);
	if (menu.style.display == '') {
		menu.style.display = "none";

		link.innerHTML = "[+]";
	}
	else {
		menu.style.display = '';
		link.innerHTML = "[-]";
		
	}


}
function ConvertToDouble(Input, Default) {
	var re = new RegExp('-?[0-9]*\\.?[0-9]+');
	var m = re.exec(Input);
	if (m == null || isNaN(m))
		m = Default;
	return m;
}
function formatCurrency(amount) {
	
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = Math.round(i * 100) / 100;	
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return "$" + s;
}



function RoundToDecimal(value, x) {
	return Math.round(value * Math.pow(10, x)) / Math.pow(10, x);

}
function setSelectedIndex(s, v) {
	for (var i = 0; i < s.options.length; i++) {
		if (s.options[i].value == v) {
			s.options[i].selected = true;
			return true;
		}
	}
	return false;
}

function ShowHideDivByID(ID) {
	var control = document.getElementById(ID);
	if (control) {
		if (control.style.display == "none")
			control.style.display = '';
		else
			control.style.display = "none";
	}
}


function MoveCategoryMenu(next) {

	var div = $('div.category-menu'),
                 ul = $('ul.category-menu');
	// unordered list's left margin
                 ulPadding = 0;
                 var Scroll = div.scrollLeft() + (450 * next);
	if (Scroll < 0)
		Scroll = 0;

	var lastLi = ul.find('li:last-child');
	var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding - 950;
	if (Scroll > ulWidth)
		Scroll = ulWidth;
	if (div.scrollLeft() != Scroll) {

		var ScrollDif = Math.abs(div.scrollLeft() - Scroll);
		var Proportion = ScrollDif * 800 / (450);
		div.animate({ scrollLeft: Scroll }, Proportion);
	}
	else if (Scroll == 0) {
		div.animate({ scrollLeft: ulWidth }, 500);
	}
	else if (Scroll == ulWidth) {
		div.animate({ scrollLeft: 0 }, 500);
	}
}

var AutoTimer = true;
function AutoMovePicture(){
    if(AutoTimer)
    {
        var div = $('div.picture-menu'), ul = $('ul.picture-menu');
	    // unordered list's left margin
	    ulPadding = 0;
	    var Scroll = div.scrollLeft() + 474;
	    if (Scroll < 0)
		    Scroll = 0;

	    var lastLi = ul.find('li:last-child');
	    var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding - 950;
	    if (Scroll > ulWidth)
		    Scroll = ulWidth;
	    if (div.scrollLeft() != Scroll) {

		    var ScrollDif = Math.abs(div.scrollLeft() - Scroll);
		    var Proportion = ScrollDif * 2000 / (474);
		    div.animate({ scrollLeft: Scroll }, Proportion);
	    }
	    else if (Scroll == 0) {
		    div.animate({ scrollLeft: ulWidth }, 0);
	    }
	    else if (Scroll == ulWidth) {
		    div.animate({ scrollLeft: 0 }, 0);
	    }
	    setTimeout('AutoMovePicture()', 10000);
    }
    
}
function SetTimerHover(value)
{
    AutoTimer = value;
    if(value)
        setTimeout('AutoMovePicture()', 10000);
}
function MovePictureMenu(next) {

	var div = $('div.picture-menu'),
                 ul = $('ul.picture-menu');
	// unordered list's left margin
	ulPadding = 0;
	var Scroll = div.scrollLeft() + (474 * next);
	if (Scroll < 0)
		Scroll = 0;

	var lastLi = ul.find('li:last-child');
	var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding - 950;
	if (Scroll > ulWidth)
		Scroll = ulWidth;
	if (div.scrollLeft() != Scroll) {

		var ScrollDif = Math.abs(div.scrollLeft() - Scroll);
		var Proportion = ScrollDif * 1000 / (474);
		div.animate({ scrollLeft: Scroll }, Proportion);
	}
	else if (Scroll == 0) {
		div.animate({ scrollLeft: ulWidth }, 0);
	}
	else if (Scroll == ulWidth) {
		div.animate({ scrollLeft: 0 }, 0);
	}
	
}



/*****************************/
/*    String Builder         */
/*****************************/

function StringBuilder(value) {
	this.strings = new Array("");
	this.Append(value);
}

// Appends the given value to the end of this instance.

StringBuilder.prototype.Append = function(value) {
	if (value) {
		this.strings.push(value);
	}
}

// Clears the string buffer

StringBuilder.prototype.clear = function() {
	this.strings.length = 1;
}

// Converts this instance to a String.

StringBuilder.prototype.toString = function() {
	return this.strings.join("");
}

String.prototype.trim = function() {
	return
	(this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))
}
String.prototype.startsWith = function(str)
{ return (this.match("^" + str) == str) }
String.prototype.endsWith = function(str)
{ return (this.match(str + "$") == str) }

String.prototype.contains = function(str) {
	if ((this.match(str)) == null)
		return false;
	return true;
}


/***************************************/
/*             Hash Map                */
/***************************************/
function Map() {
	// members
	this.keyArray = new Array(); // Keys
	this.valArray = new Array(); // Values

	// methods
	this.put = put;
	this.get = get;
	this.size = size;
	this.clear = clear;
	this.keySet = keySet;
	this.valSet = valSet;
	this.showMe = showMe;   // returns a string with all keys and values in map.
	this.findIt = findIt;
	this.remove = remove;
}

function put(key, val) {
	var elementIndex = this.findIt(key);

	if (elementIndex == (-1)) {
		this.keyArray.push(key);
		this.valArray.push(val);
	}
	else {
		this.valArray[elementIndex] = val;
	}
}

function get(key) {
	var result = null;
	var elementIndex = this.findIt(key);

	if (elementIndex != (-1)) {
		result = this.valArray[elementIndex];
	}

	return result;
}

function remove(key) {
	var result = null;
	var elementIndex = this.findIt(key);

	if (elementIndex != (-1)) {
		this.keyArray = this.keyArray.removeAt(elementIndex);
		this.valArray = this.valArray.removeAt(elementIndex);
	}

	return;
}

function size() {
	return (this.keyArray.length);
}

function clear() {
	for (var i = 0; i < this.keyArray.length; i++) {
		this.keyArray.pop(); this.valArray.pop();
	}
}

function keySet() {
	return (this.keyArray);
}

function valSet() {
	return (this.valArray);
}

function showMe() {
	var result = "";

	for (var i = 0; i < this.keyArray.length; i++) {
		result += "Key: " + this.keyArray[i] + "\tValues: " + this.valArray[i] + "\n";
	}
	return result;
}

function findIt(key) {
	var result = (-1);

	for (var i = 0; i < this.keyArray.length; i++) {
		if (this.keyArray[i] == key) {
			result = i;
			break;
		}
	}
	return result;
}

function removeAt(index) {
	var part1 = this.slice(0, index);
	var part2 = this.slice(index + 1);

	return (part1.concat(part2));
}
Array.prototype.removeAt = removeAt;



/************************************/
/*       String Builder             */
/************************************/

function StringBuilder(value) {
	this.strings = new Array("");
	this.Append(value);
}

// Appends the given value to the end of this instance.

StringBuilder.prototype.Append = function(value) {
	if (value) {
		this.strings.push(value);
	}
}

// Clears the string buffer

StringBuilder.prototype.clear = function() {
	this.strings.length = 1;
}

// Converts this instance to a String.

StringBuilder.prototype.toString = function() {
	return this.strings.join("");
}

String.prototype.trim = function() {
	return
	(this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))
}
String.prototype.startsWith = function(str)
{ return (this.match("^" + str) == str) }
String.prototype.endsWith = function(str)
{ return (this.match(str + "$") == str) }

String.prototype.contains = function(str) {
	if ((this.match(str)) == null)
		return false;
	return true;
}
// mredkj.com
function NumberFormat(num, inputDecimal) {
	this.VERSION = 'Number Format v1.5.4';
	this.COMMA = ',';
	this.PERIOD = '.';
	this.DASH = '-';
	this.LEFT_PAREN = '(';
	this.RIGHT_PAREN = ')';
	this.LEFT_OUTSIDE = 0;
	this.LEFT_INSIDE = 1;
	this.RIGHT_INSIDE = 2;
	this.RIGHT_OUTSIDE = 3;
	this.LEFT_DASH = 0;
	this.RIGHT_DASH = 1;
	this.PARENTHESIS = 2;
	this.NO_ROUNDING = -1
	this.num;
	this.numOriginal;
	this.hasSeparators = false;
	this.separatorValue;
	this.inputDecimalValue;
	this.decimalValue;
	this.negativeFormat;
	this.negativeRed;
	this.hasCurrency;
	this.currencyPosition;
	this.currencyValue;
	this.places;
	this.roundToPlaces;
	this.truncate;
	this.setNumber = setNumberNF;
	this.toUnformatted = toUnformattedNF;
	this.setInputDecimal = setInputDecimalNF;
	this.setSeparators = setSeparatorsNF;
	this.setCommas = setCommasNF;
	this.setNegativeFormat = setNegativeFormatNF;
	this.setNegativeRed = setNegativeRedNF;
	this.setCurrency = setCurrencyNF;
	this.setCurrencyPrefix = setCurrencyPrefixNF;
	this.setCurrencyValue = setCurrencyValueNF;
	this.setCurrencyPosition = setCurrencyPositionNF;
	this.setPlaces = setPlacesNF;
	this.toFormatted = toFormattedNF;
	this.toPercentage = toPercentageNF;
	this.getOriginal = getOriginalNF;
	this.moveDecimalRight = moveDecimalRightNF;
	this.moveDecimalLeft = moveDecimalLeftNF;
	this.getRounded = getRoundedNF;
	this.preserveZeros = preserveZerosNF;
	this.justNumber = justNumberNF;
	this.expandExponential = expandExponentialNF;
	this.getZeros = getZerosNF;
	this.moveDecimalAsString = moveDecimalAsStringNF;
	this.moveDecimal = moveDecimalNF;
	this.addSeparators = addSeparatorsNF;
	if (inputDecimal == null) {
		this.setNumber(num, this.PERIOD);
	} else {
		this.setNumber(num, inputDecimal);
	}
	this.setCommas(true);
	this.setNegativeFormat(this.LEFT_DASH);
	this.setNegativeRed(false);
	this.setCurrency(false);
	this.setCurrencyPrefix('$');
	this.setPlaces(2);
}
function setInputDecimalNF(val) {
	this.inputDecimalValue = val;
}
function setNumberNF(num, inputDecimal) {
	if (inputDecimal != null) {
		this.setInputDecimal(inputDecimal);
	}
	this.numOriginal = num;
	this.num = this.justNumber(num);
}
function toUnformattedNF() {
	return (this.num);
}
function getOriginalNF() {
	return (this.numOriginal);
}
function setNegativeFormatNF(format) {
	this.negativeFormat = format;
}
function setNegativeRedNF(isRed) {
	this.negativeRed = isRed;
}
function setSeparatorsNF(isC, separator, decimal) {
	this.hasSeparators = isC;
	if (separator == null) separator = this.COMMA;
	if (decimal == null) decimal = this.PERIOD;
	if (separator == decimal) {
		this.decimalValue = (decimal == this.PERIOD) ? this.COMMA : this.PERIOD;
	} else {
		this.decimalValue = decimal;
	}
	this.separatorValue = separator;
}
function setCommasNF(isC) {
	this.setSeparators(isC, this.COMMA, this.PERIOD);
}
function setCurrencyNF(isC) {
	this.hasCurrency = isC;
}
function setCurrencyValueNF(val) {
	this.currencyValue = val;
}
function setCurrencyPrefixNF(cp) {
	this.setCurrencyValue(cp);
	this.setCurrencyPosition(this.LEFT_OUTSIDE);
}
function setCurrencyPositionNF(cp) {
	this.currencyPosition = cp
}
function setPlacesNF(p, tr) {
	this.roundToPlaces = !(p == this.NO_ROUNDING);
	this.truncate = (tr != null && tr);
	this.places = (p < 0) ? 0 : p;
}
function addSeparatorsNF(nStr, inD, outD, sep) {
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}
function toFormattedNF() {
	var pos;
	var nNum = this.num;
	var nStr;
	var splitString = new Array(2);
	if (this.roundToPlaces) {
		nNum = this.getRounded(nNum);
		nStr = this.preserveZeros(Math.abs(nNum));
	} else {
		nStr = this.expandExponential(Math.abs(nNum));
	}
	if (this.hasSeparators) {
		nStr = this.addSeparators(nStr, this.PERIOD, this.decimalValue, this.separatorValue);
	} else {
		nStr = nStr.replace(new RegExp('\\' + this.PERIOD), this.decimalValue);
	}
	var c0 = '';
	var n0 = '';
	var c1 = '';
	var n1 = '';
	var n2 = '';
	var c2 = '';
	var n3 = '';
	var c3 = '';
	var negSignL = (this.negativeFormat == this.PARENTHESIS) ? this.LEFT_PAREN : this.DASH;
	var negSignR = (this.negativeFormat == this.PARENTHESIS) ? this.RIGHT_PAREN : this.DASH;
	if (this.currencyPosition == this.LEFT_OUTSIDE) {
		if (nNum < 0) {
			if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n1 = negSignL;
			if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n2 = negSignR;
		}
		if (this.hasCurrency) c0 = this.currencyValue;
	} else if (this.currencyPosition == this.LEFT_INSIDE) {
		if (nNum < 0) {
			if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n0 = negSignL;
			if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n3 = negSignR;
		}
		if (this.hasCurrency) c1 = this.currencyValue;
	}
	else if (this.currencyPosition == this.RIGHT_INSIDE) {
		if (nNum < 0) {
			if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n0 = negSignL;
			if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n3 = negSignR;
		}
		if (this.hasCurrency) c2 = this.currencyValue;
	}
	else if (this.currencyPosition == this.RIGHT_OUTSIDE) {
		if (nNum < 0) {
			if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n1 = negSignL;
			if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n2 = negSignR;
		}
		if (this.hasCurrency) c3 = this.currencyValue;
	}
	nStr = c0 + n0 + c1 + n1 + nStr + n2 + c2 + n3 + c3;
	if (this.negativeRed && nNum < 0) {
		nStr = '<font color="red">' + nStr + '</font>';
	}
	return (nStr);
}
function toPercentageNF() {
	nNum = this.num * 100;
	nNum = this.getRounded(nNum);
	return nNum + '%';
}
function getZerosNF(places) {
	var extraZ = '';
	var i;
	for (i = 0; i < places; i++) {
		extraZ += '0';
	}
	return extraZ;
}
function expandExponentialNF(origVal) {
	if (isNaN(origVal)) return origVal;
	var newVal = parseFloat(origVal) + '';
	var eLoc = newVal.toLowerCase().indexOf('e');
	if (eLoc != -1) {
		var plusLoc = newVal.toLowerCase().indexOf('+');
		var negLoc = newVal.toLowerCase().indexOf('-', eLoc);
		var justNumber = newVal.substring(0, eLoc);
		if (negLoc != -1) {
			var places = newVal.substring(negLoc + 1, newVal.length);
			justNumber = this.moveDecimalAsString(justNumber, true, parseInt(places));
		} else {
			if (plusLoc == -1) plusLoc = eLoc;
			var places = newVal.substring(plusLoc + 1, newVal.length);
			justNumber = this.moveDecimalAsString(justNumber, false, parseInt(places));
		}
		newVal = justNumber;
	}
	return newVal;
}
function moveDecimalRightNF(val, places) {
	var newVal = '';
	if (places == null) {
		newVal = this.moveDecimal(val, false);
	} else {
		newVal = this.moveDecimal(val, false, places);
	}
	return newVal;
}
function moveDecimalLeftNF(val, places) {
	var newVal = '';
	if (places == null) {
		newVal = this.moveDecimal(val, true);
	} else {
		newVal = this.moveDecimal(val, true, places);
	}
	return newVal;
}
function moveDecimalAsStringNF(val, left, places) {
	var spaces = (arguments.length < 3) ? this.places : places;
	if (spaces <= 0) return val;
	var newVal = val + '';
	var extraZ = this.getZeros(spaces);
	var re1 = new RegExp('([0-9.]+)');
	if (left) {
		newVal = newVal.replace(re1, extraZ + '$1');
		var re2 = new RegExp('(-?)([0-9]*)([0-9]{' + spaces + '})(\\.?)');
		newVal = newVal.replace(re2, '$1$2.$3');
	} else {
		var reArray = re1.exec(newVal);
		if (reArray != null) {
			newVal = newVal.substring(0, reArray.index) + reArray[1] + extraZ + newVal.substring(reArray.index + reArray[0].length);
		}
		var re2 = new RegExp('(-?)([0-9]*)(\\.?)([0-9]{' + spaces + '})');
		newVal = newVal.replace(re2, '$1$2$4.');
	}
	newVal = newVal.replace(/\.$/, '');
	return newVal;
}
function moveDecimalNF(val, left, places) {
	var newVal = '';
	if (places == null) {
		newVal = this.moveDecimalAsString(val, left);
	} else {
		newVal = this.moveDecimalAsString(val, left, places);
	}
	return parseFloat(newVal);
}
function getRoundedNF(val) {
	val = this.moveDecimalRight(val);
	if (this.truncate) {
		val = val >= 0 ? Math.floor(val) : Math.ceil(val);
	} else {
		val = Math.round(val);
	}
	val = this.moveDecimalLeft(val);
	return val;
}
function preserveZerosNF(val) {
	var i;
	val = this.expandExponential(val);
	if (this.places <= 0) return val;
	var decimalPos = val.indexOf('.');
	if (decimalPos == -1) {
		val += '.';
		for (i = 0; i < this.places; i++) {
			val += '0';
		}
	} else {
		var actualDecimals = (val.length - 1) - decimalPos;
		var difference = this.places - actualDecimals;
		for (i = 0; i < difference; i++) {
			val += '0';
		}
	}
	return val;
}
function justNumberNF(val) {
	newVal = val + '';
	var isPercentage = false;
	if (newVal.indexOf('%') != -1) {
		newVal = newVal.replace(/\%/g, '');
		isPercentage = true;
	}
	var re = new RegExp('[^\\' + this.inputDecimalValue + '\\d\\-\\+\\(\\)eE]', 'g');
	newVal = newVal.replace(re, '');
	var tempRe = new RegExp('[' + this.inputDecimalValue + ']', 'g');
	var treArray = tempRe.exec(newVal);
	if (treArray != null) {
		var tempRight = newVal.substring(treArray.index + treArray[0].length);
		newVal = newVal.substring(0, treArray.index) + this.PERIOD + tempRight.replace(tempRe, '');
	}
	if (newVal.charAt(newVal.length - 1) == this.DASH) {
		newVal = newVal.substring(0, newVal.length - 1);
		newVal = '-' + newVal;
	}
	else if (newVal.charAt(0) == this.LEFT_PAREN
&& newVal.charAt(newVal.length - 1) == this.RIGHT_PAREN) {
		newVal = newVal.substring(1, newVal.length - 1);
		newVal = '-' + newVal;
	}
	newVal = parseFloat(newVal);
	if (!isFinite(newVal)) {
		newVal = 0;
	}
	if (isPercentage) {
		newVal = this.moveDecimalLeft(newVal, 2);
	}
	return newVal;
}
