// JavaScript Document

var lastSSSID=0;

var lastTabID='';

var selectedPayType=0;

var lastFocusedId = '';

// Web20 Template Fonksiyonları

var totalTopMenuItems = 0;



function changeSPSlide(obj,num,total)

{

	num = Math.round(num % total);

	$(obj).find(".spSlide").hide('fast');

	$(obj).find(".spSlide:eq("+num+")").fadeIn('slow');

	setTimeout(function() { changeSPSlide(obj,(num + 1),total) },5000);

}



$(document).ready(function() {					   
	$('.imgCaptcha').parent().parent().remove();
	$('.spSlides').each(

		function() {

			var totalSlides = $(this).find('.spSlide').size();

			changeSPSlide(this,0,totalSlides);

		}

	);	

		/*				   

	$('#detailSearchKey').autocomplete('include/ajaxLib.php?act=arama',{

			minChars: 2,

			width: 300,

			multiple: false,

			matchContains: true,

			formatItem: formatItem,

			formatResult: formatResult,

			selectFirst:false

	});	
*/
	var zoomOptions = {preloadText:'Yükleniyor'};

	/*

	$('.lightbox:first').jqzoom(zoomOptions).removeClass('lightbox');
*/
	$('.lightbox').lightBox({fixedNavigation:false,imageBtnClose: 'images/lightbox-btn-close.gif',imageLoading:'images/lightbox-ico-loading.gif',containerResizeSpeed:350,txtOf:' / ',txtImage:$(this).attr('title')});

	if ($('#imgSepetGoster').position() != null) $("#sepetGoster").css({'top':($('#imgSepetGoster').position().top + $('#imgSepetGoster').height() + 3) + 'px','left':($('#imgSepetGoster').position().left - 400) + $('#imgSepetGoster').width() + 'px'});	

	$('#imgSepetGoster').hover(						   

		function() { $('#sepetGoster').slideDown(200); },

		function() { $('#sepetGoster').slideUp(200); }

	);

	if ($('#imgLoginGoster').position() != null) $("#loginGoster").css({'top':($('#imgLoginGoster').position().top + $('#imgLoginGoster').height() + 3) + 'px','left':($('#imgLoginGoster').position().left - 200) + $('#imgLoginGoster').width() + 'px'});

	$('#imgLoginGoster').toggle(						   

		function() { $('#loginGoster').slideDown(200); },

		function() { $('#loginGoster').slideUp(200); }

	);	

	$('.imgCaptchaRefresh').click(function() { $(this).parent().find('.imgCaptcha').attr({'src':'include/create_captcha.php?' + Math.random()}) });

	$('#gf_city,#gf_city2').change(function() { formCityChange(this) });	

	$('#gf_semt').change(

		function() {

			if (!$('#gf_semt2').html()) return;

			var errorID = $(this).attr('id').replace(/gf_/i,'error_'); 

			$('#'+errorID).text('');

			$('.generatedForm .button').show();

			if ($(this).find('option:selected').attr('kargo') == 0) 

			{

				$('#'+errorID).text(' Bu ilceye gonderimimiz yoktur!');

				$('.generatedForm .button').hide();

			}

			if ($(this).find('option:selected').attr('fiyatfarki') != 0) 

			{

				$('#'+errorID).text(' Bu ilceye +'+$(this).find('option:selected').attr('fiyatfarki')+' TL kargo farki uygulanmaktadır.');

			}

		}

	);

	if ($('#gf_city').html()) formCityChange($('#gf_city'));

});



function formCityChange(obj)

{

	var semtID = $(obj).attr('id').replace(/city/i,'semt');

	$('#'+semtID).html('<option>Yukleniyor ..</option>');

	var pars = 'act=town&cityID='+$(obj).val();

	$.ajax({

	  url: 'include/ajaxLib.php?' + pars,

	  success: function(data) 

			   { 

			   		var selected = $('#'+semtID).attr('selected');

					$('#'+semtID).html(data);

					$('#'+semtID+" option[value='"+selected+"']").attr('selected', 'selected'); 

			   }

	});

}



