adJump = function(l){ window.open(l); }

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

function confirmDelete(page){
	if (confirm("Are you sure you wish to delete this item?")){
		document.location.href=page;
	}
}

function changeYear (year) {
	document.location.href = "?y="+year;
}

function checkPoll () {
	var answer1 = document.getElementById('1');
	var answer2 = document.getElementById('2');
	var answer3 = document.getElementById('3');
	if(!answer1.checked && !answer2.checked && !answer3.checked){
		alert('Please make sure you select your choice before submitting a vote!');
		return false;	
	} else {
		return true;
	}			
}

var clearSearch=true;

function focusSearch(){

	if (clearSearch==true) {

		document.getElementById('xSearch').value="";

		clearSearch=false;

	}

}

function toggleLayer( helpLayer ){

	var elem, vis;
	if( document.getElementById )
	elem = document.getElementById( helpLayer );
	else if( document.all )
	elem = document.all[helpLayer];
	else if( document.layers )
	elem = document.layers[helpLayer];
	vis = elem.style;
	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
	vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
	vis.display = (vis.display==''||vis.display=='block')?'none':'block'; 
}

function showSupplierList(id){
	var s = document.getElementById(id);
	if (s.style.display == "inline"){
		s.style.display = "none";
	}else{
		s.style.display = "inline";
	}
}

function showSupplierAds(id){

	var s = document.getElementById(id);
	
	if (s.style.display == "inline"){
		s.style.display = "none";
	}else{
		s.style.display = "inline";
	}
}	

function submit_form(){

	var frm = document.forms[0];
	var foc = "";
	var errs = "";
	
	if (frm["company"].value == ""){
		foc = frm["company"];
		errs = "Please enter your company";
	}
	
	if ((frm["position"].value == "") && (frm["other_position"].value == "")){
		foc = frm["position"];
		errs = "Please enter your position\n" + errs;
	}
	
	if (frm["email_address"].value == ""){
		foc = frm["email_address"];
		errs = "Please enter your email address\n" + errs;
	}	
	
	if ((frm["last_name"].value == "") || (frm["first_name"].value == "")){
		errs = "Please enter your full name \n" + errs;
		if (frm["last_name"].value == ""){
			foc = frm["first_name"];
		}else{
			foc = frm["last_name"];
		}
	}	
	
	
	if (errs != ""){
		alert (errs);							
		frm.focus = foc;
	}else{
		frm.submit();
	}
			
}

function set_advert_page(issue){
	if (issue!=""){
		document.location.href="?issue="+issue;
	}
}

function showHideList(id){

	var d = document.getElementById(id);
	
	if (id == 'list_by_type'){
		var d = document.getElementById("list_by_type");
		var d2 = document.getElementById("list_by_supplier");
	}else{
		var d2 = document.getElementById("list_by_type");
		var d = document.getElementById("list_by_supplier");
	}
	
	d2.style.display = "none";
	d.style.display = "inline";	

}

function MM_validateForm() { //v4.0
  if (document.getElementById){
	var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
	for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
	  if (val) { nm=val.name; if ((val=val.value)!="") {
		if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
		  if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
		} else if (test!='R') { num = parseFloat(val);
		  if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
		  if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
			min=test.substring(8,p); max=test.substring(p+1);
			if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
	  } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
	} if (errors) alert('The following error(s) occurred:\n'+errors);
	document.MM_returnValue = (errors == '');
} }

function lookup(quick_search_text) {
	if(quick_search_text.length == 0) {
		$('#suggestions').hide(); // Hide the suggestion box.
	} else {
		$.post("/resources/auto_complete/auto_complete.php", {queryString: ""+quick_search_text+""}, function(data){
			if(data.length >0) {
				$('#suggestions').show();
				$('#autoSuggestionsList').html(data);
			}
		});
	}
}

function fill(thisValue) {
	$('#quick_search_text').val(thisValue);
	setTimeout("$('#suggestions').hide();", 200);
}

