// JScript File
var inpageedit = false;
var curmode = "browse";
var cmeditor = null;
var openedCompletion = false;

function initCodeMirror() {
    if(cmeditor == null) {
        cmeditor = CodeMirror.fromTextArea(document.getElementById('codearea'), {
        mode : 'text/html',
        tabMode : 'indent',
        lineNumbers :true,
        electricChars : true,
        onChange : onChange,
        onKeyEvent : function(i, e) {
					if(e.keyCode == 32 && (e.ctrlKey || e.metaKey) && !e.altKey) {
						e.stop();
						if(!openedCompletion) {
							openedCompletion = true;
							return startComplete();
						} else {
							openedCompletion = false;
							return ;
						}
					}
				}
        });
    }
}
          
function reindentAll() {
    var lineCount = cmeditor.lineCount();
    for(var line = 0; line < lineCount; line++) {
        cmeditor.indentLine(line);
    }
}
			
function onChange() {
  if (cmeditor != null) 
	cmeditor.save();
}
        
function updatePreview() {
    var ehtml = cmeditor.getValue();
        
    var editor = document.getElementById("contentframe");
    if (editor.contentDocument != null && editor.contentDocument.body != null)
    {
        editor.contentDocument.body.innerHTML = ehtml;
    }
    else if (editor.contentWindow.document.body != null)
    {
        editor.contentWindow.document.body.innerHTML = ehtml;
    }   
}

function NavigateTo(filename)
{
    var newaddress = apprelurl+"/"+filename+"?guid="+newGuid();
    curfilename = filename;
    document.getElementById("content").style.top = "0px";
    document.getElementById("contentframe").src= newaddress;
    currentScrollTop = 0;
    currentScrollStep = 1;    
} 

function iGoBack(slidepanelid, parentcontid)
{
    contentiframebackleft = 0;
    curparentcontid = parentcontid;
    curparentcontleft = -180;
    var slidepanel = document.getElementById(slidepanelid);
    slidepanel.style.left = contentiframebackleft+"px";
    AnimateIframeBackStep(slidepanelid);
}

var contentiframeleft = 0;
var curparentcontid = null;
var curparentcontleft = 0;
var contentiframebackleft = 0;

function AnimateIframeBackStep(slidepanelid)
{
    contentiframebackleft = contentiframebackleft + 40;
    var slidepanel = document.getElementById(slidepanelid);
    slidepanel.style.left = contentiframebackleft+"px";
    var backbar = document.getElementById(slidepanelid+"bbar");
    backbar.style.left = contentiframebackleft+"px";
    if(targetplatform == "iPad")
        curparentcontleft = curparentcontleft + 9.5;
    else
        curparentcontleft = curparentcontleft + 20;
    var parentcont = document.getElementById(curparentcontid);
    if (parentcont != null)
        parentcont.style.left = curparentcontleft+"px";
    var parentcontbbar = document.getElementById(curparentcontid+"bbar");
    if (parentcontbbar != null)
        parentcontbbar.style.left = curparentcontleft+"px";
    
    var backWidth = 360;
    if(targetplatform == "iPad")
        backWidth = 758;
    
    if (contentiframebackleft > 0 && contentiframebackleft < backWidth)
    {
        setTimeout("AnimateIframeBackStep('"+slidepanelid+"')",20);
    }
    else
    {
        contentiframebackleft = 0;
        
        document.getElementById(slidepanelid+"cont").removeChild(slidepanel);
        document.getElementById("iphone").removeChild(document.getElementById(slidepanelid+"cont"));
    }  
}

function AnimateIframe(hrefurl, parentcontid)
{
    //alert(hrefurl);
    var slidepanelcontainer = document.createElement("div");
    
	var slidepanelid = getRandomShortID();
	curparentcontid = parentcontid; 
	slidepanelcontainer.id = slidepanelid+"cont";
	if(targetplatform == "iPad")
	{
	    slidepanelcontainer.className = "slidepanelcontaineripad";
	}
	else
	{
	    slidepanelcontainer.className = "slidepanelcontainer";
	}
	document.getElementById("iphone").appendChild(slidepanelcontainer);
	
	var backbar = document.createElement("div");
    backbar.id = slidepanelid+"bbar";
    if(targetplatform == "iPad")
	    backbar.className = "backbaripad";
	else
	    backbar.className = "backbar";
	slidepanelcontainer.appendChild(backbar);
	
	backbar.innerHTML = "<img src=\"http://seattleclouds.com/images/back.png\" border=\"0\" onclick=\"iGoBack('"+slidepanelid+"','"+parentcontid+"')\" />";
	
	var slidepanel = document.createElement("div");
    slidepanel.id = slidepanelid;
    if(targetplatform == "iPad")
	    slidepanel.className = "slidepanelipad";
	else
	    slidepanel.className = "slidepanel";
	slidepanelcontainer.appendChild(slidepanel);
	
	if(targetplatform == "iPad")
	{
		slidepanel.innerHTML =
        "<iframe onload=\"ContentIframeResize('"+slidepanelid+"frame')\" id=\""+slidepanelid+"frame\" name=\""+slidepanelid+"frame\" frameborder=\"0\" scrolling=\"no\" height=\"994\" width=\"778\" src=\""+hrefurl+"\"></iframe>";          
        slidepanel.style.left = "788px";
        backbar.style.left = "788px";
        contentiframeleft = 758;
	}
	else
	{
	    slidepanel.innerHTML =
        "<iframe onload=\"ContentIframeResize('"+slidepanelid+"frame')\" id=\""+slidepanelid+"frame\" name=\""+slidepanelid+"frame\" frameborder=\"0\" scrolling=\"no\" height=\"432\" width=\"320\" src=\""+hrefurl+"\"></iframe>";          
        slidepanel.style.left = "360px";
        backbar.style.left = "360px";
        contentiframeleft = 360;
    }
    //StartAnimateIframe(slidepanelid);
    return false;
}

function StartAnimateIframe(slidepanelid)
{    
    if (contentiframeleft > 0)
    {
        setTimeout("AnimateIframeStep('"+slidepanelid+"')",20);
    }
}

function AnimateIframeStep(slidepanelid)
{
    var slidepanel = document.getElementById(slidepanelid);
    if (slidepanel == null) return;
    contentiframeleft = contentiframeleft - 40;
    slidepanel.style.left = contentiframeleft+"px";
    var backbar = document.getElementById(slidepanelid+"bbar");
    backbar.style.left = contentiframeleft+"px";
    
    curparentcontleft = curparentcontleft - 20;
    var parentcont = document.getElementById(curparentcontid);
    if (parentcont != null)
        parentcont.style.left = curparentcontleft+"px";
    var parentcontbbar = document.getElementById(curparentcontid+"bbar");
    if (parentcontbbar != null)
        parentcontbbar.style.left = curparentcontleft+"px";
        
    if (contentiframeleft > 35)
    {
        setTimeout("AnimateIframeStep('"+slidepanelid+"')",20);
    }  
}
  
