/**
 * Validate whether there is PDF conversion feature available on page.
 * @return boolean pdf_flag
 */
function pdf_flag() {
  var pdf_flag = true;
  if (ge('fid5') == null) {
    pdf_flag = false;
  }
  return pdf_flag;
}

//==============================================================
//== FUNCTIONS OF STATUS OR NUMBER CHECK
//==============================================================
/**
 * Get the number of uploading files on page.
 * @return int all_files_num
 */
function calcAllUploadFilesNum() {
  if (!pdf_flag()) {
    return;
  }
  var all_files_num = 0;
  var if_list_div = ge("upload_file_list");
  for (i = 0; i < if_list_div.childNodes.length; i++) {
    if (check_element(if_list_div.childNodes[i].id)) {
      var upload_iframe = if_list_div.childNodes[i];
      var m_fname = gfe(upload_iframe, "fname");
      if (m_fname.value && m_fname.value != '') {
        all_files_num++;
      }
    }
  }
  var ffFileNamesStr = ge("ff_file_names").value;
  if (ffFileNamesStr != '') {
    var nameArr = ffFileNamesStr.split(';');
    all_files_num += nameArr.length;
  }
  
  return all_files_num;
}
/**
 * Check how many file can be converted to PDF on page.
 * And do UI change according to the number.
 * @param boolean need_alert validate whether need to alert message
 * @return int convertable_file_num
 */
function checkPdfStatus(need_alert) {
  if (!pdf_flag()) {
    return;
  }
  var ext_arr = getSupportedFileTypes();
  var status = false;
  var convertable_file_num = 0;

  var if_list_div = ge("upload_file_list");
  for (i = 0; i < if_list_div.childNodes.length; i++) {
    if (check_element(if_list_div.childNodes[i].id)) {
      var upload_iframe = if_list_div.childNodes[i];
      var m_fname = gfe(upload_iframe, "fname");
      if (m_fname.value && m_fname.value != '') {
        var m_ext = (m_fname.value).substring((m_fname.value).lastIndexOf(".") + 1);
        m_ext = m_ext.toUpperCase();
        for (var j in ext_arr) {
          if (ext_arr[j] == m_ext) {
            status = true;
            convertable_file_num++;
            break;
          }
        }
      }
    }
  }
  //Bug Fix::7319::Anamika.gl::10/23/2008
  var ffPdfNum = checkFFPdfStatus();
  convertable_file_num += ffPdfNum;

  
  return convertable_file_num;
}
/**
 * Check how many forward file can be converted to PDF on page.
 * @return int convertable_file_num
 */
function checkFFPdfStatus() {
  var ffFileNamesStr = ge("ff_file_names").value;
  var nameArr = ffFileNamesStr.split(';');
  var ext_arr = getSupportedFileTypes();
  var convertable_file_num = 0;
  for (var i = 0; i < nameArr.length; i++) {
    var fname = nameArr[i];
    var ext = get_file_extension(fname);
    ext = ext.toUpperCase();
    for (var j in ext_arr) {
      if (ext_arr[j] == ext) {
        convertable_file_num++;
        break;
      }
    }
  }
  return convertable_file_num;
}

/**
 * input are string and a integer value 
 * @return string upto the words of supplied integer value
 */
function getSubstring(str,value){
 if (str.length>value)
 {
   str = str.substring(0,value) + "....";
 }
 return str;
}
//==============================================================
//== MAIN ENTRY OF PDF CONVERSION JS
//==============================================================
/**
 * Check all the uploading file on page, if it's extension in supported file type,
 * mark it as PDF conversion file, and add element in div 'list-of-files-topdf'
 * @param boolean need_mark verify whether need mark file as PDF conversion file
 * @param boolean from_iframe verify the entrance of this function is iframe or not,
 *                            if it's true, need to reset need_mark param.
 * @return void
 */
