function setTextInputEffects(){
	var allInputs = document.getElementsByTagName('INPUT');
	for(var i = 0; i < allInputs.length ; i++){
		if(allInputs[i].className == 'text'){
			allInputs[i].onfocus = change_inner_white;
			allInputs[i].onblur = change_inner_back;
		}
	}
	var allTextAreas = document.getElementsByTagName('TEXTAREA');
	for(var i = 0; i < allTextAreas.length ; i++){
		if(allTextAreas[i].className == 'text'){
			allTextAreas[i].onfocus = change_inner_white;
			allTextAreas[i].onblur = change_inner_back;
		}
	}
}

function addShadowToElement(ele,depth,baseColor,sides){
	if(typeof(sides) == 'undefined') var sides = new Array('top','bottom','left','right');
	for(var a = 0; a < sides.length; a++){
		for (var x=0; x<depth; x++){
			// left
			var newSd = document.createElement("DIV");
			newSd.className = "shadow_inner";
			newSd.style.backgroundColor = baseColor;
			newSd.style.opacity = (depth - x - 1)/depth;
			newSd.style.filter = "alpha(opacity=" + (depth - x - 1) * 100/depth  + ")";
			if(sides[a] == 'left'){
				newSd.style.top = '0px';
				newSd.style.left = ((-1*x)- 1)+ 'px';
				newSd.style.height = ele.offsetHeight + 'px';
				newSd.style.width = '1px';
			}
			else if(sides[a] == 'right'){
				newSd.style.top = '0px';
				newSd.style.height = ele.offsetHeight + 'px';
				newSd.style.width = '1px';
				newSd.style.right = ((-1*x)- 1)+ 'px';
			}
			else if(sides[a] == 'top'){
				newSd.style.left = '0px';
				newSd.style.width = ele.offsetWidth + 'px';
				newSd.style.height = '1px';
				newSd.style.top = ((-1*x)- 1)+ 'px';
			}
			else if(sides[a] == 'bottom'){
				newSd.style.left = '0px';
				newSd.style.width = ele.offsetWidth + 'px';
				newSd.style.height = '1px';
				newSd.style.bottom = ((-1*x)- 1)+ 'px';
			}
			
			ele.appendChild(newSd);
			
		}
	}
}

function setOpacity(theObj, value) {
	theObj.style.opacity = value/100;
	theObj.style.filter = 'alpha(opacity=' + value + ')';
}


function change_inner_white(){
	this.style.backgroundImage="none";
	this.style.borderColor="#0D6599";
}
function change_inner_back(){
	this.style.backgroundImage="url("+allsiteurl+"images/cbg3.gif)";
	this.style.borderColor="#ccc";
}

function resizeElementHeight(element) {
	var height = 0;
	var body = window.document.body;
	if (window.innerHeight) {
		height = window.innerHeight;
	} else if (body.parentElement.clientHeight) {
		height = body.parentElement.clientHeight;
	} else if (body && body.clientHeight) {
		height = body.clientHeight;
	}
	element.style.height = ((height - element.offsetTop) + "px");
}

function returnWindowDimensions(){
	var b=document.body, e=document.documentElement;
	var esw=0, eow=0, bsw=0, bow=0, esh=0, eoh=0, bsh=0, boh=0;
	if (e) {
		esw = e.scrollWidth;
		eow = e.offsetWidth;
		esh = e.scrollHeight;
		eoh = e.offsetHeight;
	}
	if (b) {
		bsw = b.scrollWidth;
		bow = b.offsetWidth;
		bsh = b.scrollHeight;
		boh = b.offsetHeight;
	}

	return Array(Math.max(esw,eow,bsw,bow),Math.max(esh,eoh,bsh,boh));
}

