// Sniffer based on http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var uagent    = navigator.userAgent.toLowerCase();
var is_safari = ( (uagent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var is_opera  = (uagent.indexOf('opera') != -1);
var is_webtv  = (uagent.indexOf('webtv') != -1);
var is_ie     = ( (uagent.indexOf('msie') != -1) && (!is_opera) && (!is_safari) && (!is_webtv) );
var is_ie4    = ( (is_ie) && (uagent.indexOf("msie 4.") != -1) );
var is_moz    = ( (navigator.product == 'Gecko')  && (!is_opera) && (!is_webtv) && (!is_safari) );
var is_ns     = ( (uagent.indexOf('compatible') == -1) && (uagent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_safari) );
var is_ns4    = ( (is_ns) && (parseInt(navigator.appVersion) == 4) );
var is_kon    = (uagent.indexOf('konqueror') != -1);

var is_win    =  ( (uagent.indexOf("win") != -1) || (uagent.indexOf("16bit") !=- 1) );
var is_mac    = ( (uagent.indexOf("mac") != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var ua_vers   = parseInt(navigator.appVersion);

function CA(id){
	$("#list_ngay_form").find("input").each(function(){											 										
		if($(this).attr("type") == "checkbox" && $(this).attr("id").indexOf("delete") > -1){			
			$(this).attr("checked",$(id).attr("checked"));
		}
	});
}

function changeUSD(obj,value){
	var valueAddedCommas = Math.ceil(value / iTyGia);
	valueAddedCommas = addCommas(valueAddedCommas.toString());
	$(obj).html(valueAddedCommas);
}
function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		return false;
	}
	return true;
}

function checkTimeInput(obj){
	var value = $(obj).val();
	if (value.length > 2){
		value = value.substring(0,2);
	}
	if (isNaN(value)){
		value = "00";
	}
	$(obj).val(value);
}
function rangeTimeInput(obj,type){
	var value = $(obj).val();
	if (value.length > 2){
		value = value.substring(0,2);
	}
	if (isNaN(value)){
		value = "00";
	}
	if (value < 0){
		value = "00";
	}
	else if (type == "hour" && value > 24){
		value = "24";
	}
	else if (value > 59) {
		value = "59"; 
	}
	$(obj).val(value);
}
function calRateMoney(objInput,objSp,value){
	var	valueChanged = iTyGia * value;
	$(objInput).val(valueChanged);
	var valueAddedCommas = addCommas(valueChanged.toString());
	$(objSp).html(valueAddedCommas);
}
function markCommasMoney(obj,value){
	var valueAddedCommas = addCommas(value.toString());
	$(obj).html(valueAddedCommas);
}
function addCommas(nStr)
{
	nStr = removeCommas(nStr);
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1))
	{
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

function removeCommas(theString)
{
	var rgx = /(\.)/g;
	return theString.replace(rgx, "");
}
/*-------------------------------------------------------------------------*/
// Center a div on the screen
/*-------------------------------------------------------------------------*/
function accessAjax(url,data_post,do_request_function,method){
		//----------------------------------
		// LOAD XML
		//----------------------------------
		if (method == "")
			method = "POST";
		xmlobj = new ajax_request();
		xmlobj.onreadystatechange( do_request_function );
		xmlobj.process( url, method, data_post);
}
function show_loading(message)
{
	if ( message )
	{
		document.getElementById( 'loading-layer-text' ).innerHTML = message;
	}
	
	var centerdiv = new center_div();
	centerdiv.divname = 'loading-layer';
	centerdiv.move_div();
}

function center_div()
{
	this.divname = '';
	this.divobj  = '';
}

/*-------------------------------------------------------------------------*/
// Main run function
/*-------------------------------------------------------------------------*/

center_div.prototype.move_div = function()
{
	try
	{
		this.divobj = document.getElementById( this.divname );
	}
	catch(e)
	{
		return;
	}
	
	//----------------------------------
	// Figure width and height
	//----------------------------------
	
	var my_width  = 0;
	var my_height = 0;
	
	if ( typeof( window.innerWidth ) == 'number' )
	{
		//----------------------------------
		// Non IE
		//----------------------------------
	  
		my_width  = window.innerWidth;
		my_height = window.innerHeight;
	}
	else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
		//----------------------------------
		// IE 6+
		//----------------------------------
		
		my_width  = document.documentElement.clientWidth;
		my_height = document.documentElement.clientHeight;
		
	}
	else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
		//----------------------------------
		// Old IE
		//----------------------------------
		
		my_width  = document.body.clientWidth;
		my_height = document.body.clientHeight;
	}

	//----------------------------------
	// Show...
	//----------------------------------
	
	this.divobj.style.position = 'absolute';
	this.divobj.style.display  = 'block';
	this.divobj.style.zIndex   = 99;
	
	//----------------------------------
	// Get div height && width
	//----------------------------------
	
	var divheight = parseInt( this.divobj.style.Height );
	var divwidth  = parseInt( this.divobj.style.Width );
	
	divheight = divheight ? divheight : 200;
	divwidth  = divwidth  ? divwidth  : 250;

	//----------------------------------
	// Get current scroll offset
	//----------------------------------
	
	var scrolly = this.getYscroll();
	
	//----------------------------------
	// Finalize...
	//----------------------------------
	
	var setX = ( my_width  - divwidth  ) / 2;
	var setY = ( my_height - divheight ) / 2 + scrolly;

	setX = ( setX < 0 ) ? 0 : setX;
	setY = ( setY < 0 ) ? 0 : setY;
	
	this.divobj.style.left = setX + "px";
	this.divobj.style.top  = setY + "px";
}