function gather_pdf_info(need_mark, from_iframe) {
  var fileNameLength = 50;
  if (!pdf_flag()) {
    return;
  }
  if (!ge("pdf-cell").hasChildNodes()) {
    rebuild_pdf_div();
  }
  if (from_iframe) {
    if (!ge('fid5').checked) {
      need_mark = false;
    }
    else {
      need_mark = true;
    }
  }
  remove_all_pdf_info();
  if (ge("fid5").checked == true) {
  var ext_arr = getSupportedFileTypes();
  // If file extension in supported file type, and param 'need_mark' is true,
  // mark it as PDF conversion file, and add one element in div 'list-of-files-topdf'
  if (null == UPLOAD_IFRAME_LIST) {
    // Single upload
    var fname = gfe(ge('file_upload_iframe'), "fname");
    if (fname.value && fname.value != '') {
      var ext = get_file_extension(fname.value);
      ext = ext.toUpperCase();
      var mark_of_already_added = false;
      for (var j in ext_arr) {
        if(ext_arr[j] == ext) {
          var fileName = getSubstring(get_file_name(fname.value),fileNameLength);
          add_element_for_pdf(fileName, ge('file_upload_iframe'), true);
          if (need_mark) {
            add_pdf_mark(ge('file_upload_iframe'));
          }
          mark_of_already_added = true;
          break;
        }
      }
      if (!mark_of_already_added) {
        var fileName = getSubstring(get_file_name(fname.value),fileNameLength);
        add_element_for_pdf_unsupported(fileName, ge('file_upload_iframe'));
      }
    }
  }
  else {
    // Multiple upload
    var upload_list = UPLOAD_IFRAME_LIST;
    for (i = 0; i < upload_list.length; i++) {
      var upload_iframe = upload_list[i];
      var fname = gfe(upload_iframe, "fname");
      if (fname.value && fname.value != '') {
        var ext = get_file_extension(fname.value);
        ext = ext.toUpperCase();
        var mark_of_already_added = false;
        for (var j in ext_arr) {
          if(ext_arr[j] == ext) {
            var fileName = getSubstring(get_file_name(fname.value),fileNameLength);
			add_element_for_pdf(fileName, upload_iframe, false);
            if (need_mark) {
              add_pdf_mark(upload_iframe);
            }
            mark_of_already_added = true;
            break;
          }
        }
        if (!mark_of_already_added) {
          var fileName = getSubstring(get_file_name(fname.value),fileNameLength);
          add_element_for_pdf_unsupported(fileName, upload_iframe);
        }
      }
    }
  }
  }
  if (ge("fid5").checked == true) {
    gather_ff_pdf_info(need_mark);
  }
  continue_to_pay();
}
/**
 * Check all the forwarding file on page, if it's extension in supported file type,
 * mark it as PDF conversion file, and add element in div 'list-of-files-topdf'
 * @param boolean need_mark verify whether need mark file as PDF conversion file
 * @return void
 */
function gather_ff_pdf_info(need_mark) {
  var ffFileNamesStr = ge("ff_file_names").value;
  var ffFileIdsStr = ge("ff_file_ids").value;
  if (ffFileNamesStr == '' || ffFileIdsStr == '') {
    return;
  }
  var nameArr = ffFileNamesStr.split(';');
  var idArr = ffFileIdsStr.split(';');
  
  var ext_arr = getSupportedFileTypes();
  for (var i = 0; i < nameArr.length; i++) {
    var fname = nameArr[i];
    var ext = get_file_extension(fname);
    ext = ext.toUpperCase();
    var mark_of_already_added = false;
    for (var j in ext_arr) {
      if (ext_arr[j] == ext) {
        add_element_for_pdf_ff(fname, idArr[i]);
        if (need_mark) {
          add_ff_pdf_mark(idArr[i]);
        }
        mark_of_already_added = true;
        break;
      }
    }
    if (!mark_of_already_added) {
      add_element_for_ff_pdf_unsupported(fname, idArr[i]);
    }
  }
}
//==============================================================
//== TOOL FUNCTIONS
//==============================================================
/**
 * Display alert message for no supported file for PDF selected.
 * @return void
 */
