function popup(url, width, height) 
{	
	var screen_width = (screen.width - width) / 2;
	var screen_height = (screen.height - height) / 2;
	
	window.open(url, '', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=0, width='+ width +', height='+ height +', left='+ screen_width +', top='+ screen_height);
}

var Cookies = {
    init: function () {
        var allCookies = document.cookie.split('; ');
        for(var i = 0; i < allCookies.length; i++) {
            var cookiePair = allCookies[i].split('=');
            this[cookiePair[0]] = cookiePair[1];
        }
    },
    create: function (name, value, days) {
        if(days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 1 * 60 * 60 * 1000));
            var expires = "; expires="+ date.toGMTString();
        }
        else var expires = "";
            document.cookie = name +"="+ value + expires +"; path=/";
            this[name] = value;
    },
    erase: function (name) {
        this.create(name, '', -1);
        this[name] = undefined;
    }
};

Cookies.init();

/* common functions for app. files */

var $j = jQuery.noConflict();

/* top last minute news ticker */
(function($j) {
	$j.fn.newsTicker = $j.fn.newsticker = function(delay)
	{
		delay = delay || 4000;
		initTicker = function(el)
		{
			stopTicker(el);
			el.items = $j("li", el);
			// hide all items (except first one)
			el.items.not(":eq(0)").hide().end();
			// current item
			el.currentitem = 0;
			startTicker(el);
		};
		startTicker = function(el)
		{
			el.tickfn = setInterval(function() { doTick(el) }, delay)
		};
		stopTicker = function(el)
		{
			clearInterval(el.tickfn);
		};
		pauseTicker = function(el)
		{
			el.pause = true;
		};
		resumeTicker = function(el)
		{
			el.pause = false;
		};
		doTick = function(el)
		{
			// don't run if paused
			if(el.pause) return;
			// pause until animation has finished
			el.pause = true;
			// hide current item
			$j(el.items[el.currentitem]).fadeOut("slow",
				function()
				{
					$j(this).hide();
					// move to next item and show
					el.currentitem = ++el.currentitem % (el.items.size());
					$j(el.items[el.currentitem]).fadeIn("slow",
						function()
						{
							el.pause = false;
						}
					);
				}
			);
		};
		this.each(
			function()
			{
				if(this.nodeName.toLowerCase()!= "ul") return;
				initTicker(this);
			}
		)
		.addClass("newsticker")
		.hover(
			function()
			{
				// pause if hovered over
				pauseTicker(this);
			},
			function()
			{
				// resume when not hovered over
				resumeTicker(this);
			}
		);
		return this;
	};
	
})(jQuery);

function textarea_limit(id, limit)
{
	var input_len = $j('#'+ id).val().length;
	var remaining = input_len < limit ? limit - input_len : '0';
	
	$j('#'+ id +'_limit').html(remaining);
	
	if(input_len > limit) {
		input = $j('#'+ id).val().substring(0, limit);
		$j('#'+ id).val(input);
	}
}

function news_comment_reset()
{
	$j("#title").val('');
	$j("#comment").val('');
	$j("#title_limit").text('50');
	$j("#comment_limit").text('500');
}

function news_comment_check()
{
	msg = '';
	
	if ($j('#title').val() == '') {
		msg = 'Lütfen Adınızı belirtiniz!';
		$j('#title').focus();
	} else if ($j('#comment').val() == '') {
		msg = 'Lütfen yorumunuzu belirtiniz!';
		$j('#comment').focus();
	}
	if (msg == '') {
		arg = 'news_id=' + $j('#news_id').val() + '&reader_id=' + $j('#reader_id').val() + '&title=' + $j('#title').val() + '&comment=' + $j('#comment').val();
		add_news_comment(arg);
	//return true;
	} else {
		alert(msg);
		
	}
}

function news_friend_check()
{
	msg = '';
	
	if ($j('#to').val() == '') {
		msg = 'Lütfen gönderilecek adresi belirtiniz!';
		$j('#to').focus();
	} else if ($j('#subject').val() == '') {
		msg = 'Lütfen konuyu belirtiniz!';
		$j('#subject').focus();
	} else if ($j('#message').val() == '') {
		msg = 'Lütfen özel mesajınızı belirtiniz!';
		$j('#message').focus();
	}
	if (msg == '') {
		arg = 'news_id=' + $j('#news_id').val() + '&to=' + $j('#to').val() + '&subject=' + $j('#subject').val() + '&message=' + $j('#message').val();
		sendfriend(arg);
	//return true;
	} else {
		alert(msg);
		
	}
}

