$(function(){
	$('a.lbox').lightBox();
});

function elm(id) { return document.getElementById(id); }
function getPos(elmId, returnTop)
{
	ret = (returnTop ? elm(elmId).offsetTop : elm(elmId).offsetLeft);
	par = elm(elmId).offsetParent;
	while (par != null)
	{
		ret += (returnTop ? par.offsetTop : par.offsetLeft);
		par = par.offsetParent;
	}
	return ret;
}

function onayliYonlendir(url,mesaj)
{
	if (confirm(mesaj))
		location.href=url
}

function loginKontrol(frm)
{
	obj=document.getElementById(frm)
	pass=obj.password.value
	emailx=obj.email.value

	if (!emailx || emailx.length<2)
	{
		alert('Kullanıcı adınızı doğru olarak girin!')
		obj.email.focus();
		return false;
	}

	if (!pass || pass.length<4)
	{
		alert('Şifrenizi girin')
		obj.password.focus();
		return false;
	}

}



function regKontrol(frm)
{
	obj=document.getElementById(frm)
	ad=obj.first_name.value
	soyad=obj.last_name.value
	emailx=obj.email_address.value
	emailDogru=emailCheck(emailx);
	if (!ad || ad.length<1)
	{
		alert('Adınızı girin!')
		obj.first_name.focus();
		return false;
	}
	if (!soyad || soyad.length<1)
	{
		alert('Soyadınızı girin!')
		obj.last_name.focus();
		return false;
	}
	if (!emailDogru)
	{
		alert('Email adresinizi doğru olarak girin!')
		obj.email_address.focus();
		return false;
	}


}

function chPassKontrol(frm)
{
	obj=document.getElementById(frm)
	pass=obj.password.value
	pass1=obj.password1.value
	pass2=obj.old_password.value

	if (pass2.length<4)
	{
		alert('Eski şifrenizi girin!')
		obj.old_password.focus();
		return false;
	}
	if (pass.length<4)
	{
		alert('Yeni şifrenizi girin!')
		obj.password.focus();
		return false;
	}
	if (pass1.length<4)
	{
		alert('Yeni şifrenizi tekrar girin!')
		obj.password1.focus();
		return false;
	}
	if (pass1!=pass)
	{
		alert('Yeni şifreniz ile tekrarı uyuşmuyor. Tekrar deneyin!')
		obj.password.focus();
		return false;
	}


}

function activeKontrol(frm)
{
	obj=document.getElementById(frm)
	ad=obj.first_name.value
	soyad = obj.last_name.value
	chk1 = obj.gender[0].checked
	chk2 = obj.gender[1].checked
	modul = obj.modulx.value
	admin = obj.admin.value
	if (modul == 'activate')
	{
		pass = obj.password.value
		pass1 = obj.password1.value
	}

	if (modul == 'register')
	{
		emailx = obj.email_address.value
		emailDogru = emailCheck(emailx);
	}


	gun=obj.gun.value
	ay=obj.ay.value
	yil=obj.yil.value
	zip=obj.zip_code.value
	adres=obj.address.value
	city=obj.city.value
	country=obj.country.value
	code1a=obj.code1a.value
	code1b=obj.code1b.value
	code1c=obj.code1c.value
	code2a=obj.code2a.value
	code2b=obj.code2b.value
	code2c=obj.code2c.value


	var regex = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
	
	if (!ad || ad.length<1)
	{
		alert('Adınızı girin!')
		obj.first_name.focus();
		return false;
	}
	if (!soyad || soyad.length<1)
	{
		alert('Soyadınızı girin!')
		obj.last_name.focus();
		return false;
	}
	if (modul=='register' && !emailDogru)
	{
		alert('Email adresinizi doğru olarak girin!')
		obj.email_address.focus();
		return false;
	}

	if (modul=='activate' && pass.length<4 && !admin)
	{
		alert('Yeni şifrenizi girin!')
		obj.password.focus();
		return false;
	}
	if (modul=='activate' && pass1.length<4 && !admin)
	{
		alert('Yeni şifrenizi tekrar girin!')
		obj.password1.focus();
		return false;
	}
	if (modul=='activate' && pass1!=pass)
	{
		alert('Yeni şifreniz ile tekrarı uyuşmuyor. Tekrar deneyin!')
		obj.password.focus();
		return false;
	}

	/*
	if (!gun || !ay || !yil)
	{
		alert('Doğum tarihinizi girin!')
		obj.gun.focus();
		return false;
	}
	if (!chk1 && !chk2)
	{
		alert('Cinsiyetinizi seçin!')
		return false;
	}
	if (!adres || adres.length<1)
	{
		alert('Adresinizi girin!')
		obj.address.focus();
		return false;
	}
	if (!zip || zip.length<5 || !regex.test(zip))
	{
		alert('Posta kodunuzu girin!')
		obj.zip_code.focus();
		return false;
	}
	if (!city || city.length<1)
	{
		alert('Şehir girin!')
		obj.city.focus();
		return false;
	}
	if (!country || country.length<1)
	{
		alert('Ülkenizi girin!')
		obj.country.focus();
		return false;
	}
*/


	if (!regex.test(code1a) && code1a!='')
	{
		alert('Telefon numaranızda sadece rakamlar olabilir!')
		obj.code1a.focus();
		return false;
	}
	if (!regex.test(code2a) && code2a!='')
	{
		alert('Telefon numaranızda sadece rakamlar olabilir!')
		obj.code2a.focus();
		return false;
	}
	if (!regex.test(code1b) && code1b!='')
	{
		alert('Telefon numaranızda sadece rakamlar olabilir!')
		obj.code1b.focus();
		return false;
	}
	if (!regex.test(code2b) && code2b!='')
	{
		alert('Telefon numaranızda sadece rakamlar olabilir!')
		obj.code2b.focus();
		return false;
	}
	if (!regex.test(code1c) && code1c!='')
	{
		alert('Telefon numaranızda sadece rakamlar olabilir!')
		obj.code1c.focus();
		return false;
	}
	if (!regex.test(code2c) && code2c!='')
	{
		alert('Telefon numaranızda sadece rakamlar olabilir!')
		obj.code2c.focus();
		return false;
	}

	//return true;


}


