// JavaScript Document

String.prototype.trim = function() {
	return this.replace(/^\s+/, '').replace(/\s+$/, '');	
}

function nl2br(str) {
	str = str.replace(/\n/g, '<br />');
	return str;
}

function br2nl(str) {
	str = str.replace(/<br \/>/g, "\n");
	str = str.replace(/<br>/g, "\n");
	return str;
}

// filater values from the queery string
function filterQueryString(url, key) {
	var start;
	var end;
	if(url.indexOf(key+'=') != -1) {
		start = url.indexOf(key+'=');
		if(url.indexOf('&', start)  != -1) {
			end = url.indexOf('&', start);
		} else {
			end = url.length;	
		}
		
		var sStr = url.substring(start, end);
		var nUrl = url.replace(sStr, '');
		nUrl = nUrl.replace('?&', '?');
		nUrl = nUrl.replace('&&', '&');
		return nUrl;
	} else {
		return url;	
	}
}

function setRollOvers() {
	var e = document.getElementsByTagName('img');
	if(e.length != 0) {
		for(var i=0; i < e.length; i++) {			
			if(e[i].className == 'rollover') {
				e[i].onmouseover = function() {
					var img = this.src;
					img = img.replace(/.jpg/g, '_hover.jpg');
					this.src=img;
				}
				e[i].onmouseout = function() {
					var img = this.src;
					img = img.replace(/_hover.jpg/g, '.jpg');
					this.src=img;
				}
			}
		}
	}
}

function isEmptyObject(obj) {
	if(obj != null) {
		for(var key in obj) {
			return false;
		}
		return true;
	} else {
		return true;	
	}
}


	Array.prototype.search = function(keyword) {
		var flag = 0;
		for(key in this) {
			if(this[key] == keyword) {
				flag = 1;
				break;
			}
		}
		if(flag == 1) {
			return true;
		} else {
			return false;	
		}
	}

function get_random_number() {
	var ran = Math.random();
	ran = ran * 10000;
	ran = Math.ceil(ran);
	return ran;
}

function page_name() {
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	return sPage;
}

function pageName() {
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	return sPage;
}


function trim(str) {
	return str.replace(/^\s+/, '').replace(/\s+$/, '');	
}


Math.diff = function(val1, val2) {
	if(val1 < 1) {
		val1 = val1 * -1;
	}
	if(val2 < 1) {
		val2 = val2 * -1;	
	}
	var min_val = Math.min(val1, val2);
	var max_val = Math.max(val1, val2);
	var difference = max_val - min_val;
	//alert(difference);
	return difference;
}

// PASS THE OBJECT FOR THE SEARCH TEXT AND NOT THE VALUE
function submit_search(search_text, search_page) {
	var query = search_text.value.trim();
	query = query.replace(/ /g, '+');
	window.location = search_page+'?q='+query;
	return false;
}

function submit_search1(search_text, search_page) {
	var query = search_text.value.trim();
	query = query.replace(/ /g, '+');
	window.location = search_page+'?q='+query+'&category_id='+document.form1.category_id.value;
	return false;
}

function confirmDelete(delUrl) {
  if (confirm("Are you sure you want to delete this record...?")) {
    document.location = delUrl;
  }
}

function popup(url) {
	window.open(url, 'my_window');	
}

function get_window_size() {
  var myWidth = 0, myHeight = 0;
  var size = new Array();
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
/*  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
*/  
	size['width'] = myWidth;
  	size['height'] = myHeight;
 	return size;
}

function get_center_position(size) {
	size = size.replace(/px/g, '');
	if(size == '' || size == null) {
		alert('Please provide a width value of the popup element to get the center position');
		return false;
	}
	var win_size = get_window_size();
	var win_width = win_size['width'];
	var max_val = Math.max(win_width, size);
	var min_val = Math.min(win_width, size);
	var diff = max_val - min_val;
	var final = Math.round(diff/2);
	return final+'px';
}