function writer_friend_check()
{
	msg = '';
	
	if ($j('#to').val() == '') {
		msg = 'Lütfen gönderilecek adresi belirtiniz!';
		$j('#to').focus();
	} else if ($j('#subject').val() == '') {
		msg = 'Lütfen konuyu belirtiniz!';
		$j('#subject').focus();
	} else if ($j('#message').val() == '') {
		msg = 'Lütfen özel mesajınızı belirtiniz!';
		$j('#message').focus();
	}
	if (msg == '') {
		arg = 'writer_id=' + $j('#writer_id').val() + '&to=' + $j('#to').val() + '&subject=' + $j('#subject').val() + '&message=' + $j('#message').val();
		sendfriend_writer(arg);
	//return true;
	} else {
		alert(msg);
		
	}
}


/*writer*/

function writer_comment_reset()
{
	$j("#title").val('');
	$j("#comment").val('');
	$j("#title_limit").text('100');
	$j("#comment_limit").text('500');
}

function writer_comment_check()
{
	msg = '';
	
	if ($j('#title').val() == '') {
		msg = 'Lütfen başlığı belirtiniz!';
		$j('#title').focus();
	} else if ($j('#comment').val() == '') {
		msg = 'Lütfen yorumunuzu belirtiniz!';
		$j('#comment').focus();
	}
	if (msg == '') {
		arg = 'post_id=' + $j('#post_id').val() + '&reader_id=' + $j('#reader_id').val() + '&title=' + $j('#title').val() + '&comment=' + $j('#comment').val();
		add_writer_comment(arg);
	//return true;
	} else {
		alert(msg);
		
	}
}

function login_check()
{
	msg = '';
	
	if ($j('#username').val() == '') {
		msg = 'Lütfen kullanıcı adınızı belirtiniz!';
		$j('#username').focus();
	} else if ($j('#password').val() == '') {
		msg = 'Lütfen şifrenizi belirtiniz!';
		$j('#password').focus();
	}
	
	if (msg == '')
		return true;
	
	alert(msg);
	return false;
	
}

function signup_check()
{
	
	msg = '';
/*	
	if ($j('#tcno').val() == '') {
		msg = 'Lütfen TC kimlik numaranızı belirtiniz!';
		$j('#tcno').focus();
	} 
*/
	if ($j('#username').val() == '') {
		msg = 'Lütfen kullanıcı adınızı belirtiniz!';
		$j('#username').focus();
	} else if ($j('#username').val().length < 6) {
		msg = 'Kullanıcı adınız en az 6 karakter olmalıdır!';
		$j('#username').focus();
	} else if ($j('#password').val() == '') {
		msg = 'Lütfen şifrenizi belirtiniz!';
		$j('#password').focus();
	} else if ($j('#password').val().length < 4) {
		msg = 'Şifreniz en az 4 karakter olmalıdır!';
		$j('#password').focus();
	} else if ($j('#password').val() != $j('#password2').val()) {
		msg = 'Şifreleriniz aynı değil, lütfen kontrol ediniz!';
		$j('#password2').focus();
	} else if ($j('#email').val() == '') {
		msg = 'Lütfen E-posta adresinizi belirtiniz!';
		$j('#email').focus();
	} else if ($j('#email').val().indexOf('@') <= 0) {
		msg = 'Lütfen E-posta adresinizi doğru belirtiniz!';
		$j('#email').focus();
	} else if ($j('#email').val().indexOf('.') <= 0) {
		msg = 'Lütfen E-posta adresinizi doğru belirtiniz!';
		$j('#email').focus();
	} else if ($j('#email').val().indexOf(' ') > 0) {
		msg = 'Lütfen E-posta adresinizi doğru belirtiniz!';
		$j('#email').focus();
	} else if ($j('#email').val().indexOf(',') > 0) {
		msg = 'Lütfen E-posta adresinizi doğru belirtiniz!';
		$j('#email').focus();
	} else if ($j('#email').val().indexOf('*') > 0) {
		msg = 'Lütfen E-posta adresinizi doğru belirtiniz!';
		$j('#email').focus();
	} else if ($j('#name').val() == '') {
		msg = 'Lütfen adınızı ve soyadınızı belirtiniz!';
		$j('#name').focus();
	} else if ($j('#contract_check:checked').val() == null) {
		msg = 'Üye olabilmeniz için sözleşmeyi kabul etmelisiniz!';
		$j('#contract_check').focus();
	}
	
	if (msg == '') {
		
		window.location = '#top';
		
		formElementIds = new Array ("username", "password", "email", "name", "birth_date_day", "birth_date_month", "birth_date_year", "sex", "ocupation", "city", "phone", "newslatter"); /*"tcno", */
		
		formElementCount = formElementIds.length;
		arg = '';
		
		for(i = 0; i < formElementCount; i++) {
			if(i == 0) {
				arg += formElementIds[i] + '=' + $j("#" + formElementIds[i]).val();
			} else {
				arg += '&' + formElementIds[i] + '=' + $j("#" + formElementIds[i]).val();
			}
		}
		
		add_reader(arg);
		
	} else {
		alert(msg);
		return false;
	}
}