function adresKontrol(frm)
{
//	if (document.getElementById(frm) != null)
//		return;
		
	obj = document.getElementById(frm);
		
	ad=obj.first_name.value;
	soyad=obj.last_name.value;
	member=obj.member_id.value;
	

	if (!member>0)
	{
		emailx=obj.email_address.value
		emailDogru=emailCheck(emailx);
		code1a=obj.code1a.value
		code1b=obj.code1b.value
		code1c=obj.code1c.value
		code2a=obj.code2a.value
		code2b=obj.code2b.value
		code2c=obj.code2c.value
	}

	
	zip=obj.zip_code.value
	adresx=obj.address.value

	cityx=obj.city.value
	country=obj.country.value


	var regex = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
	
	if (!ad || ad.length<1)
	{
		alert('Adınızı girin!')
		obj.first_name.focus();
		return false;
	}
	if (!soyad || soyad.length<1)
	{
		alert('Soyadınızı girin!')
		obj.last_name.focus();
		return false;
	}
	if (!member>0 && !emailDogru)
	{
		alert('Email adresinizi doğru olarak girin!')
		obj.email_address.focus();
		return false;
	}

	if (!adresx || adresx.length<1)
	{
		alert('Adresinizi girin!')
		return false;
	}
	/*if (!zip || zip.length<5 || !regex.test(zip))
	{
		alert('Posta kodunuzu girin!')
		obj.zip_code.focus();
		return false;
	}*/
	if (!cityx || cityx.length<1)
	{
		alert('Şehir girin!')
		obj.city.focus();
		return false;
	}
	if (!country || country.length<1)
	{
		alert('Ülkenizi girin!')
		obj.country.focus();
		return false;
	}


if (!member>0)
{
	if (!regex.test(code1a) && code1a!='')
	{
		alert('Telefon numaranızda sadece rakamlar olabilir!')
		obj.code1a.focus();
		return false;
	}
	if (!regex.test(code2a) && code2a!='')
	{
		alert('Telefon numaranızda sadece rakamlar olabilir!')
		obj.code2a.focus();
		return false;
	}
	if (!regex.test(code1b) && code1b!='')
	{
		alert('Telefon numaranızda sadece rakamlar olabilir!')
		obj.code1b.focus();
		return false;
	}
	if (!regex.test(code2b) && code2b!='')
	{
		alert('Telefon numaranızda sadece rakamlar olabilir!')
		obj.code2b.focus();
		return false;
	}
	if (!regex.test(code1c) && code1c!='')
	{
		alert('Telefon numaranızda sadece rakamlar olabilir!')
		obj.code1c.focus();
		return false;
	}
	if (!regex.test(code2c) && code2c!='')
	{
		alert('Telefon numaranızda sadece rakamlar olabilir!')
		obj.code2c.focus();
		return false;
	}
}
	return true;


}

