// $Revision: 2.1.2.3 $

/************************************************************************/
/* phpAdsNew 2                                                          */
/* ===========                                                          */
/*                                                                      */
/* Copyright (c) 2000-2003 by the phpAdsNew developers                  */
/* 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.       */
/************************************************************************/

// Original JavaScript code by Duncan Crombie: dcrombie@chirp.com.au
// Please acknowledge use of this code by including this header.
var ns4=(navigator.appName=='Netscape' && parseInt(navigator.appVersion)==4);
var ns6=(document.getElementById)? true:false;
var ie4=(document.all)? true:false;
var ie5=false;
if(ie4)
{
	if((navigator.userAgent.indexOf('MSIE 5')> 0)||(navigator.userAgent.indexOf('MSIE 6')> 0))
	{
		if(document.compatMode && document.compatMode=='CSS1Compat')docRoot='document.documentElement';
		ie5=true;
	}
	if(ns6)
	{
		ns6=false;
	}
}
// CONSTANTS
var separator = ",";  // use comma as 000's separator
var decpoint = ".";  // use period as decimal point
var percent = "%";
var currency = "$";  // use dollar sign for currency

function formatNumber(number, format, printNegativ) 
{  // use: formatNumber(number, "format")
	if (number - 0 != number) return null;  // if number is NaN return null
	var useSeparator = format.indexOf(separator) != -1;  // use separators in number
	var usePercent = format.indexOf(percent) != -1;  // convert output to percentage
	var useCurrency = format.indexOf(currency) != -1;  // use currency format
	var isNegative = (number < 0);
	number = Math.abs (number);
	if (usePercent) number *= 100;
	format = strip(format, separator + percent + currency);  // remove key characters
	number = "" + number;  // convert number input to string
	
	// split input value into LHS and RHS using decpoint as divider
	var dec = number.indexOf(decpoint) != -1;
	var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
	var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";
	
	// split format string into LHS and RHS using decpoint as divider
	dec = format.indexOf(decpoint) != -1;
	var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
	var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";
	
	// adjust decimal places by cropping or adding zeros to LHS of number
	if (srightEnd.length < nrightEnd.length) {
	var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
	nrightEnd = nrightEnd.substring(0, srightEnd.length);
	if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up
	
	// patch provided by Patti Marcoux 1999/08/06
	while (srightEnd.length > nrightEnd.length) {
	nrightEnd = "0" + nrightEnd;
	}
	
	if (srightEnd.length < nrightEnd.length) {
	nrightEnd = nrightEnd.substring(1);
	nleftEnd = (nleftEnd - 0) + 1;
	}
	} else {
	for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
	if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
	else break;
	}
	}
	
	// adjust leading zeros
	sleftEnd = strip(sleftEnd, "#");  // remove hashes from LHS of format
	while (sleftEnd.length > nleftEnd.length) {
	nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
	}
	
	if (useSeparator) nleftEnd = separate(nleftEnd, separator);  // add separator
	var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
	output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
	if (isNegative ) {
	// patch suggested by Tom Denn 25/4/2001
	output = (useCurrency) ? "(" + output + ")" : "-" + output;
	}
	return output;
}

function strip(input, chars) 
{  // strip all characters in 'chars' from input
	var output = "";  // initialise output string
	for (var i=0; i < input.length; i++)
	if (chars.indexOf(input.charAt(i)) == -1)
	output += input.charAt(i);
	return output;
}

function separate(input, separator) 
{  // format input using 'separator' to mark 000's
	input = "" + input;
	var output = "";  // initialise output string
	for (var i=0; i < input.length; i++) {
	if (i != 0 && (input.length - i) % 3 == 0) output += separator;
	output += input.charAt(i);
	}
	return output;
}
  
/*********************************************************/
/* Enable accesskeys in IE                               */
/*********************************************************/

var accessKeyEnabled = true;

function useAccessKey (evt) {
	if (accessKeyEnabled == true) {
		if (event.altKey) {
			event.srcElement.click();
		}
	} else {
		event.srcElement.blur();
		accessKeyEnabled = true;
	}
}

function releaseAccessKey() {
	if (accessKeyEnabled == false) {
		accessKeyEnabled = true;
	}
}

function initAccessKey() {
	if (navigator.appName == "Microsoft Internet Explorer") {
		for (i=0;i<document.all.length;i++) {
			a = document.all(i);
			if (a.tagName == 'A' && a.accessKey != '') {
				a.blur();
				a.onfocus = useAccessKey;
			}
		}
		if (event.altKey) {
			accessKeyEnabled = false;
			document.onkeyup = releaseAccessKey;
			setTimeout ('releaseAccessKey()', 100);
		}
	}
}


/*********************************************************/
/* General functions                                     */
/*********************************************************/
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 setCheckboxes(the_form, the_name, do_check)
{
    var elts      = document.forms[the_form].elements[the_name];
    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;

    if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) 
        {
            elts[i].checked = do_check;
        } // end for
    } else {
        elts.checked        = do_check;
    } // end if... else

    return true;
} // end of the 'setCheckboxes()' function



function openWindow(theURL,winName,features) {
  var cWindow = window.open(theURL,winName,features);
  if (!cWindow.opener) 
	cWindow.opener = self;
  if (cWindow.focus != null) 
	cWindow.focus();
  return false;
}

function setTextOfLayer(objName,newText) 
{
	obj = document.getElementById(objName);
	while (obj) {
		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();
}

function confirmMessage( message )
{
	tf = confirm( message );
	return tf;
}


/*********************************************************/
/* Open Search window                                    */
/*********************************************************/

function search_window(keyword, where)
{
	path = where+'?keyword='+keyword;
	SearchWindow = window.open("","Search","toolbar=no,location=no,status=no,scrollbars=yes,width=600,height=500,innerheight=50,screenX=100,screenY=100,pageXoffset=100,pageYoffset=100,resizable=yes");          

	if (SearchWindow.frames.length == 0) 
	{
		SearchWindow = window.open(path,"Search","toolbar=no,location=no,status=no,scrollbars=yes,width=700,height=600,innerheight=50,screenX=100,screenY=100,pageXoffset=100,pageYoffset=100,resizable=yes");
	}
	else
	{
		SearchWindow.location.href = path;
		SearchWindow.focus();
	}
}



/*********************************************************/
/* Focus the first field of the login screen             */
/*********************************************************/

function login_focus()
{
	if (document.login.phpAds_username.value == '')
		document.login.phpAds_username.focus();
	else
		document.login.phpAds_password.focus();
}


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

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


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

function boxrow_over(obj)
{
	obj.style.backgroundColor='#F6F6F6';
}

function boxrow_leave(obj)
{
	obj.style.backgroundColor='#FFFFFF';
}

function boxrow_nonbubble()
{
	if (event.stopPropagation) 
	{ 
		event.stopPropagation(); 
	} 
	else 
	{ 
		event.cancelBubble = true; 
	}
}