function posTop() {
	return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

function elmYPosition(eID) {
	var elm  = document.getElementById(eID);
	var y    = elm.offsetTop;
	var node = elm;
	while (node.offsetParent && node.offsetParent != document.body) {
		node = node.offsetParent;
		y   += node.offsetTop;
	} return y - 35; //- 35 because of top bar
}
function smoothScroll(a) {
	if(typeof a == "number" || typeof a == "boolean") stopY = a;
	else if(typeof a == "string") stopY = elmYPosition(a);
	else if(typeof a == "object") stopY = findPosY(a);
	else return false
	
	var startY   = posTop();
	var distance = stopY > startY ? stopY - startY : startY - stopY;
	if (distance < 100) {
		scrollTo(0, stopY); return;
	}
	var speed = Math.round(distance / 100);
	var step  = Math.round(distance / 25);
	var leapY = stopY > startY ? startY + step : startY - step;
	var timer = 0;
	if (stopY > startY) {
		for ( var i=startY; i<stopY; i+=step ) {
			setTimeout("window.scrollTo(0, "+leapY+")", timer * speed);
			leapY += step; if (leapY > stopY) leapY = stopY; timer++;
		} return;
	}
	for ( var i=startY; i>stopY; i-=step ) {
		setTimeout("window.scrollTo(0, "+leapY+")", timer * speed);
		leapY -= step; if (leapY < stopY) leapY = stopY; timer++;
	}
}

function randomString(length){
	var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	var pass = "";
	for(x=0;x<length;x++){
		i = Math.floor(Math.random() * chars.length);
		pass += chars.charAt(i);
	}
	return pass;
}

function randomInt(length){
	var chars = "1234567890";
	var pass = "";
	for(x=0;x<length;x++){
		i = Math.floor(Math.random() * chars.length);
		pass += chars.charAt(i);
	}
	return pass;
}

function addslashes(str) {
	return (str+'').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}

function stripslashes(str) {
	return (str+'').replace(/\\(.?)/g, function (s, n1) {
        switch (n1) {
            case '\\':
                return '\\';
            case '0':                return '\u0000';
            case '':
                return '';
            default:
                return n1;        }
    });
}

function setSelectValue(theSelect,theValue){
	for (var i = 0; i < theSelect.length; i++) {
		if (theSelect[i].value == theValue) {
			if (theSelect.selectedIndex != i) {
				theSelect.selectedIndex = i;
				//theSelect.onchange();
				break;
			}
		}
	}
}

function removeSelectOption(theSelect,theValue){
	for (var i = 0; i < theSelect.length; i++) {
		if (theSelect.options[i].value == theValue) {
			theSelect.options[i] = null;
		}
	}
}

function returnRadioValue(rName){
	var allRadios = document.getElementsByName(rName);
	for(var i = 0; i < allRadios.length; i++){
		if(allRadios[i].checked) return allRadios[i].value;
	}
}

function findPosX(obj){
	var curleft = 0;
	if(obj.offsetParent)
	while(1){
		curleft += obj.offsetLeft;
		if(!obj.offsetParent) break;
		obj = obj.offsetParent;
	}
	else if(obj.x) curleft += obj.x;
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if(obj.offsetParent)
	while(1){
		curtop += obj.offsetTop;
		if(!obj.offsetParent) break;
		obj = obj.offsetParent;
	}
	else if(obj.y) curtop += obj.y;
	return curtop;
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} 
	else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

//dynamically include js and css files
function loadjscssfile(filename, filetype){
	if (filetype=="js"){ //if filename is a external JavaScript file
		var fileref=document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src", filename);
	}
	else if (filetype=="css"){ //if filename is an external CSS file
		var fileref=document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", filename);
	}
	if (typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref);
}

var filesadded="" //list of files already added

function checkloadjscssfile(filename, filetype, addVersion){
	if(addVersion){ // make sure it's not an external file
		if (typeof cssVersion == "undefined") cssVersion = jsVersion = 1;
		var fileNameSplit = filename.split('.');
		var fileExtArray = new Array();
		fileExtArray['css'] = cssVersion;
		fileExtArray['js'] = jsVersion;
		fileNameSplit[fileNameSplit.length - 2] += '_v' + fileExtArray[filetype];
		filename = fileNameSplit.join('.');
	}
	if (filesadded.indexOf("["+filename+"]")==-1){
		loadjscssfile(filename, filetype);
		filesadded+="["+filename+"]"; //List of files added in the form "[filename1],[filename2],etc"
	}
}

function concat_collection(obj1, obj2) {
	var i;
	var arr = new Array();
	var len1 = obj1.length;
	var len2 = obj2.length;
	for (i=0; i<len1; i++) {
		arr.push(obj1[i]);
	}
	for (i=0; i<len2; i++) {
		arr.push(obj2[i]);
	}
	return arr;
}

function clearHash(){
	document.location.hash = '';
}

function gebi(theID){
	return document.getElementById(theID);
}

function gebcn(cl,eleType) {
	if (typeof eleType == "undefined") eleType = 'DIV';
	var retnode = [];
	var elem = document.getElementsByTagName(eleType);
	for (var i = 0; i < elem.length; i++) {
		if (elem[i].className == cl) retnode[retnode.length] = elem[i];
	}
	return retnode;
}; 

function trim(str){
	return str.replace(/^\s*/, "").replace(/\s*$/, "");
}

function idCheck(str){
	var regex = /^[a-zA-Z0-9]{5,20}$/gi;
	return str.match(regex)
}

function emailCheck(str){
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	return reg.test(str)
}

function findScripts(divID){
	var allScripts = document.getElementById(divID).getElementsByTagName('SCRIPT');
	for(var s = 0; s < allScripts.length; s++){
		var newScript = document.createElement('script');
		newScript.type = "text/javascript";
		newScript.text = allScripts[s].text;
		document.getElementsByTagName("head")[0].appendChild(newScript);
	}
}

function resizeImage(fullsrc,imgElement,newWidth){
	var imgz= new Image();
	imgz.src = fullsrc;
	if (imgz.complete){
		if(imgz.width>newWidth){
			imgElement.width= newWidth;
			imgElement.height= (newWidth/imgz.width)*imgz.height
		}
		else{
			imgElement.width = imgz.width;
			imgElement.height = imgz.height;
		}
	}
	else {
		setTimeout(function(){resizeImage(fullsrc,imgElement,newWidth)}, 20);
	}
}

var currentAjaxAnchor = '';

function ajaxHashCheck(){
	if(currentAjaxAnchor != document.location.hash){
		currentAjaxAnchor = document.location.hash;
		callAjaxNavigationFunctions();
	}
}

var allImageArray = new Array();

function preLoadImages(imageArray){
	for(var i = 0; i < imageArray.length; i++){
		allImageArray[i] = new Image();
		allImageArray[i].src = imageArray[i];
	}
}

function setDropBackgroundHeight(){
	var allBacks = gebcn('topnav_link_drop_back');
	for(var i = 0; i < allBacks.length; i++){
		//alert(allBacks[i].parentNode.offsetHeight);
		allBacks[i].parentNode.style.visibility = 'hidden';
		allBacks[i].parentNode.style.display = 'block';
		allBacks[i].style.height = allBacks[i].parentNode.offsetHeight + "px"
		
		allBacks[i].parentNode.style.visibility = 'visible';
		allBacks[i].parentNode.style.display = 'none';
		
		//alert("huh")
	}
}

var ajaxNavigationArray = new Array();

function setAjaxNavigationFunction(func){
	ajaxNavigationArray[ajaxNavigationArray.length] = func;
}

function callAjaxNavigationFunctions(){
	for(var i = 0; i < ajaxNavigationArray.length; i++){
		ajaxNavigationArray[i]();
	}
}

function setOverlay(){
	if(!(overlay = gebi('general_overlay'))){
		var body = window.document.body;
		var dim = returnWindowDimensions();
		var overlay = document.createElement('DIV');
		overlay.style.width = dim[0] + 'px';
		overlay.style.height = dim[1] + 'px';
		overlay.id = 'general_overlay';
		body.appendChild(overlay);
	}
	overlay.style.display = 'block';
}

function reloadPage(){
	window.location.reload()
}

function isImageLoaded(img) {
	if (!img.complete) return false; 
	if (typeof img.naturalWidth !='undefined' && img.naturalWidth == 0) return false;
	return true;
}

function resizeShadowBorders(){
	if(headerDiv && footerDiv){
		var theDivs = concat_collection(headerDiv.getElementsByTagName('DIV'),footerDiv.getElementsByTagName('DIV'));
		for(var i = 0; i < theDivs.length; i++){
			var theParent = theDivs[i].parentNode;
			if(theDivs[i].className == 'shadow_inner') theDivs[i].style.width = theParent.offsetWidth + 'px';
		}
	}
}

function showHiddenProfileSection(me,hiddenDivID){
	gebi(hiddenDivID).style.display = 'block';
	me.style.visibility = 'hidden';
	smoothScroll(me.parentNode);
	forceFooterRecalc();
}


function hideHiddenProfileSection(me){
	me.parentNode.style.display = 'none';
	smoothScroll(me.parentNode);
	gebi(me.parentNode.id + '_button').style.visibility = 'visible';
	forceFooterRecalc();
}

function submitLoginForm(){
	document.login_form.submit();
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function forceFooterRecalc(){
	if((theFooter = gebi('footer'))){
		theFooter.style.position = 'relative';
		theFooter.style.position = 'absolute';
	}
}

var pageIsLoaded = false;
var headerDiv, footerDiv = false;

addLoadEvent(function(){
	//setTextInputEffects();
	setInterval(ajaxHashCheck,150);
	pageIsLoaded = true;
	if(gebi('navigation')) setDropTop();
	var ver = getInternetExplorerVersion();
	if(ver == -1 || ver > 6){
		if((headerDiv = gebi('header'))) addShadowToElement(headerDiv,4,'#444444',Array('bottom'));
		if((footerDiv = gebi('footer'))) addShadowToElement(footerDiv,5,'#444444',Array('top'));
		if((allMainBlocks = gebcn('main_block'))) for(var m = 0; m < allMainBlocks.length; m++) addShadowToElement(allMainBlocks[m],4,'#999999');
		if((allMainBlocks = gebcn('mini_block'))) for(var m = 0; m < allMainBlocks.length; m++) addShadowToElement(allMainBlocks[m],4,'#999999');
	}
	else{
		setDropBackgroundHeight()
	}
	
});

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-24665874-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

window.onresize = function(event) {
	resizeShadowBorders();
}