function loginAlanDegistir(alan,deger,deger2)
{
	obj=document.getElementById(alan)

	if (obj.value==deger2)
		obj.value=deger

}


function uyelikKontrol(isim)
{
	obj=document.getElementById(isim)

	
	user=obj.username.value
	pass=obj.password.value
	pass1=obj.password1.value
	isim=obj.isim.value
	idvar=obj.id.value
	emailx=obj.email.value
	avatar=obj.avatar.value
	avatarIlk=avatar.substring(0,7)
	
	if (user=='' || user.length<1)
	{
		alert('Kullanıcı adı girin')
		obj.username.focus();
		return false;
	}
	
	if ((idvar>0 && pass!='') || idvar=='')
	{
	
		if (pass=='' || pass.length<1)
		{
			alert('Şifre girin!')
			obj.password.focus();
			return false;
		}
		if (pass1=='' || pass1.length<1)
		{
			alert('Şifre tekrarını girin!')
			obj.password.focus();
			return false;
		}
		if (pass!=pass1)
		{
			alert('Şifre tekrarınız ile şifre  aynı değil!')
			obj.password.focus();
			return false;
		}
	}
	
	if (isim=='')
	{
		alert('Görünen isim (rumuz) girin!')
		obj.isim.focus();
		return false;
	}

	emailDogru=emailCheck(emailx);

	if (!emailDogru)
	{
		alert('Email adresinizi doğru olarak girin!')
		obj.email.focus();
		return false;
	}


	
	if (avatar!='' && avatarIlk!='http://' )
	{
		alert('Avatar resminizin adresi http:// ile başlamalıdır!')
		obj.isim.focus();
		return false;
	}

	return true;

}



function UnuttumKontrol(isim)
{
	obj=document.getElementById(isim)

	emailx=obj.email.value

	emailDogru=emailCheck(emailx);

	if (!emailDogru)
	{
		alert('Email adresinizi doğru olarak girin!')
		obj.email.focus();
		return false;
	}
	else
		return true;

}

function emailCheck (emailStr) 
{
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		//alert("Email address seems incorrect (check @ and .'s)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	if (user.match(userPat)==null) {
		// user is not valid
		//alert("The username doesn't seem to be valid.")
		return false
	}


	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) {

	   //alert("The address must end in a three-letter domain, or two letter country.")
	   return false
	}


	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   //alert(errStr)
	   return false
	}

	return true;
}

function updateElement(content, ids)
{
	contents = content.split(";");
	for (i=0; i<ids.length; i++)
	{
		if (document.getElementById(ids[i]) == null)
			continue;
		
		if (document.getElementById(ids[i]).type == "text")
			document.getElementById(ids[i]).value = contents[i];
		else 
			document.getElementById(ids[i]).innerHTML = contents[i];
	}
	if (contents[i] != null && contents[i] == "alert")
		alert("Eklemek istediğiniz miktar, ürünümüzün stoktaki miktarını geçmektedir. Stokta kalan miktar sepetinize eklenmiştir.");
}

function newXMLHttpRequest()
{
	if( !window.XMLHttpRequest )
	{
	  try{ return new ActiveXObject("Msxml2.XMLHTTP.6.0") }catch(e){}
	  try{ return new ActiveXObject("Msxml2.XMLHTTP.3.0") }catch(e){}
	  try{ return new ActiveXObject("Msxml2.XMLHTTP") }catch(e){}
	  try{ return new ActiveXObject("Microsoft.XMLHTTP") }catch(e){}
	  throw new Error("Could not find an XMLHttpRequest alternative.")
	};
	return new XMLHttpRequest();
}

function ajax(url, ids)
{
	var request = newXMLHttpRequest();
	request.onreadystatechange = function()
	{
		if (request.readyState == 4 && request.status == 200)
		{
			updateElement(request.responseText, ids);
		}
	};
	request.open("GET", url, true);
	request.send(null);
}

