/**
 *
 * @author Ben Cole/ Pete Walker
 * @version 1.0
 * @started 06/07/06
 */
 
var aj = new ajax();
var local = new Array();
var ctr = 0;  // index pointer of current item
var curr = "" // current text in ticker 
var items = new Array();
var links = new Array(); 
var i = 0;
var ix = 0;
// The links can go inline into the page...

function fav() {
	var newT = document.createTextNode('Add to Favourites');
	var s = document.getElementById('fav');
	if (window.sidebar) {
		s.appendChild(newT);
		s.style.cursor = 'pointer';
		s.onclick = function() {window.sidebar.addPanel(document.title,self.location,'')};
	} else if (window.external) {
		s.appendChild(newT);
		s.style.cursor = 'pointer';
		s.onclick = function() {window.external.AddFavorite(self.location,document.title)};
	} else if (window.opera) {
		s.appendChild(newT);
		s.style.cursor = 'pointer';
		s.onclick = function() {
			var e = document.createElement('a');
			e.setAttribute('href',self.location);
			e.setAttribute('title',document.title);
			e.setAttribute('rel','sidebar');
			e.click();
		}
	}
}

var pageLoaded = 0;

function loaded(i,f) {
	if (document.getElementById && document.getElementById(i) != null) eval(f+'();');
	else if (!pageLoaded) setTimeout('loaded(\''+i+'\','+f+')',100);
}

function newsTicker() {
    // next character of current item
	if(i == 0 && ix == 0) {
		myTickerText.innerHTML = '';
	}
    if (i < items[ctr].length) {
		if(ix == 0) {
			ix++;
			myTickerCursor.innerHTML = '-';
		} else if(ix == 1){
			ix++;
			myTickerCursor.innerHTML = '_';
		} else {
			ix=0;
			myTickerText.innerHTML += items[ctr].substring(i,i+1);
			i++;
		}
		setTimeout('newsTicker()',30);
		return;
    }
	myTickerText.innerHTML = items[ctr];
	myTickerCursor.innerHTML = '';
	// new item
    i = 0;
	ix = 0;
    if (ctr < items.length-1) {
        ctr++;
    } else {
        ctr=0;
    }
	myTicker.href = links[ctr];
    setTimeout('newsTicker()',5000);
}

function goDomain()
{
	box = document.forms[0].domain;
	destination = box.options[box.selectedIndex].value;
	if (destination) location.href = destination;
}

function goCategory()
{
	box = document.getElementById('subdircont');
	destination = box.options[box.selectedIndex].value;
	if (destination) location.href = '/subdir/'+destination;
}
// ugly workaround for missing support for selectorText in Netscape6/Mozilla
// call onLoad() or before you need to do anything you would have otherwise used
// selectorText for.
// stolen from http://developer.apple.com/internet/webcontent/styles.html
var ugly_selectorText_workaround_flag = false;
var allStyleRules;
// code developed using the following workaround (CVS v1.15) as an example.
// http://lxr.mozilla.org/seamonkey/source/extensions/xmlterm/ui/content/XMLTermCommands.js
function ugly_selectorText_workaround() {
	if((navigator.userAgent.indexOf("Gecko") == -1) ||
	   (ugly_selectorText_workaround_flag)) {
		return; // we've already been here or shouldn't be here
	}
	var styleElements = document.getElementsByTagName("style");
	
	for(var i = 0; i < styleElements.length; i++) {
		var styleText = styleElements[i].firstChild.data;
		// this should be using match(/\b[\w-.]+(?=\s*\{)/g but ?= causes an
		// error in IE5, so we include the open brace and then strip it
		allStyleRules = styleText.match(/\b[\w-.]+(\s*\{)/g);
	}
	
	for(var i = 0; i < allStyleRules.length; i++) {
		// probably insufficient for people who like random gobs of 
		// whitespace in their styles
		allStyleRules[i] = allStyleRules[i].substr(0, (allStyleRules[i].length - 2));
	}
	
	ugly_selectorText_workaround_flag = true;
}

// setStyleById: given an element id, style property and 
// value, apply the style.
// stolen from http://developer.apple.com/internet/webcontent/styles.html
// args:
//  i - element id
//  p - property
//  v - value
//
function setStyleById(i, p, v) {
	var n = document.getElementById(i);
	n.style[p] = v;
}

// setStyleByClass: given an element type and a class selector,
// style property and value, apply the style.
// stolen from http://developer.apple.com/internet/webcontent/styles.html
// args:
//  t - type of tag to check for (e.g., SPAN)
//  c - class name
//  p - CSS property
//  v - value
var ie = (document.all) ? true : false;
function sortLabels(h) {
	setStyleByClass('label','label','display','none');
}
function setStyleByClass(t,c,p,v){
	var elements;
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					eval('node.style.' + p + " = '" +v + "'");
				}
			}
		}
	}
}
// setStyleByTag: given an element type, style property and 
// value, and whether the property should override inline styles or
// just global stylesheet preferences, apply the style.
// stolen from http://developer.apple.com/internet/webcontent/styles.html
// args:
//  e - element type or id
//  p - property
//  v - value
//  g - boolean 0: modify global only; 1: modify all elements in document
function setStyleByTag(e, p, v, g) {
	if(g) {
		var elements = document.getElementsByTagName(e);
		for(var i = 0; i < elements.length; i++) {
			elements.item(i).style[p] = v;
		}
	} else {
		var sheets = document.styleSheets;
		if(sheets.length > 0) {
			for(var i = 0; i < sheets.length; i++) {
				var rules = sheets[i].cssRules;
				if(typeof rules == "undefined") {
					rules = sheets[i].rules;
				}
				if(rules.length > 0) {
					for(var j = 0; j < rules.length; j++) {
						var s = rules[j].style;
						// selectorText broken in NS 6/Mozilla: see
						// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
						ugly_selectorText_workaround();
						if(allStyleRules) {
							if(allStyleRules[j] == e) {
								s[p] = v;
							}			
						} else {
							// use the native selectorText and style stuff
							if(((s[p] != "") && (s[p] != null)) &&
							   (rules[j].selectorText.toLowerCase() == e.toLowerCase())) {
								s[p] = v;
							}
						}

					}
				}
			}
		}
	}
	return;
}

