function tinyMCELoad() {
	tinyMCE.init({
		// General options
		editor_selector : "wysiwyg",
		mode : "textareas",
		theme : "advanced",
		relative_urls : false, 
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

		// Theme options
		theme_advanced_buttons1 : "fullscreen,|,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,sub,sup,|,preview",
		theme_advanced_buttons3 : "forecolor,backcolor,|,tablecontrols,|,charmap,emotions,media,advhr,hr|,styleprops,visualchars,nonbreaking,|,insertlayer,|,template",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example content CSS (should be your site CSS)
		content_css : "../style.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",
		
		file_browser_callback : "tinyBrowser",
		
		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
	
}

function datePickers() {
	$(document).ready(function() {
		if ($('.datepicker').length)
			$('.datepicker').datepicker({ dateFormat: 'dd-M-yy' });
   });
}

$(document).ready(function() {
	datePickers();
});

//FUNCTION TO VALIDATE FORMS
function formCheck(form) {
	
	if(form == '' || form == 'undefined' || form == undefined || form == null) form = '';
	
	returnVal = false;
	$(".requiredField").each(function() { 

		//alert(document.getElementsByTagName("form").length);
		
		//alert($(this).parent("form").attr("name"));
		//alert(form);
		type = this.tagName.toLowerCase();

		if(form == '' || (form != '' && $(this).parents("form").attr("name") == form)) {
		
			if(type=='select') {
				if($(this).val()=='') {
					$(this).css("background","#ecfea5");
					returnVal = true;
				} else {
					$(this).css("background","#FFFFFF");
				}
			}
			
			if(type=='textarea') {
				if($(this).val()=='') {
					$(this).css("background","#ecfea5");
					returnVal = true;
				} else {
					$(this).css("background","#FFFFFF");
				}
			}
			
			if(type=='input' && $(this).attr("type")=='text') {
				if($(this).val()=='') {
					$(this).css("background","#ecfea5");
					returnVal = true;
				} else {
					$(this).css("background","#FFFFFF");
				}
			}
			
			if(type=='input' && $(this).attr("type")=='password') {
				if($(this).val()=='') {
					$(this).css("background","#ecfea5");
					returnVal = true;
				} else {
					$(this).css("background","#FFFFFF");
				}
			}
			
			if(type=='input' && $(this).attr("type")=='checkbox') {
				if(!$(this).checked) {
					$(this).css("background","#ecfea5");
					returnVal = true;
				} else {
					$(this).css("background","#FFFFFF");
				}
			}
			
			if(type=='input' && $(this).attr("type")=='radio') {
				if(!$(this).checked) {
					$(this).css("background","#ecfea5");
					returnVal = true;
				} else {
					$(this).css("background","#FFFFFF");
				}
			}
		}
			
	});

	// hard coded check for filecheck_url
	
	$('#filecheck_url').each(function() {
		
		if ($(this).text() != "File name OK") {
			$('.filecheck').css("background","#ecfea5");
			returnVal = true;
		}
		
	});
	
	
	if(returnVal==true) {
		alert("Please complete/double check all highlighted fields");
		return false;
	} else {
		return true;	
	}
	
}

//Ajax loadThis function
var xmlhttp = new Array();
var i = 0;

function loadThis(url, destination, functocall, showLoadAnimation, ajaxWidth, animation, action) {
	
	animationArray = new Array();
	animationArray[1] = 'ajax_iamloading.gif';
	animationArray[2] = 'ajax-loader-bar.gif';
	animationArray[3] = 'ajax-loader-flower.gif';

	if(!ajaxWidth) 	ajaxWidth = 400;
	if(!animation) 	animation = 1;
	if(!action) 	action = 'replace';

	if(!destination && document.getElementById("ajaxContainer")) {
		$('#ajaxContainer').remove();
		$('#coverImage').remove();
	}	
	
	if (!destination) {
		destination = "ajaxContent";
		
			winHeightD	= $(document).height(); 

			if( typeof( window.innerWidth ) == 'number' ) {
				//Non-IE
				winWidth = window.innerWidth;
				winHeight = window.innerHeight;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				//IE 6+ in 'standards compliant mode'
				winWidth = document.documentElement.clientWidth;
				winHeight = document.documentElement.clientHeight;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				//IE 4 compatible
				winWidth = document.body.clientWidth;
				winHeight = document.body.clientHeight;
			}

			sWinHeight = 0;
			if( typeof( window.pageYOffset ) == 'number' ) {
				//Netscape compliant
				sWinHeight = window.pageYOffset;
			} else if( document.body &&  document.body.scrollTop  ) {
				//DOM compliant
				sWinHeight = document.body.scrollTop;
			} else if( document.documentElement && document.documentElement.scrollTop  ) {
				//IE6 standards compliant mode
				sWinHeight = document.documentElement.scrollTop;
			} 
			winHeightD = winHeightD-5;

			$(document.body).append('<div id="coverImage" style="height:'+winHeightD+'px"></div>');
			
			$(document.body).append('<div id="ajaxContainer" style="z-index:20; display:none; width:'+ajaxWidth+'px"><div id="ajaxContentBox" class="png"><div id="ajaxContent" class="png" style="width:'+(ajaxWidth-30)+'px"></div><a href="javascript:" onclick="closeAjax();" id="ajaxClose_btn" title="close" class="png"><img src="admin/images/ajax_close-big.png" border="0" alt="Close" class="png" width="48px" height="48px" /></a></div></div>');

	} else {
		if (showLoadAnimation) {
			// show loading image
			destHight = ($('#'+destination).height())-2;
			$('#'+destination).empty();
			if(animation==1) {
				$('#'+destination).append('<div ID="ajaxLoader"><div ID=ajaxLoadIcon style="background:url(admin/images/'+animationArray[animation]+') center center no-repeat;"></div></div><div style="clear:both"></div>');
			} else {
				$('#'+destination).append('<div ID=ajaxLoadIcon style="padding:0; height:'+destHight+'px; background:url(admin/images/'+animationArray[animation]+') center center no-repeat;"></div><div style="clear:both"></div>');	
			}
			
		}
	}
	
	xmlhttp[i]=null
	if (functocall=="undefined") functocall = "";
	if (destination=="undefined") destination = "";
	if (window.XMLHttpRequest) {
		xmlhttp[i] = new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		xmlhttp[i] = new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	if (xmlhttp[i]!=null) {
		var currentTime = new Date();
		xmlhttp[i].onreadystatechange=new Function("loaded("+i+",'"+destination+"','"+functocall+"','"+action+"')")
		//xmlhttp[i].open("GET",baseurl+url,true)
		xmlhttp[i].open("GET",url,true)
		xmlhttp[i].send(null)
	} else {
		alert("Your browser does not support AJAX")
	}
	i=i+1
	
}
					
function loaded(b,destination,functocall,action) {

	if (xmlhttp[b].readyState==4 || xmlhttp[b].readyState=="complete") {
		
		if (xmlhttp[b].status == 200) {
			if (destination != "undefined" && destination != "" && destination!="hidden" && destination != null && xmlhttp[b].responseText!=''){ 
				//document.getElementById(destination).innerHTML = xmlhttp[b].responseText;
				if(action=='replace') {
					$('#'+destination).html(xmlhttp[b].responseText);
				} else if(action=='append') {
					$('#'+destination).append(xmlhttp[b].responseText);
				} else if(action=='prepend') {
					$('#'+destination).prepend(xmlhttp[b].responseText);
				} else if(action=='before') {
					$('#'+destination).before(xmlhttp[b].responseText);
				} else if(action=='after') {
					$('#'+destination).after(xmlhttp[b].responseText);
				}

				adjustAjax();
				datePickers();
			}
			if (functocall!="undefined" && functocall!="") {
				
				if (functocall.indexOf('(') > -1)
					eval(functocall);
				else 
					eval(functocall+'()');
			}
		} else {
			//alert("Problem retrieving data")
			//alert(xmlhttp[b].status)
		}
	}
}

function adjustAjax() {
	
	winHeightD	= $(document).height(); 
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
	}

	sWinHeight = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		sWinHeight = window.pageYOffset;
	} else if( document.body &&  document.body.scrollTop  ) {
		//DOM compliant
		sWinHeight = document.body.scrollTop;
	} else if( document.documentElement && document.documentElement.scrollTop  ) {
		//IE6 standards compliant mode
		sWinHeight = document.documentElement.scrollTop;
	} 
	
	acWidth		= $('#ajaxContainer').width();
	acHeight	= $('#ajaxContainer').height();

	pHeight = (winHeight/2)-(acHeight/2)+sWinHeight;
	pWidth = (winWidth/2)-(acWidth/2);
	
	addHeight = 0;
	
	if(pHeight<=0) { 
		pHeight = 50;
		$('#coverImage').height(acHeight+pHeight);
	}
	
	$('#ajaxContainer').css('top',(pHeight)+'px');
	$('#ajaxContainer').css('left',pWidth+'px');
	$('#ajaxContainer').css('z-index','100');
	
	$('#ajaxContainer').fadeIn("fast");		
}

$(window).scroll(function () { 
	if (jQuery.browser.msie && jQuery.browser.version.substr(0,1)>6) {

		//adjustAjax(); 
	}
});

function closeAjax() {
	$('#ajaxContainer').fadeOut("fast",function callback() {$('#ajaxContainer').remove()});
	$('#coverImage').fadeOut("fast",function callback() {$('#coverImage').remove()});
	$('#wrapper select').each(function(){ $(this).show(); });
}

function systemMessage(text) {

	text = text.replace(/~/g,"'");

	$(document.body).append('<div id="systemMessage"><div style="width:258px; height:108px; position:relative;"><div style="position: absolute; top: 46%;display: table-cell; vertical-align: middle; text-align:center; width:258px; font-size:12px; margin:0px 0px 0px 10px"><b>'+text+'</b></div></div></div>');
	
	winHeightD	= $(document).height(); 
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
	}

	sWinHeight = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		sWinHeight = window.pageYOffset;
	} else if( document.body &&  document.body.scrollTop  ) {
		//DOM compliant
		sWinHeight = document.body.scrollTop;
	} else if( document.documentElement && document.documentElement.scrollTop  ) {
		//IE6 standards compliant mode
		sWinHeight = document.documentElement.scrollTop;
	} 
	
	acWidth		= $('#systemMessage').width();
	acHeight	= $('#systemMessage').height();
	
	pHeight = ((winHeight/2)-(acHeight/2))-50;
	pWidth = (winWidth/2)-(acWidth/2);
	
	$('#systemMessage').css('top','-120px');
		
	$(document).ready(function() {
		$('#systemMessage').css('left',pWidth+'px');
		$('#systemMessage').css('z-index','100');

		$('#systemMessage').animate({ 
			top: pHeight+'px',
			left: pWidth+'px'
		}, 800, "swing", function() {		  
			  $('#systemMessage').animate({ 
					top: pHeight-150+'px'
				}, 200, "swing", function() { 
					//textLength = text.length;
					//timeoutTime = Math.round(textLength/34);
					//setTimeout('removeSystemMessage()',timeoutTime*1200);
				});
			}
		);
		
		$('#systemMessage').click(function() {
			removeSystemMessage();		
		});
	});
}

