function xmlHttp()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
   xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
	return(xmlHttp);
}

function setAsHomepageFeature(belongs_to, id){
	var xmlReq = xmlHttp();
	
	xmlReq.onreadystatechange = function(){
		if(xmlReq.readyState == 4){
			alert(xmlReq.responseText);
		}
	}
	
	xmlReq.open('GET', 'do.php?createHomepageFeature&belongs_to=' + belongs_to + '&id=' + id, true);
	xmlReq.send(null);
}

function removeHomepageFeature(feature_id){
	var xmlReq = xmlHttp();
	
	xmlReq.onreadystatechange = function(){
		if(xmlReq.readyState == 4){
			alert(xmlReq.responseText);
		}
	}
	
	xmlReq.open('GET', 'do.php?removeHomepageFeature&feature_id=' + feature_id, true);
	xmlReq.send(null);
}

function putCaptcha(id){
	var captcha = document.getElementById('captcha_content');
	var captcha_target = document.getElementById(id);
	if(captcha_target && captcha){
		captcha.style.display = 'block';
		captcha_target.appendChild(captcha);
	}	
}

function approveComment(id){
	var xmlReq = xmlHttp();
	
	xmlReq.onreadystatechange = function(){
		if(xmlReq.readyState == 4){
			if(xmlReq.responseText == 'Comment Approved'){
				var unapp = document.getElementById('un'+id);
				if(unapp){
					unapp.innerHTML = "";
				}else{
					alert('Comment Approved');
				}
			}else{
				alert(xmlReq.responseText);
			}
		}
	}
	
	xmlReq.open('GET', 'do.php?approveComment&id=' + id, true);
	xmlReq.send(null);
}

function deleteComment2(id){
	var xmlReq = xmlHttp();
	
	xmlReq.onreadystatechange = function(){
		if(xmlReq.readyState == 4){
			if(xmlReq.responseText == 'Comment Deleted'){
				var unapp = document.getElementById('un'+id);
				if(unapp){
					unapp.innerHTML = "";
				}
			}else{
				alert(xmlReq.responseText);
			}
		}
	}
	
	xmlReq.open('GET', 'do.php?deleteComment&id=' + id, true);
	xmlReq.send(null);
}

function toggleVisibility(id){
	/*hide all open post windwos in the blog section*/
	var alldivs = document.getElementsByTagName('div');
	for(i in alldivs){
		var div = alldivs[i];
		if(div.id){
			
			if(div.id.match('postblog') && div.id != id){
				div.style.display = 'none';
			}
		}
	}

	var comments = document.getElementById(id);
	if(comments.style.display == 'block'){
		comments.style.display = 'none';
		//alert(comments.offsetHeight);
	}else{
		comments.style.display = 'block';
		var h = comments.offsetHeight;
		resizeHeight(comments, 0, h);
	}
}

function hideBlock(id){
	var comments = document.getElementById(id);	
		comments.style.display = 'none';/**/	
}

function showBlock(id){	
	var comments = document.getElementById(id);		
		comments.style.display = 'block';		
}
function jump(url){
	document.location = url;
}
function resizeHeight(obj, start, end){
	//var t = setInterval();
}
function deleteTip(id, obj){
	var query = 'delete_sighting&id=' + id;
	xmlReq = xmlHttp();
	xmlReq.onreadystatechange = function(){
		if(xmlReq.readyState == 4){
			obj.parentNode.parentNode.innerHTML = '';
		}
		
	}
	xmlReq.open('GET', 'do.php?'+query, true);
	xmlReq.send(null);	
}
function deleteComment(comment_id, obj, return_to){
	var sure = confirm('Are you sure you want to delete this comment.');
	if(sure){
		obj.parentNode.parentNode.innerHTML = "";
		window.open('admin/do.php?method=delete_comment&comment_id='+comment_id, '_blank');
		//alert(return_to);
		//document.location = 'admin/do.php?method=delete_comment&comment_id='+comment_id+'&return_to='+return_to;
	}
}
function deleteThread(thread_id, obj, return_to){
	var sure = confirm('Are you sure you want to delete this thread.');
	if(sure){
		obj.parentNode.parentNode.innerHTML = "";
		window.open('admin/do.php?delete_thread&id='+thread_id, '_blank');
		document.location = 'forums.php';
	}
}
function showAttachForm(form_id){
	var theForm = document.getElementById(form_id);
	var attachmentWindow = document.getElementById(form_id+'_window');
	attachmentWindow.style.display = 'block';
}
/*
function addToForm(form_id, media_id){
	var theForm = document.getElementById(form_id);
	var ele = document.createElement('input');
	ele.name = 'attached_files[]';
	ele.value = media_id;
	ele.type = 'hidden';
	theForm.appendChild(ele);
	alert(media_id);	
}*/

