var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined'); 

function getByName(f, n) {
	for(var i=0;i<f.elements.length;i++) {
		if(f.elements[i].name==n) {
			return f.elements[i];
		}
	}
	return false;
}
function getFromName(nam, idx) {
	var res=new Array();
	var i=0;
	res[i]=new String("");
	for(var j=0;j<nam.length;j++) {
		if(nam.charAt(j)=="_") {
			i++;
			res[i]=new String("");
		} else {
			res[i]+=nam.charAt(j);
		}
	}
	return res[idx];
}

function browserWidth() {
	var w=(ie?document.body.clientWidth:window.innerWidth);
	return w;
}
function browserHeight() {
	var h=(ie?document.body.clientHeight:window.innerHeight);
	return h;
}


function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;		
}

function parseVal(v) {
	var t = parseFloat(v);
	return isNaN(t) ? 0 : t;
}

function intLen(v, d) {
	var s = new String(v);
	while(s.length<d) {
		s = "0" + s;
	}
	return s;
}

function roundDec(a, dec) {
	var b=Math.pow(10, dec);

	var res=Math.round(a*b)/b;
	var s=new String(res);
	var c=s.indexOf('.');
	if(c<0) {
		c=s.length-1;
	}

	var d=s.length-c-1;
	if(d<dec) {
		if(d==0) {
			res+=".";
		}
		for(var i=d;i<dec;i++) {
			res+="0";
		}
	}
	if(isNaN(parseFloat(res))) {
		res="";
	}
	return res;
}

function number_format(v) {

	var decPlaces = 2;
	var separator = ".";
	var comma = ",";
	if(number_format.arguments.length>1) {
		decPlaces = arguments[1];
		separator = arguments[2];
		comma = arguments[3];
	}
	var res = roundDec(v, decPlaces);
	return res;
}
function FormatNumberBy3(num, decpoint, sep) {
  // check for missing parameters and use defaults if so
  if (arguments.length == 2) {
    sep = ",";
  }
  if (arguments.length == 1) {
    sep = ",";
    decpoint = ".";
  }
  // need a string for operations
  num = num.toString();
  // separate the whole number and the fraction if possible
  a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";


  if (typeof(x) != "undefined") {
    // reverse the digits. regexp works from left to right.
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    // add seperators. but undo the trailing one, if there
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    // reverse again to get back the number
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    // add the fraction back in, if it was there
    if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;
  }
  return x;
}

function number_format(a, b, c, d) {
	// number_format(number, decimals, comma, formatSeparator)
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if(!f[0]) f[0] = '0';
	if(!f[1]) f[1] = '';
	if(f[1].length < b){
		g = f[1];
		for(i = f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j += 3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '': c;
	return f[0] + c + f[1];
}

function getDivById(id) {
	var el = document.getElementById(id);
	if(!el) {
		el = document.createElement("DIV");
		el.id = id;
		el.style.display = "none";
		document.getElementsByTagName("BODY")[0].appendChild(el);
	}
	return el;
}

function getInputElementsList(inputElement) {
	if (inputElement) {
		var tmpElement = inputElement; 
		var isOptions = inputElement.options;
		
		if((!tmpElement.length && !isOptions) || (tmpElement.length && isOptions)) {
			tmpElement = [tmpElement];
		}
		return tmpElement;
	}
}

function inArray(needle, haystack) {
	for (var i = 0; i < haystack.length; i++) {
		if (needle == haystack[i]) {
			return true;
		}
	}
	return false;
}

function changeSortForm1(ev,divName) {
	var target = ie ? window.event.srcElement : ev.target;
	var pDiv = document.getElementById("popupDiv");
	pDiv.style.left = (findPosX(target)-10)+"px";
	pDiv.style.top = (findPosY(target)+15)+"px";
	pDiv.style.width = "200px";
	pDiv.style.display = "block";
}
function hidePopup1() {
	var pDiv = document.getElementById("popupDiv");
	pDiv.style.display="none";
}

function changeFilterForm(ev,divName) {
	var target = ie ? window.event.srcElement : ev.target;
	var pDiv = document.getElementById("popupDiv1");
	pDiv.style.left = (findPosX(target)-10)+"px";
	pDiv.style.top = (findPosY(target)+15)+"px";
	pDiv.style.width = "250px";
	pDiv.style.display = "block";
}
function hideFilterPopup() {
	var pDiv = document.getElementById("popupDiv1");
	pDiv.style.display="none";
}

