
// function ToggleNotesPopup()
// div_id - the id of the notespopup div to toggle
// div_type - the type of popup ('hover' or 'normal'). if 'normal', make sure only one div is open at a time
function ToggleNotesPopup(div_id, div_type, close_others){
	//document.getElementById('div_notespopup_'+div_id).style.left = document.getElementById('span_notespopup_'+div_id).style.left;
	if(div_type == null){
		div_type = "hover";
	}
	if(close_others == null){
		if(div_type == "normal"){
			close_others = true;
		}
		else{
			close_others = false;
		}
	}
	if(document.getElementById('div_notespopup_'+div_id).style.visibility == 'visible'){
		if(div_type == "normal"){
			document.last_div_normal = null;
		}
		document.getElementById('div_notespopup_'+div_id).style.visibility = 'hidden';
		document.getElementById('div_notespopup_'+div_id).style.display = 'none';
	}
	else{
		if(close_others == true){
			if(document.last_div_normal != null){
				document.getElementById('div_notespopup_'+document.last_div_normal).style.visibility = 'hidden';
				document.getElementById('div_notespopup_'+document.last_div_normal).style.display = 'none';
			}
			document.last_div_normal = div_id;
		}
		document.getElementById('div_notespopup_'+div_id).style.visibility = 'visible';
		document.getElementById('div_notespopup_'+div_id).style.display = 'inline';
		
		// calculate top and left
		var new_top = 0;
		var new_left = 0;
		var element = document.getElementById('span_notespopup_'+div_id);
		while(element.offsetParent != null){
			if(element.id != null && element.id.substring(0, 15) == "div_notespopup_"){break;}
			new_top += element.offsetTop;
			new_left += element.offsetLeft;
			element = element.offsetParent;
		}
		
		document.getElementById('div_notespopup_'+div_id).style.top = new_top+"px";
		document.getElementById('div_notespopup_'+div_id).style.left = new_left+"px";
	}
}

// Select all text in textarea of text box
function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}

// AJAX
ajaxObject = function() {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e){
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e){
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){
try { return new XMLHttpRequest() } catch(e){
throw new Error( "This browser does not support XMLHttpRequest." );
}}}}}}

function ajax_request(url, div_processing){
	if(div_processing != null && div_processing != ""){
		if(document.getElementById(div_processing) != null){
			document.getElementById(div_processing).style.display = "inline";
		}
	}
	
	var response = ajax_request2(url, "GET", "", "", "", "", "");
	
	if(div_processing != null && div_processing != ""){
		if(document.getElementById(div_processing) != null){
			document.getElementById(div_processing).style.display = "none";
		}
	}
	
	return response;
}

function ajax_request2(url, method, params, async, last_modified, optional_headers){
	if(method != "POST"){
		method = "GET";
		params = null;
	}
	if(async == null || async != true){async = false;}
	if(last_modified == ""){last_modified = null;}
	//if(optional_headers == ""){optional_headers = null;}
	
	var request = new ajaxObject();
	request.open(method, url, async);
	
	if(last_modified != null){	
		request.setRequestHeader("If-Modified-Since", last_modified);
	}
	
	if(optional_headers != null && optional_headers != ""){
		for(var i in optional_headers){
			// request.setRequestHeader(i, optional_headers[i]);
		}
	}
	
	if(method == "POST"){
		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		request.setRequestHeader("Content-length", params.length);
		request.setRequestHeader("Connection", "close");	
	}
	
	request.send(params);
	
	if(!request.getResponseHeader("Date") && last_modified == null){
		var cached = request;
		last_modified = cached.getResponseHeader("Last-Modified");
		last_modified = (last_modified) ? last_modified : new Date(0); // January 1, 1970
		request = ajax_request2(url, method, params, async, last_modified);
		if(request.status == 304){
			request = cached;
		}
	}
	
	var response = request.responseText;
	
	return response;
}

function ajax(name, action, variables){
	var div_processing = '';
	var div_response = '';
	if(name != null && name != ""){
		if(document.getElementById('div_processing_'+name) != null){
			div_processing = 'div_processing_'+name;
		}
		if(document.getElementById('div_response_'+name) != null){
			div_response = 'div_response_'+name;
		}
	}
	if(div_response != ""){
		document.getElementById(div_response).innerHTML = "";
	}
	var url = "?ajax="; // +action
	if(action.substring(0,1) == "?"){
		url += action.substring(1,action.length);
	} else {
		url = "/ajax.php"+url+action;
	}
	if(!!variables && variables.length > 0){
	  for(var i=0; i<variables.length; i++){
	    if(i%2 == 0){url += "&"+variables[i]+"=";}
		else{url += variables[i];}
	  }
	}
	var response = ajax_request(url, div_processing);
	if(div_response != ""){
		document.getElementById(div_response).innerHTML = response;
	}
	return response;
}

// track_url
var tracked_urls = new Array();
function track_url(url, once){
	var track = true;
	if(once != null && once == true){
		if(tracked_urls[url] != null){
			track = false;
		}
		else{
			 tracked_urls[url] = true;
		}
	}
	if(track == true){
		ajax_request('script_tracking.php?url='+url)
	}
}


function select_radio(radio, option_value){
	if(radio == null){return;}
	var radio_length = radio.length;
	if(radio_length == undefined){
		radio.checked = (radio.value == option_value);
		return;
	}
	for(var i=0; i<radio_length; i++){
		if(radio[i].value == option_value){
			radio[i].checked = true;
		}
		else{
			radio[i].checked = false;
		}
	}
}

function select_option(select, option_value){
	if(select == null){return;}
	var options_length = select.options.length;
	for(var i=0; i<options_length; i++){
		if(select.options[i].value == option_value){
			//select.options[i].selected = true;
			select.selectedIndex = i;
		}
		else{
			//select.options[i].selected = false;
		}
	}
}

function toggle_checkbox(checkbox){
	if(checkbox == null){return;}
	checkbox.checked = !checkbox.checked;
}

function popUpVideo(URL, width, height){
	day = new Date();
	id = day.getTime();
	if(width == null || width == undefined || width == "" || width == 0){width = 685;}
	if(height == null || height == undefined || height == "" || height == 0){height = 480;}
	eval("page" + id + " = window.open(URL, id, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width="+width+",height="+height+"');"); 
}


function GetElementPosition(element){
	var position = new Array(0, 0);
	if(element == null || element == undefined){
		return position;
	}
	while(element != null){
		// Check for style position
		position[0] += element.offsetLeft;
		position[1] += element.offsetTop;
		if(element.offsetParent != null && element.offsetParent != undefined){
			element = element.offsetParent;
		}
		else{
			break;
		}
	}
	return position;
}


function is_int(input, strict){
	result = ( !isNaN(input) && parseInt(input)==input );
	if(strict==true && typeof(input)!='number'){ result = false; }
	return result;
}
