var ajaxURL = '';
var autoCompleter = null;
var latestMsgDiv = null;
var popListHash = {};

function changeAutoCompleteURL(wField) {
	if (ajaxURL.indexOf('?') > 0) {
		autoCompleter.url = ajaxURL + "&dat2=" + wField.value + "&";
	} else {
		autoCompleter.url = ajaxURL + "?dat2=" + wField.value + "&";
	}
}

function initAutoComplete(fieldName, divName, url) {
	ajaxURL = url;
	autoCompleter = new Ajax.Autocompleter(fieldName, divName, url, {paramName: 'dat'});
}

function loadPage(url) {
	document.location.href = url;
	return false;
}

function initPage() {
	if ((!document.srchfrm.srchkr.checked) && (!document.srchfrm.srchki.checked)) {
		document.srchfrm.srchkr.checked = 1;
		document.srchfrm.srchki.checked = 1;
	}
}

function replaceDiv(targetDiv, sourceDiv, whichLink) {
	document.getElementById(targetDiv).innerHTML = document.getElementById(sourceDiv).innerHTML;
	document.getElementById('link1').className = '';
	document.getElementById('link2').className = '';
	document.getElementById('link3').className = '';
	document.getElementById(whichLink).className = 'current';
	return false;
}

function loadDiv(url, whichDiv, msgDiv) {
	latestMsgDiv = msgDiv;
	document.getElementById(msgDiv).innerHTML = 'YÜKLENİYOR';
	new Ajax.Updater(whichDiv, url, {method: 'get', asynchronous: true, onComplete: updateYukleniyor});
}

function updateYukleniyor(originalRequest) {
	document.getElementById(latestMsgDiv).innerHTML = '';
}

function modifySelectedHedef(selectField) {
	var form = selectField.form;
	if (selectField.value == 3) {
		form.imzalimit.value = 90;
		form.email.value = '';
		form.dilekce.value = '';
		form.email.disabled = true;
		form.imzalimit.disabled = true;
		form.dilekce.disabled = true;
		form.email.style.background = "#F0F0F0";
		form.imzalimit.style.background = "#F0F0F0";
		form.dilekce.style.background = "#F0F0F0";
	} else {
		form.email.disabled = false;
		form.imzalimit.disabled = false;
		form.dilekce.disabled = false;
		form.email.style.background = "#FFFFFF";
		form.imzalimit.style.background = "#FFFFFF";
		form.dilekce.style.background = "#FFFFFF";
		if (form.dilekce.value == '') {
			form.dilekce.value = 'Sayin .............,\n\n Biz, asagida imzasi bulunanlar, [lutfen protesto konusunu bir cumle ile ekleyiniz] protesto ediyor, gerekenin yapilmasini arz ediyoruz.\n\nSaygilarimizla.';
		}
	}
}



//POP SUPPORT

/*
Sample Usage: 

<div id="mypop" class="pkapali">
<p class="popkapat"><a class="rtvpoplink" onclick="togglePop('mypop',this,-1,-1);">Kapat</a></p>
<div class="rtvpop">
  <h4>BASLIK YAZISI</h4>
  <p>BASLIK ALTI</p>
</div>
</div>

<a class="rtvpoplink" onclick="togglePop('mypop',this,10,40);">Test PopUp</a>
*/
var popList = [];

function togglePop(whichDiv, xPlus, yPlus) {
	togglePopRefresh(whichDiv, xPlus, yPlus, 0);
}

function togglePopRefresh(whichDiv, xPlus, yPlus, refreshPage) {
	var divObj      = document.getElementById(whichDiv);
	if (divObj.className.indexOf('pkapali') >= 0) {
		if (popListHash[whichDiv] == null) {
			popListHash[whichDiv] = divObj.innerHTML;
		}
		popList[popList.length] = divObj;
		hideExistingPops();
		movePop(divObj, xPlus, yPlus);
		divObj.className = 'pacik';
		if ((navigator.userAgent.toLowerCase().indexOf('mac') >= 0) && document.all && document.getElementById) {
	  	document.body.style.backgroundColor = "red";
		  document.body.style.backgroundColor = "white";
  	}
	} else {
		closePOP(whichDiv);
		if (popListHash[whichDiv] != null) {
			divObj.innerHTML = popListHash[whichDiv];
		}
	}
	if (refreshPage > 0) {
		window.location.reload(true);
	}
}

