//SET GLOBALS 
var origRowColor
var origBorderColor

function set_inner_html(id,value)
{
  if(document.getElementById(id)) 
  {
    document.getElementById(id).innerHTML = value; 
  } 
}
function get_inner_html(id)
{
  if(document.getElementById(id)) 
  {
		return document.getElementById(id).innerHTML; 
  }
	else
	{
		return false;
	} 
}

function reset_form_vals(formId)
{
	if(formDoc = document.getElementById(formId))
	{
		for (var i = 0; i<formDoc.elements.length; i++)
		{
		 	//this func could use some work, perhaps.. text, vs TEXT, textArea, etc.
		 	if(formDoc.elements[i].type=='text')
		 	{
				formDoc.elements[i].value = '';
			}
		}
	}		
}

function slowlyDisappear(id)
{
	if(document.getElementById(id))
	{
		var delay = 10;
		var stHt = getHeight(id);
		var a = 0;
		document.getElementById(id).style.overflow='hidden';
		document.getElementById(id).style.visibility='hidden';
		document.getElementById(id).style.fontSize='1px';
		for(i=stHt;i>=0;i--)
		{
			a++; 
			setTimeout("set_height('"+id+"',"+i+")",(delay*a));
			if(i<3)
			{
			 	//break out when it gets to 3px high... time to remove from DOM
				setTimeout("remove_elem('"+id+"')",(delay*a));
				break;
			}
		}
	}
}

function remove_elem(id)
{
	//watch for DUPLICATE id names on the page!!!!!!!
	if(document.getElementById(id))
	{
		var item = document.getElementById(id);
		item.parentNode.removeChild(item);
	}
}

function set_border_color(id,color)
{
  // REQUIRES that the ID element has a VALID class in the CSS AND needs border=1+
	if(document.getElementById(id))
	{
  	document.getElementById(id).style.borderColor = color;  
	}
}
function set_bg_color(id,color)
{
	if(document.getElementById(id))
	{
	  document.getElementById(id).style.backgroundColor = color;  
	}
}

/* ONLY works if style is set INSIDE THE ELEM, NOT in CSS FILE */
function get_div_bg_color(id)
{
	if(document.getElementById(id))
	{
		return document.getElementById(id).style.backgroundColor;	
	}
}

/* ONLY works if style is set INSIDE THE ELEM, NOT in CSS FILE */
function get_div_border_color(id)
{
	if(document.getElementById(id))
	{
		return document.getElementById(id).style.borderColor;	
	}
}

function fade_color(id,type,speed)
{
	if(document.getElementById(id))
	{
  	var letter = "F";
  	var color = "";
  	var time = 0; //this gets added by every factor in between color shifts 
  	var factor = 65;  //delay between color shifts.  65ms*15colors = appx 1000ms (1 sec)
		//optional param is the speed... if not F,S,L, then use normal of 65
		if(speed == 'F')
		{
			factor = 35;
		}
		else if (speed == 'S')
		{
			factor = 200;
		}
		else if (speed == 'L')
		{
			//lightning
			factor = 10;
		}			
  	if(type == "") { type = "bg";}
  	var i=1;
  	while(i <= 9)
  	{
    	color = "#" + i + i + "FF" + i + i;  
    	time = time + factor; 
    
    	if(type == "border")
    	{
      	setTimeout("set_border_color('"+id+"','"+color+"')",time);
    	}
    	else
    	{
      	setTimeout("set_bg_color('"+id+"','"+color+"')",time);
    	}
    	i+=1;
  	}
  	i=1;
  	while(i<=6)
  	{
    	if(i == 1) {letter = "A";}
    	else if(i == 2) {letter = "B";}
    	else if(i == 3) {letter = "C";}
    	else if(i == 4) {letter = "D";}
    	else if(i == 5) {letter = "E";}
    	else if(i == 6) {letter = "F";}
    	color = "#" + letter + letter + "FF" + letter + letter; 
    	time = time + factor; 
    	if(type == "border")
    	{
      	setTimeout("set_border_color('"+id+"','"+color+"')",time);  //fading to white
    	}
    	else
    	{
      	setTimeout("set_bg_color('"+id+"','"+color+"')",time);  //fading white
    	}	
    	i+=1;
  	}
		if(type=="bg")
		{
			//return to the original color, if possible
			if(origRowColor != "")
			{
		 		//set AHEAD of the time factor else it gets overwritten
      	setTimeout("set_bg_color('"+id+"','"+origRowColor+"')",(time+25));
			}
		}
		if(type=="border")
		{
			//return to the original color, if possible
			if(origBorderColor != "")
			{
		 		//set AHEAD of the time factor else it gets overwritten
      	setTimeout("set_border_color('"+id+"','"+origBorderColor+"')",(time+25));
			}
		}
	}//end if ID
}	//end func

