// ANA FONKSİYONLAR

function setHomePageMain() {
	document.body.style.behavior='url(#default#homepage)';
	document.body.setHomePage(window.location.href);
}

function addToFavorites(){
	if (document.all) {
		window.external.AddFavorite(location.href, document.title);
	} else if (window.sidebar) {
		window.sidebar.addPanel(document.title, location.href, "");
	}
}

function showAdviceFriend() {
	var adviceContent = document.getElementById("adviceFriendMain");
	
	if (adviceContent.style.display == "block") {
		adviceContent.style.display = "none";
	} else {
		adviceContent.style.display = "block";
	}
}

function doSearch() {
	var searchString = document.getElementById("searchMainStr").value;
	if (searchString != "") {
		if(searchString.length > 2) {
			document.searchForm.action = 'pages.php?page=search&section_id=99';
			document.searchForm.submit();
		} else {
			//alert("Aradığınız kelime en az 3 karakter olmalıdır...");
		}
	}
}

function fixDivHeights (d1,d2) {
	var option = [d1,d2];
	var nh = document.getElementById(d2).offsetHeight;
	for(var i=0; i<option.length; i++) {
		document.getElementById(option[i]).style.height = nh + "px";
	}
}

var orjinalYorumContentDurum = true;
var orjinalYorumContent = "";

function yorumYaz () {
	document.getElementById("yorumYazMain").style.display = "block";
	document.getElementById("yorumlarMain").style.display = "none";
	document.getElementById("yorumYazUyari").style.display = "none";
	document.getElementById("yorumYazSonuc").style.display = "none";
	var yorumEditor = FCKeditorAPI.GetInstance('yorumContent');
	yorumEditor.SetData("<span></span>");
}

function yorumBack () {
	document.getElementById("yorumYazMain").style.display = "none";
	document.getElementById("yorumlarMain").style.display = "block";
	document.getElementById("yorumYazUyari").style.display = "none";
	document.getElementById("yorumYazSonuc").style.display = "none";
	var yorumEditor = FCKeditorAPI.GetInstance('yorumContent');
	yorumEditor.SetData("<span></span>");
}

function yorumGonder (userId, veriId) {
	var yorumEditor = FCKeditorAPI.GetInstance('yorumContent');
	var yorumEditorValue = html_entity_decode(yorumEditor.GetXHTML());
	
	if(yorumEditorValue == "" || yorumEditorValue == null || yorumEditorValue == "&nbsp;" || yorumEditorValue == "<br />") {
		document.getElementById("yorumYazUyari").style.display = "block";
	} else {
		document.getElementById("yorumYazUyari").style.display = "none";
		
		noCache = Math.random();
		var tumVeriYorum = "userId=" + userId + "&veriId=" + veriId + "&veriContent=" + yorumEditorValue + "&nocache=" + noCache;
		
		var tarayiciYorumGonder = navigator.appName;
		if (tarayiciYorumGonder == "Microsoft Internet Explorer") {
			httpYorumGonder = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			httpYorumGonder = new XMLHttpRequest();
		}
		
		httpYorumGonder.open('post', 'do_send_yorum.php', true);
		httpYorumGonder.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpYorumGonder.send(tumVeriYorum);
		httpYorumGonder.onreadystatechange = function () {
			if (httpYorumGonder.readyState == 4) {
				//alert(httpYorumGonder.responseText);
				if (httpYorumGonder.responseText == "Tamam") {
					document.getElementById("yorumYazSonucText").innerHTML = "Yorumunuz başarılı bir şekilde eklendi. Editör onayından sonra yayınlanacaktır...";
					document.getElementById("yorumYazSonuc").style.display = "block";
					document.getElementById("yorumYazMain").style.display = "none";
					document.getElementById("yorumlarMain").style.display = "none";
				} else {
					if (httpYorumGonder.responseText == "Hata_1") {
						document.getElementById("yorumYazSonucText").innerHTML = "Yorumunuzun eklenmesi sırasında hata oluştu. Lütfen Tekrar deneyiniz...";
						document.getElementById("yorumYazSonuc").style.display = "block";
						document.getElementById("yorumYazMain").style.display = "none";
						document.getElementById("yorumlarMain").style.display = "none";
					} else if (httpYorumGonder.responseText == "Hata_0") {
						document.getElementById("yorumYazSonucText").innerHTML = "Lütfen bilgilerinizi kontrol ediniz...";
						document.getElementById("yorumYazSonuc").style.display = "block";
						document.getElementById("yorumYazMain").style.display = "none";
						document.getElementById("yorumlarMain").style.display = "none";
					}
				}
			}
		}
	}
}

