// common JavaScript functions

/* ---------------- Common basic type operations/ --------------- */
// serial of functions to trim string
var whitespace = " \t\n\r";

function charInString (c, s)
{   for (i = 0; i < s.length; i++)
	{   if (s.charAt(i) == c) return true;
	}
	return false
}

function stripInitialWhitespace (s)
{   var i = 0;

	while ((i < s.length) && charInString (s.charAt(i), whitespace))
	   i++;

	return s.substring (i, s.length);
}

function stripEndingWhitespace (s)
{   var i = s.length - 1;

	while ((i > 0) && charInString (s.charAt(i), whitespace))
	   i--;

	return s.substring (0, i+1);
}

function trimString (s)
{
	return stripEndingWhitespace ( stripInitialWhitespace (s) );

}

// function to check E-mail address like string
function isEmail (s)
{
	// there must be >= 1 character before @, so we
	// start looking at character position 1
	// (i.e. second character)
	var i = 1;
	var sLength = s.length;

	if ( sLength <= 1 ) return false ;

	// look for @
	while ((i < sLength) && (s.charAt(i) != "@"))
	{ i++
	}

	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;

	// look for .
	while ((i < sLength) && (s.charAt(i) != "."))
	{ i++
	}

	// there must be at least one character after the .
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	else return true;
}

// function to check IP Address like string
function isIPAddress ( strIPAddress )
{
	regExp = new RegExp ( "([0-9]{1,3})[\.]{1,}([0-9]{1,3})[\.]{1,}([0-9]{1,3})[\.]{1,}([0-9]{1,3})" ) ;
	aParts = regExp.exec ( strIPAddress ) ;
	if ( aParts == null )
	{
		return false ;
	}

	if ( aParts.length != 5 )
	{
		return false ;
	}

	for ( nLoopCnt = 1 ; nLoopCnt < 5 ; nLoopCnt ++ )
	{
		if ( aParts [ nLoopCnt ] < 0 || aParts [ nLoopCnt ] > 255 )
		{
			return false ;
		}
	}

	return true ;
}

// function to check login name like string
function fn_CheckLoginName ( strLogin )
{
	strLogin = trimString( strLogin ) ;
	if (strLogin.search(/^[A-Z|\d][A-Z|\d|\-|.|_]*$/i) != -1)
		return true;
	else
		return false;
}

// function to check Domain name like string
function fn_CheckDomainName ( strDomainName )
{
	strDomainName = trimString ( strDomainName ) ;
	if (strDomainName.search(/^[a-z0-9\u4E00-\u9FA5\uFE30-\uFFA0]([-.]?[a-z0-9\u4E00-\u9FA5\uFE30-\uFFA0])*\.([a-z]{2,4}|[\u4E00-\u9FA5\uFE30-\uFFA0]{2}|travel)$/) != -1)
		return true;
	else
		return false;
}

// function to check Domain name(name part only)
function fn_CheckDomainNameOnly ( strDomainName )
{
	arrayOfStrings = strDomainName.split(".");
	if ( arrayOfStrings.length > 1 )
	{
		return (false);	// redundant parts
	}
	for(i=0;i<arrayOfStrings.length;i++)
	{
		oldlen = strDomainName.length;
		str = trimString(arrayOfStrings[i]);
		if(str.length==0 || str.length != oldlen )
		{
			return (false);  // empty part
		}

		re = /[^a-z0-9\-]+/gi;   
		match=re.test(str);
		if ( match )
			return (false);  // non digit and character and '-' char
		re = /^\-/;
		match=re.test(str);
		if ( match )
			return (false);  // start with '-'
		re = /\-$/;
		match=re.test(str);
		if ( match )
			return (false);  // end with '-'
	}
	return (true);
}