function clearDefaultText(ele){
	var name = ele.name;
	if(name == 'username'){
		ele.value == 'Username' ? ele.value = '':'';
	}
	if(name == 'password'){
		if(document.all){
			var newPasswordBox = document.createElement('input');
			newPasswordBox.name = 'password';
			newPasswordBox.id = 'password';
			newPasswordBox.type = 'password';
			newPasswordBox.className = 'text';
			ele.parentNode.replaceChild(newPasswordBox, ele);
			setTimeout(function(){document.getElementById('password').focus();},10);	
		}else{
			if(ele.value = 'Password'){
				ele.type = 'password';
				ele.value = '';
			}
		}
	}
}

function returnToDefaultText(ele){
	var name = ele.name;
	if(name == 'username'){
		ele.value == '' ? ele.value = 'Username':'';
	}
	if(name == 'password'){
		if(!document.all){
			if(ele.value == ''){
				ele.type = 'text';
				ele.value = 'Password';
			}
		}
	}
}

/************** FRIEND REQUEST FUNCTIONS ***************/
function closeReqWindow(){
		var reqWindow = document.getElementById('reqWindow');
		document.body.removeChild(reqWindow)
}
function addFriend(id){
	recReq.location = "do.php?request_friend&id="+id;
	//closeReqWindow();
}
function requestAFriend(id, fname, lname, photo){
	var reqWrapper = document.createElement('div');
	reqWrapper.innerHTML += "<a name='friend_req_window'></a><br><br><br><br>";
	reqWrapper.style.position = 'absolute';
	reqWrapper.style.top = '0px';
	reqWrapper.style.width = '100%';
	reqWrapper.id = 'reqWindow';	
	
	var reqWindow = document.createElement('div');
	reqWindow.innerHTML += "<h2 class='section_heading1' style='padding:0;margin:0'>"+fname+" "+lname+"</h2>";
	reqWindow.innerHTML += "<p>You must be freinds with "+fname+" to see the full profile.</p>";
	reqWindow.innerHTML += "<div style='float:left;background:url("+photo+") center 0px no-repeat; width:50px; height:50px; margin-left:20px'></div>";	
	reqWindow.innerHTML += "<div style='float:left;padding:0 0 0 20px'>Name:"+fname+" "+lname+"</div>";
	reqWindow.innerHTML += "<div style='float:left;padding:0 0 0 20px'><a href='JavaScript:addFriend("+id+")'>Add As Friend</a></div><iframe name='recReq' style='display:none'></iframe>";
	reqWindow.innerHTML += "<p class='clear'><a href=\"JavaScript:closeReqWindow()\">Close</a><br><br></p>";
	reqWindow.style.border = '5px solid #666';
	reqWindow.style.padding = '10px';
	reqWindow.style.width = '400px';
	reqWindow.style.margin = '0 auto';
	reqWindow.style.background = '#fff';
	reqWindow.style.left = (document.body.clientWidth / 2) - 200;
	reqWindow.display = 'block';
	
	
	reqWrapper.appendChild(reqWindow);
	document.body.appendChild(reqWrapper);
	document.location = '#friend_req_window';
	return false;
}

function confirmDelete(msg, url){
	var msgBox = confirm(msg);
	if(msgBox){
		document.location = url;
	}
}


/******************** TEXT FORMATTING ************************/
var selectionElement;
var selRange;

