/**
* Utility functions.
* @author Vladan Antic
* @date January-November 2007.
*/

var isSafari = false;
var isMoz    = false;
var isIE     = false;
var isOpera  = false;

if (navigator.userAgent.indexOf("Safari") > 0) {
    isSafari = true;
} else if (navigator.product == "Gecko") {
    isMoz = true;
} else if (navigator.userAgent.indexOf('Opera') >= 0) {
    isOpera = true;
} else if (navigator.userAgent.indexOf('MSIE') >= 0 && navigator.userAgent.indexOf('Opera') < 0) {
    isIE = true;
}

/*
 * Helpful functions
 **************************/
function setElement(elm, prm) {
    if (elm == null || prm == null) return false;
    if (!(e = $(elm)))
        e = elm;
    
    for (var i=0; i<prm.length; i++){
        if (prm[i][0] == null || prm[i][1] == null)
            return false;
            
        k = prm[i][0];
        v = prm[i][1];
        
        eval("e."+k+" = v");
    }
    return true;
}

function getElementWidth(element) {
    element = $(element);
    return element.offsetWidth;
}

function getWindowWidth() {
  var w = 0;
  if (self.innerWidth)
    w = self.innerWidth;
  else if (document.documentElement && document.documentElement.clientWidth)
    w = document.documentElement.clientWidth;
  else if (document.body)
    w = document.body.clientWidth;
  return w;
}

function getWindowHeight() {
  var h = 0;
  if (self.innerHeight)
    h = self.innerHeight;
  else if (document.documentElement && document.documentElement.clientHeight)
    h = document.documentElement.clientHeight;
  else if (document.body)
    h = document.body.clientHeight;
  return h;
}

function getDocumentWidth() {
  var w = getWindowWidth();
  if(document.width && w < document.width) w = document.width;
  else if(document.body.offsetWidth && w < document.body.offsetWidth) w = document.body.offsetWidth;
  return w;
}

function getDocumentHeight() {
  var h = getWindowHeight();
  if(document.height && h < document.height) h = document.height;
  else if(document.body.offsetHeight && h < document.body.offsetHeight) h = document.body.offsetHeight;
  return h;
}

function getWindowYOffset() {
  var y = 0;
  if(window.pageYOffset)
    y = window.pageYOffset;
  else if (document.documentElement && document.documentElement.scrollTop)
    y = document.documentElement.scrollTop;
  return y;
}

function objAlpha(element, nOpacity) {
  var obj = $(element);
  obj.style.opacity = (nOpacity/100);
  obj.style.MozOpacity = (nOpacity/100);
  obj.style.KhtmlOpacity = (nOpacity/100);
  obj.style.filter = "alpha(opacity=" + nOpacity + ")";
}

/* ----------------------- */

function isAlien(a) {
  return isObject(a) && typeof a.constructor != 'function';
}

function isArray(a) {
  return isObject(a) && a.constructor == Array;
}

function isBoolean(a) {
  return typeof a == 'boolean';
}

function isEmpty(o) {
  var i, v;
  if (isObject(o)) {
    for (i in o) {
      v = o[i];
      if (isUndefined(v) && isFunction(v)) {
        return false;
      }
    }
  }
  return true;
}

function isFunction(a) {
  return typeof a == 'function';
}

function isNull(a) {
  return typeof a == 'object' && !a;
}

function isNumber(a) {
  return typeof a == 'number' && isFinite(a);
}

function isObject(a) {
  return (a && typeof a == 'object') || isFunction(a);
}

function isString(a) {
  return typeof a == 'string';
}

function isUndefined(a) {
  return typeof a == 'undefined';
}

function getPropertyByName(node, nodeName) {
  var arr = new Array();
  var items = node.getElementsByTagName("name");
  for (var i=0; i<items.length; i++) {
    if (nodeName == items[i].firstChild.nodeValue) {
      arr[0] = items[i].firstChild.nodeValue;
      arr[1] = items[i].parentNode.getElementsByTagName("value")[0].firstChild.nodeValue;
      return arr;
    }
  }
  return null;
}

function evalBoolean(value, defaultValue) {
  if (!isNull(value) && isString(value)) {
    return ("true" == value.toLowerCase() || "yes" == value.toLowerCase()) ? "true" : "false";
  } else {
    return defaultValue == true ? "true" : "false";
  }
}

