//parameter info
// chkName = the name of the chekboxes list without 1,2,3,... numbers
// frmName = html form name 
// chkTotal = no of checkboxes to look for
function select_all( chkName , frmName )
{
	var frm=document.forms[frmName];
	for(var i=0;i<frm.elements.length;i++)
	{
		var e =frm.elements[i];
		if ((e.name != 'check_all') && (e.type=='checkbox'))
		{
			e.checked =frm.check_all.checked;
		}
	}
}

function checkAll(chkName,frmName)
{
	var frm = document.forms[frmName];
	var i = 0;
	for (i=1 ; i<=frm.elements.length; i++)
	{
		var o = frm.elements[chkName + i.toString()];
		if (o) o.checked = true;
	}
	return false;
}

function clearAll( chkName , frmName)
{
	var frm = document.forms[frmName];
	var i = 0;
	for (i=1 ; i<=frm.elements.length; i++)
	{
		var o = frm.elements[chkName + i.toString()];
		if (o) o.checked = false;
	}
	return false;
}

function getCheckedNum(){
  var num = 0;
  for(var i=0;i<document.checkForm.elements.length;i++) {
    var e = document.checkForm.elements[i];
    if (e.name == 'chkId') {
      if(e.checked)
        num++;
    }
  }
  return num;
}

//additional err_msg + same parameters list as the above functions checkAll and clearAll 
//err_msg will be displayed if not checkbox is not selected
function is_checked(chkName , frmName , chkTotal, err_msg)
{
	var frm = document.forms[frmName];
	var i = 0;
	for (i=1 ; i<=chkTotal ; i++)
	{
		var o = frm.elements[chkName + i.toString()];
		if(o && o.checked)
			return true;
	}
	alert(err_msg); 
	return false;
}

function checkByteLength(str,minlen,maxlen) {
	if (str == null) return false;
	var l = str.length;
	var blen = 0;
	for(i=0; i<l; i++) {
		if ((str.charCodeAt(i) & 0xff00) != 0) {
			blen ++;
		}
		blen ++;
	}
	if (blen > maxlen || blen < minlen) {
		
		return false;
	}
	return true;
}

function validateNotEmpty(obj) {
    var str = obj.value;
	if(str!=null && str.length>0){
		return 0;
	}
	return 1;
}

function isEmptyField(aTextField) {
   if ((aTextField.value==null)||(aTextField.value.replace(/(^\s*)|(\s*$)/g, "").length==0)) {
      return true;
   } else { 
      return false; 
   }
}

function validateEmail(obj){
	var str = obj.value;
	var patn = /^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]*)*@[a-zA-Z0-9\-]+([\.][a-zA-Z0-9\-]+)+$/;
	if(patn.test(str)){
    	return 0;
	}else{
	    return 1; //incorrect format
	}
}
function isNumber(str) {
	var patn = new RegExp("^\\d{1,15}$"); 
	if (patn.test(str)) {
		return true;
	} else {
		return false; 	
	}
}

function checkchi(myint) {
	var checkStr = myint;
	var allValid = true;
	var len=checkStr.length;
	for (i = 0;  i <len ;  i++)
	{
		ch = checkStr.charCodeAt(i);			
		if (ch > 127 && !isOkChar(ch))
		{      
			allValid = false;
			break;
		}
	}
	if (!allValid)
	{
		return (false);
	}
	return (true);
}

function isOkChar(ch){
	var checkOk = new Array();
	checkOk[0] = "12288";
	checkOk[1] = "8364";
	for (j = 0;  j < checkOk.length;  j++)
   	if (ch == checkOk[j]){
       return true;
    }
    return false;
}	

function validateSelectValue(obj){
	var str = obj.value;		
	if(str.trim().length >0) return 0;
		return 1; 	
}

function open_window(url, width, height)
{
	var sw = window.open(url,"subWnd","Toolbar=1,menubar=0,scrollbars=1,resizable=1,width="+width+",height="+height+",top=100,left=200");
	if (!sw)
	{
		alert("Popup Blocker Detected!\n\nPlease disable your popup blocker software or allow popups on this page.");
	}
	else
	{
		sw.focus();
	}
	return false;
}