function checkCaptcha(formID) 

{
	$('#'+formID).submit();
	return;
	var pars = 'txtCaptcha='+$('.' + formID + '_txtCaptcha').val();

	$.ajax({

	  url: 'include/ajaxLib.php?act=captcha',

	  type: 'POST',

	  data: pars,

	  success: function(data) 

			   {

					if(data != 'true') alert('Güvenlik kodu hatalı.');

					else {

						$('#'+formID).submit();

					}

			   }

	});	

}

var ArkadasimaGonderWidth = 450;

var ArkadasimaGonderHeight = 440;



function arkadasimaGonderPopup(urunID) { window.open('popup.php?act=arkadasimaGonder&urunID='+urunID,'_blank','width='+ArkadasimaGonderWidth+',height='+ArkadasimaGonderHeight); return false; }



function formatItem(row) {

	return '<img src="include/resize.php?path=images/urunler/' + row[0] + '&width=20&height=20"> ' + row[1];

}

function formatResult(row) {

	return row[1];	

	//return row[0].replace(/(<.+?>)/gi, '');

}



function getHash(clientId , oid , amount , okUrl , failUrl ,islemtipi, taksit  ,rnd , storekey) {

	var hash='';

	var pars = 'act=getHash&clientId='+clientId+'&oid='+oid+'&amount='+amount+'&okUrl='+okUrl+'&failUrl='+failUrl+'&islemtipi='+islemtipi+'&taksit='+taksit+'&rnd='+rnd+'&storekey='+storekey;

	$.ajax({

	  url: 'include/ajaxLib.php?' + pars,

	  success: function(data) 

			   { 

					$('#hash').val(data);

			   }

	});

	return hash;

}



// Üst Kategori Listesi Fonksiyonları



var topCatID=0;

var topMarkaID=0;

var urunCatInsert = '';

var urunMarkaInsert = '';

var userNameError = '';

var emailNameError = '';	



function  checkRegisterStatus() {

	if ($('input[name=file_resim]').val() == '') {
		alert('Lütfen resminizi yükleyin');
		return false;
	}
	if (userNameError == 'false' && emailNameError == 'false') return true;

	

	checkAvail($('#gf_username').val(),'username');

	checkAvail($('#gf_email').val(),'email');

	return false;

}



function checkAvail(value,type) {

	if (value) { 

		$.ajax({

		  url: 'include/ajaxLib.php?act=checkAvail&str='+value+'&type='+type,

		  success: function(data) 

		  		   { 

		  		   		if  (data != '' && data.length > 5 && !(lastFocusedId == 'gf_'+type) ) {  

							if (type=='username') { $('#gf_'+type).focus(); alert('Bu kullanıcı adı daha önce alınmış. Lütfen farklı bir kullanıcı adı girin.');  userNameError = 'true';}

							if (type=='email' && userNameError != 'true') { $('#gf_'+type).focus(); alert('Bu E-Posta adresi daha önce sisteme kayıt edilmiş.'); emailNameError = 'true'; }												

						}

						else {

							if (type=='username') { userNameError = 'false';}

							if (type=='email') { emailNameError = 'false'; }								

						}

						if (userNameError == 'false' && emailNameError == 'false') $('.generatedForm').submit();

		  		   }

		});

	}

}



function updateSubCats(sID) {	

	document.getElementById('opt2').options.length = 1;

	document.getElementById('opt2').options[0].text = 'Lütfen Bekleyin...';

	var url = 'include/ajaxLib.php?act=topSubCategory&catID='+sID;

	$.ajax({

	  url: url,

	  success: function(data) 

			   { 

					if  (data != '' && data.length > 5 && !(lastFocusedId == 'gf_'+type) ) { upOptions('opt2',data); }

			   }

	});

	updateSubMarka(sID);

}



