// trim a string


	  function create_account() {
						 if (!$('email').value) {
							alert('{- "Please enter your email, it will be used as your account name."|translate|escape:"javascript" -}');
							return;
						 }
						 if (!$('pass').value) {
							alert('{- "Please enter your password"|translate|escape:"javascript" -}');
							return;
						 }
						 if ($('pass').value != $('pass2').value) {
							alert('{- "The two passwords don\'t match"|translate|escape:"javascript" -}');
							return;
						 }
						 if ($('email').value && $('pass').value && ($('pass').value == $('pass2').value)) {
							$('formy').submit();
							return;
						 }
					  }






function trim(str) {
  return str.replace(/^\s*|\s*$/g,"");
}

// escape quotes in strings
function escape_quotes(str) {
  return str.replace(/\'/g,"\\'").replace(/\"/g,"\\\"");
}

// escapes only double-quotes in strings
function escape_double_quotes(str) {
  return str.replace(/\"/g,"\\\"");
}

// similar to explode in PHP
function explode(sep, str) {
  return str.split(sep);
}

// similar to implode in php
function implode(sep, arr) {
  var my_sep = '';
  var ret = '';
  for (var i = 0; i < arr.length; i++) {
    ret += my_sep + arr[i];
    my_sep = sep;
  }
  return ret;
}

// similar to in_array in PHP
function in_array(which, arr) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == which) {
      return true;
    }
  }
  return false;
}

function msleep(numberMillis) {
   var now = new Date();
   var exitTime = now.getTime() + numberMillis;
   while (true) {
      now = new Date();
      if (now.getTime() > exitTime)
          return;
   }
}

// open a popup
function open_popup(url, width, height, name) {
  if (width) {
    width_clause = 'width=' + width + ',';
  } else {
    width_clause = '';
  }
  if (height) {
    height_clause = 'height=' + height + ',';
  } else {
    height_clause = '';
  }
  if (!name || !name.length) {
    name = '_blank';
  }

  wnd = window.open(url, name, width_clause + height_clause + 'scrollbars=no, status=no, resizable=no');
  wnd.blur();
  wnd.focus();
}

//create a popup for bookmarks with scrollbars and resizable
function open_popup_bookmark(url, width, height, name) {
  if (width) {
    width_clause = 'width=' + width + ',';
  } else {
    width_clause = '';
  }
  if (height) {
    height_clause = 'height=' + height + ',';
  } else {
    height_clause = '';
  }
  if (!name || !name.length) {
    name = '_blank';
  }

  wnd = window.open(url, name, width_clause + height_clause + 'scrollbars=yes, status=no, resizable=yes');
  wnd.focus();
}

//create a popup for media with scrollbars and resizable
function open_popup_media(url, width, height, name) {
  if (width) {
    width_clause = 'width=' + width + ',';
  } else {
    width_clause = '';
  }
  if (height) {
    height_clause = 'height=' + height + ',';
  } else {
    height_clause = '';
  }
  if (!name || !name.length) {
    name = '_blank';
  }

  //msleep(5000);
  wnd = window.open(url, name, width_clause + height_clause + 'scrollbars=yes, status=no, resizable=yes');
  wnd.blur();
  window.focus();
}

// add the entitify method to the String object
Function.prototype.method = function (name, func) {
  this.prototype[name] = func;
  return this;
};

String.
method('entitify', function () {
    return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}).
method('quote', function (noquotes) {
        var c, i, l = this.length, o = '';
        for (i = 0; i < l; i += 1) {
            c = this.charAt(i);
            if (c >= ' ') {
                if (c == '\\' || c == '"') {
                    o += '\\';
                }
                o += c;
            } else {
                switch (c) {
                case '\b':
                    o += '\\b';
                    break;
                case '\f':
                    o += '\\f';
                    break;
                case '\n':
                    o += '\\n';
                    break;
                case '\r':
                    o += '\\r';
                    break;
                case '\t':
                    o += '\\t';
                    break;
                default:
                    c = c.charCodeAt();
                    o += '\\u00' + Math.floor(c / 16).toString(16) +
                        (c % 16).toString(16);
                }
            }
        }
        if (noquotes) {
          return o;
        } else {
          return '"' + o + '"';
        }
    });
    
    
    
    
