// JavaScript Document


//*************************************************************************
// HTMLAREA extra funcionts 
//**************************************************************************


//HTMLAREA init funcitons
function initHTMLArea(TAid,docURL) {
	
	var editor=null;
	XML2TextArea(docURL,TAid);
	editor = new HTMLArea(TAid);
	var cfg=editor.config;
	cfg.registerButton({
	  id        : "section",
	  tooltip   : "New Section",
	  image     : "htmlarea/images/ed_section.gif",
	  textMode  : false,
	  action    : function(editor, id) {
					editor.insertHTML('<div class="section"><strong>(insert title here)</strong><p>(insert section contents here)</p></div>');
				  }
	});
	cfg.registerButton({
	  id        : "note",
	  tooltip   : "Insert Note",
	  image     : "htmlarea/images/ed_note.gif",
	  textMode  : true,
	  action    : function(editor, id) {
					//alert(editor.getParentElement().getAttributeNode('class').nodeValue);
					if (HTMLArea.is_ie) {
						if (editor.getParentElement().getAttributeNode('class').nodeValue=="note")
							editor.getParentElement().getAttributeNode('class').nodeValue="p";
						else
							editor.surroundHTML('<div class="note">','</div>');
					}
					else  
					if(editor.getParentElement().getAttribute('class')=="note"){
						editor.getParentElement().setAttribute('class','p');
						
					}else
            if (editor.getSelectedHTML()!="")
					    editor.surroundHTML('<div class="note">','</div>');
            else
              editor.insertHTML('<div class="note">(replace your note contents here)</div>');
				  },
		context : "p"
	});
	cfg.registerButton({
	  id        : "source",
	  tooltip   : "Insert Source",
	  image     : "htmlarea/images/ed_source.gif",
	  textMode  : true,
	  action    : function(editor, id) {
	  				if (HTMLArea.is_ie) {
						if (editor.getParentElement().getAttributeNode('class').nodeValue=="source")
							editor.getParentElement().getAttributeNode('class').nodeValue="p";
						else
							editor.surroundHTML('<div class="source"><pre>','</pre></div>');
					}
					else  
					if(editor.getParentElement().getAttribute('class')=="source"){
						editor.getParentElement().setAttribute('class','p');
						
					}else
            if (editor.getSelectedHTML()!="")
    					editor.surroundHTML('<div class="source"><pre>','</pre></div>');
            else
              editor.insertHTML('<div class="source"><pre>(replace your source contents here)</pre></div>');
				  },
		context : "p"
	});
		cfg.registerButton({
	  id        : "code",
	  tooltip   : "Insert Code",
	  image     : "htmlarea/images/ed_source.gif",
	  textMode  : true,
	  action    : function(editor, id) {
		  			if (HTMLArea.is_ie) {
						if (editor.getParentElement().getAttributeNode('class').nodeValue=="code")
							editor.getParentElement().getAttributeNode('class').nodeValue="p";
						else
							editor.surroundHTML('<div class="code">','</div>');
					}
					else  
					if(editor.getParentElement().getAttribute('class')=="code"){
						editor.getParentElement().setAttribute('class','p');
						
					}else
            if (editor.getSelectedHTML()!="")
    					editor.surroundHTML('<div class="code"><pre>','</pre></div>');
            else
              editor.insertHTML('<div class="code"><pre>(replace your code contents here)</pre></div>');
				  },
		context : "p"
	});
	cfg.registerButton({
	  id        : "p",
	  tooltip   : "New Paragraph",
	  image     : "htmlarea/images/ed_p.gif",
	  textMode  : false,
	  action    : function(editor, id) {
					editor.surroundHTML('<p>','</p>');
				  }
	});
		
	
				
	cfg.toolbar = [
							 ["bold", "italic", "subscript", "superscript", "separator",
							  "orderedlist", "unorderedlist", "outdent", "indent", "separator",
							  "createlink", "insertimage", "inserttable", "separator",
							  "section", "p", "note", "source", "code", "separator","copy", "cut", "paste", "space", "separator",
							  "htmlmode"
								]
						];
	cfg.pageStyle = "@import url(css/styles.css); body { background-color: white; text-align:left }";
	editor.registerPlugin(SpellChecker);
	editor.registerPlugin(TableOperations);
	editor.generate();
}

