//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Public functions and objects
var Davinci = new Object();
var TODEBUG = false;

function DEBUG(p_strDebugMsg)
{
	if(TODEBUG)
	{
		alert(p_strDebugMsg);
	}
}

function ShowObjectPropertyNames(p_Obj)
{
	var names = "";
	for(var name in p_Obj)
		names += name + ":" + p_Obj[name] + "\n";
	alert(names);
}

function parseXMLData(node)
{
	var obj = new Object();
	for(var t_Index=0;t_Index<node.childNodes.length;t_Index++)
	{
		var newObj = null;
		if(node.childNodes[t_Index].childNodes.length == 1 && node.childNodes[t_Index].childNodes[0].nodeType == 3)
		{
			newObj = node.childNodes[t_Index].childNodes[0].nodeValue;
			//alert(newObj);
		}
		else
		{
			newObj = parseXMLData(node.childNodes[t_Index]);
		}

		if(obj[node.childNodes[t_Index].tagName] == null)
		{
			obj[node.childNodes[t_Index].tagName] = newObj;
		}
		else if(obj[node.childNodes[t_Index].tagName] instanceof Array)
		{
			var length = obj[node.childNodes[t_Index].tagName].length;
			obj[node.childNodes[t_Index].tagName][length] = newObj;
		}
		else
		{
			var oldObj = obj[node.childNodes[t_Index].tagName];
			obj[node.childNodes[t_Index].tagName] = new Array();
			obj[node.childNodes[t_Index].tagName][0] = oldObj;
			obj[node.childNodes[t_Index].tagName][1] = newObj;
		}
	}
	return obj;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Davinci.RequestHandler
Davinci.RequestHandler = function(p_OnData,p_OnErr)
{
	this.OnData = p_OnData;
	this.OnErr = p_OnErr;
	DEBUG("RequestHandler created");
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Davinci.Reqest
Davinci.Request = function(p_CmdID,p_strURL,p_Handler,p_strMethod,p_ParamList,p_strContentType)
{
	var m_ObjectSelf = this;	
	this.m_Data = new Object();
	this.m_CmdID = p_CmdID;
	this.m_strURL = p_strURL;
	this.m_Handler = p_Handler;
	this.m_strMethod = p_strMethod;
	this.m_ParamList = p_ParamList;
	this.m_strContentType = p_strContentType;
	
	this.onState = function()
	{
		if(m_ObjectSelf.m_XMLHttpReq.readyState == 4)
		{
			if(m_ObjectSelf.m_XMLHttpReq.status == 200)
			{				
				m_ObjectSelf.m_Handler.OnData(m_ObjectSelf.m_XMLHttpReq.responseXML,m_ObjectSelf);
			}
			else
			{
				m_ObjectSelf.m_Handler.OnErr(100,"t_XMLHttpReq Fail Status="+m_ObjectSelf.m_XMLHttpReq.status,m_ObjectSelf);
			}
		}
	}
}

Davinci.Request.prototype.fire = function()
{	
	if(!this.m_strMethod)
	{		
		this.m_strMethod = "GET";
	}
	
	if(!this.m_strContentType && this.m_strMethod=="POST")
	{		
		this.m_strContentType = "application/x-www-form-urlencoded;";		
	}
	
	if(window.ActiveXObject)
	{
		try
		{
			this.m_XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{			
			try
			{
				this.m_XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				alert("222");
			}
		}
	}
	else if(window.XMLHttpRequest)
	{
		this.m_XMLHttpReq = new XMLHttpRequest();
	}
		
	if(!this.m_XMLHttpReq)
	{
		this.m_Handler.OnErr(10,"Can not create Microsoft.XMLHTTP object");
		return;
	}		
	
	try
	{			
		DEBUG(this.m_ParamList);
		this.m_XMLHttpReq.onreadystatechange = this.onState;
		this.m_XMLHttpReq.open(this.m_strMethod,this.m_strURL,true);

		if(this.m_strContentType)
			this.m_XMLHttpReq.setRequestHeader("Content-Type",this.m_strContentType);

		this.m_XMLHttpReq.send(this.m_ParamList);
	}
	catch(e)
	{	
		this.m_Handler.OnErr(10,"Exception when send request,Msg:"+e.message);
		return;
	}
	DEBUG("Request fired");	
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function createForm(p_strAction)
{	
	var t_RootForm = document.createElement("Form");		
	t_RootForm.method = "POST";
	t_RootForm.action = p_strAction;
	return t_RootForm;
}

function Submit(p_strAction,p_divID)
{
	var t_Form = createForm(p_strAction);
	t_Form.appendChild(document.getElementById(p_divID));
	document.appendChild(t_Form);
	t_Form.submit();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Davinci.Page = function()
{
	this.m_PageLoadObserverList = new Array();
}

Davinci.Page.prototype.regPageLoadObserver = function(p_Object)
{
	this.m_PageLoadObserverList.push(p_Object);
}

Davinci.Page.prototype.onLoad = function()
{	
	for(t_Index=0;t_Index<this.m_PageLoadObserverList.length;t_Index++)
	{
		this.m_PageLoadObserverList[t_Index].onPageLoad();
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function addtelcode(textname)
{
	var teltext = getselecttext();
	insertC(document.all[textname],"<a href='tel:"+teltext+"'>"+teltext+"</a>");
}
function addmailcode(textname)
{
	var mailtext = getselecttext();
	insertC(document.all[textname],"<a href='mailto:"+mailtext+"'>"+mailtext+"</a>");
}
function addlinkcode(textname)
{
	insertC(document.all[textname],"<a href='在这里加入网址'>"+getselecttext()+"</a>");
}
function addimagecode(textname)
{
	insertC(document.all[textname],"<img src='http://地址文件名' border='0'></img>");
}
function addmqcode(textname)
{
	insertC(document.all[textname],"<marquee bgcolor='orange'>"+getselecttext()+"</marquee>");
}
function addtextcolorcode(selector,textname,color)
{
	if(color != "nc")
	{
		insertC(document.all[textname],"<font color='"+color+"'>"+getselecttext()+"</font>");
		selector.options.selectedIndex = 0;
	}
}
function addretcode(textname)
{
	insertC(document.all[textname],"<br>");
}

function storeCaret(s)
{ 
	if (s.createTextRange) s.caretPos = document.selection.createRange().duplicate();
}
function getselecttext()
{
	if(document.selection.createRange().text == null || document.selection.createRange().text == "")
		return "内容";
	return document.selection.createRange().text;
}
function insertC(s,text)
{ 
	if (s.createTextRange && s.caretPos)
	{ 
		var caretPos = s.caretPos; 
		caretPos.text =caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?text + ' ' : text;  
	}
	else s.value+=text;
	s.focus();
}

function ltrim(s)
{ 
	return s.replace( /^\s*/, "");
} 

function rtrim(s)
{ 
	return s.replace( /\s*$/, "");
} 

function trim(s)
{ 
	return rtrim(ltrim(s));
}

function getEText(e)
{
	if(e == null)
		return "";
	if(e.firstChild == null)
		return "";
	if(e.firstChild.nodeValue == null)
		return "";
	return e.firstChild.nodeValue;
}

function getEAttr(e,name)
{
	if(e == null)
		return "";
	if(e.getAttribute(name) == null)
		return "";
	return e.getAttribute(name);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
