/*----------------------------------------------------------------
(validate.js) Êý¾ÝÑéÖ¤£¨Client£©
Usage:validate.txt
Wen jian
-----------------------------------------------------------------*/

//----------------------------------------------------------------

SpecialChar = "<>\"\'=?!()*";

//----------------------------------------------------------------
var _RegExp = new RegExp();
var _RegExp_switch = "g";
function toRegExpPattern(pStr)
{
		var PatternStr = pStr;
		var specialCharS = "\\$^*+?.()|{},-[]";
		for(var l=0;l<specialCharS.length;l++)
		{
			var specialChar = specialCharS.charAt(l);
			var PatternStrA = PatternStr.split(specialChar);
			PatternStr = PatternStrA[0];
			for(var i=1;i<PatternStrA.length;i++)
					PatternStr += "\\" + specialChar + PatternStrA[i];
		}
		return PatternStr;
}
function RegExpTest(pStr, pPattern)
{
	_RegExp.compile(pPattern,_RegExp_switch);
	return _RegExp.test(pStr);
}
function fnStrComp(pStrA,pStrB)
{
	if(RegExpTest(pStrA,"^"+toRegExpPattern(pStrB)+"$"))return 0;
	if(RegExpTest(pStrA,toRegExpPattern(pStrB)))return 1;
	if(RegExpTest(pStrB,toRegExpPattern(pStrA)))return 2;
	return -1;
}
function fnStrComprise(pStrA,pStrB)
{
	if(pStrB=="")return true;
	if(RegExpTest(pStrA,"["+toRegExpPattern(pStrB)+"]"))
		return true;
	return false;
}
function fnStrReside(pStrA,pStrB)
{
	var i = 0;
	while(i<pStrB.length)
	{
		if (!(fnStrComp(pStrA,pStrB.charAt(i)) != -1))
			return false;
		i = i+1;
	}
	return true;
}

function isNull(pObject)
{

	switch((pObject.tagName).toUpperCase())
	{
		case "INPUT":
		case "TEXTAREA":
				if((pObject.value).length==0)return true;
				break;
		case "SELECT":
				if(pObject.options.length>0 && pObject.selectedIndex>=0){
					if(pObject.options[pObject.selectedIndex].value.length==0)return true;
				}
				else{
					return true;
				}
	}
	return false;
}

function isExist(pObject, value)
{
	switch((pObject.tagName).toUpperCase())
	{
		case "INPUT":
		case "TEXTAREA":
				if(pObject.value==value)return true;
				break;
		case "SELECT":
				for(var i=0; i<pObject.options.length; i++){
					if(pObject.options[i].value==value)return true;
				}
	}
	return false;
}

function _vDate(pDate)
{
	var sDate;
	var y=0;
	var m=1;
	var d=2;
	if(!RegExpTest(pDate,'^[ ]*[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}[ ]*$'))return false; 	//Error date format
	sDate = trim(pDate).split("-");
	if(parseInt(sDate[y],10)<1900 || parseInt(sDate[y],10)>3000)return false;				//Error year
	if(parseInt(sDate[m],10)<1 || parseInt(sDate[m],10)>12)return false;					//Error month
	if(parseInt(sDate[d],10)<1)return false		//Error day
	if (parseInt(sDate[m],10)==2)
	{
		if (((parseInt(sDate[y],10)%4==0)&&(parseInt(sDate[y],10)%100 != 0))||(parseInt(sDate[y],10)%400==0))
		{
			if (parseInt(sDate[d],10) > 29) return false;	//Error day
	     }
		else
		{
		      if (parseInt(sDate[d],10) > 28)return false;	//Error day
	    }
    }
    if((parseInt(sDate[m],10)==4)||(parseInt(sDate[m],10)==6)||(parseInt(sDate[m],10)==9)||(parseInt(sDate[m],10)==11))
    {
		if (parseInt(sDate[d],10) > 30)return false;	//Error day
    }

     if ((parseInt(sDate[m],10)==1)||(parseInt(sDate[m],10)==3)||(parseInt(sDate[m],10)==5)||(parseInt(sDate[m],10)==7)||(parseInt(sDate[m],10)==8)||(parseInt(sDate[m],10)==10)||(parseInt(sDate[m],10)==12))
     {
		if (parseInt(sDate[d],10) > 31)return false;	//Error day
     }
	return true;

}
function vDate(pDate)
{
	return vDateTime(pDate);
}
function vDateTime(pDateTime)
{
	if(!RegExpTest(pDateTime,'^[ ]*[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}[ ]*$') && !RegExpTest(pDateTime,'^[ ]*[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}[ ]*$') && !RegExpTest(pDateTime,'^[ ]*[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}[ ]*$')	&& !RegExpTest(pDateTime,'^[ ]*[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}[ ]*$'))return false; 	//Error datetime format
	var tDate = trim(pDateTime).split(" ");
	var sDate = tDate[0];
	var sTime = tDate[1];
	if(!_vDate(sDate))return false; //Error date
	if(tDate.length==2){
		var tTime = sTime.split(":");
		if(parseInt(tTime[0],10)<0 || parseInt(tTime[0],10)>23)return false;	//Error hour
		if(parseInt(tTime[1],10)<0 || parseInt(tTime[1],10)>59)return false;	//Error minute
		if(parseInt(tTime[2],10)<0 || parseInt(tTime[2],10)>59)return false;	//Error second
	}
	return true;
}

