function randomString(string_length) {
	var chars = "0123456789";
	// var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function randomChars(string_length) {
	// var chars = "0123456789";
	var chars = "0123456789abcdefghiklmnopqrstuvwxyz";
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

$(document).ready(function() {
	$('#kuponeklebutton').click(function()
		{
			for(var i=1;i<=$('#kuponekle').val();i++)
			{
				var obj ='#id_kuponlist';
				if ($(obj).val() == null) obj = '#kuponlist';
				if ($(obj).val()) $(obj).val($(obj).val() + "\n");
				$(obj).val($(obj).val() + randomChars(10));
			}
		}
	);
});

function checkAll(check){
	for (var i=0;i<document.getElementsByTagName('input').length;i++)
	{
		var e=document.getElementsByTagName('input')[i];
		if ((e.name != 'allbox') && (e.type=='checkbox'))
		{
			e.checked=check;
		}
	}
}
function removeFile(dbase,field,dbID,filePath,spanID) {
	if (confirm('Dosya hem veritabanından hemde diskden silinecek. Emin misiniz?')) {
		document.getElementById('ajaxEmu').src = './removeFile.php?dbName='+dbase+'&dbField='+field+'&dbID='+dbID+'&filePath='+filePath;
		// window.open('./s.php?f=removeFile.php&dbName='+dbase+'&dbField='+field+'&dbID='+dbID+'&filePath='+filePath);
		document.getElementById(spanID).innerHTML = '<b>Lütfen Bekleyin...</b>';
		setTimeout(function(){ document.getElementById(spanID).innerHTML = '<b>Dosya Silindi!</b>'; },500);
	}
	return false;
}

function xmlCheckFields(data)
{
	var cont = false;
	var checkFileds = data.split(',');
	for(var i = 0; i<=checkFileds.length; i++)
	{
		if ($('#'+checkFileds[i]).is(':checked')) cont = true;
	}
	if(!cont) alert('Lütfen en az bir seçeneği işaretleyin.');
	return cont;
}

var XMLWindow;
function xmlUpadte(updateURL,dilim)
{
	if (dilim == 0) {
		$('#plsWait').html('<img src="../templates/system/admin/template/images/loading.gif">');
		$('#ViewLogFile').show().val('Lütfen Bekleyin ..');
		XMLWindow = window.open('xmlViewLog.php?rand=' + randomString(5),'XML','width=800,height=600,scrollbars=1');
		window.open (updateURL + '&dilim=' + dilim + '&rand=' + randomString(5));
	}
	if (dilim != 0) $('#xmlPer').css('backgroundColor','green');
	$.ajax({
	  url: updateURL + '&dilim=' + dilim + '&rand=' + randomString(5),
	  error:function(data) {
		$('#xmlPer').html(' %' + (dilim +1) + '0 ').css({'width':((dilim + 1) * 4) + '0px'});
		if (dilim < 9) xmlUpadte(updateURL,(dilim + 1));
		else {
			$('#plsWait').html('<img src="../templates/system/admin/template/images/ok.gif">');
			$('#ViewLogFile').show().val('Güncelleme tamamlandı ..');
		}
		XMLWindow.location.replace('xmlViewLog.php?rand=' + randomString(5));
	  },
	  timeout: 3600000,
	  success: function(data) {
		$('#xmlPer').html(' %' + (dilim +1) + '0 ').css({'width':((dilim + 1) * 4) + '0px'});
		if (dilim < 9) xmlUpadte(updateURL,(dilim + 1));
		else {
			$('#plsWait').html('<img src="../templates/system/admin/template/images/ok.gif">');
			$('#ViewLogFile').show().val('Güncelleme tamamlandı ..');
		}
		XMLWindow.location.replace('xmlViewLog.php?rand=' + randomString(5));
		/*
		$.ajax({
			url:'xmlViewLog.php?rand=' + randomString(5),
			success: function(data) { if (data) $('#ViewLogFile').val(data); }
		});
		*/
	  }
	});	
}