function tumYorumlar (pageNo, veriId) {
	noCache = Math.random();
	var tumVeriYorumlar = "yorumPage=" + pageNo + "&newsId=" + veriId + "&nocache=" + noCache;
	
	var tarayiciYorumlar = navigator.appName;
	if (tarayiciYorumlar == "Microsoft Internet Explorer") {
		httpYorumlar = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		httpYorumlar = new XMLHttpRequest();
	}
	
	httpYorumlar.open('post', 'tum_yorumlar.php', true);
	httpYorumlar.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpYorumlar.send(tumVeriYorumlar);
	httpYorumlar.onreadystatechange = function () {
		if (httpYorumlar.readyState == 4) {
			//alert(httpYorumlar.responseText);
			document.getElementById("yorumlarMain").innerHTML = httpYorumlar.responseText;
			document.getElementById("yorumYazMain").style.display = "none";
			document.getElementById("yorumlarMain").style.display = "block";
			document.getElementById("yorumYazUyari").style.display = "none";
			document.getElementById("yorumYazSonuc").style.display = "none";
		}
	}
}

function html_entity_decode (str) {
	var ta = document.createElement("textarea");
	ta.innerHTML = str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
	toReturn = ta.value;
	ta = null;
	return toReturn;
}

function changePanelContent (sectionFile, imgId) {
	var targetDivPanel = document.getElementById("comboBoxMain");
	targetDivPanel.innerHTML = "<img src='images/loading.gif' style='padding-top:53px; padding-left:149px;' />";
	
	switch (imgId) {
			case "comboId_1":
			tempComboIcerik = "<img src='images/hava_durumu_header_on.gif' width='76' height='22' border='0' />";
			document.getElementById("comboId_1").innerHTML = tempComboIcerik;
			document.getElementById("comboId_2").innerHTML = orginalLi_2;
			document.getElementById("comboId_3").innerHTML = orginalLi_3;
			document.getElementById("comboId_4").innerHTML = orginalLi_4;
			break;
			case "comboId_2":
			tempComboIcerik = "<img src='images/doviz_kuru_header_on.gif' width='76' height='22' border='0' />";
			document.getElementById("comboId_1").innerHTML = orginalLi_1;
			document.getElementById("comboId_2").innerHTML = tempComboIcerik;
			document.getElementById("comboId_3").innerHTML = orginalLi_3;
			document.getElementById("comboId_4").innerHTML = orginalLi_4;
			break;
			case "comboId_3":
			tempComboIcerik = "<img src='images/son_dakika_header_on.gif' width='70' height='22' border='0' />";
			document.getElementById("comboId_1").innerHTML = orginalLi_1;
			document.getElementById("comboId_2").innerHTML = orginalLi_2;
			document.getElementById("comboId_3").innerHTML = tempComboIcerik;
			document.getElementById("comboId_4").innerHTML = orginalLi_4;
			break;
			case "comboId_4":
			tempComboIcerik = "<img src='images/cok_okunanlar_header_on.gif' width='82' height='22' border='0' />";
			document.getElementById("comboId_1").innerHTML = orginalLi_1;
			document.getElementById("comboId_2").innerHTML = orginalLi_2;
			document.getElementById("comboId_3").innerHTML = orginalLi_3;
			document.getElementById("comboId_4").innerHTML = tempComboIcerik;
			break;
	}
	
	var tarayiciPanel = navigator.appName;
	if (tarayiciPanel == "Microsoft Internet Explorer") {
		httpPanel = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		httpPanel = new XMLHttpRequest();
	}
	
	httpPanel.open('get', sectionFile, true);
	httpPanel.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpPanel.send(null);
	httpPanel.onreadystatechange = function () {
		if (httpPanel.readyState == 4) {
			targetDivPanel.innerHTML = httpPanel.responseText;
			if (imgId == "comboId_1") {
				changeHavaDurumu (0);
			}
		}
	}
}

