/*************************************************************************
 * BSC GEMINI IBS JavaScript Library 3.2.2.0                             *
 *************************************************************************/
function PopWindow( ImageURL, Parameters )
{
	PopUp = window.open( ImageURL, "PopUp", Parameters );
}

function st(statusObject){
  if(statusObject==null){
    status = "";
  }
  else{
    status = statusObject.alt;
  }
  return true;
}

function convertFormValues(form){
    for (zz = 0; zz < form.elements.length; zz++) {
        var elementType = form.elements[zz].type;
        if (elementType == "text" || elementType == "textarea"){
          var elementName = form.elements[zz].name;
          var hiddenElementName = elementName.substr(0, elementName.length-3);
          if(form.elements[hiddenElementName]!=null){
            form.elements[hiddenElementName].value = toCharacterReferences(form.elements[zz].value);
          }
        }
        else if (elementType=="select-one"){
          var selectElement = form.elements[zz];
          var optionElement = selectElement.options[selectElement.selectedIndex];
          optionElement.value = toCharacterReferences(optionElement.value);
        }
        else if (elementType == "submit" || elementType == "button"){
          // do nothing
        }
        else if (elementType!="hidden"){
          form.elements[zz].value = toCharacterReferences(form.elements[zz].value);
        }
    }
}

function disableLinks(){
    for (zzz = 0; zzz < document.links.length; zzz++) {
      document.links[zzz].href="";
    }  
}

function selLine(line){
    if(document.all!=null){
        var num = line.substr(1);
        if(num%2==0){
          document.all[line].style.backgroundColor="#F1E9B2";
          document.all[line].style.cursor = 'default';
        }
        else{
          document.all[line].style.backgroundColor="#F5F5F5";
          document.all[line].style.cursor = 'default';
        }
    }
}
function deselLine(line){
    if(document.all!=null){
        var num = line.substr(1);
        if(num%2==0){
          document.all[line].style.backgroundColor="#FCF3BA";
        }
        else{
          document.all[line].style.backgroundColor="#FFFFFF";
        }
    }
}


/**
 * Converts Unicode characters in string to Unicode character references.
 */
function toCharacterReferences(input) {
  var result = "";
  for(i = 0; i < input.length; i++) {
    ch = input.charAt(i);
    chc = input.charCodeAt(i);
    if(chc > 127 || ch == '<' || ch == '>' || ch == '/' || chc==13 || chc==10)
      ch = "&#"+chc+";";
    else if(ch == '&' && i < input.length + 1 && input.charAt(i+1) != '#') {
      ch = "&#"+chc+";";
    }
    result += ch;
  }
  return result;
}

/**
 * Converts Unicode characters in string from Unicode character references.
 */
function fromCharacterReferences(input) {
  var result = "";
  var crcode;
  var num;
  var endpos;
  var temp;
  for (i=0;i<input.length;i++) {
    crcode = input.substr(i,2);
    if (crcode=="&#") {
      endpos = input.indexOf(";",i);
      if (endpos>0) {
        temp = input.substring(i+2,endpos);
        result += String.fromCharCode(temp);
        i=endpos;
      } else {
        result += input.substr(i,1);
      }
    } else {
      result += input.substr(i,1);
    }
  }
  return result;
}

function setBackCommand(cmd,custom1,custom2,custom3,custom4,custom5,account) {
  var frm = document.forms["PostLink"];
  frm.back_command.value = cmd;
  if (custom1!=null) frm.back_custom1.value = custom1;
  if (custom2!=null) frm.back_custom2.value = custom2;
  if (custom3!=null) frm.back_custom3.value = custom3;
  if (custom4!=null) frm.back_custom4.value = custom4;
  if (custom5!=null) frm.back_custom5.value = custom5;
  if (account!=null) frm.back_account.value = account;
}

function post(cmd,custom1,custom2,custom3,custom4,custom5,account,extension) {
  var frm = document.forms["PostLink"];
  frm.command.value = cmd;
  frm.target="";
  if (custom1!=null) frm.custom1.value = custom1;
  if (custom2!=null) frm.custom2.value = custom2;
  if (custom3!=null) frm.custom3.value = custom3;
  if (custom4!=null) frm.custom4.value = custom4;
  if (custom5!=null) frm.custom5.value = custom5;
  if (account!=null) frm.account.value = account;
  if (extension!=null) frm.extension.value = extension;
  frm.submit();
}