var contentHeight=0;
var isWebClient = false;
function ContentIframeResize(frameelemid)
{
	var iframeElem = document.getElementById(frameelemid);
    var miniHeight = 412;
//    if (isWebClient == true)
//        miniHeight = 461;
	if(targetplatform == "iPad")
	{
	    if(isWebClient || isTabBarWheel)
	    {
	        miniHeight = 1024;
	    }
	    else
	    {
	        miniHeight = 974;
	    }
	}
	else
	{
	    if(isWebClient || isTabBarWheel)
	    {
	        miniHeight = 461;
	    }
	    else
	    {
	        miniHeight = 412;
	    }
	}
    if (iframeElem != null)
    {
        if (inpageedit)
        {
        
        }
        else
        {
            try
            {
                var contid = frameelemid.substring(0,frameelemid.length-5);
                StartAnimateIframe(contid);
                var a;
                if (iframeElem.contentDocument != null)
                    a = iframeElem.contentDocument.getElementsByTagName("a");
                else
                    a = iframeElem.contentWindow.document.getElementsByTagName("a");
                
                for(var i=0; a[i]; i++) 
                {
                    if (isWebClient == true)
                        continue;
                    if (a[i].target=="_blank")
                        continue;
                    if (a[i].href.substring(0,1)=="#")
                    {
                        continue;
                    }
                    if (a[i].href.substring(0,11)=="external://")
                    {
                        a[i].setAttribute("target", "_blank");
                        continue;
                    }
                    if (a[i].href.substring(0,6)=="map://")
                    {
                        a[i].setAttribute("target", "_blank");
                        continue;
                    }
                    if (a[i].href.substring(0,7) == "http://")
                    {
                        a[i].setAttribute("parentcontid",contid);
                        a[i].onclick = function()
                        {
                            return parent.AnimateIframe(this.href, this.getAttribute("parentcontid"));
                        }
                    }
                }
                
                var b;
                if (iframeElem.contentDocument != null)
                    b = iframeElem.contentDocument.getElementsByTagName("iframe");
                else
                    b = iframeElem.contentWindow.document.getElementsByTagName("iframe");

                for(var j=0; b[j]; j++) 
                {
                    var c;
                    if (b[j].contentDocument != null)
                        c = b[j].contentDocument.getElementsByTagName("a");
                    else
                        c = b[j].contentWindow.document.getElementsByTagName("a");
                        
                    for(var i=0; c[i]; i++) 
                    {
                        if (isWebClient == true)
                            continue;
                        if (c[i].target=="_blank")
                            continue;
                        if (c[i].href.substring(0,1)=="#")
                        {
                            continue;
                        }
                        if (c[i].href.substring(0,11)=="external://")
                        {
                            c[i].setAttribute("target", "_blank");
                            continue;
                        }
                        if (c[i].href.substring(0,6)=="map://")
                        {
                            c[i].setAttribute("target", "_blank");
                            continue;
                        }                          
                        if (c[i].href.substring(0,7) == "http://")
                        {
                            c[i].setAttribute("parentcontid",contid);
                            c[i].onclick = function()
                            {
                                return parent.parent.AnimateIframe(this.href, this.getAttribute("parentcontid"));
                            }
                        }
                    }
                }
            }
            catch(err)
            {
                iframeElem.style.height = "824px";
                contentHeight = miniHeight;
                iframeElem.style.width = "320px";
                if (invisiblelayer != null)
                {
                  invisiblelayer.style.height = "824px";
                  invisiblelayer.style.width = "320px";
                }
                iframeElem.style.overflow="auto";
                if(targetplatform == "iPad")
                {
                    iframeElem.style.height = "1024px";
                    contentHeight = miniHeight;
                    iframeElem.style.width = "768px";
                    if (invisiblelayer != null)
                    {
                      invisiblelayer.style.height = "1024px";
                      invisiblelayer.style.width = "768px";
                    }
                }
            }
        }
        try
        {
            var iDoc = iframeElem.contentWindow.document;
            var invisiblelayer = document.getElementById("invisiblelayer");
            if (iDoc != null)
            {
                var ifrH = iDoc.body.offsetHeight + 40;
                if (ifrH < miniHeight)
                {
                    ifrH = miniHeight;
                }
                    
                iframeElem.style.height = ifrH+"px";
                contentHeight = ifrH;
                
                var ifrW = iDoc.body.offsetWidth;
                if (ifrW < (targetplatform == "iPad" ? 768 : 320))
                {
                    ifrW = (targetplatform == "iPad" ? 768 : 320);
                }
                
                iframeElem.style.width = ifrW+"px";
                if (invisiblelayer != null)
                {
                  invisiblelayer.style.height = ifrH+"px";
                  invisiblelayer.style.width = ifrW+"px";
                }
                //alert(ifrH);
            }
            else
            {

            }
        }
        catch(err)
        {
                iframeElem.style.height = "824px";
                contentHeight = miniHeight;
                iframeElem.style.width = "320px";
                if (invisiblelayer != null)
                {
                  invisiblelayer.style.height = "824px";
                  invisiblelayer.style.width = "320px";
                }
                iframeElem.style.overflow="auto";
                if(targetplatform == "iPad")
                {
                    iframeElem.style.height = "1024px";
                    contentHeight = miniHeight;
                    iframeElem.style.width = "768px";
                    if (invisiblelayer != null)
                    {
                      invisiblelayer.style.height = "1024px";
                      invisiblelayer.style.width = "768px";
                    }
                }
        }
    }
    else
    {
        alert("iframeElem is null");
    }    
}

function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

var currentScrollTop = 0;
var currentScrollStep = 1;
var scrollingup = false;
var scrollingdown = false;

function MakeContentEditable()
{
  var editor = document.getElementById("contentframe");
  
  document.getElementById("codediv").style.display="none";
  if (document.getElementById("edittoolbox") != null)
  {
    document.getElementById("edittoolbox").style.visibility="visible"; 
    document.getElementById("edittoolboxheader").style.visibility="visible";
  }
  document.getElementById("savebutton").style.visibility="visible";
  SwitchToMode("edit");
  if (editor.contentDocument != null)
  {
      editor.contentDocument.designMode = "on";
  }
  else
  {   
      //Make content editable only once, i.e. when the page loads
      if (selectedrange == undefined)
      {   
        editor.contentWindow.document.designMode = "on";
      }
      editor.contentWindow.document.onselectionchange = SaveSelection;
  }
  document.getElementById("contentcontainer").style.overflowY = "scroll";
}