function doLogin() {
	noCache = Math.random();
	var tumVeriLogin = "username=" + encodeURI(document.getElementById("username").value) + "&password=" + encodeURI(document.getElementById("password").value) + "&nocache=" + noCache;

	var tarayiciLogin = navigator.appName;
	if (tarayiciLogin == "Microsoft Internet Explorer") {
		httpLogin = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		httpLogin = new XMLHttpRequest();
	}
	
	httpLogin.open('post', 'do_login.php', true);
	httpLogin.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpLogin.send(tumVeriLogin);
	httpLogin.onreadystatechange = function () {
		if (httpLogin.readyState == 4) {
			if (httpLogin.responseText == "user_logged"){
				window.location.href = document.location.href;
			} else {
				if (httpLogin.responseText == "user_not_logged"){
					document.getElementById("username").value = "";
					document.getElementById("password").value = "";
				}
			}
		}
	}
}

var timerMainRegister;
var emailRegisterChecked = false;

function sendRegisterForm() {
	if (timerMainRegister != undefined || timerMainRegister != "undefined"){
		clearTimeout(timerMainRegister);
		document.getElementById("hataSonuc").style.display = "none";
	}
	var formObject = document.getElementById("AddUser");
	var sayi = formObject.elements.length;
	var kontrol_edilecek = sayi;
	var kontrol_edilen = 0;
	var tip;
	var isim;
	var hata = [];
	hata[0] = "Lütfen Tüm Alanları Doldurunuz...";
	var sayac = 0;
	for (i=0;i<sayi;i++) {
		tip = formObject.elements.item(i).type;
		isim = formObject.elements.item(i).id;
		switch (tip) {
			case "text":
			if (formObject.elements.item(i).value != ''){
				if (isim == "registerEmail") {
					if (!emailRegisterChecked) {
						var email = formObject.elements.item(i).value;
						var emailResult = checkRegisterEmail(email);
						if (!emailResult) {
							hata[0] = "Geçerli bir E-Mail adresi giriniz...";
							emailRegisterChecked = false;
						} else {
							emailRegisterChecked = true;
							kontrol_edilen++;
						}
					} else {
						kontrol_edilen++;
					}
				} else {
					kontrol_edilen++;
				}
			}
			break;
			case "password":
			if (formObject.elements.item(i).value != ''){
				var tempPass = formObject.elements.item(i).value;
				if (tempPass.length < 5) {
					if (isim == "registerPass_2") {
						hata[0] = "Şifreniz ile Şifreniz (Tekrar) bölümleri aynı olmalıdır...";
					} else {
						hata[0] = "Şifreniz en az 5 karakter olmalıdır...";
					}
				} else {
					if (isim == "registerPass_2") {
						if (formObject.elements.item(i).value != formObject.elements.item(i-1).value) {
							hata[0] = "Şifreniz ile Şifreniz (Tekrar) bölümleri aynı olmalıdır...";
						}
					}
				}
				kontrol_edilen++;
			}
			break;
			case "select-one":
			if (formObject.elements.item(i).value != '0'){
				kontrol_edilen++;
			}
			break;
			case "hidden":
				kontrol_edilen++;
			break;
		}
	}
	//alert(kontrol_edilecek + " : " + kontrol_edilen);
	kontrol_edilen = kontrol_edilecek;
	if (kontrol_edilecek == kontrol_edilen) {
		document.getElementById("hataSonuc").style.display = "none";
		
		var tempVeriRegister = [];
		for (i=0;i<sayi;i++) {
			tempVeriRegister.push(formObject.elements.item(i).name + "=" + formObject.elements.item(i).value);
		}
		var tumVeriRegister = tempVeriRegister.toString().replace(/,/g,"&");
		//alert(tumVeri)
		var tarayiciRegister = navigator.appName;
		if (tarayiciRegister == "Microsoft Internet Explorer") {
			httpRegister = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			httpRegister = new XMLHttpRequest();
		}
		
		httpRegister.open('post', 'add_user.php', true);
		httpRegister.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpRegister.send(tumVeriRegister);
		httpRegister.onreadystatechange = function () {
			if (httpRegister.readyState == 4) {
				if (httpRegister.responseText == "register_ok"){
					document.getElementById("hataSonuc").innerHTML = "Üyelik kaydınız yapılmıştır. Aktivasyon ile ilgili bilgiler E-Mail adresinize gönderilmiştir.";
					document.getElementById("hataSonuc").style.display = "block";
					document.getElementById("registerButton").innerHTML = "<img src='images/kaydet_button.gif' width='56' height='19' border='0' />";
					for (i=0;i<sayi;i++) {
						formObject.elements.item(i).disabled = true;
					}
					//timerMainRegister = setTimeout ("hideRegisterMessage()", 8000);
				} else {
					if (httpRegister.responseText == "register_not_ok"){
						document.getElementById("hataSonuc").innerHTML = "Üyelik kaydı sırasında hata oluştu. Lütfen tekrar deneyiniz...";
						document.getElementById("hataSonuc").style.display = "block";
						//timerMainRegister = setTimeout ("hideRegisterMessage()", 5000);
					} else {
						document.getElementById("hataSonuc").innerHTML = httpRegister.responseText;
						document.getElementById("hataSonuc").style.display = "block";
						timerMainRegister = setTimeout ("hideRegisterMessage()", 5000);
					}
				}
			}
		}
	} else {
		document.getElementById("hataSonuc").innerHTML = hata[0];
		document.getElementById("hataSonuc").style.display = "block";
		timerMainRegister = setTimeout ("hideRegisterMessage()", 3000);
	}
}