function insertBBCode(type){
	var params ='';
	if(document.selection){		
		if(selectionElement){
			if(type == 'a'){
				var hyperLink = prompt('Enter HyperLink:', 'http://');
				if(hyperLink){
					params = " href='"+hyperLink+"' target='_blank'";
				}else{
					return false;
				}
			}
			if(type == 'f'){
				type = 'a';
				params = " href='do.php?get_file&id="+ media_id_of_file_to_insert + "'";
			}
			if(type == 'img'){
				params = " src='" + imgSrc + "' class='" + imgStyles + "'";
			}
			if(document.selection.type == 'Text'){
				var sel = document.selection.createRange();
				//alert(sel);
				switch(type){
				case 'img':
					sel.text = sel.text +  '<'+type+params+'>';
					break;
				default:
					sel.text = '<'+type+params+'>' + sel.text +  '</'+type+'>';
				}		
				selectionElement = undefined;				
			}
		}
	}else{
		if(selectionElement){
			if(type == 'a'){
				var hyperLink = prompt('Enter HyperLink:', 'http://');
				if(hyperLink){
					params = " href='"+hyperLink+"' target='_blank'";
				}else{
					return false;
				}
			}
			if(type == 'f'){
				type = 'a';
				params = " href='do.php?get_file&id="+ media_id_of_file_to_insert + "'";
			}
			if(type == 'img'){
				params = " src='" + imgSrc + "' class='" + imgStyles + "'";
			}
			var all = selectionElement.value.split("");
			var selected = all.slice(selRange[0], selRange[0] + selRange[1]);
			switch(type){
				case 'img':
					var newStr = selected.join('') +  '<'+type+params+'>';
					break;
				default:
					var newStr = '<'+type+params+'>' + selected.join('') +  '</'+type+'>';
			}
			//var newStr = '<'+type+params+'>' + selected.join('') +  '</'+type+'>';
			all.splice(selRange[0], selRange[1], newStr);
			selectionElement.value = all.join('');
			selectionElement.focus();
			selectionElement.setSelectionRange(selRange[0], 0);
			//alert(selRange[0]);
			selectionElement = undefined;			
		}
	}
}

function outputPreview(){
	/*var preview = document.getElementById('preview_div');
	if(selectionElement){
		var str = selectionElement.value;
		str = str.replace(/\[([^\]]*)\]/g, "<$1>");
		preview.innerHTML = str;
	}*/
}

function removeFormatting(){
	//var preview = document.getElementById('preview_div');
	if(selectionElement){
		var str = selectionElement.value;
		str = str.replace(/\[([^\]]*)\]/g, '');
		selectionElement.value = str;
		//preview.innerHTML = str;
	}
}

function setSelection(e){
	if(!e) e = window.event;
	if (e.stopPropagation) {
	e.preventDefault();
	e.stopPropagation();
	} else {
	e.cancelBubble = true;
	}
	(document.all) ? txtBox = e.srcElement : txtBox = e.target;
	selectionElement = txtBox;
		if(window.getSelection){
		var start = txtBox.selectionStart;		
		var end = txtBox.selectionEnd;
		selRange = new Array(start, (end-start));		
	}
	if(e.keyCode == '73' && e.ctrlKey == true){
		insertBBCode('i');
		return false;	
	}
	if(e.keyCode == '66' && e.ctrlKey == true){
		insertBBCode('b');
		return false;	
	}
	if(e.keyCode == '85' && e.ctrlKey == true){
		insertBBCode('u');
		return false;	
	}
	if(e.keyCode == '72' && e.ctrlKey == true){
		insertBBCode('a');
		return false;	
	}
	 
	
	
}

/******************** END TEXT FORMATTING ************************/


/***********************  INSERT IMAGES *************************/

var theImage = '';
var resizeContainer = '';
var txtWidth = '';
var txtHeight = '';
var mediaId = '';
var imgSrc = '';
var imgStyles = 'embed_left';
var image_width = 0;
var image_height = 0;
var ratio = 0;
var media_id = 0;
var status_window;
var lockar = '';
var g_group_id = '';
var hasBeenWarned = false;
var formatting_options = false;
//document.onmousedown = startDrag;
//document.onmouseup = stopDrag;
document.onmousemove = getMouseXY;

var activeEle = '';

var isDragging = false;

var mouseXY = new Array();
var mouseOffsetXY = new Array();
var mouseStartXY = new Array();

function setImgStyle(style){
	imgStyles = style;
}

function startDrag(e){
	isDragging = true;
	if(!e) e = window.event;
	if(e.target){
		activeEle = e.target;
		mouseStartXY[0] = e.clientX;
		mouseStartXY[1] = e.clientY;		
	}else{
		activeEle = e.srcElement;
		mouseStartXY[0] = e.x;
		mouseStartXY[1] = e.y;
	}
}

