function GE( id)
{
	return document.getElementById(id);
}

function CtoH( id) 
{
	var str = GE(id).value;
	var result = "";
	for (var i = 0; i < str.length; i++) {
		if (str.charCodeAt(i) == 8220 || str.charCodeAt(i) == 8221) {
			result += String.fromCharCode(34);
		} else if (str.charCodeAt(i) == 8216 || str.charCodeAt(i) == 8217) {
			result += String.fromCharCode(39);
		} else if (str.charCodeAt(i) == 12290) {
			result += String.fromCharCode(46);
		} else if (str.charCodeAt(i) == 12289) {
			result += String.fromCharCode(44);
		} else if (str.charCodeAt(i) == 12288) {
			result += String.fromCharCode(32);
		} else if (str.charCodeAt(i) > 65280 && str.charCodeAt(i) < 65375) {
			result += String.fromCharCode(str.charCodeAt(i) - 65248);
		} else {
			result += String.fromCharCode(str.charCodeAt(i));
		}
	}
	GE(id).value = result.trim();
}

function StringCtoH( p_source) 
{

	var str = p_source;
	var result = "";
	for (var i = 0; i < str.length; i++) {
		if (str.charCodeAt(i) == 8220 || str.charCodeAt(i) == 8221) {
			result += String.fromCharCode(34);
		} else if (str.charCodeAt(i) == 8216 || str.charCodeAt(i) == 8217) {
			result += String.fromCharCode(39);
		} else if (str.charCodeAt(i) == 12290) {
			result += String.fromCharCode(46);
		} else if (str.charCodeAt(i) == 12289) {
			result += String.fromCharCode(44);
		} else if (str.charCodeAt(i) == 12288) {
			result += String.fromCharCode(32);
		} else if (str.charCodeAt(i) > 65280 && str.charCodeAt(i) < 65375) {
			result += String.fromCharCode(str.charCodeAt(i) - 65248);
		} else {
			result += String.fromCharCode(str.charCodeAt(i));
		}
	}
	
	return result;
}

var firstErrorControl = "";

function SetError( id,state, text)
{
	if( text.trim().length > 0)
		GE( id + "Error" ).innerHTML = text;
		
	if( state.toLowerCase() == "block" && firstErrorControl != null && firstErrorControl == "")
	{
		firstErrorControl = id;
	}
	
	GE( id + "Error" ).style.display = state;
}

function SetErrorFocus()
{
	if( firstErrorControl != null && firstErrorControl != "")
	{
 		try
		{
			
			if( GE(firstErrorControl ).type != "hidden")
			{
				if( GE(firstErrorControl ) != null)
					GE(firstErrorControl ).focus();
				else
					GE(firstErrorControl + "Error" ).focus();
			}
			else
			{
				GE(firstErrorControl + "Error" ).focus();
			}
		}
		catch(e)
		{
			GE(firstErrorControl + "Error" ).focus();
		}
	}
}

function SetErrorControlFocus()
{
	if( firstErrorControl != null && firstErrorControl != "")
	{
 		try
		{
			
			if( GE(firstErrorControl ).type != "hidden")
			{
				if( GE(firstErrorControl ) != null)
					GE(firstErrorControl ).focus();
				else
					GE(firstErrorControl + "Error" ).focus();
			}
			else
			{
				GE(firstErrorControl + "Error" ).focus();
			}
		}
		catch(e)
		{
			if(GE(firstErrorControl + "Error" ) != null)
			{
				GE(firstErrorControl + "Error" ).style.display = "block";
			}
		}
	}
}

function isEmpty(str)
{
	return (str.trim()=="");
}

function isInteger(str)
{
    if (isEmpty(str)) return true;
    
    var reg = /^((\+|-)\d)?\d*$/;
    return reg.test(str);
}

function isDouble(str)
{
    if (isEmpty(str)) return true; 
    
	var reg = /^(?:\+|-)?\d+(?:\.\d+)?$/;
	return reg.test(str);	        
}

function isDate(str)
{
    if (isEmpty(str)) return true;
     
   var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/; 

	var r = str.match(reg); 
	if(r==null)return false; 
	var d= new Date(r[1], r[3]-1,r[4]); 
	var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate();
	return newStr==str;
}


