function get_document_window_height() {
	if(document.body && document.body.clientHeight &&
			(document.body.clientHeight > 1)) return document.body.clientHeight;
	else if (window && window.innerHeight) return window.innerHeight;
	else if (document.documentElement && document.documentElement.offsetHeight)
		return document.documentElement.offsetHeight;
	else if (document.body && document.body.offsetHeight)
		return document.body.offsetHeight;
	return 0;
}

function get_document_window_width() {
	if (document.body && document.body.clientWidth)
		return document.body.clientWidth;
	else if (window && window.innerWidth) return window.innerWidth;
	else if (document.documentElement && document.documentElement.offsetWidth)
		return document.documentElement.offsetWidth;
	else if (document.body && document.body.offsetWidth)
		return document.body.offsetWidth;
	return 0;
}

function convert_width(width,window_width) {
   if (width == null) width = page_width + 52;
   else if (typeof(width) == 'string') {
      var percent_pos = width.indexOf('%');
      if (percent_pos != -1) width = window_width * (width.substr(0,percent_pos) / 100);
   }
	
   if (! document.all) {
	   if (width > (window_width - 25)) width = window_width - 25;
   }
   else if (width > (window_width - 10)) width = window_width - 10;
   
   return width;
}

function convert_height(height,window_height) {
	if (height == null) height = window_height;
	else if (typeof(height) == 'string') {
		var percent_pos = height.indexOf('%');
	    if (percent_pos != -1) height = window_height * (height.substr(0,percent_pos) / 100);
	}
	
   if (height > (window_height - 40)) height = window_height - 40;

   return height;
}

function calculate_left(width,window_width) {
   var left = (window_width - width) / 2;

   if (! document.all) left += 10;
   if (left < 0) left = 0;
	
   return left;
}

function calculate_top(height,window_height) {
	var top = ((window_height - height) / 2) - 3;
	
	if (top < 0) top = 0;
	
	return top;
}

function select_date(field_name)
{    
    var date_popup = calender_popup[field_name];
    var date_string_field = $(field_name + '_string');
    var date_value = date_string_field.value;
    if (date_value.length != 8) {
       var date_info = date_value.split("/");
       if (date_info.length == 3) {
          if (date_info[0].length == 1) date_info[0] = '0' + date_info[0];
          if (date_info[1].length == 1) date_info[1] = '0' + date_info[1];
          date_string_field.value = date_info[0] + '/' + date_info[1] +
                                    '/' + date_info[2];
       }
    }
    date_popup.select(date_string_field,field_name + '_icon','MM/dd/yy');
    //positon_calendar($(field_name+"_icon"));
}

function parse_date_field(date_string_field,field_name)
{
    var date_value = date_string_field.value;
    var date_field = $(field_name);
    if (date_value == '') {
       date_field.value = '';   return;
    }
    var date_info = date_value.split("/");
    if (date_info.length != 3) {
       date_field.value = '';   return;
    }
    var month = parse_int(date_info[0]) - 1;
    var day = parse_int(date_info[1]);
    var year = parse_int(date_info[2]);
    if (year < 70) year += 2000;
    else year += 1900;
    var date_obj = new Date(year,month,day,6,0,0);
    if (isNaN(date_obj)) {
       date_field.value = '';   return;
    }
    date_field.value = date_obj.getTime() / 1000;
    
//    if(range_display) update_range_display(date_field);
}

function hide_popups(){
  for(var i = 0; i<ppcnt; i++){
    if($('pop'+i)){
	  $('pop'+i).hide();
	}
  }
}

function parse_int(num)
{
   if (typeof(num) == "undefined") return 0;
   if (isNaN(num)) return 0;
   var int_num = parseInt(num,10);
   if (isNaN(int_num)) return 0;
   return int_num;
}

