// AJAX Functions /////////

var xmlHttp

function showIlce(str)
{
	var url="javascr.php?ref=ilcegoster" + "&q=" + str
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
}

function showIlce2(str)
{
	var url="javascr.php?ref=ilcegoster2" + "&q=" + str
	xmlHttp=GetXmlHttpObject(stateChanged2)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
}

function IlceGoster(str)
{
	var url="javascr.php?ref=ilcegoster2" + "&q=" + str
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
}

function IlceBox(str)
{
	var url="javascr.php?ref=ilcebox" + "&q=" + str
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	xmlHttp.send(null)
	document.getElementById("town2").length = 0
}

function stateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		document.getElementById("txtHint").innerHTML=xmlHttp.responseText
	}
}

function stateChanged2()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		document.getElementById("search").innerHTML=xmlHttp.responseText
	}
}

function GetXmlHttpObject(handler)
{
	var objXmlHttp=null

	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		alert("This example doesn't work in Opera")
		return
	}
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
		{
			strName="Microsoft.XMLHTTP"
		}
		try
		{
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler
			return objXmlHttp
		}
		catch(e)
		{
			alert("Error. Scripting for ActiveX might be disabled")
			return
		}
	}
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler
		return objXmlHttp
	}
}


// List Script Functions


function DeleteList() {
	document.getElementById("town2").length = 0
}

function AlreadyAtList(element) {

	y =document.getElementById("town2")
	for (i = 0; i < y.length; i++) {
		value = y.options[i].value;
		if (element == value) return true;
	}
	return false;
}

function AddToList() {
	x = document.getElementById("selection")
	y = document.getElementById("town2");
	if (x.selectedIndex==-1)
	return false
	if (AlreadyAtList(x.options[x.selectedIndex].value))
	return false
	y.length++
	y.options[y.length-1].value= x.options[x.selectedIndex].value
	y.options[y.length-1].text= x.options[x.selectedIndex].text
}

function DeleteFromList() {
	y = document.getElementById("town2");
	if (y.selectedIndex==-1)
	return false
	if(y.length==1){
		DeleteList()
		return true
	}
	else if (y.selectedIndex==y.length-1){
		y.length--
		return true
	}

	else if (y.selectedIndex==-1){
		return false
	}
	else {
		for (i=y.selectedIndex;i<y.length-1;i++)
		y.options[i] = y.options[i+1]
		return true
	}


}

function AddAll() {
	x = document.getElementById("selection")
	y = document.getElementById("town2");
	DeleteList()
	for (i=0; i<x.length; i++) {
		y.length = x.length
		y.options[i].value = x.options[i].value
		y.options[i].text = x.options[i].text
	}
}


function PostTown() {
	var y = document.getElementById("town2")
	var hdn_town = document.getElementById("hdn_town")
	var temp = ""
	for (i=0;i<y.length;i++)
	temp = temp + y.options[i].value + ","
	if (hdn_town.value = temp)
	return true
	else
	return false
}

// Form Validation

function NumberOnly(id) {

	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	var match = /^(\d|\s)+$/.test(val)
	if (val=="")
	match=1
	if (!match) {
		//	alert ()
		elm.style.backgroundColor = "#eeeeee"
		elm.focus()
		return false
	}
	return true
}

function ValidEmail(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	var match = /^\S+\@\w+(\.\w+$)|(\.\w+\.\w+$)/.test(val)
	if (!match)  {
		//	alert ("Gecerli email adresi giriniz.")
		elm.style.backgroundColor = "#eeeeee"
		elm.focus()
		return false	}
		return true
}

function NotEmpty(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	if (val=="") {
		///	alert ("L�tfen gerekli alanlari doldurunuz.")
		elm.style.backgroundColor = "#eeeeee"
		elm.focus()
		return false	}
		return true
}

function StringOnly(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	var match = /^\w+$/.test(val)
	if (val=="")
	match=1
	if (!match) {
		//	alert ("Lutfen alanlara uygun degerler giriniz.")
		elm.style.backgroundColor = "#eeeeee"
		elm.focus()
		return false		}
		return true
}

function ValidUsername(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	var match = /^\w{6,10}/.test(val)
	if (!match) {
		//	alert ("Lutfen kullanici adina en az 6 en fazla 10 karakter giriniz,bosluk ve �zel karakter kullanmayiniz.")
		elm.style.backgroundColor = "#eeeeee"
		elm.focus()
		return false		}
		return true
}

function ValidPassword(id) {
	var elm = document.getElementById(id)
	var val = document.getElementById(id).value
	var match = /^\w{6,10}/.test(val)
	if (!match) {
		//	alert ("Lutfen sifre alanina en az 6 en fazla 10 karakter giriniz,bosluk ve �zel karakter kullanmayiniz.")
		elm.style.backgroundColor = "#eeeeee"
		elm.focus()
		return false		}
		return true
}

