/**
 * Misc Functions for GUI
 *
 *
 * Some functions are based on phpAdsNew 2 functions
 * http://sourceforge.net/projects/phpadsnew
 * 
 * 
 * This program is free software. You can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 */


function findObj(n, d) { 
  var p,i,x;
  
  if(!d) {
  	d=document;
  }
  
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
  	d=parent.frames[n.substring(p+1)].document;
  	n=n.substring(0,p);
  }
  
  if(!(x=d[n])&&d.all) {
  	x=d.all[n];
  }
  
  for (i=0;!x&&i<d.forms.length;i++) {
  	x=d.forms[i][n];
  }
  
  for (i=0;!x&&d.layers&&i<d.layers.length;i++) {
  	x=findObj(n,d.layers[i].document);
  }
  if (!x && document.getElementById) {
  	x=document.getElementById(n);
  }
  return x;
}

function openWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
  return false;
}

function setTextOfLayer(objName,newText) {
	while (obj = document.getElementById(objName)) {
		with (obj)
			if (document.layers) {document.write(unescape(newText)); document.close();}
			else innerHTML = unescape(newText);
		obj.id = '';
	}
}

function showLayer(obj) { 
	if (obj.style) {
		obj=obj.style;
	}
	obj.display = 'block'
}

function hideLayer(obj) { 
	if (obj.style) {
		obj=obj.style;
	}
	obj.display = 'none'
}


/*********************************************************/
/* Confirm form submit                                   */
/*********************************************************/

function confirm_submit(o, str)
{
	f = findObj(o);
	if(confirm(str)) f.submit();
}


/*********************************************************/
/* Copy the contents of a field to the clipboard         */
/*********************************************************/

function phpAds_CopyClipboard(obj)
{
	obj = findObj(obj);
	
	if (obj) {
		window.clipboardData.setData('Text', obj.value);
	}
}

function formatPercent(percent) {
	return percent.toFixed(1).replace(/\./, ',');
}

// Helper function to format number
function formatNumber(incoming_number) {
	seperator_format_number = '.';

	format_number_output = '';
	incoming_number = "" + incoming_number;
	for(fn_i=0;fn_i<=incoming_number.length;fn_i++) {
		format_number_output = incoming_number.substr(incoming_number.length-fn_i,1)+format_number_output;
		if((fn_i%3==0) && (fn_i != incoming_number.length) && (fn_i != 0)) {
			format_number_output = seperator_format_number+format_number_output;
		}
	}
	
	return format_number_output;
}