function ShowCode()
{    
  var editor = document.getElementById("contentframe");

  var ehtml = "";
    if (editor.contentDocument != null && editor.contentDocument.body != null)
    {        
        ehtml = editor.contentDocument.body.innerHTML;        
    }
    else if (editor.contentWindow.document.body != null)
    {
        ehtml = editor.contentWindow.document.body.innerHTML;
    }
  
  if (editor.contentDocument != null)
  {
      editor.contentDocument.designMode = "off";
  }
  else
  {
      //turning designMode off is not required anymore as turning it back on leads to page refresh and loosing changes
      //editor.contentWindow.document.designMode = "Off";
  }
  document.getElementById("codediv").style.display="block";
  if (document.getElementById("edittoolbox") != null)
  {
    document.getElementById("edittoolbox").style.visibility="hidden";
    document.getElementById("edittoolboxheader").style.visibility="hidden";
  }
  document.getElementById("savebutton").style.visibility="visible"; 
  SwitchToMode("code"); 
  
  document.getElementById("codearea").value = ehtml;
  document.getElementById("contentcontainer").scrollTop = 0;  
  document.getElementById("contentcontainer").style.overflowY = "hidden";
  if(cmeditor == null) {
    try {
        initCodeMirror();
    }catch(err) {
    }
  }
  if(cmeditor != null) {
    cmeditor.setValue(ehtml);
    reindentAll();
    cmeditor.refresh();
  }
}

function AutoSave()
{

}

var dcrwidth = 0;
var dcmeditor = null;
function DetachCode() {
    cDialog();
    $('#deAttachedTextarea').val(cmeditor.getValue());
    if (dcmeditor == null) {
        dcmeditor = CodeMirror.fromTextArea(document.getElementById('deAttachedTextarea'), {
        mode : 'text/html',
        tabMode : 'indent',
        lineNumbers :true,
        });
	}
	dcmeditor.setValue(cmeditor.getValue());
	$("#deAttachedEditDialog").dialog({
        modal : true,
        minWidth: 600,
		buttons : {
			"Save" : function() {
				cmeditor.setValue(dcmeditor.getValue());
			    dcmeditor.save();
				$(this).dialog("close");
			},
			"Cancel" : function() {
			    dcmeditor.save();
				$(this).dialog("close");
			}
		}
	});
	dcmeditor.refresh();
}

function cDialog() {
	if(!($('#deAttachedEditDialog').length > 0)) {
		jQuery('<div/>', {
			id : 'deAttachedEditDialog',
			title : 'HTML',
		}).appendTo('body');
		jQuery('<textarea/>', {
		    id : 'deAttachedTextarea', 
		}).appendTo('#deAttachedEditDialog');
	}
}

function SwitchToMode(mode)
{
    if (document.getElementById("edititem1")!=null) document.getElementById("edititem1").className = "modetoolboxitem";
    if (document.getElementById("codeitem1")!=null) document.getElementById("codeitem1").className = "modetoolboxitem";
    if (document.getElementById(mode+"item1")!=null) document.getElementById(mode+"item1").className = "selmodetoolboxitem";
    if (document.getElementById("edititem2")!=null) document.getElementById("edititem2").className = "modetoolboxitem";
    if (document.getElementById("codeitem2")!=null) document.getElementById("codeitem2").className = "modetoolboxitem";
    if (document.getElementById(mode+"item2")!=null) document.getElementById(mode+"item2").className = "selmodetoolboxitem";        
    if (curmode == "code")
    {
        if(cmeditor != null) {
            var ehtml = cmeditor.getValue();
  
            var dButton = document.getElementById("detachbutt");
            if(dButton != null) {
                dButton.style.visibility = "hidden";
            }
        } else {
            var ehtml = document.getElementById("codearea").value;
        }   
        var editor = document.getElementById("contentframe");
        if (editor.contentDocument != null && editor.contentDocument.body != null)
        {
            editor.contentDocument.body.innerHTML = ehtml;
        }
        else if (editor.contentWindow.document.body != null)
        {
            editor.contentWindow.document.body.innerHTML = ehtml;
        }   
    } else if (curmode == "edit") {
        var dButton = document.getElementById("detachbutt");
        if(dButton != null) {
            dButton.style.visibility = "visible";
        }
    }
    curmode = mode; 
}

function MakeBold() {
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("bold", false, null);
  else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("Bold");
      selectedrange.execCommand("Bold");
  }  
}

function MakeItalic() {
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("italic", false, null);
  else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("italic");
      selectedrange.execCommand("italic");
  }  

}

function MakeAlignLeft() {
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("justifyleft", false, null);
   else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("justifyleft");
      selectedrange.execCommand("justifyleft");
  }
}

function MakeAlignCenter() {
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("justifycenter", false, null);
   else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("justifycenter");
      selectedrange.execCommand("justifycenter");
  } 
}

function MakeAlignRight() {
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("justifyright", false, null);
   else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("justifyright");
      selectedrange.execCommand("justifyright");
  } 
}

function MakeAlignFull() {
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("justifyfull", false, null);
   else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("justifyfull");
      selectedrange.execCommand("justifyfull");
  } 
}

function MakeIndent() {
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("indent", false, null);
   else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("indent");
      selectedrange.execCommand("indent");
  }
}

function MakeOutdent() {
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("outdent", false, null);
   else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("outdent");
      selectedrange.execCommand("outdent");
  } 
}

function MakeCut() {
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("cut", false, null);
   else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("cut");
      selectedrange.execCommand("cut");
  }
}

function MakeCopy() {
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("copy", false, null);
   else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("copy");
      selectedrange.execCommand("copy");
  } 
}

function MakePaste() {
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.selection.createRange().execCommand("paste", false, null);
  else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("paste");
      selectedrange.execCommand("paste", false, null);
  } 
}

function MakeUndo() {
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("undo", false, null);
   else if (selectedrange != null)
  {
      editor.contentWindow.document.execCommand("undo");
      //selectedrange.execCommand("undo");
  } 
}

function MakeRedo() {
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("redo", false, null);
   else if (selectedrange != null)
  {
      editor.contentWindow.document.execCommand("redo");
      //selectedrange.execCommand("redo");
  }
}

function MakeInsertImage()
{
    selectedtab = "resources";
    OpenSelector();
    onChoose = InsertImage;
}