function FormValidate(id) {

	var id
	switch (id)
	{
		case "install" :
		if (!ValidUsername("db_user") || !ValidPassword("db_pass"))
		return false
		break

		case "addauc":
		case "editauc":
		if (!NotEmpty ("status") || !NotEmpty ("city") || !NotEmpty ("town") || !NotEmpty ("type")  ||!NotEmpty("price") || !NotEmpty("kur") || !NumberOnly ("m2"))
		return false
		break

		case "support" :
		if (!NotEmpty("isim") || !NotEmpty("soyisim") || !NotEmpty("mesaj"))
		return false
		break

		case "addclass":
		case "editclass":
		case "adddetail":
		case "editdetail":
		case "editcity":
		case "edittown" :
		if (!NotEmpty ("isim"))
		return false
		break

		case "addcontact" :
		case "editcontact" :
		if (!NotEmpty ("isim") || !ValidEmail ("email") || !NumberOnly("phone"))
		return false
		break

		case "changepass":
		if (!ValidUsername("username1") || !ValidPassword("oldpass") || !ValidPassword("newpass"))
		return false
		break

		case "changeuser":
		if (!ValidUsername("username2") || !ValidPassword("password") || !ValidPassword("newuser"))
		return false
		break

		case "editpublic" :
		if (!NotEmpty("name") || !ValidEmail("email") || !NumberOnly("cell"))
		return false
		break

		case "admingeneral" :
		if (!ValidEmail ("email"))
		return false
		break

		case "sendauc" :
		if (!NotEmpty ("status") || !NotEmpty ("city") || !NotEmpty ("town") || !NotEmpty ("type")  ||!NotEmpty("price") || ! NumberOnly ("m2") || !NotEmpty ("isim") || !ValidEmail ("email") || !NotEmpty("UserText"))
		return false
		break

		case "searchform" :
		if (!NumberOnly ("m2"))
		return false
		PostTown();
		break

		case "register" :
		if (!NotEmpty ("adsoyad") || !ValidEmail("email"))
		return false
		break

		case "editmail" :
		if (!NotEmpty ("isim") || !ValidEmail("email"))
		return false
		break

		case "detayli" :
		if (!NotEmpty ("isim")||!ValidEmail("email") )
		return false
		document.getElementById("link1").value = document.location.href
		break

		case "arkgonder" :
		if (!NotEmpty ("isim1") || !NotEmpty("isim2") ||  ! ValidEmail ("email1") || ! ValidEmail("email2"))
		return false
		document.getElementById("link2").value = document.location.href
		break

		case "talepgonder" :
		if (! NotEmpty ("status") || ! NotEmpty ("city") || ! NotEmpty ("town") || ! NotEmpty ("type")  ||! NotEmpty("price") || ! NotEmpty ("isim") || ! ValidEmail ("sender"))
		return false
		break

		case "editcur" :
		var elm = document.getElementById("ratio")
		var val = document.getElementById("ratio").value
		var match = /(^\d+\.\d+$)|(^\d+$)/.test(val)
		if (!match) {
			elm.style.backgroundColor = "#eeeeee"
			elm.focus()
			return false
		}
		break

	} //end switch

	return true

} // end function


// General Functions //

function kredi_hesap()
{
	var tutar=document.forms['frm_kredi'].tutar.value;
	var vade=document.forms['frm_kredi'].vade.value;
	var faiz=document.forms['frm_kredi'].faiz.value;
	var	match1=/(^\d+$)|(^\d+\.\d+$)/.test(tutar)
	var	match2=/(^\d+$)|(^\d+\.\d+$)/.test(vade)
	var	match3=/(^\d+$)|(^\d+\.\d+$)/.test(faiz)
	if(!match1||!match2||!match3||vade=="0"||faiz=="0")
	{
		alert("L�tfen alanlarda sadece rakam ve nokta(.) kullan�n�z!!")
		return false
	}
	var aylik_odeme=Math.round((tutar*(Math.pow((1+faiz*1.05/100),vade)*(faiz*1.05/100))/(Math.pow((1+(faiz*1.05/100)),vade)-1)),0);
	document.getElementById('aylik_odeme').innerHTML=aylik_odeme+" YTL";
}

function ShowTip(src) {
	var src
	var tiphtml = "<img src='" + src +"'  width=100 height=80 />"
	document.getElementById("tipdiv").innerHTML = tiphtml
}

function ChangeVis(id1,id2) {
	var id1
	var id2
	var elm1 = document.getElementById(id1)
	var elm2 = document.getElementById(id2)
	elm1.style.display = "none"
	elm2.style.display = "block"
}

function ChangePic(id)
{
	var sub = document.getElementById(id)
	var parent = document.getElementById("parent")
	parent.src = sub.src
}

function moneyFormat(id) {
	var	id  = document.getElementById(id)
	var nf = new NumberFormat(id.value)
	nf.setPlaces(0)
	nf.setSeparators(true)
	var num = nf.toFormatted()
	id.value = num
	return true
}

function CheckAll(){
	var x=document.getElementById("a_maillist")
	var i=0
	for (i=0;i<x.length-1;i++)
	x.elements[i].checked = true
}

function UnCheckAll(){
	var x=document.getElementById("a_maillist")
	var i=0
	for (i=0;i<x.length-1;i++)
	x.elements[i].checked = false
}

function resimGoster(src) {
	var src
	window.open(src,"_blank","directories=0,toolbar=0,resizable=0,width=500,height=500");
}

function videoGoster(src){
	var src
	window.open(src,"_blank","directories=0,toolbar=0,resizable=0,width=310,height=300");
}