function replaceWithValue(sourceString, regExp, element) {
  var retString = "";
  switch (element.type) {
    case 'checkbox':
    case 'radio':
    case 'text':
    case 'textarea':
    case 'password':
    case 'hidden':
    case 'select-one':
    case 'select-multiple':
      retString = sourceString.replace(regExp, encodeEscChr($F(element)));
      break;
    default:
      retString = sourceString.replace(regExp, encodeEscChr(element.innerHTML));
      break;
  }
  return retString;
}

function arrayToParameterString(array) {
  var str = '';
  for (var i=0; i<array.length; i++) {
    // escape parameter values
    var pair = array[i].split('=');
    if (pair.length == 2) {
      str += (i >= 0 ? '&' : '') + pair[0] + '=' + escape(pair[1]);
    } else {
      str += (i >= 0 ? '&' : '') + array[i];
    }
  }
  return str;
}

function decodeHtml(str) {
    str = str.replace(/&amp;/, "&");
    str = str.replace(/&lt;/, "<");
    str = str.replace(/&gt;/, ">");
    return str;
}

function encodeHtml(str) {
    str = str.replace(/&/g, "&amp;");
    str = str.replace(/</g, "&lt;");
    str = str.replace(/>/g, "&gt;");
    str = str.replace(/"/g, "&quot;");
    str = str.replace(/'/g, "&rsquo;");
    return str;
}

function encodeEscChr(inVar) {
    inVar = inVar.replace(/&/g, "%26");
    inVar = inVar.replace(/'/g, "%27");
    inVar = inVar.replace(/"/g, "%22");
    inVar = inVar.replace(/\?/g, "%3F");
    inVar = inVar.replace(/\$/g, "%24");
    inVar = inVar.replace(/>/g, "%3E");
    inVar = inVar.replace(/</g, "%3C");
    inVar = inVar.replace(/%/g, "%25");
    inVar = inVar.replace(/\+/g, "%2B");
    return inVar;
}  
 
function trimString(str) {
    return str.replace(/ /g, '');
}

function truncateString(str, len, sfx, exa){
    sfx = sfx || '';
    exa = exa || false;
    var ls = len;
    if (str.length > len){
        str = str.substr(0, len);
        if (!exa) {
            var ls = str.lastIndexOf(' ');
            if (ls < 1) ls = len;
        }
        str = str.substr(0, ls) + sfx;
    }
    return str;
}


var pop_up = false;
var dummy  = false;

function showPopUp(elm, id, width, left, top){
	if (pop_up) closePopUp();
	
	if (elm && id){
		var obj = $(elm);
		var pos = getPosition(obj);
		var popup = $(id);
		
		var leftPos = 0;
		var topPos = 0;
		
		if (left){
			var leftPos = eval(pos.x) + eval(left);
		} else {
			var leftPos = eval(pos.x);
		}
		
		if (top){
			var topPos = eval(pos.y) + eval(document.body.scrollTop) + eval(top);
		} else {
			var topPos = eval(pos.y) + eval(document.body.scrollTop);
		}
		
		popup.style.left = leftPos + 'px';
		popup.style.top = topPos + 'px';
				
		popup.style.width = width + 'px';
		popup.style.display = 'block';
		pop_up = popup;
		
		dummy = $('dummy');
		dummy.style.width  = getDocumentWidth() - 20 + 'px';
		dummy.style.height = getDocumentHeight() - 10 + 'px';
		dummy.style.display = 'block';
		
		btn = popup.getElementsByTagName('A')[0];
		btn.focus();
		
	} else {
		pop_up = false;
	}
}

function closePopUp(){
	if (pop_up){
		pop_up.style.display = 'none';
		pop_up = false;
	}        
	if (dummy){
		dummy.style.display = 'none';
		dummy = false;
	}        
}

function alertMessage(msg, width, left, top){
	width = width || 400;
	left  = left || 550;
	top   = top || 200;
	
	if (pop_up) closePopUp();

	var popup = $('alert_message');
	
	if (msg && popup){
		msg_div = popup.getElementsByTagName('DIV')[0];
		msg_div.innerHTML = msg;
		
		popup.style.left = left + 'px';
		popup.style.top = top + 'px';
		popup.style.width = width + 'px';
		popup.style.display = 'block';
		pop_up = popup;
		
		dummy = $('dummy');
		dummy.style.width  = getDocumentWidth() - 20 + 'px';
		dummy.style.height = getDocumentHeight() - 10 + 'px';
		dummy.style.display = 'block';
		
		btn = popup.getElementsByTagName('A')[0];
		btn.focus();
	}
}

