// Browser
var Browser = 
{
	is :
	{
		IE:     !!(window.attachEvent && !window.opera),
		Opera:  !!window.opera,
		WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
		Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
		MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
	}
}

// document
document.getCookie = function(sName)
{
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++) {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0])
      return decodeURIComponent(aCrumb[1]);
  }

  return null;
}

document.setCookie = function(sName, sValue, sExpires)
{
  var sCookie = sName + "=" + encodeURIComponent(sValue);
  if (sExpires != null) {
    sCookie += "; expires=" + sExpires;
  }

  document.cookie = sCookie;
}

document.removeCookie = function(sName,sValue) {
  document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

/**
 * Gets URL
 */
function getURL()
{
	return location.href.lastIndexOf("?") == -1 ? 
	       location.href.substring((location.href.lastIndexOf("/")) + 1) : 
		   location.href.substring((location.href.lastIndexOf("/")) + 1, location.href.lastIndexOf("?"));
}

/**
 * Go URL
 */
function goURL(url, msg)
{
	if (typeof(msg) == 'string') {
		if (confirm(msg)) {
			location.href = url;
		}
	} else {
		location.href = url;
	}
}

/**
 * Open a window
 */
function openWin(url, width, height)
{
	window.open(url,'','width='+ width +',height='+ height +',toolbar=no, status=no, menubar=no, resizable=no, scrollbars=yes, left=20,top=20');	
}

/**
 * Gets position
 */
function getPosition(o)
{
	var t = l = 0;
	
	do {
		t += o.offsetTop || 0;
		l += o.offsetLeft || 0;
		o = o.offsetParent;
	} while (o)
	
    return {top:t,left:l};
}

// Gets event object
function getEventObject(e)
{
	return e || window.event;
}

// Gets pointer position
function getPotinerPosition(e)
{
	e = e || getEventObject(e);
	
	var x = e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
	var y = e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
	
	return {'x' : x, 'y' : y}
}

// Set opacity
function setOpacity(elem, level)
{
	if (elem.filters)
	  elem.style.filter = 'alpha(opacity=' + level * 100 + ')';
	else
	  elem.style.opacity = level;
}

// css
function css(elem, prop) {
	for(var i in prop) {
		if(i == 'opacity')
		  setOpacity(elem,prop[i]);
		else
		  elem.style[i] = prop[i];
    }
    
		return elem;
}

function showNode(id)
{
	var node = document.getElementById(id);
	
	if (node)
	  node.style.display = node.style.display == 'none' ? '' : 'none';
}

function showNote(elem, msg, level)
{
	if (elem) {
		elem.innerHTML = msg;
		elem.style.color = level ? '#00CC00' : '#FF0000';
	}
}

// submit form
function submitForm(form_name)
{
	if (document.forms[form_name]) {
		document.forms[form_name].submit();
	}
}

// format number
function formatNumber(num, point)
{
	var a_int=parseInt(num * Math.pow(10,(point+1)));
	
	if (a_int == 0) {
		return "0.00";
	}
	
	var a_str=a_int.toString();
	
	// positive number
	if (a_str.substring(0,1) != "-") {
		var b_int = parseInt(a_str.substring(a_str.length-1, a_str.length));
	
		if (b_int >= 5) {
			a_int += 10;
		}
	} else { // negative number
		var b_int = parseInt(a_str.substring(a_str.length-1, a_str.length));
		if (b_int >= 5) {
			a_int -= 10
		}
	}
	
	a_str=a_int.toString();
	
	var l_str = a_str.substring(0, a_str.length-(point+1));
	var r_str = a_str.substring(a_str.length-point-1, a_str.length);
	r_str = r_str.substring(0,point);
	var c_str = l_str + "." + r_str;
	
	if (num.toString().substring(0,3)=="-0.") {
		c_str="-0." + r_str;
	}
		
	if(num.toString().substring(0,2)=="0.") {
		c_str="0." + r_str;
	}
	
	return c_str;	
}

// window box
function winBox(title, content, width, height)
{
	var container = document.getElementById('window');
	if (!container) return;
	container.style.display = "block";
	container.innerHTML = "";
	
	
	// mask	
	var mask = document.createElement('div'); 
	mask.style.position = "absolute";
	mask.style.top = 0;
	mask.style.left = 0;
	mask.style.width = document.body.clientWidth + 'px';
	mask.style.height = (document.body.clientHeight > document.documentElement.clientHeight ? document.body.clientHeight : document.documentElement.clientHeight) + 'px';
	mask.style.zIndex = '1000';
	mask.style.background ="#000000"; 
	mask.style.filter     = "progid:DXImageTransform.Microsoft.Alpha(style=4,opacity=50,finishOpacity=10)"; 
	mask.style.opacity    = "0.5";
	
	// win
	var win = document.createElement('div');
	win.className = 'win';
	win.style.position = 'absolute';
	win.style.zIndex = '1001';
	win.style.left = '33%';
	win.style.top = '33%';
	win.style.background = '#FFFFFF';
	win.style.width = (Utils.isInt(width) ? width : 500) + 'px';
	if (Utils.isInt(height)) {
		win.style.height = height + 'px';
	}
	
	var winTitle = document.createElement('div');
	winTitle.className = 'title';
	var winTxt = document.createElement('div');
	winTxt.className = 'txt';
	winTxt.innerHTML = title;
	var winClose = document.createElement('div');
	winClose.className = 'close';
	winClose.title = 'Close';
	winClose.onclick = function() {
		document.getElementById('window').style.display = 'none';
	}
	winTitle.appendChild(winClose);
	winTitle.appendChild(winTxt);
	
	var winContent = document.createElement('div');
	winContent.className = 'content';
	winContent.innerHTML = content;
	
	win.appendChild(winTitle);
	win.appendChild(winContent);
	
	container.appendChild(mask);
	container.appendChild(win);
}
