// scripts by johan@orbitsville.com
// with ideas from dan steinman

// variable used for showing and hiding layers
hidden = (bNS)? 'hide': 'hidden';
visible = (bNS)? 'show': 'visible';
var currLyr
var notWys = 'This is not avaiable in this edit mode.'
var wysEdit = true;


function samLyr(name,frame) {
  // properties
  try {
  if (frame)
  {
	  //this.obj = frame.document.getElementById(name);
	  if (document.frames[frame].document.getElementById(name) != null)
	    this.obj = document.frames[frame].document.getElementById(name);
	  else
	    return;
	}
	else
	  this.obj = document.getElementById(name);
	  
	this.style = this.obj.style;
	this.left = this.obj.offsetLeft;
	this.top = this.obj.offsetTop;		
	this.width = this.obj.offsetWidth;
	this.height = this.obj.offsetHeight;
	this.x = this.left;
	this.y = this.top;
	this.name = name

  this.obj2 = name + "samLyr"
	// BAD HACK, PROBLEM WITH FORM1-1
  if (name != 'FORM1-1' && name != 'FOLLOW-UP') {
  	eval(this.obj2 + "=this")
  }

  // methods
	this.moveBy = samMoveBy
	this.moveTo = samMoveTo
	this.show = samShow
	this.hide = samHide
	this.w = samChangeWidth
	this.h = samChangeHeight
	this.remove = samRemove
	this.bringback = samReturn
	this.lyrwrite = samWrite

	this.slideTo = samSlideTo
	this.slideStart = samSlideStart
	this.slide = samSlide
	this.sliding = false
	if (this.name == 'dInfoSlide') this.slideDone = samSlideDone	
	
	this.makeTable = samCreateTable
	this.makeAnch = samCreateAnch
	this.makeAnchLink = samCreateAnchLink
	this.makeBold = samMakeBold
  //	exHeader change
	this.makeSpecStyle = samMakeSpecStyle
  //	dictionary change
	this.makeDictWord = samMakeDictWord
	this.makeItalic = samMakeItalic
	this.doCmd = samCmd
	this.insertCallout = samInsertCallout;
	this.makeSup = samCreatesup
	this.makeSub = samCreatesub
	this.execCmd = samExecCmd
	this.makeLink = samCreateLink
	this.makeExtLink = samCreateExtLink
	this.setAttrib = samSetAttrib
	this.pasteHTML = samPasteHTML
	this.insertSpecialChar = samInsertSpecialChar
	this.insertImage = samInsertImage
	this.insertFile = samInsertFile
	this.insertStyle = samInsertStyle
	this.insertWC = samInsertWC
	//this.insertSpecialChar = samInsertSpecialChar
	
	this.makeUL = samCreateUL
 	this.makeOL = samCreateOL
	this.insertLinkedImage = samInsertLinkedImage;
	}
	catch(ex) {
		alert('Error initializing '+ name +'!!\n\n'+ex.description);
	}
}

function samIF(name,pth) {
// properties
	this.obj = document.frames['fWysiwyg'].document.frames['fWysSrc'].document
	this.obj.contentEditable = true;
	
// methods
	this.makeTable = samCreateTable
	this.makeAnch = samCreateAnch
	this.makeAnchLink = samCreateAnchLink
	this.makeBold = samMakeBold
//	exHeader change
	this.makeSpecStyle = samMakeSpecStyle
//	dictionary change
	this.makeDictWord = samMakeDictWord
	this.makeItalic = samMakeItalic
	this.doCmd = samCmd
	this.insertCallout = samInsertCallout;
	this.makeSup = samCreatesup
	this.makeSub = samCreatesub
	this.execCmd = samExecCmd
	this.makeLink = samCreateLink
	this.makeExtLink = samCreateExtLink
	this.setAttrib = samSetAttrib
	this.pasteHTML = samPasteHTML
	this.insertSpecialChar = samInsertSpecialChar
	this.insertImage = samInsertImage
	this.insertFile = samInsertFile	
 	//this.insertSpecialChar = samInsertSpecialChar
 	
}

