﻿/*
 * Utility routines
*/
function writeInfo() {
      alert("Đang chuyển files lên Server. Xin vui lòng chờ ... ");
      return (true);
}

function writeDescription() {

      var object = document.getElementById('testit');
      var desc = document.upload.description.value;

      var lf1 = /\n/g; // \n\r or \r\n for IE and \n for Mozilla
/*
      var nl = /\n/;
      var index1 = desc.search(nl);
      alert(index1);
      var lf  = /\f/;
      var index2 = desc.search(lf);
      alert(index2);
      var lr  = /\r/;
      var index3 = desc.search(lr);
      alert(index3);
*/
      var desc2 = desc.replace( lf1, "<br />");
      var tbl = '<table width=720 border=1><tr><td bgcolor=gray>';
      var endtbl = '</td></tr>'; 
      object.innerHTML = '<div id="testit"><img onClick="writeDescription()" src="../images/test_btn.gif" width="48" height="48" alt="Push to test"></div>'
                                         + '<br><strong>Nháp:</strong><br><br>' + tbl + desc2 + endtbl;
}

function checkform ( form, ftype) {
       var result;
       var first;
       var lastSubs;
       var maxlen;

       if (form.title.value.length >= 80) {
           alert( "Không thể đặt tên phim quá 80 chữ." );
           form.title.focus();
           return false ;
       }
       if (form.title.value.length == 0) {
           alert( "Xin vui lòng cho tựa." );
           form.title.focus();
           return false ;
       }
       first = true;
       for (var i = 0; i  < form.length; i++) {
           var e = form.elements[i];
           if (e.type == "file") {
               if (!e.value) { 
                   if (ftype > 20 && ftype < 30) {
                       alert("Không thể xót tên hồ sơ của radio.");  
                       e.focus();
                       return false;
                   }
                   if (!first && ftype > 30 && ftype < 40) {
                       alert("Không thể xót tên hồ sơ của phim.");  
                       e.focus();
                       return false;
                   }
                   first = false;
                   continue;
               }
               result = e.value.split("\\");     
               if (result.length <= 1) {
                    result = e.value.split("/");
               }
               lastSubs = result.length - 1;
               if (result[ lastSubs].length > 30) {
                    alert("Tên một file không quá 30 chữ.");
                    e.focus();
                    return false;
               }
               first = false;
           }  // file
       } // for

       if (form.keyword.value.length >= 256) {
           alert( "Không thể tìm một phim quá 256 chữ." );
           form.keyword.focus();
           return false ;
       }
       if (form.description.value.length >= 256) {
           alert( 'Không thể mô tả phim/radio/truyện/tin quá 256 chữ.' );
           form.description.focus();
           return false ;
       }
       writeInfo();
       return true ;
}

function color_sel(c) {
       
       if (document.selection) {
               var str = document.selection.createRange().text;
               document.upload.description.focus();
               var sel = document.selection.createRange();
               sel.text = "<font color=\"" + c + "\">" + str + "</font>";
       } else if (document.upload.description.selectionStart || document.upload.description.selectionStart == '0') {
               var startPos = document.upload.description.selectionStart;
               var endPos = document.upload.description.selectionEnd;
               var str = document.upload.description.value.substring( startPos, endPos);
               document.upload.description.focus();
               document.upload.description.value = document.upload.description.value.substring( 0, startPos) +
                        "<font color=\"" + c + "\">" + str + "</font>" + document.upload.description.value.substring( endPos, document.upload.description.value.length);
       } else {
               alert ('This browser does not support selection feature !!! Try IE or Mozilla');
       }
       return;
}

function format_sel(v) {

       if (document.selection) {
               var str = document.selection.createRange().text;
               document.upload.description.focus();
               var sel = document.selection.createRange();
               sel.text = "<" + v + ">" + str + "</" + v + ">";
       } else if (document.upload.description.selectionStart || document.upload.description.selectionStart == '0') {
               var startPos = document.upload.description.selectionStart;
               var endPos = document.upload.description.selectionEnd;
               var str = document.upload.description.value.substring( startPos, endPos);
               document.upload.description.focus();
               document.upload.description.value = document.upload.description.value.substring( 0, startPos) +
                        "<" + v + ">" + str + "</" + v + ">" + document.upload.description.value.substring( endPos, document.upload.description.value.length);
       } else {
               alert ('This browser does not support selection feature !!! Try IE or Mozilla');
       }
       return;
}

function insert_link() {

       if (document.selection) {
               var str = document.selection.createRange().text;
               document.upload.description.focus();
               var my_link = prompt("Cho URL:","http://");
               if (my_link != null) {
                        var sel = document.selection.createRange();
	      sel.text = "<a href=\"" + my_link + "\">" + str + "</a>";
               }
       } else if (document.upload.description.selectionStart || document.upload.description.selectionStart == '0') {
               var startPos = document.upload.description.selectionStart;
               var endPos = document.upload.description.selectionEnd;
               var str = document.upload.description.value.substring( startPos, endPos);
               document.upload.description.focus();
               var my_link = prompt("Cho URL:","http://");
               if (my_link != null) {
                        document.upload.description.value = document.upload.description.value.substring( 0, startPos) +
                                  "<a href=\"" + my_link + "\">" + str + "</a>" + document.upload.description.value.substring( endPos, document.upload.description.value.length);
               }
       } else {
               alert ('This browser does not support selection feature !!! Try IE or Mozilla');
       }
       return;
}
       
function insert_img() {

       if (document.selection) {
               var str = document.selection.createRange().text;
               document.upload.description.focus();
               var my_img = prompt("Tên hồ sơ của hình:","");
               if (my_img != null) {
                        var sel = document.selection.createRange();
	      sel.text = "<img src=\"" + my_img + "\" hspace=\"5\" vspace=\"5\" align=\"left\" width=\"150\" height=\"150\" alt=\"" + my_img + "\">" + str + "</img>";
               }
       } else if (document.upload.description.selectionStart || document.upload.description.selectionStart == '0') {
               var startPos = document.upload.description.selectionStart;
               var endPos = document.upload.description.selectionEnd;
               var str = document.upload.description.value.substring( startPos, endPos);
               document.upload.description.focus();
               var my_img = prompt("Tên hồ sơ của hình:","");
               if (my_img != null) {
                        document.upload.description.value = document.upload.description.value.substring( 0, startPos) +
                                  "<img src=\"" + my_img + "\" hspace=\"5\" vspace=\"5\" align=\"left\" width=\"150\" height=\"150\" alt=\"" + my_img + "\">" + str + "</img>" + 
                                        document.upload.description.value.substring( endPos, document.upload.description.value.length);
               }
       } else {
               alert ('This browser does not support selection feature !!! Try IE or Mozilla');
       }
       return;
}

function mouseover(el) {
  el.className = "raised";
}

function mouseout(el) {
  el.className = "button";
}

function mousedown(el) {
  el.className = "pressed";
}

function mouseup(el) {
  el.className = "raised";
}