//just the standard yellow fade
function yellow_splash(id)
{
	if(document.getElementById(id))
	{
		elem = document.getElementById(id)
		var origColor = get_div_bg_color(id);
  	var letter = "F";
  	var factor = 50;
		var time;
		
  	var i=1;
  	for(i=0;i<10;i++)
  	{
    	color = "#FFFF" + i + i;  
    	time = i * factor; 
     	setTimeout("set_bg_color('"+id+"','"+color+"')",time);
   	}

  	i=1;
  	while(i<=6)
  	{
    	if(i == 1) {letter = "A";}
    	else if(i == 2) {letter = "B";}
    	else if(i == 3) {letter = "C";}
    	else if(i == 4) {letter = "D";}
    	else if(i == 5) {letter = "E";}
    	else if(i == 6) {letter = "F";}
    	color = "#FFFF" + letter + letter; 
    	time = time + factor; 
     	setTimeout("set_bg_color('"+id+"','"+color+"')",time);  //fading white
    	i++;
  	}
   	setTimeout("set_bg_color('"+id+"','"+origColor+"')",(time+25));
	}//end if ID
}	//end func


//does NOT change height of parent
function remove_float_div(id)
{
	//this will not work (especially in IE) if there are DUP id names on the page!!!)
	if(document.getElementById(id))
	{
		var item = document.getElementById(id);
		item.parentNode.removeChild(item);
	}
}

//PASS id of of the NEW div we will create, so just make up something UNIQUE
//PUTS a div into the BODY, we then position it in relation to the parent div 
function create_new_float_div(id)
{
	var newDiv;
	var doc = document.getElementsByTagName("body")[0];	//body tag MUST have an id set
	//safety check to see if this ID already exists, hit a link twice, or whatever, 
	//must first kill the existing elem to create a new one, easier than editing one that 
	//already exists
	remove_elem(id);
	
	newDiv = document.createElement("div");
	newDiv.id=id;
	//append the new div to the body
	//alert('append:'+newDiv.id);
	
	document.getElementById(doc.id).appendChild(newDiv); 

	//set up some key items, basically hide it for now, it has no info yet anyway
	document.getElementById(newDiv.id).style.visibility='hidden';
	document.getElementById(newDiv.id).style.overflow='hidden';
	document.getElementById(newDiv.id).style.zIndex=95;
	document.getElementById(newDiv.id).style.position='absolute';
	document.getElementById(newDiv.id).style.border='1px';
	//alert(document.getElementById(newDiv.id).style.visibility );
}

function grow_div_by_height(id)
{
	//REMEMBER:  only works if the div has overflow : hidden, either set in STYLE SHEET, OF if this 
	//is a NEW FLOAT DIV, then we SET when created IT !!!!!!!!!!
	
	//i am going to base the growth rate on how big we need to get!
	//set std growth rate
	var delay = 3;
	var inc = 1;
	var h = getHeight(id);
	set_height(id,1);
	//if building a large div, over 100px, speed up delay var, and increase inc by factor of 1x per 50
	var scope = (h / 100);
	if(scope >= 3)	//at least 300px high
	{
		inc = Math.round(inc * scope);
		delay = 1;
	}
	else if(scope >= 2)//at least 200px
	{
		inc = Math.round(inc * scope);
		delay = 2;
	}
	else if(scope >= 1)//at least 100px
	{
		//no change to delay var, only to inc var
		inc = Math.round(inc * scope);
	}

	//make visible (do not use toggle, just in case we are growing an already visible div)
	document.getElementById(id).style.visibility = 'visible';
	document.getElementById(id).style.display = 'block';
	
	//expand div to correct size, we use a std speed, but a variable increment amt.
	for(i=inc; i <= (h+inc+inc); i+=inc)
	{
		setTimeout("set_height('"+id+"',"+i+");",eval(delay*i));
	}
}