function samInsertSpecialChar(sChar) {
	oSTR = this.obj.document.selection.createRange ();
	if (is_ie5) oSTR = this.obj.document.selection.createRange ();
	pElm = oSTR.parentElement()
	if (pElm.id != 'mainBody') {
		if (!is_ie5) this.obj.setActive()
		oSTR.pasteHTML (sChar);
		addUndo();
	}

}

function samInsertCallout(style) {
	oSTR = document.selection.createRange();
	strCallout = (oSTR.text != '')? oSTR.text : '[callout text]'
	strWrite = '';
	strWrite += '<table id="CALLOUT" title="'+style+'" align="right">'
	strWrite += '	<tr><td id="CALLOUT_CELL1">'+strCallout+'</td></tr>'
	strWrite += '	<tr><td id="CALLOUT_CELL2">[author]</td></tr>'
	strWrite += '	<tr><td id="CALLOUT_CELL3">[links]</td></tr>'
	strWrite += '</table>'
	oSTR.collapse(true)
	oSTR.pasteHTML(strWrite)
	document.selection.empty();
}

function samInsertImage(src,desc) {
	oSTR = this.obj.document.selection.createRange ();
	if (is_ie5) oSTR = this.obj.document.selection.createRange ();
	pElm = oSTR.parentElement()
	if (pElm.id != 'mainBody') {
		res = '<img type="image" src="'+src+'" alt=" '+desc+'">'
		if (!is_ie5) this.obj.setActive()
		//this.obj.focus();
		this.obj.document.selection.createRange().pasteHTML(res)
		//oSTR.pasteHTML (res);
		addUndo();
		checkRefImages();
	}
}

function samInsertStyle(siteStyle,style,tag)
{
  //this.obj.focus();
  var sel = this.obj.document.selection.createRange();
  this.obj.setActive()
  var insert = '<' + tag + ' siteStyle="' + siteStyle + '" style="' + style + '">' + sel.text + '</' + tag + '>';
  sel.pasteHTML(insert)
}

function samInsertWC(WC) {
	oSTR = this.obj.document.selection.createRange ();
	if (is_ie5) oSTR = this.obj.document.selection.createRange ();
	pElm = oSTR.parentElement()
	if (pElm.id != 'mainBody') {
		linktext = window.prompt('Text that will be linked to the webcast. Will appear as a link on the site, and with yellow background in the wysiwyg window.','')
		
		res = '<SPAN alt="'+WC+'" type="WEBCAST" style="background-color:#FCF2A2" title="\'Webcast id: '+WC+'\'">'+linktext+'</SPAN>'

		oSTR.pasteHTML (res);
		addUndo();
	}
}

function samInsertFile(file) {
	oSTR = this.obj.document.selection.createRange ();
	if (is_ie5) oSTR = this.obj.document.selection.createRange ();
	pElm = oSTR.parentElement()
	if (pElm.id != 'mainBody') {
		linktext = window.prompt('Text that will be linked to the file. Will appear as a link on the site, and with gray background in the wysiwyg window.','')
		
		res = '<span type="file" alt="'+file+'" style="background-color:#CCCCCC" title="\'File: '+file+'\'">'+linktext+'</span>'

		//parent.inlineEdit.activeObj.setActive()
		this.obj.setActive();
		this.obj.document.selection.createRange().pasteHTML(res)
		addUndo();
	}	
}

function samCreateTable() {
	var tblName,tblCols,tblRows
	oSTR = document.selection.createRange ();
	if (is_ie5) oSTR = this.obj.selection.createRange ();
	
	pElm = oSTR.parentElement()
	if (pElm.id != 'mainBody') {

		tblName = 'table1'
		tblCols = window.prompt('How many columns?','')
		tblRows = window.prompt('How many rows?','')	
		res = '<TABLE border="1">'
		if (parseInt (tblCols) == tblCols && parseInt (tblRows) == tblRows){
			
			for (i = 0; i< tblRows; i++) {
				res += '<tr>'
				for (j = 0;j< tblCols;j++) {
					res += '<td>&nbsp;</td>'
				}
				res += '</tr>'
			}
			res += '</TABLE>'
			oSTR.pasteHTML (res);	
			addUndo();
		}
		else {
		  showAlert('alert','Error','');
			alert ('Erroneous column and/or row value.')
		}
	}
}