// function to check CN Domain name like string
function fn_CheckCNDomainName ( strDomainName )
{
	strDomainName = trimString ( strDomainName ) ;
	
	arrayOfStrings = strDomainName.split(".");
	if ( arrayOfStrings.length < 2 )
	{
		return (false);	// no enough parts
	}
	for(i=0;i<arrayOfStrings.length;i++)
	{
		str = trimString(arrayOfStrings[i]);
		// must not exceed 20 chars
	  if( str.length==0 || str.length > 20)
	  {
		  return (false);
	  }
	
	  // with only leagal chars
	  re = /^[a-zA-Z0-9\-\u4E00-\u9FA5\uFE30-\uFFA0]+$/g
	  if ( ! re.test(str) )
	  {
		  return (false);
	  }
	  // must with a chinese char at least
//		  re = /^[a-zA-Z0-9\-]+$/g
//		  if ( re.test(str) )
//		  {
//			  return (false);
//		  }
	  // must not start with '-'
	  re = /^\-/;
	  if ( re.test(str) )
		  return (false);
	  // must not end with '-'
	  re = /\-$/;
	  if ( re.test(str) )
		  return (false);
	}
	return (true);
}

// function to check CN Domain name(name part only)
function fn_CheckCNDomainNameOnly ( strDomainName )
{				
	str = trimString(strDomainName);
	// must not exceed 20 chars
	if( str.length==0 || str.length > 20)
	{
		return (false);
	}
	
	// with only leagal chars
	re = /^[a-zA-Z0-9\-\u4E00-\u9FA5\uFE30-\uFFA0]+$/g
	if ( ! re.test(str) )
	{
		return (false);
	}
	// must with a chinese char at least
	re = /^[a-zA-Z0-9\-]+$/g
	if ( re.test(str) )
	{
		return (false);
	}
	// must not start with '-'
	re = /^\-/;
	if ( re.test(str) )
		return (false);
	// must not end with '-'
	re = /\-$/;
	if ( re.test(str) )
		return (false);
												
	return (true);
}

// function to check year, month, day numbers
function fn_ValidateDate ( nYear , nMonth , nDay )
{
	if ( nMonth < 1 || nMonth > 12 ) return false ;
	if ( nDay < 1 || nDay > 31 ) return false ;
	if ( nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11 )
	{
		if ( nDay > 30 ) return false ;
	}
	if ( nMonth == 2 )
	{
		if ( nYear % 4 == 0 )
		{
			if ( nYear % 100 == 0 )
			{
				if ( nYear % 400 == 0 )
				{
					if ( nDay > 29 ) return false ;
				}
				else
				{
					 if ( nDay > 28 ) return false ;
				}
			}
			else
			{
				if ( nDay > 29 ) return false ;
			}
		}
		else
		{
			if ( nDay > 28 ) return false ;
		}
	}

	return true ;
}

function getObject(objectId)//获取id的函数 
	{
		if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId);
		} else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId);
		} else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
		} else {
		return false;
		}
	}

/* ---------------- /Common basic type operations --------------- */

// requires basic type functions ( basicType.js )
/* ---------------- Common form operations/ --------------- */
function fn_DisplayOrderImg ( strCurrentColumn, strOrderByColumn , nSortOrderDesc )
{
	if ( strOrderByColumn == strCurrentColumn )
	{
		if ( nSortOrderDesc == "1"  || nSortOrderDesc == "DESC" )
			document.write ( '<img src="images/order_down.gif" border="0">' ) ;
		else
			document.write ( '<img src="images/order_up.gif" border="0">' ) ;
	}
}
function AllSelect(form)
{
	for (var i=0;i<form.elements.length;i++)
	{
		var e = form.elements[i];
		e.checked = true;
	}
}
function ReverseSelect(form)
{
	for (var i=0;i<form.elements.length;i++)
	{
		var e = form.elements[i];
		if (e.checked == true){ e.checked = false; }
		else { e.checked = true;}
	}
}
// function to check required form element
function fn_CheckRequired ( theElement , theElementName )
{
	theElement.value = trimString ( theElement.value ) ;
	if( theElement.value == "" )
	{
		alert( "\"" + theElementName + "\"" + "必须填写！" ) ;
		theElement.focus();
		return false ;
	}
	return true;
}

// function to check INT form element
function fn_CheckIntNumber ( theElement , theElementName )
{
	if( isNaN( parseInt ( theElement.value ) ) )
	{
		alert( "\"" + theElementName + "\"" + "不是一个有效的整数！" ) ;
		theElement.focus();
		return false ;
	}
	return true;
}

