/*This file is part of UC.
*
*UC is free software; you can redistribute it and/or modify
*it under the terms of the GNU General Public License as published by
*the Free Software Foundation; either version 2 of the License, or
*(at your option) any later version.
*
*UC is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*GNU General Public License for more details.
*
*You should have received a copy of the GNU General Public License
*along with UC; if not, write to the Free Software
*Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

// JavaScript Document
//obtenir la taille de la frame courante;

function getFrameWidth(){
     var h;
     if (document.all){h=document.body.clientWidth;}
     else{h=window.innerWidth;}
     return h;
}
function getFrameHeight(){
     var h;
     if (document.all){///IE
	  h=document.body.clientHeight;
	  //h=document.body.offsetHeight;
	}else{h=window.innerHeight;}//NETSCAPE
	return h;
}
//retourn un element en fonction de son id;
function getElement(id){
     var element= null;
     if(document.getElementById){
	  element=document.getElementById(id);
     }else if (document.all){
	  element = document.all[id]
     }
     return element;	
}

//TODO
function getSelectedRadioValue(radiobutton)
{
     var returnValue = "";
     if (radiobutton.length == 1){
	  returnValue = radiobutton.value;
     } else {
	  for (i=0;i<radiobutton.length;i++){
	       if (radiobutton[i].checked==true) {
		    returnValue=radiobutton[i].value;
	       }
	  }
     }
     return returnValue;
}

//=================================================================CHANGE LE TEXTE D'ID SPECIFIE ET LE VIDE
function changeText(id,texte){
     var element= $(id);
     if (element) {
	  element.innerHTML=texte;
     }
}
function checkRadio(form, radioName){
     var element=document.forms[form].elements[radioName];
     for (var i=0; i<element.length;i++) {
	  if (element[i].checked) {
	       return element[i].value;
	  }
     }
     return false;
}
//=================================================================FONCTION DE GESTION DES FORMULAIRES
function nothing(){
}
function validemail(field)
{
     var str = field.value;
     if (str != "") {
	  if (window.RegExp) {
	       var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
	       var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
	       var reg1 = new RegExp(reg1str);
	       var reg2 = new RegExp(reg2str);
	       if (!reg1.test(str) && reg2.test(str)) {
		    return true;
	       }
	       field.focus();
	       field.select();
	       return false;
	  } else {
	       if(str.indexOf("@") >= 0)
		    return true;
	       field.focus();
	       field.select();
	       return false;
	  }
     }
     else {return false; }
}
//=================================================================FONCTION DE GESTION DES IMAGES
function makevisible(){
     if (cur.filters.alpha.opacity<70) cur.filters.alpha.opacity =70;
     if (cur.filters.alpha.opacity>100) cur.filters.alpha.opacity =100;
     cur.filters.alpha.opacity=cur.filters.alpha.opacity+which;
     vitessedefil = setTimeout("makevisible()", 10);
}

function makeopt(arg1,arg2){
     which=arg1;
     cur=arg2;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////FONCTION DE GESTION DES REQUETES AJAX///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
updateContainer = function(container, dest, params){
     $(container).innerHTML = '<div style="text-align:center;"><img src="'+_uc_absurl+'template/theme_std/clip_loading_2.gif><br>loading</div>';
     new Ajax.Updater(container, dest, {
	       parameters: params, method  :'post', evalScripts: true,
     encoding:'utf-8'});
}

/////////////////////////METHODE POUR UPLOAD D'UN FICHIER
var updater ;
var destination;
function getLoadDiv(){
     var loadDiv = document.createElement("div");
     loadDiv.id = "loadingDiv";
     loadDiv.innerHTML =  '<div style="text-align:center;"><img src="'+_uc_absurl+'template/theme_std/clip_loading_2.gif><br>loading</div>';
     return loadDiv;
}
var fN = function callBack(o) {
     var elem = $(destination);
     elem.update(o.responseText);
     if (!($("loadingDiv") === null))$("loadingDiv").remove();
     elem.show();
     if (YAHOO.util.Dom.isAncestor( $('adminDiv') , elem )) {CURRENTLOGFRAME = elem.firstChild.id;}
     // updater.stop();
}
var callback = { success:fN, upload:fN }
// the function that is posts the upload form. ASYNCHRONOUSLY. WTF. oh yea. iframes. whew. 
function postFormAsync(formName, params, nextdestination){
      destination = nextdestination;
     $(destination).hide();
     $(formName).hide();
     $(destination).parentNode.insertBefore(getLoadDiv(), $(destination));
     YAHOO.util.Connect.setForm(formName,1);
     YAHOO.util.Connect.appendPostData(params);
     YAHOO.util.Connect.asyncRequest('POST','.',callback);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////FONCTIONS POUR LA DEBUG WINDOW////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////                                 
var CURRENTLOGFRAME = 'adminDiv';

function debug(val, fullMsg){
     if ($('uc_logger')) {
	  if (fullMsg) $('uc_logger').innerHTML += val;
	  else $('uc_logger').innerHTML += "[INFO] "+val+"<br>";
     }
}
function ucMessage(val){
     $('uc_message').innerHTML += val;    
}
function addUploadedFrames(uploadId){
     if (!($("addedFrames"+uploadId) === null)) $("addedFrames"+uploadId).remove()
     if (!($("uc_debug_win_content") === null)) $("uc_debug_win_content").appendChild($("UPDATEFRAMECONTENT").remove());
     $("UPDATEFRAMECONTENT").id="addedFrames"+uploadId;
     
}
function showTextFrameContent(content){
     $('adminDiv').innerHTML = content;
     showFrameContent('adminDiv');
}
function showAjaxFrameContent(frame,dest,params){
     showFrameContent('adminDiv');
     $('adminDiv').innerHTML =  '<div style="text-align:center;"><img src="'+_uc_absurl+'template/theme_std/clip_loading_2.gif><br>loading</div>';
     new Ajax.Updater('adminDiv', dest, {
	       parameters: params, method  :'post', evalScripts: true,
	       encoding:'utf-8', onComplete: function(transport) {
		    CURRENTLOGFRAME = $('adminDiv').firstChild.id;
     }});
}
function showFrameContent(frameName){
     if (!($(CURRENTLOGFRAME) === null))$(CURRENTLOGFRAME).hide();
     $(frameName).show();
     CURRENTLOGFRAME = frameName;
     YAHOO.example.container.uc_debug_win.show();					 YAHOO.example.container.uc_debug_win.focus();
}
//////////////////////////METHODE POUR LA TRADUCTION
createAlternateAdminWindow = function(){
     YAHOO.example.container.uc_debug_win = new YAHOO.widget.Panel("uc_debug_win",
	  { width : "550px", height:getFrameHeight()-70+"px", xy:[300,30], modal:false,
	       visible : true, 
	       constraintoviewport : false,
	  draggable: true,underlay:"none", effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25} });
     
     YAHOO.example.container.uc_debug_win.setBody('<div id="adminDiv" style="overflow:auto;height:'+(getFrameHeight()-100)+'px;"></div>');
     YAHOO.example.container.uc_debug_win.render(document.body);
     YAHOO.example.container.manager.register(YAHOO.example.container.uc_debug_win);
}
function syncIncludeJSTest(test, after){
     if (eval("window."+test+"!=undefined")) { debug("Javascript file loaded");eval(after); }
     else setTimeout("syncIncludeJSTest(\""+test+"\", '"+after+"')",500);
}
function syncIncludeJS(url, test, after){
     if (eval("window."+test+"===undefined")) {
	  debug("Including javascriptFile: "+url+"...");
	  includeJS(url);
	  setTimeout("syncIncludeJSTest(\""+test+"\", '"+after+"')", 500);
     }else eval(after);
}
function includeJS(url) {
     var head = document.getElementsByTagName('head')[0];
     var script = document.createElement('script');
     script.setAttribute('src',url);
     script.setAttribute('type', 'text/javascript');
     head.appendChild(script);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////FONCTIONS POUR XINHA//////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////   

var xinha_editors=null; 
var xinha_editors2=null;
var xinha_init2=null;
var xinha_config=null;
var xinha_plugins=null;
var xinha_init = function(dest){
     xinha_editors2 = null;
     syncIncludeJS(_uc_absurl+"lib/ext_lib/Xinha/XinhaCore.js", "Xinha", "xinha_init2(\""+dest+"\")");
}
xinha_init2=xinha_init2?xinha_init2:function(dest){
     xinha_editors2=xinha_editors2?xinha_editors2:[dest];
     //xinha_plugins=xinha_plugins?xinha_plugins:["CharacterMap","ContextMenu","ListType","Stylist","Linker","SuperClean","TableOperations"];
     xinha_plugins=xinha_plugins?xinha_plugins:["CharacterMap","ContextMenu","DoubleClick","ListType","Stylist","TableOperations"];
     if(!Xinha.loadPlugins(xinha_plugins,xinha_init2)){
	  return;
     }
     xinha_config=xinha_config?xinha_config:new Xinha.Config();
     xinha_config.stripScripts = false;
     xinha_config.height='400px';
     xinha_config.pageStyleSheets=[_editor_url+"examples/full_example.css"];
     debug("Creation of editor: "+xinha_editors2);
     xinha_editors=Xinha.makeEditors(xinha_editors2,xinha_config,xinha_plugins);
     Xinha.startEditors(xinha_editors);
};

