// Popup olarak yeni ekleme islemi
// -------------------------------------------------------------------------------------------
function yeni_ekle(sayfa, field_name)
{
	window.open("add" + server_extention + "?id=" + sayfa + "&field_name=" + field_name, "ekleme", "height=700,width=700,scrollBars=1");
}

// Select kontrolune yeni ekleme islemi
// -------------------------------------------------------------------------------------------
function option_ekle(id, text, value)
{
	var obj = document.getElementById(id);
	var opt = document.createElement('OPTION');
	opt.value = value;
	opt.text = text;
	
	obj.options.add(opt, obj.options.length);
}

// Para ve Float Islemleri
// -------------------------------------------------------------------------------------------
function money_control(sender)
{
	sender.value = formatCurrency(sender.value);
}

function float_control(sender)
{
	sender.value = formatFloat(sender.value);
}

function int_control(sender)
{
	sender.value = formatInt(sender.value);
}

function formatCurrency(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}

function formatFloat(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	// num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}

function formatInt(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	// num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num);
}

// Form Kontrolu
// -------------------------------------------------------------------------------------------
function form_control(emailControl, email_field)
{
	for (i=0 ; i < document.form_edit.elements.length ; i++)
	{		
		if (document.form_edit.elements[i].title != null && document.form_edit.elements[i].title != "" && (document.form_edit.elements[i].value == null || document.form_edit.elements[i].value == ""))
		{			
			alert(document.form_edit.elements[i].title);
			document.form_edit.elements[i].focus();
			return false;
		}
	}
	
	if (emailControl)
	{
		var email = document.getElementById(email_field);
		if (!isEMailAddr(email))
		{
			alert("E-mail Adresi Gecerli Degil");
			return false;
		}
		
	}
	
	document.form_edit.submit();
}

function isEMailAddr(elem) 
{
    var str = elem.value;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) 
	{
        return false;
    } else {
        return true;
    }
}

// Urun Islemleri 
function urun_islem(urun_tip, urun_id, urun_ad, ust_id, ust_ad, sira, islem)
{
	document.getElementById("div_urunislem").style.display = "";
	
	if (ust_id == 0)
	{
		document.getElementById("urun_ustklasor").innerText = "Ana Klasör";
	} else {
		document.getElementById("urun_ustklasor").innerText = ust_ad;
	}
	
	if (urun_id == 0)
	{
		document.getElementById("urun_ad").value = "";
	} else {
		document.getElementById("urun_ad").value = urun_ad;
	}
	
	document.getElementById("islem").value = islem;
	document.getElementById("urun_tip").value = urun_tip;
	document.getElementById("sira").value = sira;
	document.getElementById("urun_id").value = urun_id;
	document.getElementById("ust_id").value = ust_id;

	document.getElementById("baslik").innerText = urun_tip + " İşlemleri";
}

function urun_islem_ok()
{
	var query = "&urun_tip=" + document.getElementById("urun_tip").value +
				"&islem=" + document.getElementById("islem").value +
				"&sira=" + document.getElementById("sira").value +
				"&urun_id=" + document.getElementById("urun_id").value +
				"&urun_ust_id=" + document.getElementById("ust_id").value +
				"&urun_ad=" + document.getElementById("urun_ad").value;
					
	phantom_setContent('sub_0', 'phantom.php?type=get_urunler&ust_id=0' + query, '', 'GET', true );
	
	getElemID('div_urunislem').style.display = 'none';
}

function getUrunler(id, derece)
{
	phantom_setContent('sub_' + id, 'phantom.php?type=get_urunler&ust_id=' + id + '&derece=' + derece, '', 'GET', true );
}