function CartAmountUpdate(name,id)
{
	obj = document.getElementById(name)
	val = obj.value;
	//location.href = 'index.php?m=shop&s=update&id='+id+'&amount='+val;
	ajax("ajax_update.php?action=cartUpdate&id="+id+"&amount="+val,Array("price"+id, "total", "amount"+id, "totalWSale"));
	ajax("ajax_update.php?action=cartSum&id="+id+"&amount="+val,Array("ssAmount"+id,"ssPrice"+id, "ssTotal", "ssTotalWSale"));
}
function CargoAmountUpdate(name,id,mid)
{
	obj = document.getElementById(name)
	val = obj.value;
	//location.href = 'index.php?m=shop&s=update&id='+id+'&amount='+val;
	ajax("ajax_update.php?action=cargoUpdate&id="+id+"&val="+val+"&mid="+mid,Array("cargo"+id,"cs"+id));
}
function SetDisableFields(name)
{
	obj=document.forms[name];
	if (obj == null)
		return;

	if (obj.ayni.checked==true)
		typex="0"
	else
		typex="1"


	if (typex=="1")
		val=true
	else
		val=false

	if (val)
		document.getElementById('formUyari').style.visibility='hidden';
	else
		document.getElementById('formUyari').style.visibility='visible';
		
	obj.first_name.disabled=val;
	obj.last_name.disabled=val;
	obj.address.disabled=val;
	obj.zip_code.disabled=val;
	obj.city.disabled=val;
	obj.country.disabled=val;
	obj.resetx.disabled=val;
	obj.code1a.disabled=val;
	obj.code1b.disabled=val;
	obj.code1c.disabled=val;
	obj.code2a.disabled=val;
	obj.code2b.disabled=val;
	obj.code2c.disabled=val;
	
	if (typex=="0")
	{/*
		valx=''
		obj.first_name.value=valx
		obj.last_name.value=valx
		obj.address.value=valx
		obj.zip_code.value=valx
		obj.city.value=valx
		obj.country.value=valx*/
	}
	else
	{
		obj.first_name.value=obj.first_name1.value
		obj.last_name.value=obj.last_name1.value
		obj.address.value=obj.address1.value
		obj.zip_code.value=obj.zip_code1.value
		obj.city.value=obj.city1.value
		obj.country.value=obj.country1.value
		
	}

}

function SetDisableFields2(name)
{
	obj=document.forms[name];

	if (obj.ckbox.checked==true)
		typex="0"
	else
		typex="1"


	if (typex=="1")
		val=true
	else
		val=false
		
	if (val)
		document.getElementById('formUyari').style.visibility='hidden';
	else
		document.getElementById('formUyari').style.visibility='visible';
	obj.address.disabled=val;
	obj.zip_code.disabled=val;
	obj.city.disabled=val;
	obj.country.disabled=val;
	obj.resetx.disabled=val;
}

function FormTemizle(name)
{
	obj=document.getElementById(name)
	valx=''
	obj.first_name.value=valx
	obj.last_name.value=valx
	obj.address.value=valx
	obj.zip_code.value=valx
	obj.city.value=valx
	obj.country.value=valx
}

function FormTemizle2(name)
{
	obj=document.getElementById(name)
	valx=''
	obj.address.value=valx
	obj.zip_code.value=valx
	obj.city.value=valx
	obj.country.value=valx
}

function PaymentKontrol(name)
{
	
	obj=document.forms[name]
	pt = obj.payment_type.value;
	if(pt == "kk") {
		number=obj.cc_number.value
	month=obj.cc_exp_month.value
	year=obj.cc_exp_year.value
	cid=obj.cc_cid.value
	var regex = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

	if (number.length!=16 || !regex.test(number))
	{
		alert("Kredi kartı numaranızı doğru olarak girin!")
		obj.cc_number.focus();
		return false;
	}

	if(obj.cc_name.value==''){
		alert("Kredi kartı sahibinin adını giriniz!");
		obj.cc_name.focus();
		return false;
	}
	if (month=='')
	{
		alert("Son kullanım ayını girin!")
		obj.cc_exp_month.focus();
		return false;
	}
	if (year=='')
	{
		alert("Son kullanım yılını girin!")
		obj.cc_exp_year.focus();
		return false;
	}
	if (cid.length!=3 || !regex.test(cid))
	{
		alert("Güvenlik / CID numarasını girin!")
		obj.cc_cid.focus();
		return false;
	}
	
	return true;
	}
	else {
		return true;
	}
	
}

