

function ContributeSubmitAdmin(){
  ClearValidators();
  DisableEnableForm(document.contributeForm,true);
  var link = "0";
  var index = document.getElementById("subtype").selectedIndex;
  if(document.getElementById("subtype")[index].value == "link"){
      link = "1";
    }
  if(ValidateContribution(link)){
    var subcat = document.getElementById("subcategory")[index].value;
    var intsubcat = GetArticleType(subcat);
    var headline = convertText_TBtoDB(document.getElementById("tbHeadline").value);
    var description = convertText_TBtoDB(stripCRLF(document.getElementById("taDescription").value));
    var content = convertText_TBtoDB(document.getElementById("taContent").value);
    var url = document.getElementById("tbUrl").value;
    var indexpid = document.getElementById("user").selectedIndex;
    var pid = document.getElementById("user")[indexpid].value;
    var imagelink = document.getElementById("contentContainer").childNodes[0].src;
    var futuretime = document.getElementById("input11").value;
    if( imagelink.indexOf('ph.gif') > 0){ imagelink = ''; }
    imgpos = imagelink.indexOf('img/');
    imagelink = imagelink.substr(imgpos,imagelink.length);
    $("#VDtbSubmit").fadeIn("fast", function(){
    document.getElementById("VDtbSubmit").innerHTML = "Submitting...";
    $.post("lib/postartadmin.php", { pid: pid, link: link, subcat: intsubcat, headline: headline, description: description, content: content, url: url, imagelink: imagelink, futuretime: futuretime }, function(data){
        results = data.split("|");
        $("#VDtbSubmit").fadeIn("slow");
        if(results[1]=="good"){
          document.getElementById("VDtbSubmit").innerHTML = results[3];//"Submission OK, beginning social spray, rating.  You can sumbit more articles while this process completes.  No need to wait.";
          UpdateSideText('Submitted article id ' + results[2] + ': ' + headline + '<br><br>');
	   //ShowSprayLinks(results[2]);
          
	   //var rating = 10;
          //if(pid%2==0) { rating = 8; }
          //$.get("lib/calculations.php", { action: "rating", id: pid, article: results[2], rating: rating }, function(data){
          //  UpdateSideText('User id ' + pid + ' rated article id ' + results[2] + ' a ' + rating/2 + ' - completed.<br><br>');
          //});

          //$.post("lib/spray.php", { headline: headline, pid: results[2] }, function(data){
          //  resultsSpray = data.split("|");
          //  document.getElementById("VDtbSubmit").innerHTML = "process complete.";
          //  UpdateSideText('Social spray for article id ' + results[2] + ' completed.<br><br>');
          //});
          //location.replace('/new/');
        } else if(results[1]=="duplicate"){
          document.getElementById("VDtbSubmit").innerHTML = results[2];
        } else if(results[1]=="future"){
          document.getElementById("VDtbSubmit").innerHTML = "future posting scheduled.";
          //UpdateSideText('Submitted article id ' + results[2] + ' for future posting'<br><br>');
        } else {
          document.getElementById("VDtbSubmit").innerHTML = "Submission Failed";
        }
    });
  });
  }
  DisableEnableForm(document.contributeForm,false);
}
function ShowSprayLinks(id){
  mixx = "<a href='javascript:(window.open(\"http://www.mixx.com/submit?page_url=http://moviedemocracy.com/article/"+id+"/\"))()'>Mixx</a>&nbsp;";
  UpdateSideText("Social Spray Links<br>");
  UpdateSideText(mixx);
  UpdateSideText("<br><br>");
}
function UpdateSideText(t){
  sidetext = document.getElementById("adminsidetext").innerHTML;
  sidetext = sidetext + t;
  document.getElementById("adminsidetext").innerHTML = sidetext;
}
function BlogSubmit(){
  ok=true;
  ClearBlogValidators();
  myEditor.saveHTML(); 
  title = document.getElementById("tbBlogTitle").value;
  content = document.getElementById("taBlogContent").value;
  //content = convertText_TBtoDB(content);
  if(title==""){ document.getElementById("VDtbBlogTitle").innerHTML = "*enter a title."; ok=false; }
  if(content==""){ document.getElementById("VDtbBlogContent").innerHTML = "*enter your blog post."; ok=false; }
  if(ok==true){
    document.getElementById("VDtbBlogSubmit").innerHTML = "Submitting...";
    $("#VDtbBlogSubmit").fadeIn("fast", function(){
      document.getElementById("VDtbBlogSubmit").innerHTML = "Submitting...";
      $.get("lib/admin.php", { action: "addblog", id: "-1", title: title, content: content }, function(data){
          results = data.split("|");
          $("#VDtbBlogSubmit").fadeIn("slow");
          if(results[1]=="1"){
            document.getElementById("VDtbBlogSubmit").innerHTML = "Blog post submitted.";
          } else {
            document.getElementById("VDtbBlogSubmit").innerHTML = "There was a problem submitting this blog post.";
          }
      });
    });
  }
}
function ClearBlogValidators(){
  document.getElementById("VDtbBlogTitle").innerHTML = "";
  document.getElementById("VDtbBlogContent").innerHTML = "";
  document.getElementById("VDtbBlogSubmit").innerHTML = "";
}
function RSSSubmit(){
  if(ValidateRSS()==true){
    if(document.getElementById("checkbox1").checked==true){ cb1 = '1'; } else { cb1 = '0'; };
    if(document.getElementById("checkbox2").checked==true){ cb2 = '1'; } else { cb2 = '0'; };
    if(document.getElementById("checkbox3").checked==true){ cb3 = '1'; } else { cb3 = '0'; };
    if(document.getElementById("checkbox4").checked==true){ cb4 = '1'; } else { cb4 = '0'; };
    if(document.getElementById("checkbox5").checked==true){ cb5 = '1'; } else { cb5 = '0'; };
    url = document.getElementById("tbRss").value;
    name = document.getElementById("tbRssName").value;
    document.getElementById("VDtbrssSubmit").innerHTML = "Submitting...";
    $("#VDtbrssSubmit").fadeIn("fast", function(){
      document.getElementById("VDtbrssSubmit").innerHTML = "Submitting...";
      $.get("lib/admin.php", { action: "addrss", id: "0", name: name, url: url, cb1 : cb1, cb2 : cb2, cb3 : cb3, cb4 : cb4, cb5 : cb5 }, function(data){
          results = data.split("|");
          $("#VDtbrssSubmit").fadeIn("slow");
          if(results[1]=="1"){
            document.getElementById("VDtbrssSubmit").innerHTML = "RSS feed submitted.";
            insertOption("rss", name, url);
          } else {
            document.getElementById("VDtbrssSubmit").innerHTML = "There was a problem submitting this RSS feed.";
          }
      });
    });
  }
} 
function ValidateRSS(type){
  result = true;
  if(document.getElementById("hiddenrssURLOK").innerHTML == "0"){
      document.getElementById("VDrsstbUrl").innerHTML = " *Please enter a valid URL"; result = false;
  }
  if(!document.getElementById("tbRssName").value.length > 0){
    document.getElementById("VDtbRssName").innerHTML = " *Please enter a name"; result = false;
  }
  return(result);
}
function fillRSS(index){
  removeOptions("rss");
  $("#VDrss").fadeIn("fast", function(){
    document.getElementById("VDrss").innerHTML = "populating...";
    $.get("lib/admin.php", { action: "fillrss", id : '-1', index: index }, function(data){
      results = data.split("|");
      num = results[1];
      insertOption("rss", "Select...", "-1");
      for(i=0;i<num;i++){
        nameurl = results[i+2].split("^");
        insertOption("rss", nameurl[0], nameurl[1]);
      }
      document.getElementById("VDrss").innerHTML = "";
    });  
  });
}
function fillUser(){
  removeOptions("user");
  $("#VDrss").fadeIn("fast", function(){
    document.getElementById("VDuser").innerHTML = "populating...";
    $.get("lib/admin.php", { action: "filluser", id : '-1' }, function(data){
      results = data.split("|");
      num = results[1];
      insertColorOption("user", "ME", "-1", 0);
      for(i=0;i<num;i++){
        nameurl = results[i+2].split("^");
        insertColorOption("user", nameurl[0], nameurl[1], nameurl[2]);
      }
      document.getElementById("VDuser").innerHTML = "";
    });  
  });
}
function fillUserLite(userid){
  removeOptions("user");
    $.get("lib/admin.php", { action: "filluser", id : userid }, function(data){
      results = data.split("|");
      num = results[1];
      insertColorOption("user", "ME", "-1", 0);
      for(i=0;i<num;i++){
        nameurl = results[i+2].split("^");
        insertColorOption("user", nameurl[0], nameurl[1], nameurl[2]);
      }
      document.getElementById("VDuser").innerHTML = "";
    });  
}
function admin_ChangeUser()
{
  
}
function admin_ChangeRSS()
{
  removeOptions("article");
  rss = document.getElementById("rss");
  index = rss.selectedIndex;
  url = rss[index].value;
  $("#VDarticle").fadeIn("fast", function(){
    document.getElementById("VDarticle").innerHTML = "populating...";
    $.get("lib/admin.php", { action: "fillarticles", id : '-1', url: url }, function(data){
      results = data.split("|||%|||");
      num = results[1];
      if(num == 'bad'){
        document.getElementById("VDarticle").innerHTML = "error";
      } else {
        for(i=0;i<num;i++){
          article = results[i+2].split("!(#");
          insertOption("article", article[1], article[0] + '!(#' + article[2] + '!(#');
        }
        document.getElementById("VDarticle").innerHTML = "";
        admin_ChangeArticle();
      }
    });
  });
}
function admin_ChangeArticle()
{
  article = document.getElementById("article");
  index = article.selectedIndex;
  value = article[index].value;
  name = article[index].text;
  values = value.split('!(#');
  document.getElementById("tbUrl").value = values[0];
  document.getElementById("taDescription").value = values[1];
  document.getElementById("tbHeadline").value = name;
  contribute_Headline();
  contribute_Description();
  admin_contribute_Content();
  contribute_URL();
  rss_URL();
}
function rss_URL(){
  url = document.getElementById("tbRss").value;
  $("#VDtbrssUrl").fadeIn("fast", function(){
    document.getElementById("VDtbrssUrl").innerHTML = "Checking URL...";
    $.get("lib/calculations.php", { action: "checkurl", id: "0", url: url }, function(data){
        results = data.split("|");
        $("#VDtbrssUrl").fadeIn("slow");
        if(results[1]=="good"){
          document.getElementById("hiddenrssURLOK").innerHTML = "1";
          document.getElementById("VDtbrssUrl").innerHTML = "URL OK!";
        } else {
          document.getElementById("VDtbrssUrl").innerHTML = "URL not found.";
        }
    });
  });
}
function admin_contribute_Content(){
  characterCount("taContent",glob_content_lim,"spanContentCount");
  document.getElementById("exampleContent").innerHTML = convertText_TBtoDB(document.getElementById("taContent").value);
}
function admincontribute_SubCategory(){
  var index = document.getElementById("subcategory").selectedIndex;
  if(document.getElementById("subcategory")[index].value == "news"){
    document.getElementById("exampleType").className = "lhs_news";
    document.getElementById("exampleTypeText").innerHTML = "news";
    rss = 1;
  } else if(document.getElementById("subcategory")[index].value == "scoop"){
    document.getElementById("exampleType").className = "lhs_scoop";
    document.getElementById("exampleTypeText").innerHTML = "scoop";
    rss = 2;
  } else if(document.getElementById("subcategory")[index].value == "tabloid"){
    document.getElementById("exampleType").className = "lhs_theory";
    document.getElementById("exampleTypeText").innerHTML = "tabloid";
    rss = 4;
  } else if(document.getElementById("subcategory")[index].value == "opinion"){
    document.getElementById("exampleType").className = "lhs_opinion";
    document.getElementById("exampleTypeText").innerHTML = "opinion";
    rss = 3;
  } else if(document.getElementById("subcategory")[index].value == "review"){
    document.getElementById("exampleType").className = "lhs_review";
    document.getElementById("exampleTypeText").innerHTML = "review";
    rss = 5;
  }
  fillRSS(rss);
}
function insertOption(selectname, name, value)
{
  var elSel = document.getElementById(selectname);
  var elOptNew = document.createElement('option');
  elOptNew.text = name;
  elOptNew.value = value;
  try
  {
    elSel.add(elOptNew, null);
  }
  catch(ex)
  {
    elSel.add(elOptNew); // IE only
  }
}
function insertColorOption(selectname, name, value, colorbool){
  var elSel = document.getElementById(selectname);
  var elOptNew = document.createElement('option');
  elOptNew.text = name;
  elOptNew.value = value;
  if(colorbool==1){
    elOptNew.style.color = "deeppink";
  }
  try
  {
    elSel.add(elOptNew, null);
  }
  catch(ex)
  {
    elSel.add(elOptNew); // IE only
  }
}

function removeOptions(name)
{
  var elSel = document.getElementById(name);
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
      elSel.remove(i);
  }
}
