function trim(str){
   return str.replace(/^\s*|\s*$/g,"");
}

function init(){
	var x,y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) // all but Explorer Mac
	{
		x = document.body.scrollWidth;
		y = document.body.scrollHeight;
	}
	else // Explorer Mac;
		 //would also work in Explorer 6 Strict, Mozilla and Safari
	{
		x = document.body.offsetWidth;
		y = document.body.offsetHeight;
	}
	//document.images['sizer'].height = y-140;
}

function enlargeImage(path, title){
	if(title==undefined){
		title=":: image ::";
	}
	var iw = window.open('', 'iw', 'toolbars=no,width=300,height=200,top=100,left=100,resizable=yes');
	iw.document.open();
	iw.document.writeln('<html><head>');
	iw.document.writeln('<title>' + title + '</title>');
	iw.document.writeln('<META HTTP-EQUIV="imagetoolbar" content="no">');
	iw.document.writeln('<script language="javascript" type="text/javascript">function resizeWin(){if (document.all || document.layers) window.resizeTo(document.images[0].width+10,document.images[0].height+30);}</script>');
	iw.document.writeln('</head>');
	iw.document.writeln('<body onload="resizeWin();" style="margin:0px;" onblur="self.close();">');
	iw.document.writeln('<div align="center"><img src="' + path + '" onclick="self.close();" onmouseover="this.style.cursor=\'pointer\';"></div>');
	iw.document.writeln('</body></html>');
	iw.document.close();
	return false;
}

function checkForm(frm){ /* test if element will work here */
	if(!validEmail(frm)){
		return false;
	}
	switch(frm.selPreference.value){
		case "telephone" :
			if(!validTelephone(frm)){
				alert("Please supply a valid telephone number if you would like to be contacted by this method");
				return false;
			}
			break;
		case "email" :
			return true;
			break;
		default:
			return false;
			break;
	}
	return true;
}

function validTelephone(frm){
	var str = frm.txtTelephone.value;
	str = str.replace(/\s/g, '');
	var regpat = /^(\+)?[\d\(\)]{6,}$/;
	var b = regpat.test(str);
	return b;
}

function validURL(str){
	var regpat = new RegExp("^(((http(s?))|(ftp))\:\/\/)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(\/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$");
	var b = regpat.test(str);
	return b;
}

function confirmDelete(){
	var agree=confirm("Are you sure you want to delete this item?");
	if (agree)
		return true ;
	else
		return false ;
}

function buildBreadcrumb() {
	var aFolders = new Array();
	var href = document.location.toString();
	aFolders = href.split('/');
	
	var strout = '';
	for(var i=2; i<aFolders.length; i++){
		if(i==2){aFolders[i]='home'}
		strout += '<a href="';
		if(i==aFolders.length-1){
			strout += buildDepth((aFolders.length-i)+1) + aFolders[i];
		}
		else {
			strout += buildDepth((aFolders.length-i)+1) + 'default.php';
		}
		strout += '">' + tidyLabel(aFolders[i]) + '</a>&nbsp;&gt;&nbsp;';
	}
	document.write(strout);
}

function buildDepth(iterations){
	var iterations=iterations-3;
    var depthStr='';
	
    for (i=0;i<iterations;i++)
    {
        depthStr=depthStr + '../';
    }
    return depthStr;
}

function tidyLabel(str){
	var strout = '';
	var a = str.split(".")
	strout = a[0];
	strout = strout.replace(/(_)/g, " ");
	strout = strout.replace(/(-)/g, " ");
	return strout;
}

function MM_findObj(n, d) { //v4.01
  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=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function buildDateSelect(name, adate){
	if(adate!=''){
		var theDate = new Date(adate);
		var theDay = theDate.getUTCDate()+1;
		var theMonth = theDate.getUTCMonth();
		var theYear = theDate.getUTCFullYear();
	}
	else{
		var today = new Date();
		var theDay = today.getDate();
		var theMonth = today.getMonth();
		var theYear = today.getYear();
	}
	var yearsahead = 3;
	var daysinmonth = getDaysInMonth(theMonth+1, theYear);
	var aMonths = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	document.write('<select name="sel' + name + 'day">');
	for(var i=1;i<=daysinmonth;i++){
		document.write('<option value="' + i + '"');
		if(i==theDay) document.write(' selected');
		document.write('>' + i + '</option>');
	}
	document.write('</select>&nbsp;');
	document.write('<select name="sel' + name + 'month" onchange="buildDaySelect(\'' + name + '\');">');
	for(var i=0;i<aMonths.length;i++){
		document.write('<option value="' + (i+1) + '"');
		if(i==theMonth) document.write(' selected');
		document.write('>' + aMonths[i] + '</option>');
	}
	document.write('</select>&nbsp;');
	document.write('<select name="sel' + name + 'year" onchange="buildDaySelect(\'' + name + '\');">');
	document.write('<option value="' + theYear + '" selected>' + theYear + '</option>');
	for(var i=1;i<=yearsahead;i++){
		document.write('<option value="' + (theYear+i) + '">' + (theYear+i) + '</option>');
	}
	document.write('</select>');
}	

function getDaysInMonth(month,year){
	var daysInMonth = 31;
	if(month == 4 || month == 6 || month == 8 || month == 11) daysInMonth = 30;
	if(month == 2) daysInMonth = 28;
	if(month == 2 && (((year/4) == Math.floor(year/4)) && (((year/100)!=Math.floor(year/100)) || (year/400)==Math.floor(year/400)))) daysInMonth = 29;
	return daysInMonth;
}

function buildDaySelect(name){
	var dayobject = eval("document.frmEvent.sel" + name + "day");
	var monthobject = eval("document.frmEvent.sel" + name + "month");
	var yearobject = eval("document.frmEvent.sel" + name + "year");
	var today = dayobject.selectedIndex + 1;
	var month = monthobject[monthobject.selectedIndex].value;
	var year = yearobject[yearobject.selectedIndex].value;
	var daysinmonth = getDaysInMonth(month, year);
	dayobject.length = 0;
	for(var i=1;i<=daysinmonth;i++){
		selected = (today==i) ? 1: 0;
		dayobject.options[dayobject.options.length] = new Option(i, i, false, selected);
	}
	if(today > dayobject.length) dayobject.selectedIndex = dayobject.length-1;
}

function click_tracker(type, id){
	var req;
	try {
		req = window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e){
	}
	req.open("GET", "/common/script/click-tracker.php?id=" + id + "&type=" + type);
	req.onreadystatechange = function(){
		if ((req.readyState == 4) && (req.status == 200)){
			//alert(req.responseText);
		}
	}
	req.send(null);
	return true;
}

function updateOrder(list){
	var url = '/common/script/update-order.php';
	var params = Sortable.serialize(list.id,{name:'list'});
	var ajax = new Ajax.Request(url,{method:'post',parameters:params});
}

function validEmail(frm){
	var regpat = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*\s+<(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})>$|^(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})$/;
	var b = regpat.test(frm.txtEmail.value);
	if(!b){
		alert("please enter a valid email address");
	}
	return b;
}