// function to check Float form element
function fn_CheckFloatNumber ( theElement , theElementName )
{
	if( isNaN( parseFloat ( theElement.value ) ) )
	{
		alert( "\"" + theElementName + "\"" + "不是一个有效的数字！" ) ;
		theElement.focus();
		return false ;
	}
	return true;
}

// function to check Email form element
function fn_CheckEmail ( theElement , theElementName )
{
	if( ! isEmail( theElement.value ) )
	{
		alert( "\"" + theElementName + "\"" + "不是一个有效的E-mail地址！" ) ;
		theElement.focus();
		return false ;
	}
	return true;
}

// function to check if 2 password match
function fn_ValidatePassword ( thePassword1 , thePasswordName1 , thePassword2 , thePasswordName2 )
{
	if ( thePassword1.value.length < 6 )
	{
		alert ( "\"" + thePasswordName1 + "\"至少要有6个字符！" ) ;
		thePassword1.value = "" ;
		thePassword1.focus ( ) ;
		return false ;
	}

	if ( thePassword2.value.length < 6 )
	{
		alert ( "\"" + thePasswordName2 + "\"至少要有6个字符！" ) ;
		thePassword2.value = "" ;
		thePassword2.focus ( ) ;
		return false ;
	}

	if ( thePassword1.value != thePassword2.value )
	{
		alert ( "两次输入的密码不符合！请重新输入。" )
		thePassword1.value = "" ;
		thePassword2.value = "" ;
		thePassword1.focus ( ) ;
		return false ;
	}
	return true ;
}

// function to check valid login name
function fn_ValidateLoginName ( theElement , theElementName )
{
	theElement.value = trimString ( theElement.value ) ;
	if ( theElement.value == "" )
	{
	  alert ( "\"" + theElementName + "\"必须填写！" ) ;
	  theElement.focus ( ) ;
	  return false ;
	}
	if ( ! fn_CheckLoginName ( theElement.value ) )
	{
		alert ( "\"" + theElementName + "\"中只能包含字母、数字和\"-\"，\".\"，\"_\"字符！并且只字母字符开头！" ) ;
		theElement.focus ( ) ;
		return false ;
	}
	return true ;
}

// function to check valid domain name
function fn_ValidateDomainName ( theElement , theElementName )
{
	theElement.value = trimString ( theElement.value ) ;
	if ( theElement.value == "" )
	{
	  alert ( "\"" + theElementName + "\"必须填写！" ) ;
	  theElement.focus ( ) ;
	  return false ;
	}

	if ( ! fn_CheckDomainName ( theElement.value ) )
	{
		alert ( "\"" + theElementName + "\"中只能包含字母、数字和\"-\"字符！并且不能以\"-\"字符开头！" ) ;
		theElement.focus ( ) ;
		return false ;
	}
	return true ;
}

function fn_ValidateDomainNameOnly ( theElement , theElementName )
{
	theElement.value = trimString ( theElement.value ) ;
	theElement.value = theElement.value.toLowerCase() ;
	if ( theElement.value == "" )
	{
	  alert ( "\"" + theElementName + "\"必须填写！" ) ;
	  theElement.focus ( ) ;
	  return false ;
	}

	if ( ! fn_CheckDomainNameOnly ( theElement.value ) )
	{
		alert ( "\"" + theElementName + "\"中只能包含字母、数字和\"-\"字符！并且不能以\"-\"字符开头！" ) ;
		theElement.focus ( ) ;
		return false ;
	}
	return true ;
}

// function to check valid CN domain name
function fn_ValidateCNDomainName ( theElement , theElementName )
{
	theElement.value = trimString ( theElement.value ) ;
	if ( theElement.value == "" )
	{
	  alert ( "\"" + theElementName + "\"必须填写！" ) ;
	  theElement.focus ( ) ;
	  return false ;
	}

	if ( ! fn_CheckCNDomainName ( theElement.value ) )
	{
		alert ( "\"" + theElementName + "\"中只能包含中文、字母、数字和\"-\"字符，不能以\"-\"字符开头或结束，至少需要含有一个中文文字，最多不超过20个字符!" ) ;
		theElement.focus ( ) ;
		return false ;
	}
	return true ;
}