function sort(cmd,sorting,account) {
  var frm = document.forms["SortLink"];
  frm.command.value = cmd;
    frm.target="";
  if (sorting!=null) frm.sorting.value = sorting;
  if (account!=null) frm.account.value = account;
  frm.submit();
}


function popup(command,width,height,left,top,custom1,custom2,custom3,custom4,custom5) {
  window.status='';
  var win = window.open('blank.htm','','left='+left+',top='+top+',height='+height+',width='+width+',toolbar=no,status=no,scrollbars');
  win.document.close();
  win.document.open();
  win.document.writeln("<HTML>");
  win.document.writeln("<HEAD>");
  win.document.writeln("</HEAD>");
  win.document.writeln("<BODY>...");
  win.document.writeln("<h1>custom1 :-)</h1>");
  win.document.writeln("</body></html>");
}

function popup.PUVODNI(command,width,height,left,top,custom1,custom2,custom3,custom4,custom5) {
  window.status='';
  var win = window.open('blank.htm','popupIBS','left='+left+',top='+top+',height='+height+',width='+width+',toolbar=no,status=no,scrollbars');
  win.document.close();
  win.document.open();
  win.document.writeln("<HTML>");
  win.document.writeln("<HEAD>");
  win.document.writeln("</HEAD>");
  win.document.writeln("<BODY>...");
  win.document.writeln("<FORM name='postLink' method='POST' action='"+window.document.location+"'>");
  win.document.writeln("<INPUT type='hidden' name='command' value='"+command+"'>");
  if (custom1!=null) win.document.writeln("<INPUT type='hidden' name='custom1' value='"+custom1+"'>");
  if (custom2!=null) win.document.writeln("<INPUT type='hidden' name='custom2' value='"+custom2+"'>");
  if (custom3!=null) win.document.writeln("<INPUT type='hidden' name='custom3' value='"+custom3+"'>");
  if (custom4!=null) win.document.writeln("<INPUT type='hidden' name='custom4' value='"+custom4+"'>");
  if (custom5!=null) win.document.writeln("<INPUT type='hidden' name='custom5' value='"+custom5+"'>");
  win.document.writeln("</FORM>");
  win.document.writeln("<SCRIPT LANGUAGE='JavaScript'>");
  win.focus();
  win.document.writeln("document.postLink.submit();");
  win.document.writeln("<\/SCRIPT>\n");
  win.document.writeln("</BODY>");
}

function printPopup(pcommand,pcustom1,pcustom2,pcustom3,pcustom4,pcustom5) {
  window.status='';
  var win = window.open('blank.htm','','left=20,top=20,height=500,width=700,toolbar=no,status=no,scrollbars');
  win.document.writeln("<HTML>");
  win.document.writeln("<HEAD>");
  win.document.writeln("</HEAD>");
  win.document.writeln("<BODY>...");
  win.document.writeln("<FORM name='postLink' method='POST' action='"+window.document.location+"'>");
  win.document.writeln("<INPUT type='hidden' name='command' value='"+pcommand+"'>");
  if (pcustom1!=null) win.document.writeln("<INPUT type='hidden' name='custom1' value='"+pcustom1+"'>");
  if (pcustom2!=null) win.document.writeln("<INPUT type='hidden' name='custom2' value='"+pcustom2+"'>");
  if (pcustom3!=null) win.document.writeln("<INPUT type='hidden' name='custom3' value='"+pcustom3+"'>");
  if (pcustom4!=null) win.document.writeln("<INPUT type='hidden' name='custom4' value='"+pcustom4+"'>");
  if (pcustom5!=null) win.document.writeln("<INPUT type='hidden' name='custom5' value='"+pcustom5+"'>");
  win.document.writeln("</FORM>");
  win.document.writeln("<SCRIPT LANGUAGE='JavaScript'>");
  win.document.writeln("document.postLink.submit();");
  win.document.writeln("<\/SCRIPT>\n");
  win.document.writeln("</BODY>");
}

function postPopup(ppcmd,ppcustom1,ppcustom2,ppcustom3,ppcustom4,ppcustom5,ppaccount) {
  var frm = document.forms["PostLink"];
  frm.command.value = ppcmd;
  frm.target="_blank";
  if (ppcustom1!=null) frm.custom1.value = ppcustom1;
  if (ppcustom2!=null) frm.custom2.value = ppcustom2;
  if (ppcustom3!=null) frm.custom3.value = ppcustom3;
  if (ppcustom4!=null) frm.custom4.value = ppcustom4;
  if (ppcustom5!=null) frm.custom5.value = ppcustom5;
  if (ppaccount!=null) frm.account.value = ppaccount;
  frm.submit();
}