function alert_no_supported_file_msg() {
  var ext_arr = getSupportedFileTypes();
  var msg = "Please select at least one file that can be converted to PDF. Supported file types are: ";
  var alert_msg = "";
  for (var i in ext_arr) {
    if ((typeof ext_arr[i]) == 'string') {
      if (alert_msg == "") {
        alert_msg = msg + "." + ext_arr[i].toLowerCase();
      }
      else {
        alert_msg += ", ." + ext_arr[i].toLowerCase();
      }
    }
  }
  alert(alert_msg);
}

//Bug Fix::7319::Anamika.gl::10/23/2008
//==============================================================
//== TOOL FUNCTIONS
//==============================================================
/**
 * Display alert message for no supported file for PDF selected.
 * @return void
 */
function alert_no_supported_file_continue() {
  var ext_arr = getSupportedFileTypes();
  var msg = "You have chosen the 'Send as Adobe PDF' option but you have not selected any files for PDF conversion. Do you still want to send these files without conversion?\n\nNote: Supported file types are: ";
  var alert_msg = "";
  for (var i in ext_arr) {
    if ((typeof ext_arr[i]) == 'string') {
      if (alert_msg == "") {
        alert_msg = msg + "." + ext_arr[i].toLowerCase();
      }
      else {
        alert_msg += ", ." + ext_arr[i].toLowerCase();
      }
    }
  }


 var continue_with_upload= confirm(alert_msg);
 if (continue_with_upload == true)
 {
   return true;
 }
 else
 {
  return false;
 }


}
/**
 * Check the element is an valid iframe element or just some tags on page.
 * @return boolean result
 */
function check_element(id) {
  var result = false;
  if (id != undefined) {
    if (id == "file_upload_iframe") {
      result = true;
    }
    if (id.substring(0, 9) == "upload_if") {
      result = true;
    }
  }
  return result;
}
/**
 * Add PDF conversion mark for uploading file.
 * @reutrn void
 */
function add_pdf_mark(iframe) {
  var pdf_mark = gfe(iframe, "pdf_mark");
  pdf_mark.value = "true";
}
/**
 * Remove PDF conversion mark for uploading file.
 * @reutrn void
 */
function remove_pdf_mark(iframe) {
  var pdf_mark = gfe(iframe, "pdf_mark");
  if (pdf_mark != null) {
    pdf_mark.value = "false";
  }
}
/**
 * Add PDF conversion mark for forwarding file.
 * @reutrn void
 */
function add_ff_pdf_mark(ufid) {
  var ffPdfMark = ge("ff_pdf_mark");
  if ("" == ffPdfMark.value) {
    ffPdfMark.value = ufid;
  }
  else {
    ffPdfMark.value = ffPdfMark.value + ';' + ufid;
  }
}
/**
 * Remove PDF conversion mark for forwarding file.
 * @reutrn void
 */
function remove_ff_pdf_mark(ufid) {
  var ffPdfMark = ge("ff_pdf_mark");
  ffPdfMark.value = ffPdfMark.value.replace(ufid, "");
  ffPdfMark.value = ffPdfMark.value.replace(/^;+/, '');
  ffPdfMark.value = ffPdfMark.value.replace(/;+$/, '');
}
/**
 * Get the extension of uploading file name.
 * @reutrn file extension
 */
function get_file_extension(fname) {
  return fname.substring(fname.lastIndexOf(".") + 1);
}
/**
 * Get the file name of uploading file address.
 * @reutrn fileName file name
 */
function get_file_name(fileName) {
  if (fileName != "") {
    if (fileName.match(/^(\\\\|.:)/)) {
      var temp = new Array();
      temp = fileName.split("\\");
      var len = temp.length;
      fileName = temp[len-1];
    } else {
      temp = fileName.split("/");
      var len = temp.length;
      if(len>0)
        fileName = temp[len-1];
    }
  }  
  return fileName;
}
/**
 * Remove the PDF conversion mark value of specified element for file forwarding on page.
 * @return void
 */
function remove_all_ff_pdf_mark() {
  var ffPdfMark = ge("ff_pdf_mark");
  ffPdfMark.value = "";
}
/**
 * Remove all the child nodes of div 'list-of-files-topdf'.
 * And remove all the PDF conversion mark of uploading files, and forwarding files.
 * @return void
 */