function sistemSepeteEkle() {

	var stop = false;

	var secilenUrunArray = new Array();	

	for (var i=1;i<=toplamkategori;i++) {

		catID = catNum[i];

		var urunID = document.getElementById('urunSelected_'+catID).options[document.getElementById('urunSelected_'+catID).selectedIndex].value;

		var stok  = urunStok[urunID];

		if (stok <=0) {

			alert('Stokta bulunmayan ürünü sepete ekleyemezsiniz.');

			stop = true;

		}

		if (urunID > 0) secilenUrunArray[secilenUrunArray.length]=urunID;		

	}

	if (secilenUrunArray.length > 0 && stop == false) {	

		document.getElementById('SistemSepeteEkleDiv').innerHTML = '<table><tr><td><img src="images/ajax.gif"></td><td stlye="padding-left:10px;">Lütfen Bekleyin...</td></tr></table>';

		for (var i=0;i<secilenUrunArray.length;i++) {

			var url = 'include/ajaxLib.php?act=sepetEkle&urunID=' + secilenUrunArray[i];

			if (i<(secilenUrunArray.length - 1)) {

					

					$.get(url, function(data) {  });			

			}

			else {

					$.get(url, function(data) {  window.location = 'page.php?act=sepet'; });

			}			

		}

	}

}



function updateSubMarka(sID) {

	document.getElementById('opt3').options.length = 1;

	document.getElementById('opt3').options[0].text = 'Lütfen Bekleyin...';

	var pars = 'act=topSubMarka&catID='+sID;

	$.ajax({

	  url: 'include/ajaxLib.php?act=topSubMarka&catID='+sID,

	  success: function(data) 

			   { 

					 upOptions('opt3',data);

			   }

	});	

	topCatID = (document.getElementById('opt2').selectedIndex >= 0?document.getElementById('opt2').options[document.getElementById('opt2').selectedIndex].value:document.getElementById('opt1').options[document.getElementById('opt1').selectedIndex].value);																																																		}



function upOptions(id,result) {

	document.getElementById(id).options.length = 0;

	var optsArray = result.split('||');

	for (var i=0;i<optsArray.length;i++) {

		var optDataArray = optsArray[i].split('$$');

		optDataArray[1] = optDataArray[1].replace('&#39;','\'');

		document.getElementById(id).options[document.getElementById(id).options.length] = new Option(optDataArray[1],optDataArray[0]);

	}

}



// Üst Kategori Listesi Fonksiyonları



var toplamkdvdahil = 0;

var toplamkdvharic = 0;

var toplamkdv = 0;



var KDVHaricArray = new Array();



function updateToplam() {

	var is = document.getElementsByTagName('input');

	toplamkdvdahil = 0;

	toplamkdvharic = 0;

	toplamkdv = 0;

	for (var i=0;i<is.length;i++) {

		if (is[i].id.indexOf('fiyat_') == 0) {

			var realID = is[i].id.replace('fiyat_','');

			toplamkdvdahil+=parseFloat(is[i].value);

			if (KDVHaricArray[realID]) toplamkdvharic+=parseFloat(KDVHaricArray[realID]);

		}

	}

	toplamkdv = toplamkdvdahil - toplamkdvharic;

	document.getElementById('kdvdahil').innerHTML = moneyFormat(toplamkdvdahil);

	document.getElementById('kdvharic').innerHTML = moneyFormat(toplamkdvharic);

	document.getElementById('toplamytl').innerHTML = moneyFormat(toplamkdvdahil);

	document.getElementById('toplamkdv').innerHTML = moneyFormat(toplamkdv);	

	document.getElementById('dolar').innerHTML = moneyFormat(toplamkdvdahil / dolar);

	document.getElementById('euro').innerHTML = moneyFormat(toplamkdvdahil / euro);

	if (document.getElementById('havaleile')) document.getElementById('havaleile').innerHTML =  moneyFormat(toplamkdvdahil - (toplamkdvdahil * havaleindirim));	

}



