function startUpload(v1){
 document.getElementById('f1_upload_process').style.visibility = 'visible';
 //document.getElementById('f1_upload_form').style.visibility = 'hidden';
 // document.upload_form.arg.value=z1;

 document.forms["upload_form"].submit();
 return true;
 }

// reload preview after reloading image list
function rpreview(a,b,c)
{
imagePreview();
}
                              
function stopUpload(success,url){
 var result = '';

 if (success == 1){
 result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
 if(document.getElementById('img_list')) {
  $("#img_list").load(url,"",rpreview);
  }
 }
 else {
 result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
 }
 document.getElementById('f1_upload_process').style.visibility = 'hidden';
 document.getElementById('f1_upload_msg').innerHTML = result ;
 document.getElementById('f1_upload_form').style.visibility = 'visible';      
 $('.MultiFile-remove').click();
 return true;   
 }

function delImage(url)
{
url += "/";
if(document.show_images_by_date) {
 i = document.show_images_by_date.from_days.selectedIndex;
 days = document.show_images_by_date.from_days[i].value; 
 url += "i:1::f:" + days; 
 }
 if(document.getElementById('edit_mce')) 
  url += "::e:1";
 if(document.getElementById('img_list')) {
  $("#img_list").load(url,"",rpreview);
  }
 }



function from_days_func(url)
{

i = document.show_images_by_date.from_days.selectedIndex;
days = document.show_images_by_date.from_days[i].value;
addon = "";
 if(document.getElementById('edit_mce')) addon = "::e:1";
if(document.upload_form) {
 document.upload_form.arg.value = "i:1::f:" + days + addon;
 }
if(document.getElementById('img_list')) {
  $("#img_list").load(url + "i:1::f:" + days + addon,"",rpreview);
  }
 }

// this is article view
// not sure it belongs here but bla bla
function view_article(url,id,hash)
{
hash = Math.random();
$("#article_view").load(url + "articles/view/index/" + id + "/1/" + hash);
}

function reditor(a,b,c)
{

tinyMCE.init({
  // General options
 mode : "textareas",
 theme : "advanced",
 plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking",
 // Theme options
 relative_urls: false,
 elements: 'nourlconvert',
remove_script_host : false,
 theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
 theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
 theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
 theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
 theme_advanced_toolbar_location : "top",
 theme_advanced_toolbar_align : "left",
 theme_advanced_statusbar_location : "bottom",
 theme_advanced_resizing : true

 // Example content CSS (should be your site CSS)
 // Drop lists for link/image/media/template dialogs
 // Replace values for the template plugin
 });



$("input[type=file].multi").MultiFile();
imagePreview();
}
var SAVED_HTML;

function edit_article(url,id,hash)
{
SAVED_HTML = $("#article_edit").html();
hash = Math.random();
$("#article_edit").load(url + "articles/view/edit/" + id + "/" + hash,'',reditor);

}

function new_article(url,id,hash)
{
SAVED_HTML = $("#article_edit").html();
hash = Math.random();
$("#article_edit").load(url + "articles/view/add/"  + hash,'',reditor);
}


//////////////////////////////////////////////////////////////////
//comments stuff
function delComment(id,url)
{
confirmed = window.confirm("Delete the comment?");
if(!confirmed) return;
if(document.getElementById('comm')) {
  $("#comments").load(url+id);
  }
 }


function preview_article()
{
document.forms.article_form.preview.value = 1;
document.forms.article_form.target="_blank";
document.forms.article_form.submit();
}
var ggo_there;
function save_done(a,b,c) 
{
window.location = ggo_there;
}

function save_article(go_there)
{
document.forms.article_form.preview.value = 0;
ggo_there = go_there;
document.forms.article_form.target= "save_output";
if($("#article_form").length != 0 ) {
 tinyMCE.triggerSave();
 tinyMCE.execCommand( 'mceRemoveControl', true, 'ModelField'); 
 url = $("#article_form").attr('action');
 data = $("#article_form").serialize();
 jQuery.post(url,data,save_done) ;
}
//document.forms.article_form.submit();
//alert("am salvat");
//window.location = go_there;
}

function close_article(go_there) 
{
if (tinyMCE.activeEditor.isDirty()) 
   if(!confirm("There is unsaved data in the editor.\nClose anyway ?.")) return false;
if(document.forms.article_form.isnew.value == "1") {
 if(!confirm("Do you want to keep this article")) {
   document.forms.article_form.isnew.value = "100";

   document.forms.article_form.preview.value = 0;
   document.forms.article_form.target= "save_output";
   document.forms.article_form.submit();
   window.location = go_there;
  return false;
  }
}
window.location = go_there;
}

function edit_user_text(url) 
{

user_id = $("#user_id").val();
SAVED_HTML = $("#user_text_edit").html();
ret = "/cpanel/index/users/" + Math.random();
$("#user_text_edit").load(url + "cpanel/index/user_page/" + user_id + "/" + Math.random() + ret,reditor);
}



function close_user_text(url)
{
$("#user_text_edit").html(SAVED_HTML);
}

function preview_user_text()
{
document.forms.article_form.preview.value = 1;
document.forms.article_form.target="_blank";
document.forms.article_form.submit();
}

function save_user_done(a,b,c)
{
close_user_text("aaa");
}

function save_user_text(go_there)
{
document.forms.article_form.preview.value = 0;
ggo_there = go_there;
document.forms.article_form.target= "save_output";
if($("#article_form").length != 0 ) {
 tinyMCE.triggerSave();
 tinyMCE.execCommand( 'mceRemoveControl', true, 'ModelField'); 
 url = $("#article_form").attr('action');
 data = $("#article_form").serialize();
 jQuery.post(url,data,save_user_done) ;
 }
}

function killSimpleUpload(url) 
{
$('#simple_upload').remove();
window.location = url;
}

function killCSP() 
{
$('#category_sponsor').remove();
}


function xlogin(e,url )
	{
    var evt = e || window.event;
    pageX = evt.clientX;
    pageY =  evt.clientY;
	$("body").append("<div id='simple_login'></div>");
	$("#simple_login")
	 .css("top",(pageY - 10) + "px")
	 .css("left",(pageX - 250) + "px")
	 .fadeIn("fast");
	                                                                                               
	  $("#simple_login").load(url);
 }

function do_login()
{
if($("input[name$='email']").val() == "" || $("input[name$='password']").val() == "") {
  $("#login_message").html("<b>Email and password are required</b>");
 } 
document.forms.login.submit();

}

function forgot_password(url)
{
 
 var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
 ema =  $("input[name$='email']").val();

if(ema.search(emailRegEx)  == -1 ) {
  $("#login_message").html("<b>Type in your VALID email first ... </b>");
 return;
 }

 var ne = "";
for(i=0;i<ema.length;i++) {
 ne += ema.charCodeAt(i).toString(16);
}
$("#login_message").load(url + "cpanel/newuser/fp/" + ne + "/" + Math.random());
}

function category_sponsor(e) {
    var evt = e || window.event;
    pageX = evt.clientX;
    pageY =  evt.clientY;
    url = $("#base_url").val();
    category_id = $("#category_id").val();	
	$("body").append("<div id='category_sponsor'></div>");
	$("#category_sponsor")
	 .css("top",(pageY - 10) + "px")
	 .css("left",(pageX - 10) + "px")
	 .fadeIn("fast");
     url += "cpanel/index/csponsor"; 
     $("#category_sponsor").load(url + "/" + category_id + "/" + Math.random());
 }
