var tmr;

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

window.onload = function() {

	modifyNewWindowLinks();
	hideDottedLines();
	//setModalHeight();
}

function modifyNewWindowLinks() {
	//This function looks for all the links with a class of "NewWindow"
	//and will cause them to open in a new window.

	var aTags = document.getElementsByTagName("a");
	for (var x = 0; x < aTags.length; x++) {
		var currTag = aTags[x];
		if (String(currTag.className).indexOf('NewWindow') > -1) {
			currTag.onclick = function(){ window.open(this.href); return false;}
		}
	}
}

function findPos(obj) {
// gets the 'top' and 'left' positions of the object.

  var curtop = 0;
  var curleft = 0;
  if (obj.offsetParent) {
	  while (obj.offsetParent) {
		  curtop += obj.offsetTop;
		  curleft += obj.offsetLeft;
		  obj = obj.offsetParent;
	  }
  }
  else {
	  if (obj.y) curtop += obj.y;
	  if (obj.x) curleft += obj.x;
  }
  var r=Array();
  r[0]=curtop;
  r[1]=curleft;
  return r;
}

function setModalHeight() {
	var o=document.getElementById("modalDiv");
	if (o) {
		var eop1=document.getElementById("eopDiv_lhs");
		var eop2=document.getElementById("eopDiv_rhs");
		var pos1=(eop1)?findPos(eop1):0;
		var pos2=(eop2)?findPos(eop2):0;
		var max=(pos1[0]>pos2[0])?pos1[0]:pos2[0];
		//alert(pos1[0]+' '+pos2[0]+' = '+max);

		o.style.height=(max+50)+"px";
	}
}

function hideDottedLines() {
// hide the 'dotted lines' around the link for links with image-replacement.

	var theahrefs = document.getElementsByTagName("a");

	// fix dotted line thing when link is OnClicked
	for(var x=0;x!=theahrefs.length;x++) {
		//if (theahrefs[x].className!="") alert(theahrefs[x].className);
		if (theahrefs[x].className.indexOf("hideOutline")>-1) {
			theahrefs[x].onfocus = function stopLinkFocus(){ this.hideFocus=true; };
		}
	}
}

function form_autoSubmit() {
// used to submit the "event type" select-box on the schedule page...

	f=document.forms["modeSel"];
	if (f) f.submit();
}


/* AJAX functions.... */

var AJAX_CACHE=new Array();



function updateProvinces() {
/* MS: used on profile forms... */

	var params;
	var text="";
	var f=document.forms.profileForm;
	var c=f.elements["a_country"].value;

	params="c="+c;
	if (AJAX_CACHE[params]) text=AJAX_CACHE[params];
	else {
		var responseText=http_getPage("/ajax/get-provinces.php",params,1,"");
		if (responseText != '""') {
			text=responseText;
			AJAX_CACHE[params]=text;
		}
	}

	o=document.getElementById("af_a_province");
	if (o) o.innerHTML=text;

	return false;
}

function updateHousingForm(obj) {

	var wf=obj.options[obj.selectedIndex].value;
	var wanted=(wf=="wanted")?1:0;
	o=document.getElementById("fr_h_unit"); if (o) o.style.display=(wanted?"none":"block");
	o=document.getElementById("fr_h_address"); if (o) o.style.display=(wanted?"none":"block");
	/*o=document.getElementById("fr_h_city"); if (o) o.style.display=(wanted?"none":"block");*/
	o=document.getElementById("fr_h_postal"); if (o) o.style.display=(wanted?"none":"block");
	o=document.getElementById("fr_image"); if (o) o.style.display=(wanted?"none":"block");
	return false;
}

function updateRideForm(obj) {

	var tt=obj.options[obj.selectedIndex].value;
	var onetime=(tt=="once")?1:0;
	o=document.getElementById("fr_daysDriving"); if (o) o.style.display=(onetime?"none":"block");
	return false;
}

function updateRideList(ride_id) {

	var params;
	var text="";

	params="id="+ride_id;
	if (AJAX_CACHE[params]) text=AJAX_CACHE[params];
	else {
		var responseText=http_getPage("/ajax/get-rideinfo.php",params,1,"");
		if (responseText != '""') {
			text=responseText;
			AJAX_CACHE[params]=text;
		}
	}
	return text;
}

var curCalendar=1;
var curCalendarPos=0;

function updateCalendar(pos) {

	var params;
	var text="";

	params="pos="+pos;
	if (AJAX_CACHE[params]) text=AJAX_CACHE[params];
	else {
		var responseText=http_getPage("/ajax/get-calendar.php",params,1,"");
		if (responseText != '""') {
			text=responseText;
			AJAX_CACHE[params]=text;
		}
	}
	curCalendar++;
	if (curCalendar==3) curCalendar=1;

	o=document.getElementById("calendar_"+curCalendar);
	if (o) o.innerHTML=text;
	
	if (curCalendarPos==0) $('#sidebar_calendar a.prev').hide();
	else $('#sidebar_calendar a.prev').show();

	if (curCalendarPos==5) $('#sidebar_calendar a.next').hide();
	else $('#sidebar_calendar a.next').show();

	return 1;
}

var curClassified=1;
var curClassifiedPos=0;
var curClassifiedMode='recent';

function updateClassifieds(mode,pos,returnText) {

	var params;
	var text="";

	params="mode="+mode+"&pos="+pos;
	if ((mode!="random")&&(AJAX_CACHE[params])) text=AJAX_CACHE[params];
	else {
		var responseText=http_getPage("/ajax/get-classifieds.php",params,1,"");
		if (responseText != '""') {
			text=responseText;
			AJAX_CACHE[params]=text;
		}
	}

	if (returnText==1) return text;

	curClassified++;
	if (curClassified==3) curClassified=1;
	o=document.getElementById("classified_"+curClassified);
	if (o) o.innerHTML=text;
	
	return 1;
}

function submitPoll(mode) {
	var f=document.forms["cannonPoll"];
	var pollID=f.elements["pollID"].value;
	var params;
	var text="";
	var answer=0;
	
	for (i=0;i<f.elements["answer"].length;i++) {
		if (f.elements["answer"][i].checked) answer=f.elements["answer"][i].value;
	}

	params="mode="+mode+"&id="+pollID;
	if (answer>0) params+="&answer="+answer;
	
	if (AJAX_CACHE[params]) text=AJAX_CACHE[params];
	else {
		var responseText=http_getPage("/ajax/get-poll.php",params,1,"");
		if (responseText != '""') {
			text=responseText;
			AJAX_CACHE[params]=text;
		}
	}

	$("#poll").html(text);
	return 1;
}

function updateViewpointSubcategories(obj) {

	var tt=obj.options[obj.selectedIndex].value;
	var opinion=(tt=='opinion');
	o=document.getElementById("fr_subcategory_opinion"); if (o) o.style.display=(opinion?"block":"none");
	o=document.getElementById("fr_subcategory_review"); if (o) o.style.display=(opinion?"none":"block");
	return false;
	
}

function javascript_active() {
// updates the session variable to let us know the user has activated Javascript.

	var responseText=http_getPage("/ajax/javascript-active.php","",1,"");
	//alert(responseText);

	return false;
}