function grow_div_by_width(id)
{
 	//width id OPTIONAL, may already have it if this is NOT a new div
	//i am going to base the growth rate on how big we need to get!
	//set std growth rate
	var delay = 5;
	var inc = 1;
	
	var w = getWidth(id);
	set_width(id,1);

	//if building a large div, over 100px, speed up delay var, and increase inc by factor of 1x per 50
	var scope = (w / 250);
	if(scope >= 3)	//at least 300px high
	{
		inc = Math.round(inc * scope);
		delay = 3;
	}
	else if(scope >= 2)//at least 200px
	{
		inc = Math.round(inc * scope);
		delay = 3;
	}
	else if(scope >= 1)//at least 100px
	{
		//no change to delay var, only to inc var
		inc = Math.round(inc * scope);
	}
	//alert('scp: '+scope+' and '+inc+' and delay: '+delay);
	//make visible (do not use toggle, just in case we are growing an already visible div)
	document.getElementById(id).style.visibility='visible';
	//expand div to correct size, we use a std speed, but a variable increment amt.
	for(i=inc; i <= (w+inc); i+=inc)
	{
		setTimeout("set_width('"+id+"',"+i+");",eval(delay*i));
	}
}

function grow_div_from_starting_width(id,currW,newW)
{
 
	//i am going to base the growth rate on how big we need to get!
	//set std growth rate
	var delay = 3;
	var inc = 1;
	
	var diff = (newW - currW);
	var w = newW;
	//alert('grow: '+newW+' from '+ currW+' with diff of '+diff);
	//make visible (do not use toggle, just in case we are growing an already visible div)
	document.getElementById(id).style.visibility='visible';
	//expand div to correct size, we use a std speed, but a variable increment amt.
	var myCnt = 0;
	while(diff != 0 && myCnt < 500)
	{
	 	myCnt++;
		diff--;
		setTimeout("set_width('"+id+"',"+(newW - diff)+");",eval(delay*myCnt));
	}
}

function shrink_div_by_height(id)
{
	//REMEMBER:  only works if the div has overflow : hidden!!!!!!!!!!
	//i am going to base the growth rate on how big we need to get!
	//set std growth rate
	var delay = 3;
	var inc = 1;
	
	var h = getHeight(id);
	
	//if building a large div, over 100px, speed up delay var, and increase inc by factor of 1x per 50
	var scope = (h / 100);
	if(scope >= 3)	//at least 300px high
	{
		inc = Math.round(inc * scope);
		delay = 1;
	}
	else if(scope >= 2)//at least 200px
	{
		inc = Math.round(inc * scope);
		delay = 2;
	}
	else if(scope >= 1)//at least 100px
	{
		//no change to delay var, only to inc var
		inc = Math.round(inc * scope);
	}

	//expand div to correct size, we use a std speed, but a variable increment amt.
	var i=0;
	while(h>0)
	{
	 	h=h-inc;
	 	i++;
		setTimeout("set_height('"+id+"',"+h+");",eval(delay*i));
	}
	setTimeout("hide_item('"+id+"');",eval(delay*i));
}

function pos_sub_div_right_parent_div(parId,newId)
{
	var x = getRightPos(parId);
	var y = getTopPos(parId);
	document.getElementById(newId).style.left = x + 'px';
	document.getElementById(newId).style.top = y + 'px';
}

function pos_sub_div_under_parent_div(parId,newId)
{
 	//let's determine WHERE to put X coordinates...
	var L = getLeftPos(parId);
	var R = getRightPos(parId);
	var W = getWidth(parId);
	var w = getWidth(newId);
	var bodyW = getWidth('pageContainer');
	
	var x;
	var y;
	
	var diff = eval(bodyW - w - L);
	//if Lpos + elem width exceeds body, move it left until it fits
	if(diff >= 0)
	{
		x = L + 5;
	}
	else
	{
		x = eval(L + diff)- 5;
	}
	//alert('L '+ L + 'R '+R + 'W '+W + 'w '+w+' body '+bodyW+' diff '+diff+' new pos '+x);	
	y = getBottomPos(parId)+3;
	document.getElementById(newId).style.left = x + 'px';
	document.getElementById(newId).style.top = y + 'px';
	//position_div(newId,x,y);
}

