// #############################################################################
//
// 字符串处理公用函数
//
// #############################################################################

function openNewWindow(fileName,windowName,theWidth,theHeight) 
{
	if (windowName == "newMessageWindow") 
	{
		//generate random window ID

		 windowName = new String(Math.round(Math.random() * 100000));
	}
	if (theWidth == 0 && theHeight == 0 )
	{
		window.open(fileName,windowName,"toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,top=0,left=0,width=" + (window.screen.availWidth - 10) + ",height=" + (window.screen.availHeight - 50) )
	}
	else
	{
		window.open(fileName,windowName,"toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,width="+theWidth+",height="+theHeight)
	}
}

//===============================================================================
//getStrAllocateLen - get string's length allocated in database
//===============================================================================
function getStrAllocateLen(str){
	var iLen=0;
	var iUnicode;
	
	for(i=0;i<str.length;i++){
		iUnicode=str.charCodeAt(i);
		if (iUnicode<=128){				//英文字符
			iLen+=1;
		}else{
			if (iUnicode>128 && iUnicode<=255){
				iLen+=2;
			}else{
				if (iUnicode>=4112){	//中文字符
					iLen+=2;
				}else{
					iLen+=1;			//其他字符
				}
			}
		}
	}
	return(iLen);
}

//===============================================================================
//validateInputString - validate if the string inputed contain the invalid chat
//===============================================================================
function validateInputString(strInput){
	if (typeof(strInput)!="string"){
		return(false);
	}
	var i;
	var iCode;
	for (i=0;i<strInput.length;i++){
		iCode=strInput.charCodeAt(i);
		if (iCode!=95){		//	char _
			if (iCode<48 || iCode>122 || (iCode>57 && iCode<65) || (iCode>90 && iCode<97) ){
				return(false);
			}
		}
	}
	return(true);
}

//=============================================================================
//	isValidCode	--	confirm that a string does not contain invalid characters
//	Effect:			to validate that the string does not contain the following characters:
//					" / \ [ ] : ; | = , + * ? < >
//					and cannot be a single period (.) or space
//					This criterion is specified in Windows NT Security System
//	Return Values:	True if the string follows the criterion, else False.
//=============================================================================
function isValidCode(asCode)
{
	if(asCode.indexOf('"') != -1) return(false);
	if(asCode.indexOf('/') != -1) return(false);
	if(asCode.indexOf('\\') != -1) return(false);
	if(asCode.indexOf('[') != -1) return(false);
	if(asCode.indexOf(']') != -1) return(false);
	if(asCode.indexOf(':') != -1) return(false);
	if(asCode.indexOf(';') != -1) return(false);
	if(asCode.indexOf('|') != -1) return(false);
	if(asCode.indexOf('=') != -1) return(false);
	if(asCode.indexOf(',') != -1) return(false);
	if(asCode.indexOf('+') != -1) return(false);
	if(asCode.indexOf('*') != -1) return(false);
	if(asCode.indexOf('?') != -1) return(false);
	if(asCode.indexOf('<') != -1) return(false);
	if(asCode.indexOf('>') != -1) return(false);
	if(asCode.indexOf('\'') != -1) return(false);	// add this to help working well in SQL
	if(asCode.indexOf('.') != -1 && asCode.length == 1) return(false);
	if(asCode.indexOf(' ') != -1 && asCode.length == 1) return(false);
	return(true);
}

//===============================================================================
//encodeURL - encode the url string
//===============================================================================
function encodeURL(strURL){
//		Code			encode
//		[				[10
//		#				[20
//		&				[30
//		*				[40
//		+				[50
//						[60
//		"				[70
//	    %				[80
//		'				[90
	var strRtn=strURL
	if (typeof(strURL)!='string'){
		reportError(L_errFuncParaTypeMissMatch_ErrMsg,"encodeURL")
		return -1;
	}
	strRtn = replaceString( strRtn,"[","[10");
	strRtn = replaceString( strRtn,"#","[20");
	strRtn = replaceString( strRtn,"&","[30");
	strRtn = replaceString( strRtn,"*","[40");
	strRtn = replaceString( strRtn,"+","[50");
	strRtn = replaceString( strRtn," ","[60");
	strRtn = replaceString( strRtn,'"',"[70");
	strRtn = replaceString( strRtn,"%","[80");
	strRtn = replaceString( strRtn,"'","[90");
	return strRtn;
}


//===============================================================================
//reportError - encode the url string
//===============================================================================
function reportError(strMsg,strFunc){
	if (strFunc!="")
		alert(L_NameOfFunction_Text + strFunc + L_NameOfError_Text + ":" + strMsg);
	else
		alert(strMsg);
}