function process_selected_date(y,m,d)
{
    if (window.CP_targetInput != null) {
       var dt = new Date(y,m - 1,d,0,0,0);
       if (window.CP_calendarObject != null) {
          window.CP_calendarObject.copyMonthNamesToWindow();
       }
       window.CP_targetInput.value = formatDate(dt,window.CP_dateFormat);
    }
    var field_name = window.CP_targetInput.name;
    field_name = field_name.substring(0,field_name.length - 7);
    parse_date_field(window.CP_targetInput,field_name);
}

function format_date_value(date_value)
{
    if (date_value) var date_obj = new Date(date_value * 1000);
    else var date_obj = new Date();
    var month_value = date_obj.getMonth() + 1;
    if (month_value < 10) month_value = '0' + month_value;
    var day_value = date_obj.getDate();
    if (day_value < 10) day_value = '0' + day_value;
    var date_string = month_value + '/' + day_value + '/' +
                      date_obj.getFullYear();
    return date_string;
}

var month_names = ["January","February","March","April","May","June","July",
                   "August","September","October","November","December"];

function format_date_time_value(date_value)
{
    if (date_value) var date_obj = new Date(date_value * 1000);
    else var date_obj = new Date();
    var minutes = date_obj.getMinutes();
    if (minutes < 10) minutes = "0" + minutes;
    var hours = date_obj.getHours();
    var ampm = "am";
    if (hours == 0) hours = 12;
    if (hours > 11) ampm = "pm";
    if (hours > 12) hours -= 12;
    var date_string = month_names[date_obj.getMonth()] + " " + date_obj.getDate() + ", " +
                      date_obj.getFullYear() + " " + hours + ":" + minutes + " " + ampm;
    return date_string;
}

function get_iframe(id)
{
	var iframe = document.frames ? document.frames[id] : document.getElementById(id);
	var ifWin = iframe.contentWindow || iframe;
	return ifWin;
	
} 

function get_dialog_document(id)  
{  
   var dialog = get_iframe(id);
   var doc_object = (dialog.contentWindow || dialog.contentDocument || dialog);
   if (doc_object.document) doc_object = doc_object.document;
   return doc_object; 
}

function get_dialog_document_height_utility(dialog_doc){
   if (dialog_doc.body && dialog_doc.body.scrollHeight &&
       (dialog_doc.body.scrollHeight > 1)) var document_height = dialog_doc.body.scrollHeight;
   else if (dialog_doc.documentElement && dialog_doc.documentElement.offsetHeight)
      var document_height = dialog_doc.documentElement.offsetHeight;
   else if (dialog_doc.body && dialog_doc.body.offsetHeight)
      var document_height = dialog_doc.body.offsetHeight;
   else var document_height = 0;
   return document_height;
}

function resize_dialog_utility(dialog,width,height)
{
   if (! dialog) {
      alert('Error: Dialog not found for resize_dialog');   return;
   }

   var window_width = get_document_window_width();
   width = convert_width(width,window_width);
   var window_height = get_document_window_height();
   height = convert_height(height,window_height);
   var left = calculate_left(width,window_width);
   var top = calculate_top(height,window_height);
   if (top >= 15) top -= 15;

   //dhtmlwindow.moveTo(dialog,left,top);
   //dhtmlwindow.setSize(dialog,width,height);
   dialog.style.height = height+'px';
   dialog.style.width = width+'px';
}

function grow_dialog_utility(dialog,doc){
   //var document_height = get_dialog_document_height(dialog.doc,dialog.win) + 20;
   var document_height = get_dialog_document_height_utility(doc);
   var window_height = get_document_window_height() - 40;
   var dialog_height = dialog.style.height.replace('px','')*1;
   if ((document_height > dialog_height) && (dialog_height < window_height)) {
      dialog_height = document_height;
      if (dialog_height > window_height) dialog_height = window_height;
      if (dialog_height > dialog.height)
         resize_dialog_utility(dialog,dialog.style.width.replace('px','')*1,dialog_height);
   }
}

function print_iframe(id)
{
	var ifWin = get_iframe(id);
	ifWin.focus()
	ifWin.print();
	
	return false;
} 