function position_div(newId,x,y)
{
	document.getElementById(newId).style.left = x + 'px';
	document.getElementById(newId).style.top = y + 'px';
}

/*
	NOW, CREATE SCRIPTS THAT WILL APPEND TO THE CURRENT DIV 
	FOR EXAMPLE, FORUM POSTS, WANT THE DIV TO BE APPENDED INTO THE POSTS DIV, AND FILL IT.
*/

//PASS id of of the NEW div we will create, so just make up something UNIQUE
function append_new_div(parentId,id)
{
	//KILL existing div (if ran script twice, etc)
	remove_elem(id);
	var newDiv = document.createElement("div");
	newDiv.id=id;
	var doc = document.getElementById(parentId);
	document.getElementById(doc.id).appendChild(newDiv); 
	//set up some key items, basically hide it for now, it has no info yet anyway
	document.getElementById(newDiv.id).style.visibility='hidden';
	document.getElementById(newDiv.id).style.overflow='hidden';
	//document.getElementById(newDiv.id).style.zIndex=100;
	//document.getElementById(newDiv.id).style.position='absolute';
	//document.getElementById(newDiv.id).style.border='1px';
}

/*
//i could not get this to work without it just writing on top of sub divs.
function append_new_div_before(parentId,id)
{
 	//var doc = document.getElementsByTagName("body")[0];	//body tag MUST have an id set

	//KILL existing div (if ran script twice, etc)
	remove_elem(id);
	var newDiv = document.createElement("div");
	newDiv.id=id;
	var doc = document.getElementById(parentId);
	var ids = new Array();
	ids = getElementsById(parentId);
	alert(ids);
	for(i=0;i<ids.length;i++)
	{
	 	var tempp = ids[i].id;
		alert(parentId+' i: '+i+' val: '+ids[i] + ' '+tempp);
	}
	var firstChild = document.getElementById(tempp);
	
	//alert('id: '+id+' and pid: '+parentId);
	//var temp = document.getElementsById(obj.parentId)[0];	//body tag MUST have an id set
	//alert('show::::'+temp);
	//alert(doc.childNodes[0].id);
	//doc.appendChild(newDiv); 
	//alert('what is fchild ' + doc.firstChild);
	doc.insertBefore(newDiv,firstChild.id);
	//set up some key items, basically hide it for now, it has no info yet anyway
	document.getElementById(newDiv.id).style.visibility='hidden';
	document.getElementById(newDiv.id).style.overflow='hidden';
	document.getElementById(newDiv.id).style.zIndex=100;
	document.getElementById(newDiv.id).style.position='absolute';
	document.getElementById(newDiv.id).style.border='1px';
}
*/
function ajax_measure_stick(lvl)
{
 	if(document.getElementById('ajaxMeasureStick'))
 	{
		if(lvl == undefined)
		{
			lvl = 1;
		}
		if(lvl == 0)
		{
			document.getElementById('ajaxMeasureStick').style.visibility = 'hidden';
			document.getElementById('ajaxMeasureStickBar').style.visibility = 'hidden';
			//document.getElementById('ajaxMeasureStick').style.display = 'none';
 			set_width('ajaxMeasureStickBar',0);
		}
		else
		{
 			if(document.getElementById('ajaxMeasureStick').style.visibility != 'visible')
 			{
 				toggle_item('ajaxMeasureStick');	//show it
 				toggle_item('ajaxMeasureStickBar');	//show it
		 	}

	 		var currW = getWidth('ajaxMeasureStickBar');
			var w = lvl * 100;
			grow_div_from_starting_width('ajaxMeasureStickBar',currW,w);
		}
	}
}	  


//some other js stuff i yanked out of a js file
//global VAR
rowElemColors = new Array();
elemGlowColor = 'lightyellow';
elemDefaultColor = 'white';
//start functions
//puts the focus on a field
function fieldFocus(fieldName)
{
	if(document.getElementById)
	{
		if(document.getElementById(fieldName))
		{
			fieldColor(fieldName);
			document.getElementById(fieldName).focus();
		}
	}
}

//highlights the field (not normally needed, using colorCheckBox with onfocus)
function fieldColor(fieldName)
{
	if(document.getElementById)
	{
		if(document.getElementById(fieldName))
		{
			document.getElementById(fieldName).style.backgroundColor="#EEFFFF";
		}
	}
}