function hideRegisterMessage() {
	document.getElementById("hataSonuc").style.display = "none";
}

function checkRegisterEmail(email) {
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	for (i=0; i < email.length ;i++) {
		if (ok.indexOf(email.charAt(i)) < 0) { 
			return false;
		}
	} 

	if (document.images) {
		ereg1 = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		ereg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!email.match(ereg1) && email.match(ereg2)) {
			return true;		
		} else {
			return false;
		}
	}
}

function checkUserName(valUsername) {
	var username = valUsername.value;
	var userToBeCheck = "userMainVal=" + valUsername.value;
	
	var tarayiciCheckUser = navigator.appName;
	if (tarayiciCheckUser == "Microsoft Internet Explorer") {
		httpCheckUser = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		httpCheckUser = new XMLHttpRequest();
	}
	
	httpCheckUser.open('post', 'do_check_username.php', true);
	httpCheckUser.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpCheckUser.send(userToBeCheck);
	httpCheckUser.onreadystatechange = function () {
		if (httpCheckUser.readyState == 4) {
			if (httpCheckUser.responseText == "user_exist"){
				document.getElementById("hataSonuc").innerHTML = "Girmiş olduğunuz Kullanıcı Adı kullanılıyor. Lütfen tekrar deneyiniz...";
				document.getElementById("hataSonuc").style.display = "block";
			} else {
				if (httpCheckUser.responseText == "user_not_exist"){
					var tempOnEk = username;
					if (tempOnEk.length > 4) {
						tempAra = " ";
						if (tempOnEk.search(tempAra) < 0){
							changeSwfValue (tempOnEk);
						} else {
							changeSwfValue ("null");
						}
					} else {
						changeSwfValue ("null");
					}
					document.getElementById("hataSonuc").style.display = "none";
				}
			}
		}
	}
}

function addToMailList() {
	var httpMailList;
	var email = document.getElementById("emailListText");
	if (email.value != "") {
		var tarayiciMailList = navigator.appName;
		if (tarayiciMailList == "Microsoft Internet Explorer") {
			httpMailList = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			httpMailList = new XMLHttpRequest();
		}
		httpMailList.open('get', 'add_to_maillist.php?userMail=' + email.value);
		httpMailList.onreadystatechange = function () {
			if (httpMailList.readyState == 4) {
				tempResult = document.getElementById("emailListResult");
				tempResultAll = document.getElementById("emailListResultAll");
				if (httpMailList.responseText == "false"){
					tempResult.innerHTML = "Email adresiniz geçersiz!..";
				} else {
					if (httpMailList.responseText == "true"){
						tempResult.innerHTML = "Email adresiniz listemize kaydedilmiştir...";
						email.disabled = true;
						
					} else {
						tempResult.innerHTML = "Email adresiniz listemize kayıtlı durumda...";
					}
				}
				if (tempResultAll.style.display == "none") {
					tempResultAll.style.display = "block";
					setTimeout ( "hideMailListResult()", 4000);
				} else {
					tempResultAll.style.display = "none";
				}
			}
		};
		httpMailList.send(null);
	}
}

function hideMailListResult() {
	document.getElementById("emailListResultAll").style.display = "none";
}


function hideMessage() {
	document.getElementById("hataSonuc").innerHTML = "";
}

