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



function timestamp()
{
	var now = new Date();
	var ts = "&uqtimestamp=" + now.getTime()
	return ts
}


function getUrl(url,pars,div)
{	
	if(typeof div == "string")
	{
		if( $(div) != null)
			var div = $(div) 
		else
			return
	}	
		
	div.innerHTML = "<img src='../media/img/loading-circle.gif' border='0'>"		
	pars = pars + timestamp()
	//alert(pars)
	
	var myAjax = new Ajax.Updater( 		  
	    {  success: div },  
       	url, 
       	{ 
           	method: 'get',
			parameters: pars,
			evalScripts: true,
			//onComplete: function(req){ alert( req.responseText ) },
			onFailure: function(req){ div.innerText = req.responseText },
			onException: function(req){ div.innerText = req.responseText }
       	});
}	

function postUrl(url,pars,div)
{	
	if(typeof div == "string")
	{
		if( $(div) != null)
			var div = $(div) 
		else
			return
	}	
	
	div.innerHTML = "<img src='../media/img/loading-circle.gif' border='0'>"		
	pars = pars + timestamp()
	//alert("POSTURL():\n"+pars)
	
	var myAjax = new Ajax.Updater( 		  
	    {  success: div },  
       	url, 
       	{ 
           	method: 'post',
			parameters: pars,
			evalScripts: true,
			//onComplete: function(req){ alert( trimString(req.responseText) ) },
			onFailure: function(req){ alert("onFailure:"+req.responseText); div.innerText = req.responseText; },
			onException: function(req){  alert("onException:"+req.responseText);  div.innerText = req.responseText; }
       	});
	
}	

function postUrlRequest(url,pars,oFunct)
{	
	if(typeof oFunct == "string")
	{
		if( $(oFunct) != null)
			var oFunct = $(oFunct)
		else
			var oFunct = function(){void(0)}
	}	
	
	
	var erDiv = $('ajViewReturn')
	pars = pars + timestamp()
	//alert(pars)
	
	var myAjax = new Ajax.Request( 				   
       	url, 
       	{ 
           	method: 'post',
			parameters: pars,
			onComplete: oFunct,
			onFailure: function(req){ erDiv.innerText = req.responseText },
			onException: function(req){ erDiv.innerText = req.responseText }
       	});
}	




function postUrlRequestAsync(url,pars,oFunct)
{	
	//var erDiv = $('ajaxError')
	pars = pars + timestamp()
	//alert(pars)
	//alert(url)
	var myAjax = new Ajax.Request( 				   
       	url, 
       	{ 
           	method: 'post',
			asynchronous: true,
			parameters: pars,
			onSuccess: function(r){ alert("onSuccess: "+r)  },
			onFailure: function(r){ alert("onFailure: "+r)  },
			onException: function(r,s){ alert("onException: " + r + " | " + s )}
       	});
	//alert("after the request call")
}	




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;
}
/* Functions that swaps images. */
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;
}

/* Functions that handle preload. */
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];}}
}

var picwin = null;
function openwin(name,passwindowprops)
{
  if ( ( picwin == null )
    || picwin.closed )
  {
    // the window has been closed, or never opened yet
    // --> open it
    if(passwindowprops==0){
        var windowprops = "width=750,height=650,toolbar=0,location=0,directories=0,status=0,menuBar=1,scrollBars=1,resizable=1,top=15,left=15";
    } else {
        var windowprops = passwindowprops;
    }  
		//var windowprops = "width=420,height=625,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1,top=15,left=15";    
    picwin = window.open(name,'picWindow',windowprops);
  }
  else
  {
    // window is opened somewhere -> bring it in front
    picwin.location.href=name;
		picwin.focus();
  }
}

/*  ================================================================
    FUNCTION:  isCreditCard(st)
 
    INPUT:     st - a string representing a credit card number

    RETURNS:  true, if the credit card number passes the Luhn Mod-10
		    test.
	      false, otherwise
    ================================================================ */

function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19){
	  alert('The Credit Card field contains some invalid values:\n\n');
    return (false);
  }
	// 
  if (st.length == 0 || st.value ==''){
	  alert('The Credit Card field is required:\n\n');
    return (false);
  }
  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  
  if ((sum % 10) == 0){
    return (true);
  }else{
	  alert('The Credit Card field contains some invalid values:\n\n');
    return (false);
  }
} // END FUNCTION isCreditCard()



