imagesDir = "http://www.tutiempo.net/Tierra/Editor/icons/";
wysiwygWidth = 673;
wysiwygHeight = 350;

var Fonts = new Array();
Fonts[0] = "Arial";
Fonts[1] = "Sans Serif";
Fonts[2] = "Tahoma";
Fonts[3] = "Verdana";
Fonts[4] = "Courier New";
Fonts[5] = "Georgia";
Fonts[6] = "Times New Roman";
Fonts[7] = "Impact";
Fonts[8] = "Comic Sans MS";

var FontSizes = new Array();
FontSizes[0]  = "1";
FontSizes[1]  = "2";
FontSizes[2]  = "3";
FontSizes[3]  = "4";
FontSizes[4]  = "5";
FontSizes[5]  = "6";
FontSizes[6]  = "7";

var buttonName = new Array();
buttonName[0]  = "bold";
buttonName[1]  = "italic";
buttonName[2]  = "underline";
buttonName[3]  = "seperator";
buttonName[4]  = "subscript";
buttonName[5]  = "superscript";
buttonName[6]  = "seperator";
buttonName[7]  = "justifyleft";
buttonName[8]  = "justifycenter";
buttonName[9] = "justifyright";
buttonName[10]  = "justifyfull";
buttonName[11] = "seperator";
buttonName[12] = "unorderedlist";
buttonName[13] = "orderedlist";
buttonName[14] = "outdent";
buttonName[15] = "indent";
buttonName[16] = "seperator";
buttonName[17]  = "cut";
buttonName[18]  = "copy";
buttonName[19]  = "paste";
buttonName[20]  = "seperator";
buttonName[21]  = "undo";
buttonName[22]  = "redo";
buttonName[23]  = "seperator";

	
var ToolbarList = {
//Name              buttonID                 buttonTitle           buttonImage                            buttonImageRollover
"bold":           ['Bold',                 'Negrita',               imagesDir + 'bold.gif',               imagesDir + 'bold_on.gif'],
"italic":         ['Italic',               'Cursiva',             imagesDir + 'italics.gif',            imagesDir + 'italics_on.gif'],
"underline":      ['Underline',            'Subrayado',          imagesDir + 'underline.gif',          imagesDir + 'underline_on.gif'],
"strikethrough":  ['Strikethrough',        'Strikethrough',      imagesDir + 'strikethrough.gif',      imagesDir + 'strikethrough_on.gif'],
"seperator":      ['',                     '',                   imagesDir + 'seperator.gif',          imagesDir + 'seperator.gif'],
"subscript":      ['Subscript',            'Subindice',          imagesDir + 'subscript.gif',          imagesDir + 'subscript_on.gif'],
"superscript":    ['Superscript',          'Superindice',        imagesDir + 'superscript.gif',        imagesDir + 'superscript_on.gif'],
"justifyleft":    ['Justifyleft',          'Alinear a la izquierda',        imagesDir + 'justify_left.gif',       imagesDir + 'justify_left_on.gif'],
"justifycenter":  ['Justifycenter',        'Centrar',      imagesDir + 'justify_center.gif',     imagesDir + 'justify_center_on.gif'],
"justifyright":   ['Justifyright',         'Alinear a la derecha',       imagesDir + 'justify_right.gif',      imagesDir + 'justify_right_on.gif'],
"justifyfull":   ['Justifyfull',         'Justificar',       imagesDir + 'justify_full.gif',      imagesDir + 'justify_full_on.gif'],
"unorderedlist":  ['InsertUnorderedList',  'Insertar lista',imagesDir + 'list_unordered.gif',     imagesDir + 'list_unordered_on.gif'],
"orderedlist":    ['InsertOrderedList',    'Insertar lista ordenada',  imagesDir + 'list_ordered.gif',       imagesDir + 'list_ordered_on.gif'],
"outdent":        ['Outdent',              'Reducir sangria',            imagesDir + 'indent_left.gif',        imagesDir + 'indent_left_on.gif'],
"indent":         ['Indent',               'Aumentar sangria',             imagesDir + 'indent_right.gif',       imagesDir + 'indent_right_on.gif'],
"cut":            ['Cut',                  'Cortar',                imagesDir + 'cut.gif',                imagesDir + 'cut_on.gif'],
"copy":           ['Copy',                 'Copiar',               imagesDir + 'copy.gif',               imagesDir + 'copy_on.gif'],
"paste":          ['Paste',                'Pegar',              imagesDir + 'paste.gif',              imagesDir + 'paste_on.gif'],
"forecolor":      ['ForeColor',            'Color fuente',          imagesDir + 'forecolor.gif',          imagesDir + 'forecolor_on.gif'],
"backcolor":      ['BackColor',            'BackColor',          imagesDir + 'backcolor.gif',          imagesDir + 'backcolor_on.gif'],
"undo":           ['Undo',                 'Deshacer',               imagesDir + 'undo.gif',               imagesDir + 'undo_on.gif'],
"redo":           ['Redo',                 'Rehacer',               imagesDir + 'redo.gif',               imagesDir + 'redo_on.gif'],
"inserttable":    ['InsertTable',          'Insertar tabla',        imagesDir + 'insert_table.gif',       imagesDir + 'insert_table_on.gif'],
"insertimage":    ['InsertImage',          'Insertar imagen',        imagesDir + 'insert_picture.gif',     imagesDir + 'insert_picture_on.gif'],
"createlink":     ['CreateLink',           'Crear un vinculo',         imagesDir + 'insert_hyperlink.gif',   imagesDir + 'insert_hyperlink_on.gif'],
"viewSource":     ['ViewSource',           'Ver codigo HTML',         imagesDir + 'view_source.gif',        imagesDir + 'view_source_on.gif'],
"viewText":       ['ViewText',             'Vista normal',           imagesDir + 'view_text.gif',          imagesDir + 'view_text_on.gif'],
"help":           ['Help',                 'Ayuda',               imagesDir + 'help.gif',               imagesDir + 'help_on.gif'],
"selectfont":     ['SelectFont',           'Seleccionar fuente',         imagesDir + 'select_font.gif',        imagesDir + 'select_font_on.gif'],
"selectsize":     ['SelectSize',           'Seleccionar tamaņo',         imagesDir + 'select_size.gif',        imagesDir + 'select_size_on.gif']
};