//===============================================================================
//replaceString - in strSource string,search the strFind string and replace with strReplace string 
//===============================================================================
function replaceString(strSource,strFind,strReplace){
	var pos = 0
	var srcLen
	var fndLen
	var repLen
	var strRtn=""
	
	if (typeof(strSource)!='string' || typeof(strFind)!='string' || typeof(strReplace)!='string' ){
		reportError(L_errFuncParaTypeMissMatch_ErrMsg,"replaceString")
		return -1;
	}
	srcLen = strSource.length;
	fndLen = strFind.length;
	repLen = strReplace.length;
	if ( srcLen==0 || srcLen<fndLen || fndLen==0 )
		return strSource;
	pos=strSource.indexOf(strFind,0);
	while ( pos!=-1){
		strRtn += strSource.substr(0,pos);
		strRtn += strReplace;
		strSource=strSource.substr(pos+fndLen);
		pos=strSource.indexOf(strFind,0);
	}
	strRtn+=strSource;
	return strRtn;
}

// #############################################################################
//
// 时间处理公用函数
//
// #############################################################################

// Valiate a string that can present a date
// Correct format is 1999/1/31 or 1999-1-13
function isDateStr(sDate)
{
	var aDate = sDate.split('-');
	if(aDate.length == 3 )
	{
		if(isDate(aDate[0], aDate[1], aDate[2])) return(true);
		return(false);
	}
	aDate = sDate.split('/');
	if(aDate.length == 3 )
	{
		if(isDate(aDate[0], aDate[1], aDate[2])) return(true);
		return(false);
	}
	return(false);
}

//===============================================================================
//isDate - check if strYear,strMonth and strDay can combine to date
//===============================================================================
function isDate(strYear,strMonth,strDay){
	if (typeof(strYear)!='string' || typeof(strMonth)!='string' || typeof(strDay)!='string'){
		reportError(L_errFuncParaTypeMissMatch_ErrMsg,"isDate")
		return false;
	}
	var iYear = strYear * 1
	var iMonth = strMonth * 1
	var iDay = strDay * 1
	if (isNaN(iYear) || isNaN(iMonth) || isNaN(iDay)){
		return false;
	}
	if (iYear<1800 || iYear>9999)
		return false;
	if (iMonth<1 || iMonth>12)
		return false;
	if (iDay<1 || iDay>31)
		return false;
	var strDate = iMonth+"/"+iDay+"/"+iYear
	var objDate = new Date(strDate)
	iYear = objDate.getFullYear()
	iMonth = objDate.getMonth()+1
	iDay = objDate.getDate()
	if (strDate!=(iMonth+"/"+iDay+"/"+iYear))
		return false;
	return true;	
}

//===============================================================================
//isTime - check if strHour,strMinute and strSecond can combine to time
//===============================================================================
function isTime(strHour,strMinute,strSecond){
	if (typeof(strHour)!='string' || typeof(strMinute)!='string' || typeof(strSecond)!='string'){
		reportError(L_errFuncParaTypeMissMatch_ErrMsg,"isDate")
		return false;
	}
	if (strHour=="" && strMinute=="" && strSecond==""){
		return true;
	}
	var iHour = strHour * 1
	var iMinute = strMinute * 1
	var iSecond = strSecond * 1
	if (isNaN(iHour) || isNaN(iMinute) || isNaN(iSecond)){
		return false;
	}
	if (iHour<0 || iHour>23) return false;
	if (iMinute<0 || iMinute>59) return false;
	if (iSecond<0 || iSecond>59) return false;
	return true;
}

// To clear starting & ending space in a string object
function trimStr(sVal)
{
	var iPos;
	//clear starting space
	while(true)
	{
		iPos = sVal.indexOf(' ');
		if(iPos == -1) break;
		if(iPos > 0) break;
		sVal = sVal.slice(1);
	}
	//clear ending space
	while(true)
	{
		iPos = sVal.lastIndexOf(' ');
		if(iPos == -1) break;
		if(iPos < sVal.length-1) break;
		sVal = sVal.slice(0, iPos);
	}
	return(sVal);
}

function optimizeDate(szDate)
{
	if (typeof(szDate)!='string') return '';
	var datePart = szDate.split('-');
	if (datePart.length!=3) return '';
	var iYear = datePart[0] * 1;
	var iMonth = datePart[1] * 1;
	var iDay = datePart[2] * 1;
	if (isNaN(iYear) || isNaN(iMonth) || isNaN(iDay)) return '';
	if (iYear<1800 || iYear>9999) return '';
	if (iMonth<1 || iMonth>12) return '';
	if (iDay<1 || iDay>31) return '';
	var szTemp = "" + iMonth + "/" + iDay + "/" + iYear;
	var oDate = new Date(szTemp);
	if (szTemp != ("" + ( oDate.getMonth() + 1) + "/" +oDate.getDate() + "/" + oDate.getFullYear())) return '';
	return "" + iYear + "-" + iMonth + "-" + iDay;
}

function compareTime(t1,t2)
{
	if (typeof(t1)!='string') return null;
	if (typeof(t2)!='string') return null;
	var datePart1 = t1.split('-');
	var datePart2 = t2.split('-');
	
	t1 = new Date(datePart1[1]+"/"+datePart1[2]+"/"+datePart1[0]);
	t2 = new Date(datePart2[1]+"/"+datePart2[2]+"/"+datePart2[0]);
	if (t1.valueOf()>t2.valueOf())
	{
		return(1);
	}
	else
	{
		if (t1.valueOf()<t2.valueOf())
		{
			return(-1);
		}
		else
		{
			return(0);
		}
	}
}