function InsertImage(filename)
{
  var imgurl = filename;
  var editor = document.getElementById("contentframe");
  var html = "<img src='"+filename+"' class='iimage' />";
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {  
          //strImageName = filename + '"' + " class=iimage";
          //editor.contentDocument.execCommand("InsertImage","",strImageName);
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE8
  {
        selectedrange.pasteHTML(html);
        //  strImageName = filename + '"' + " class=iimage";
        //  selectedrange.execCommand("InsertImage","",strImageName);
  }
  ContentIframeResize("contentframe");
}


function MakeInsertMovie()
{
    selectedtab = "resources";
    OpenSelector();
    onChoose = InsertMovie;
}

function InsertMovie(filename)
{
  var movieshortname = filename;
  
  //if it's an mp3 stream then get just the folder name
  if (movieshortname.indexOf("/", 0) != -1)
  {
      movieshortname = movieshortname.substring(0, movieshortname.lastIndexOf("/"));
      movieshortname = movieshortname.substring(movieshortname.lastIndexOf("/") + 1, movieshortname.Length);
  }
  else
  {
      filename = "movie://"+filename;
  }

  var editor = document.getElementById("contentframe");
  var html = "<a href='" + filename + "'>"+movieshortname+"</a>";
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE7
  {
//      editor.contentWindow.focus();
//      var range = editor.contentWindow.document.selection.createRange();
      selectedrange.pasteHTML(html);
  }
  ContentIframeResize("contentframe");
}

function MakeInsertMp3()
{
    selectedtab = "resources";
    OpenSelector();
    onChoose = InsertMp3;
}

function InsertMp3(filename)
{
  var mp3name = filename;
  var mp3shortname = mp3name;
  
  //if it's an mp3 stream then get just the folder name
  if (mp3shortname.indexOf("/", 0) != -1)
  {
      mp3shortname = mp3shortname.substring(0, mp3shortname.lastIndexOf("/"));
      mp3shortname = mp3shortname.substring(mp3shortname.lastIndexOf("/") + 1, mp3shortname.Length);
  }
  
  var editor = document.getElementById("contentframe");
  var html = "<a href='"+mp3name+"'><img border='0' src='listen.gif' border='0'/>"+mp3shortname+"</a>";
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE7
  {
//      editor.contentWindow.focus();
//      var range = editor.contentWindow.document.selection.createRange();
      selectedrange.pasteHTML(html);
  }
  ContentIframeResize("contentframe");
}


function MakeInsertWAV()
{
    selectedtab = "resources";
    OpenSelector();
    onChoose = InsertWAV;
}

function InsertWAV(filename)
{
  
  var editor = document.getElementById("contentframe");
  var html = "<a href='sound://"+filename+"'><img border='0' src='images/listen.gif' border='0'/>"+filename+"</a>";
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE7
  {
//      editor.contentWindow.focus();
//      var range = editor.contentWindow.document.selection.createRange();
      selectedrange.pasteHTML(html);
  }
  ContentIframeResize("contentframe");
}

function MakeInsertList()
{
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("insertunorderedlist");
  else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("InsertUnorderedList");
      selectedrange.execCommand("InsertUnorderedList");
  }
}

function MakeInsertNumbers()
{
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("insertorderedlist");
  else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("InsertOrderedList");
      selectedrange.execCommand("InsertOrderedList");
  }
}

function MakeInsertParagraph()
{
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
  {
      editor.contentDocument.execCommand("insertParagraph", true, null);
  }
  else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("InsertParagraph", true, "");
      selectedrange.execCommand("insertParagraph", true, null);
  }
  else
  {
      selectedrange.execCommand("insertParagraph", true, null);
  }
  ContentIframeResize("contentframe");
}

function MakeInsertMp3Link()
{
    ShowiForm();
    iFormOnInsert = InsertMp3Link;
    document.getElementById("mp3linkdata").style.display="block";
}

function InsertMp3Link()
{
  var mp3linktext = document.getElementById("mp3linktext").value;
  var mp3linkaddress = document.getElementById("mp3linkselect").value;
  
  var editor = document.getElementById("contentframe");
  var html = "<a href='"+mp3linkaddress+"'>"+mp3linktext+"</a>";
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE7
  {
//      editor.contentWindow.focus();
//      var range = editor.contentWindow.document.selection.createRange();
      selectedrange.pasteHTML(html);
  }
  ContentIframeResize("contentframe");
}

function MakeInsertPurchaseLink()
{
    ShowiForm();
    iFormOnInsert = InsertPurchaseLink;
    document.getElementById("purchasedata").style.display="block";
}

function InsertPurchaseLink()
{
  var purchasetext = document.getElementById("purchasetext").value;
  var purchaseid = document.getElementById("purchaseid").value;  
  var linkpage3 = document.getElementById("linkpage3").value; 
  var editor = document.getElementById("contentframe");
  var html = "<a href='buy://"+purchaseid+":"+linkpage3+"'>"+purchasetext+"</a>";
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE7
  {
//      editor.contentWindow.focus();
//      var range = editor.contentWindow.document.selection.createRange();
      selectedrange.pasteHTML(html);
  }

}

function MakeInsertMapLink()
{
    ShowiForm();
    iFormOnInsert = InsertMapLink;
    document.getElementById("maplinkdata").style.display="block";
}

function InsertMapLink()
{
  var maplinktext = document.getElementById("maplinktext").value;
  var maplinkaddress = document.getElementById("maplinkaddress").value;
  
  var editor = document.getElementById("contentframe");
  var html = "<a href='map://"+maplinkaddress+"'>"+maplinktext+"</a>";
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE7
  {
//      editor.contentWindow.focus();
//      var range = editor.contentWindow.document.selection.createRange();
      selectedrange.pasteHTML(html);
  }

}

function MakeInsertPageLink()
{
    ShowiForm();
    iFormOnInsert = InsertPageLink;
    document.getElementById("linkdata").style.display="block";
}

function InsertPageLink()
{
  var newlinktext = document.getElementById("newlinktext").value;
  var newlinkurl = document.getElementById("linkpage").value; 
  
  var editor = document.getElementById("contentframe");
  var html = "<a href='"+newlinkurl+"'>"+newlinktext+"</a>";
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE7
  {
//      editor.contentWindow.focus();
//      var range = editor.contentWindow.document.selection.createRange();
      selectedrange.pasteHTML(html);
  }
}

function MakeInsertExternalLink()
{
    ShowiForm();
    iFormOnInsert = InsertExternalLink;
    document.getElementById("extlinkdata").style.display="block";
}

function InsertExternalLink()
{
  var newlinktext = document.getElementById("extnewlinktext").value;
  var newlinkurl = document.getElementById("extnewlinkurl").value;
  
  var editor = document.getElementById("contentframe");
  var html = "<a href='"+newlinkurl+"'>"+newlinktext+"</a>";
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE7
  {
//      editor.contentWindow.focus();
//      var range = editor.contentWindow.document.selection.createRange();
      selectedrange.pasteHTML(html);
  }
}

function MakeUnLink()
{
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("unlink", false, null);
  else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("UnLink", "", null);
      selectedrange.execCommand("UnLink", "", null);
  }
}

function MakeInsertLink()
{
    ShowiForm();
    iFormOnInsert = InsertLink;
    document.getElementById("linkselectiondata").style.display="block";
}

function InsertLink()
{
  var newlinkurl = document.getElementById("linkpage2").value;
  
  if (document.getElementById("linktype1").checked == true)
  {
      newlinkurl = document.getElementById("linkselectionurl").value;;
  }
  else if (document.getElementById("linktype3").checked == true)
  {
      newlinkurl = document.getElementById("linkselectionexternal").value;
  }
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("createlink", false, newlinkurl);
  else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("CreateLink", "", newlinkurl);
      selectedrange.execCommand("CreateLink", "", newlinkurl);
  }
}
function MakeInsertVideo()
{
    ShowiForm();
    iFormOnInsert = InsertVideo;
    document.getElementById("videodata").style.display="block";
}