function samCreateAnch(nme) {
	if (wysEdit) {
		var oSTR;
		oSTR = this.obj.document.selection.createRange ();		
		if (is_ie5) oSTR = this.obj.document.selection.createRange ();
		pElm = oSTR.parentElement()
			if (pElm.id != 'mainBody') {		
				res = '<IMG type="anchor" src="inlineEdit/imgs/wys-anch.gif" alt="'+nme+'">'
				oSTR.pasteHTML (res);
				addUndo();
			}
	}
	else {
		alert (notWys);	
	}
}

function samCreateAnchLink(nme) {
	if (wysEdit) {
	oSTR = this.obj.document.selection.createRange ();
		if (is_ie5) oSTR = this.obj.document.selection.createRange ();	
		if (oSTR) {
			if (oSTR.text.length > 0) {
				this.execCmd ('CreateLink',false,'#'+nme);
				addUndo();
			}
			else {
			  showAlert('selectText','Error','You must have text selected in order to perform this task.');
				//alert ('You must have text selected in order to perform this task.')
			}
		}
	}
	else {
		alert (notWys);	
	}
}

function samMakeBold() {
	if (wysEdit) {
		this.execCmd('bold')
		addUndo();
	}
	else {
		alert (notWys)
	}
}

function samCreateOL() {
	if (wysEdit) {
		this.execCmd('insertorderedlist')
		addUndo();
	}
	else {
		alert (notWys)
	}
}

function samCreateUL() {
	if (wysEdit) {
		this.execCmd('insertunorderedlist')
		addUndo();
	}
	else {
		alert (notWys)
	}
}

//	exHeader change
function samMakeSpecStyle() {
	if (wysEdit) {
		var oSTR;
		oSTR = document.selection.createRange ();		
		if (is_ie5) oSTR = this.obj.selection.createRange ();
		pElm = oSTR.parentElement()
			if ((pElm.id == 'exantaHeadline')||(oSTR.htmlText.indexOf('</SPAN>') != -1)) {
				alert('You may not have nested specific styles.')
				return false;			
			}
			if (pElm.id != 'mainBody') {		
				res = '<SPAN ID="exantaHeadline" class="exantaHeadline">'+ oSTR.htmlText +'</SPAN>'
				oSTR.pasteHTML (res);
				addUndo();
			}
	}
	else {
		alert (notWys);	
	}
}

//	dictionary change
function samMakeDictWord(arrIds,arrWords) {
	var objWysiwyg;
	if (wysEdit) {
		if (!is_ie5) {
			objWysiwyg = dWysEditLyr.obj;
		} else {
			objWysiwyg = document.frames['fWysiwyg'].document.frames['fWysSrc'].document.body;
		}
	// remove all existing dictword markup 
	oRegExp = new RegExp ('<SPAN(.*?)class=dictWord(.*?)>(.*?)<\/SPAN>', 'gi');
	objWysiwyg.innerHTML = objWysiwyg.innerHTML.replace(oRegExp, '$3');
	// add the new dictwords
	for(i=0;i<arrWords.length;i++){
		if (arrWords[i].length!=0){
			strRegExp = '([^A-Za-z0-9])' + arrWords[i] + '([^A-Za-z0-9])';
			oRegExp = new RegExp (strRegExp, 'gi');
			objWysiwyg.innerHTML = objWysiwyg.innerHTML.replace(oRegExp, ('$1<SPAN class="dictWord" id="' + arrIds[i] + '" type="dictWord">'+arrWords[i]+'</SPAN>$2'));

			strRegExp = '([^A-Za-z0-9])' + arrWords[i] + '$';
			oRegExp = new RegExp (strRegExp, 'gi');
			objWysiwyg.innerHTML = objWysiwyg.innerHTML.replace(oRegExp, ('$1<SPAN class="dictWord" id="' + arrIds[i] + '" type="dictWord">'+arrWords[i]+'</SPAN>'));

			strRegExp = '^' + arrWords[i] + '([^A-Za-z0-9])';
			oRegExp = new RegExp (strRegExp, 'gi');
			objWysiwyg.innerHTML = objWysiwyg.innerHTML.replace(oRegExp, ('<SPAN class="dictWord" id="' + arrIds[i] + '" type="dictWord">'+arrWords[i]+'</SPAN>$1'));
		}
	}
	}
	else {
		alert (notWys);	
	}
}

