function openWindow(url,title,options){
	w=window.open(url,title,options);
	w.focus();
}
function updateWindow(id, url) {
	window.setTimeout("handleRequest('"+id+"','"+url+"')", 200);	
}
function ajaxUpdateAsync(id, url){ 
   new Ajax.Updater(id,url,{asynchronous:true, evalScripts:true});
}

function ajaxUpdateSync(id, url){ 
   new Ajax.Updater(id,url,{asynchronous:false, evalScripts:true});
}

function closePopup(){
   Popup.close();
}

function httpRequest(url,type,options){
  if(type==null){
    type='GET';
  }
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  } 
  if (req) {
    // Synchronous request, wait till we have it all
    req.open(type, url, false);
	if(type=='POST' && options !=null){
	  req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	}
    req.send(options);
    return req.responseText;
  } else {
    return "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
  }
}

function handleRequest(id,url){
	 var element = document.getElementById(id);
	 if (!element) {	  
	   loff();
	   return;
	 }
	element.innerHTML = httpRequest(url);
	loff(); 
}

function lon(){	
	
   try {
	
		var o_set = document.getElementById('loaderContainerWH');
		var o_getH = document.getElementsByTagName('body')[0];		

		o_set.style.height = o_getH.scrollHeight;
	
		document.getElementById("loaderContainer").style.display = "";
	} catch (e) {
		
	}	
}

function loff(){
	try {
	
	  document.getElementById("loaderContainer").style.display = "none";	
		
	} catch (e) {
	}
}

function bringLocaleToForeground(select,id){	
	var localeOptions=select.childNodes;
	for(var i=0;i<localeOptions.length;i++){
		if(localeOptions[i].nodeType!=3){		  
		  var l=localeOptions[i].getAttribute('value');		  
		  $(document.getElementsByName('layer_'+l)[0]).setStyle({visibility: 'hidden'});
		}
	}
	$(document.getElementsByName('layer_'+locale)[0]).setStyle({visibility: 'visible'});
}

function translate(elementId,containsTextString){
    var localeSelect=document.getElementsByName('locale')[0];
    var translationSelect=document.getElementsByName('langpair')[0];	
	var dest=document.getElementsByName('text_'+translationSelect.value)[0];
	var sourceEditor=FCKeditorAPI.GetInstance('text_'+localeSelect.value);
	var destEditor=FCKeditorAPI.GetInstance('text_'+translationSelect.value);	
	
	if(destEditor && (destEditor.GetXHTML(true).toString()=='' || confirm(containsTextString))){
	   var options='fromLocale='+localeSelect.value+'&toLocale='+translationSelect.value+'&text='+escape(sourceEditor.GetXHTML(true));		
	   var text=httpRequest('/'+elementId+'/translateText/'+elementId+'/setSettings', 'POST',options);
	   destEditor.SetHTML(text);
	   localeSelect.value=translationSelect.value;
	   bringLocaleToForeground(localeSelect.value);
	   setTranslations(localeSelect)
	}		
}

function setTranslations(localeSelect){
	var localeOptions=localeSelect.childNodes;
	var translationSelect=document.getElementsByName('langpair')[0];
			
	while(translationSelect.childNodes.length!=0){
	   translationSelect.removeChild(translationSelect.lastChild);
	}	
	for(var i=0;i<localeOptions.length;i++){
		if(localeOptions[i].nodeType!=3){
		  var locale=localeOptions[i].getAttribute('value');
		  if(locale!=localeSelect.value){
		    var option=document.createElement('option');
		    option.setAttribute('value',locale);
		    option.appendChild(document.createTextNode(localeSelect.value+' -> '+locale));
		    translationSelect.appendChild(option);
		  }
		}
	}
}

function update(transport){
   new Ajax.Updater('e'+transport.getResponseText,'/_call/'+transport.responseText+'/getElementHtml',{asynchronous: true, evalScripts: true});
}

function success(transport){
	if(transport.status == 200){
	   update(transport);
	   observe()
	}
}

function failure(transport){
   if(transport.status == 500){
      observe();
   }
}

function observe(){
    new Ajax.Request("/observe",{onSuccess: success, onFailure: failure, asynchronous: true, evalScripts:true});
}

function fileBrowser (field_name, url, type, win) {  
    tinyMCE.activeEditor.windowManager.open({
        file : '/_call/texteditor/_file_admin_html?field_name='+field_name+'&url='+url+"&type="+type,
        title : 'My File Browser',
        width : 1024,  // Your dimensions may differ - toy around with them!
        height : 600,
        resizable : "yes",
        inline : "yes",  // This parameter only has an effect if you use the inlinepopups plugin!
        close_previous : "no"
    }, {
        window : win,
        input : field_name
    });
    return false;
 }
 
 var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}









