// global vars here
var page_content = new Object();
var timeout = new Object();

// set up cursor_pos for later use
var cursor_pos = {x:0, y:0};
document.onmousemove = get_cursor_pos;

function toggle_div(div_id,mode){
	var showme = ( mode == 'block' ?mode:'none' );
	document.getElementById(div_id).style.display = showme;
}

// check for content in parent, fill from there or fire_trans to get 
function flip_content(page){
	content[current_page] = document.getElementById('content').innerHTML;
	current_page = page;
	page = page + '.pl';
	fire_trans(page);
}

function show_div(page, div_to_show,menu_class) {
	if (current_page[page]) {
		document.getElementById(current_page[page]).style.display = 'none';	
        document.getElementById(current_page[page] + '_tab').className = menu_class;
	}
	current_page[page] = div_to_show;
	document.getElementById(current_page[page]).style.display = 'block';
    document.getElementById(current_page[page] + '_tab').className = menu_class + '_selected';
	return false;
}

function write_content(div_id,content){
    document.getElementById(div_id).innerHTML = content;
}

function fire_trans(url){
	parent.tgtrans.document.location.href = url;
	return true;
}

function draw_main(div_id,component){
	var url = component + '.pl?div_id=' + div_id;
	write_content(div_id,component);
	fire_trans(url);
	return true;
}

function draw_menu_list(div_id,data) {
	var filename = location.pathname.substr(location.pathname.lastIndexOf("\\")+2,location.pathname.length);
    var menu = '';
	for(var i in data) {
		if (data[i][0] == current_app) {
		    menu += '<DIV CLASS=menu_item_selected>' + data[i][1] + '</DIV>\n';
		}
		else {
		    menu += '<DIV CLASS=menu_item onMouseOver="this.style.backgroundColor=\'#ffff80\'" ' +
					'onMouseOut="this.style.backgroundColor=\'#faf365\'" onClick="draw_main(\'page_content\',\'' + 
				     data[i][0] + '\')">' + data[i][1] + '</DIV>';
		}
	}
	write_content(div_id,menu);
}

function vertical_menu(div_id,title,onclick,data){
	var size = div_width_height(div_id);
	var div_width = size[0] - 20;
	var menu = '';
	if(title){
    	menu = '<SPAN CLASS=lmenu_header>' + title + '</SPAN><BR>';
	}
    for(var i in data) {
		if(data[i][0] == undefined){
			continue;
		}
		else{
			if (current_page[div_id] == data[i][0]) {
				disp_class = 'lmenu_item_selected';
			}
			else {
				disp_class = 'lmenu_item';
			}
        	menu += '<DIV ID="id_' + data[i][1] + '" CLASS=' + disp_class + ' onClick="' + onclick + '(this)"'; 
			menu += ' STYLE="width:' + div_width + 'px" onMouseOver="this.style.backgroundColor=\'#' + hovercolor;
			menu += '\'" onMouseOut="this.style.backgroundColor=\'#' + bgcolor + '\'">' + data[i][1] + '</DIV>';
		}
    }
	// have to write html to page to get dynamic height/width from divs
	write_content(div_id,menu);
}

function horizontal_menu(div_id,data) {
	var size = div_width_height(div_id);
	var div_width = size[0];
	var div_height = size[1];
	div_width = div_width/data.length - 8;
    var menu = '';
	// cycle data, draw, then populate, then find tallest div, then adjust for vert align
    for(var i in data) {
		if(data[i][0] == undefined){
			continue;
		}
		else{
			if (current_page[div_id] == data[i][0]) {
				disp_class = 'menu_item_selected';
			}
			else {
				disp_class = 'menu_item';
			}
        	menu += '<DIV ID="id_' + data[i][0] + '" CLASS=' + disp_class + ' onClick="flip_content(\'' + data[i][0] 
				  + '\');return false;" STYLE="width:' + div_width + 'px" onMouseOver="this.style.backgroundColor=\'#343434\'" ' 
				  + 'onMouseOut="this.style.backgroundColor=\'#000\'" >' + data[i][1] + '</DIV>';
		}
    }
	// have to write html to page to get dynamic height/width from divs
	write_content(div_id,menu);
	var max_height = 0;
    for(var i in data) {
		if(data[i][0] == undefined){
			continue;
		}
		else{
			size = div_width_height('id_' + data[i][0]);
			div_height = size[1];
			max_height = div_height > max_height? div_height: max_height;
    	}
	}
	// cycle divs, adjust vertical spacing, ALWAYS ASSUME no more than 2 lines
    for(var i in data) {
		if(data[i][0] == undefined){
			continue;
		}
		else{
			div_ele = document.getElementById('id_' + data[i][0]);
			size = div_width_height('id_' + data[i][0]);
			div_height = size[1];
			if(max_height > div_height){
				// want to put bottom of single line at 66%
				div_ele.style.height = max_height;
				div_ele.style.lineHeight = max_height + 'px';
			}
		}
    }
}