function timedPop(timeOut, whichDiv, xPlus, yPlus) {
	togglePop(whichDiv, xPlus, yPlus);
	setTimeout("closePOP('" + whichDiv + "')", timeOut);
}

function closePOP(whichDiv) {
	var divObj = document.getElementById(whichDiv);
	var newArray = [];
	var shiftVal = 0;
	for (var i = 0; i < popList.length; i++) {
		if (whichDiv == popList[i].id) {
			shiftVal = -1;
			continue;
		}
		newArray[i] = popList[i + shiftVal];
	}
	popList = newArray;
	divObj.className = 'pkapali';
	if ((navigator.userAgent.toLowerCase().indexOf('mac') >= 0) && document.all && document.getElementById) {
		document.body.style.backgroundColor = "red";
	  document.body.style.backgroundColor = "white";
	}
}

function movePop(div, xPlus, yPlus) {
	var startX = getXPos(document.getElementById("mntb"));
	var startY = getYPos(document.getElementById("mntb"));
	/*
	if(navigator.appName=="Netscape") {
		startX += 232;
		startY += 10;
	} else {
		startX += 332;
		startY += 180;
	}
	*/

	moveObj(div, startX + xPlus, startY + yPlus);
}

function hideExistingPops() {
	for (var i = 0; i < popList.length; i++) {
		if (popList[i].className.indexOf('pacik') >= 0) {
			popList[i].className = 'pkapali';
		}
	}
}

function getXPos(obj) {
  var x=0;
	if(document.layers) {
		x=obj.pageX;
	} else {
		while (eval(obj)) {
			x+=obj.offsetLeft;
			obj=obj.offsetParent;
		}
	} 
	return x;
}

function getYPos(obj) {
  var y=0;
	if(document.layers) {
		y=obj.pageY;
	} else {
		while (eval(obj)) {
			y+=obj.offsetTop;
			obj=obj.offsetParent;
		}
	} 
	return y;
}

function moveObj(obj, x, y) {
	if (document.layers) {
		obj.left = x;
	} else {
		thisPos = x + "px";
		obj.style.left = thisPos;
	}

	if (document.layers) {
		obj.top = y;
	} else {
		thisPos = y + "px";
		obj.style.top =  thisPos;
	}
}

function submitPopForm(formId, divName, url) {
	if (formId.length > 0) {
		var postData = Form.serialize(formId);
		new Ajax.Updater(divName, url, {method: 'post', parameters: postData, asynchronous: true});
	} else {
		new Ajax.Updater(divName, url, {method: 'get', asynchronous: true});
	}
	setPopBekleDiv(divName, 'Lütfen Bekleyin');
}

function setPopBekleDiv(divName, msgText) {
	divName = divName.substring(0, divName.length - 3);
	var divObj = document.getElementById(divName + 'Bekle');
	divObj.innerHTML = msgText;
	var link = document.getElementById(divName + "_link");
	if (link != null) {
  	link.removeAttribute('href');
  	link.style.color="gray";
	}
}

// _][_
function getMail(addr) {
	addr = addr.substring(2).substring(0, addr.length - 4);
	addr = addr.replace('|', '') + "@" + "protestocu.com";
	document.location.href="mailto:" + addr ;
}

function resetDizinFilter() {
	document.dzform.hedtur.value='5';
	document.dzform.dat.value='';
	document.dzform.submit();
	return 1;
}

function arkadasCagirClick(event) {
	var keyCode = event.keyCode ? event.keyCode : event.which;
	if (keyCode == 13) {
		togglePop('arkgc',-140,400);
		submitPopForm('arkgf', 'arkgc_ic', '/prt/js/arkg');		
    if (event.preventDefault) {
      event.preventDefault();
      event.stopPropagation();
    } else {
      event.returnValue = false;
      event.cancelBubble = true;
    }
		return false;
	}
	return true;
}

function getSikayetLinkLoc(link) {
	var link = document.getElementById(link);
	return getYPos(link) - 150;
}