// include another js script
function include(script_filename) {
    document.write('<' + 'script');
    document.write(' language="javascript"');
    document.write(' type="text/javascript"');
    document.write(' src="' + script_filename + '">');
    document.write('</' + 'script' + '>');
};

// include another js script, DOM way
function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

// check enter in a text field / text area
// call with onKeyPress="checkEnter(event, 'action')", where
// action is the javascript code to execute
function checkEnter(e, action){ //e is event object passed from function invocation
  var characterCode; // literal character code will be stored in this variable

  if (e && e.which) { //if which property of event object is supported (NN4)
    e = e;
    characterCode = e.which; //character code is contained in NN4's which property
  } else {

    e = event;
    characterCode = e.keyCode; //character code is contained in IE's keyCode property
  }

  if (characterCode == 13){ // if generated character code is equal to ascii 13 (if enter key)
    // document.msg.m.value = document.msg.m.value.substr(0, document.msg.m.value.length - 1);
    noClose = true;
    eval(action);
    e.returnValue = false;   // for IE, no enter gets entered in the textarea
    return false;            // for NN, no enter gets entered in the textarea
  } else {
    return true;
  }
}

// URL functions
function cleanURL(url) {
  // eliminate spaces and replace them with "-"
  url = url.replace(/\s/g, '-');
  // eliminate slashes and replace them with "-"
  url = url.replace(/\//g, '-');
  // french characters
  url = eliminateAccentedChars(url);
  // eliminate all chars that are not allowed
  url = url.replace(/[^\x21-\x7E]/g, '');
  url = url.replace(/[\x3F\x26]/g, '');
  // lower case
  url = url.toLowerCase();
  return url;
}

function eliminateAccentedChars(text) {
  // see CVS 1.7 for var chars string of real cahrs.
  var chars = "SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy";
  var replaces = "SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy";
  result = "";
  for (var j = 0; j < text.length; j++) {
    replacement = text.charAt(j)
    for (var i = 0; i < chars.length; i++) {
      if (chars.charAt(i) == replacement) {
        replacement = replaces.charAt(i);
        break;
      }
    }
    result = result + replacement;
  }
  return result;
}

// ajax functions
include('/js/prototype/prototype.js');
include('/js/loginpanel.js');
include('/js/forms.js');

function __o(objectName) { return document.getElementById(objectName); } //get element by ID

// function that retrieves the value of a radio control
// the radio-controls must have the ids in the format:
// <name>_0, <name>_1, <name>_2 ...
function radioValue(name) {
  var i = 0;
  var obj;
  while(obj = __o(name + '_' + i)) {
    if (obj.checked) {
      return obj.value;
    }
  };
}

function cloneObject(what) {
    for (i in what) {
        this[i] = what[i];
    }
}

__store_updater = null;
function __store_upload(ctrl) {

	Element.hide($('submitbuttondiv'));
	Element.show($('progresscontainer'));
  new Ajax.Request('upload_progress.php?start=1');
  // new Ajax.Updater('progresscontainer', 'upload_progress.php?start=1');
  // start upload
  ctrl.form.submit();
  // start periodical update upload info
	__store_updater = new Ajax.PeriodicalUpdater({},'/upload_progress.php',{'decay': 1,'frequency' : 1,'method': 'post','onSuccess' : function(request){__store_updateProgress($('progresscounter'), $('progressspeed'), $('progressfile'), $('progresstime'), request)},'onFailure':function(request){__store_updateFailure($('progresscounter'),request)}})
}

var uploads_in_progress = 0;
var sids = {};
         
function __store_upload_perl(ctrl, sid) {

	Element.hide($('submitbuttondiv'));
	Element.show($('progresscontainer'));
	Element.show($('progressbarcontainer'));
	
	ctrl.form.submit();

	//alert(sid);
	// sids[ul.name] = sid;
	//uploads_in_progress = uploads_in_progress + 1;
	//upimage = new Image();
	//$('progresscompleted').style.background="url(/images/bgr-progresscompleted.gif)";


	// var pb = $(ul.name + "_progress");
	new Ajax.PeriodicalUpdater({},'/upload_progress.php',{'decay': 1,'frequency' : 2,'method': 'post','parameters': 'sid=' + sid,'onSuccess' : function(request){__store_updateProgress($('progresscounter'), $('progressspeed'), $('progressfile'), $('progresstime'), request)},'onFailure':function(request){__store_updateFailure($('progresscounter'),request)}})
}
function __store_updateProgress(pb ,ps, fn ,pt, req) {
  if (req.responseText != '-1') {
  	var mes = req.responseText;
	//alert(mes);
    if (mes.substring(0, 4) == 'perl') {
      // special handling
      
      var data = mes.substring(5).split(' ');
      if (data[0] == 'done') {
        $('progresscompleted').style.width = '100%';
        window.focus();
        __store_endUpload(data[1], data[2]);
        return;
      }
      var current_size = data[0];
      var total_size = data[1];
      var current_size_pretty = data[2];
      var total_size_pretty = data[3];
      var up_speed_pretty = data[4];
      var up_time_pretty = data[5];
      if (current_size && total_size) 
	  {
	   	     if (((current_size * 100 / total_size) < 100) && ((current_size * 100 / total_size) > 99))
			    { pb.innerHTML = 'Your file will be processed after the upload is complete, please DO NOT CLOSE this window. The processing may be take upto 90 seconds';
				
				}
			 else{
			      pb.innerHTML = current_size_pretty + " / " + total_size_pretty;
			      ps.innerHTML = up_speed_pretty + "/s";
                  	      pt.innerHTML = up_time_pretty + " ";
			      $('progresscompleted').style.width = (current_size * 100 / total_size) + "%";
                 }
      } else {
				if (mes.indexOf('400 Bad Request')>0) return; // dummy workaround
				if (mes.indexOf('0B 0B 0B')>0) return; // dummy workaround
    	        pb.innerHTML = mes;
             }
  }
}

}

function __store_updateFailure(pb,req) {
	var mes = req.responseText;
	pb.innerHTML = mes;
/*
	alert(mes);
	uploads_in_progress = uploads_in_progress - 1;
*/
}

function __store_endUpload(fileid, pass) {
  if (__store_updater) {
    __store_updater.stop();
  }
  Element.hide($('progresscontainer'));
  Element.show($('uploadcompleted'));
  document.location = '/index.php?f=' + fileid + '&pass=' + pass;
}
/*

hidding the table 'upload_form_container' and showing the uploading bar with adds
and testing for the browser compatibility
cos there were problem in Safari
BEGIN*/
var upload_uploading=0;
function upload_is_safari(){
if(navigator.userAgent.indexOf("Safari")!=-1)
{
	return 1;
}
return 0;
}

function upload_is_moz_one(){
if(navigator.userAgent.indexOf("Mozilla/5.0")!=-1&&navigator.userAgent.indexOf("rv:1.0")!=-1)
{
return 1;
}
return 0;
}

function upload_click_button()
{
if(upload_is_safari()){
	upload_hide_form();
   
	document.getElementById("upload_form").submit();
}
upload_uploading=1;
return true;
}

function upload_submit_form(){
   if(!upload_is_safari()&&!upload_is_moz_one()) {
      upload_hide_form();
   }
   
   upload_uploading=1;
   
   //theForm = document.forms['upload_form'];
   //theText = theForm['numupfiles'].value;
 
   //theForm.action =  theForm.action + "&numupfiles=" + theText;
   return true;
}
/*FUNCTION FOR CHANGING THE DIVS VISIBILITY */
function upload_hide_form()
{
   document.getElementById("upload_form_container").style.display="none";
   document.getElementById("upload_uploading_container").style.display="block";
}


/*

hidding the table 'contentRight' and showing the uploading bar with adds
and testing for the browser compatibility
cos there were problems in Safari
BEGIN*/

function upload_click_button_acc(){
if(upload_is_safari())
{
upload_hide_form_acc();
document.getElementById("formx").submit();
}
upload_uploading=1;
return true;}

function upload_submit_form_acc(){
if(!upload_is_safari()&&!upload_is_moz_one())
{
upload_hide_form_acc();

}
upload_uploading=1;
return true;
}
/*FUNCTION FOR CHANGING THE DIVS VISIBILITY */
function upload_hide_form_acc(){
document.getElementById("contentRight").style.display="none";
document.getElementById("contentUploadBar").style.display="block";
}
/*END edit by CRistina 31.07.2006 */

/*Gads edit by MAurice */
function submitGad(url, frm, ifrm)
{
	var adfrm = document.getElementById(frm);
	var adifrm = document.getElementById(ifrm);
	
	if(!(adfrm == null && adifrm == null))
	{
		adfrm.submit();
		adifrm.src=url;
	}
}