function stopDrag(){
	if(theImage){
		isDragging = false;
		image_width = theImage.style.width.replace('px', '') *1;
		image_height = theImage.style.height.replace('px', '') *1;
	}
}

function position(e){
	
}

function getMouseXY(e){
	if(!e) e = window.event;
	if(!document.all){
		mouseXY[0] = e.clientX;
		mouseXY[1] = e.clientY;
	}else{
		mouseXY[0] = e.x;
		mouseXY[1] = e.y;
	}
	if(formatting_options == false) return false;
	var max_width = document.getElementById('max_width').value;
	var max_width_wrapper = document.getElementById('max_width_wrapper');
	if(txtWidth.value*1 > max_width*1){
		max_width_wrapper.style.color = 'red';		
		if(hasBeenWarned == false){
			//hasBeenWarned = true;
			//alert('The maximum recommended width is '+max_width+'.');
		}	
	}
	else
		max_width_wrapper.style.color = 'black';
	if(isDragging == true){
		if(activeEle.id == 'image_resize_wrapper'){
			if(lockar.checked != true){					
				var newW = (image_width + (mouseXY[0] - mouseStartXY[0]));
				if(newW > 425) newW = 425;
				var newH = (image_height + (mouseXY[1] - mouseStartXY[1]));
				if(newW <=1) newW = 1;
				if(newH <=1) newH = 1;
				theImage.style.width =  newW + 'px';
				theImage.style.height =  newH + 'px';
				txtWidth.value = theImage.style.width.replace('px', '')*1;
				txtHeight.value = theImage.style.height.replace('px', '')*1;
			}else{
				var newW = (image_width + (mouseXY[0] - mouseStartXY[0]));
				if(newW > 425) newW = 425;
				var newH = Math.round(txtWidth.value.replace('px', '')*ratio);
				if(newW <=1) newW = 1;
				if(newH <=1) newH = 1;				
				txtWidth.value = newW;
				txtHeight.value = newH;
				theImage.style.width = txtWidth.value  + 'px';;
				theImage.style.height = txtHeight.value  + 'px';;					
			}
		}
		return false;
	}
	
}

function setImageSize(){
	theImage.style.width = txtWidth.value + 'px';
	theImage.style.height = txtHeight.value + 'px';
	image_width = txtWidth.value *1;
	image_height = txtHeight.value *1;
}

function getOffsetXY(e){
	return false;
}

function useThisImage(group_id, m_id, w, h){
	if(w > 425){
		new_w = 425;
		h = Math.ceil((h/w) * 425);
		w = new_w;		
	}
	theImage = document.getElementById('resize_img'+group_id);
	g_group_id = group_id;
	resizeContainer = document.getElementById('image_resize'+group_id);
	txtWidth = document.getElementById('image_width'+group_id);
	txtHeight = document.getElementById('image_height'+group_id);
	mediaId = document.getElementById('media_id'+group_id);
	status_window = document.getElementById('status_window'+group_id);
	lockar = document.getElementById('lockar'+group_id);
	
	theImage.src = '../do.php?get_file&id=' + m_id;
	theImage.style.width = w + 'px';
	theImage.style.height = h + 'px';
	image_width = w;
	image_height = h;
	txtHeight.value = h;
	txtWidth.value = w;
	mediaId.value = m_id;
	media_id = m_id;
	ratio = image_height/image_width;
	document.getElementById('image_options_wrapper'+group_id).style.display = 'block';
}

function reSelectMe(group_id){
	g_group_id = group_id;
	theImage = document.getElementById('resize_img'+group_id);
	resizeContainer = document.getElementById('image_resize'+group_id);
	txtWidth = document.getElementById('image_width'+group_id);
	txtHeight = document.getElementById('image_height'+group_id);
	mediaId = document.getElementById('media_id'+group_id);
	status_window = document.getElementById('status_window'+group_id);
	lockar = document.getElementById('lockar'+group_id);
}

function insertImage(){
	//alert(status_window);
	//alert('image_resize.php?method=Insert Image&media_id='+media_id+'&image_width='+txtWidth.value+'&image_height='+txtHeight.value);
	status_window.src = 'image_resize.php?method=Insert Image&group_id='+g_group_id+'&media_id='+media_id+'&image_width='+txtWidth.value+'&image_height='+txtHeight.value;
}



/***********************  END INSERT IMAGES *************************/


