//<!--
//alert ("functions JS Included");

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 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 Validate(oForm,verbose){
index=0;
var message='';
var loop=1;
var Style;
var validForm=true;
var validField=false;
var oTest;
var nElements=oForm.elements.length;
var formfeedback=oForm.getAttribute("feedback");
while(index<nElements){
oTest=oForm.elements[index];
type=oTest.type;
customScript=oTest.getAttribute("customScript");
name=oTest.getAttribute("name");
field=oTest.value;
fieldfeedback=oTest.getAttribute("feedback");
max=oTest.getAttribute("max");
min=oTest.getAttribute("min");
vtype=oTest.getAttribute("vtype");
vname=oTest.getAttribute("vname");
dField=oTest.getAttribute("dfield");
dValue=oTest.getAttribute("dvalue");
mandatory=oTest.getAttribute("mandatory");
warning=oTest.getAttribute("warning");
validCustom=true;
validFormat=true;
validRange=true;
validChars=true;
validMandatory=true;
validSelect=true;
validRadio=true;
if(customScript!=null){
validCustom=(eval(customScript))}
else{
validCustom='true';
if(dField!=null){
var dVal=eval("document.getElementById('"+dField+"')")
if(dVal.type=='radio'||dVal.type=='select-one'||dVal.type=='checkbox'){
var dStatus =(dValue=='true') 
if(dVal.status==dStatus)
mandatory='true'
else
mandatory='false'}		
else{
if(!isEmpty(dVal.value))
mandatory='true'
else
mandatory='false'}}
switch(type){
case'password':
case'text':
case'textarea':
if(!isEmpty(field))	{
validChars=true;
switch(vtype){
case'text':
field=field.replace(/"/g,"`");
validFormat=true;
break;
case'numeric':
validFormat=(field.search(/^[-|+]*\d*.{1}\d*$/)!=-1);
break;
case'email':
validFormat=(field.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)!=-1);
break;
case'url':
validFormat=(field.search(/^((\w+):\/\/)([\w.]+)(\/)?(\S*)$/)!=-1);
break;
case'Date'://uk format
validFormat=(field.search(/^((mon|tue|wed|thur|fri|sat|sun){1}\w{0,3}(day)?)(\\|\/|\s|.|-)?(1|2|3|)[0-9]((st|nd|rd|th)?){1}(\\|\/|\s|.|-){1}(((jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)(r|((t)?e)|o|y)?(uary|ch|((m)?ber)|il|aust)?)|((1|2)?[0-9])){1}(\\|\/|\s|.|-){1}((19|20)?\d{2})$/)!=-1);
break;
case'Time':
validFormat=(field.search(/^[0-2]{0,1}\d[:|.]*\d\d\s*([a|p].*m.*)*$/)!=-1);
break;
case'Date/Time':
validFormat=(field.search(/^[0-2]{0,1}\d[:|.]*\d\d\s*([a|p].*m.*)*((mon|tue|wed|thur|fri|sat|sun){1}\w{0,3}(day)?)?(\\|\/|\s|.|-)?(1|2|3|)[0-9]((st|nd|rd|th)?){1}(\\|\/|\s|.|-){1}(((jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)(r|((t)?e)|o|y)?(uary|ch|((m)?ber)|il|aust)?)|((1|2)?[0-9])){1}(\\|\/|\s|.|-){1}((19|20)?\d{2})$/)!=-1);
break;
case'filename':
validFormat=(field.search(/^((\\\\[\w]+)|([A-Za-z]:\\))?([(\w|\s)+]+\\)?[\w]+[.{1}](jpg|bmp|gif|jpeg|pdf|doc|xls|mdb|ppt|txt|rtf)$/)!=-1);
break;
case'phone':
validFormat=(field.search(/^\d{4}\s*\d\s*\d{2}\s*\d\s*\d\s*\d{2}$/)!=-1);	
break;	
case'CC':
validFormat=(field.search(/^\d{4}\s*\d{4}\s*\d{4}\s*\d{4}\s*$/)!=-1);
break;	
case'Postcode':
//validFormat=(field.search(/^[A-Za-z]{1,2}[1-9]{1,2}\s?[1-9][A-Za-z]{2}$/)!=-1);
break;	
case'password':
//validFormat=(field.search(\w)!=-1);
break;}			
switch(vtype){
case'numeric'	:
if(min!=null){
if(field<min){
validRange=false;
msgRange="\nIt is smaller then the minimum allowed value of "+min+"."}}
if(max!=null){
if(field>max){
validRange=false;
msgRange="\nIt is larger then the maximum allowed value of "+max+"."}}	
default:
if(min!=null){
if(field.length<min){
validRange=false;
msgRange="\nIt is smaller then the minimum allowed size of "+min+"."}}
if(max!=null){
if(field.length>max){
validRange=false;
msgRange="\nIt is larger then the maximum allowed size of "+max+"." }}}}		
else{
if(mandatory=='true')
validMandatory=false;}	
break;	
case'checkbox':
case'radio':
validRadio=false;
while(index<nElements){
oTest=oForm.elements[index];
if(oTest.name!=name){
index--
break;}
if(mandatory=='true')
validRadio=(validRadio||oTest.status);
else{
validRadio=true
validMandatory=true}
index++}
break;
case'hidden':		
case'button':		
break;
case'select-one':
if((oTest.selectedIndex==-1)||(oTest.value==''))
{
if(mandatory=='true')
validMandatory=false;
else
validMandatory=true;
}
else
validMandatory=true;
break;
default:
break;}}
validField=((validCustom=='true')&&validFormat&&validRange&&validChars&&validMandatory&&validRadio)
if(!validField){
if(formfeedback!='none'||fieldfeedback!=null){
message+='\nYou have not correctly filled in the '+vname+' field.'
if(!validChars)
//message+="\nIt contains invalid characters."
if(!validFormat)
message+="\n'+vname+' is not in the correct format."
if(!validRange)
//message+=msgRange
if(!validRadio)
//message+="\nDoes not have a value selected."
if(!validMandatory)
//message+="\n'+vname+' is a required field and does not appear to contain a value."
if(validCustom!='true')
message+=validCustom}}
if(!validField)
Style='#ff6600';
else
Style='';
switch(type){
case'select-one':
if(oTest.parentElement.tagName!='SPAN'){
var oMySelect=oTest;
var oNewSpan=document.createElement("SPAN");
oMySelect.parentElement.insertBefore(oNewSpan,oMySelect);
var oClone=oMySelect.cloneNode(true);
oNewSpan.insertAdjacentElement("afterbegin",oClone);
oMySelect.removeNode(true);
oClone.selectedIndex =oMySelect.selectedIndex;}
else
oNewSpan=oTest.parentElement
if(!validField)
oNewSpan.style.border="2px solid "+Style;
else
oNewSpan.style.border="none"
break;
default:
oForm.elements[index].style.borderColor=Style;
break;}
validForm=(validForm&&(validField||(warning=='true')))
if((formfeedback=='incremental'||fieldfeedback=='incremental')&&!validField)
break;
index++}
//if(!verbose)
//return true;
if(validForm)
return validForm;
else{
if(formfeedback!='none'||fieldfeedback=='incremental'){
message+='\nPlease correct the appropriate entries and resubmit.'
alert(message)}
return validForm;}}
function isEmpty(str){
if(str.length==0){
return true;}
if(str.search(/\S/)==-1){
return true;}
return false;}

function replaceImage(objImg, imagePath){

	objImg.src = imagePath

}

function swapBullet(oElement,Value)
{

}
function DblClickHandler(DblClick)
	{
	
		//alert("called");
		//alert(document.getElementById('image').getAttribute('elementType'));
		if (DblClick.target)
		{
			//alert("mozilla");
			DblClickTarget = DblClick.target;
			DblClickElement = DblClickTarget;
		}
		else
		{
			//alert("IE")
			DblClickElement = DblClick.srcElement	
			
		}
		
		if (DblClickElement)
		{
			//alert('hello');
			if (DblClickElement.getAttribute('elementType') != "")
			{
				//alert('exists');
				DblClickElementType = DblClickElement.getAttribute('elementType');
				switch (DblClickElementType)
				{
					case 'Image':
						//alert('Image');
						imagedetails = showModalDialog("/admin/dialogs/image_swapper.asp?time=<%=now()%>&element="+DblClickElement.id,DblClickElement,"dialogWidth:500px;dialogHeight:325px;scroll:no; status:no;help:no;");
					//	alert(imagedetails);
						if (imagedetails)
						{
							alert(imagedetails);
							imgdetails = imagedetails;
							//imgdetails = imagedetails.split("¬")
							//alert(imagedetails);
							//DblClickElement.src = imgdetails[0];
							//DblClickElement.border = " 0 ";
							
							if (DblClickElement.parentNode.tagName == "A")
							{
								if (imgdetails == "None")
								{
								//	alert("Deleting parent node")
									par = DblClickElement.parentNode;
									spar = par.parentNode;
									spar.removeChild(par);
									spar.appendChild(DblClickElement)
									
									//par = DblClickElement.parentNode;
									//spar = par.parentNode
									//spar[0] = DblClickElement

									//document.deleteElement(DblClickElement.parentNode);
								}
								else
								{
								//	alert("Editing href details");
									DblClickElement.parentNode.href = imgdetails;
									DblClickElement.parentNode.title = " " + DblClickElement.alt + " ";
								}
							}
							else
							{
								if (imgdetails != "None")
								{
									alert("Creating ParentElement");
									
									hyp = document.createElement("A");
									hyp.href = imgdetails;
									hyp.title = " " + imgdetails + " ";
									
									thehref = DblClickElement.parentNode.appendChild(hyp)
									thehref.appendChild(DblClickElement)
									
								}
							}
							fetchcontainingdiv(DblClickElement);
							
						}
						break;
					default:
						break;
				}
				
			
			}
		}
}
var Modules
function formatbutton(e)
{
	if (!e) var e = window.event;
	var action = e.type;
	//alert(action);
	if (e.srcElement) var element = e.srcElement
	else if (e.target) var element = e.target
	//alert(element);
	element.className = "btn_"+action;
}
function exec(cmd,opt)
{
	//alert(LastModuleID);
	//document.execCommand('ForeColor',null,'#ff0000');
	//alert(cmd);
	//alert(document.getElementById(ModuleID));
	try{
		switch (cmd)
		{
			case 'adddoctor':
							break;
			case 'otherstaff':
							break;
			default:	var ModuleID = Modules[0]
						var oModule	= document.getElementById(ModuleID);
						var Type	= oModule.getAttribute('type');
		}
	
		
	}
	catch(err)
	{
	alert('No Modules have been edited/selected since your last save!')
	return null;
	}
	
	//var view = document.getElementById(Window).getAttribute('view');
	//if (view=='html')
	//{
	//EditWindow = document.getElementById(Window)
		
		switch(cmd)
		{
			case 'save': switch(Type)
							{
							
							case 'text'		: document.getElementById('updater').src = '/_includes/update_module.asp?';break;
							case 'branch'	: document.getElementById('updater').src = '/_includes/update_branch.asp';break;
							case 'times'	: document.getElementById('updater').src = '/_includes/update_times.asp';break;
							case 'partner'	: document.getElementById('updater').src = '/_includes/update_practice.asp';break;
							case 'staff'	: document.getElementById('updater').src = '/_includes/update_staff.asp';break;
							case 'rightmenu': document.getElementById('updater').src = '/_includes/update_rightmenu.asp';break;
							case 'meta'		:document.getElementById('updater').src = '/_includes/update_meta.asp';break;
							case 'access'	:document.getElementById('updater').src = '/_includes/update_access.asp';break;
							default: alert('Error!');break;
						   	}
						   	break;
			case 'createimage':
							Stamp = new Date();
							var Middles
							var myObject = new Object();

							Middles = Modules[Modules.length-1]
							Middeh = document.getElementById(Middles)
							myObject.moo = Middeh
							myObject.moop = document
							

							cimage = showModalDialog('/admin/dialogs/insert_image.asp?now='+Stamp,myObject, 'dialogWidth:700px;dialogHeight:300px;scroll:no; status:no;help:no;');
							if (cimage)
							{
								cimage = cimage.split("¬");
								
								if ((cimage[0] != 'none') && (cimage[0] != ''))
								{
									NewHyperlink = document.createElement(cimage[0]);
									NewImage = document.createElement(cimage[1]);
									NewHyperlink.appendChild(NewImage);
									Middeh.appendChild(NewHyperlink);
								}
								else
								{
									NewImage = document.createElement(cimage[1]);
									Middeh.appendChild(NewImage);
								}
								
								
								
							}
							
							break;
			case 'removeparent':
							var sText = document.selection.createRange();
							sParent = sText.parentElement();
							
							if (sParent.tagName == "P")
							{
								

								sParentText = sParent.innerHTML;


								NewChild = "<mid>"+sParentText+"</mid>";
								//alert(NewChild);
								InsertChild = document.createElement(NewChild);

								ParentParent = sParent.parentElement;
								ParentParent.removeChild(sParent);
								//TNode = document.createTextNode(escape(sParentText));
								sText.pasteHTML(sParentText)
								//TNode = document.createTextNode(escape(sParentText));
								//alert(sParentText);
								//ParentParent.appendChild(TNode);
								
							}
							else
							{
								alert("Can not remove the parent element of this text");
							}
							//alert(sParent.tagName);
							break;
			case 'download' :
							Stamp = new Date();
							showModalDialog('/admin/dialogs/add_download.asp?now='+Stamp, document, 'dialogWidth:700px;dialogHeight:300px;scroll:no; status:no;help:no;');
							break;

			case 'View':	toggleSource(LastModule);
							break;
			case 'Lock':	toggleMode(LastModule);
							break;
			case 'palette':	//togglePalette(LastModule);
							PosY = event.offsetY;
							PosX = event.offsetX;
							LMode = document.getElementById(Modules[Modules.length-1]);
							
							togglePalette(PosY,PosX);
							break;	
			case 'Spell':	EditWindow.focus();
							toggleSource(LastModule);
							spell(LastModule);
							break;
			case 'adddoctor':window.location.href='/staff/doctor/addnew/';
							break;
			case 'otherstaff'	:window.location.href='/staff/other/addnew/';
			break;
			case 'link':Stamp = new Date();
							showModalDialog('/admin/dialogs/add_link.asp?now='+Stamp, document, 'dialogWidth:700px;dialogHeight:300px;scroll:no; status:no;help:no;');
							break;
			default:
							//if (view == 'source')
							//{
							//	if (confirm('You cannot format the text in source code view. Click OK to Switch to HTML mode?'))
							//	{
							//		toggleSource(LastModule);
							//		break;
							//	}	
							//	else return;
							//}
							//else
							//{
							document.execCommand(cmd,false,opt);
						//}
			
		//}
	}
}	

function togglePalette(Y,X)

{
	//alert(LastModule);
//	alert(Y);
//	alert(X);
	if (document.getElementById('Palette').style.display == "")
	{
	document.getElementById('Palette').style.display = "none";
	} 
	else
	{
	
	//alert(document.getElementById('Palette').style.position);
	//document.getElementById('Palette').style.position = "relative";
	Y -= 8;
	//X += <%=12 * 26%>;
//	alert(X);
	//document.getElementById('Palette').style.top = Y+"px";
	//document.getElementById('Palette').style.left = X+"px";
document.getElementById('Palette').style.display = "";
	
	}
}


function fetchcontainingdiv(oElement)
{
	if (oElement.parentNode.tagName == "DIV")
	{
		//check if they have a div starter kick
		if (oElement.getAttribute('type') == "text" || oElement.getAttribute('type') == "rightmenu")
		{
		//	alert("Found")
		//	alert(oElement.getAttribute('id'))
			returnvalue = oElement.getAttribute('id');
			//alert("returnValue="+returnvalue);
			if (Modules==undefined)
			{
				Modules = new Array(1);
				Modules[0] = returnvalue;
				document.title = '*'+ document.title;
			}
			else
			{
				Modules[Modules.length] = returnvalue;
			}
			return returnvalue;
		}
		else
		{
			//alert("Tag name Div but not parent node");
			fetchcontainingdiv(oElement.parentNode);
		}
	}
	else
	{
	//	alert("tag name not div");
		fetchcontainingdiv(oElement.parentNode);
	}
}
function notSaved()
{
if (Modules){
alert('Your changes will not  be saved.');
}
}

function attachEvents()
{
//alert("Called");
moduleID = window.event.srcElement.id;
document.getElementById(moduleID).attachEvent('onkeypress',edit);
document.getElementById(moduleID).attachEvent('oncut',edit);
document.getElementById(moduleID).attachEvent('onpaste',edit);
document.getElementById(moduleID).detachEvent('onfocus',attachEvents);
document.getElementById(moduleID).onfocus=null;

}
function edit(oModule)
{
//alert(window.onunload);
//alert('firing')
moduleID = oModule.srcElement.id;
document.getElementById(moduleID).detachEvent('onkeypress',edit);
document.getElementById(moduleID).detachEvent('oncut',edit);
document.getElementById(moduleID).detachEvent('onpaste',edit);

if (Modules==undefined){
Modules = new Array(1);
Modules[0] = moduleID;
document.title = '*'+ document.title;

if (window.onunload !='')window.attachEvent('onunload',notSaved);
}
else
{
var isThere = false
for (Module in Modules)
	{
	//alert(Modules[Module]==moduleID);
	if (Modules[Module] == moduleID)
		{
		var isThere = true;
		break;	
		}
	}
	if (!isThere)
	{
	 Modules[Modules.length] = moduleID;
	}
}
}
function RemoveHTMLTags( strText )
{
	alert ('hello')
	var regEx = '/<[^>]*>/';
	return strText.replace(regEx, "");
}
function GoTo(oElement)
{
	//alert('lets Go!')
	//alert(oElement.value);
	window.location.href = oElement.value;
}
//-->
