/*************************************
** File: lib.js
** author: thangnc(ntsc279@hotmail.com)
** Date : 11/05/2006
*************************************/

function onlyNumber() {
	if((event.keyCode < 48)||(event.keyCode > 57))
	event.returnValue=false;
}

//**************************************************
function goto_pages(url_) {
	window.location = url_;
}

//*************************************
function open_new_window(sURL, sName, iWidth, iHeight, bResizable, bScrollbars) {
	var iTop  = (screen.height - iHeight) / 2 ;
	var iLeft = (screen.width  - iWidth) / 2 ;
	
	var sOptions = "toolbar=no" ;
	sOptions += ",width=" + iWidth ; 
	sOptions += ",height=" + iHeight ;
	sOptions += ",resizable=yes";//  + (bResizable  ? "yes" : "no") ;
	sOptions += ",scrollbars=yes";// + (bScrollbars ? "yes" : "no") ;
	sOptions += ",channelmode=no";
	sOptions += ",directories=no";
	sOptions += ",fullscreen=no,menubar=no,status=no,titlebar=no,location=no";
	
	sOptions += ",left=" + iLeft ;
	sOptions += ",top=" + iTop ;
	
	var oWindow = window.open(sURL, sName, sOptions,false);
	oWindow.focus();	
	return oWindow ;
}

function onSelectItem(_id,_id_value,_name, _name_value) {
	with (opener.document) {             
		if (getElementById(_id)) {
			getElementById(_id).value = _id_value;
			getElementById(_name).value = _name_value;
			window.focus();
		} else
			self.window.close();
	}        
	self.window.close();
}

function confirm_del(msg,_btn_) {
	if (!document.frm_list.boxchecked.value) {
		alert("Vui lòng chọn một đối tượng từ danh sách !");
		return false;
	}
	
	if (confirm(msg)) {
		document.frm_list.btn_process.value = _btn_;
		document.frm_list.submit();
		return true;
	} else {
		return false;
	}
}

function on_publish(_btn_) {
	if (!document.frm_list.boxchecked.value) {
		alert("Vui lòng chọn một đối tượng từ danh sách !");
		return false;
	} else {
		document.frm_list.btn_process.value = _btn_;
		document.frm_list.submit();
		return true;
	}
}

/**
* Toggles the check state of a group of boxes
*
* Checkboxes must have an id attribute in the form cb0, cb1...
* @param The number of box to 'check'
* @param An alternative field name
*/
function checkAll( n, fldName ) {
  if (!fldName) {
     fldName = 'chk_id';
  }
	var f = document.frm_list;
	var c = f.chk_all.checked;
	var n2 = 0;
	for (i=0; i < n; i++) {
		cb = eval( 'f.' + fldName + '' + i );
		if (cb) {
			cb.checked = c;
			n2++;
		}
	}
	if (c) {
		f.boxchecked.value = n2;
	} else {
		f.boxchecked.value = 0;
	}
}

function isChecked(isitchecked){
	if (isitchecked == true){
		document.frm_list.boxchecked.value++;
	}
	else {
		document.frm_list.boxchecked.value--;
	}
}

function submit_button(_btn_) {
	document.frm_list.btn_process.value = _btn_;
	document.frm_list.submit();
	return true;
}

function submit_button2(_btn_) {
	document.frm_list2.btn_process.value = _btn_;
	document.frm_list2.submit();
	return true;
}

function cart_process(_btn_, key) {
	document.frm_list.mode_cart.value = _btn_;
	if (key) {
		document.frm_list.cart_id.value = key;
	}
	
	document.frm_list.submit();
	return true;
}

function cart_config(_btn_) {
	document.frm_list.mode_cart.value = _btn_;
	document.frm_list.submit();
	return true;
}

/**
* Pops up a new window in the middle of the screen
*/
function popupWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

// LTrim(string) : Returns a copy of a string without leading spaces.
function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
   return rtrim(ltrim(str));
}