function isEmail(str)
{
    if (isEmpty(str)) return true; 
     
   var reg=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
   return reg.test(str);
}

function isEmailA(str)
{
   var reg=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
   return reg.test(str);
}

function isBetween (val, lo, hi) {
    if ((val < lo) || (val > hi)) { return(false); }
    else { return(true); }
}

function isDateTime(str)

{
   if (str=="") return true;
   
   var strDate = str;
   if (strDate.length == 0) return false;

   var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})/;
   var r = strDate.match(reg);

   if (r != null)   //??strDate
      strDate = strDate + " 00:00:00";
 
    reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})/;
    r = strDate.match(reg);
    if (r == null)
    {
       return false;
    }

    var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
    if (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()== r[7])
    {
       return d;
    }
    else
    {
       return false;
    }
}

function isChineseChar(str)
{
   if (isEmpty(str)) return true; 
  
   var reg=/^([\u4E00-\u9FA5]|[\uFE30-\uFFA0])*$/gi;
   return reg.test(str);  
}

function includeChineseChar(str)
{
   if (isEmpty(str)) return true;
  
   var reg=/^[\x00-\x7F]*$/;
   return reg.test(str);  
}

function isEngCharAll(str)
{ 
   if (isEmpty(str)) return true;	
   var reg=/^[a-zA-Z]{1,20}$/;
   return reg.test(str);
}
function isCountryCode(str)
{ 
   if (isEmpty(str)) return true;	
   var reg=/^([0-9\+]){1,10}$/;
   return reg.test(str);
}
function isDigitOrEngChar(str)
{ 
   if (isEmpty(str)) return true;	
   var reg=/^([a-zA-Z0-9]){1,100}$/;
   return reg.test(str);
}
function isEngCharUpper(str)
{ 
   if (isEmpty(str)) return true;
   var reg=/^[A-Z]{1,20}$/;
   return reg.test(str);
  
}
function isEngCharLower(str)
{ 
   if (isEmpty(str)) return true;
   var reg=/^[a-z]{1,20}$/;
   return reg.test(str);
}
function isPostCode(str)
{
   if (isEmpty(str)) return true;
   var reg=/^[A-Za-z0-9]{1,12}$/;
   return reg.test(str);
  
}
function isYear(str)
{
   if (isEmpty(str)) return true;
   var reg=/^[0-9]{4}$/;
   return reg.test(str);
  
}
function isUrl(str)
{
	return true;
   if (isEmpty(str) || str.toLowerCase() == "http://" ) return true;
   var reg=/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
   return reg.test(str);
}
function isDigit(str)
{
    if (isEmpty(str)) return true;
    var reg=/^[0-9]{1,20}$/;
    return reg.test(str);  
}
function isDigitDot(str)
{
    if (isEmpty(str)) return true;
    var reg=/^[0-9.]{1,20}$/;
    return reg.test(str);
}
function isRegisterUserName(str)
{
    if (isEmpty(str)) return true;
    var reg=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){2,19}$/;
    return reg.test(str);
    
}
function isTrueName(str)
{
    if (isEmpty(str)) return true;
    var reg=/^[a-zA-Z]{1,30}$/;
    return reg.test(str);
    
}
function isEngDigitEmpty(str)
{
    if (isEmpty(str)) return true;
    var reg=/^([a-zA-Z0-9]|[ ]){1,100}$/;
    return reg.test(str);
    
}
function isEngEmpty(str)
{
    if (isEmpty(str)) return true;
    var reg=/^([a-zA-Z]|[ ]){1,100}$/;
    return reg.test(str);
    
}
function isKeyWord(str)
{
    if (isEmpty(str)) return true;
    var reg=/^([a-zA-Z]|[0-9]|[ ]|[-]|[/]|[_]){1,100}$/;
    return reg.test(str);
    
}
function isPasswd(str)
{
    if (isEmpty(str)) return true;
    var reg=/^(\w){6,20}$/;
    return reg.test(str);
    
}
function isTel(str)
{
    if (isEmpty(str)) return true;
    var reg=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
    return reg.test(str);
    
}
function isMobil(str)
{
    if (isEmpty(str)) return true;
	var reg=/^[0-9]{1,16}$/;
	return reg.test(str);
}
function isIP(str)  
{
   if (isEmpty(str)) return true; 
   var reg = /^((1??\d{1,2}|2[0-4]\d|25[0-5])\.){3}(1??\d{1,2}|2[0-4]\d|25[0-5])$/;
   return reg.test(str);
}
function isReal (theStr, decLen) {
    var dot1st = theStr.indexOf('.');
    var dot2nd = theStr.lastIndexOf('.');
    var OK = true;
    
    if (isEmpty(theStr)) return false;

    if (dot1st == -1) {
        if (!isInt(theStr)) return(false);
        else return(true);
    }
    
    else if (dot1st != dot2nd) return (false);
    else if (dot1st==0) return (false);
    else {
        var intPart = theStr.substring(0, dot1st);
        var decPart = theStr.substring(dot2nd+1);

        if (decPart.length > decLen) return(false);
        else if (!isInt(intPart) || !isInt(decPart)) return (false);
        else if (isEmpty(decPart)) return (false);
        else return(true);
    }
}
function isComdateMsg (lessDate1 ,startstr, moreDate1,endstr){
	var re =/\./g;
	var lessDate = lessDate1.replace(re,"-");
	var moreDate = moreDate1.replace(re,"-");
	var re1 =/\\/g;
	var lessDate = lessDate.replace(re1,"-");
	var moreDate = moreDate.replace(re1,"-");
	if(!isComdate(lessDate,moreDate)){
		var err = endstr + "";
		err = err + startstr;
		alert(err);
	}
	return isComdate(lessDate,moreDate);
}
function need_input(sForm)
{ 
    for(i=0;i<sForm.length;i++)
    {  
		if(sForm[i].tagName.toUpperCase()=="TEXTAREA")
		{
			if(sForm[i].value.realLength()>sForm[i].title)
			{
				sWarn= sForm[i].title + "'";
				alert(sWarn);
				sForm[i].focus();
				return false;				
			}
		}
		if(sForm[i].tagName.toUpperCase()=="INPUT" &&sForm[i].type.toUpperCase()=="TEXT")
		{  
			sForm[i].value = sForm[i].value.trim();
			if(sForm[i].value.indexOf('\'')>=0)
			{
				sWarn= "!";
				alert(sWarn);
				sForm[i].focus();
				return false;				
			}
			if(sForm[i].value.realLength()>sForm[i].maxLength)
			{
				sWarn= sForm[i].maxLength + "'";
				alert(sWarn);
				sForm[i].focus();
				return false;				
			}
		} 
		if(sForm[i].tagName.toUpperCase()=="INPUT" &&sForm[i].type.toUpperCase()=="TEXT" && (sForm[i].title!=""))
			if(sForm[i].value=="")
			{
				sWarn=sForm[i].title+"";
				alert(sWarn);
				sForm[i].focus();
				return false;
			}
		}
	return true;
} 
function Check_IsEmail(name,error,message)
{
	   if( includeChineseChar( GE(name).value) == false){
			SetError( error,"block","Please enter English.");	return false;
		}
	   else if( isEmailA(GE(name).value.trim()) == false){
			SetError( error,"block",message);	return false;
		}
		else{
			SetError( error,"none","");	return true;
		}
}
function Check_NotNull(name,error,errorM)
{
		CtoH( name);
	    
		if( isEmpty( GE(name).value.trim()) == true){
			SetError( error,"block",errorM);	return false;
		}
		else{
			SetError( error,"none","");	return true;
		}
}
function Check_NotNull_EnName(name,error,errorM){


		CtoH( name);
		
	    
		if( isEmpty( GE(name).value.trim()) == true){
			SetError( error,"block",errorM);	return false;
		}
		if( isEngEmpty( GE(name).value) == false){
			SetError( error,"block","Please enter English.");	return false;
		}
		else{
			SetError( error,"none","");	return true;
		}
}
function Check_NotCN(name,error){


		CtoH( name);

		if( includeChineseChar( GE(name).value.trim()) == false){
			SetError( error,"block","Please enter English.");	return false;
		}
		else{
			SetError( error,"none","");	return true;
		}
}
function Check_NotNull_NotCN(name,error,errorM){


		CtoH( name);

		if( isEmpty( GE(name).value.trim()) == true){
			SetError( error,"block",errorM);	return false;
		}
		else if( includeChineseChar( GE(name).value) == false){
			SetError( error,"block","Please enter English.");	return false;
		}
		else{
			SetError( error,"none","");	return true;
		}
}
function Check_NotNull_IsInteger(name,error,errorM)
{
		CtoH( name);

		if( isEmpty( GE(name).value.trim()) == true){
			SetError( error,"block",errorM);	return false;
		}
		else if( isInteger( GE(name).value) == false){
			SetError( error,"block","Please enter Number.");	return false;
		}
		else{
			SetError( error,"none","");	return true;
		}
}
function Check_IsInteger(name,error,errorM){

		CtoH( name);

		if( isInteger( GE(name).value.trim()) == false){
			SetError( error,"block","Please enter Number.");	return false;
		}
		else{
			SetError( error,"none","");	return true;
		}
}
function Check_NotNull_Length(name,error,maxLength,errorEM,errorLM){

		CtoH( name); 

		if( isEmpty( GE(name).value.trim()) == true){
			SetError( error,"block",errorEM);	return false;
		}
		else if(GE(name).value.length > maxLength){
			SetError( error,"block",errorLM);	return false;
		}
		else{
			SetError( error,"none","");	return true;
		}
		
		
}
function Check_NotNull_NotCN_Length(name,error,maxLength,errorEM,errorLM){

		CtoH( name); 

		if( isEmpty( GE(name).value.trim()) == true){
			SetError( error,"block",errorEM);	return false;
		}
		else if( includeChineseChar( GE(name).value) == false){
			SetError( error,"block","Please enter English.");	return false;
		}
		else if(GE(name).value.length > maxLength){
			SetError( error,"block",errorLM);	return false;
		}
		else{
			SetError( error,"none","");	return true;
		}
}
function Check_SelectNotFirstItem(name,error,errorMessage){

		if( GE(name).selectedIndex == 0){
			SetError( error,"block",errorMessage);	return false;
		}
		else{
			SetError( error,"none","");	return true;
		}
}
function UrlEncode(p_source)
{
	
	return ( ( p_source.split("&").join("ll_ll") ).split(",").join("dd_dd") ).split(" ").join("_");
}
function UrlDEcode(p_source)
{
	return( ( temp.split("ll_ll").join("&") ).split("dd_dd").join(",") ).split("_").join(" ");
}
function ClearUrlString(p_source)
{
	return p_source.replace(/[^0-9A-Za-z_]/ig,"")
}
String.prototype.trim = function()
{
    return this.replace(/(^[\s]*)|([\s]*$)/g, "");
}
String.prototype.lTrim = function()
{
    return this.replace(/(^[\s]*)/g, "");
}
String.prototype.rTrim = function()
{
    return this.replace(/([\s]*$)/g, "");
}
String.prototype.realLength = function()
{
  return this.replace(/[^\x00-\xff]/g,"**").length;
}
function addToFavorite(title){
  window.external.AddFavorite(document.location,title)
}


//Add by XiangWei
function ResizeImage(img, mWidth, mHeight){
    var image=new Image();
    image.src=img.src;
   
  var oldW = image.width;
  var oldH = image.height;
    if(image.width > 0 && image.height > 0){
        if(image.width/image.height >= mWidth/mHeight){
            if(image.width > mWidth){ 
                img.style.width = mWidth +"px";
                img.style.height = (image.height * mWidth)/image.width +"px";
            }else{
                img.style.width=image.width +"px"; 
                img.style.height=image.height +"px";
            }
        }else{
            if(image.height>mHeight){
                img.style.height = mHeight +"px";
                img.style.width = (image.width * mHeight)/image.height +"px"; 
            }else{
                img.style.width=image.width +"px"; 
                img.style.height = image.height +"px";
            }
        }
    }
   
}
