var IMGDIR = './images';
var userAgent = navigator.userAgent.toLowerCase();

var is_opera = userAgent.indexOf('opera') != -1 && opera.version();

var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);

var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);

function $(id) {

	return document.getElementById(id);

}
function create_bg(){
	bg = document.createElement("div");
	bg.id = "dark_bg";
	with(bg.style){
		position = "absolute";
		top = "0";
		left = "0";
		width = document.documentElement.scrollWidth + "px";
		if(document.documentElement.scrollHeight<document.documentElement.clientHeight){
				height = document.documentElement.clientHeight + "px";
		}else{
				height = document.documentElement.scrollHeight + "px";
		}
	}
//	document.documentElement.style.overflow = "hidden";
//	document.body.style.overflow = "hidden";
	document.body.appendChild(bg);
}

var hiddenobj = new Array();

function showwin(action, param, title) {

	var objs = document.getElementsByTagName("OBJECT");

	if(action == 'open') {
		create_bg();
		for(i = 0;i < objs.length; i ++) {

			if(objs[i].style.visibility != 'hidden') {

				objs[i].setAttribute("oldvisibility", objs[i].style.visibility);

				objs[i].style.visibility = 'hidden';

			}

		}

		var clientWidth = document.body.clientWidth;

		var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;

		var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;

		var pmwidth = 500;

		var pmheight = clientHeight * 0.35;

		if(!$('pmlayer')) {
			div = document.createElement('div');
			
			div.id = 'pmlayer';

			div.style.width = pmwidth + 'px';

			div.style.height = pmheight + 'px';

			div.style.left = ((clientWidth - pmwidth) / 2) + 'px';

			div.style.position = 'absolute';
			
			div.style.zIndex = '999';

			$('append_parent').appendChild(div);

			$('pmlayer').innerHTML = '<!--<div style="width: ' + pmwidth + 'px; background: #666666; position: relative; left: 70px; top: -60;  margin: 5px auto; text-align: left">-->' +

				'<div style="width: ' + pmwidth + 'px; height: ' + pmheight + 'px; padding: 1px; background: #171717; border: 1px solid #7597B8; position: relative; left: 70px; top: -250px">' +

				'<div onmouseover="buywindrag(this)" style="cursor: move; position: relative; left: 0px; top: 0px; width: ' + pmwidth + 'px; height: 30px; margin-bottom: -30px;"><div style="height: 32px; line-height: 32px; background: url(./images/headerbg.gif) repeat-x 50%; border-bottom: 1px solid #CAD9EA; color: #005C89; font-size: 1.13em; padding-left: 1em;"><strong>' + title + '</strong></div></div>' +

				'<a href="javascript:" onclick="showwin(\'close\')"><img style="position: absolute; right: 20px; top: 15px" src="' + IMGDIR +'/close.gif" title="close" border="0" /></a>' +

				'<div id="buywinmask" style="margin-top: 30px; position: absolute; width: 100%; height: 100%; display: none"></div><iframe id="pmframe" name="pmframe" style="width:' + pmwidth + 'px;height:100%; overflow-x:hidden;" allowTransparency="true" frameborder="0"></iframe></div></div>';

		}

		$('pmlayer').style.display = '';

		$('pmlayer').style.top = ((clientHeight - pmheight) / 2 + scrollTop) + 'px';

		pmframe.location = param;

	} else if(action == 'close') {

		for(i = 0;i < objs.length; i ++) {

			if(objs[i].attributes['oldvisibility']) {

				objs[i].style.visibility = objs[i].attributes['oldvisibility'].nodeValue;

				objs[i].removeAttribute('oldvisibility');

			}

		}

		hiddenobj = new Array();

		$('pmlayer').style.display = 'none';
		pmframe.location = 'about:blank';
		var dark_bg = $('dark_bg');
		dark_bg.parentNode.removeChild(dark_bg);

	}

}

var buywindragstart = new Array();

function buywindrag(obj) {

	obj.onmousedown = function(e) {

		if(is_ie) {

			document.body.onselectstart = function() {

				return false;

			}

		}

		buywindragstart = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];

		buywindragstart[2] = parseInt($('pmlayer').style.left);

		buywindragstart[3] = parseInt($('pmlayer').style.top);

		$('buywinmask').style.display = '';

		doane(e);

	}

	_attachEvent(document.body, 'mousemove', function(e) {

		if(buywindragstart[0]) {

			var buywindragnow = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];

			with($('pmlayer')) {

				style.left = (buywindragstart[2] + buywindragnow[0] - buywindragstart[0]) + 'px';

				style.top = (buywindragstart[3] + buywindragnow[1] - buywindragstart[1]) + 'px';

			}

			doane(e);

		}

	});

	obj.onmouseup = function(e) {

		if(is_ie) {

			document.body.onselectstart = function() {

				return true;

			}

		}

		buywindragstart = [];

		$('buywinmask').style.display = 'none';

		doane(e);

	}

	obj.onmouseover = null;

}

function doane(event) {

	e = event ? event : window.event;

	if(is_ie) {

		e.returnValue = false;

		e.cancelBubble = true;

	} else if(e) {

		e.stopPropagation();

		e.preventDefault();

	}

}