function fn_ValidateCNDomainNameOnly ( theElement , theElementName )
{
	theElement.value = trimString ( theElement.value ) ;
	//theElement.value = theElement.value.toLowerCase() ;
	if ( theElement.value == "" )
	{
	  alert ( "\"" + theElementName + "\"必须填写！" ) ;
	  theElement.focus ( ) ;
	  return false ;
	}

	if ( ! fn_CheckCNDomainNameOnly ( theElement.value ) )
	{
		alert ( "\"" + theElementName + "\"中只能包含中文、字母、数字和\"-\"字符，不能以\"-\"字符开头或结束，至少需要含有一个中文文字，最多不超过20个字符!" ) ;
		theElement.focus ( ) ;
		return false ;
	}
	return true ;
}

// function to check valid ip address
function fn_ValidateIPAddress ( theElement , theElementName )
{
	theElement.value = trimString ( theElement.value ) ;
	if ( theElement.value == "" )
	{
	  alert ( "\"" + theElementName + "\"必须填写！" ) ;
	  theElement.focus ( ) ;
	  return false ;
	}

	if ( ! isIPAddress ( theElement.value ) )
	{
		alert ( "\"" + theElementName + "\"不是合法的IP地址！" ) ;
		theElement.focus ( ) ;
		return false ;
	}
	return true ;
}

// function to check valid ip address Group
function fn_ValidateIPAddressGroup ( theElement1 , theElementName1 , theElement2 , theElementName2 )
{
	theElement1.value = trimString ( theElement1.value ) ;
	if ( theElement1.value == "" )
	{
	  alert ( "\"" + theElementName1 + "\"必须填写！" ) ;
	  theElement1.focus ( ) ;
	  return false ;
	}

	if ( ! isIPAddress ( theElement1.value ) )
	{
		alert ( "\"" + theElementName1 + "\"不是合法的IP地址！" ) ;
		theElement1.focus ( ) ;
		return false ;
	}

	theElement2.value = trimString ( theElement2.value ) ;
	if ( theElement2.value == "" )
	{
	  alert ( "\"" + theElementName2 + "\"必须填写！" ) ;
	  theElement2.focus ( ) ;
	  return false ;
	}

	if ( ! isIPAddress ( theElement2.value ) )
	{
		alert ( "\"" + theElementName2 + "\"不是合法的IP地址！" ) ;
		theElement2.focus ( ) ;
		return false ;
	}
	return true ;
}

// function to check or uncheck all checkbox elements of a form by a select-all checkbox value
function fn_ChangeSelectAll( theForm , bSelected )
{
	for ( i = 0 ; i < theForm.elements.length ; i ++ )
	{
		obj = theForm.elements[i] ;
		if ( obj.type == "checkbox" )
		{
			obj.checked = bSelected ;
		}
	}
}

// function to set selected option of select form element by given option value
function fn_FillSelectInput ( theSelectInput , strSelectedValue )
{
	for ( i = 0 ; i < theSelectInput.length ; i ++ )
	{
		if ( theSelectInput.options [ i ].value == strSelectedValue )
		{
			theSelectInput.options[i].selected = true ;
			break ;
		}
	}
}

// function to build select input options of an object list
function fn_BuildOptions( nTotalCount , nItemPerPage , nStartItem )
{
	if( nItemPerPage == -1 )
		return "<option value=\"1\" selected>第1页</option>";

	nPages = Math.ceil( Number( nTotalCount ) / Number ( nItemPerPage ) ) ;

	nPage = Math.ceil( Number( nStartItem ) / Number ( nItemPerPage ) ) ;

	if( nPages > 1 )
	{
		strReturn = "<option value=\"1\">最前页</option>" ;
	}else
	{
		strReturn = "" ;
	}

	if( nPage > 10 )
	{
		strReturn += "<option value=\"" + (nPage-1) + "\">前10页</option>" ;
		nStart = nPage-1 ;
	}else
	{
		nStart = 0 ;
	}

	nEnd = Math.min( nPages , nStart + 10 ) ;
	//alert(nStart);
	//alert(nEnd);
	for( nLoopCount = nStart ; nLoopCount < nEnd ; nLoopCount++ )
	{
		strReturn += "<option value=\"" + (nLoopCount+1) + "\"" ;

		if( nLoopCount == nPage-1 )
			strReturn += " selected " ;

		strReturn += ">第" + (nLoopCount+1) + "页</option>" ;
	}

	if( nPages > nEnd )
	{
		strReturn += "<option value=\"" + (nEnd+1) + "\">后10页</option>" ;
	}

	if( nPages > 1 )
	{
		strReturn += "<option value=\"" + nPages + "\">最后页</option>" ;
	}

	return strReturn;
}