function feedback_check()
{
	msg = '';
	
	if($j("#title").val() == '') {
		msg = 'Lütfen konuyu belirtiniz!';
		$j("#title").focus();
	} else if($j("#feedback").val() == '') {
		msg = 'Lütfen şikayetinizi belirtiniz.';
		$j("#feedback").focus();
	}
	
	if(msg == '') {
		arg = 'title=' + $j("#title").val() + '&feedback=' + $j("#feedback").val();
		add_feedback(arg);
	} else {
		alert(msg);
		return false;
	}
}

function contact_reset()
{
	$j("#squad").val('');
	$j("#name").val('');
	$j("#email").val('');
	$j("#phone").val('');
	$j("#message").val('');
}

function contact_check()
{
	msg = '';
	
	if($j("#squad").val() == '') {
		msg = 'Lütfen gönderilecek departmanı belirtiniz!';
		$j("#squad").focus();
	} else if($j("#name").val() == '') {
		msg = 'Lütfen adınızı ve soyadınızı belirtiniz!';
		$j("#name").focus();
	} else if($j("#email").val() == '') {
		msg = 'Lütfen e-posta adresinizi belirtiniz!';
		$j("#email").focus();
	} else if($j("#message").val() == '') {
		msg = 'Lütfen mesajınızı belirtiniz!';
		$j("#message").focus();
	}
	
	if(msg == '') {
		arg = 'squad=' + $j("#squad").val() + '&name=' + $j("#name").val() + '&email=' + $j("#email").val() + '&phone=' + $j("#phone").val() + '&message=' + $j("#message").val();
		contact_send(arg);
		contact_reset();
	} else {
		alert(msg);
		return false;
	}
	
}

function password_send_check()
{
	msg = '';

	if($j("#username").val() == '') {
		msg = 'Lütfen kullanıcı adınızı belirtiniz!';
		$j("#username").focus();
	} else if($j("#email").val() == '') {
		msg = 'Lütfen e-posta adresinizi belirtiniz!';
		$j("#email").focus();
	}
	
	if(msg == '') {
		arg = 'username=' + $j("#username").val() + '&email=' + $j("#email").val();
		password_send(arg);
	} else {
		alert(msg);
		return false;
	}
	
}

function password_edit_check()
{
	msg = '';

	if($j("#password").val() == '') {
		msg = 'Lütfen eski şifrenizi belirtiniz!';
		$j("#password").focus();
	} else if($j("#new_password").val() == '') {
		msg = 'Lütfen yeni şifrenizi belirtiniz!';
		$j("#new_password").focus();
	} else if ($j('#new_password').val().length < 4) {
		msg = 'Yeni şifreniz en az 4 karakter olmalıdır!';
		$j('#password').focus();
	} else if($j("#new_password2").val() != $j("#new_password").val()) {
		msg = 'Lütfen yeni şifrelerinizi kontrol ediniz! Şifreler aynı değil!';
		$j("#new_password2").focus();
	}
	
	if(msg == '') {
		arg = 'password=' + $j("#password").val() + '&new_password=' + $j("#new_password").val();
		password_edit(arg);
	} else {
		alert(msg);
		return false;
	}
	
}