function InsertVideo()
{
  var newvideoembed = document.getElementById("newvideoembed").value;
  if (newvideoembed.indexOf("opaque") < 0)
  {
     newvideoembed = newvideoembed.replace("<embed","<embed wmode=\"opaque\"");
  }
  var editor = document.getElementById("contentframe");
  var html = newvideoembed;
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE7
  {
//      editor.contentWindow.focus();
//      var range = editor.contentWindow.document.selection.createRange();
      selectedrange.pasteHTML(html);
  }
  ContentIframeResize("contentframe");
}

function MakeInsertTable()
{
    ShowiForm();
    iFormOnInsert = InsertTable;
    document.getElementById("tabledata").style.display="block";

}

function InsertTable()
{

  var newtablecols = document.getElementById("newtablecols").value;
  var newtablerows = document.getElementById("newtablerows").value;
  var newtableborder = document.getElementById("newtableborder").value;
  var tcols = parseInt(newtablecols);
  var trows = parseInt(newtablerows);
  var tborder = parseInt(newtableborder);
  
  
  var html = "<table cellspacing='0' border='"+newtableborder+"' style='width:312px;margin-left:auto;margin-right:auto'>";
  
  for(var i=0;i<trows;i++)
  {
      html += "<tr>";
      for(var j=0;j<tcols;j++)
      {
        html+= "<td>&nbsp;</td>";
      }
      html += "</tr>";
  }
  html += "</table>";
  
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE7
  {
      selectedrange.pasteHTML(html);
  }
  ContentIframeResize("contentframe");
}

function colorsAll(ncolor)
{
    document.getElementById("newcolor").value = ncolor;
}

var textcolorpicker;
var colorpickersource;

function OpenTextColorPicker(source)
{
    colorpickersource = source;
    
    var container = document.getElementById("colorpicker");
    
    container.style.left = ((screen.width - 440) / 2).toString() + "px";
    container.style.top = ((screen.height - 540) / 2).toString() + "px";
    
    if (textcolorpicker == null)
    {
        textcolorpicker = new Refresh.Web.ColorPicker('cp1', {startHex: '18BA00', startMode: 's'});
    }  
    
    container.style.visibility = "visible";
}

function MakeForeBackColor()
{
    if (colorpickersource == "fore")
    {
        InsertForeColor()
    }
    else if (colorpickersource == "back")
    {
        InsertBackColor()
    }
    
    CloseColorPicker();
}

function MakeForeColor()
{
    ShowiForm();
    iFormOnInsert = InsertForeColor;
    document.getElementById("colordata").style.display="block";
}

function InsertForeColor()
{
  var newcolor = "#" + document.getElementById("newcolor").value;
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("ForeColor", false, newcolor);
  else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("ForeColor", false, newcolor);  
      selectedrange.execCommand("ForeColor", false, newcolor);
  }
}

function MakeBackColor()
{
    ShowiForm();
    iFormOnInsert = InsertBackColor;
    document.getElementById("colordata").style.display="block";
}

function InsertBackColor()
{
  var newcolor = "#" + document.getElementById("newcolor").value;
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("backcolor", false, newcolor);
  else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("backcolor", false, newcolor); 
      selectedrange.execCommand("backcolor", false, newcolor);
  }
}

function MakeRemoveFormat()
{
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("removeformat");
  else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("removeformat", false, newcolor); 
      selectedrange.execCommand("removeformat");
  }
}

function MakeFontName()
{
    ShowiForm();
    iFormOnInsert = InsertFontName;
    document.getElementById("fontname").style.display="block";
}

function InsertFontName()
{
  var newfontname = document.getElementById("newfontname").value;
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("fontname", false, newfontname);
  else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("fontname", false, newfontname); 
      selectedrange.execCommand("fontname", false, newfontname);
  }
}

function MakeFontSize()
{
    ShowiForm();
    iFormOnInsert = InsertFontSize;
    document.getElementById("fontsize").style.display="block";
}

function InsertFontSize()
{
  var newfontsize = document.getElementById("newfontsize").value;
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("fontsize", false, newfontsize);
  else if (selectedrange != null)
  {
      //editor.contentWindow.document.execCommand("fontsize", false, newfontname); 
      selectedrange.execCommand("fontsize", false, newfontsize);
  }
}


function MakeFormatBlock()
{
    ShowiForm();
    iFormOnInsert = InsertFormatBlock;
    document.getElementById("formatblock").style.display="block";
}

function InsertFormatBlock()
{
  var newformatblock = document.getElementById("newformatblock").value;
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
      editor.contentDocument.execCommand("formatblock", false, newformatblock);
  else if (selectedrange != null)
  {
      //alert(editor.contentWindow.document.execCommand("formatblock", false, newformatblock)); 
      selectedrange.execCommand("formatblock", false, newformatblock);
  }
}

function MakeInsertField()
{
    ShowiForm();
    iFormOnInsert = InsertField;
    document.getElementById("insertfieldblock").style.display="block";
}

function InsertField()
{
  var insertfieldlabel = document.getElementById("insertfieldlabel").value;
  var insertfieldtype = document.getElementById("insertfieldtype").value;
  var insertfieldrequired = document.getElementById("insertfieldrequired").value;
  var html = "";
  if(insertfieldtype == "rate")
  {
    html = "[%"+insertfieldtype+"%]";
  }
  else
    html = "[%=field;"+insertfieldlabel+";"+insertfieldtype+";"+insertfieldrequired+"%]";

  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {  
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE8
  {
        selectedrange.pasteHTML(html);
  }
}

function MakeInsertIFrame()
{
    ShowiForm();
    iFormOnInsert = InsertIFrame;
    document.getElementById("iframedata").style.display="block";
}

function InsertIFrame()
{

  var isource = document.getElementById("iframesrc").value;
  var iwidth = document.getElementById("iframewidth").value;
  var iheight = document.getElementById("iframeheight").value;
  
  var html = "<iframe src='" + isource + "' width='" + iwidth + "' height='" + iheight + "' />";
  
  var editor = document.getElementById("contentframe");
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE7
  {
      selectedrange.pasteHTML(html);
  }
  ContentIframeResize("contentframe");
}

function MakeInsertMailtoLink()
{
    ShowiForm();
    iFormOnInsert = InsertMailtoLink;
    document.getElementById("mailtodata").style.display="block";
}

function InsertMailtoLink()
{
  var newlinktext = document.getElementById("mailtotext").value;
  var newlinkurl = document.getElementById("mailtoaddress").value;
  
  var editor = document.getElementById("contentframe");
  var html = "<a href='mailto:"+newlinkurl+"'>"+newlinktext+"</a>";
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE7
  {
      selectedrange.pasteHTML(html);
  }
}

function MakeInsertPhoneNumberLink()
{
    ShowiForm();
    iFormOnInsert = InsertPhoneNumberLink;
    document.getElementById("phonenumberdata").style.display="block";
}

function InsertPhoneNumberLink()
{
  var linktext = document.getElementById("phonenumbertext").value;
  var number = document.getElementById("phonenumber").value;
  
  var editor = document.getElementById("contentframe");
  var html = "<a href='tel:"+number+"'>"+linktext+"</a>";
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(html);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,html);
      }
  }
  else if (selectedrange != null)//IE7
  {
      selectedrange.pasteHTML(html);
  }
}