function compareDate(pDate1,pDate2)
{
	return Date.parse(_formatDate(pDate2))-Date.parse(_formatDate(pDate1));
}

function vInteger(pNum,pIntegerDigit)
{
	if((typeof pIntegerDigit).toLowerCase() =='undefined')
	{
		if(!RegExpTest(pNum,'^[ ]*[0-9]{0,}[ ]*$') || trim(pNum).length==0)return false; 	//Error Integer format
	}
	else
	{
		if(!RegExpTest(pNum,'^[ ]*[0-9]{0,'+ pIntegerDigit +'}[ ]*$') || trim(pNum).length==0)return false; 	//Error Integer format
	}
	return true;
}
function vSInteger(pNum,pIntegerDigit)
{
	if((typeof pIntegerDigit).toLowerCase() =='undefined')
	{
		if(!RegExpTest(pNum,'^[ ]*[\\-\\+]?[0-9]{0,}[ ]*$') || trim(pNum).length==0)return false; 	//Error Integer format
	}
	else
	{
		if(!RegExpTest(pNum,'^[ ]*[\\-\\+]?[0-9]{0,'+ pIntegerDigit +'}[ ]*$') || trim(pNum).length==0)return false; 	//Error Integer format
	}
	return true;
}

function vFloat(pNum,pDecimalDigits,pIntegerDigit)
{
	if((typeof pDecimalDigits).toLowerCase() =='undefined' && (typeof pIntegerDigit).toLowerCase() =='undefined')
	{
		if(!RegExpTest(pNum,'^[ ]*[0-9]{0,}(\\.[0-9]{1,})?[ ]*$') || trim(pNum).length==0  || trim(pNum)==".")return false; 	//Error float format
	}
	else if((typeof pIntegerDigit).toLowerCase() =='undefined')
	{
		if(!RegExpTest(pNum,'^[ ]*([0-9]{0,})(\\.[0-9]{0,'+ pDecimalDigits +'})?[ ]*$') || trim(pNum).length==0 || trim(pNum)=="." )return false; 	//Error float format
	}
	else
	{
		if(!RegExpTest(pNum,'^[ ]*([0-9]{0,' + pIntegerDigit + '})(\\.[0-9]{0,'+ pDecimalDigits +'})?[ ]*$') || trim(pNum).length==0 || trim(pNum)==".")return false; 	//Error float format
	}
	return true;
}
function vSFloat(pNum,pDecimalDigits,pIntegerDigit)
{
	if((typeof pDecimalDigits).toLowerCase() =='undefined' && (typeof pIntegerDigit).toLowerCase() =='undefined')
	{
		if(!RegExpTest(pNum,'^[ ]*[\\-\\+]?[0-9]{1,}(\\.[0-9]{1,})?[ ]*$') || trim(pNum).length==0  || trim(pNum)==".")return false; 	//Error float format
	}
	else if((typeof pIntegerDigit).toLowerCase() =='undefined')
	{
		if(!RegExpTest(pNum,'^[ ]*[\\-\\+]?([0-9]{1,})(\\.[0-9]{0,'+ pDecimalDigits +'})?[ ]*$') || trim(pNum).length==0  || trim(pNum)==".")return false; 	//Error float format
	}
	else
	{
		if(!RegExpTest(pNum,'^[ ]*[\\-\\+]?([0-9]{1,' + pIntegerDigit + '})(\\.[0-9]{0,'+ pDecimalDigits +'})?[ ]*$') || trim(pNum).length==0  || trim(pNum)==".")return false; 	//Error float format
	}
	return true;
}

function vNumber(pNum,pDecimalDigits,pIntegerDigit)
{
	if(vFloat(pNum,pDecimalDigits,pIntegerDigit)){
		return true;
	}

	if((typeof pDecimalDigits).toLowerCase() =='undefined' && (typeof pIntegerDigit).toLowerCase() =='undefined')
	{
		if(!RegExpTest(pNum,'^[ ]*[0-9]{1,3}(\\,[0-9]{3})*(\\.[0-9]{0,})?[ ]*$') || trim(pNum).length==0  || trim(pNum)==".")return false; 	//Error float format
	}
	else if((typeof pIntegerDigit).toLowerCase() =='undefined')
	{
		if(!RegExpTest(pNum,'^[ ]*[0-9]{1,3}(\\,[0-9]{3})*(\\.[0-9]{0,'+ pDecimalDigits +'})?[ ]*$') || trim(pNum).length==0 || trim(pNum)=="." )return false; 	//Error float format
	}
	else
	{
		var num = pNum.split(".");
		num  = num[0].split(",");
		num = num.join("");
		if(num.length>pIntegerDigit)return false;
		if(!RegExpTest(pNum,'^[ ]*[0-9]{1,3}(\\,[0-9]{3})*(\\.[0-9]{0,'+ pDecimalDigits +'})?[ ]*$') || trim(pNum).length==0 || trim(pNum)==".")return false; 	//Error float format
	}
	return true;
}