function removeSystemMessage() {
	$('#systemMessage').fadeOut("fast",function callback() {$('#systemMessage').remove()});
}


function loadMap() {

	
	$(document.body).append('<iframe ID="mapFrame" frameborder=0 marginheight=0 marginwidth=0 scrolling=no width=900 height=630 style="position:absolute;top:2px;left:50%;margin-left:-450px;padding:0" src="ext/callHandler.php?task=map"></iframe>');
			
	

	$(document.body).append('<div ID="blocker" style="display:none;position:fixed;_position:absolute;height:100%;width:100%;top:0;left:0;background:#000000;z-index:1;"></div>');
	$('#blocker').css({"opacity": "0.2"});
	$('#blocker').fadeIn("slow");
	
	$('#blocker').click(function() {
		
		$('#mapFrame').remove();
		$(this).remove();
		
	});

}

function addKeyword() {
	

	curkwds		 = $('#keywords').val();
	
	group		 = $('#key_group').val();
	individual	 = $('#key_individual').val();
	
	overview	 = $('#key_overview').val();
	relationship = $('#key_relationship').val();
	
	if ((group != '' || individual != '') &&  (overview != '' || relationship != '')) {
		kwds = new Array();
		rels = new Array();
		outString = '';
		
		if (group != '') {
			// do clever stuff
			kwds = group.split('|');
		} else {
			kwds[0] = individual;
		}
		
		if (overview != '') {
			// do clever stuff
			rels = overview.split('|');
		} else {
			rels[0] = relationship;
		}
		
		for (i=0;i<kwds.length;i++) {
			for (j=0;j<rels.length;j++) {
			
				outString += kwds[i]+'#'+rels[j]+',';
			
			}
		}
		
		outString = outString.substr(0,outString.length-1);
		
		if (curkwds != '')
			outString = ','+outString;
		
		$('#keywords').val(curkwds + outString);
		
	} else {
		alert('Please select either a group OR an individual as well as the relationship overview OR specific.');	
	}
}