function MakeInsertPayPalButton()
{
    ShowiForm();
    iFormOnInsert = InsertPayPalButton;
    document.getElementById("paypalbutton").style.display="block";
    //document.getElementById("iform").style.posTop = 150;
    //document.getElementById("iform").style.height = '340px';
}

function InsertPayPalButton()
{
  var email = document.getElementById("paylrecipient").value;
  var currency = document.getElementById("paylcurrency").value;
  var description = "";//document.getElementById("payldescription").value;
  var merchant = document.getElementById("paylmerchant").value;

  var itemid;// = document.getElementById("paylitemid").value;
  var itemname = document.getElementById("paylitemname").value;
  var itemprice = document.getElementById("paylitemprice").value;
  var itemcount = 0;//document.getElementById("paylitemcount").value;
  
  var shipping = 0;//document.getElementById("paylshipping").value;
  var tax = 0;//document.getElementById("payltax").value;
  
  var linkpage4 = document.getElementById("linkpage4").value;
  itemid = linkpage4;
  var editor = document.getElementById("contentframe");
  var button = "<div id='paypallink'>" +
               "<a id='link' href='pay://"+pageid+"?" +
              "paylprop0="+ email + 
             "&paylprop1="+ currency +
             "&paylprop2="+ escape(description) +
             "&paylprop3="+ escape(merchant) +
             "&paylprop4="+ itemid +
             "&paylprop5="+ escape(itemname) +
             "&paylprop6="+ itemprice +
             "&paylprop7="+ itemcount +
             "&paylprop8="+ shipping +
             "&paylprop9="+ tax +
             "'><input type='button' id='paylbutton' value='pay with PayPal'/></a>" +
             "</div>";
             
    var idocP;
    if (editor.contentDocument != null)
        idocP = editor.contentDocument;
    else
        idocP = editor.contentWindow.document;
  var paypallink = idocP.getElementById("paypallink");
  if(paypallink)
  {
      paypallink.parentNode.removeChild(paypallink);
  }
  
  if (editor.contentDocument != null)
  {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(button);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,button);
      }
  }
  else if (selectedrange != null)//IE7
  {
      selectedrange.pasteHTML(button);
  }
}

function MakeInsertRemoveShopLink()
{
    ShowiForm();
    iFormOnInsert = InsertRemoveShopLink;
    document.getElementById("shopcartremove").style.display="block";
}

function InsertRemoveShopLink()
{
    var itemid = document.getElementById("shopcartremoveitemid").value;
    var itemname = document.getElementById("shopcartremoveitemname").value;
    var link = "";
    if(itemid != null)
        link += "<a href='removefromcart://" + itemid + "'>Remove ";
    if(itemname != null)    
        link += itemname;
    link += " from shoppihg cart.</a>";
        
    var editor = document.getElementById("contentframe");
    var idocP;
    if (editor.contentDocument != null)
        idocP = editor.contentDocument;
    else
        idocP = editor.contentWindow.document;

    if (editor.contentDocument != null)
    {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(link);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,link);
      }
    }
    else if (selectedrange != null)//IE7
    {
      selectedrange.pasteHTML(link);
    }
}

function MakeInsertAddShopLink()
{
    ShowiForm();
    iFormOnInsert = InsertAddShopLink;
    document.getElementById("shopcartadd").style.display="block";
}

function InsertAddShopLink()
{
    var itemid = document.getElementById("shopcartitemid").value;
    var itemname = document.getElementById("shopcartitemname").value;
    var itemprice = document.getElementById("shopcartitemprice").value;
    var itemurlimage = document.getElementById("shopcarturlimg").value;
    var itemurlinfo = document.getElementById("shopcarturlinfo").value;
    var link = "<a href='addtocart://";
    if(itemid != null) link += itemid;
    link += "?";
    if(itemname != null) link += "itemname=" + escape(itemname);
    if(itemprice != null) link += "&itemprice=" + itemprice;
    if(itemurlimage != null) link += "&urlimg=" + escape(itemurlimage);
    if(itemurlinfo != null) link += "&urlinfo=" + escape(itemurlinfo);
    link += "'>Add ";
    if(itemid != null) link += itemname;
    link += " to shopping cart.</a>";
    
    var editor = document.getElementById("contentframe");
    var idocP;
    if (editor.contentDocument != null)
        idocP = editor.contentDocument;
    else
        idocP = editor.contentWindow.document;

    if (editor.contentDocument != null)
    {
      if (BrowserDetect.browser == "Explorer")      
      {
          editor.contentWindow.focus();
          editor.contentDocument.selection.createRange().pasteHTML(link);
      }
      else
      {
          editor.contentDocument.execCommand("inserthtml",false,link);
      }
    }
    else if (selectedrange != null)//IE7
    {
      selectedrange.pasteHTML(link);
    }
}

function MakeInsertSliderElement()
{
    ShowiForm();
    iFormOnInsert = InsertSliderElement;
    document.getElementById("sliderelement").style.display="block";
}

function InsertSliderElement()
{
// get values from this div and insert into editor document
}
function MakeInsertSearchBar()
{
    ShowiForm();
    iFormOnInsert = InsertSearchBar;
    document.getElementById("searchbaradd").style.display="block";
}

function InsertSearchBar()
{

}

function MakeInsertSelectMenu()
{
    ShowiForm();
    iFormOnInsert = InsertSelectMenu;
    document.getElementById("selectmenuadd").style.display="block";
}

function InsertSelectMenu()
{

}

var transitiontype = "";

function UpdateTransitionType()
{
    var radiobuttons = document.getElementsByName('transitiontype');
    
    for (var i = 0; i < radiobuttons.length; i++)
    {
        if (radiobuttons[i].checked)
        {
            transitiontype = radiobuttons[i].value;
            break;
        }
    }
}

var topad = "no"; // for backward compatibility
var topadtype = "none";
// @author Timur Calmatui
function updateTopAdType()
{
    var radiobuttons = document.getElementsByName('topadtype');
    
    for (var i = 0; i < radiobuttons.length; i++)
    {
        if (radiobuttons[i].checked)
        {
            topadtype = radiobuttons[i].value;
            if (topadtype == "admob")
                topad = "yes";
            else
                topad = "no";
                
            break;
        }
    }
}

var bottomad = "no"; // for backward compatibility
var bottomadtype = "none";
// @author Timur Calmatui
function updateBottomAdType()
{
    var radiobuttons = document.getElementsByName('bottomadtype');
    
    for (var i = 0; i < radiobuttons.length; i++)
    {
        if (radiobuttons[i].checked)
        {
            bottomadtype = radiobuttons[i].value;
            if (bottomadtype == "admob")
                bottomad = "yes";
            else
                bottomad = "no";
                
            break;
        }
    }
}