function remove_all_pdf_info() {
  if (!pdf_flag()) {
    return;
  }
  var pdf_list = ge('list-of-files-topdf');
  while (pdf_list.hasChildNodes()) {
    pdf_list.removeChild(pdf_list.childNodes[0]);
  }
  remove_pdf_mark(ge('file_upload_iframe'));
  if (null != UPLOAD_IFRAME_LIST) {
    var upload_list = UPLOAD_IFRAME_LIST;
    for (i = 0; i < upload_list.length; i++) {
      var upload_iframe = upload_list[i];
      remove_pdf_mark(upload_iframe);
    }
  }
  remove_all_ff_pdf_mark();
}
/**
 * Validate whether all the checkbox in div 'list-of-files-topdf' has been unchecked,
 * if it's true uncheck checkbox 'fid5' and remove all the element in this div,
 * and hide div 'step-two-subdiv-pdf'
 * @return void
 */
function validateCheckStatus() {
  var allUnchecked = true;
  var topDiv = ge('list-of-files-topdf');
  var i = 0;
  while (topDiv.hasChildNodes()) {
    var subDiv = topDiv.childNodes[i];
    if (subDiv == null) {
      break;
    }
    var innerChkBox = subDiv.childNodes[0];
    if (innerChkBox.checked == true) {
      allUnchecked = false;
    }
    i++;
  }
  if (allUnchecked) {
    ge("fid5").checked = false;
    while (topDiv.hasChildNodes()) {
      topDiv.removeChild(topDiv.childNodes[0]);
    }
    ge("step-two-subdiv-pdf").style.display="none";
  }
}

//==============================================================
//== DIV ELEMENT OPERATION FUNCTIONS
//==============================================================
/**
 * Rebuild all the elements in PDF div, after remove them.
 * @return void
 */
function rebuild_pdf_div() {
  var pdf_cell = ge("pdf-cell");
  var pdf_div = document.createElement("div");
  pdf_div.id = "step-two-subdiv-pdf";
  pdf_div.style.display = "none";
  pdf_div.style.width = "257px";
  pdf_div.style.paddingLeft = "25px";
  pdf_div.style.paddingBottom = "10px";
  pdf_div.style.paddingTop = "10px";
  pdf_div.style.marginBottom = "0px";
  pdf_cell.appendChild(pdf_div);
  
  var setpdf = document.createElement("div");
  setpdf.id = "setpdf";
  setpdf.style.display = "inline";
  pdf_div.appendChild(setpdf);
  
  var font_div = document.createElement("div");
  font_div.style.width = "257px";
  //font_div.style.float = "left";
  font_div.style.paddingTop = "2px";
  font_div.innerText = "Select the file to convert:";
  setpdf.appendChild(font_div);
  
  var br = document.createElement("br");
  br.clear = "all";
  br.style.height = "5px";
  setpdf.appendChild(br);
  
  var list_div = document.createElement("div");
  list_div.id = "list-of-files-topdf";
  list_div.style.width = "257px";
  //list_div.style.float = "left";
  list_div.style.border = "2px solid #DEDEDE";
  list_div.style.backgroundColor = "white";
  list_div.style.marginTop = "2px";
  setpdf.appendChild(list_div);
}
/**
 * Add one element for forwarding file in div 'list-of-files-topdf'.
 * @param string fname file name
 * @param string ufid ufid of file
 * @return void
 */