var sections = new Array ("info", "role");
function showClick(obj, section) {
	for(i = 0; i < section.length; i++){
		if (section[i]==obj) {
			document.getElementById('content_'+section[i]).style.display = 'block';
		} else {
			document.getElementById('content_'+section[i]).style.display = 'none';			
		}
	}
	if (document.getElementById('_msg'))
		document.getElementById('_msg').style.display = 'none';
}

function reset_from() {
	for(var i = 0; i < document.frm_list.elements.length; i++) {
		if(document.frm_list.elements[i].type == 'text') {
			document.frm_list.elements[i].value = '';
			
		}		
	}
	document.frm_list.category_id.value= '-1';
	document.frm_list.product_type.value= '-1';
	document.frm_list._status.value= '-1';
	document.frm_list.member_id.value= '';
}

function order_by(order_by) {
	document.frm_list.order_by.value= order_by;
	document.frm_list.submit();
}

function show_id(_id) {
	if (document.getElementById('_obj[category_type]').value=='N') {
		document.getElementById(_id).style.display = 'block';
		document.getElementById('_obj[category_special]').disabled = false;
	} else {
		document.getElementById(_id).style.display = 'none';
		document.getElementById('_obj[category_special]').checked= false;
		document.getElementById('_obj[category_special]').disabled = 'disabled';			
	}
}

function set_home_page(ctrl) {
	title = "ACF"; 

 	url = location.host;
	// 	location.host

	if (window.sidebar) { // Mozilla Firefox Bookmark
		alert('Trinh duyet ko ho tro chuc nang nay');	
	} else if( window.external ) { // IE Favorite
		obj.style.behavior='url(#default#homepage)';       
		obj.setHomePage(url);
		
	}
	else if(window.opera && window.print) { // Opera Hotlist
		return true;
	}		
}

function openNewImage(file)
{	 	
    picfile = new Image(); 
    picfile.src =(file);
    alert(file);
    width=picfile.width;
    height=picfile.height;
    winDef = 'status=no,resizable=no,scrollbars=no,toolbar=no,location=no,fullscreen=no,titlebar=yes,height='.concat(height).concat(',').concat('width=').concat(width).concat(',');
	winDef = winDef.concat('top=').concat((screen.height - height)/2).concat(',');
	winDef = winDef.concat('left=').concat((screen.width - width)/2);
	newwin = open('', '_blank', winDef);

	newwin.document.writeln('<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">');
	newwin.document.writeln('<a href="" onClick="window.close(); return false;"><img src="', file, '" alt="', (fBrw) ? 'Dong lai' : 'Dong lai', '" border=0></a>');
	newwin.document.writeln('</body>');

}

function thanh_tien() {
	var number = parseInt(document.getElementById('amount').value);
	var	price = parseInt(document.getElementById('_obj[price]').value);
	var tong_tien;
	tong_tien = number * price;
	document.getElementById('tong_tien').innerHTML = tong_tien+' USD';
	return 1;
}

function calculatePrice() {
	var f = document.frm_list;
	var priceCtrl = document.getElementById('view_price_total');
	var usdPrice = 0;
	var vndPrice = 0;
	var hidProductsId = document.getElementById('str_pid');
	hidProductsId.value = '';
	for(var i=0;i < f.elements.length;i++)
	{
		var ctrl = f.elements[i];

		if(ctrl.id.indexOf('_products[]')>=0 && ctrl.options.selectedIndex > 0) {
				var ddlValue = ctrl.options[ctrl.options.selectedIndex].value;
				var arrId = ddlValue.split('_');
				hidProductsId.value += arrId[0] + ',';
				usdPrice += arrId[1]*1.0;
		}
	}
	if(typeof(priceCtrl.innerText)!= 'undefined')
		priceCtrl.innerText = usdPrice;
	else if(typeof(priceCtrl.textContent)!= 'undefined')
		priceCtrl.textContent = usdPrice;
	document.getElementById('view_price_total').value = usdPrice;
} 

function set_img(img,host) {
		document.getElementById('pro_img').src = host + 'images/products/' + img;
}
