function yaz(str){document.write(str);}
function yazln(str){ document.write(str+"<br/>\n");}

function get(id){return document.getElementById(id);}

function hide(obj){obj.style.display = 'none';}
function show(obj){obj.style.display = '';}
function is_hidden(obj){return obj.style.display == "none";}
function hideShow(obj){	is_hidden(obj)?show(obj):hide(obj);}

function disable(obj){obj.disabled = true;}
function enable(obj){obj.disabled = false;}
function is_disabled(obj){return obj.disabled;}
function enableDisable(obj){is_disabled(obj)?enable(obj):disable(obj);}

function numLines(str){return str.split("\n").length;}
function numParenthesis(str){return str.split('(').length-1;}

// START: get method param implementation
var queryHash = null;
function param(key){
	if( queryHash == null ){
		//yazln("queryHash:null");
		var str = location.href;
		var parts = str.split("?");
		var query = "";
		if( parts.length > 1 ){
			query = parts[1];
		}
		//alert(query);
		var arr_key_value = query.split("&");
		queryHash = new Array();
		for(var i=0; i<arr_key_value.length; i++){
			//yaz(arr_key_value[i]);
			var key_value = arr_key_value[i].split("=");
			queryHash[key_value[0]] = key_value[1];
		}
	}
	return queryHash[key];
}
// END: get method param implementation


// START: top 10 useful functions
//10              body  load  alert()  
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}
/*example:
addEvent(window,'load',func1,false);  // load means onload
addEvent(window,'load',func2,false);
addEvent(window,'load',func3,false);
*/
//9
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
//8
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
//7 licenced, not published
//6 toggle: i have better
/*
function toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}*/
//5
function insertAfter(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
}


// standard array functions:
/*
Function	Description																JS		J
concat	Add one or more arrays, and return the new array. No array is modified.		1.2		3
copy	Copy all elements to a new array, by reference if possible, or by value.		
pop		Remove and return the last element of an array.								1.2		5
push	Add one or more elements to the end of an array, return the new length.		1.2		5.5
shift	Remove and return the first element.										1.2		5.5
slice	Copy and return several elements. The original array is not modified.		1.2		3
splice	Remove or replace several elements and return any deleted elements			1.2		5.5
unshift	Add an element to the beginning of an array, return the new length.			1.2		5.5

*/


// w3schools'dan alinti cookie fonksiyonlari:
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

function getCookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){ 
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return null;
}

function deleteCookie(c_name){
	setCookie(c_name,'',-1);
}

/*
// Sample Usage:
var obj1 = document.getElementById('element1');
var obj2 = document.getElementById('element2');
function alertElements() {
  var i;
  var elements = $('a','b','c',obj1,obj2,'d','e');
  for ( i=0;i*/
 // END: top 10 useful functions
 
 
// START: ajax manager:
// gets a url via ajax calls:
// NOTE: load edilen sayfadaki javascript'ler execute edilmiyor
function AJAXManager(){
	//var pageReady = false;
	var xmlHttp;
	var containerObj;
	
	// senkron:
	this.getUrl = getUrl;
	function getUrl(url) {
		AJAX = GetXmlHttpObject();
		if(AJAX){
			AJAX.open("GET", url, false);                             
			AJAX.send(null);
			return AJAX.responseText;                                         
		}
		else{
			return false;
		}                                             
	}
	
	
	// asenkron:
	this.loadUrl = loadUrl;
	function loadUrl(url,container){
		containerObj = container;
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){
			alert ("Browser does not support HTTP Request");
			return;
		}		
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);		
	}
	
	function GetXmlHttpObject(){ // TODO: bunu sadece page load oldugunda yaratmak yeterli olabilir
		var objXMLHttp=null
		if (window.XMLHttpRequest){
			objXMLHttp=new XMLHttpRequest()
		}
		else if (window.ActiveXObject){
			objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
		}
		return objXMLHttp
	}
		
	function stateChanged(container){ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			// debug:
			//alert(xmlHttp.responseText);
			containerObj.innerHTML = xmlHttp.responseText;
		}
	}
}
// AJAX kullanilacaksa bunu uncomment et:
var am = new AJAXManager();
// kullanilisi:
// non blocking: am.loadUrl("den.html",get('container'));  // id'si container olan bir div'in icine den.html'yi load eder
// blocking: var str = am.getUrl("den.html");  // blocking bir call yapar, sayfanin source'unu str'ye esitler!!
// dipnot: dizinden direk acinca calismiyor, sadece local server veya internetten calisiyor
// END: ajax manager


// array functions (added to prototype):

// Array.forEach( function ) - Apply a function to each element
Array.prototype.forEach = function( f ) {
 var i = this.length, j, l = this.length;
 for( i=0; i<l; i++ ) { if( ( j = this[i] ) ) { f( j ); } }
};

// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
Array.prototype.indexOf = function( v, b, s ) {
 for( var i = +b || 0, l = this.length; i < l; i++ ) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.insert( index, value ) - Insert value at index, without overwriting existing keys
Array.prototype.insert = function( i, v ) {
 if( i>=0 ) {
  var a = this.slice(), b = a.splice( i );
  a[i] = v;
  return a.concat( b );
 }
};

// Array.lastIndexOf( value, begin, strict ) - Return index of the last element that matches value
Array.prototype.lastIndexOf = function( v, b, s ) {
 b = +b || 0;
 var i = this.length;
 while(i-- > b) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// Array.random( range ) - Return a random element, optionally up to or from range
Array.prototype.random = function( r ) {
 var i = 0, l = this.length;
 if( !r ) { r = this.length; }
 else if( r > 0 ) { r = r % l; }
 else { i = r; r = l + r % l; }
 return this[ Math.floor( r * Math.random() - i ) ];
};

// Array.shuffle( deep ) - Randomly interchange elements
Array.prototype.shuffle = function( b ) {
 var i = this.length, j, t;
 while( i ) {
  j = Math.floor( ( i-- ) * Math.random() );
  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  this[i] = this[j];
  this[j] = t;
 }
 return this;
};

// Array.unique( strict ) - Remove duplicate values
Array.prototype.unique = function( b ) {
 var a = [], i, l = this.length;
 for( i=0; i<l; i++ ) {
  if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
 }
 return a;
};

// Array.walk() - Change each value according to a callback function
Array.prototype.walk = function( f ) {
 var a = [], i = this.length;
 while(i--) { a.push( f( this[i] ) ); }
 return a.reverse();
};


//4   added to array prototype:
Array.prototype.inArray = function (value) {
	return this.indexOf(value) >=0;
};

// added by daghan: array difference (array_diff in php)
Array.prototype.arrayDiff = function (arr) {
	var i;
	var resultArr=[];
	for (i=0; i < this.length; i++) {
		if (!arr.inArray(this[i])) {
			resultArr.push(this[i]);
		}
	}
	return resultArr;
};

// daghan added:  uses square-and-multiply type algorithm
String.prototype.repeat = function(count){
	if(count==0) return '';
	var b = count.toString(2).split("");		
	var resultStr='';
	for(var i=0; i<b.length; i++){
		resultStr+=resultStr;// for both 0 and 1
		if(b[i] == '1'){
			resultStr+=this;
		}
	}
	return resultStr;
};

function serialize(object){
	var str='';
	for(i in object){
		str += i+':'+object[i]+' ';
	}
	return str;
}

/*
// did not work correctly
function serializeRecursive(object,optIndent,visited){
	if(!optIndent) optIndent = '';
	if(!visited) visited = [];
	var str='';
	visited.push(object);
	
	for(i in object){	
		str += optIndent+i+':'+object[i]+'\n';
		//alert(object[i]);
		if( !visited.inArray(object[i]) ){
			try{
				str += serializeRecursive(object[i] , optIndent+'  ',visited);
			}catch(e){
			}
		}
		//object[i];
	}
	return str;
}*/

/*
function diagnose(msg){
	var diagnoseBox = document.createElement("div");		
	diagnoseBox.innerText=msg;
	diagnoseBox.style.backgroundColor='yellow';
	diagnoseBox.style.border='1px solid black';
	diagnoseBox.style.position='absolute';
	diagnoseBox.style.z_index='1';
	diagnoseBox.style.top = (0+30+document.body.scrollTop)+"px";
	diagnoseBox.style.left = (0+200+document.body.scrollLeft)+"px";
	diagnoseBox.onclick=function(){document.body.removeChild(this);};
	document.body.appendChild(diagnoseBox);
}*/


function inArray(arr,val){
	for(var i=0;i<arr.length;i++){
		if(arr[i] == val){
			return true;
		}
	}
	return false;
}



// include javascript or css dynamically
//<script language="javascript" src="javascript.js"></script>
function include(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

//<link href= "style.css" rel="stylesheet" type="text/css"/>
function include_css(stylesheet_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var css = document.createElement('link');    
	css.setAttribute('rel', 'stylesheet');
    css.setAttribute('type', 'text/css');
    css.setAttribute('href', stylesheet_filename);
    html_doc.appendChild(css);
    return false;
}

// used for ensuring of reporting missing classes or functions
// not used currently
function require(function_or_class_name){
	if(typeof(function_or_class_name) != 'function'){
		alert('Error: '+function_or_class_name+' not found');
	}
}

// used for <br> typed rich code editing instead of <p> type
// not used currently due to stability issues (<p>'s are used)
function htmlEntities(someInnerText){
	//alert(someInnerText);
	someInnerText = someInnerText.replace(/&/g,'&amp;');
	someInnerText = someInnerText.replace(/ /g,'&nbsp;');
	someInnerText = someInnerText.replace(/>/g,'&gt;');
	someInnerText = someInnerText.replace(/</g,'&lt;');
	someInnerText = someInnerText.replace(/(&nbsp;)?\r\n/g,'\r\n<BR/>');
	//alert(someInnerText);
	return someInnerText;
}

