/*
 * onpageedit.js	- JavaScript support routines for EPiServer
 * Copyright (c) 2007 EPiServer AB
*/

var propInfo = new Array();
var isDopeEditing = false;

function InitDopeMode()
{
    window.isDopeEditing = true;
	for(var i = 0; i < propInfo.length ; i++)
	{
		propInfo[i].EnableEditing();
	}
}

function OnDopeEdit()
{
    var _epiOnPageEditControl = EPi.GetProperty(window, "EPiOnPageEditControl");
	__doPostBack(_epiOnPageEditControl, "Edit");
}

function OnDopeCancel()
{
	window.location.href = window.location.href;
}

function OnDopeSave()
{
	isDopeEditing = false;
	PreparePostback();
	var _epiOnPageEditControl = EPi.GetProperty(window, "EPiOnPageEditControl");
	__doPostBack(_epiOnPageEditControl, "Publish");
}

function PreparePostback()
{
	var form = getMainForm();

	var input = null;
	for(var i = 0 ; i < propInfo.length ; i++)
	{
		input = form.elements[propInfo[i].PostbackReference];
		if(input==null)
		{
		    input = document.createElement('input');
		    input.setAttribute('type', 'hidden');
		    input.setAttribute('name', propInfo[i].PostbackReference);
			form.appendChild(input);
		}
		var realElement = document.getElementById(propInfo[i].id);
		if(propInfo[i].IsHtmlContent=="True")
			input.value=realElement.innerHTML;
		else
			input.value=realElement.innerText;
	}
}

function RegisterProperty(element)
{
	for(var i=0;i<propInfo.length;i++)
	{
		if(element.PostbackReference==propInfo[i].PostbackReference)
		{
			propInfo[i]=element;
			return;
		}
	}
	
	propInfo[propInfo.length] = element;
}

function OnDopeCopySelection()
{    
    //TODO: Fix firefox changes...
	if (document.selection && document.selection.createRange)
	{
	    var sel=document.selection.createRange();
        sel.select();
	    document.execCommand('Copy');
	}
	//return false;
}

function OnDopeNavigateEvent(name,url)
{
	var wnd = getEditWindow();
	if(wnd!=null)
		wnd.commandEvent(window, new wnd.commandDescriptor(name,url));

}

function isEditMode()
{
	var frame;
	var wnd = this.window;
	
	while (wnd != null)
	{
		try
		{
			if(wnd.commandEvent && wnd.latestNavigate!=null && wnd.latestNavigate!='')
				return true;
		} 
		catch (e)
		{
			return false;
		}
		if (wnd == wnd.parent)
			break;
		wnd = wnd.parent;
	}
	return false;
}

function getEditWindow()
{
	var frame;
	var wnd = this.window;
	
	while (wnd != null)
	{
		try
		{
			if(wnd.commandEvent && wnd.latestNavigate!=null && wnd.latestNavigate!='')
				return wnd;
		}
		catch (e)
		{
			return null;
		}

		if (wnd == wnd.parent)
			break;
		wnd = wnd.parent;
	}
	return null;
}

function getMainForm()
{
    //using asp.net form name. If user have several forms on page document.forms[0] will not work
    var formNode;
    if (window.theForm)
    {
        // theForm is a global reference to the main form specified by asp.net.
        formNode = theForm;
    }
    else
    {
        formNode = document.forms[0];
    }
    
    return formNode;
}

function getParentForm(obj)
{
	var tag = obj;
	while(tag=tag.parentElement)
	{
		if(tag.tagName.toLowerCase()=='form')
			return tag;
	}
	
	return null;
}