function fn_IsFormChanged( theForm )
{
	var i;
	var obj;
	for( i=0; i<theForm.elements.length; i++ )
	{
		obj = theForm.elements[i];
		if( obj.type == "text" || obj.type == "password" || obj.type == "textarea" )
		{
			if( obj.value != obj.defaultValue )
				return true;
		}else if( obj.type == "checkbox" )
		{
			if( obj.checked != obj.defaultChecked )
				return true;
		}else if( obj.type == "select-one" )
		{
			if( !obj.options[obj.selectedIndex].defaultSelected )
				return true;
		}else if( obj.type == "radio" )
		{
			if (obj.defaultChecked!=obj.checked)
			{
				return true;
			}
		}
	}

	return false;
}

function fn_FillRadioInput ( theRadioInput , strSelectedValue )
{
	for ( i = 0 ; i < theRadioInput.length ; i ++ )
	{
		if ( theRadioInput [ i ].value == strSelectedValue )
		{
			theRadioInput[i].checked = true ;
			break ;
		}
	}
}

function fn_BuildSelectOptions(theSelect,theOptions,theDefaultValue)
{
	while ( theSelect.options.length > 0 && theSelect.options[theSelect.options.length-1].value != "" )
		theSelect.options[theSelect.options.length-1] = null;

	for (var i=0; theOptions[i] && theOptions[i+1]; i+=2)
	{
		theSelect.options[theSelect.options.length] = new Option(theOptions[i+1]);
		theSelect.options[theSelect.options.length-1].value = theOptions[i];
		if ( theOptions[i] == theDefaultValue )
		{
			theSelect.options[theSelect.options.length-1].selected = 1;
		}
	}
}

function isTelephone(str)
{
	regExp = /^[0-9]+(\-[0-9]+){1,2}$/ ;
	return regExp.test ( str ) ;
}

function isPostcode(str)
{
	regExp = /^[0-9]{6,6}$/ ;
	return regExp.test ( str ) ;
}

function fn_DoDefaultSelect ( theSelect , theDefaultValue )
{
	if ( theSelect )
	{
		for ( var i=0; i<theSelect.options.length; i++ )
		{
			if ( theSelect.options[i].value == theDefaultValue )
			{
				theSelect.selectedIndex = i ;
				break;
			}
		}
	}
}

function isMobile(str)
{
	regExp = /^[0-9]{11,11}$/ ;
	return regExp.test ( str ) ;
}

function isIDNumber(str)
{
	regExp1 = /^[0-9]{17,17}[0-9xX]$/ ;
	regExp2 = /^[0-9]{15,15}$/ ;
	return (regExp1.test ( str ) || regExp2.test ( str ));
}

function CreateXMLHTTPObject()
{
	var xObject = null;
	try 
	{
		xObject = new ActiveXObject("Msxml2.xmlhttp.4.0");
	}
	catch (e)
	{
		try
		{
			xObject = new XMLHttpRequest(); 
			if(xObject.overrideMimeType){
				xObject.overrideMimeType('text/xml');
			}
		}
		catch (e)
		{
			try 
			{
				xObject = new ActiveXObject("Msxml2.xmlhttp");
			} 
			catch (e) 
			{
				try 
				{
					xObject = new ActiveXObject("Microsoft.xmlhttp");
				} 
				catch (e) 
				{	
					alert("Error: Unable to create XML HTTP object!");
				}
			}
		}
	}
	return xObject;
}

function initRequest(){
	request = CreateXMLHTTPObject(); 
}