function draw_links(div_id,menu_class,function_call,colspan,data) {
    var menu = '';
	var colcount = 0;
    for(var i in data) {
		colcount++;
		if (colcount == 1) {
			menu += '<DIV CLASS=page_text>';
		}
        menu += '<DIV ID="' + data[i][0] + '_link" CLASS=' + menu_class + ' onMouseOver="this.style.backgroundColor=\'#ddd\'" ' +                    'onMouseOut="this.style.backgroundColor=\'#fff\'" onClick="' + 
				function_call + '(\'' + data[i][0] + '\');return false;">' + data[i][1] + '</DIV>\
';
		if (colcount == colspan) {
			menu += '</DIV>';
			colcount = 0;
		}
    }
	if (colcount != colspan) {
		menu += '</DIV>';
	}
	write_content(div_id,menu);
}

function set_opacity(obj_id,opacity_pct) {
	// first line is rational browsers, second is for dumb ones
	var obj = document.getElementById(obj_id);
	try{
		obj.style.opacity = opacity_pct/100;
	}catch(err){}
	try{
		obj.filters.alpha.opacity = opacity_pct;
	}catch(err){}
	try{
		obj.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity=" + opacity_pct + ");";
	}catch(err){}
}

// starting opacity pct, ending opacity, pct increment in pct
function fade_element(obj_id,start,end,increment) {
	var timer = 100;
	var obj = document.getElementById(obj_id);
	if(start < end){
		// filling element
		while(start < end) {
			setTimeout("set_opacity('" + obj_id + "'," + start + ")",timer);
			start += increment;
			timer += 50;
		}
		setTimeout("set_opacity('" + obj_id + "'," + end + ")",timer);
	}
	if(start > end){
		while(start > end) {
			setTimeout("set_opacity('" + obj_id + "'," + start + ")",timer);
			start -= increment;
			timer += 50;
		}
		setTimeout("set_opacity('" + obj_id + "'," + end + ")",timer);
		if(end == 0){
			setTimeout("toggle_div('" + obj_id + "','none')",timer);
		}
	}
}

function fade_swap(fade_name,show_name,time){
	fade_element(fade_name,100,20,20);
	fade_element(show_name,50,100,20);
}

// get window size, calculate center, calculate offset to upper left absolute position
function pop_div(div_id,width,height,content,top_adjust){
	if(top_adjust == undefined){
		top_adjust = 0;
	}
	var size = window_width_height();
	var mid_w = size[0]/2;
	var mid_h = size[1]/2;
	var divleft = mid_w - width/2;
	var divtop = mid_h - height/2 + top_adjust;
	var div_ele = document.getElementById(div_id);
	div_ele.style.width = width;
	if(height){
		div_ele.style.height = height;
	}
	div_ele.style.left = divleft + 'px';
	div_ele.style.top = divtop + 'px';
	write_content(div_id,content);
	div_ele.style.display = 'block';
	div_ele.style.visibility = 'visible';
	fade_element(div_id,40,100,10);
}
function direct(directions){
	content = '<DIV CLASS=directions>' + directions + '</DIV>';
	pop_cursor_div('dynamic',100,50,content);
}
// get window size, calculate center, calculate offset to upper left absolute position
function pop_cursor_div(div_id,width,height,content){
	var div_ele = document.getElementById(div_id);
	div_ele.style.width = width;
	div_ele.style.height = height;
	write_content(div_id,content);
	div_ele.style.left = cursor_pos.x - width + 5;
	div_ele.style.top = cursor_pos.y - height + 5;
	div_ele.style.display = 'block';
}
function window_width_height(){
	var width;
	var height;
	try{
		width = window.innerWidth;
		height = window.innerHeight;
		if(height == undefined){
			height = document.body.offsetHeight;
			width = document.body.offsetWidth;
		}
	}catch(err){}
	return [ width, height ];
}