//** Animated Collapsible DIV v2.0- (c) Dynamic Drive DHTML code library: http://www.dynamicdrive.com.
//** May 24th, 08'- Script rewritten and updated to 2.0.
//** June 4th, 08'- Version 2.01: Bug fix to work with jquery 1.2.6 (which changed the way attr() behaves).

var animatedcollapse={
divholders: {}, //structure: {div.id, div.attrs, div.$divref}
divgroups: {}, //structure: {groupname.count, groupname.lastactivedivid}
lastactiveingroup: {}, //structure: {lastactivediv.id}

show:function(divids){ //public method
	if (typeof divids=="object"){
		for (var i=0; i<divids.length; i++)
			this.showhide(divids[i], "show")
	}
	else
		this.showhide(divids, "show")
},

hide:function(divids){ //public method
	if (typeof divids=="object"){
		for (var i=0; i<divids.length; i++)
			this.showhide(divids[i], "hide")
	}
	else
		this.showhide(divids, "hide")
},

toggle:function(divid){ //public method
	this.showhide(divid, "toggle")
},

addDiv:function(divid, attrstring){ //public function
	this.divholders[divid]=({id: divid, $divref: null, attrs: attrstring})
	this.divholders[divid].getAttr=function(name){ //assign getAttr() function to each divholder object
		var attr=new RegExp(name+"=([^,]+)", "i") //get name/value config pair (ie: width=400px,)
		return (attr.test(this.attrs) && parseInt(RegExp.$1)!=0)? RegExp.$1 : null //return value portion (string), or 0 (false) if none found
	}
},

showhide:function(divid, action){
	var $divref=this.divholders[divid].$divref //reference collapsible DIV
	if (this.divholders[divid] && $divref.length==1){ //if DIV exists
		var targetgroup=this.divgroups[$divref.attr('groupname')] //find out which group DIV belongs to (if any)
		if ($divref.attr('groupname') && targetgroup.count>1 && (action=="show" || action=="toggle" && $divref.css('display')=='none')){ //If current DIV belongs to a group
			if (targetgroup.lastactivedivid && targetgroup.lastactivedivid!=divid) //if last active DIV is set
				this.slideengine(targetgroup.lastactivedivid, 'hide') //hide last active DIV within group first
				this.slideengine(divid, 'show')
			targetgroup.lastactivedivid=divid //remember last active DIV
		}
		else{
			this.slideengine(divid, action)
		}
	}
},

slideengine:function(divid, action){
	var $divref=this.divholders[divid].$divref
	if (this.divholders[divid] && $divref.length==1){ //if this DIV exists
		var animateSetting={height: action}
		if ($divref.attr('fade'))
			animateSetting.opacity=action
		$divref.animate(animateSetting, $divref.attr('speed')? parseInt($divref.attr('speed')) : 500)
		return false
	}
},

generatemap:function(){
	var map={}
	for (var i=0; i<arguments.length; i++){
		if (arguments[i][1]!=null){
			map[arguments[i][0]]=arguments[i][1]
		}
	}
	return map
},

init:function(){
	var ac=this
	jQuery(document).ready(function($){
		var persistopenids=ac.getCookie('acopendivids') //Get list of div ids that should be expanded due to persistence ('div1,div2,etc')
		var groupswithpersist=ac.getCookie('acgroupswithpersist') //Get list of group names that have 1 or more divs with "persist" attribute defined
		if (persistopenids!=null) //if cookie isn't null (is null if first time page loads, and cookie hasnt been set yet)
			persistopenids=(persistopenids=='nada')? [] : persistopenids.split(',') //if no divs are persisted, set to empty array, else, array of div ids
		groupswithpersist=(groupswithpersist==null || groupswithpersist=='nada')? [] : groupswithpersist.split(',') //Get list of groups with divs that are persisted
		jQuery.each(ac.divholders, function(){ //loop through each collapsible DIV object
			this.$divref=$('#'+this.id)
			if ((this.getAttr('persist') || jQuery.inArray(this.getAttr('group'), groupswithpersist)!=-1) && persistopenids!=null){
				var cssdisplay=(jQuery.inArray(this.id, persistopenids)!=-1)? 'block' : 'none'
			}
			else{
				var cssdisplay=this.getAttr('hide')? 'none' : null
			}
			this.$divref.css(ac.generatemap(['height', this.getAttr('height')], ['display', cssdisplay]))
			this.$divref.attr(ac.generatemap(['groupname', this.getAttr('group')], ['fade', this.getAttr('fade')], ['speed', this.getAttr('speed')]))
			if (this.getAttr('group')){ //if this DIV has the "group" attr defined
				var targetgroup=ac.divgroups[this.getAttr('group')] || (ac.divgroups[this.getAttr('group')]={}) //Get settings for this group, or if it no settings exist yet, create blank object to store them in
				targetgroup.count=(targetgroup.count||0)+1 //count # of DIVs within this group
				if (!targetgroup.lastactivedivid && this.$divref.css('display')!='none' || cssdisplay=="block") //if this DIV was open by default or should be open due to persistence								
					targetgroup.lastactivedivid=this.id //remember this DIV as the last "active" DIV (this DIV will be expanded)
				this.$divref.css({display:'none'}) //hide any DIV that's part of said group for now
			}
		}) //end divholders.each
		jQuery.each(ac.divgroups, function(){ //loop through each group
			if (this.lastactivedivid)
				ac.divholders[this.lastactivedivid].$divref.show() //and show last "active" DIV within each group (one that should be expanded)
		})
		var $allcontrols=$('*[rel]').filter('[@rel^="collapse-"], [@rel^="expand-"], [@rel^="toggle-"]') //get all elements on page with rel="collapse-", "expand-" and "toggle-"
		var controlidentifiers=/(collapse-)|(expand-)|(toggle-)/
		$allcontrols.each(function(){
			$(this).click(function(){
				var relattr=this.getAttribute('rel')
				var divid=relattr.replace(controlidentifiers, '')
				var doaction=(relattr.indexOf("collapse-")!=-1)? "hide" : (relattr.indexOf("expand-")!=-1)? "show" : "toggle"
				return ac.showhide(divid, doaction)
			}) //end control.click
		})// end control.each
		$(window).bind('unload', function(){
			ac.uninit()
		})
	}) //end doc.ready()
},

uninit:function(){
	var opendivids='', groupswithpersist=''
	jQuery.each(this.divholders, function(){
		if (this.$divref.css('display')!='none'){
			opendivids+=this.id+',' //store ids of DIVs that are expanded when page unloads: 'div1,div2,etc'
		}
		if (this.getAttr('group') && this.getAttr('persist'))
			groupswithpersist+=this.getAttr('group')+',' //store groups with which at least one DIV has persistance enabled: 'group1,group2,etc'
	})
	opendivids=(opendivids=='')? 'nada' : opendivids.replace(/,$/, '')
	groupswithpersist=(groupswithpersist=='')? 'nada' : groupswithpersist.replace(/,$/, '')
	this.setCookie('acopendivids', opendivids)
	this.setCookie('acgroupswithpersist', groupswithpersist)
},

getCookie:function(Name){ 
	var re=new RegExp(Name+"=[^;]*", "i"); //construct RE to search for target name/value pair
	if (document.cookie.match(re)) //if cookie found
		return document.cookie.match(re)[0].split("=")[1] //return its value
	return null
},

setCookie:function(name, value, days){
	if (typeof days!="undefined"){ //if set persistent cookie
		var expireDate = new Date()
		expireDate.setDate(expireDate.getDate()+days)
		document.cookie = name+"="+value+"; path=/; expires="+expireDate.toGMTString()
	}
	else //else if this is a session only cookie
		document.cookie = name+"="+value+"; path=/"
}

}