var browserInfo = new myBrowserDetect();

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

function getWindowPosAndSize() {
	if( window.innerWidth ) {
		return [window.pageXOffset,window.pageYOffset,window.innerWidth,window.innerHeight];
	} else {
		var ieBody = ( document.compatMode && document.compatMode != 'BackCompat' ) ? document.documentElement : document.body;
		return [ieBody.scrollLeft,ieBody.scrollTop,ieBody.clientWidth,ieBody.clientHeight];
	}
}

function centerInWindow( alignEle,winPosList ) {
	var alignWidth = getAttr( alignEle,'clientWidth',true );
	var alignHeight = getAttr( alignEle,'clientHeight',true );
	with ( alignEle.style ) {
		left = winPosList[0] + ( ( winPosList[2] - alignWidth ) / 2 ) + 'px';
		top = winPosList[1] + ( ( winPosList[3] - alignHeight ) / 2 ) + 'px';
	}
}

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,stopParentId ) {
	this.aTop = 0;
	this.aLeft = 0;
	var parEle = null;
	if( typeof stopParentId == 'string' ) parEle = document.getElementById( stopParentId );
	if( getEle ) {
		while ( getEle.offsetParent != null && getEle != parEle ) {
			this.aTop += getEle.offsetTop;
			this.aLeft += getEle.offsetLeft;
			getEle = getEle.offsetParent;
		}
	}
}
//Code From:  http://blog.stchur.com/2006/06/21/css-computed-style/
function getStyle(_elem, _style) {
  var computedStyle;
  if (typeof _elem.currentStyle != 'undefined')
    { computedStyle = _elem.currentStyle; }
  else
    { computedStyle = document.defaultView.getComputedStyle(_elem, null); }

  return computedStyle[_style];
}

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( typeof selectorText == 'string' && 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( typeof selectorText == 'string' && 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 && qlist[i].indexOf('page') == -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;
}

function getKey( e ) {
	var e = window.event || e;
	var keyCode = e.keyCode || e.charCode;
	if( keyCode == 13 ) searchLink();
}

function clearTextInSearchBox() {
	document.getElementById("searchWord").setAttribute("value",'');
}

function initCheckOpResult() {
	var searchE;
	if( typeof opResult == 'object' ) {
		with( opResult ) {
			if( typeof result == 'number' ) {
				alert( message );
				if( typeof urlBack != 'undefined' && urlBack ) history.back();
			}
		}
	}
	if( typeof miscellaneousConfig == 'object' ) {
		for( i in miscellaneousConfig ) {
			if ( i == 'searchOnEnter' ) {
				if( document.addEventListener ) {
					document.addEventListener('keypress',getKey,false);
				} else if ( document.attachEvent ) {
					document.attachEvent('onkeypress',getKey,false);
				}
			}
		}
	}
	if( searchE = document.getElementById("searchWord") ) {
		if( searchE.addEventListener ) searchE.addEventListener( "focus" , clearTextInSearchBox, false );
		else if ( searchE.attachEvent ) searchE.attachEvent( "onfocus", clearTextInSearchBox );
	}
}

if( window.addEventListener ) window.addEventListener( "load" , initCheckOpResult, false );
else if ( window.attachEvent ) window.attachEvent( "onload", initCheckOpResult );