function add_element_for_pdf_ff(fname, ufid) {
  var pdf_list = ge('list-of-files-topdf');
  var f_id = "f-" + ufid;
  var s_id = "s-" + ufid;
  var d_id = "d-" + ufid;
  var checkbox = document.createElement("input");
  checkbox.type = "checkbox";
  checkbox.id = f_id;
  checkbox.setAttribute("if_id", "if-" + ufid);
  checkbox.onclick = function() {
    var chkBox = ge(f_id);
    if (chkBox.checked) {
      add_ff_pdf_mark(ufid);
    }
    else {
      remove_ff_pdf_mark(ufid);
      validateCheckStatus();
    }
  };
  
  var span = document.createElement("span");
  span.id = s_id;
  var div = document.createElement("div");
  div.id = d_id;
  div.style.clear = "both";
  pdf_list.appendChild(div);
  ge(d_id).style.paddingTop = "5px";
  ge(d_id).style.paddingBottom = "5px";
  ge(d_id).style.paddingLeft = "5px";
  ge(d_id).style.paddingRight = "5px";
  ge(d_id).appendChild(checkbox);
  ge(f_id).checked = true;
  ge(d_id).appendChild(span);
  ge(s_id).style.paddingLeft = "5px";
  ge(s_id).style.fontWeight = "normal";
  if (is_ie) {
    ge(s_id).innerText = fname;
  }
  else {
    ge(s_id).textContent = fname;
  }
}
/**
 * Add one element for forwarding file unsupported
 * for PDF conversion in div 'list-of-files-topdf'.
 * @param string fname file name
 * @param string ufid ufid of file
 * @return void
 */
function add_element_for_ff_pdf_unsupported(fname, ufid) {
  var pdf_list = ge('list-of-files-topdf');
  var f_id = "f-" + ufid;
  var s_id = "s-" + ufid;
  var d_id = "d-" + ufid;
  var checkbox = document.createElement("input");
  checkbox.type = "checkbox";
  checkbox.id = f_id;
  checkbox.setAttribute("if_id", "if-" + ufid);
  checkbox.disabled = true;
  if (is_ie) {
    checkbox.style.styleFloat = "left";
  }
  else {
    checkbox.style.cssFloat = "left";
  }

  var span = document.createElement("span");
  span.id = s_id;
  var span_unsupported = document.createElement("span");
  span_unsupported.id = s_id + "_unsupported";
  var div = document.createElement("div");
  div.id = d_id;
  div.style.clear = "both";
  pdf_list.appendChild(div);
  ge(d_id).style.paddingTop = "5px";
  ge(d_id).style.paddingBottom = "10px";
  ge(d_id).style.paddingLeft = "5px";
  ge(d_id).style.paddingRight = "5px";
  ge(d_id).appendChild(checkbox);
  ge(f_id).checked = false;
  ge(d_id).appendChild(span);
  ge(s_id).style.paddingLeft = "5px";
  ge(s_id).style.fontWeight = "normal";
  if (is_ie) {
    ge(s_id).style.styleFloat = "left";
    ge(s_id).innerText = fname;
  }
  else {
    ge(s_id).style.cssFloat = "left";
    ge(s_id).textContent = fname;
  }
  ge(d_id).appendChild(span_unsupported);
  ge(span_unsupported.id).style.paddingRight = "25px";
  ge(span_unsupported.id).style.fontWeight = "normal";
  ge(span_unsupported.id).style.color = "gray";
  if (is_ie) {
    ge(span_unsupported.id).style.styleFloat = "right";
    ge(span_unsupported.id).innerText = "(unsupported)";
  }
  else {
    ge(span_unsupported.id).style.cssFloat = "right";
    ge(span_unsupported.id).textContent = "(unsupported)";
  }
}
/**
 * Add one element for uploading file in div 'list-of-files-topdf'.
 * @param string fname file name
 * @param string ufid ufid of file
 * @param boolean singleUpload validate whether it's single file uploading
 * @return void
 */