//HTMLAREA convertion functions

function XML2TextArea(sUri, textarea) {
   var xmlHttp = XmlHttp.create();
   var async = false;
   xmlHttp.open("GET", sUri, async);
   xmlHttp.send(null);
   document.getElementById(textarea).value=xmlHttp.responseText; // responseText : String
}

function TextArea2CDATA(textarea) {
	text=document.getElementById(textarea).value;
	document.getElementById(textarea).value='<![CDATA['+text+']]>';
	//alert(document.getElementById(textarea).value);
}


//***************************************************************
// VALIDATION SCRIPTS
//*************************************************************
var reqFields=new Array();
function addRequiredField(fieldID) {
		reqFields[reqFields.length]=fieldID;
}

function validateForm() {
		var ret=true;
		for (i=0;i<reqFields.length;i++){
				var obj=document.getElementById(reqFields[i]);			
				if (obj.value=="") {
						ret=false;
						obj.style.backgroundImage="url()";
						var imgerrID="iconerr_"+reqFields[i];
						var imagerr=document.getElementById(imgerrID);
						imagerr.className="tip1";
						imagerr.src="images/xforms-x.gif";
						document.getElementById("errors").style.visibility='visible';
						 xInitTooltips(1);
				}
		}
	return (ret);
}
//*******************************************************************************
//tooltips error messages
//*******************************************************************************
xInclude('cross-browser.com/x/x_core.js', 'cross-browser.com/x/x_event.js', 'cross-browser.com/x/x_dom.js', 'cross-browser.com/x/x_slide.js');

function xInitTooltips(n) {
  var i, j, tips;
  for (i = 1; i <= n; ++i) {
    tips = xGetElementsByClassName('tip' + i);
    for (j = 0; j < tips.length; ++j) {
	  tips[j].isTip = true;
      tips[j].onmouseover = xttOnMouseover;
    }
  }
  xAddEventListener(document, 'mousemove', xttOnMousemove, false);
}
function xttOnMouseover(e) {
  
  var tt = xGetElementById('xTooltip');
  tt.className = 'x' + this.className;
  tt.innerHTML = "This field is <strong>required</strong>.<br/>Please fill it to continue.";
  //xMoveTo(tt, xPageX(this)-500, xPageY(this) - xHeight(tt) +200);
  xShow(tt);
  xSlideTo(tt, xPageX(this), xPageY(this) - xHeight(tt) - 2, 250);
}
function xttOnMousemove(e) {
  var evnt = new xEvent(e);
  var ele = evnt.target;
  if (!ele || !ele.isTip) {
    xHide('xTooltip');
  }
}


//******************************************************
// Cookies Functions
//******************************************************


function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
//
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
	var j = i + alen;
	if (document.cookie.substring(i, j) == arg)
	return getCookieVal(j);
	i = document.cookie.indexOf(" ", i) + 1;
	if (i == 0) break;
	}
	return null;
}



function getCookieData(labelName) {
	var labelLen = labelName.length
	// read cookie property only once for speed
	var cookieData = document.cookie
	var cLen = cookieData.length
	var i = 0
	var cEnd
	while (i < cLen) {
		var j = i + labelLen
		if (cookieData.substring(i,j) == labelName) {
			cEnd = cookieData.indexOf(";",j)
			if (cEnd == -1) {
			cEnd = cookieData.length
		}
		return unescape(cookieData.substring(j+1, cEnd))
		}
		i++
	}
	return ""
}
function DeleteCookie (name,path,domain) {
	if (GetCookie(name)) {
	document.cookie = name + "=" +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
