//javascript library for admin area

var monthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

function call_class_func(classname, functionname) 
//calls a function embedded in a class
{
	
	clsStr = "tmpcls = new " + classname + "()";
	tmpcls = eval(clsStr);
	ret = eval("tmpcls." + functionname);
	
	return ret;
	
}

function formatdaterange(startdate, enddate)
//formats the date range appropriately
//startdate and enddate should be unix timestamps not js date objects
{
	var ret = "";
	
	sd = new Date(startdate * 1000);
	ed = new Date(enddate * 1000);
	
	sd.fullyear = sd.getFullYear();
	sd.month = sd.getMonth();
	sd.day = sd.getDate();
	sd.hour = sd.getHours();
	sd.minutes = sd.getMinutes();
	
	ed.fullyear = ed.getFullYear();
	ed.month = ed.getMonth();
	ed.day = ed.getDate();
	ed.hour = ed.getHours();
	ed.minutes = ed.getMinutes();
	
	sd.hour = String(sd.hour);
	sd.minutes = String(sd.minutes);
	ed.hour = String(ed.hour);
	ed.minutes = String(ed.minutes);

	sd.day += getdatedaysuffix(sd.day);
	ed.day += getdatedaysuffix(ed.day);
	
	while (sd.hour.length<2)
	{
		sd.hour  = "0" + sd.hour;
	}

	while (sd.minutes.length<2)
	{
		sd.minutes  = "0" + sd.minutes;
	}

	while (ed.hour.length<2)
	{
		ed.hour  = "0" + ed.hour;
	}

	while (ed.minutes.length<2)
	{
		ed.minutes  = "0" + ed.minutes;
	}

	if ((startdate>0)&&(enddate>0))
	{
		if (sd.fullyear==ed.fullyear)
		{
			if (sd.month==ed.month)
			{
				if (sd.day == ed.day)
				{
					ret += sd.hour + ":" + sd.minutes;
					ret += " to " + ed.hour + ":" + ed.minutes + " " + ed.day + " " + monthNames[ed.month] + ", " + ed.fullyear;			
				}
				else
				{
					ret += sd.hour + ":" + sd.minutes + " " + sd.day;
					ret += " to " + ed.hour + ":" + ed.minutes + " " + ed.day + " " + monthNames[ed.month] + ", " + ed.fullyear;			
				}
			}
			else
			{
				ret += sd.hour + ":" + sd.minutes + " " + sd.day + " " + monthNames[sd.month] + ", " + sd.fullyear;
				ret += " to " + ed.hour + ":" + ed.minutes + " " + ed.day + " " + monthNames[ed.month] + ", " + ed.fullyear;			
			}
		}
		else
		{
			ret += sd.hour + ":" + sd.minutes + " " + sd.day + " " + monthNames[sd.month] + ", " + sd.fullyear;
			ret += " to " + ed.hour + ":" + ed.minutes + " " + ed.day + " " + monthNames[ed.month] + ", " + ed.fullyear;
		}
	}
	else if (startdate>0)
	{
			ret += sd.hour + ":" + sd.minutes + " " + sd.day + " " + monthNames[sd.month] + ", " + sd.fullyear;		
	}
	else if (enddate>0)
	{
			ret += " to " + ed.hour + ":" + ed.minutes + " " + ed.day + " " + monthNames[ed.month] + ", " + ed.fullyear;
	}
	else
	{
		ret = "no Date Specified";
	}
	
	return ret;
}

function getdatedaysuffix(day)
{
	var ret = "";
	
	if (day)
	{
		day = String(day);
		
		if (day.length>1)
		{
			daysub1 = day.substr(day.length-1);
			daysub2 = day.substr(day.length-2, 1);
			
			if (daysub2=="1")
			{
				ret = "th";
			}
			else
			{
				switch (daysub1)
				{
					case "1":
						
						ret = "st";
						break;
						
					case "2":
					
						ret = "nd";
						break;
						
					case "3":
						
						ret = "rd";
						break;
						
					default:
						
						ret = "th";
						break;
				}
			}
		}
		else
		{
			switch (day)
			{
				case "1":
					
					ret = "st";
					break;
					
				case "2":
				
					ret = "nd";
					break;
					
				case "3":
					
					ret = "rd";
					break;
					
				default:
					
					ret = "th";
					break;
			}
		}
	}
	
	return ret;
}

function getDirections(postcode)
{

	var targetpc = findObj('dirpc');
	window.open('http://www.multimap.com/map/aproute.cgi?client=public&lang=&rn=GB&input_rt=aproute_pan&startcountry=GB&startrd=&starttown=&startpc=' + targetpc.value + '&endcountry=GB&endrd=&endtown=&endpc=' + postcode + '&qs=q&starttime=00%3A00');
}

function findObj(theObj, theDoc)
//returns the specified object
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}

function toggleDisplay(itemname)
{
	if (itm = findObj(itemname))
	{
		if (itm.style.display == "none")
		{
				itm.style.display = "";
		}
		else
		{
			itm.style.display = "none";
		}
	}
}