function samMakeItalic () {
	if (wysEdit) {
		this.execCmd('italic')
		addUndo();
	}
	else {
		alert (notWys)
	}
}

function samCmd(sStart,sEnd) {
	if (wysEdit) {
	oSTR = this.obj.document.selection.createRange ();
	if (is_ie5) oSTR = this.obj.document.selection.createRange ();
		if (oSTR.text.length > 0) {
			oSTR.pasteHTML (sStart + oSTR.htmlText + sEnd);
		}
		else {
			alert ('You must have text selected in order to perform this task.')
		}
	}
	else {
		alert (notWys)
	}
}

function samExecCmd (sCommand, bUI, vVal) {
	if (wysEdit) {
		if (!is_ie5) this.obj.document.execCommand (sCommand, bUI, vVal);
		else {
			oSTR = document.selection.createRange ();
			oSTR = this.obj.selection.createRange ();
			oSTR.execCommand (sCommand, bUI, vVal);

		}
	}
	else {
		alert (notWys)
	}
}

function samCreatesup () {
	if (wysEdit) {
		this.execCmd('superscript')
		addUndo();
	}
	else {
		alert (notWys)
	}
}

function samCreatesub () {
	if (wysEdit) {
		this.execCmd('subscript');
		addUndo();
	}
	else {
		alert (notWys)
	}
}

function samCreateLink (url) {
	if (wysEdit) {
		oSTR = document.selection.createRange ();
		if (is_ie5) oSTR = this.obj.selection.createRange ();
		if (oSTR.text.length > 0) {
			if (confirm ('Would you like to open the link in a new window?\nOk = yes, Cancel = no.')) this.makeExtLink (url);
			else oSTR.execCommand ('CreateLink',null,url);
			addUndo();
		}
		else {
			alert ('You must have text selected in order to perform this task.')
		}
	}
	else {
		alert (notWys)
	}	
}

function samCreateExtLink (url) {
	if (wysEdit) {
		var oSTR, vValExt;
		oSTR = document.selection.createRange ();
		if (is_ie5) oSTR = this.obj.selection.createRange ();		
		oSTR.execCommand ('CreateLink',null,url);
		if (confirm ('Is this external site affiliated with AstraZeneca\nOk = yes, Cancel = no.')) sAttrib = ' ID="EXTERNALAFFILIATED"';
		else sAttrib = ' ID="EXTERNALNONAFFILIATED"';
		this.setAttrib ('A', sAttrib)
		addUndo();
	}
	else {
		alert (notWys)
	}
 
}

function samSetAttrib (sTagName, sAttrib) {
	var oSTR;
	oSTR = this.obj.document.selection.createRange ();
	if (is_ie5) oSTR = this.obj.selection.createRange ();	
	sStr = oSTR.htmlText;
	oRegExp = new RegExp ('<'+ sTagName + '([^>]*)', 'ig')
	sStr = sStr.replace(oRegExp,'<'+sTagName+'$1 ' +sAttrib)
	oSTR.pasteHTML (sStr);
}

function samPasteHTML (sString) {
	var oSTR;
	oSTR = this.obj.document.selection.createRange ();
	pElm = oSTR.parentElement()

	if (pElm.id != 'mainBody') {
		if (is_ie5) oSTR = this.obj.document.selection.createRange();
		oSTR.pasteHTML (sString);
	}
	else {
		alert('Please choose where to insert.')
		return false;
	}
}