function sendContactForm (){
	
	var veri1 = encodeURIComponent(document.getElementById("contactFormAdSoyad").value);
	var veri2 = encodeURIComponent(document.getElementById("contactFormTelefon").value);
	var veri3 = encodeURIComponent(document.getElementById("contactFormEmail").value);
	var veri4 = encodeURIComponent(document.getElementById("contactFormMesaj").value);
	
	var sayi = 5;
	var sayac = 1;
	
	for (i=1;i<sayi;i++){
		tempVeri = eval("veri" + i);
		if (tempVeri == ""){
			document.getElementById("hataSonuc").innerHTML = "Lütfen Tüm Alanları Doldurunuz...";
			timerMain = setTimeout ("hideMessage()", 2000);
			break;
		} else {
			sayac++;
		}
	}
	
	if (sayac == sayi) {
		var tumVeriContactForm = "ad_soyad="+veri1+"&telefon="+veri2+"&email="+veri3+"&mesaj="+veri4;
		var tarayiciContactForm = navigator.appName;
		
		if (tarayiciContactForm == "Microsoft Internet Explorer") {
			httpContactForm = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			httpContactForm = new XMLHttpRequest();
		}
		
		httpContactForm.open('post', 'send_contact_form.php', true);
		httpContactForm.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpContactForm.send(tumVeriContactForm);
		httpContactForm.onreadystatechange = function () {
			if (httpContactForm.readyState == 4) {
				if (httpContactForm.responseText == "email_ok"){
					document.getElementById("hataSonuc").innerHTML = "Formunuz gönderilmiştir. Teşekkür ederiz...";
					document.getElementById("contactFormAdSoyad").value= "";
					document.getElementById("contactFormTelefon").value= "";
					document.getElementById("contactFormEmail").value= "";
					document.getElementById("contactFormMesaj").value= "";
					timerMain = setTimeout ("hideMessage()", 10000);
				} else {
					if (httpContactForm.responseText == "email_not_ok"){
						document.getElementById("hataSonuc").innerHTML = "E-Mail adresiniz geçersiz!..";
						timerMain = setTimeout ("hideMessage()", 2000);
					}
				}
			}
		}
	}
}

function clearContactForm () {
	document.getElementById("contactFormAdSoyad").value= "";
	document.getElementById("contactFormTelefon").value= "";
	document.getElementById("contactFormEmail").value= "";
	document.getElementById("contactFormMesaj").value= "";
}

function doVotePoll() {
	
	var tempAnketId = document.getElementById("mainAnketId").value;
	var element = document.forms['pollForm'].elements['anketDeger'];
	var radioLength = element.length;
	for (var i = 0; i < radioLength; i++) {
		if (element[i].checked) {
			var selected = element[i].value;
		}
	}
	
	if (selected) {
		noCache = Math.random();
		var tumVeriPoll = "selectedVote=" + encodeURI(selected) + "&anketId=" + tempAnketId + "&nocache=" + noCache;
	
		var tarayiciPoll = navigator.appName;
		if (tarayiciPoll == "Microsoft Internet Explorer") {
			httpPoll = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			httpPoll = new XMLHttpRequest();
		}
		
		httpPoll.open('post', 'do_vote_poll.php', true);
		httpPoll.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpPoll.send(tumVeriPoll);
		httpPoll.onreadystatechange = function () {
			if (httpPoll.readyState == 4) {
				if (httpPoll.responseText == "Tamam"){
					document.getElementById("AnketMain").style.display = "none";
					document.getElementById("AnketSonuc").style.display = "block";
					document.getElementById("anketSonucKapat").style.display = "none";
				} else {
					if (httpPoll.responseText == "hata"){
						document.getElementById("AnketMain").style.display = "block";
						document.getElementById("AnketSonuc").style.display = "none";
					}
				}
			}
		}
	}
}

function showHidePollResults() {
	var anket = document.getElementById("AnketMain");
	var sonuc = document.getElementById("AnketSonuc");
	if (anket.style.display == "none") {
		anket.style.display = "block";
		sonuc.style.display = "none";
	} else {
		anket.style.display = "none";
		sonuc.style.display = "block";
	}
}

function harfCevir($metin) {
	$kh = array("ı","i","ü","ö","ş","ğ","ç");
	$bh = array("I","İ","Ü","Ö","Ş","Ğ","Ç");
	$metin = str_replace($kh,$bh,$metin);
	$metin = strtoupper($metin);
	return $metin;
}