var debug_container = new Element('div', {id:'debug_cont',style:"width:700px;color:#666;background:#fff;overflow:auto;height:200px;"});

var debug_table = new Element('table', {cellpadding:2,cellspacing:1,style:'width:100%;'});
var debug_tbody = new Element('tbody');
var debug_tr    = new Element('tr', {id:'debug_header'});
var function_td = new Element('th',{style:"width:150px;line-height:19px;color:#fff;background:#666;",valign:'top'});
var message_td  = new Element('th',{style:"line-height:19px;color:#fff;background:#666;",valign:'top'});


function show_debug() {	
    		
	function_td.insert("FUNCTION");
	message_td.insert("MESSAGE");

	debug_tr.insert(this.function_td); 
	debug_tr.insert(this.message_td); 
	debug_tbody.insert(this.debug_tr); 
	debug_table.insert(this.debug_tbody); 	
	debug_container.insert(this.debug_table);
	
	dbody.insert(this.debug_container); 
	debug_container.absolutize();
    divDialogs.create_dialog('draggable', 'debug_cont', 'debug_header', null);
}

function add_debug(funct,mess) {
	if(!$('debug_cont'))
		show_debug();
	
	temp_debug_tr    = new Element('tr');
	temp_function_td = new Element('td',{style:"width:100px;line-height:19px;color:#000;background:#ccc;",valign:'top'});
	temp_message_td  = new Element('td',{style:"line-height:19px;color:#000;background:#ccc;",valign:'top'});	
    		
	temp_function_td.insert(funct);
	temp_message_td.innerHTML = mess;

	temp_debug_tr.insert(temp_function_td); 
	temp_debug_tr.insert(temp_message_td); 
	debug_tbody.insert(temp_debug_tr); 
}

var ppcnt = 0;

var ppl_opts = {
		draggable:   true,
		static_top:  false,
		add_overlay: false,
		overlay:     null,
		alt_cont:    false,
		hide_only:   true,
		anchored:    false,
		id:          null,
		corners:      {
				top_left:false,
				bottom_left:false,
				top_right:false,
				bottom_right:false
		},
		padding: false,
		
		element: {
			obj: null,
			handle: null,
			data: null
		}
}
var active_popup_urls = new Array();
function load_div_diags() {
	var popup_links = $$("a.pop_link");
	popup_links.each(function(pl) {
	    var myhref = pl.href+"";
	    if(active_popup_urls.indexOf(pl.href)<0){
		  ppl_opts.id = "pop"+ppcnt;
		  var link_pop = new DivDialogs(ppl_opts);
		  var dimen = pl.type;
		
		  dimen = dimen.split("|");
		  dimen[0] = parseInt(dimen[0]);
		  dimen[1] = parseInt(dimen[1]);
		
		  if(pl.title != "") title = pl.title;
		  else title = "&nbsp;";
		
		  link_pop.create(title,{"width":dimen[0],"height":dimen[1]});
		  link_pop.static_top = "50";
		  link_pop.element.data.style.background = "#ffffff";
		  link_pop.add_overlay = true;
		  link_pop.init();
		  //pl.href = encodeURIComponent(pl.href);
		  if(pl.hasClassName('catalog')){
		    var hrefar = pl.href.split('');
		    hrefar[hrefar.length-1]="&popup=true/";
		    pl.href = hrefar.join('');
		  }else if(pl.href.search('~(\.pdf|\.gif|\.jpg|\.png|\.txt)')){
		    //don't change the href
		  }else{
		    pl.href+="?popup=on";
		  }
		  var myonloadfunc = 'var inbody'+ppcnt+' = get_dialog_document(this.id).body; this.style.height = ($(inbody'+ppcnt+').getHeight()-80) + "px"';
		  var myonloadfunc = 'var doc = get_dialog_document(this.id); grow_dialog_utility(this,doc)';
		  var myonloadfunc = '';
		  if(dimen[1] != 0)myonloadfunc = '';
		    var clframe  = new Element('iframe', 
		               {id:'clframe'+ppcnt,name:'clframe'+ppcnt,style:'width: '+dimen[0]+'px; height: '+dimen[1]+'px;',frameborder:"0",scrolling:"auto",marginheight:"10",marginwidth:"10",src:pl.href,
					    onload:myonloadfunc
					   });
		  link_pop.element.data.update(clframe);
				
		  pl.onclick = function() {
			$(link_pop.id).show();
			$('site_overlay').show();
			var iframeid = 'clframe'+link_pop.id.replace('pop','');
			var doc = get_dialog_document(iframeid);
			grow_dialog_utility(document.getElementById(iframeid),doc);
			return false;
		  }
		  active_popup_urls.push(myhref);
		  ppcnt++;
		}
		//link_pop.show_element();
	})
}