function sortForm()
{
  document.getElementById('email').style.clear='both'
  document.getElementById('add1').style.width='410px';
  document.getElementById('add2').style.width='410px';
  document.getElementById('bname').style.width='229px';
}
function switchDirectory(input,response)
{
	if (response != '') { 
	  response = response.split(",");
	  final = new Array();
	  for(i = 1; i < response.length; i++) {
	    final[i-1] = response[i].split(":");
	  }
	  local[response[0]] = final;
	  populateCats(final);
	} else {
  	box = document.getElementById('directorybox');
  	id = box.options[box.selectedIndex].value;
  	//alert ("?p=directory&id="+id);
  	if (id) {
  	  v = document.getElementById('subdircont');
  	  for(i=0;i<v.options.length;i++) {
        v.options[i] = null;
      }
      v.options[0] = new Option("Loading...","-");
  	  if(local[id]) {
  	    populateCats(local[id]);
  	  } else {
  	    url = 'index.php?p=dirxml&id='+id;
  	    aj.loadXMLDoc(url);
  	  }
  	}
	}
	//location.href = '?p=directory&id='+id;
}

function populateCats(a)
{
  populate(document.getElementById('subdircont'),a);
}

function populate(v,a)
{
  for(i=0;i<v.options.length;i++) {
		v.options[i] = null;
	}
	if(a.length > 0) {
	  v.options[0] = new Option('Please Select:','-');
		for(i=0;i<a.length;i++) {
			v.options[i+1] = new Option(a[i][1],a[i][0]);
		}
	}
	else {
		box = document.getElementById('directorybox');
		id = box.options[box.selectedIndex].value;
		location.href = '/directory/'+id;
		v.options[0] = new Option("No Matches","-");
	}
}
function toggleQuote()
{
  if(document.getElementById('adtype').value>2)
  {
    document.getElementById('quotefield').style.display='block';
    document.getElementById('lblquotefield').style.display='block';
  }
  else
  {
    document.getElementById('quotefield').style.display='none';
    document.getElementById('lblquotefield').style.display='none';
  }
  if(document.getElementById('adtype').value>1 && document.getElementById('adtype').value<4)
  {
    document.getElementById('step6').style.display='block';
    document.getElementById('payMethods').style.display='block';
  }
  else
  {
    document.getElementById('step6').style.display='none';
    document.getElementById('payMethods').style.display='none';
  }
  
}
function hideButtons(h)
{
  var elem, vis;
  if( document.getElementById ){ // this is the way the standards work
    elem = document.getElementById( 'dombutton' );
	if(!h)
	{
    elem2 = document.getElementById('dirbutton');
	elem3 = document.getElementById('catbutton');
	}
    }
  else if( document.all ){ // this is the way old msie versions work
  elem = document.all['dombutton'];
  	if(!h)
	{
      
      elem2 = document.all['dirbutton'];
	  elem3 = document.all['catbutton'];
	  }
      }
  else if( document.layers ){ // this is the way nn4 works
  elem = document.layers['dombutton'];
  	if(!h)
	{
    
    elem2 = document.layers['dirbutton'];
	elem3 = document.layers['catbutton'];
	}
    }
  vis = elem.style;
  vis.display = 'none';
  if(!h)
  {
  vis2 = elem2.style;
  vis2.display='none';
  vis3 = elem3.style;
  vis3.display='none';
  }

}
function apresSortLabels() {
	//setStyleById('error','marginBottom','27px');
	for(var i=0; i<inputLabels.length; i++) {
		if(document.getElementById(inputLabels[i][0]).value == '') {
			document.getElementById(inputLabels[i][0]).value = inputLabels[i][1];
		}
		document.getElementById(inputLabels[i][0]).onfocus = sortFocus;
		document.getElementById(inputLabels[i][0]).onblur = sortBlur;
	}
}
function eTarg(e)
{
	if (!e) var e = window.event
	var targ = null;
	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	return targ;
}

function findIndex(e)
{
	for(var i=0; i<inputLabels.length; i++) {
		if(e.id == inputLabels[i][0]) {
			return i;
		}
	}
	return -1;
}

function sortFocus(x) {
	el = eTarg(x);
	i = findIndex(el);
	if(document.getElementById(inputLabels[i][0]).value == inputLabels[i][1]) {
		document.getElementById(inputLabels[i][0]).value = '';
	}
}

function sortBlur(x) {
	el = eTarg(x);
	i = findIndex(el);
	if(document.getElementById(inputLabels[i][0]).value == '') {
		document.getElementById(inputLabels[i][0]).value = inputLabels[i][1];
	}
}