if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){
  HTMLElement.prototype.insertAdjacentElement = function
  (where,parsedNode)
	{
	  switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling) 
      this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else this.parentNode.appendChild(parsedNode);
			break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function
  (where,htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}


	HTMLElement.prototype.insertAdjacentText = function
  (where,txtStr)
	{
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
};


viewTextMode = 0;

function generate_wysiwyg(textareaID) {
document.getElementById(textareaID).style.display = 'none'; 
var n = textareaID;
toolbarWidth = parseFloat(wysiwygWidth) + 2;
var toolbar;
toolbar =  '<table cellpadding="0" cellspacing="0" border="0" class="toolbar1" style="width:' + toolbarWidth + 'px;"><tr><td style="width: 6px;"><img src="' +imagesDir+ 'seperator2.gif" alt="" hspace="3"></td>';
for (var i = 0; i <= buttonName.length;) { 
	if (buttonName[i]) {
	var buttonObj = ToolbarList[buttonName[i]];
	var buttonID = buttonObj[0];
	var buttonTitle = buttonObj[1];
	var buttonImage = buttonObj[2];
	var buttonImageRollover = buttonObj[3];
		if (buttonName[i] == "seperator") {
		toolbar += '<td style="width: 12px;" align="center"><img src="' +buttonImage+ '" border=0 unselectable="on" width="2" height="18" hspace="2" unselectable="on"></td>';
		}
		else {
		toolbar += '<td style="width: 22px;"><img src="' +buttonImage+ '" border=0 unselectable="on" title="' +buttonTitle+ '" id="' +buttonID+ '" class="button" onClick="formatText(this.id,\'' + n + '\');" onmouseover="if(className==\'button\'){className=\'buttonOver\'}; this.src=\'' + buttonImageRollover + '\';" onmouseout="if(className==\'buttonOver\'){className=\'button\'}; this.src=\'' + buttonImage + '\';" unselectable="on" width="20" height="20"></td>';
		}
    }
    i++;
  }
toolbar += '<td>&nbsp;</td></tr></table>';  
 	
var iframe = '<table cellpadding="0" cellspacing="0" border="0" style="width:' + wysiwygWidth + 'px; height:' + wysiwygHeight + 'px;border: 1px solid #CCCCCC;margin-top:3px;"><tr><td valign="top">\n'
  + '<iframe frameborder="0" id="wysiwyg' + n + '"></iframe>\n'
  + '</td></tr></table>\n';

document.getElementById(n).insertAdjacentHTML("afterEnd", toolbar + iframe);
document.getElementById("wysiwyg" + n).style.height = wysiwygHeight + "px";
document.getElementById("wysiwyg" + n).style.width = wysiwygWidth + "px";
var content = document.getElementById(n).value;
var doc = document.getElementById("wysiwyg" + n).contentWindow.document;
doc.open();
doc.write('<style type="text/css" media="screen">body {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #333333;} a {color: #FA9E23; text-decoration:none;} a:hover {color: #000000; text-decoration:none;}</style>' + content);
//doc.write(content);
doc.close();
doc.body.contentEditable = true;
doc.designMode = "on";
var browserName = navigator.appName;
  if (browserName == "Microsoft Internet Explorer") {
    for (var idx=0; idx < document.forms.length; idx++) {
      document.forms[idx].attachEvent('onsubmit', function() { updateTextArea(n); });
    }
  }
  else {
  	for (var idx=0; idx < document.forms.length; idx++) {
    	document.forms[idx].addEventListener('submit',function OnSumbmit() { updateTextArea(n); }, true);
    }
  }
};

function formatText(id, n, selected) {

  document.getElementById("wysiwyg" + n).contentWindow.focus();
	
	var formatIDs = new Array("FontSize","FontName","Bold","Italic","Underline","Subscript","Superscript","Strikethrough","Justifyleft","Justifyright","Justifycenter","InsertUnorderedList","InsertOrderedList","Indent","Outdent","ForeColor","BackColor","InsertImage","InsertTable","CreateLink");
  
	for (var i = 0; i <= formatIDs.length;) {
		if (formatIDs[i] == id) {
			 var disabled_id = 1; 
		}
	  i++;
	}
	
	if (viewTextMode == 1 && disabled_id == 1) {
	  alert ("You are in HTML Mode. This feature has been disabled.");	
	}
	
	else {
	
	  // FontSize
	  if (id == "FontSize") {
      document.getElementById("wysiwyg" + n).contentWindow.document.execCommand("FontSize", false, selected);
	  }
	  
		// FontName
	  else if (id == "FontName") {
      document.getElementById("wysiwyg" + n).contentWindow.document.execCommand("FontName", false, selected);
	  }
	
	  
		// Every other command
	  else {
      document.getElementById("wysiwyg" + n).contentWindow.document.execCommand(id, false, null);
		}
  }
};



function insertHTML(html, n) {

  var browserName = navigator.appName;
	 	 
	if (browserName == "Microsoft Internet Explorer") {	  
	  document.getElementById('wysiwyg' + n).contentWindow.document.selection.createRange().pasteHTML(html);   
	} 
	 
	else {
	  var div = document.getElementById('wysiwyg' + n).contentWindow.document.createElement("div");
		 
		div.innerHTML = html;
		var node = insertNodeAtSelection(div, n);		
	}
	
}


function insertNodeAtSelection(insertNode, n) {
  // get current selection
  var sel = document.getElementById('wysiwyg' + n).contentWindow.getSelection();

  // get the first range of the selection
  // (there's almost always only one range)
  var range = sel.getRangeAt(0);

  // deselect everything
  sel.removeAllRanges();

  // remove content of current selection from document
  range.deleteContents();

  // get location of current selection
  var container = range.startContainer;
  var pos = range.startOffset;

  // make a new range for the new selection
  range=document.createRange();

  if (container.nodeType==3 && insertNode.nodeType==3) {

    // if we insert text in a textnode, do optimized insertion
    container.insertData(pos, insertNode.nodeValue);

    // put cursor after inserted text
    range.setEnd(container, pos+insertNode.length);
    range.setStart(container, pos+insertNode.length);
  } 
	
	else {
    var afterNode;
    
		if (container.nodeType==3) {
      // when inserting into a textnode
      // we create 2 new textnodes
      // and put the insertNode in between

      var textNode = container;
      container = textNode.parentNode;
      var text = textNode.nodeValue;

      // text before the split
      var textBefore = text.substr(0,pos);
      // text after the split
      var textAfter = text.substr(pos);

      var beforeNode = document.createTextNode(textBefore);
      afterNode = document.createTextNode(textAfter);

      // insert the 3 new nodes before the old one
      container.insertBefore(afterNode, textNode);
      container.insertBefore(insertNode, afterNode);
      container.insertBefore(beforeNode, insertNode);

      // remove the old node
      container.removeChild(textNode);
    } 
	
	  else {
      // else simply insert the node
      afterNode = container.childNodes[pos];
      container.insertBefore(insertNode, afterNode);
    }

    range.setEnd(afterNode, 0);
    range.setStart(afterNode, 0);
  }

  sel.addRange(range);
};

	


function _dec_to_rgb(value) {
  var hex_string = "";
  for (var hexpair = 0; hexpair < 3; hexpair++) {
    var myByte = value & 0xFF;            // get low byte
    value >>= 8;                          // drop low byte
    var nybble2 = myByte & 0x0F;          // get low nybble (4 bits)
    var nybble1 = (myByte >> 4) & 0x0F;   // get high nybble
    hex_string += nybble1.toString(16);   // convert nybble to hex
    hex_string += nybble2.toString(16);   // convert nybble to hex
  }
  return hex_string.toUpperCase();
};



function outputFontSelect(n) {

  var FontSelectObj        = ToolbarList['selectfont'];
  var FontSelect           = FontSelectObj[2];
  var FontSelectOn         = FontSelectObj[3];
  
	Fonts.sort();
	var FontSelectDropDown = new Array;
	FontSelectDropDown[n] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td onMouseOver="document.getElementById(\'selectFont' + n + '\').src=\'' + FontSelectOn + '\';" onMouseOut="document.getElementById(\'selectFont' + n + '\').src=\'' + FontSelect + '\';"><img src="' + FontSelect + '" id="selectFont' + n + '" width="85" height="20" onClick="showFonts(\'' + n + '\');" unselectable="on"><br>';
	FontSelectDropDown[n] += '<span id="Fonts' + n + '" class="dropdown" style="width: 145px;">';

	for (var i = 0; i <= Fonts.length;) {
	  if (Fonts[i]) {
      FontSelectDropDown[n] += '<button type="button" onClick="formatText(\'FontName\',\'' + n + '\',\'' + Fonts[i] + '\')\; hideFonts(\'' + n + '\');" onMouseOver="this.className=\'mouseOver\'" onMouseOut="this.className=\'mouseOut\'" class="mouseOut" style="width: 120px;"><table cellpadding="0" cellspacing="0" border="0"><tr><td align="left" style="font-family:' + Fonts[i] + '; font-size: 12px;">' + Fonts[i] + '</td></tr></table></button><br>';	
    }	  
	  i++;
  }
	FontSelectDropDown[n] += '</span></td></tr></table>';
	document.getElementById('FontSelect' + n).insertAdjacentHTML("afterBegin", FontSelectDropDown[n]);
};



function outputFontSizes(n) {

  var FontSizeObj        = ToolbarList['selectsize'];
  var FontSize           = FontSizeObj[2];
  var FontSizeOn         = FontSizeObj[3];

	FontSizes.sort();
	var FontSizesDropDown = new Array;
	FontSizesDropDown[n] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td onMouseOver="document.getElementById(\'selectSize' + n + '\').src=\'' + FontSizeOn + '\';" onMouseOut="document.getElementById(\'selectSize' + n + '\').src=\'' + FontSize + '\';"><img src="' + FontSize + '" id="selectSize' + n + '" width="49" height="20" onClick="showFontSizes(\'' + n + '\');" unselectable="on"><br>';
  FontSizesDropDown[n] += '<span id="Sizes' + n + '" class="dropdown" style="width: 170px;">';

	for (var i = 0; i <= FontSizes.length;) {
	  if (FontSizes[i]) {
      FontSizesDropDown[n] += '<button type="button" onClick="formatText(\'FontSize\',\'' + n + '\',\'' + FontSizes[i] + '\')\;hideFontSizes(\'' + n + '\');" onMouseOver="this.className=\'mouseOver\'" onMouseOut="this.className=\'mouseOut\'" class="mouseOut" style="width: 145px;"><table cellpadding="0" cellspacing="0" border="0"><tr><td align="left" style="font-family: arial, verdana, helvetica;"><font size="' + FontSizes[i] + '">size ' + FontSizes[i] + '</font></td></tr></table></button><br>';	
    }	  
	  i++;
  }
	FontSizesDropDown[n] += '</span></td></tr></table>';
	document.getElementById('FontSizes' + n).insertAdjacentHTML("afterBegin", FontSizesDropDown[n]);
};



function hideFonts(n) {
  document.getElementById('Fonts' + n).style.display = 'none'; 
};



function hideFontSizes(n) {
  document.getElementById('Sizes' + n).style.display = 'none'; 
};

function showFonts(n) { 
  if (document.getElementById('Fonts' + n).style.display == 'block') {
    document.getElementById('Fonts' + n).style.display = 'none';
	}
  else {
    document.getElementById('Fonts' + n).style.display = 'block'; 
    document.getElementById('Fonts' + n).style.position = 'absolute';		
  }
};

function showFontSizes(n) { 
  if (document.getElementById('Sizes' + n).style.display == 'block') {
    document.getElementById('Sizes' + n).style.display = 'none';
	}
  else {
    document.getElementById('Sizes' + n).style.display = 'block'; 
    document.getElementById('Sizes' + n).style.position = 'absolute';		
  }
};

function viewText(n) {


  var getDocument = document.getElementById("wysiwyg" + n).contentWindow.document;
  var browserName = navigator.appName;
	
	// View Text for IE 	  	 
  if (browserName == "Microsoft Internet Explorer") {
    var iText = getDocument.body.innerText;
    getDocument.body.innerHTML = iText;
	}
  
	// View Text for Mozilla/Netscape
  else {
    var html = getDocument.body.ownerDocument.createRange();
    html.selectNodeContents(getDocument.body);
    getDocument.body.innerHTML = html.toString();
	}
  
	// Hide the Text Mode button and show the HTML Mode button
	
	// reset the font values
  getDocument.body.style.fontSize = "";
	getDocument.body.style.fontFamily = ""; 
	viewTextMode = 0;
};

function updateTextArea(n) {
document.getElementById(n).value = document.getElementById("wysiwyg" + n).contentWindow.document.body.innerHTML;
};




var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
function comprobar(f){
if (f.nombre.value == ''){alert('Debes especificar tu nombre.'); return false;}
else if (f.mail.value == ''){alert('Debes especificar tu E-Mail.'); return false;}
else if (f.acepar_condiciones.checked == false){alert('Antes de continuar es necesario que aceptes que has leido y comprendido el Aviso legal y la Proteccion de datos de la Web.'); return false;}
else
	{
	var returnval=emailfilter.test(f.mail.value);
	if (returnval==false){alert('El E-Mail que has introducido, no es correcto'); return false;}
	else {return true;}
	}
}