function updateKategori(urunID) {

	var url = 'include/ajaxLib.php';

	var catID=0;

	var data='';

	var myAjax = new Array();

	for (var i=2;i<=toplamkategori;i++) {

		catID = catNum[i];

		var pars = 'act=updateKategori&urunID='+urunID+'&catID='+catID+'&randID='+Math.floor(Math.random()*1000000);

		var target = 'catNum_'+i;

		if (urunID > 0) {

			pcTopLoading(i);

			$('#' + target).load('include/ajaxLib.php?'+pars);

		}

		up('adet_'+catID,0);

		up('fiyat_'+catID,0);

		document.getElementById('detail_href_'+catID).href = '#';

		document.getElementById('detail_href_'+catID).target = '_self';

		ch('stok_'+catID,'stok_def_'+catID);		

		KDVHaricArray[catID]=0;

		//window.open('include/ajaxLib.php?'+pars);

	}

	

	updateToplam();

}



function pcTopLoading(i) {

	document.getElementById('catNum_'+i).innerHTML='<img src="images/ajax.gif"> '; 

}



			 



function pcTopLoaded(response,i) {

	document.getElementById('catNum_'+i).innerHTML=response.responseText; 

}



function updateFiyat(urunID,catID) {

	var fiyat = urunFiyat[urunID];

	var stok  = urunStok[urunID];

	up('adet_'+catID,1);

	if (fiyat > 0) up('fiyat_'+catID,moneyFormat(fiyat));

	if (urunID > 0) {

		if (stok > 0) ch('stok_'+catID,'stok_var_'+catID);

		else {

			ch('stok_'+catID,'stok_yok_'+catID);

			up('adet_'+catID,0);

			up('fiyat_'+catID,0);

		}

	}

	else {

		ch('stok_'+catID,'stok_def_'+catID);

		up('adet_'+catID,0);

		up('fiyat_'+catID,0);

	}

	if (stok > 0) KDVHaricArray[catID] = urunKDVHaricFiyat[urunID];

	document.getElementById('detail_href_'+catID).href=(urunID > 0?'page.php?act=urunDetay&urunID=' + urunID:'#');

	document.getElementById('detail_href_'+catID).target=(urunID > 0?'_blank':'_self');

	updateToplam();

}



function updateAdet(v,catID) {

	var urunID = document.getElementById('urunSelected_'+catID).options[document.getElementById('urunSelected_'+catID).selectedIndex].value;

	var fiyat = urunFiyat[urunID];

	var stok  = urunStok[urunID];

	if (!isInt(v)) {		

		alert('Lütfen adet girişlerinde sadece rakkam kullanın.');

		v = (urunID > 0 && stok > 0 ? 1 : 0);

		up('adet_'+catID,v);

	}

	else if (v > stok) {

		alert('Stoklarımızda girdiğiniz adet kadar ürün bulunmamaktadır.');	

		up('adet_'+catID,stok);

		v = stok;

	}

	if (urunID > 0) up('fiyat_'+catID,moneyFormat(fiyat * v));

	updateToplam();

}



function ShowDetailPic(catID) {

	var urunID = document.getElementById('urunSelected_'+catID).options[document.getElementById('urunSelected_'+catID).selectedIndex].value;

	document.getElementById('detail_div_'+catID).innerHTML = '<img src="include/resize.php?path=images/urunler/'+urunResim[urunID]+'&width=500&height=100">';

	if (urunResim[urunID]) document.getElementById('detail_div_'+catID).style.display = 'block';

}

// PC Toplama Fonksiyonları



function moneyFormat(V){

	var intPart,decPart

	ret=V*100;ret=Math.round(ret);

	ret=V<.1?"0"+ret:ret;

	ret=V< 1?"0"+ret:""+ret;

	intPart=ret.substring(0,ret.length-2);

	decPart=ret.substring(ret.length-2);

	ret=intPart+"."+decPart;

	if (ret.indexOf('-') >=0) ret = ret.replace('00','');

	return(ret);

}