function div_width_height(div_id){
	var div_ele = document.getElementById(div_id);
	var width = parseInt(div_ele.offsetWidth,10);
	var height = parseInt(div_ele.offsetHeight,10);
	return [ width,height ];
}

function getkey(e) {
	var keynum;
	var keychar;
	var numcheck;
	if(window.event){
  		keynum = e.keyCode;
  	} 
	else if(e.which){
  		keynum = e.which;
  	}
	keychar = String.fromCharCode(keynum);
}
function upload(source,id){
	var upload_form = draw_form(upload_form);
}
	
function admin(){
	var content = draw_form('',login_form);
	pop_cursor_div('dynamic',330,80,content);
	document.forms.loginform.login.focus();
	var admin_timeout = 0;
	timeout['dynamic'] = setTimeout('toggle_div(\'dynamic\',\'none\')',10000);
}
function clear_timeout(timeout){
	try{
		clearTimeout(timeout);
	}catch(err){}
}
function get_cursor_pos(e) {
	e = e || window.event;
	if (e.pageX || e.pageY) {
		cursor_pos.x = e.pageX;
		cursor_pos.y = e.pageY;
	}
	else {
		cursor_pos.x = e.clientX + (document.documentElement.scrollLeft ||
				   document.body.scrollLeft) - document.documentElement.clientLeft;
		cursor_pos.y = e.clientY + (document.documentElement.scrollTop ||
				   document.body.scrollTop) - document.documentElement.clientTop;
	}
}
function hide_form_div(div_ele){
	div_ele.parentNode.parentNode.parentNode.parentNode.style.display = 'none';
}
function draw_display_rows(id,page_data,data){
	var content = '<DIV CLASS=main_display>';
	var labelwidth = 0;
	var valuewidth = 0;
	for(var i in page_data){
		if(page_data[i][0] && page_data[i][1]){
			labelwidth = labelwidth > page_data[i][0].length? labelwidth: page_data[i][0].length;
			valuewidth = valuewidth > page_data[i][1].length? valuewidth: data[page_data[i][1]].length;
		}
	}
	for(var i in page_data){
		if(page_data[i][0]){
		    content += '<DIV CLASS=displayrow>'
                     + '<DIV CLASS=displaylabel STYLE="width:"' + labelwidth + 'px">' + page_data[i][0] + ':</DIV>'
                     + '<DIV CLASS=displayvalue STYLE="width:"' + valuewidth + 'px">' + data[page_data[i][1]] + '</DIV>'
                     + '</DIV>';
		}
	}
	return content;
}

function url_window(url,name,width,height){
	window.open(url,'',"height=' + height + ',width=' + width + ',menubar=0,status=1,scrollbars=0,resizable=1");
}
function valid_phone(ele){
	var phone = ele.value;
	phone = phone.replace(/\D/g,"");
	if(phone.length != validate['phone_length']){
		alert('Phone numbers must be ' + validate['phone_length'] + ' digits.');
		ele.focus();
		return true;
	}
	ele.value = format_phone(phone);
	return true;
}
function format_phone(phone){
	if(phone.length == 10){
		return '(' + phone.substr(0,3) + ') ' + phone.substr(3,3) + '-' + phone.substr(6,4);
	}
	if(phone.length == 7){
		return phone.substr(0,3) + '-' + phone.substr(3,4);
	}
	return phone;
}

function set_class_name(ele_id,classname){
	var ele = document.getElementById(ele_id);
	ele.className = classname;
}

function sleep(seconds){
	seconds *= 1000;
	var sleeping = true;
	var now = new Date();
	var alarm;
	var startingMSeconds = now.getTime();
	while(sleeping){
		alarm = new Date();
		alarmMSeconds = alarm.getTime();
		if(alarmMSeconds - startingMSeconds > naptime){ 
			sleeping = false; 
		}
    }
	return false;
}

function set_style(objid,attrib,value){
	document.getElementById(objid).style[attrib] = value;
}