function reader_edit_check()
{
	msg = '';
	
	if ($j('#tcno').val() == '') {
		msg = 'Lütfen TC kimlik numaranızı belirtiniz!';
		$j('#tcno').focus();
	} else if ($j('#email').val() == '') {
		msg = 'Lütfen E-posta adresinizi belirtiniz!';
		$j('#email').focus();
	} else if ($j('#email').val().indexOf('@') <= 0) {
		msg = 'Lütfen E-posta adresinizi doğru belirtiniz!';
		$j('#email').focus();
	} else if ($j('#email').val().indexOf('.') <= 0) {
		msg = 'Lütfen E-posta adresinizi doğru belirtiniz!';
		$j('#email').focus();
	} else if ($j('#email').val().indexOf(' ') > 0) {
		msg = 'Lütfen E-posta adresinizi doğru belirtiniz!';
		$j('#email').focus();
	} else if ($j('#email').val().indexOf(',') > 0) {
		msg = 'Lütfen E-posta adresinizi doğru belirtiniz!';
		$j('#email').focus();
	} else if ($j('#email').val().indexOf('*') > 0) {
		msg = 'Lütfen E-posta adresinizi doğru belirtiniz!';
		$j('#email').focus();
	} else if ($j('#name').val() == '') {
		msg = 'Lütfen adınızı ve soyadınızı belirtiniz!';
		$j('#name').focus();
	}
	
	if(msg == '') {
		
		formElementIds = new Array ("email", "name", "birth_date_day", "birth_date_month", "birth_date_year",
				"ocupation", "city", "phone");

		formElementCount = formElementIds.length;
		
		arg = '';
		
		for(i = 0; i < formElementCount; i++) {
			if(i == 0) {
				arg += formElementIds[i] + '=' + $j("#" + formElementIds[i]).val();
			} else {
				arg += '&' + formElementIds[i] + '=' + $j("#" + formElementIds[i]).val();
			}
		}
		
		newslatter = $j("input[@name='newslatter']:checked").val();
		
		if(newslatter == undefined)
			newslatter = 0;
		
		arg += '&newslatter=' + newslatter;
		
		sex = $j("input[@name='sex']:checked").val();
		arg += '&sex=' + sex;
		
		reader_edit(arg);
		
	} else {
		alert(msg);
		return false;
	}
}
/* Ajax All Functions */

function ajax_loading(id, top, align)
{
	$j(id).html("<div style=\"width:100%; margin:"+ top +"px 0px 0px 0px; text-align:"+align+"\"><img src=\"data/template/ajax_loading.gif\"></div>");
}

function add_news_comment(arg)
{
	id = "#addcomment"; // ajax response id
	
	$j.ajax({
		type: "POST", // method = GET or POST
		cache: true, // cache = true or false
		url: "module/comment/ajax_news_comment.php", // ajax include file
		data: arg, // method parameters
		beforeSend: function(){ajax_loading(id, 20, 'center');},
		success: function(msg) {
			/* ajax response */
			$j(id).html(msg);
			
			$j(id).ajaxStop(function(){
				if($j("#send_status").val() == '31')
					window.location = 'comment_send.html';
			});
		},
		/* ajax error */
		error: function(oXHR, status) {
			$j(id).html("<span class=\"error\">Yorumunuz gönderilirken hata oluştu.</span>");
		}
	});
}

function add_writer_comment(arg)
{
	id = "#addcomment"; // ajax response id
	
	$j.ajax({
		type: "POST", // method = GET or POST
		cache: true, // cache = true or false
		url: "module/comment/ajax_writer_comment.php", // ajax include file
		data: arg, // method parameters
		beforeSend: function(){ajax_loading(id, 20, 'center');},
		success: function(msg) {
			/* ajax response */
			$j(id).html(msg);
			
			$j(id).ajaxStop(function(){
				if($j("#send_status").val() == '31')
					window.location = 'comment_send.html';
			});
		},
		/* ajax error */
		error: function(oXHR, status) {
			$j(id).html("<span class=\"error\">Yorumunuz gönderilirken hata oluştu.</span>");
		}
	});
}

function reader_edit(arg)
{
	id = "#reader_edit"; // ajax response id
	
	$j.ajax({
		type: "POST", // method = GET or POST
		cache: true, // cache = true or false
		url: "module/login/ajax_reader_edit.php", // ajax include file
		data: arg, // method parameters
		beforeSend: function(){ajax_loading(id, 0, 'left');},
		success: function(msg) {
			/* ajax response */
			$j(id).html(msg);
		},
		/* ajax error */
		error: function(oXHR, status) {
			$j(id).html("<span class=\"error\">Kayıt işlemi sırasında hata oluştu. Daha sonra tekrar deneyiniz.</span>");
		}
	});
}

function contact_send(arg)
{
	id = "#get_contact"; // ajax response id
	
	$j.ajax({
		type: "POST", // method = GET or POST
		cache: true, // cache = true or false
		url: "iletisim.asp", // ajax include file
		data: arg, // method parameters
		beforeSend: function(){ajax_loading(id, 0, 'left');},
		success: function(msg) {
			/* ajax response */
			$j(id).html(msg);
		},
		/* ajax error */
		error: function(oXHR, status) {
			$j(id).html("<span class=\"error\">Kayıt işlemi sırasında hata oluştu. Daha sonra tekrar deneyiniz.</span>");
		}
	});
}

