function getObj(objId)
{
	if (document.getElementById)
		return document.getElementById(objId);
	else if (document.all)
	    return document.all[objId];
	else if (document.layers)
	    return document.layers[objId];
	else return false;
}

// AJAX definition........................
function getHTTPObject() 
{
	var httprequest=false;

	if (window.XMLHttpRequest) //  if Mozilla, Safari etc
	{
		httprequest=new XMLHttpRequest()
		if (httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml')
	}
	else if (window.ActiveXObject)
	{ // if IE
		try {
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e)
		{
			try
			{
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	
	return httprequest;
}

var ajax_object=new Object();
//ajax_object.basedomain="http://"+window.location.hostname;
ajax_object.httpreq=getHTTPObject();
ajax_object.addrandomnumber=0; 


function inner_state() 
{
	var _ajax=ajax_object.httpreq;

	if(_ajax.readyState == 1)
	{
		if(ajax_object.tagId != '')
		getObj(ajax_object.tagId).innerHTML = ajax_object.msg;
//		getObj(ajax_object.tagId).innerHTML = '&nbsp;&nbsp;<img src="lifa_net/images/spinner.gif">'+ajax_object.msg;
	}
	else if(_ajax.readyState == 4)
	{ 
		var answer = _ajax.responseText;
		
		if(answer.search(/redirect:/) != -1)
		{
			var redirect=answer.split("redirect:")[1];
			top.location.href=redirect;
		}
		else if(ajax_object.tagId != '')
			getObj(ajax_object.tagId).innerHTML = answer;
	}
}

ajax_object.getAjaxRequest=function(url, parameters, callbackfunc,tagId,msg)
{

	ajax_object.httpreq=getHTTPObject();
	ajax_object.tagId=tagId;
	ajax_object.msg=msg;
	
	if (ajax_object.addrandomnumber==1) 
		var parameters=parameters+"&ajaxcachebust="+new Date().getTime();

		if (this.httpreq)
		{
			this.httpreq.onreadystatechange=callbackfunc;
			this.httpreq.open('GET', url+"?"+parameters, true);
			this.httpreq.send(null);
		}
}

ajax_object.postAjaxRequest=function(url, parameters, callbackfunc,tagId,msg)
{
	ajax_object.httpreq=getHTTPObject();
	ajax_object.tagId=tagId;
	ajax_object.msg=msg;

	if (this.httpreq)
	{
		this.httpreq.onreadystatechange = callbackfunc;
		this.httpreq.open('POST', url, true);
		this.httpreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=windows-1254");
		this.httpreq.setRequestHeader("Content-length", parameters.length);
		this.httpreq.setRequestHeader("Connection", "close");
		this.httpreq.send(parameters);
	}
}
// -------------------------
ajax_object.AjaxRequest=function(method,url, parameters, callbackfunc)
{
	ajax_object.httpreq=getHTTPObject();
	ajax_object.tagId='';
	ajax_object.msg='';

	if (this.httpreq)
	{
	if (ajax_object.addrandomnumber==1) 
		var parameters=parameters+"&ajaxcachebust="+new Date().getTime();

		this.httpreq.onreadystatechange = callbackfunc;

		this.httpreq.open(method, url, true);
		
		if(method == 'POST'){
			this.httpreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=windows-1254");
			this.httpreq.setRequestHeader("Content-length", parameters.length);
			this.httpreq.setRequestHeader("Connection", "close");
			this.httpreq.send(parameters);
		}
		else
			this.httpreq.send(null);
	}	
}
 
 // =================================== Ajax end ===========================================================================
var error_email_msg='??? e-mail';
var popWindow;

// Mail ===============================================
function isEmail(who) {
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	return(email.test(who));
}


function check_email(email)
{
	if(isEmail(email.value) == false)
	{
		email.value=error_email_msg;
		email.focus();
		return false;
	}
	return true;
}


function check_requiredinput(frm)
{
	prefix='chk_';

	var input_count=(frm.all.tags("INPUT"))?frm.all.tags("INPUT").length:0;
	input_count+=(frm.all.tags("TEXTAREA"))?frm.all.tags("TEXTAREA").length:0;
	input_count+=(frm.all.tags("SELECT"))?frm.all.tags("SELECT").length:0;

	var re='/'+prefix+'/i';
	var email=/email/;

	var first_input=null;

	this.alertfunc=function(input)
	{
		if(input.name.match(eval(email)))
		{
			if (!check_email(input))
			{
				if(!first_input) first_input=input;
				return false;
			}
			
			return true;
		}
		else if(input.name.match(eval(re)) && ((input.value == '') || (input.value == '???')))
		{
			alert_str='???';

			input.value=alert_str;

			if(!first_input) first_input=input;
			return false;
		}

		return true;
	}

	var retval=true;
	
	for (var i=0; i < input_count;i++)
		if(frm.all.tags("INPUT")[i]) 
			retval =retval && this.alertfunc(frm.all.tags("INPUT")[i]);

	for (var i=0; i < input_count;i++)
		if(frm.all.tags("TEXTAREA")[i])
			retval =retval && this.alertfunc(frm.all.tags("TEXTAREA")[i]);

	if(!retval) first_input.focus();

	return retval;
}

// ============================ Field kontrol end =============================================================

// ============================= chekcklerin hepsinin kontrolu ================================================
function sel_allchk(ischekced,prefix)
{
	if(!prefix) prefix='sel_';

	var input_count=document.all.tags("INPUT").length;

	var re='/'+prefix+'/i';

	for (var i=0; i < input_count;i++)
	{
		window.status='count: '+input_count+ ' / '+i;
		if(document.all.tags("INPUT")[i].type == 'checkbox')
		{
			var new_str=new String(document.all.tags("INPUT")[i].name);

			if(new_str.match(eval(re)))
				document.all.tags("INPUT")[i].checked=ischekced;
		}
	}

	window.status='';
}

// ============================= form daki verilerin düzenlenmesi ================================================


function init_fields(frm,data)
{
	this.text_value=function(obj)
	{
		if(data[obj.name])
		{
			obj.value=data[obj.name];
		}
	}

	this.check_value=function(obj)
	{
		if(data[obj.name])
		{
			obj.checked=true;
		}
	}

	this.radio_value=function(obj)
	{
		if(data[obj.name])
		{
			obj.checked=(obj.value == data[obj.name]);
		}
	}

	this.select_value=function(obj)
	{
		if(data[obj.name])
		{
			for (var i = 0; i < obj.options.length; i++) 
				if(obj.options[i].value == data[obj.name])
					obj.options[i].selected=true;
		}
	}

	this.textarea_value=function(obj)
	{
		if(data[obj.name])
		{
			obj.value=data[obj.name];
		}
	}

	var input_count=(frm.all.tags("INPUT"))?frm.all.tags("INPUT").length:0;
	input_count+=(frm.all.tags("TEXTAREA"))?frm.all.tags("TEXTAREA").length:0;
	input_count+=(frm.all.tags("SELECT"))?frm.all.tags("SELECT").length:0;

	// Dikkat : Bu tür ayrık döngüler bilerek yapılmıştır. !!!!!!!!!!!!!!!!!
	for (var i=0; i < input_count;i++)
	{		
		if(frm.all.tags("INPUT") && frm.all.tags("INPUT")[i])
		{
			if(frm.all.tags("INPUT")[i].type == 'text') 
			{
				this.text_value(frm.all.tags("INPUT")[i]);
			}
			else if(frm.all.tags("INPUT")[i].type == 'checkbox') 
			{
				this.check_value(frm.all.tags("INPUT")[i]);
			}
			else if(frm.all.tags("INPUT")[i].type == 'radio') 
			{
				this.radio_value(frm.all.tags("INPUT")[i]);
			}
		}
	}

	for (var i=0; i < input_count;i++)
	{		
		if(frm.all.tags("SELECT") && frm.all.tags("SELECT")[i])
		{
			this.select_value(frm.all.tags("SELECT")[i]);
		}
	}

	for (var i=0; i < input_count;i++)
	{		
		if(frm.all.tags("TEXTAREA") && frm.all.tags("TEXTAREA")[i])
		{
			this.textarea_value(frm.all.tags("TEXTAREA")[i]);
		}
	}
}

// =======================================
function ctrl_buttons(sender)
{
	var btn=getObj('btnDelete');
	if(btn)
		btn.disabled=true;
	
	var btn=getObj('btnNew');
	if(btn)
		btn.disabled=true;

	var btn=getObj('btnUpdate');
	if(btn)
		btn.disabled=true;

	var btn=getObj('btnRelease');
	btn.value=sender.name;

	sender.form.submit();
}

function openNewWindow(sURL, sName, iWidth, iHeight, titlebar,bResizable, bScrollbars,iLeft,iTop)
{
	if(!iWidth)
		iWidth=screen.height / 2;

	if(!iHeight)
		iHeight=screen.height / 2;

	if(!iTop)
		var iTop  = (screen.height - iHeight) / 2 ;	
	if(!iLeft)
		var iLeft = (screen.width  - iWidth) / 2 ;


	var sOptions = "toolbar=no" ;	
	sOptions += ",width=" + iWidth ; 
	sOptions += ",height=" + iHeight ;	
	sOptions += ",resizable="  + (!bResizable ? "no" : bResizable) ;
	sOptions += ",scrollbars=" + (!bScrollbars ? "no" : bScrollbars) ;
	sOptions += ",left=" + iLeft ;
	sOptions += ",top=" + iTop ;
	sOptions += ",titlebar=" + (!titlebar ? "no" : titlebar) ;


	return  window.open(sURL, sName, sOptions);
}

function show_popup(theURL,features) 
{
	if(popWindow)
		popWindow.close();

	popWindow=window.open(theURL,"popupwin",features);
	popWindow.focus();
}

function popup_returnmsg(sender,title,filename,file)
{
	if(popWindow)
		popWindow.close();

	popWindow = openNewWindow("index.php?popupfile="+file+"&filename="+filename+"&title="+title+"&"+sender.url, 
		"returnmsg",800, 650) ;

	popWindow.focus();
}

function edit_fotoimgs(sender,id,_class,_classfile) 
{
	if(popWindow)
		popWindow.close();

	popWindow = openNewWindow("index.php?popupfile=popbrwsimg.php&class="+_class+'&classfile='+_classfile+
		"&func=update_img&id="+id+"&path="+sender.filepath+"&senderid="+sender.id+
		"&title=Resim Duzenleme",
		"UploadWin",760, 560) ;
	popWindow.focus();
}

function cursor_shape(shape)
{
	document.body.style.cursor=shape;
}

function redirect(newpage)
{
	window.location=newpage;
}

function change_reccount(sender,page)
{
	redirect(page+'&'+sender.name+'='+sender.value);
}

// regular expression kullanılıyor param_name de
// /&page=\w+/ gibi
function change_urlparam(values,param_name,param_value)
{
	var str=new String(values);

	if(str.match(param_name)){
		str=str.replace(param_name,param_value);
	}
	else if(param_value != "")
	{
		str+=param_value;
	}

	return str;
}

function ftimage_show(filepath,img_width,img_height)
{
	if(popWindow)
		popWindow.close();

	if(!img_width)
		img_width=400;
	if(!img_height)
		img_height=300;

	popWindow=window.open("","ProductImage","toolbar=0, resizable=no, ,width="+img_width+",height="+img_height);
	popWindow.document.write('<head>');
	popWindow.document.write("<title>Resim</title>");
	popWindow.document.write('</head>');
	popWindow.document.write('<body id="popup" style="margin:0;"><center><img src="'+filepath+'" width="100%" height="100%" /></center>');//width="100%" height="100%"
	popWindow.document.write('</body>');
	popWindow.focus();
}

// options elemtinin doldurulması
// options parametresi array[]array[0..1] şeklindedir.0 : value, 1 : text değerleridir.
function selection_elem(_selection,_options,selectedvalue)
{

	this.select=_selection;

		this.add_option=function(value,text){
			var option=document.createElement('OPTION');
			option.value=value;
			option.text=text;
			this.select.options.add(option);
		}

		this.delete_option=function(index){
			this.select.options[index]=null;
		}

		this.clear_options=function(){
			for(var i=this.select.options.length-1; i >=1; i--)
				this.delete_option(i);
		}


	this.clear_options();

	if(_options)
		for(var i=0; i < _options.length;i++)
			this.add_option(_options[i][0],_options[i][1]);

	this.select.value=selectedvalue;
}

var img_window=null;

function close_imagewindow()
{
	if (img_window)
		img_window.close();
}

function image_show(filepath,title)
{
	close_imagewindow();

	img_window=window.open("","ProductImage","toolbar=0, resizable=yes,width=450, "+
			"height=400");
	img_window.document.write('<head>');
	img_window.document.write("<title>"+title+"</title>");
	img_window.document.write('</head>');
	img_window.document.write('<body id="popup" style="margin:0;"><img src="'+filepath+'" width="100%" height="100%" >');
	img_window.document.write('</body>');
}

function image_pop(sender,title)
{
	image_show(sender.src,title);
}