function _attachEvent(obj, evt, func) {

	if(obj.addEventListener) {

		obj.addEventListener(evt, func, false);

	} else if(obj.attachEvent) {

		obj.attachEvent("on" + evt, func);

	}

}

	// function to check required form element
	function fn_CheckRequired( theElement , theElementName )
	{
		theElement.value = trimString ( theElement.value ) ;
		if( theElement.value == "" )
		{
			alert( "\"" + theElementName + "\"" + "must write!" ) ;
			theElement.focus();
			return false ;
		}
		return true;
	}

	// function to check INT form element
	function fn_CheckIntNumber ( theElement , theElementName )
	{
		if( isNaN( parseInt ( theElement.value ) ) )
		{
			alert( "\"" + theElementName + "\"" + "not a valid integer!" ) ;
			theElement.focus();
			return false ;
		}
		return true;
	}

	// function to check Float form element
	function fn_CheckFloatNumber ( theElement , theElementName )
	{
		if( isNaN( parseFloat ( theElement.value ) ) )
		{
			alert( "\"" + theElementName + "\"" + "not a valid number!" ) ;
			theElement.focus();
			return false ;
		}
		return true;
	}

	// function to check Email form element
	function fn_CheckEmail ( theElement , theElementName )
	{
		if( ! isEmail( theElement.value ) )
		{
			alert( "\"" + theElementName + "\"" + "not a valid E-mail address?" ) ;
			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 + "\"need as less 6 bytes!" ) ;
			thePassword1.value = "" ;
			thePassword1.focus ( ) ;
			return false ;
		}

		if ( thePassword2.value.length < 6 )
		{
			alert ( "\"" + thePasswordName2 + "\"need as less 6 bytes!" ) ;
			thePassword2.value = "" ;
			thePassword2.focus ( ) ;
			return false ;
		}

		if ( thePassword1.value != thePassword2.value )
		{
			alert ( "password doss not confirm" )
			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 + "\"must write!" ) ;
		  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 ) ;

		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 ) ;
		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 fn_ImageOnMouseOver ( theImage )
    {
        theImage.className = 'imgonmouseover';
    }

    function fn_ImageOnMouseOut ( theImage )
    {
        theImage.className = 'imgonmouseout';

    }


var sUserAgent = navigator.userAgent;
var fAppVersion = parseFloat(navigator.appVersion);
var isOpera = sUserAgent.indexOf("Opera")>-1;
var isKHTML = sUserAgent.indexOf("KHTML")>-1
                || sUserAgent.indexOf("Konqueror")>-1
                || sUserAgent.indexOf("AppleWebKit")>-1;
var isIE = sUserAgent.indexOf("compatible") > -1
            &&sUserAgent.indexOf("MSIE") > -1
            &&!isOpera;
var isFF = sUserAgent.indexOf("Firefox")>-1&&!isKHTML;
var isMoz = sUserAgent.indexOf("Gecko")>-1&&!isKHTML;
var isNS4 = !isIE&&!isOpera&&!isMoz&&!isKHTML
            &&(sUserAgent.indexOf("Mozilla")==0)
            &&(navigator.appName == "Netscape")
            &&(fAppVersion >= 4.0 && fAppVersion <5.0);
var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC")
            || (navigator.platform == "Macintosh");

var isUnix = (navigator.platform == "X11" && !isWin && !isMac);
var Utils = new Object;
Utils.addEventHandler=function(oTarget,sEventType,fnHandler){
    if(oTarget.addEventListener){
        oTarget.addEventListener(sEventType,fnHandler,false);
    }else if(oTarget.attachEvent){
        oTarget.attachEvent("on" + sEventType,fnHandler);
    }else{
        oTarget["on" + sEventType] = fnHandler;
    }
};
Utils.delEventHandler=function(oTarget,sEventType,fnHandler){
    if(oTarget.removeEventListener){
        oTarget.removeEventListener(sEventType,fnHandler,false);
    }else if(oTarget.detachEvent){
        oTarget.detachEvent("on" + sEventType,fnHandler);
    }else{
        oTarget["on" + sEventType] = null;
    }
};
Utils.formatEvent = function(oEvent){
    if(isIE&&isWin){
        oEvent.charCode=(oEvent.type=="keypress")?oEvent.keyCode:0;
        oEvent.eventPhase=2;
        oEvent.isChar = (oEvent.charCode>0);
        var scrollX=0;
        var scrollY=0;
        if(document.all){ 
            if(!document.documentElement.scrollLeft)
                scrollX=document.body.scrollLeft;
            else
                scrollX=document.documentElement.scrollLeft;
            if(!document.documentElement.scrollTop)
                scrollY=document.body.scrollTop;
            else
                scrollY=document.documentElement.scrollTop;
        }
        else{
            scrollX=window.pageXOffset;
            scrollY=window.pageYOffset;
        }
        oEvent.scrollX=scrollX;
        oEvent.scrollY=scrollY;
        oEvent.pageX=oEvent.clientX+scrollX;
        oEvent.pageY=oEvent.clientY+scrollY;
        oEvent.preventDefault=function(){this.returnvalue=false;};
        if(oEvent.type=="mouseout")
            oEvent.relatedTarget=oEvent.toElement;
        else if(oEvent.type=="mouseover")
            oEvent.relatedTarget=oEvent.fromElement;
        oEvent.stopPropagation=function(){this.cancelBubble=true;};
        oEvent.target=oEvent.srcElement;
        oEvent.time=(new Date()).getTime();
    }
    return oEvent;
};
Utils.getEvent=function(event){
    if(window.event)
        return Utils.formatEvent(window.event);
    else
        return Utils.getEvent.caller.arguments[0];
};