function checkSecto(){
  //s = new String(document.Secto);
  //alert(document.Secto.eval);
  //alert(document.Secto.type);
  //if (s.indexOf("bsc.security.Secto")!=0){
  //  alert(fromCharacterReferences("Secto not loaded."));
  //  return false;
  //}
  return true;
}

function openErrorWindow(title, message) {
  window.status='';
  var width = 400;
  var height = 150;
  var left = (screen.width - width)/2;
  var top = (screen.height - height)/2;
  var win = window.open('blank.htm','errormessage','left='+left+',top='+top+',height='+height+',width='+width+',toolbar=no,status=no,scrollbars');
  win.document.writeln("<HTML><HEAD><TITLE>"+title+"</TITLE>");
  win.document.writeln("<LINK REL='stylesheet' HREF='logon.css' TYPE='text/css'>");
  win.document.writeln("</HEAD><BODY><TABLE>");
  win.document.writeln("<TR><TD align='left' valign='top' rowspan='2' width='60' height='140'>&nbsp;</TD><TD height='25%'><FONT class='error'>"+title+"</FONT></TD></TR>");
  win.document.writeln("<TR><TD align='left' valign='top' height='75%'><FONT class='errorMessage'>"+message+"</FONT></TD></TR>");
  win.document.writeln("</TABLE></BODY></HTML>");
  win.document.close();
}

function openMessageWindow(message) {
  window.status='';
  var width = 400;
  var height = 150;
  var left = (screen.width - width)/2;
  var top = (screen.height - height)/2;
  var win = window.open('blank.htm','message','left='+left+',top='+top+',height='+height+',width='+width+',toolbar=no,status=no,scrollbars');
  win.document.writeln("<HTML><HEAD><TITLE></TITLE>");
  win.document.writeln("<LINK REL='stylesheet' HREF='logon.css' TYPE='text/css'>");
  win.document.writeln("</HEAD><BODY>");
  win.document.writeln("<FONT class='formData'>"+message+"</FONT>");
  win.document.writeln("</BODY></HTML>");
  win.document.close();
}

function fileDialog(title,mode,formName,fieldName) {
  var fn = document.Secto['fileFromDialog'](title,mode);
  if (fn==null || fn=='' || fn=='nullnull') return;
  var textField = document.forms[formName].elements[fieldName];
  textField.value = fn;
}

function help(url) {
  window.status='';
  var win = window.open(url,'','left=20,top=20,height=480,width=640,toolbar=no,status=yes,scrollbars,resizable');
}

/**
 * Converts file path to URI
 */
function filePathToURI(file) {
  var uri = "file://";
  var i;
  var ch;
  for (i=0;i<file.length;i++) {
    ch = file.substr(i,1);
    if (ch=="\\") {
      ch = "/";
    }
    uri += (ch);
  }
  return uri;
}

function checkTextAreaSize(controlToCheck, maxSize){
  if(controlToCheck.value.length>maxSize){
    controlToCheck.value = controlToCheck.value.substring(0, maxSize);
  }
}

function addExt(ext, field) {
  if (field == null || field.value == '' || field.value.indexOf(ext) >= 0) {
    return;
  }
  field.value = field.value + ext;
}

function checkStringCharSet(value, charset) {
  for(i = 0; i < value.length; i++) {
    if (charset.indexOf(value.charAt(i)) < 0) {
      return false;
    }
  }
  return true;
}

function toFormatedNumber(input) {
  var decimalAlt = '.';
  var decimal = ',';
  var thousands = '';
  var nulls = '00';
  var pos;
  var decimals;
  var wholes;
  var amount = input.value
  amount = amount.replace(/ /gi, '');
  if((pos = amount.indexOf(decimalAlt)) != -1) {
    amount = amount.substring(0, pos) + decimal + amount.substring(pos+1, amount.length);
  }
  if((pos = amount.indexOf(decimal)) == -1) {
    amount += decimal;
  }
  decimals = amount.length - amount.indexOf(decimal) - 1;
  if(decimals > 2) {
    amount = amount.substring(0, amount.length - (decimals - 2));
  } else if(decimals < 2) {
    amount += nulls.substring(0, 2 - decimals);
  }
  wholes = amount.indexOf(decimal);
  while(wholes > 3) {
    amount = amount.substring(0, wholes-3) + thousands + amount.substring(wholes-3, amount.length);
    wholes -= 3;
  }
  input.value = amount;
}