function pause(numberMillis)

{

	var now = new Date();

	var exitTime = now.getTime() + numberMillis;

	while (true)

	{

		now = new Date();

		if (now.getTime() > exitTime)

		return;

	}

} 



function isInt(x) { 

   var y=parseInt(x); 

   if (isNaN(y)) return false; 

   return x==y && x.toString()==y.toString(); 

 } 





function gv(id) {

	if (document.getElementById(id)) return document.getElementById(id).value; 

	else alert('DEBUG : '+id+' -> ID sine sahip bir element bulunmuyor.');	

}



function up(id,v) {

	if (document.getElementById(id)) document.getElementById(id).value = v; 

	else alert('DEBUG : '+id+' -> ID sine sahip bir element bulunmuyor.');

}



function ch(oldid,newid) {

	if (!document.getElementById(oldid)) alert('DEBUG : '+oldid+' -> ID sine sahip bir element bulunmuyor.');

	if (!document.getElementById(newid)) alert('DEBUG : '+newid+' -> ID sine sahip bir element bulunmuyor.');

	document.getElementById(oldid).innerHTML = document.getElementById(newid).innerHTML;

}



function openTab(id) {

	if (lastTabID == id) return;	

	var cont = false;

	{

		if (lastTabID) $('#tabData' + lastTabID).hide('fast');

		$('#tabData' + id).show('fast');

		$('#tabData' + id + ' .imgCaptchaRefresh').click();

		lastTabID = id;

		cont = true;

	}

	for (var i=1;i<=10;i++) {

		if (document.getElementById('option'+i)) {

			document.getElementById('option'+i).style.backgroundPosition = '0% 0px';

			document.getElementById('option'+i).getElementsByTagName('span')[0].style.backgroundPosition = '100% 0px';

		}

	}

	if (cont) {

		document.getElementById('option'+id).style.backgroundPosition = '0% -42px';

		document.getElementById('option'+id).getElementsByTagName('span')[0].style.backgroundPosition = '100% -42px';

	}

}



function flash(w,h,u,t) {

	document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='"+w+"' height='"+h+"'><param name='movie' value='"+u+"'><param name='quality' value='high'>");

	document.write("<param name='wmode' value='transparent'>");

	document.write("<embed src='"+u+"' quality='high' type='application/x-shockwave-flash' width='"+w+"' height='"+h+"'></embed></object>");	

}



function Validate_Email_Address(email_address)

         {

         at = email_address.indexOf('@');

         dot = email_address.indexOf('.');         

         if(at == -1 || 

            dot == -1 || 

            dot <= at + 1 ||

            dot == 0 || 

            dot == email_address.length - 1)

            return(false);

			else return true;

		 }





function trFix (string) {

	//string = string.replace(/y/g,'ı');

	//string = string.replace(/?/g,'ş');

	//string = string.replace(/?/g,'ğ');	

	return string;

}



function sssOpen(ID) {

	if (lastSSSID) {

		$('#sss_cevap_'+lastSSSID).slideUp('fast');

		$('#sss_image_'+lastSSSID).attr({'src':'images/sss_close.gif'});

	}

	if (lastSSSID != ID) {

		$('#sss_cevap_'+ID).slideDown('fast');

		$('#sss_image_'+ID).attr({'src':'images/sss_open.gif'});

		lastSSSID=ID;

	}

	else lastSSSID=0;

	

}



function fc(obj) {

	obj.innerHTML = obj.innerHTML + '<input type="submit" style="display:none" id="sb">';

	document.getElementById('sb').click();

}



function pencereAc(url,en,boy) {

	window.open(url,null,"height="+boy+",width="+en+",status=no,toolbar=no,menubar=no,location=no,scrollbars=1");

}


