var browserInfo = new myBrowserDetect();

function debug( deStr ) {
	var debugEle;
	if( debugEle = document.getElementById("debug") ) {
		debugEle.innerHTML += deStr;
	}
}

function centerIt( alignEle,parentEle,centerByClientProperty ) {
	var parentWidth,parentHeight,alignWidth,alignHeight;
	if( !centerByClientProperty ) {
		parentWidth = getAttr( parentEle,'width',true );
		parentHeight = getAttr( parentEle,'height',true );
		alignWidth = getAttr( alignEle,'width',true );
		alignHeight = getAttr( alignEle,'height',true );
		if( !( parentWidth && parentHeight && alignWidth && alignHeight ) )
			centerByClientProperty = true;
	}
	if( centerByClientProperty ) {
		parentWidth = getAttr( parentEle,'clientWidth',true );
		parentHeight = getAttr( parentEle,'clientHeight',true );
		alignWidth = getAttr( alignEle,'clientWidth',true );
		alignHeight = getAttr( alignEle,'clientHeight',true );
		//alert( parentWidth );
	}
	with ( alignEle.style ) {
		left = ( ( parentWidth - alignWidth ) / 2 ) + 'px';
		top = ( ( parentHeight - alignHeight ) / 2 ) + 'px';
	}
}

function getAttr( pEle,attr,returnInterger ) {
	var pAttr = null;
	if( pEle.currentStyle && typeof pEle.currentStyle[attr] != 'undefined' ) {
		pAttr = pEle.currentStyle[attr];
	} else if( document.defaultView  ) {
		var pStyle = document.defaultView.getComputedStyle(pEle,'');
		if( typeof pStyle[attr] != 'undefined' ) pAttr = pStyle[attr];
	}
	if( !pAttr ) pAttr = pEle.getAttribute( attr );
	if( !pAttr ) pAttr = pEle[attr];	
	return ( returnInterger && pAttr ) ? parseInt( pAttr ) : pAttr;
}

function absolutePos( getEle ) {
	this.aTop = 0;
	this.aLeft = 0;
	if( getEle ) {
		while ( getEle.offsetParent != null ) {
			this.aTop += getEle.offsetTop;
			this.aLeft += getEle.offsetLeft;
			getEle = getEle.offsetParent;
		}
	}
}

function getDemoPath() {
	return ( typeof demoInfo == 'object' && typeof demoInfo.demoNumber == 'string' ) ? demoInfo.demoNumber + '/' : '';
}

function myParseJson( jstr ) {
	var tmpJson;
	try {
		if( typeof JSON != 'undefined' && JSON.parse ) {
			return JSON.parse( jstr );
		} else {
			eval('tmpJson=' + jstr);
			return tmpJson;
		}
	} catch( e ) {
		return null;
	}
}

function getHttpRequest() {
	with( browserInfo ) {
		try {
			if( browserType == 'msie' ) {
				if( majorVersion == 8 ) {
					return new XDomainRequest();
				} else if( majorVersion == 7 ) {
					return new XMLHttpRequest();
				} else if ( majorVersion < 7 ) {
					return new ActiveXObject("MSXML2.XMLHTTP.3.0")
				}
			} else {
				return new XMLHttpRequest();
			}
		} catch( exception ) {
			return null;
		}
	}
}

function myBrowserDetect() {
	var bdRegExp = /(firefox|msie|chrome|opera)/i;
	var fIndex = -1;
	var verStr = '';
	this.browserType = '';
	this.majorVersion = 0;
	this.minorVersion = 0;
	if( ( fIndex = navigator.userAgent.search( bdRegExp ) ) != -1 ) {
		this.browserType = RegExp.$1.toLowerCase();
		verStr = navigator.userAgent.substr( fIndex + this.browserType.length );
		if( this.browserType == 'firefox' || this.browserType == 'chrome' ) {
			if( /(\d+)\.(\d+)/.test( verStr) ) {
				this.majorVersion = RegExp.$1;
				this.minorVersion = RegExp.$2;
			}
		} else if ( this.browserType == 'msie' ) {
			if( /(\d+)/.test( verStr) ) this.majorVersion = RegExp.$1;
		}
	}
}

function findStyleBySelector( sT ) {
	if ( browserInfo.browserType != 'msie' ) {
		for( var i = 0; i < document.styleSheets.length ; ++i ) 
			for( var j = 0; j < document.styleSheets[i].cssRules.length ; ++j ) 
				with( document.styleSheets[i].cssRules[j] ) if( selectorText == sT ) return style;
	} else {
		for( var i = 0; i < document.styleSheets.length ; ++i ) 
			for( var j = 0; j < document.styleSheets[i].rules.length ; ++j ) 
				with( document.styleSheets[i].rules[j] ) if( selectorText == sT ) return style;
	}
	return null;
}

// this function found at http://www.go4expert.com/forums/showthread.php?t=886
function daysInMonth(iMonth, iYear)
 {
     return 32 - new Date(iYear, iMonth, 32).getDate();
 }
 
function getMaxVal( aList ) {
	var vMax = 0;
	if( aList.length ) {
		for ( var i=0 ; i < aList.length ; ++i ) 
			if( aList[i] > vMax ) vMax = aList[i];
	}
	return vMax;
}

function changeImg( imgId,imgSrc ) {
	var imgEle ;
	if( imgEle = document.getElementById( imgId ) ) {
		if( /MSIE/.test( navigator.userAgent ) ) imgEle.setAttribute("src",imgSrc,0);
		else imgEle.setAttribute("src",imgSrc);
	}
}

function searchLink() {
	var newQuestion = '';
	var searchPair = '';
	var demoNum = '';
	with( location ) {
		var qmark = search.substr(1); //ignore ? character
		var qlist = qmark.split('&');
		for( var i=0; i < qlist.length ; ++i ) {
			if( qlist[i].indexOf('keyWord') == -1  ) {
				if( newQuestion.length ) newQuestion += '&' + qlist[i];
				else newQuestion =  qlist[i];
			}
		}
		var searchVal = document.getElementById("searchWord").value;
		if( searchVal.length ) searchPair = 'keyWord=' + encodeURIComponent(searchVal);
		if( newQuestion.length ) {
			if( searchPair.length ) newQuestion = '?' + newQuestion + '&' + searchPair;
			else newQuestion = '?' + searchPair;
		} else if( searchPair.length ) newQuestion = '?' + searchPair;
		var catchDemo = pathname.match(/C\d{6}/);
		if( catchDemo && catchDemo.length ) demoNum = '/' + catchDemo[0];
		var url = protocol + '//' + host + demoNum + '/search.php' + newQuestion;
	}
	window.location = url;
}