function add_reader(arg)
{
	id = "#addreader"; // ajax response id
	
	$j.ajax({
		type: "POST", // method = GET or POST
		cache: false, // cache = true or false
		url: "module/reader/ajax_reader.php", // ajax include file
		data: arg, // method parameters
		beforeSend: function(){ajax_loading(id, 20, 'center');},
		success: function(msg) {
			/* ajax response */
			$j(id).html(msg);
			
			$j(id).ajaxStop(function(){
				if($j("#status").val() == '1')
					window.location = 'signup.php?status=true';
			});
			
		},
		/* ajax error */
		error: function(oXHR, status) {
			$j(id).html("<span class=\"error\">Kayıt işlemi sırasında hata oluştu. Daha sonra tekrar deneyiniz.</span>");
		}
	});
}

function add_feedback(arg)
{
	id = "#addfeedback"; // ajax response id
	
	$j.ajax({
		type: "POST", // method = GET or POST
		cache: true, // cache = true or false
		url: "module/reader/ajax_feedback.php", // ajax include file
		data: arg, // method parameters
		beforeSend: function(){ajax_loading(id, 0, 'left');},
		success: function(msg) {
			/* ajax response */
			$j(id).html(msg);
		},
		/* ajax error */
		error: function(oXHR, status) {
			$j(id).html("<span class=\"error\">Kayıt işlemi sırasında hata oluştu. Daha sonra tekrar deneyiniz.</span>");
		}
	});
}

function password_edit()
{
	id = "#password_edit"; // ajax response id
	
	$j.ajax({
		type: "POST", // method = GET or POST
		cache: true, // cache = true or false
		url: "module/login/ajax_password_edit.php", // ajax include file
		data: arg, // method parameters
		beforeSend: function(){ajax_loading(id, 0, 'left');},
		success: function(msg) {
			/* ajax response */
			$j(id).html(msg);
		},
		/* ajax error */
		error: function(oXHR, status) {
			$j(id).html("<span class=\"error\">Kayıt işlemi sırasında hata oluştu. Daha sonra tekrar deneyiniz.</span>");
		}
	});
}

function password_send(arg)
{
	id = "#password_send"; // ajax response id
	
	$j.ajax({
		type: "POST", // method = GET or POST
		cache: true, // cache = true or false
		url: "module/login/ajax_password_send.php", // ajax include file
		data: arg, // method parameters
		beforeSend: function(){ajax_loading(id, 0, 'left');},
		success: function(msg) {
			/* ajax response */
			$j(id).html(msg);
		},
		/* ajax error */
		error: function(oXHR, status) {
			$j(id).html("<span class=\"error\">Kayıt işlemi sırasında hata oluştu. Daha sonra tekrar deneyiniz.</span>");
		}
	});
}

function sendfriend(arg)
{
    id = "#sendfriend"; // ajax response id
    
    $j.ajax({
        type: "POST", // method = GET or POST
        cache: true, // cache = true or false
        url: "module/news/ajax_send_friend.php", // ajax include file
        data: arg, // method parameters
        beforeSend: function(){$j(id).html("<tr class=\"normal\"><td colspan=\"9\"><img src=\"data/template/ajax_loading.gif\"></td></tr>");},
        success: function(msg) {
            //ajax response
            $j(id).html(msg);
            //league_table();
        },
        // ajax error
        error: function(oXHR, status) {
            $j(id).html("<span class=\"error\">Gönderme işlemi sırasında hata oluştu. Daha sonra tekrar deneyiniz.</span>");
        }
    });
}

function sendfriend_writer(arg)
{
    id = "#sendfriend"; // ajax response id
    
    $j.ajax({
        type: "POST", // method = GET or POST
        cache: true, // cache = true or false
        url: "module/writer/ajax_send_friend.php", // ajax include file
        data: arg, // method parameters
        beforeSend: function(){$j(id).html("<tr class=\"normal\"><td colspan=\"9\"><img src=\"data/template/ajax_loading.gif\"></td></tr>");},
        success: function(msg) {
            //ajax response
            $j(id).html(msg);
            //league_table();
        },
        // ajax error
        error: function(oXHR, status) {
            $j(id).html("<span class=\"error\">Gönderme işlemi sırasında hata oluştu. Daha sonra tekrar deneyiniz.</span>");
        }
    });
}

//---------------------------------------------------------------------------------------------