/**
* colorTextbox
*
* Changes the color of a text box or text area
*
* @param  string  myObject  name of field
*/
function colorTextbox(myObject) {
  if(document.getElementById) {
    myObject.style.backgroundColor = '#FFFFCC';
  } else if(document.all) {
    myObject.style.backgroundColor = '#FFFFCC';
  }
}

/**
* uncolorTextbox
*
* Changes the color of a text box or text area
* back to white
*
* @param  string  myObject  name of field
*/
function uncolorTextbox(myObject) {
  if(document.getElementById) {
    myObject.style.backgroundColor = '#FFFFFF';
  } else if(document.all) {
    myObject.style.backgroundColor = '#FFFFFF';
  }
}

/* toggle item */
function hide_item(id)
{
	if(document.getElementById(id))
  {
 		document.getElementById(id).style.visibility = 'hidden';
    document.getElementById(id).style.display = 'none';
  }
}

function show_item(id)
{
	if(document.getElementById(id))
  {
 		document.getElementById(id).style.visibility = 'visible';
    document.getElementById(id).style.display = 'block';
  }
}

function toggle_item(itemId)
{
	if(document.getElementById(itemId))
  {
  	if(document.getElementById(itemId).style.visibility != 'visible')
  	{
  	 	show_item(itemId);
    }
    else
    {
			hide_item(itemId);
    }
  }
}

function getTopPos(id)
{		
	var item = document.getElementById(id);
	var returnValue = item.offsetTop;
	while((item = item.offsetParent) != null)
	{
		if(item.tagName!='HTML')returnValue += item.offsetTop;
	}
	return returnValue;
}
	
function getWidth(id)
{
	var item = document.getElementById(id);
	returnValue = item.offsetWidth;
	return returnValue;
}

function getHeight(id)
{
	var item = document.getElementById(id);
  var returnValue = item.offsetHeight;
  return returnValue;
}
	
function getLeftPos(id)
{
	var item = document.getElementById(id);
	var returnValue = item.offsetLeft;
	while((item = item.offsetParent) != null)
	{
		if(item.tagName!='HTML')returnValue += item.offsetLeft;
	}
	return returnValue;
}

function getRightPos(id)
{
	var item = document.getElementById(id);
 	var returnValue = eval(getLeftPos(id) + getWidth(id));
 	return returnValue; 
}

function getBottomPos(id)
{
	var item = document.getElementById(id);
 	var returnValue = eval(getTopPos(id) + getHeight(id));
 	return returnValue; 
}

function set_width(id,width)
{
	if(document.getElementById(id))
	{
	  document.getElementById(id).style.width=width+'px';  
	}
}

function set_height(id,height)
{
	if(document.getElementById(id))
	{
	  document.getElementById(id).style.height=height+'px';  
	}
}

//function i grabbed somehwere...
function getElementsById(sId)
{
    var outArray = new Array();	
	if(typeof(sId)!='string' || !sId)
	{
		return outArray;
	};
	
	if(document.evaluate)
	{
		var xpathString = "//*[@id='" + sId.toString() + "']"
		var xpathResult = document.evaluate(xpathString, document, null, 0, null);
		while ((outArray[outArray.length] = xpathResult.iterateNext())) { }
		outArray.pop();
	}
	else if(document.all)
	{
		
		for(var i=0,j=document.all[sId].length;i<j;i+=1){
		outArray[i] =  document.all[sId][i];}
		
	}else if(document.getElementsByTagName)
	{
	
		var aEl = document.getElementsByTagName( '*' );	
		for(var i=0,j=aEl.length;i<j;i+=1){
		
			if(aEl[i].id == sId )
			{
				outArray.push(aEl[i]);
			}
		}
		
	}
	return outArray;
}

function glow_elem(id)
{
  var elem = document.getElementById(id);
  var currColor = elem.style.backgroundColor;
  if(currColor == "" || currColor == "undefined")
  {
		currColor = elemDefaultColor;
	}
	//alert('curr: '+currColor+' and ' + elemGlowColor);
  if(currColor != elemGlowColor)
  {
		elem.style.backgroundColor = elemGlowColor;
    rowElemColors[id]=currColor;
  }
  else
  {
 		elem.style.backgroundColor = rowElemColors[id];     
  }
}