function samSlideTo(endx,endy,inc,speed,fn) {
	if (endx==null) endx = this.x
	if (endy==null) endy = this.y
	var distx = endx-this.x
	var disty = endy-this.y
	this.slideStart(endx,endy,distx,disty,inc,speed,fn)
}
function samSlideStart(endx,endy,distx,disty,inc,speed,fn) {
	if (this.slideActive) return
	if (!inc) inc = 10
	if (!speed) speed = 20
	var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/inc
	if (num==0) return
	var dx = distx/num
	var dy = disty/num
	if (!fn) fn = null
	this.slideActive = true
	this.slide(dx,dy,endx,endy,num,1,speed,fn)
}
function samSlide(dx,dy,endx,endy,num,i,speed,fn) {
	if (!this.slideActive) return
	if (i++ < num) {
		this.moveBy(dx,dy)
		if (this.slideActive) setTimeout(this.obj2+".slide("+dx+","+dy+","+endx+","+endy+","+num+","+i+","+speed+",\""+fn+"\")",speed)
	}
	else {
		this.slideActive = false
		this.moveTo(endx,endy)
		// AZPO SPEC
		if (this.name == 'dInfoSlide') this.slideDone()
	}
}


function samMoveBy(x,y) {
	this.x += parseInt(x)
	this.style.left = this.x
	this.left = this.x
	this.y += parseInt(y)
	this.top = this.y
	this.style.top = this.y
}
function samMoveTo(x,y) {
	this.x = parseInt(x)
	this.style.left = this.x
	this.left = this.x
	this.y = parseInt(y)
	this.style.top = this.y
	this.top = this.y
}

function samShow() {
	this.style.visibility = visible
}

function samHide() {
	this.style.visibility = hidden	
}

function samChangeWidth(w) {
	if (bNS) this.obj.clip.width = w
	else this.style.width = w
	this.width = w
}

function samChangeHeight(h) {
	if (bNS) this.obj.clip.height = h
	else this.style.height = h
	this.height = h
}

function samImg(name,pth) {
	if (pth != '') this.obj = pth.document.images[name];
	else this.obj = document.images[name];
}

function samRemove () {
	if (!bNS) {
		this.style.display = "none";
	}
	else {
		alert ('IE/NS6 only');
	}	
}

function samReturn () {
	if (!bNS) {
		this.style.display = "block";
	}
	else {
		alert ('IE/NS6 only');
	}	
}

function samWrite (text) {
	if (bNS) {
		this.obj.document.open()
		this.obj.document.write(text)
		this.obj.document.close()
	}
	else {
		this.obj.innerHTML = text
	}
	updateProperties(this)
}

function samMouseover(e) {
	if (!bNS && !is_nav6) e = event
	evLyr.lyrwrite('Latest event: '+e.type+' Layer: '+this.id)
	if (!bNS) this.style.background = '#FF0000'
}

function samClick(e) {
	if (!bNS && !is_nav6) e = event
	evLyr.lyrwrite('Latest event: '+e.type+' Layer: '+this.id)
	cLyr.lyrwrite('Current layer: '+this.id)
	if (!bNS) this.style.background = '#00FF00'
	currLyr = this.ref;
}

function updateProperties (o) {
	if (is_ie4) {		
		o.style = o.obj.style;
		o.left = o.obj.offsetLeft;
		o.top = o.obj.offsetTop;			
		o.width = o.obj.offsetWidth;
		o.height = o.obj.offsetHeight;
	}
	else if (bNS){
		o.style = o.obj
		o.left = o.obj.left;
		o.top = o.obj.top;			
		o.width = o.obj.clip.width;
		o.height = o.obj.clip.height;
	}
	else {
		o.style = o.obj.style;
		o.left = o.obj.offsetLeft;
		o.top = o.obj.offsetTop;		
		o.width = o.obj.offsetWidth;
		o.height = o.obj.offsetHeight;
	}
}

function samInsertLinkedImage(src) {
	thisObj = objCurrentRefImage
	thisObj.refimage = src;
	objCurrentRefImage = null;
	checkRefImages();
}
