﻿function ajax(islem, url, id, evals, parameters){
	var request = false
	if (window.XMLHttpRequest)
		request = new XMLHttpRequest()
	else if (window.ActiveXObject){
		try {
		request = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){
			try {
			request = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	}
	else {
		return false
	}
	request.onreadystatechange = function(){
		if (request.readyState == 4 && (request.status==200 || window.location.href.indexOf("http")==-1)) {
			if (evals == 1) {eval(request.responseText)}
			if (id != 0 && id != null) {document.getElementById(id).innerHTML = request.responseText}
		}
	}
	if (islem == "get") {
		request.open("GET", url, true)
		request.send(null)
	}
	else {
		request.open('POST', url, true);
		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		request.setRequestHeader("Content-length", parameters.length);
		request.setRequestHeader("Connection", "close");
		request.send(parameters);
	}
};

function textarea_duzenle() {
	var areas = document.getElementsByTagName("textarea");
	for (i=0;i<areas.length;i++) {
		textheight(areas[i]);
		areas[i].onkeyup = function() {textheight(this);}
	}
}

window.onload = function(){
	textarea_duzenle();
};

function kututop(event) {
	var arac_kutusu = document.getElementById("arac_kutusu")
	arac_kutusu.style.marginTop = (event.clientY-(arac_kutusu.offsetHeight/2))+"px"
}

function textheight(v) {
	var duzenle = document.getElementById("duzenle");
	var textarea_value = v.value.replace(/\n/g, "<p>");
	div = document.createElement("div");
	div.innerHTML = textarea_value;
	duzenle.appendChild(div);
	vheight = (div.offsetHeight<50)?50:div.offsetHeight+14;
	v.style.height = vheight+"px";
	duzenle.removeChild(div);
}

function tnc(value) {
	textarea_number = value;
}

// apply tagOpen/tagClose to selection in textarea,
// use sampleText instead of selection if there is none
var textarea_number = 0;
function insertTags(tagOpen, tagClose, sampleText) {
	var txtarea;
	if (document.editform) {
		txtarea = document.editform.wpTextbox1;
	} else {
		// some alternate form? take the first one we can find
		var areas = document.getElementsByTagName('textarea');
		txtarea = areas[textarea_number];
	}
	var selText, isSample = false;

	if (document.selection  && document.selection.createRange) { // IE/Opera

		//save window scroll position
		if (document.documentElement && document.documentElement.scrollTop)
			var winScroll = document.documentElement.scrollTop
		else if (document.body)
			var winScroll = document.body.scrollTop;
		//get current selection  
		txtarea.focus();
		var range = document.selection.createRange();
		selText = range.text;
		//insert tags
		checkSelectedText();
		range.text = tagOpen + selText + tagClose;
		//mark sample text as selected
		if (isSample && range.moveStart) {
			if (window.opera)
				tagClose = tagClose.replace(/\n/g,'');
			range.moveStart('character', - tagClose.length - selText.length); 
			range.moveEnd('character', - tagClose.length); 
		}
		range.select();   
		//restore window scroll position
		if (document.documentElement && document.documentElement.scrollTop)
			document.documentElement.scrollTop = winScroll
		else if (document.body)
			document.body.scrollTop = winScroll;

	} else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla

		//save textarea scroll position
		var textScroll = txtarea.scrollTop;
		//get current selection
		txtarea.focus();
		var startPos = txtarea.selectionStart;
		var endPos = txtarea.selectionEnd;
		selText = txtarea.value.substring(startPos, endPos);
		//insert tags
		checkSelectedText();
		txtarea.value = txtarea.value.substring(0, startPos)
			+ tagOpen + selText + tagClose
			+ txtarea.value.substring(endPos, txtarea.value.length);
		//set new selection
		if (isSample) {
			txtarea.selectionStart = startPos + tagOpen.length;
			txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
		} else {
			txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
			txtarea.selectionEnd = txtarea.selectionStart;
		}
		//restore textarea scroll position
		txtarea.scrollTop = textScroll;
	} 

	function checkSelectedText(){
		if (!selText) {
			selText = sampleText;
			isSample = true;
		} else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
			selText = selText.substring(0, selText.length - 1);
			tagClose += ' '
		} 
	}

}
function yp(sayfa, ismi, w, h, scroll){
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	if (navigator.appName == "Netscape"){h = h +40}
	ayarlar ='height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=no'
	win = window.open(sayfa, ismi, ayarlar)
	if (parseInt(navigator.appVersion) >= 4) {win.window.focus();};
};
