// Utility.js

// newFunction
function isEmpty(val){
	if (val == null) {
		return true;
	}
	else {
		val = trim(val);
		if (val.length == 0) {
			return true;
		}
	} //if (val == null)
	return false;
}  //isEmpty()

function trim(val)
{
	if( val != null )
	{
		var space = ' '+'\n'+'\r'+'\t';
		var startIndex,endIndex;
		for( startIndex = 0 ; startIndex < val.length ; startIndex++ ){
			if(space.indexOf(val.charAt(startIndex)) == -1)
				break;
		}
		for( endIndex = val.length - 1 ; endIndex >= 0  ; endIndex-- )
		{
			if(space.indexOf(val.charAt(endIndex)) == -1)
				break;
		}
		return val.substring(startIndex,endIndex+1);
	}
	return val;
}

function getRadioValue( radio ){
	for( var i = 0; i < radio.length; ++i ){
		if ( radio[i].checked ){
			return radio[i].value;
		}
	}
	return '';
}

function showMeg( msg ){
	alert(msg);
}

function checkChkBtn(formName,chkName,isCheck){
	var form = document.getElementById(formName);
	if ( form ){
		var chkBtn = form[chkName];
		if ( chkBtn ){
			if ( chkBtn[0] ){
				for(var i = 0; i < chkBtn.length; ++i){
					chkBtn[i].checked = isCheck;
				}
			} else {
				chkBtn.checked = isCheck;
			}
		}
	}
}

function hasChecked( chkObj ){
	if ( chkObj[0] ){
		//alert('hasChecked'  + typeof(chkObj) + ":" + chkObj.length + ":" + chkObj[0].value);
		for(var i = 0; i < chkObj.length; ++i){
			if ( chkObj[i].checked ){
				return true;
			}
		}		
	} else if ( chkObj ) {
		return chkObj.checked;
	}
	return false;
}

function isAllChecked( chkObj ){
	if ( chkObj[0] ){
		for(var i = 0; i < chkObj.length; ++i){
			if ( !chkObj[i].checked ){
				return false;
			}
		}
		return true;		
	} else if ( chkObj ) {
		return chkObj.checked;
	} else {
		return false;
	}
}

/*判斷當前字符是英文字符還是中文字符*/ 
function ifWesternChr(chrInput){ 
    var chrInput_e; 
    chrInput_e=escape(chrInput); 
    if(chrInput_e.length==(chrInput.length)*6 || chrInput=="\r" || chrInput=="\n") 
        return false; /*只有非普通ASCII字符經過escape()函數編碼之後的長度才會等於原長度的6倍，所以用這種方法可以避開內碼直接判斷當前字符是否為中文。*/ 
    else 
        return true; 
/* true 是英文, false 是中文 */
}

function getRealStringLength( str ){
    var length = str.length;
    for( var i = 0; i < str.length; ++i){
        if ( !ifWesternChr(str.charAt(i)) ){
            length += 1;
        }
    }
}

function hasChineseWord( str ) {
    return str.length == getRealStringLength( str );
}

function trimString(strData) {
	if ((strData == null) || (strData.length <= 0)) return strData;
	
	var strTrimData = "";
	for (i = 0; i < strData.length; i++) {
		curChar = strData.charAt(i);
		if ((curChar != " ") && (curChar != "\n") && (curChar != "\t") && (curChar != "\r"))
			strTrimData = strTrimData + curChar;
	}  //for (i = 0; i < strData.length; i++)
	return strTrimData;
} //trimString()

function isAllDigtialString(strData) {
	var bIsAllDigtial = true;
	if (strData == null) {
		bIsAllDigtial = false;
	} else if (strData.length <= 0) {
		bIsAllDigtial = false;
	} else {
		var strNewData = trim(strData);			
		var i;
		var curChar = "";
		for (i = 0; i < strNewData.length; i++) {
			curChar = strNewData.charAt(i);
			if ((curChar != "1") && (curChar != "2") && (curChar != "3") && (curChar != "4") &&
				(curChar != "5") && (curChar != "6") && (curChar != "7") && (curChar != "8") &&
				(curChar != "9") && (curChar != "0")) {
				bIsAllDigtial = false;
				break;
			}
		}  //for (i = 0; i < strNewData.length; i++)
	}  //if (strData == null)
	return bIsAllDigtial;
}  //isAllDigtial

function searchFormObjectByNameAndType(targetForm, strElementName, strElementType) {
	var targetElement = null;
	if ((targetForm == null) || 
		((strElementName == null) || (strElementName.length <= 0)) || 
		((strElementType == null) || (strElementType.length <= 0)))
		return targetElement;
		
	var i = 0;
	for (i = 0; i < targetForm.elements.length; i++) {
		if ((targetForm.elements[i].type == strElementType) &&
			(targetForm.elements[i].name == strElementName)) {
			targetElement = targetForm.elements[i];
			break;
		}  //if ((targetForm.elements[i].type == strElementType) &&
	}  //for (i = 0; i < document.DataListForm.elements.length; i++)
	
	return targetElement;
}  //searchFormObjectByNameAndType()

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function showHide( showId, hideId ){
	var sObj = window.document.getElementById(showId);
	var hObj = window.document.getElementById(hideId);
	if ( sObj && hObj ) {
		hObj.style.display = 'none';
		sObj.style.display = '';
	}
}
function show( objId ){
	var obj = window.document.getElementById(objId);
	if ( obj ) {
		obj.style.display = '';
	}
}
function hide( objId ){
	var obj = window.document.getElementById(objId);
	if ( obj ) {
		obj.style.display = 'none';
	}
}

function getPosTop(obj)
{
	var top = obj.offsetTop;
    if (obj.offsetParent != null)
    {
       top += getPosTop(obj.offsetParent);
    }
	return top;
}

function getPosLeft(obj)
{
    var left = obj.offsetLeft;
    if (obj.offsetParent != null)
    {
       left += getPosLeft(obj.offsetParent);
    }
	return left;
}

function removeCookie(name,path,domain)
{
    var strCookie=name+"=";
    if(!path) path="/";
    strCookie+="; path="+path;
    if(domain) {
        strCookie+="; domain="+domain;
    }
    strCookie+="; expires=Thu,1 Jan 1970 00:00:01 UTC";
    if(getCookie(name)){
        document.cookie=strCookie;
    }
}

function tryListen( path,musicId ){
	var newWin = window.open( path+'/pages/TryPlayer.jsp?id=' + musicId,'try','width=190,height=195');
	newWin.focus();
}

function setCookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" + (value) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