function SetHasReviews()
{
    var hasreviewscheckbox = document.getElementById("hasreviewscheckbox");
    if (hasreviewscheckbox != null)
    {
        if (hasreviewscheckbox.checked)
            hasreviews="yes";
        else
            hasreviews="no";
    }
}

function SaveHTML()
{
	if (cmeditor != null)
		cmeditor.save();

    if (typeof beforeHTMLSave == 'function')
	    beforeHTMLSave();
	    
    var editor = document.getElementById("contentframe");
    var slidepageid = "none";
    var slidepageselect = document.getElementById("slidepageselect");
    if (slidepageselect != null)
        slidepageid = slidepageselect.value;
    var slidetransition = "";
    if (document.getElementById("slidetransition") != null)
        slidetransition = document.getElementById("slidetransition").value;
        
    var idoc;
    if (editor.contentDocument != null)
        idoc = editor.contentDocument;
    else
        idoc = editor.contentWindow.document;
    var fileepubbook = idoc.getElementById("fileepubbook");
    var bookepub;
     if (fileepubbook!=null)
     {
       var fileepubbooktitle = fileepubbook.title;
       var laslash = fileepubbooktitle.lastIndexOf("/");
       if (laslash >= 0)
           fileepubbooktitle = fileepubbooktitle.substring(laslash+1);
       bookepub = fileepubbooktitle;
     }
    
    updateTopAdType(); // updates topad and topadtype
    updateBottomAdType(); // updates bottomad and bottomadtype
    
    var hassend = "no";
    if (document.getElementById("hassendcheckbox") != null)
        if (document.getElementById("hassendcheckbox").checked)
           hassend = "yes";
    var ehtml = "";
    if (curmode == "code")
    {
        ehtml = document.getElementById("codearea").value;
    }
    else
    {
        if (editor.contentDocument != null)
            ehtml = editor.contentDocument.body.innerHTML;
        else
            ehtml = editor.contentWindow.document.body.innerHTML;
    }
    
    document.getElementById("savemessage").innerHTML = "Saving... please wait...";
	makePOSTRequestSync("htmlsave.ashx?username=" + username + "&appid=" + appid + "&filename=" + curfilename  
	                        + "&transitiontype=" + transitiontype + "&hasreviews=" + hasreviews + "&slidepageid=" + slidepageid
	                        + "&slidetransition=" + slidetransition 
	                        + "&topad=" + topad + "&bottomad=" + bottomad 
	                        + "&topadtype=" + topadtype + "&bottomadtype=" + bottomadtype
	                        + "&hassend=" + hassend + "&fileepub=" + bookepub
	                        + "&isdynamic=" + isDynamic + "&jsonurl=" + escape(jsonURL),
	                        ehtml, SaveHTMLDone);
	ContentIframeResize("contentframe");
	SaveInitialContent();

}

function SaveHTMLDone(response)
{
   //alert("Debug: a");
   if (response == "OK")
   {
       document.getElementById("savemessage").innerHTML = "Successfully saved.";
       transitiontype = "";
       setTimeout("ClearMessage()",2000);   
   }
   else
   {
       document.getElementById("savemessage").innerHTML = "Error saving..."+response;
       setTimeout("ClearMessage()",20000);  
   }   
}

function ClearMessage()
{
   document.getElementById("savemessage").innerHTML = "";
}

function SelectTab1()
{
    document.getElementById("tab1").className = "seltabbaritem";
    document.getElementById("tab2").className = "tabbaritem";
    document.getElementById("tab3").className = "tabbaritem";
    document.getElementById("tab4").className = "tabbaritem";
    selectfileitem("tab1.html");
}
function SelectTab2()
{
    document.getElementById("tab1").className = "tabbaritem";
    document.getElementById("tab2").className = "seltabbaritem";
    document.getElementById("tab3").className = "tabbaritem";
    document.getElementById("tab4").className = "tabbaritem";
    selectfileitem("tab2.html");    
}
function SelectTab3()
{
    document.getElementById("tab1").className = "tabbaritem";
    document.getElementById("tab2").className = "tabbaritem";
    document.getElementById("tab3").className = "seltabbaritem";
    document.getElementById("tab4").className = "tabbaritem";
    selectfileitem("tab3.html");    
}
function SelectTab4()
{
    document.getElementById("tab1").className = "tabbaritem";
    document.getElementById("tab2").className = "tabbaritem";
    document.getElementById("tab3").className = "tabbaritem";
    document.getElementById("tab4").className = "seltabbaritem";
    selectfileitem("tab4.html");    
}
function newGuid()
{
    var result, i, j;
    result = '';
    for(j=0; j<32; j++)
    {
        if( j == 8 || j == 12|| j == 16|| j == 20)
            result = result + '-';
        i = Math.floor(Math.random()*16).toString(16).toUpperCase();
        result = result + i;
    }
    return result
}

var selectedfilename = "tab1.html";

function selectfileitem(filename)
{
    var selectedfileitem = document.getElementById("fileitem"+selectedfilename);
    if (selectedfileitem != null)
    {
        if (selectedfilename == "tab1.html" || selectedfilename == "tab2.html" || selectedfilename == "tab3.html" || selectedfilename == "tab4.html")
        {
            selectedfileitem.className="tabfileitem";
        }
        else
        {
            selectedfileitem.className="fileitem";
        }
    }
    
    selectedfilename = filename;
    
    var fitem = document.getElementById("fileitem"+filename);
    if (fitem != null)
    {
        if (selectedfilename == "tab1.html" || selectedfilename == "tab2.html" || selectedfilename == "tab3.html" || selectedfilename == "tab4.html")
        {
            fitem.className="selectedtabfileitem";
        }
        else
        {
            fitem.className="selectedfileitem";
        }
    }
    
    //
    if (filename.substring(0,4) == "tab1" || filename.substring(0,4) == "tab2" || filename.substring(0,4) == "tab3" || filename.substring(0,4) == "tab4")
    {
        var ttelem1 = document.getElementById("tab1");
        if (ttelem1 != null) ttelem1.className = "tabbaritem";
        var ttelem2 = document.getElementById("tab2");
        if (ttelem2 != null) ttelem2.className = "tabbaritem";
        var ttelem3 = document.getElementById("tab3");
        if (ttelem3 != null) ttelem3.className = "tabbaritem";
        var ttelem4 = document.getElementById("tab4");
        if (ttelem4 != null) ttelem4.className = "tabbaritem";
        var ttelem5 = document.getElementById(filename.substring(0,4));
        if (ttelem5 != null) ttelem5.className = "seltabbaritem";
    }
    
    NavigateTo(filename);    
}