function scEffects() {
	this.options = {
			duration: .5
	}
}

scEffects.prototype.slide_left_in = function(element) {
	new Effect.SlideRightIn(element, this.options); 
}

scEffects.prototype.slide_left_out = function(element) {
	 new Effect.SlideLeftOut(element, this.options); 
}

scEffects.prototype.raise_blind = function(element) {
	new Effect.BlindUp(element, this.options); 
}
		 
scEffects.prototype.lower_blind = function(element) {
	new Effect.BlindDown(element, this.options); 
}
		 
scEffects.prototype.make_fade = function(element) {
	new Effect.Fade(element, this.options); 
}
		 
scEffects.prototype.slide_down = function(element) {
	new Effect.SlideDown(element,this.options); 
}
		 
scEffects.prototype.slide_up = function(element) {
	new Effect.SlideUp(element,this.options); 
}
		 
scEffects.prototype.make_appear = function(element) {
    new Effect.Appear(element,this.options); 
}
		   
scEffects.prototype.toggle_appear = function(element) {			     
	if(element.visible()) new Effect.Fade(element, this.options); 
	else new Effect.Appear(element, this.options); 
}
function create_show_div_dialogs(opts,headerTxt,size,content,obj,eventoffset){
	if(!obj)var obj = new DivDialogs(opts);
	if(!$(obj.id)){
		obj.create(headerTxt,size);
		obj.init();
		obj.element.data.update(content);
	}
	if(!eventoffset){		
		obj.show_element();
	}else{
		obj.show_offset(eventoffset);
	}
	return obj;
}
function add_onload_function(funct){
    if (typeof(window.addEventListener) != "undefined")
       window.addEventListener("load",funct,false);
    else if (typeof(document.addEventListener) != "undefined")
       document.addEventListener("load",funct,false);
    else if (typeof(window.attachEvent) != "undefined")
       window.attachEvent("onload",funct);
    else {
       var oldfunct = window.onload;
       if (typeof(window.onload) != "function") window.onload = funct;
       else window.onload = function() { oldfunct(); funct(); }
    }
}
function add_video_full(targetid,width,height,swf){
	if($(targetid))
	{				
		$(targetid).update(AC_FL_PreloadContent( 
				                          'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
				                          'width',width,
				                          'height',height,
				                          'movie',swf,
				                          'quality','high',
				                          'allowfullscreen','true',
				                          'wmode','transparent' ));
	}
}
function offset(obj){  
	var divX = obj.offsetLeft;  
	var divY = obj.offsetTop;  
	var parent = obj.offsetParent;  
	while (parent) {  
	  if (parent.offsetLeft) divX += parent.offsetLeft;  
	  if (parent.offsetTop) divY += parent.offsetTop;  
	  parent = parent.offsetParent;  
	}
	return {x:divX,y:divY};
}

//add an event listener
function addEvent(element,eventType,functionName,uC){
	if(element.addEventListener){
		element.addEventListener(eventType,functionName,uC);
		return true;
	}else if(element.attachEvent){
		return element.attachEvent('on'+eventType,functionName);
	}else{
		element['on'+eventType]=functionName;
		return false;
	}
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