/*-------------------------------------------------------------------------*/
// Hide div
/*-------------------------------------------------------------------------*/

center_div.prototype.hide_div = function()
{
	try
	{
		if ( ! this.divobj )
		{
			return;
		}
		else
		{
			this.divobj.style.display  = 'none';
		}
	}
	catch(e)
	{
		return;
	}
}

/*-------------------------------------------------------------------------*/
// Get YScroll
/*-------------------------------------------------------------------------*/

center_div.prototype.getYscroll = function()
{
	var scrollY = 0;
	
	if ( document.documentElement && document.documentElement.scrollTop )
	{
		scrollY = document.documentElement.scrollTop;
	}
	else if ( document.body && document.body.scrollTop )
	{
		scrollY = document.body.scrollTop;
	}
	else if ( window.pageYOffset )
	{
		scrollY = window.pageYOffset;
	}
	else if ( window.scrollY )
	{
		scrollY = window.scrollY;
	}
	
	return scrollY;
}


// === Get/Hide/Show/Toggle ===  src="suggest/base.js"

function ge()
{
  var ea;
  for( var i = 0; i < arguments.length; i++ ) {
    var e = arguments[i];

    if( typeof e == 'string' )
	{
      e = document.getElementById(e);
	  
	}
    if( arguments.length == 1 )
      return e;
    if( !ea )
      ea = new Array();
    ea[ea.length] = e;
  }
  return ea;
}

function show_div()
{
  for( var i = 0; i < arguments.length; i++ ) {
    var element = ge(arguments[i]);
    if (element && element.style) element.style.display = '';
  }
  return false;
}

function hide_div()
{
  for( var i = 0; i < arguments.length; i++ ) {
    var element = ge(arguments[i]);
    if (element && element.style) element.style.display = 'none';
  }
  return false;
}

function showhide()
{
  for( var i = 0; i < arguments.length; i++ ) {
    var element = ge(arguments[i]);
	if (element && element.style) 
	{
		if (element.style.display == '')
			element.style.display = 'none';
		else element.style.display = '';
	}
  }
  return false;
}
function p2dot(theString)
{
	var rgx = /(\.)/g;
	return theString.replace(rgx, ".");
}
function dot2p(theString)
{
	var rgx = /(\.)/g;
	return theString.replace(rgx, ",");
}

function sendHTML(filename,id,divID)
{
	var url = 'ajax/' + filename + "?id=" + id;
	
	do_request_function = function()
	{
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			//xmlobj.show_loading("Bạn hãy chờ giây lát !");
			return;
		}
		//xmlobj.hide_loading();
		
		var html = xmlobj.xmlhandler.responseText;

		if ( html != '' )
		{
			document.getElementById(divID).innerHTML = html;
		}
		//else alert('Có lỗi, không thể gởi bài !');
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	xmlobj.process( url );
}



function sendVALUE(filename,id,divID)
{
	var url = 'ajax/' + filename;
	var data_post = "id=" + id;

	do_request_function = function()
	{
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			//xmlobj.show_loading("Bạn hãy chờ giây lát !");
			return;
		}
		//xmlobj.hide_loading();
		var html = xmlobj.xmlhandler.responseText;

		if ( html != '' )
		{
				
			document.getElementById(divID).value = html;
			
		}
		//else alert('Có lỗi, không thể gởi bài !');
	}
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	xmlobj.process( url, "POST", data_post);
}


function toggleSBSize(big) {
    var sb = document.getElementById('header-searchbox');
    if (big)
        sb.style.width = '20em';
    else
        sb.style.width = '10em';
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