function viewHideLink(obj,linkId)
{
	if(obj.style.display == "none")
	{
		obj.style.display = ""
		if($(linkId))
		{
			var ntxt = $(linkId).innerHTML.replace(/view/,"hide")	
			$(linkId).innerHTML = ntxt
		}
	}
	else if(obj.style.display == "")
	{
		obj.style.display = "none"
		if($(linkId))
		{
			var ntxt = $(linkId).innerHTML.replace(/hide/,"view")	
			$(linkId).innerHTML = ntxt			
		}			
	}
} 


function trimString (str) 
{
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}  






function viewHideLinkSelector(parent,selector,linkId)
{
	var objArray = $(parent).getElementsBySelector(selector)
	
	//alert(objArray)
	if(objArray.length)
	{
		//alert(objArray.length)
		for(i=0;i<objArray.length;i++)
		{
			var obj = objArray[i]			
			if(obj.style.display == "none")
			{
				obj.style.display = ""
				if($(linkId))
				{
					var ntxt = $(linkId).innerHTML.replace(/view/,"hide")	
					$(linkId).innerHTML = ntxt
				}
			}
			else if(obj.style.display == "")
			{
				obj.style.display = "none"
				if($(linkId))
				{
					var ntxt = $(linkId).innerHTML.replace(/hide/,"view")	
					$(linkId).innerHTML = ntxt			
				}			
			}
		}	
	}	
}




xmlreplace = function(txt)
{
	//this function catches variables that are "bad"
	//for example the 2 below are the "evil" quotes created by MS Word some times but they are hated by xml
	// “   and   ”
	//txt = txt.replace(/“/g,'"');
	//txt = txt.replace(/”/g,'"'); 
	//we escape the text so that the ticks are in ascii form
	//i am sure that there are going to be a few more of these,  I have run into them before but without 
	// the %xxxxx value I cant replace them
	//			txt = txt.replace("’","'");  //evil tick
	//			txt = txt.replace("‘","'");  //evil tick
	//			txt = txt.replace("…","'");  //evil elipse
	// I also have a partial list of ones that have caused issues so far.
	// http://www.ss64.com/ascii.html 
	txt = escape(txt)  
	txt = txt.replace(/%u201C/g,'"');
	txt = txt.replace(/%u201D/g,'"');
	txt = txt.replace(/%u2018/g,"'"); 
	txt = txt.replace(/%u2019/g,"'");   
	txt = txt.replace(/%B2/g,'<sup>2</sup>'); //footnote 2
	txt = txt.replace(/%B3/g,'<sup>3</sup>'); //footnote 3
	txt = txt.replace(/%B9/g,'<sup>1</sup>');   //footnote 1
	txt = txt.replace(/%BC/g,'1/4');
	txt = txt.replace(/%BD/g,'1/2');
	txt = txt.replace(/%BE/g,'3/4');
	txt = txt.replace(/%D7/g,'x');
	txt = txt.replace(/%B7/g,'x');
	txt = txt.replace(/%F7/g,' / ');
	txt = txt.replace(/%3C/g,'<');
	txt = txt.replace(/%3E/g,'>');
	txt = txt.replace(/%u2013/g,'-');
	txt = txt.replace(/%u2014/g,'-'); 
	//alert(txt)
	loc = txt.search( /%u..../) 
	loc2 = txt.search( /&/)
	if(loc>=0 || loc2>=0)
	{
		invalid = txt.substring(loc,loc+6)
		loc = unescape(txt).indexOf(unescape(invalid))
		msg = "Found a bad character in your transcription at character "+loc+" \n"
		msg +="If this has been copied from Microsoft Word some characters can cause problems \n";
		msg +="\nCheck for fractions, super script/footnotes and special symbols"
		msg +="\n\nThe bad character is  "+unescape(invalid)+"  or '"+invalid+"'"
		alert(msg)
		return false
	}
	//txt = txt.replace(/%u..../g,"44");  //replace all %uXXXX unicoded hex values since they all error out the code
	txt = unescape(txt)
	return txt
}






var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();