function open_window_full(url, width, height)
{
	var sw = window.open(url,"subWnd","Toolbar=0,menubar=0,scrollbars=0,resizable=1,width="+width+",height="+height+",top=0,left=0");
	if (!sw)
	{
		alert("Popup Blocker Detected!\n\nPlease disable your popup blocker software or allow popups on this page.");
	}
	else
	{
		sw.focus();
	}
	return false;
}

//Dont change window name 'subWnd', its being used on some other pages
function open_window_scroll(url, width, height)
{
	var sw=window.open(url,"subWnd","Toolbar=1,menubar=0,scrollbars=1,resizable=1,width="+width+",height="+height+",top=75,left=150");
	if (!sw)
	{
		alert("Popup Blocker Detected!\n\nPlease disable your popup blocker software or allow popups on this page.");
	}
	else
	{
		sw.focus();
	}
	return false;
}

function TextArea_Keypress( txtName , spnName )
{
	if (document.getElementById(txtName).value.length >= parseInt(document.getElementById(txtName).attributes['MaxChars'].value))
		return false;
}

function TextArea_Keyup( txtName , spnName )
{
	if (document.getElementById(txtName).value.length >= parseInt(document.getElementById(txtName).attributes['MaxChars'].value))
	{
		document.getElementById(txtName).value = document.getElementById(txtName).value.substr(0, parseInt(document.getElementById(txtName).attributes['MaxChars'].value)-1);
	}
	//document.getElementById(spnName).innerHTML = (parseInt(document.getElementById(txtName).attributes['MaxChars'].value)-parseInt(document.getElementById(txtName).value.length));
	document.getElementById(spnName).innerHTML = (parseInt(document.getElementById(txtName).value.length));
}

function trim(str)
{
	str = str.replace(/^\s*|\s*$/g,"");
	return str;
}

function menuclick(submenu,imgbar) {
	if (document.getElementById(submenu).className=="close"){
		document.getElementById(submenu).className ="exp";
		document.getElementById(imgbar).src="images/decrease.gif";
	}else{
		document.getElementById(submenu).className="close";
		document.getElementById(imgbar).src="images/adding.gif";
	}
}

function setImgSizeWH(theURL,sImage,imgW,imgH){
var imgObj;
imgObj = new Image();
imgObj.src = theURL;
if ((imgObj.width != 0) && (imgObj.height != 0)) {
	
	if(imgObj.width>imgW || imgObj.height>imgH){
		
		var iHeight = imgObj.height*imgW/imgObj.width;
		
		if(iHeight<=imgH){
			sImage.width=imgW;
			sImage.height=iHeight;
		}else{
		var iWidth=imgObj.width*imgH/imgObj.height;
		sImage.width=iWidth;
		sImage.height=imgH;
		}
	}else{
	sImage.width=imgObj.width;
	sImage.height=imgObj.height;
	}

}else{
sImage.width = imgW;
sImage.height= imgH;
}
}
function validate_form(theForm)
{
	var keyword = document.getElementById("keyword");
	if(validateNotEmpty(keyword))
	{
		alert("Please input a search term.");
		theForm.keyword.focus();
		return false;
	}
	return true;
}
function changeSearchType(sType,obj){
	var typeSel = document.getElementById('type');
	for(i=0;i<4;i++){
		typeSel[i].selected=false;
	}
	var oLi=obj.parentNode.parentNode.getElementsByTagName('li');
	for(i=0;i<oLi.length;i++){
		oLi[i].className="";	
	}
	obj.parentNode.className="current";
	switch(sType){
		case("product"):{
			typeSel[0].selected=true;
			break;
			}
		case("buy"):{
			typeSel[1].selected=true;
			break;
			}
		case("sell"):{
			typeSel[2].selected=true;
			break;
			}
		case("company"):{
			typeSel[3].selected=true;
			break;
			}
		}
}

function isEmail(str){
   var reg=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
   return reg.test(str);
}

function isEmpty(str){
	return (str.trim()=="");
}

function includeChineseChar(str){
    var reg=/^[\x00-\x7F]*$/;
	return reg.test(str);  
}

function isEngDigitEmpty(str){
    var reg=/^([a-zA-Z0-9]|[ ]){1,100}$/;
    return reg.test(str);
}