function vSNumber(pNum,pDecimalDigits,pIntegerDigit)
{
	if(vSFloat(pNum,pDecimalDigits,pIntegerDigit)){
		return true;
	}

	if((typeof pDecimalDigits).toLowerCase() =='undefined' && (typeof pIntegerDigit).toLowerCase() =='undefined')
	{
		if(!RegExpTest(pNum,'^[ ]*[\\-\\+]?[0-9]{1,3}(\\,[0-9]{3})*(\\.[0-9]{0,})?[ ]*$') || trim(pNum).length==0  || trim(pNum)==".")return false; 	//Error float format
	}
	else if((typeof pIntegerDigit).toLowerCase() =='undefined')
	{
		if(!RegExpTest(pNum,'^[ ]*[\\-\\+]?[0-9]{1,3}(\\,[0-9]{3})*(\\.[0-9]{0,'+ pDecimalDigits +'})?[ ]*$') || trim(pNum).length==0 || trim(pNum)=="." )return false; 	//Error float format
	}
	else
	{
		var num = pNum.split(".");
		num  = num[0].split(",");
		num = num.join("");
		if(num.length>pIntegerDigit)return false;
		if(!RegExpTest(pNum,'^[ ]*[\\-\\+]?[0-9]{1,3}(\\,[0-9]{3})*(\\.[0-9]{0,'+ pDecimalDigits +'})?[ ]*$') || trim(pNum).length==0 || trim(pNum)==".")return false; 	//Error float format
	}
	return true;
}
/*
function unFormatNumber(formatedNumber)
{

	var numbers = formatedNumber.split(",");
	var numbersLength = numbers.length;
	var number = "";
	for(var i=0; i<numbersLength; i++){
		number += numbers[i];
	}
	if(number.length==0)number = "0.0";
	return number;
}
*/
function compareNumber(pNum1,pNum2)
{
	return parseFloat(pNum2)-parseFloat(pNum1);
}

function vEmail(pStrEmail)
{
	if(!RegExpTest(pStrEmail,".{1,}@.{1,}"))return false;
	return true;
}

function vNotificationEmail(pStrNotificationEmail)
{
	var tEmailList = pStrNotificationEmail.split(",");
	for(var i=0;i<tEmailList.length;i++){
		var oneEmail = tEmailList[i];
		if(fnStrComp(oneEmail,"@")==1){
			if(!RegExpTest(oneEmail,"^[oO]{1}[wW]{1}[nN]{1}[eE]{1}[rR]{1}@[PFTA]{1}[0-9]{9}$|^[qQ]{1}[aA]{1}@[PFTA]{1}[0-9]{9}$|^[mM]{1}[eE]{1}[mM]{1}[bB]{1}[eE]{1}[rR]{1}@[PFTA]{1}[0-9]{9}$|^[vV]{1}[iI]{1}[eE]{1}[wW]{1}[eE]{1}[rR]{1}@[PFTA]{1}[0-9]{9}$") || oneEmail.length==0)return false;
		}
		else{
			if(!fnStrReside(UserID,oneEmail) || oneEmail.length==0)return false;
		}
	}
	return true;

}


//------------------------------------------------------------------------------------------------
function trim(pString)
{
	var tString = pString;
	for(var i=0;i<tString.length;i++)
	{
		if(tString.substr(i,1)!=" ")break;
	}
	tString = tString.substr(i);
	for(var i=tString.length-1;i>0;i--)
	{
		if(tString.substr(i,1)!=" ")break;
	}
	tString = tString.substr(0,i+1);
	return tString;
}

function _formatDate(pDate) //YYYY-MM-DD -> MM/DD/YYYY
{
	var y=0,m=1,d=2;
	var tDate = trim(pDate).split(" ");
	var sDate = tDate[0];
	var sTime = tDate[1];
	var tDate =sDate.split("-");
	if(trim(pDate).length<=10)return tDate[m]+"/"+tDate[d]+"/"+tDate[y];
	if(trim(pDate).length<=18)return tDate[m]+"/"+tDate[d]+"/"+tDate[y]+" "+sTime;
	return null;
}
function formatDateTo(pDate) //YYYY-MM-DD -> YYYY/MM/DD
{
	var y=0,m=1,d=2;
	var tDate = trim(pDate).split(" ");
	var sDate = tDate[0];
	var sTime = tDate[1];
	var tDate =sDate.split("-");
	if(trim(pDate).length<=10)return tDate[y]+"/" + tDate[m] +"/"+tDate[d];
	if(trim(pDate).length<=16)return tDate[y]+"/" + tDate[m] +"/"+tDate[d]+sTime;
	return null;
}