function ShowiForm()
{
    document.getElementById("iformcurtain").style.visibility = "visible";
    document.getElementById("iform").style.visibility = "visible"; 
    document.getElementById("linkselectiondata").style.display="none";
    document.getElementById("mp3linkdata").style.display="none";
    document.getElementById("maplinkdata").style.display="none";
    document.getElementById("extlinkdata").style.display="none";
    document.getElementById("linkdata").style.display="none";
    document.getElementById("videodata").style.display="none";
    document.getElementById("tabledata").style.display="none";
    document.getElementById("colordata").style.display="none";
    document.getElementById("fontname").style.display="none";          
    document.getElementById("fontsize").style.display="none";      
    document.getElementById("formatblock").style.display="none";
    document.getElementById("insertfieldblock").style.display="none";   
    document.getElementById("iframedata").style.display="none"; 
    document.getElementById("mailtodata").style.display="none";
    document.getElementById("phonenumberdata").style.display="none"; 
    document.getElementById("purchasedata").style.display="none"; 
    document.getElementById("paypalbutton").style.display="none"; 
    document.getElementById("shopcartremove").style.display="none";
    document.getElementById("shopcartadd").style.display="none";
    document.getElementById("sliderelement").style.display="none";
}

function CloseiForm()
{
    document.getElementById("iformcurtain").style.visibility = "hidden";
    document.getElementById("iform").style.visibility = "hidden"; 
    document.getElementById("linkselectiondata").style.display="none";
    document.getElementById("mp3linkdata").style.display="none"; 
    document.getElementById("maplinkdata").style.display="none"; 
    document.getElementById("extlinkdata").style.display="none";
    document.getElementById("linkdata").style.display="none";
    document.getElementById("videodata").style.display="none";
    document.getElementById("tabledata").style.display="none";
    document.getElementById("colordata").style.display="none";
    document.getElementById("fontname").style.display="none";
    document.getElementById("fontsize").style.display="none"; 
    document.getElementById("formatblock").style.display="none";
    document.getElementById("insertfieldblock").style.display="none";   
    document.getElementById("iframedata").style.display="none"; 
    document.getElementById("mailtodata").style.display="none";
    document.getElementById("phonenumberdata").style.display="none";  
    document.getElementById("purchasedata").style.display="none"; 
    document.getElementById("paypalbutton").style.display="none";
    document.getElementById("shopcartremove").style.display="none";
    document.getElementById("shopcartadd").style.display="none";    
    document.getElementById("sliderelement").style.display="none";
    iFormOnInsert = null;   
}

var iFormOnInsert = null;

function InsertiForm()
{
    document.getElementById("iformcurtain").style.visibility = "hidden";
    document.getElementById("iform").style.visibility = "hidden";  
    if (iFormOnInsert != null)
        iFormOnInsert();
    iFormOnInsert = null;  
}

function ViewHelpVideo()
{

}

//Save Modifications remainder
var initialContent;

function SaveInitialContent()
{
    initialContent = GetEditorContent();
}

function WasContentModified()
{
    document.getElementById("savebutton").style.visibility="visible";
    var iframeElem = document.getElementById("contentframe");
    if (iframeElem != null)
    {  
        var formShop = iframeElem.contentWindow.document.getElementById("shop");//For shopping cart page edit.
        if (formShop != null)
            formShop.innerText = "";
    }
    if (initialContent == GetEditorContent() && transitiontype == "")
    {
        return false;
    }
    else
    {
        return true;
    }
}

function GetEditorContent()
{
    var editor = document.getElementById("contentframe");
    var content = "";
    if (curmode == "code")
    {
        content = document.getElementById("codearea").value;
    }
    else
    {
        if (editor.contentDocument != null)
        {
            if (editor.contentDocument.body != null)
            {
                content = editor.contentDocument.body.innerHTML;
            }
            else
            {
                editor.contentWindow.onload = function(){initialContent = document.getElementById("contentframe").contentDocument.body.innerHTML;}
                return initialContent;
            }
        }
        else
        {
            if (editor.contentWindow.document.body != null)
            {
                content = editor.contentWindow.document.body.innerHTML;
            }
            else
            {
                editor.contentWindow.onload = function(){initialContent = document.getElementById("contentframe").contentWindow.document.body.innerHTML;}
                return initialContent;
            }
        }
    }
    
    return content;
}

// function to save selection for IE
var selectedrange;

function SaveSelection()
{
    
    var editor = document.getElementById("contentframe");
    if (editor.contentWindow.document != null)
    {
        selectedrange = editor.contentWindow.document.selection.createRange();
    }
    //alert("DEBUG INFO: save selection reached " + selectedrange);
}

function SetTransitionType(type)
{
    var radiobuttons = document.getElementsByName('transitiontype');
    
    for (var i = 0; i < radiobuttons.length; i++)
    {
        if (radiobuttons[i].value == type)
        {
            radiobuttons[i].checked = true;
            break;
        }
    }

    var hasreviewscheckbox = document.getElementById('hasreviewscheckbox');
    if (hasreviewscheckbox != null)
    {
        if (hasreviews == "yes")
            hasreviewscheckbox.checked = true;
        else
         hasreviewscheckbox.checked = false;
    }   
}

// @author Timur Calmatui
function setSelectedRadio(name, selectedValue)
{
    var radiobuttons = document.getElementsByName(name);
    
    for (var i = 0; i < radiobuttons.length; i++)
    {
        if (radiobuttons[i].value == selectedValue)
        {
            radiobuttons[i].checked = true;
            break;
        }
    }
}


function SelectNewlyCreatedPage(selresname)
{
    makePOSTRequestSync("getpagelist.ashx?username="+username+"&appid="+appid+"&pagename="+selresname, "", PageListCallback);
    if (selresname != "")
        selectfileitem(selresname + ".html");
    else
        selectfileitem("tab1.html");
    ChooseSelection();
}

function PageListCallback(response)
{
   var respages = document.getElementById("respages");
   respages.innerHTML = response;
}

// Dynamic HTML JS functions

var isDynamic = "false";
var jsonURL = "";

function initDynamicBlock(isDynamicParam, jsonURLParam)
{
    isDynamic = isDynamicParam;
    jsonURL = jsonURLParam;
    if (isDynamic == "true")
    {
        document.getElementById("dynamic_html_checkbox").checked = true;
    }
    else
    {
        document.getElementById("dynamic_html_checkbox").checked = false;
    }
    document.getElementById("jsonurl").value = jsonURL;
    onDynamicHTMLClick();
}

function onDynamicHTMLClick()
{
    var chkbox = document.getElementById("dynamic_html_checkbox");
    var dynamic_block = document.getElementById("dynamic_html_block");
    if (chkbox.checked)
    {
        isDynamic = "true";
        dynamic_block.style.display = "block";
    }
    else
    {
        isDynamic = "false";
        dynamic_block.style.display = "none";
    }
}

function onJsonURLChange()
{
    var jsoneditbox = document.getElementById("jsonurl");
    jsonURL = jsoneditbox.value;
}