//need to paass the event for this function, example
// element.onclick = function(e) { c  = getMoutseXY(e) };
function getMouseXY(e) {
	var tempX = 0
	var tempY = 0

	var IE = document.all?true:false
	
	// If NS -- that is, !IE -- then set up for mouse capture
	if (!IE) document.captureEvents(Event.MOUSEMOVE)
	
	if (IE) { // grab the x-y pos.s if browser is IE
	tempX = event.clientX + document.body.scrollLeft
	tempY = event.clientY + document.body.scrollTop
	} else {  // grab the x-y pos.s if browser is NS
	tempX = e.pageX
	tempY = e.pageY
	}  
	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}  
	// show the position values in the form named Show
	// in the text fields named MouseX and MouseY
	
	var mouse = new Array();
	mouse['x'] = tempX;
	mouse['y'] = tempY;
	return mouse;
	//  document.Show.MouseX.value = tempX
	// document.Show.MouseY.value = tempY
	//  return true
}

function populateInMultilist(values, toListBox, fromListBox) {
	moveAllOptions(document.getElementById(toListBox), document.getElementById(fromListBox),true,'');
	selected_list = values.split(',');
	parent_list = document.getElementById(fromListBox);
	for(u=0; u < parent_list.length; u++) {
		if(selected_list.indexOf(parent_list[u].value) != -1) {
			parent_list[u].selected = true;
		}
	}
	moveSelectedOptions(parent_list, document.getElementById(toListBox),true,'');
}

function file_type(file_name) {
	var dot = file_name.lastIndexOf('.');
	if(dot != -1) {
		dot++;
		return file_name.substr(dot, 4);
	}
}

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 "";
}

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 addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// FILES MANAGER, LIST FILES, FILES php - listDirFiles()

function processFilesOnClick(flag, file, callOnClick, element, mainPath, subPath) {
	//alert(flag+'  '+file+'  '+callOnClick+'  '+element+'  '+mainPath+'  '+subPath);
	if(flag == 'F') {
		var val = callOnClick+'("'+file+'")';
		try {
			eval(val);
		} catch(err)  {
			alert("callOnClick function does not exist, please create a valid callOnClick function");	
		}
	} else if(flag == 'D') {
		subPath = file;
		var url = pageName()+'?mp='+mainPath+'&sp='+subPath+'&coc='+callOnClick+'&e='+element+'&method=ajax&action=getFiles';
		//alert(url);
		var a = new jajax(url, element);
		a.onSuccess = function() {
			setClickEventOnFilesLoad(element, callOnClick, mainPath, subPath);	
		}
		a.get();
	} else if(flag == 'U') {
		subPath = subPath.substring(0, subPath.lastIndexOf('/'));		
		//alert(subPath);
		var url = pageName()+'?mp='+mainPath+'&sp='+subPath+'&coc='+callOnClick+'&e='+element+'&method=ajax&action=getFiles';
		//alert(url);
		var a = new jajax(url, element);
		
		a.onSuccess = function() {
			setClickEventOnFilesLoad(element, callOnClick, mainPath, subPath);	
		}
		a.get();
	}
}

function queryString() {
	var url = parent.document.URL;
	var queryString = url.substr(url.indexOf('?')+1);
	var param = new Array();
	queryString = queryString.split('&');
	if(queryString.length != 0) {
		var i;
		for(i=0; i < queryString.length; i++) {
			var values = new Array();
			if(queryString[i] != '') {
				values = queryString[i].split('=');
				param[values[0]] = values[1];
			}
			
		}
	}
	return param;
}

function setClickEventOnFilesLoad(element, callOnClick, mainPath, subPath) {
	//alert("sdsd");
	var links = document.getElementById(element).getElementsByTagName('a');
	for(var i = 0; i < links.length; i++) {
		var classFullName;
		links[i].onclick = function() {
			flag = this.className.substr(0, 1);
			file = this.title;			
			processFilesOnClick(flag, file, callOnClick, element, mainPath, subPath);
		}
	}
}