function add_element_for_pdf(fname, iframe, singleUpload) {
  var pdf_list = ge('list-of-files-topdf');
  var f_id = "f-" + iframe.id;
  var s_id = "s-" + iframe.id;
  var d_id = "d-" + iframe.id;
  var checkbox = document.createElement("input");
  checkbox.type = "checkbox";
  checkbox.id = f_id;
  checkbox.setAttribute("if_id", iframe.id);
  checkbox.onclick = function() {
    var pdf_list = ge('list-of-files-topdf');
    if (singleUpload) {
      var chkBox = ge(f_id);
      if (!chkBox.checked) {
        remove_pdf_mark(iframe);
        validateCheckStatus();
      }
      else {
        add_pdf_mark(iframe);
      }
    }
    else {
      var allUnchecked = true;
      var if_list_div = ge("upload_file_list");;
      for (i = 0; i < if_list_div.childNodes.length; i++) {
        if (check_element(if_list_div.childNodes[i].id)) {
          var ck_iframe = if_list_div.childNodes[i];
          var innerChkBox = ge("f-" + ck_iframe.id);
          if (ck_iframe.id == iframe.id) {
            if (!innerChkBox.checked) {
              remove_pdf_mark(ck_iframe);
        }
            else {
              add_pdf_mark(ck_iframe);
          }
        }
        if (innerChkBox.checked == true) {
          allUnchecked = false;
        }
        }
      }
      if (allUnchecked) {
        ge("fid5").checked = false;
        while (pdf_list.hasChildNodes()) {
          pdf_list.removeChild(pdf_list.childNodes[0]);
        }
        ge("step-two-subdiv-pdf").style.display="none";
      }
    }
   toggleSendContinueButton();
  };

  var span = document.createElement("span");
  span.id = s_id;
  var div = document.createElement("div");
  div.id = d_id;
  div.style.clear = "both";
  pdf_list.appendChild(div);
  ge(d_id).style.paddingTop = "5px";
  ge(d_id).style.paddingBottom = "5px";
  ge(d_id).style.paddingLeft = "5px";
  ge(d_id).style.paddingRight = "5px";
  ge(d_id).appendChild(checkbox);
  ge(f_id).checked = true;
  ge(d_id).appendChild(span);
  ge(s_id).style.paddingLeft = "5px";
  ge(s_id).style.fontWeight = "normal";
  if (is_ie) {
    ge(s_id).innerText = fname;
  }
  else {
    ge(s_id).textContent = fname;
  }
}

/**
 * Add one element for uploading file unsupported
 * for PDF conversion in div 'list-of-files-topdf'.
 * @param string fname file name
 * @param string ufid ufid of file
 * @param boolean singleUpload validate whether it's single file uploading
 * @return void
 */
function add_element_for_pdf_unsupported(fname, iframe) {
  var pdf_list = ge('list-of-files-topdf');
  var f_id = "f-" + iframe.id;
  var s_id = "s-" + iframe.id;
  var d_id = "d-" + iframe.id;
  var checkbox = document.createElement("input");
  checkbox.type = "checkbox";
  checkbox.id = f_id;
  checkbox.setAttribute("if_id", iframe.id);
  checkbox.disabled = true;
  if (is_ie) {
    checkbox.style.styleFloat = "left";
  }
  else {
    checkbox.style.cssFloat = "left";
  }

  var span = document.createElement("span");
  span.id = s_id;
  var span_unsupported = document.createElement("span");
  span_unsupported.id = s_id + "_unsupported";
  var div = document.createElement("div");
  div.id = d_id;
  div.style.clear = "both";
  pdf_list.appendChild(div);
  ge(d_id).style.paddingTop = "5px";
  ge(d_id).style.paddingBottom = "10px";
  ge(d_id).style.paddingLeft = "5px";
  ge(d_id).style.paddingRight = "5px";
  ge(d_id).appendChild(checkbox);
  ge(f_id).checked = false;
  ge(d_id).appendChild(span);
  ge(s_id).style.paddingLeft = "5px";
  ge(s_id).style.fontWeight = "normal";
  if (is_ie) {
    ge(s_id).style.styleFloat = "left";
    ge(s_id).innerText = fname;
  }
  else {
    ge(s_id).style.cssFloat = "left";
    ge(s_id).textContent = fname;
  }
  ge(d_id).appendChild(span_unsupported);
  ge(span_unsupported.id).style.paddingRight = "25px";
  ge(span_unsupported.id).style.fontWeight = "normal";
  ge(span_unsupported.id).style.color = "gray";
  if (is_ie) {
    ge(span_unsupported.id).style.styleFloat = "right";
    ge(span_unsupported.id).innerText = "(unsupported)";
  }
  else {
    ge(span_unsupported.id).style.cssFloat = "right";
    ge(span_unsupported.id).textContent = "(unsupported)";
  }
}

